From f80322bc17151408a7a0aaf7dd84de4dd62b88ea Mon Sep 17 00:00:00 2001
From: xamgore <xamgore@ya.ru>
Date: Wed, 24 Jun 2015 20:44:27 +0300
Subject: [PATCH] Refactoring of Model_Grade::toStructuredArray()

---
 .../classes/Controller/Admin/Accounts.php      |  2 +-
 .../classes/Controller/Admin/Students.php      |  4 ++--
 .../classes/Controller/DeanOffice/Students.php |  2 +-
 .../application/classes/Model/Grades.php       | 18 +++++-------------
 .../views/admin/accounts/codes.twig            | 12 ++++++------
 .../application/views/admin/students/add.twig  | 12 ++++++------
 .../views/admin/students/index.twig            | 12 ++++++------
 .../views/dean_office/students/index.twig      |  6 +++---
 8 files changed, 30 insertions(+), 38 deletions(-)

diff --git a/~dev_rating/application/classes/Controller/Admin/Accounts.php b/~dev_rating/application/classes/Controller/Admin/Accounts.php
index 2f2d809c2..3646f8341 100644
--- a/~dev_rating/application/classes/Controller/Admin/Accounts.php
+++ b/~dev_rating/application/classes/Controller/Admin/Accounts.php
@@ -10,7 +10,7 @@ class Controller_Admin_Accounts extends Controller_Environment_Admin
         Cookie::set('fD', 'true');
         $this->twig->set([
             'Faculties' => Model_Faculties::load(),
-            'Grades'    => Model_Grades::toStructuredArray(),
+            'Grades'    => Model_Grades::groupByDegree(),
         ])->set_filename('admin/accounts/codes');
     }
 }
diff --git a/~dev_rating/application/classes/Controller/Admin/Students.php b/~dev_rating/application/classes/Controller/Admin/Students.php
index b7cfcb695..563ecf59b 100644
--- a/~dev_rating/application/classes/Controller/Admin/Students.php
+++ b/~dev_rating/application/classes/Controller/Admin/Students.php
@@ -5,14 +5,14 @@ class Controller_Admin_Students extends Controller_Environment_Admin
     public function action_index() {
         $this->twig->set([
             'Faculties' => Model_Faculties::load(),
-            'Grades'    => Model_Grades::toStructuredArray(),
+            'Grades'    => Model_Grades::groupByDegree(),
         ])->set_filename('admin/students/index');
     }
 
     public function action_add() {
         $this->twig->set([
             'Faculties' => Model_Faculties::load(),
-            'Grades'    => Model_Grades::toStructuredArray(),
+            'Grades'    => Model_Grades::groupByDegree(),
         ])->set_filename('admin/students/add');
     }
 
diff --git a/~dev_rating/application/classes/Controller/DeanOffice/Students.php b/~dev_rating/application/classes/Controller/DeanOffice/Students.php
index 6321ab847..8007072c9 100644
--- a/~dev_rating/application/classes/Controller/DeanOffice/Students.php
+++ b/~dev_rating/application/classes/Controller/DeanOffice/Students.php
@@ -6,7 +6,7 @@ class Controller_DeanOffice_Students extends Controller_Environment_DeanOffice
     public function action_index() {
         $this->twig->set([
             'Faculties' => Model_Faculties::load(),
-            'Grades'    => Model_Grades::toStructuredArray(),
+            'Grades'    => Model_Grades::groupByDegree(),
         ])->set_filename(static::DEAN . '/students/index');
     }
 
diff --git a/~dev_rating/application/classes/Model/Grades.php b/~dev_rating/application/classes/Model/Grades.php
index c46125cc3..94d528cf9 100644
--- a/~dev_rating/application/classes/Model/Grades.php
+++ b/~dev_rating/application/classes/Model/Grades.php
@@ -35,21 +35,13 @@ class Model_Grades extends Model
         }
     }
 
