(function () { "use strict"; var app = angular.module("markingSchemeAdmin"); var templatePath = modulesSharedResourcesUrl + "Modules/MarkingSchemeAdmin/Views/"; //a directive that renders the list of marking schemes app.directive("markingSchemeList", [ "markingSchemeAdminDataContext", "markingSchemeAdminService", "common", "$location", "terminology", function ( markingSchemeAdminDataContext, markingSchemeAdminService, common, $location, terminology ) { return { restrict: "E", templateUrl: templatePath + "markingschemelist.html?version=270122", link: link, }; function link($scope, elem, attrs) { var getLogFn = common.logger.getLogFn; var logSuccess = getLogFn("markingScheme", "success"); var logError = getLogFn("markingScheme", "error"); $scope.terminology = terminology; // Define the status filters $scope.filters = [ { name: "Draft", value: 0 }, { name: "Published", value: 1 }, //{ name: 'Withdrawn', value: 2 }, { name: "Deleted", value: 3 }, { name: "Archived", value: 4 }, ]; $scope.filterValues = [0, 1]; // Apply the status filters $scope.applyStatusFilter = function (value, index) { return $scope.filterValues.indexOf(value.status) !== -1; }; // Get all the marking scemes for the user function getMarkingSchemes() { markingSchemeAdminDataContext .getAllMarkingSchemes() .then(function (data) { $scope.markingSchemes = data; $scope.markingSchemesLoaded = true; }); } getMarkingSchemes(); // function to archive a marking scheme $scope.archiveMarkingScheme = function (scheme) { markingSchemeAdminDataContext .archiveMarkingScheme(scheme) .then(function (data) { logSuccess("Grading system successfully archived"); getMarkingSchemes(); }); }; // function to unarchive a marking scheme $scope.unarchiveMarkingScheme = function (scheme) { markingSchemeAdminDataContext .unarchiveMarkingScheme(scheme) .then(function (data) { logSuccess("Grading system successfully unarchived"); getMarkingSchemes(); }); }; // function to delete a marking scheme $scope.deleteMarkingScheme = function (scheme) { markingSchemeAdminDataContext .deleteMarkingScheme(scheme) .then(function (data) { logSuccess("Grading system successfully deleted"); getMarkingSchemes(); }); }; // function to manage a marking scheme $scope.manageMarkingScheme = function (scheme) { if (scheme) { markingSchemeAdminService.setCurrentMarkingScheme(scheme); $location.path("/managescheme"); } else { markingSchemeAdminService.setCurrentMarkingScheme(null); $location.path("/managescheme"); } }; $scope.importMarkingScheme = function () { $location.path("/importscheme"); }; $scope.exportMarkingScheme = function (scheme) { markingSchemeAdminDataContext .exportMarkingScheme(scheme) .then(function (data) { logSuccess("Grading system successfully exported"); download(data, scheme.name); }); }; const download = (exportObj, exportName) => { var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj)); var downloadAnchorNode = document.createElement("a"); downloadAnchorNode.setAttribute("href", dataStr); downloadAnchorNode.setAttribute("download", exportName + ".json"); document.body.appendChild(downloadAnchorNode); // required for firefox downloadAnchorNode.click(); downloadAnchorNode.remove(); }; } }, ]); //a directive to manage a marking scheme app.directive("manageMarkingScheme", [ "markingSchemeAdminDataContext", "markingSchemeAdminService", "taskAdminDataContext", "fileUpload", "common", "$location", "virusScanModalService", "terminology", function ( markingSchemeAdminDataContext, markingSchemeAdminService, taskAdminDataContext, fileUpload, common, $location, virusScanModalService, terminology ) { return { restrict: "E", templateUrl: templatePath + "managemarkingscheme.html?version=270122", link: link, }; function link($scope, elem, attrs) { var getLogFn = common.logger.getLogFn; var logSuccess = getLogFn("markingScheme", "success"); var logError = getLogFn("markingScheme", "error"); var initial = true; $scope.terminology = terminology; $scope.disableSort = false; $scope.newValue = {}; $scope.changesMadeToScheme = false; //get the selected template if exists in the service $scope.currentScheme = markingSchemeAdminService.getCurrentMarkingScheme(); // Get the scheme types from the service $scope.schemeTypes = markingSchemeAdminService.getSchemeTypes(); //if it doesn't exist use the service to create a blank scheme if (!$scope.currentScheme) { $scope.currentScheme = markingSchemeAdminService.createBlankMarkingScheme(); } //if ($scope.currentScheme.id) { // taskAdminDataContext // .isMarkingSchemeAssociatedToTask($scope.currentScheme) // .then(function (data) { // $scope.checkedAssociation = true; // if (data) { // $scope.disableSort = true; // $scope.markingSchemeInUse = true; // } // }); //} else { // $scope.checkedAssociation = true; //} $scope.checkedAssociation = true; if ($scope.currentScheme.status === 2) $scope.disableSort = true; // Watch for changes made to the marking scheme $scope.$watchGroup( "currentScheme", function (newValue, oldValue) { if (newValue !== oldValue) { $scope.changesMadeToScheme = true; } }, true ); // Watch for changes made to the marking scheme values $scope.$watchCollection( "currentScheme.markingSchemeValues", function (newValue, oldValue) { if (newValue !== oldValue) { $scope.changesMadeToScheme = true; } }, true ); // Because the attachments array can contain file objects which cannot be used with angular.copy // we have to stringify the model and watch for changes $scope.$watch( function () { return JSON.stringify($scope.currentScheme.attachments); }, function () { if (initial) { initial = false; } else { $scope.changesMadeToScheme = true; } } ); // function to get back to the marking schemes list $scope.goBack = function () { $location.path("/schemes"); }; // function to create or save a marking scheme $scope.saveMarkingScheme = function () { $scope.saveDisabled = true; $scope.numberOfFiles = 0; if (!$scope.currentScheme.id) { markingSchemeAdminDataContext .createMarkingScheme($scope.currentScheme) .then(function (data) { logSuccess("Grading system successfully created"); $location.path("/schemes"); }) .catch(function (errorData) { if (errorData === "Virus scan positive.") { virusScanModalService.virusModal(); } else { logError("An error occured saving the item."); } $scope.isSaving = false; $scope.saveDisabled = false; }); } else { markingSchemeAdminDataContext .updateMarkingScheme($scope.currentScheme) .then(function (data) { logSuccess("Grading system successfully updated"); $location.path("/schemes"); }) .catch(function (errorData) { if (errorData === "Virus scan positive.") { virusScanModalService.virusModal(); } else { logError("An error occured saving the item."); } $scope.isSaving = false; $scope.saveDisabled = false; }); } }; // function to publish a marking scheme $scope.publishMarkingScheme = function () { $scope.numberOfFiles = 0; if (!$scope.uploading) { var oldStatus = $scope.currentScheme.status; $scope.currentScheme.status = 1; if ($scope.currentScheme.id) { markingSchemeAdminDataContext .updateMarkingScheme($scope.currentScheme) .then(function (data) { logSuccess("Grading system successfully published"); $location.path("/schemes"); }) .catch(function (errorData) { if (errorData === "Virus scan positive.") { $scope.currentScheme.status = oldStatus; virusScanModalService.virusModal(); } else { logError("An error occured saving the item."); } $scope.isSaving = false; }); } else { markingSchemeAdminDataContext .createMarkingScheme($scope.currentScheme) .then(function (data) { logSuccess( "Grading system successfully created and published" ); $location.path("/schemes"); }) .catch(function (errorData) { if (errorData === "Virus scan positive.") { $scope.currentScheme.status = oldStatus; virusScanModalService.virusModal(); } else { logError("An error occured saving the item."); } $scope.isSaving = false; }); } } }; // function to withdraw a marking scheme $scope.withdrawMarkingScheme = function () { $scope.currentScheme.status = 2; markingSchemeAdminDataContext .updateMarkingScheme($scope.currentScheme) .then(function (data) { logSuccess("Grading system successfully withdrawn"); $location.path("/schemes"); }); }; // Function to add a new value to the marking schemes values $scope.addNewSchemeValue = function (form) { $scope.currentScheme.markingSchemeValues.push({ name: $scope.newValue.newSchemeValueName, description: $scope.newValue.newSchemeValueDescription, ordinal: $scope.currentScheme.markingSchemeValues.length, colour: $scope.newValue.newSchemeValueColour, numericValue: $scope.newValue.newSchemeValueNumericValue, }); $scope.newValue.newSchemeValueName = ""; $scope.newValue.newSchemeValueDescription = ""; $scope.newValue.newSchemeValueColour = ""; $scope.newValue.newSchemeValueNumericValue = ""; form.$setPristine(); }; $scope.setValueOrder = function (evt) { for (i in $scope.currentScheme.markingSchemeValues) { if ( $scope.currentScheme.markingSchemeValues[i].ordinal === evt.newIndex ) { $scope.currentScheme.markingSchemeValues[i].ordinal = evt.oldIndex; } if ( $scope.currentScheme.markingSchemeValues[i].name === evt.model.name ) { $scope.currentScheme.markingSchemeValues[i].ordinal = evt.newIndex; } } }; // Constantly check the marking schemes validility $scope.checkSchemeValidility = function (form) { if ( $scope.currentScheme.name === "" || $scope.currentScheme.description === "" ) { return true; } else if ($scope.currentScheme.type == null) { return true; } else if ( $scope.currentScheme.type === 1 && $scope.currentScheme.markingSchemeValues.length === 0 ) { return true; } else if ($scope.currentScheme.type === 0) { if ( $scope.currentScheme.rangeType === 0 && ($scope.currentScheme.numericRangeLower == null || $scope.currentScheme.numericRangeUpper == null) ) { return true; } else if ( $scope.currentScheme.rangeType === 1 && (!$scope.currentScheme.alphabeticRangeLower || !$scope.currentScheme.alphabeticRangeUpper) ) { return true; } else { return false; } } else { return false; } }; $scope.maxLengthCheck = function (object) { if (object > 10000) $scope.currentScheme.numericRangeLower = 9999; if (object < 0) $scope.currentScheme.numericRangeLower = 0; if (object % 1 !== 0) $scope.currentScheme.numericRangeLower = Math.round(object * 1) / 1; if (object >= $scope.currentScheme.numericRangeUpper) $scope.currentScheme.numericRangeUpper = object + 1; }; $scope.maxLengthCheck2 = function (object) { if (object > 10000) $scope.currentScheme.numericRangeUpper = 10000; if (object < 0) $scope.currentScheme.numericRangeUpper = 1; if (object % 1 !== 0) $scope.currentScheme.numericRangeUpper = Math.round(object * 1) / 1; if (object <= $scope.currentScheme.numericRangeLower) $scope.currentScheme.numericRangeLower = object - 1; }; $scope.checkAlphabeticalValue = function () { if ( $scope.currentScheme.alphabeticRangeLower && $scope.currentScheme.alphabeticRangeUpper ) { if ( $scope.currentScheme.alphabeticRangeLower.toLowerCase() === $scope.currentScheme.alphabeticRangeUpper.toLowerCase() ) { $scope.currentScheme.alphabeticRangeUpper = ""; } } }; $scope.checkAlphabeticalOrderLower = function () { if ($scope.currentScheme.alphabeticRangeUpper != null) { if ( $scope.currentScheme.alphabeticRangeLower .toLowerCase() .charCodeAt(0) >= $scope.currentScheme.alphabeticRangeUpper .toLowerCase() .charCodeAt(0) ) { var nextLetter = $scope.currentScheme.alphabeticRangeLower.charCodeAt(0) + 1; if (isLetter(String.fromCharCode(nextLetter))) { $scope.currentScheme.alphabeticRangeUpper = String.fromCharCode(nextLetter); } else { $scope.currentScheme.alphabeticRangeLower = ""; } } } }; $scope.checkAlphabeticalOrderHigher = function () { if ( $scope.currentScheme.alphabeticRangeUpper .toLowerCase() .charCodeAt(0) <= $scope.currentScheme.alphabeticRangeLower .toLowerCase() .charCodeAt(0) ) { var previousLetter = $scope.currentScheme.alphabeticRangeUpper.charCodeAt(0) - 1; if (isLetter(String.fromCharCode(previousLetter))) { $scope.currentScheme.alphabeticRangeLower = String.fromCharCode(previousLetter); } else { $scope.currentScheme.alphabeticRangeUpper = ""; } } }; function isLetter(str) { return str.length === 1 && str.match(/[a-z]/i); } // Called when image file has been selected $scope.onFileSelect = function (files) { if (files) { $scope.errorMessage = null; $scope.file = files[0]; $scope.file.fileName = $scope.file.name; if ($scope.file.size > 40000000) { $scope.errorMessage = "File size is too large, max 40MB"; return; } var workingAttachment = { file: $scope.file, name: "", description: "", fileExtension: $scope.file.type, fileUrl: "", fileType: 2, fileName: $scope.file.name, visibility: true, }; var reader = new FileReader(); reader.onload = function (e) { workingAttachment.fileAsBase64 = reader.result; $scope.currentScheme.attachments.unshift(workingAttachment); $scope.$apply(); }; reader.readAsDataURL($scope.file); } }; $scope.removeAttachment = function (file) { for (i in $scope.currentScheme.attachments) { if (file.id) { if ($scope.currentScheme.attachments[i].id === file.id) { $scope.currentScheme.attachments.splice(i, 1); return; } } else { if ($scope.currentScheme.attachments[i].file) { if ( $scope.currentScheme.attachments[i].file.lastModified === file.file.lastModified ) { $scope.currentScheme.attachments.splice(i, 1); return; } } } } }; $scope.idealTextColor = function (bgColor) { if ( bgColor === "transparent" || bgColor === null || bgColor === undefined ) return ""; var nThreshold = 105; var components = getRgbComponents(bgColor); var bgDelta = components.R * 0.299 + components.G * 0.587 + components.B * 0.114; return 255 - bgDelta < nThreshold ? "#000000" : "#ffffff"; }; function getRgbComponents(color) { var r = color.substring(1, 3); var g = color.substring(3, 5); var b = color.substring(5, 7); return { R: parseInt(r, 16), G: parseInt(g, 16), B: parseInt(b, 16), }; } } }, ]); //a directive to import a marking scheme app.directive("importMarkingScheme", [ "markingSchemeAdminDataContext", "common", "$location", "terminology", function (markingSchemeAdminDataContext, common, $location, terminology) { return { restrict: "E", templateUrl: templatePath + "importmarkingscheme.html?version=270122", link: link, }; function link($scope, elem, attrs) { var getLogFn = common.logger.getLogFn; var logSuccess = getLogFn("markingScheme", "success"); var logError = getLogFn("markingScheme", "error"); $scope.terminology = terminology; // function to get back to the marking schemes list $scope.goBack = function () { $location.path("/schemes"); }; $scope.onFileSelect = function (files) { if (files) { $scope.errorMessage = null; $scope.file = files[0]; $scope.file.fileName = $scope.file.name; if ($scope.file.size > 40000000) { $scope.errorMessage = "File size is too large, max 40MB"; return; } //Check that the file contains a grading scheme var invalidGradingFileMessage = "The file must contain " + terminology.grading + " information"; var fileReader = new FileReader(); if (fileReader != null) { fileReader.readAsText($scope.file); fileReader.onload = function () { if (!fileReader.result.includes('{"scheme"')) { $scope.errorMessage = invalidGradingFileMessage return; } }; fileReader.onerror = function () { $scope.errorMessage = invalidGradingFileMessage; return; }; } markingSchemeAdminDataContext .importMarkingScheme($scope.file) .then(function (data) { logSuccess("Grading system successfully imported"); $location.path("/schemes"); }) .catch(function (errorData) { if (errorData === "Virus scan positive.") { virusScanModalService.virusModal(); } else { logError("An error occured saving the item."); } $scope.isSaving = false; }); } }; } }, ]); })();