/// AIA.controller("LabExercController", ["$scope", "$rootScope", "pages", "$log", '$http', 'DataService', '$filter', '$location', '$document', '$sce', "$compile", "$location", "LabExerciseService", function ($scope, $rootScope, pages, log, $http, DataService, $filter, $location, $document, $sce, $compile, $location, LabExerciseService) { $scope.$sce = $sce; $scope.LabExerciseName; $scope.LabExerciseQuiz = null; $scope.TotalNumberofQuiz = "0"; $scope.quiznumber = 1; $scope.activityTitle = ""; $scope.Title = ""; $scope.Imagepath = ""; $scope.DraggedList = []; $scope.AnswerList = []; $scope.UserAnswersForAllQuestions = []; $scope.LabExercise = []; $scope.dragableId = ""; $scope.blReviewAttempt = false; $scope.$on('$viewContentLoaded', function (event) { // code that will be executed ... // every time this view is loaded //get current path var currentURL = $location.path(); var selectedModuleName = ''; //set module title angular.forEach($rootScope.userModules, function (value, key) { if (value.slug === currentURL.replace('/', '')) { selectedModuleName = value.name; } $rootScope.currentActiveModuleTitle = selectedModuleName; }) $scope.showme = false; $rootScope.currentActiveModuleTitle = pages[8].name; $scope.getLabExerciseModules(); $scope.scroll(); $document.find('div#divoptions div.thumbnail').draggable(); //$document.find("#droppable").droppable({ // drop: function (event, ui) { // $(this) // .find("img") // .html("Dropped!"); // } //}); }); // $scope.showme = false; $scope.IsVisible = function () { // $scope.scroll(); } $scope.scroll = function () { // $window.scrollTo(0, 0); $("html,body").scrollTop(0); //alert("scroll"); } // $rootScope.currentActiveModuleTitle = pages[8].Name; //to get all lab exercise modules from json files $scope.getLabExerciseModules = function () { //debugger; $scope.LabExerciseModules1 = null; var labExerciseModulePath = '~/../content/data/json/le/labexercise.json'; DataService.getAnotherJson(labExerciseModulePath).then( function (result) { //debugger; //alert(JSON.stringify(result)); $scope.LabExerciseModules1 = result; }, function (error) { console.log(error.statusText) } ) } $scope.getLabExerciseTitle = function (title) { $rootScope.menuLabExer = 1; if (title != null) { $scope.LabExerciseQuiz = title; $scope.LabExerciseName = title.Topic; $location.url("/lab-exercises-detail?labexercise=" + (title.Slug || "")); } } $scope.GetQuizByTopic = function () { var keywords = $location.search(); $scope.LabExerciseName = keywords.labexercise; $scope.LabExerciseModules = null; $scope.LabExerciseBoxes = null; //alert($scope.LabExerciseName) var labExerciseModulePath = '~/../content/data/json/le/' + $scope.LabExerciseName + '.json'; DataService.getAnotherJson(labExerciseModulePath).then( function (result) { $scope.LabExData = result; // alert(JSON.stringify(result.LabExercise)); $.each(result.LabExercise, function (index, value) { $scope.LabExerciseName = result.LabExercise[index].Topic; if (result.LabExercise[index].Slug == keywords.labexercise) { $scope.TotalNumberofQuiz = result.LabExercise[index].NumberofQuestions; $.each(result.LabExercise[index].Questions, function (index1, value1) { if (result.LabExercise[index].Questions[index1].Number == $scope.quiznumber) { if (result.LabExercise[index].Questions[index1].ImagePath == "") { $("#imgblock").css("display", "none"); //$("#imgblock").remove(); $scope.IsVisible = false; $("#textblock").css("display", "block"); $scope.LabExerciseModules = result.LabExercise[index].Questions[index1]; $scope.deliberatelyTrustDangerousSnippet = function (html) { //var htmltag= $compile(html)($scope); return $sce.trustAsHtml(html); }; $scope.activityTitle = result.LabExercise[index].Questions[index1].activityTitle; $scope.Title = result.LabExercise[index].Questions[index1].Title; } else { $("#imgblock").css("display", "block"); $scope.IsVisible = true; $("#textblock").css("display", "none"); // $("#textblock").remove(); $scope.LabExerciseModules = result.LabExercise[index].Questions[index1]; $scope.activityTitle = result.LabExercise[index].Questions[index1].activityTitle; $scope.Title = result.LabExercise[index].Questions[index1].Title; $scope.Imagepath = keywords.labexercise + "/" + result.LabExercise[index].Questions[index1].ImagePath; } $scope.ShowHideDiv(result.LabExercise[index].Questions[index1].Options[0].textalign); } }); //$scope.quiznumber++; } }); //$scope.ShowHideDiv(); }, function (error) { console.log(error.statusText) } ) } $scope.nextQuiz = function () { //need to save all questions till the current question number when user clicks on save if ($scope.dragableId!="") { $scope.UserAnswersForAllQuestions.push({'QuestionNo':$scope.quiznumber,'UserAnswer':$scope.AnswerList}) } else { //need to anattampted question means user ahs not dragged, dropped options so save blank $scope.UserAnswersForAllQuestions.push({ 'QuestionNo': $scope.quiznumber, 'UserAnswer': "" }) } $scope.dragableId = ""; if ($scope.quiznumber == $scope.TotalNumberofQuiz) return; else $scope.quiznumber++; $scope.resetQuiz(); $scope.ShowHideDiv(); } $scope.prevQuiz = function () { $scope.dragableId = ""; if ($scope.quiznumber == 1) return; else $scope.quiznumber--; $scope.resetQuiz(); $scope.ShowHideDiv(); } $scope.handleDragStart = function (e) { this.style.opacity = '0.4'; e.dataTransfer.setData('text/plain', this.innerHTML); $scope.dragableId = $(this).attr("Id"); }; $scope.handleDragEnd = function (e) { this.style.opacity = '1.0'; }; $scope.handleDrop = function (e) { e.preventDefault(); e.stopPropagation(); var x = $("#droppable").offset(); //alert(x.left + "," + $("#droppable").clientWidth + "," + x.top); var id = $(this).attr("id"); var keywords = $location.search(); $scope.labExercideIdentifier = (keywords.labexercise.replace('.json', '')).split('dat_')[1]; var dataText = e.dataTransfer.getData('text/plain'); if (id == "divoptions" || id == "divleft" || id == "divright") { var item = $('#' + $scope.dragableId).html(); var rindex = $scope.DraggedList.indexOf(item); var labExerciseModulePath = '~/../content/data/json/le/' + keywords.labexercise + '.json'; DataService.getAnotherJson(labExerciseModulePath).then( function (result) { $.each(result.LabExercise, function (index, value) { if (result.LabExercise[index].Slug == keywords.labexercise) { $.each(result.LabExercise[index].Questions, function (index1, value1) { if (result.LabExercise[index].Questions[index1].Number == $scope.quiznumber) { var Options = result.LabExercise[index].Questions[index1].Options; $scope.maxScore = Options.length; $.each(Options, function (inx, value2) { if (Options[inx].OptionTitle == item) { if ($scope.DraggedList != null) { $.each($scope.DraggedList, function (inx1, value3) { // $scope.AnswerList.push({ "optionName": id.split('-')[1], "AnswerName": $scope.dragableId.split('-')[1] }); var balnkBox=id.split('-')[1]; $scope.UserAttempt.push({balnkBox :+ $scope.dragableId.split('-')[1] }); if ($scope.DraggedList[inx1].Value == item) { $scope.DraggedList.splice(inx1, 1); $('#block-' + Options[inx].OptionNumber).css("display", "block"); $('#blockans-' + Options[inx].OptionNumber).css("background-color", "transparent !important"); } }); } } }); } }); } }); }, function (error) { console.log(error.statusText) } ) } else { var labExerciseModulePath = '~/../content/data/json/le/' + keywords.labexercise + '.json'; DataService.getAnotherJson(labExerciseModulePath).then( function (result) { $.each(result.LabExercise, function (index, value) { if (result.LabExercise[index].Slug == keywords.labexercise) { $.each(result.LabExercise[index].Questions, function (index1, value1) { if (result.LabExercise[index].Questions[index1].Number == $scope.quiznumber) { var Options = result.LabExercise[index].Questions[index1].OptionBox; $.each(Options, function (inx, value2) { if (Options[inx].BoxName == id.split('-')[1]) { if ($scope.DraggedList != null) { $.each($scope.DraggedList, function (inx1, value3) { if ($scope.DraggedList[inx1].Value == dataText) { $scope.DraggedList.splice(inx1, 1); } }); } $scope.AnswerList.push({ "optionName": id.split('-')[1], "AnswerName": $scope.dragableId.split('-')[1] }); $scope.DraggedList.push({ "id": id.split('-')[1], "optionName": id.split('-')[1], "Value": dataText, "topcoord": Options[inx].topcoord, "leftcoord": Options[inx].leftcoord }); //$('#' + $scope.dragableId).remove(); $('#' + $scope.dragableId).css("display", "none"); $('#blockans-' + Options[inx].OptionNumber).css("background-color", "transparent !important"); //$('#' + id).remove(); } }); } }); } }); }, function (error) { console.log(error.statusText) } ) } $scope.$apply(); //alert($("#divoptions").css("display") + "," + $("#divoptions").html()); //if ($("#divoptions").css("display") == "block" && $("#divoptions").html()=="") //{ // $("#divoptions").html("