-    /** @return array an array with elements, grouped by grades. */
-    public static function toStructuredArray() {
-        $grades = self::toArray();
+    /** @return array */
+    public static function groupByDegree() {
         $list = [];
-        $degree = 'null';
-        $i = 0;
 
-        foreach ($grades as $row) {
-            if ($degree != $row['Degree']) {
-                $degree = $row['Degree'];
-                $i++;
-            }
-
-            $list[$i]['Title'] = RusLang::tr($row['Degree']);
-            $list[$i]['Grades'][] = $row;
+        foreach (self::getGrades() as $row) {
+            $degree = $row['Degree'];
+            $list[$degree][] = $row;
         }
 
         return $list;
diff --git a/~dev_rating/application/views/admin/accounts/codes.twig b/~dev_rating/application/views/admin/accounts/codes.twig
index 3aff72706..04b2b049e 100644
--- a/~dev_rating/application/views/admin/accounts/codes.twig
+++ b/~dev_rating/application/views/admin/accounts/codes.twig
@@ -48,12 +48,12 @@
         <div class="step_body">
             <select id="gradeSelect">
                 <option value="0">— Курс —</option>
-                {% for Degree in Grades %}
-                <optgroup label="{{ Degree.Title }}">
-                    {% for Grade in Degree.Grades %}
-                        <option value="{{ Grade.ID }}">{{ Grade.Num }} РєСѓСЂСЃ</option>
-                    {% endfor %}
-                </optgroup>
+                {% for Title, Degree in Grades %}
+                    <optgroup label="{{ Rus[Title] }}">
+                        {% for Grade in Degree %}
+                            <option value="{{ Grade.ID }}">{{ Grade.Num }} РєСѓСЂСЃ</option>
+                        {% endfor %}
+                    </optgroup>
                 {% endfor %}
             </select>
         </div>
diff --git a/~dev_rating/application/views/admin/students/add.twig b/~dev_rating/application/views/admin/students/add.twig
index 40a1b2a85..2362fc0c3 100644
--- a/~dev_rating/application/views/admin/students/add.twig
+++ b/~dev_rating/application/views/admin/students/add.twig
@@ -44,12 +44,12 @@
             </select>
             <select id="gradeSelect">
                 <option value="0">— Курс —</option>
-                {% for Degree in Grades %}
-                <optgroup label="{{ Degree.Title }}">
-                    {% for Grade in Degree.Grades %}
-                        <option value="{{ Grade.ID }}">{{ Grade.Num }} РєСѓСЂСЃ</option>
-                    {% endfor %}
-                </optgroup>
+                {% for Title, Degree in Grades %}
+                    <optgroup label="{{ Rus[Title] }}">
+                        {% for Grade in Degree %}
+                            <option value="{{ Grade.ID }}">{{ Grade.Num }} РєСѓСЂСЃ</option>
+                        {% endfor %}
+                    </optgroup>
                 {% endfor %}
             </select>
             <select id="groupSelect">
diff --git a/~dev_rating/application/views/admin/students/index.twig b/~dev_rating/application/views/admin/students/index.twig
index 3de43e198..62e35b9b8 100644
--- a/~dev_rating/application/views/admin/students/index.twig
+++ b/~dev_rating/application/views/admin/students/index.twig
@@ -35,12 +35,12 @@
                     <div class='filter'>
                         <select id="gradeSelect">
                             <option value="0">— Курс —</option>
-                            {% for Degree in Grades %}
-                            <optgroup label="{{ Degree.Title }}">
-                                {% for Grade in Degree.Grades %}
-                                    <option value="{{ Grade.ID }}">{{ Grade.Num }} РєСѓСЂСЃ</option>
-                                {% endfor %}
-                            </optgroup>
+                            {% for Title, Degree in Grades %}
+                                <optgroup label="{{ Rus[Title] }}">
+                                    {% for Grade in Degree %}
+                                        <option value="{{ Grade.ID }}">{{ Grade.Num }} РєСѓСЂСЃ</option>
+                                    {% endfor %}
+                                </optgroup>
                             {% endfor %}
                         </select>
                     </div>
diff --git a/~dev_rating/application/views/dean_office/students/index.twig b/~dev_rating/application/views/dean_office/students/index.twig
index e778ec44a..faf58bdcd 100644
--- a/~dev_rating/application/views/dean_office/students/index.twig
+++ b/~dev_rating/application/views/dean_office/students/index.twig
@@ -25,9 +25,9 @@
                     <div class='filter'>
                         <select id="gradeSelect">
                             <option value="0">— Курс —</option>
-                            {% for Degree in Grades %}
-                                <optgroup label="{{ Degree.Title }}">
-                                    {% for Grade in Degree.Grades %}
+                            {% for Title, Degree in Grades %}
+                                <optgroup label="{{ Rus[Title] }}">
+                                    {% for Grade in Degree %}
                                         <option value="{{ Grade.ID }}">{{ Grade.Num }} РєСѓСЂСЃ</option>
                                     {% endfor %}
                                 </optgroup>
-- 
GitLab