From 84c6319bd00fa2de5f4bd4a57c00e80f62bcd826 Mon Sep 17 00:00:00 2001
From: PavelBegunkov <asml.Silence@gmail.com>
Date: Mon, 15 Jun 2015 22:43:32 +0300
Subject: [PATCH] Big chunk of fixes.

1) exam page: hide hidden div with ( json data )
2) update session info on profile/settings changes (User.php)
3) fix profile page model ( Model_Teacher )
4) fix change discipline's control type ( sql )
5) fix sql response in bind group ( repeated binding problem )
---
 db/StoredFunctions.sql                        | 79 +++++++++----------
 .../application/classes/Model/Teacher.php     | 10 +--
 ~dev_rating/application/classes/User.php      | 37 ++++++---
 .../application/views/teacher/exam.twig       |  2 +-
 4 files changed, 69 insertions(+), 59 deletions(-)

diff --git a/db/StoredFunctions.sql b/db/StoredFunctions.sql
index 2f0784b2a..93ea46dcc 100644
--- a/db/StoredFunctions.sql
+++ b/db/StoredFunctions.sql
@@ -948,7 +948,7 @@ CREATE FUNCTION `ChangeDisciplineControl` (
     NO SQL
 BEGIN
     DECLARE vChecker, vExtraMax, vExtraID INT DEFAULT -1;
-    DECLARE vOldExamType enum('exam', 'credit', 'grading_credit') DEFAULT -1;
+    DECLARE vOldExamType enum('exam', 'credit', 'grading_credit');
     DECLARE EXIT HANDLER FOR SQLEXCEPTION RETURN -1;
 
     IF  InternalIsMapLocked(pDisciplineID) OR
@@ -968,42 +968,40 @@ BEGIN
         RETURN -1;
     END IF;
 
+    # check type changing: exam <-> credit/grading_credit
+    IF NOT (vOldExamType = 'exam' XOR pExamType != 'exam') THEN
+
+        # TODO: extract method addExtraModule
+        IF pExamType = 'exam' THEN # change to exam
+            SET vExtraMax = 7;
+            # count discipline's current max rate
+            SELECT view_disciplines_results.DisciplineRateMax INTO vChecker
+                FROM `view_disciplines_results`
+                WHERE view_disciplines_results.DisciplineID = pDisciplineID
+                LIMIT 1;
+
+            IF vChecker >= 61 THEN # can't add exam module ( > 100 points)
+                RETURN 1;
+            END IF;
+            SET vChecker = AddModuleExam(pTeacherID, pDisciplineID);
+
+            # delete extra submodules(only 1 extra for exam)
+            DELETE FROM `submodules`
+                WHERE submodules.OrderNum > 1 AND submodules.ModuleID = vExtraID;
+        ELSE # change to credit
+            SET vExtraMax = 29;
+            SET vChecker = DeleteModuleExam(pTeacherID, pDisciplineID);
+            # 2 extra submodules (1 already created for exam)
+            SET vChecker = AddSubmodule(pTeacherID, vExtraID, vExtraMax, '', NULL, 'LandmarkControl');
+        END IF;
+    END IF;
+
     # set new exam type
     UPDATE `disciplines`
         SET disciplines.ExamType = pExamType
         WHERE disciplines.ID = pDisciplineID
         LIMIT 1;
 
-    # check type changing: exam <-> credit/grading_credit
-    IF vOldExamType = 'exam' XOR pExamType != 'exam' THEN
-        RETURN 0;
-    END IF;
-
-
-    # TODO: extract method addExtraModule
-    IF pExamType = 'exam' THEN # change to exam
-        SET vExtraMax = 7;
-        # count discipline's current max rate
-        SELECT view_disciplines_results.DisciplineRateMax INTO vChecker
-            FROM `view_disciplines_results`
-            WHERE view_disciplines_results.DisciplineID = pDisciplineID
-            LIMIT 1;
-
-        IF vChecker >= 61 THEN # can't add exam module ( > 100 points)
-            RETURN 1;
-        END IF;
-        SET vChecker = AddModuleExam(pTeacherID, pDisciplineID);
-
-        # delete extra submodules(only 1 extra for exam)
-        DELETE FROM `submodules`
-            WHERE submodules.OrderNum > 1 AND submodules.ModuleID = vExtraID;
-    ELSE # change to credit
-        SET vExtraMax = 29;
-        SET vChecker = DeleteModuleExam(pTeacherID, pDisciplineID);
-        # 2 extra submodules (1 already created for exam)
-        SET vChecker = AddSubmodule(pTeacherID, vExtraID, vExtraMax, '', NULL, 'LandmarkControl');
-    END IF;
-
     # set max rate for extra
     UPDATE `submodules`
         SET submodules.MaxRate = vExtraMax
@@ -1074,18 +1072,19 @@ BEGIN
         ON DUPLICATE KEY UPDATE
             disciplines_groups.ID = ( @isAttached := LAST_INSERT_ID(disciplines_groups.ID) );
 
-    IF @isAttached < 0 THEN # group was attached
+    IF @isAttached > 0 THEN # group was attached
         SET vSemesterID = GetDisciplineProperty(pDisciplineID, 'semester');
 
         # 3. delete students of this group which were bound to discipline before
-                DELETE FROM `disciplines_students`
-                WHERE   disciplines_students.DisciplineID = pDisciplineID AND
-                        disciplines_students.StudentID IN (
-                            SELECT students_groups.StudentID
-                            FROM `students_groups`
-                            WHERE   students_groups.GroupID = pGroupID AND
-                                    students_groups.SemesterID = vSemesterID
-                        );
+        DELETE FROM `disciplines_students`
+        WHERE   disciplines_students.DisciplineID = pDisciplineID AND
+                disciplines_students.StudentID IN (
+                    SELECT students_groups.StudentID
+                    FROM `students_groups`
+                    WHERE   students_groups.GroupID = pGroupID AND
+                            students_groups.SemesterID = vSemesterID
+                );
+        RETURN 1;
     END IF;
 
     RETURN 0;
diff --git a/~dev_rating/application/classes/Model/Teacher.php b/~dev_rating/application/classes/Model/Teacher.php
index b7b6a91d0..15bf9281e 100644
--- a/~dev_rating/application/classes/Model/Teacher.php
+++ b/~dev_rating/application/classes/Model/Teacher.php
@@ -16,17 +16,17 @@ class Model_Teacher extends Model
 
 
     public function changeInfo($lastName, $firstName, $secondName, $degreeID, $departmentID) {
-        $sql = "SELECT `ChangeTeacherInfo`(:id, :last, :first, :second, :degree, :department) AS `UserID`;";
-        return DB::query(Database::SELECT, $sql)
+        $sql = "SELECT `ChangeTeacherInfo`(:id, :last, :first, :second, :degree, :department) AS `Num`;";
+        $res = DB::query(Database::SELECT, $sql)
             ->parameters([
                 ':id' => $this->ID,
                 ':last' => $lastName,
                 ':first' => $firstName,
                 ':second' => $secondName,
                 ':degree' => $degreeID,
-                'department' => $departmentID,
-            ])->execute()->get('UserID');
-        // todo: what does it return?
+                ':department' => $departmentID,
+            ])->execute()->get('Num');
+        return ( $res == 0 );
     }
 
     /** @return Model_Discipline[] */
