--- /dev/null
+(function() {
+ "use strict";
+
+ angular.module("nero.app.dashboard")
+ .directive("homeworkbanner", homeworkBannerDirective);
+
+ function homeworkBannerDirective() {
+ return {
+ restrict: "E",
+ scope: {
+ isHeadMaster: "="
+ },
+ templateUrl: contextPath + "/js/homework/homeworkBanner.html?t=" + dashboardTimestamp,
+ controller: HomeworkBannerController,
+ controllerAs: "HomeworkBannerCtrl",
+ bindToController: true
+ };
+ }
+
+ HomeworkBannerController.$inject = ["$uibModal", "homeworkService", "toastr", "$scope", "$window", "dashboardProperties", "$rootScope"];
+
+ function HomeworkBannerController($uibModal, homeworkService, toastr, $scope, $window, dashboardProperties, $rootScope) {
+
+ var vm = this;
+
+
+ // User's role
+ vm.isStudent;
+ vm.isParent;
+ vm.isTeacher;
+
+ // Connected userId
+ vm.userId;
+
+ // UserId used to fetch homework events (child id for parents)
+ vm.targetUserId;
+
+ // Children management
+ vm.childrenList = [];
+ vm.selectedChild;
+ vm.moreThanOneChild = false;
+
+ // The list of events
+ vm.eventList;
+
+ // Functions
+ vm.init = init;
+ vm.getChildren = getChildren;
+ vm.childSelected = childSelected;
+
+
+ // Init date with today's date
+ vm.currentDate = moment();
+ vm.currentDateStr = vm.currentDate.format("DD-MM-YYYY");
+ vm.currentMonth = "";
+
+
+
+ init();
+
+ // Initialize
+ function init() {
+ vm.currentDate = moment();
+
+ vm.myPromise = homeworkService.getUserInfos()
+ .then(function(data) {
+ vm.userId = data.userId;
+ vm.targetUserId = vm.userId;
+ vm.isTeacher = data.isTeacher;
+ vm.isParent = data.isParent;
+ vm.isStudent = data.isStudent;
+
+ // Parents => get children first
+ if (vm.isParent) {
+ getChildren(vm.userId);
+ } else {
+ dashboardProperties.setTargetUserId(vm.userId);
+ $rootScope.$broadcast('refresh-homework', {});
+
+ }
+ })["catch"](function(message) {
+ toastr.error(message);
+ });
+ }
+
+
+ function getChildren(parentUserId) {
+
+ homeworkService.getChildren(parentUserId)
+ .then(function(data) {
+ vm.childrenList = data.childrenList;
+
+ // Default child is the first one
+ if (vm.childrenList.length > 0) {
+ vm.selectedChild = vm.childrenList[0];
+ dashboardProperties.setTargetUserId(vm.selectedChild.childId);
+ $rootScope.$broadcast('refresh-homework', {});
+
+ if (vm.childrenList.length > 1) {
+ vm.moreThanOneChild = true;
+ }
+ }
+
+ })["catch"](function(message) {
+ toastr.error(message);
+ });
+ }
+
+ // Child selected if more than 1
+ function childSelected(child) {
+ vm.selectedChild = child;
+ dashboardProperties.setTargetUserId(child.childId);
+ $rootScope.$broadcast('refresh-homework', {});
+ }
+
+ setCurrentMonth();
+
+ function setCurrentMonth(month) {
+ if(!month){
+ vm.currentMonth = dashboardProperties.getCurrentMonth();
+ }else {
+ vm.currentMonth = month;
+ }
+ angular.element("#homeworkBannerCurrentMonth").html(dashboardProperties.getCurrentMonth());
+ }
+
+ $scope.$on('refresh-currentMonth', function(event, parameters) {
+ setCurrentMonth(parameters.currentMonth);
+
+ });
+
+
+ }
+})();