--- /dev/null
+(function() {
+ "use strict";
+
+ angular.module("nero.app.dashboard")
+ .directive("scheduler", schedulerDirective);
+
+ function schedulerDirective() {
+ return {
+ restrict: "E",
+ scope: {
+ isHeadMaster: "="
+ },
+ templateUrl: contextPath + "/js/scheduler/scheduler.html?t="+dashboardTimestamp,
+ controller: SchedulerController,
+ controllerAs: "SchedulerCtrl",
+ bindToController:true
+ };
+ }
+
+ SchedulerController.$inject = ["$uibModal", "schedulerService", "toastr", "$scope", "$window"];
+
+ function SchedulerController($uibModal, schedulerService, toastr, $scope, $window) {
+
+ var vm = this;
+
+
+ // User's role
+ vm.isStudent;
+ vm.isParent;
+ vm.isTeacher;
+
+ // Connected userId
+ vm.userId;
+
+ // UserId used to fetch scheduler 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.getEvent = getEvents;
+ vm.previousDay = previousDay;
+ vm.nextDay = nextDay;
+ vm.getHour = getHour;
+ vm.getCurrentDate = getCurrentDate;
+ vm.getChildren = getChildren;
+ vm.childSelected = childSelected;
+ vm.redirectToUrl = redirectToUrl;
+
+ // Init date with today's date
+ vm.currentDate = moment();
+ vm.currentDateStr = vm.currentDate.format("DD/MM/YYYY");
+
+ init();
+
+ // Initialize
+ function init() {
+ vm.currentDate = moment();
+
+ vm.myPromise = schedulerService.getUserInfos()
+ .then(function(data) {
+ vm.userId = data.userId;
+ vm.targetUserId = vm.userId;
+ vm.isTeacher = data.isTeacher;
+ vm.isParent = data.isParent;
+ vm.isStudent = data.isStudent;
+
+ // Teachers and students => get their events
+ if (vm.isTeacher || vm.isStudent) {
+ getEvents(vm.targetUserId, vm.currentDate);
+ }
+
+ // Parents => get children first
+ if (vm.isParent) {
+ getChildren(vm.userId);
+ }
+ })
+ ["catch"](function(message){
+ toastr.error(message);
+ });
+ }
+
+
+ /*
+ * Redirection
+ */
+ function redirectToUrl(targetUrl) {
+ if (targetUrl != undefined && targetUrl != "") {
+ $window.location.href = targetUrl;
+ }
+ }
+
+
+ // Fetch scheduler events for given userId at given date
+ function getEvents(targetUserId, date) {
+
+ // Empty current event list
+ vm.eventList = [];
+
+ if (vm.isStudent || vm.isParent) {
+
+ schedulerService.getStudentSchedule(targetUserId, date.format("DD/MM/YYYY HH:mm"))
+ .then(function(data) {
+ vm.eventList = data.eventList;
+ })
+ ["catch"](function(message){
+ toastr.error(message);
+ });
+
+ }
+ else {
+ // Teacher
+ schedulerService.getTeacherSchedule(targetUserId, date.format("DD/MM/YYYY HH:mm"))
+ .then(function(data) {
+ vm.eventList = data.eventList;
+ })
+ ["catch"](function(message){
+ toastr.error(message);
+ });
+ }
+ //tmp
+ //vm.eventList = vm.customEventList;
+ }
+
+
+
+ function getChildren(parentUserId) {
+
+ schedulerService.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];
+ vm.targetUserId = vm.selectedChild.childId;
+ if (vm.childrenList.length > 1) {
+ vm.moreThanOneChild = true;
+ }
+ }
+ getEvents(vm.targetUserId, vm.currentDate);
+
+ })
+ ["catch"](function(message){
+ toastr.error(message);
+ });
+ }
+
+
+ // Child selected if more than 1
+ function childSelected(child) {
+ vm.selectedChild = child;
+ vm.targetUserId = child.childId;
+ getEvents(vm.targetUserId, vm.currentDate);
+ }
+
+ // Fetch events for the day before
+ function previousDay() {
+ vm.currentDate = moment(vm.currentDate).add(-1, 'days');
+ getEvents(vm.targetUserId, vm.currentDate);
+ }
+
+
+ // Fetch events for the day after
+ function nextDay() {
+ vm.currentDate = moment(vm.currentDate).add(1, 'days');
+ getEvents(vm.targetUserId, vm.currentDate);
+ }
+
+ // Extract the hour from the date
+ function getHour(sessionDate) {
+ return moment(sessionDate, "DD/MM/YYYY HH:mm").format("HH:mm");
+ }
+
+ // Format the current date
+ function getCurrentDate() {
+ moment.locale('fr');
+ return moment(vm.currentDate, "DD/MM/YYYY HH:mm").format("ddd D/MM/YY");
+ }
+
+ }
+})();