diff --git a/~dev_rating/application/classes/User.php b/~dev_rating/application/classes/User.php
index 47b60f6b6..f086804bb 100644
--- a/~dev_rating/application/classes/User.php
+++ b/~dev_rating/application/classes/User.php
@@ -68,7 +68,7 @@ class User implements ArrayAccess
     }
 
 
-    private function __construct($config = array()) {
+    private function __construct($config = []) {
         $this->_config = $config;
         $this->_general = Model_System::loadConfig("general.json");
 
@@ -132,17 +132,17 @@ class User implements ArrayAccess
         $id = Model_Account::activateAccount($login, $password, $email, $code);
         switch ($id) {
             case -1:
-                return array(false, 'something wrong!');
+                return [false, 'something wrong!'];
             case -2:
-                return array(false, 'invalid_code');
+                return [false, 'invalid_code'];
             case -3:
-                return array(false, 'mail_exists');
+                return [false, 'mail_exists'];
             case -4:
-                return array(false, 'login_exists');
+                return [false, 'login_exists'];
         }
 
         $this->completeSignIn($id, $this->hash($password));
-        return array(true, 'ok');
+        return [true, 'ok'];
     }
 
     /**
@@ -262,7 +262,11 @@ class User implements ArrayAccess
             return false;
 
         $res = (int)Model_Account::changeAccountData($this->ID, $login, 'login');
-        return ( $res === 0 );
+        if ( $res === 0 ) {
+            $this->Login = $login;
+            return true;
+        }
+        return false;
     }
 
     public function changeMail($email) {
@@ -287,12 +291,19 @@ class User implements ArrayAccess
 //    }
 
     public function changeProfile($data) {
-        if ($this->Type == 'teacher') {
-            Model_Teacher::with($this['TeacherID'])->changeInfo(
-                $data['lastName'], $data['firstName'], $data['secondName'],
-                $data['jobPositionID'], $data['departmentID']
-            );
+        if ($this->Type != 'teacher')
+            return false;
+
+        $res = Model_Teacher::with($this['TeacherID'])->changeInfo(
+            $data['lastName'], $data['firstName'], $data['secondName'],
+            $data['jobPositionID'], $data['departmentID']
+        );
+        if ( $res ) {
+            $this->LastName = $data['lastName'];
+            $this->FirstName = $data['firstName'];
+            $this->SecondName = $data['secondName'];
         }
+        return $res;
     }
 
     /* Info */
@@ -306,7 +317,7 @@ class User implements ArrayAccess
         if ($this->isSignedIn()) {
             return $this->_session->as_array();
         } else {
-            return array();
+            return [];
         }
     }
 
diff --git a/~dev_rating/application/views/teacher/exam.twig b/~dev_rating/application/views/teacher/exam.twig
index d258181de..0573d9b11 100644
--- a/~dev_rating/application/views/teacher/exam.twig
+++ b/~dev_rating/application/views/teacher/exam.twig
@@ -201,7 +201,7 @@
 			<span id="maxRate">Максимальный балл: <b></b></span>
 		</div>
 	</div>
-	<div id="hidden_div">
+	<div id="hidden_div" style="display: none;">
 		{{ Discipline_JSON|raw }}
 	</div>
 	<input type="hidden" id="pageType" value="exam"/> 
-- 
GitLab