From f6a465a0f88ddb5e4106f3656dd9b49d7393fa3b Mon Sep 17 00:00:00 2001
From: PavelBegunkov <asml.Silence@gmail.com>
Date: Fri, 26 Jun 2015 13:41:07 +0300
Subject: [PATCH] remove aggregation from get teacher list for student.

---
 db/StoredProcedures.sql                       | 64 +++++++++----------
 .../application/classes/Model/Student.php     | 10 ++-
 2 files changed, 37 insertions(+), 37 deletions(-)

diff --git a/db/StoredProcedures.sql b/db/StoredProcedures.sql
index a4065f910..6cdeb067c 100644
--- a/db/StoredProcedures.sql
+++ b/db/StoredProcedures.sql
@@ -449,55 +449,51 @@ END //
 DROP PROCEDURE IF EXISTS GetTeachersListForStudent//
 CREATE PROCEDURE `GetTeachersListForStudent` (
     IN `pStudentID` INT,
-    IN `pSemesterID` INT
-    # todo: new parameter, pLoadAll. See Model_Student::getTeachers().
+    IN `pSemesterID` INT,
+    IN `pLoadAll` INT
 ) NO SQL
 BEGIN
     DECLARE vStudentGroup INT DEFAULT -1;
     SET vStudentGroup = GetStudentGroup(pStudentID, pSemesterID);
 
-# select all disciplines for user
+
+    # select all disciplines for user
     CREATE TEMPORARY TABLE IF NOT EXISTS tDisciplines AS (
         SELECT disc2.DisciplineID
         FROM (
-             SELECT  disc1.DisciplineID,
-                     COALESCE(disc1.Type) AS 'Type'
-                 FROM (
-                          SELECT  disciplines_students.DisciplineID,
-                              disciplines_students.Type
-                          FROM `disciplines_students`
-                          WHERE   disciplines_students.StudentID = pStudentID
-                          UNION
-                          SELECT  disciplines_groups.DisciplineID,
-                              NULL AS 'Type'
-                          FROM `disciplines_groups`
-                          WHERE   disciplines_groups.GroupID = vStudentGroup
-                      ) AS disc1
-                     INNER JOIN `disciplines` ON disciplines.ID = disc1.DisciplineID
-                 WHERE disciplines.SemesterID = pSemesterID
-                 GROUP BY disc1.DisciplineID
-             ) AS disc2
+            SELECT  disc1.DisciplineID,
+                    COALESCE(disc1.Type) AS 'Type'
+                FROM (
+                    SELECT  disciplines_students.DisciplineID,
+                            disciplines_students.Type
+                        FROM `disciplines_students`
+                        WHERE disciplines_students.StudentID = pStudentID
+                    UNION
+                    SELECT  disciplines_groups.DisciplineID,
+                            NULL AS 'Type'
+                        FROM `disciplines_groups`
+                        WHERE   disciplines_groups.GroupID = vStudentGroup
+                ) AS disc1
+                INNER JOIN `disciplines` ON disciplines.ID = disc1.DisciplineID
+                WHERE disciplines.SemesterID = pSemesterID
+                GROUP BY disc1.DisciplineID
+            ) AS disc2
         WHERE NOT disc2.Type <=> 'detach'
     );
 
-
     SELECT  tDisciplines.DisciplineID,
-            GROUP_CONCAT(
-                CONCAT(teachers.LastName, ' ', teachers.FirstName, ' ', teachers.SecondName)
-                ORDER BY    teachers.ID = disciplines.AuthorID DESC,
-                teachers.LastName ASC,
-                teachers.FirstName ASC
-                SEPARATOR ';'
-            ) AS 'FullNameList'
-    FROM tDisciplines
+            teachers.ID AS 'TeacherID',
+            teachers.LastName,
+            teachers.FirstName,
+            teachers.SecondName
+        FROM tDisciplines
         INNER JOIN `disciplines` ON disciplines.ID = tDisciplines.DisciplineID
         LEFT JOIN `disciplines_teachers` ON disciplines_teachers.DisciplineID = tDisciplines.DisciplineID
         INNER JOIN `teachers` ON teachers.ID = disciplines_teachers.TeacherID
-    GROUP BY tDisciplines.DisciplineID
-    ORDER BY tDisciplines.DisciplineID;
-
-    # todo: aggregation
-    # todo: return teachers' ID
+        WHERE pLoadAll OR disciplines.Subtype IS NULL
+        ORDER BY    tDisciplines.DisciplineID ASC,
+                    teachers.LastName ASC,
+                    teachers.FirstName ASC;
 END //
 
 
diff --git a/~dev_rating/application/classes/Model/Student.php b/~dev_rating/application/classes/Model/Student.php
index b52a52696..43fc83de1 100644
--- a/~dev_rating/application/classes/Model/Student.php
+++ b/~dev_rating/application/classes/Model/Student.php
@@ -43,18 +43,22 @@ class Model_Student extends Model_Container
         // todo: don't load the full data at the Controller_Student_Index
         $semesterID = $semesterID ? $semesterID : User::instance()->SemesterID;
 
-        $sql = 'CALL `GetTeachersListForStudent`(:id, :semester)';
+        $sql = 'CALL `GetTeachersListForStudent`(:id, :semester, :loadAll)';
         $result = DB::query(Database::SELECT, $sql)
             ->param(':id', $this->ID)
             ->param(':semester', $semesterID)
+            ->param(':loadAll', $loadAll)
             ->execute();
 
         $list = [];
+        // TODO: use TeacherID. Split fullName?
         foreach ($result as $row) {
             $names =& $list[$row['DisciplineID']];
-            $names = explode(';', $row['FullNameList']);  // todo: remove aggregation
+            $fullName = $row['LastName'] .' '. $row['FirstName'] .' '. $row['SecondName'];
+            if (!$names)
+                $names = [];
+            array_push($names, $fullName);
         }
-
         return $list;
     }
 
-- 
GitLab