Exam003.Controller.js 3.7 KB
Newer Older
Thitichaipun Wutthisak committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
'use strict'

angular.module('EXAM').controller('Exam003',['$scope', '$http', 'EXAMSERVICES', '$window', '$filter', function($scope, $http, EXAMSERVICES, $window, $filter){

   $scope.questset3 = {
      'question_type': '0',
      'total_score': parseFloat(0).toFixed(2)
   };
   $scope.questset3ch = [];

   // 2 ตัวนี้ใช้เก็บเป็น Tmp เวลามีการเปลี่ยน type $scope.questset3ch จะถูกเคลียร์ค่าแต่จะมีการเก็บค่าก่อนเคลียร์ไว้ก่อน
   $scope.tmpquestset4 = [];
   $scope.tmpquestion = [];

   $scope.queryByKey = function(keyname, keyval){
      EXAMSERVICES.ManageExam003.query({examid: keyval, querytype: 'ex03'})
      .$promise.then(function(data){
         $scope.questset3 = data.mquestionset3;
         $scope.questset3ch = data.mquestionset3ch;

         $scope.calScore();
      });
   };

   $scope.save = function(){
      $scope.calScore();
      var allobj = {
         'questionset3': $scope.questset3,
         'questionset3ch': $scope.questset3ch
      };

      EXAMSERVICES.ManageExam003.save(allobj)
      .$promise.then(function(data){
         alert(MyCode[18]);
      });
   };
   $scope.delete = function(){
      EXAMSERVICES.ManageExam003.delete({examid: $scope.questset3.questionset3id})
      .$promise.then(function(data){
         alert(MyCode[19]);
         window.location.reload();
      });
   };

   $window.putNewChild = function (allarr) {
      allarr.forEach(function(data){
         var isFound = $filter('findWithAttr')($scope.questset3ch, 'questionid', data.questionid);
         if (!isFound) {
            $scope.questset3ch.push(data);
         }
      });
      $scope.calScore();
      $scope.$apply();
   };

   $window.newChild = function(objlist){
      var isFound = $filter('findWithAttr')($scope.questset3ch, 'questionid', objlist.questionid);
      if (!isFound) {
         $scope.questset3ch.push(objlist);
      }
      $scope.calScore();
      $scope.$apply();
   };


   $scope.add = function () {
      if ($scope.questset3.question_type == 1){
         $window.open("EXAM_ADD_QUESTION.jsp?hasAddQuestion=1", "ADD_QUESTION", "toolbar=yes, scrollbars=yes, resizable=yes, top=100, left=300, width=1200, height=800");
      }else if ($scope.questset3.question_type == 0){
         $window.open("EXAM003_ADD.jsp", "ADD_QUESTION", "toolbar=yes, scrollbars=yes, resizable=yes, top=100, left=300, width=800, height=500");
      }
   };

   $scope.calScore = function(){
      var result = parseFloat(0.00);
      $scope.questset3ch.forEach(function(data){
         console.log(data)
         result += parseFloat(data.score);
      });
      $scope.questset3.total_score = parseFloat(result).toFixed(2);
   };

   $scope.selectAll = function(){
      $scope.questset3ch.forEach(function(data){
         data.ischeck = true;
      });
   };

   $scope.deSelectAll = function(){
      $scope.questset3ch.forEach(function(data){
         data.ischeck = false;
      });
   };

   $scope.delSelect = function(){
      var listindex = [];

      $scope.questset3ch.forEach(function(data, index){
         if (data.ischeck){
            listindex.push(index);
         }
      });

      while (listindex.length > 0){
         $scope.questset3ch.splice(listindex.pop(), 1);
      }

      $scope.calScore();
   };

   $scope.checkBeforeChange = function(){
      if ($scope.questset3.question_type == '0'){
         $scope.tmpquestset4 = $scope.questset3ch;
         $scope.questset3ch = $scope.tmpquestion;
      }else if ($scope.questset3.question_type == '1'){
         $scope.tmpquestion = $scope.questset3ch;
         $scope.questset3ch = $scope.tmpquestset4;
      }
   }
}]);