") //} //alert(JSON.stringify($scope.AnswerList)); }; $scope.handleDragOver = function (e) { e.preventDefault(); // Necessary. Allows us to drop. e.dataTransfer.dropEffect = 'move'; // See the section on the DataTransfer object. //return false; }; $scope.resetQuiz = function () { $scope.DraggedList = []; $scope.GetQuizByTopic(); }; $scope.ReviewAttempt = function () { var keywords = $location.search(); var labExerciseModulePath = '~/../content/data/json/le/' + keywords.labexercise + '.json'; DataService.getAnotherJson(labExerciseModulePath).then( function (result) { //alert(result) $.each(result.LabExercise, function (index, value) { if (result.LabExercise[index].Slug == keywords.labexercise) { $.each(result.LabExercise[index].Questions, function (index1, value1) { if (result.LabExercise[index].Questions[index1].Number == $scope.quiznumber) { var correctResponse = result.LabExercise[index].Questions[index1].correctResponse; var Options = result.LabExercise[index].Questions[index1].Options; $.each(correctResponse, function (inx, value2) { $scope.OptionName = ""; $.each(Options, function (inx1, value3) { if (Options[inx1].OptionTitle == $('#blockans-' + correctResponse[inx].OptionBox).text()) { $scope.OptionName = Options[inx1].OptionNumber; } }); if (correctResponse[inx].Answer == $scope.OptionName) { blReviewAttempt = true; } else { blReviewAttempt = false; $('#blockans-' + correctResponse[inx].OptionBox).css("background", "#ff4e00"); } }); } }); } }); }, function (error) { console.log(error.statusText) } ) }; $scope.ShowHideDiv = function (status) { if (status == "bottom") { $("#divright").css("display", "none"); $("#divleft").css("display", "none"); $("#divoptions").css("display", "block"); } else { $("#divright").css("display", "block"); $("#divleft").css("display", "block"); $("#divoptions").css("display", "none"); } }; $scope.SaveAnswer = function () { //get user answers var userAnswers = []; var blockBoxes = $("div[id*='blockbox']"); var blockBoxesInTextBlock = $("#textblock").children(); var blockBoxLength = blockBoxes.length - blockBoxesInTextBlock.length; var blockAnswers = $("div[id*='blockans']"); for (var i = 0; i < blockAnswers.length; i++) { var id = (blockAnswers[i].id).split('-')[1]; userAnswers[id.replace('T','')] = blockAnswers[i].innerHTML; } var commaSeperatedUserAnswers = ''; for (var i = 1; i <=blockBoxLength; i++) { var userAns = userAnswers[i]; if(userAns!=undefined){ commaSeperatedUserAnswers +=userAns + ','; } else{ commaSeperatedUserAnswers +=''+','; } } //remove last comma commaSeperatedUserAnswers = commaSeperatedUserAnswers.replace(/,$/, ''); //get correct response $scope.correctResponse =''; $scope.correctResponseForSavingDatabase =''; var cr = new jinqJs() .from($scope.LabExData.LabExercise[0].Questions) .where('Number == ' + $scope.quiznumber) .select(); for (var i = 0; i < cr[0].correctResponse.length; i++) { var blankBox = cr[0].correctResponse[i].OptionBox; var options = cr[0].correctResponse[i].Answer; $scope.correctResponse += blankBox + ' ' + options + ','; $scope.correctResponseForSavingDatabase += parseInt(blankBox.replace('T',''))-1 + ':' + options + ','; } $scope.correctResponse = $scope.correctResponse.replace(/,$/, ''); $scope.correctResonseKeyValue = $scope.correctResponse.split(','); $scope.correctResponseForSavingDatabase = $scope.correctResponseForSavingDatabase.replace(/,$/, ''); // $scope.correctResponseForSavingDatabaseArray = $scope.correctResponseForSavingDatabase.split(','); //get dragItems var OptionBoxes = $("div[id*='block-']"); $scope.DragItems = ''; for (var i = 0; i < OptionBoxes.length; i++) { $scope.DragItems += OptionBoxes[i].id.replace('block-', '') + ',' + OptionBoxes[i].innerHTML + ';'; } $scope.DragItems = DragItems.replace(/;$/, ''); //get score $scope.LabExerciseAttemptedData = { userId: localStorage.getItem('loggedInUserDetails').id, //Attemptnumber: 1, labExerciseIdentifier: $scope.labExercideIdentifier, LastQuestion: $scope.quiznumber, TotalQuestions: $scope.TotalNumberofQuiz, MaxScore: $scope.maxScore, UserAnswer: commaSeperatedUserAnswers, QuestionNo: $scope.quiznumber, CorrectAnswer: JSON.stringify($scope.correctResponseForSavingDatabase), DragItems: $scope.DragItems, Score: 1 } var abc = $scope.AnswerList; //LabExerciseService.saveLabExerciseAttempt("superadmin", $scope.LabExerciseAttemptedData) LabExerciseService.saveLabExerciseAttempt($scope.LabExerciseAttemptedData) .then( function (result) { alert("Successfully updated"); }, function (error) { console.log(' Error in authentication = ' + error.statusText); alert("Error"); }); }; }] ); AIA.directive('draggable', function () { return { link: function ($scope, element, attrs) { element[0].addEventListener('dragstart', $scope.handleDragStart, false); element[0].addEventListener('dragend', $scope.handleDragEnd, false); } } }); AIA.directive('droppable', function () { return { link: function ($scope, element, attrs) { element[0].addEventListener('drop', $scope.handleDrop, false); element[0].addEventListener('dragover', $scope.handleDragOver, false); } } }); AIA.directive('imageonload', function () { return { restrict: 'A', link: function (scope, element, attrs) { element.bind('load', function (e) { $('#imgdiv').css('width', this.naturalWidth + "px"); }); element.bind('error', function () { //alert('image could not be loaded'); }); } }; }); //angular.module('app') //.filter('to_trusted', ['$sce', function ($sce) { // return function (text) { // return $sce.trustAsHtml(text); // }; //}]);