From 541d1742cddb62c05b992ab464d16b1734da807e Mon Sep 17 00:00:00 2001
From: xamgore <xamgore@ya.ru>
Date: Tue, 23 Jun 2015 19:58:19 +0300
Subject: [PATCH] XMLCreator refactoring

---
 .../classes/Controller/Handler/XMLCreator.php | 121 ++----------------
 .../application/views/pdf/xmlRate.twig        |  32 +++++
 2 files changed, 43 insertions(+), 110 deletions(-)
 create mode 100644 ~dev_rating/application/views/pdf/xmlRate.twig

diff --git a/~dev_rating/application/classes/Controller/Handler/XMLCreator.php b/~dev_rating/application/classes/Controller/Handler/XMLCreator.php
index 007483e81..85819aedd 100644
--- a/~dev_rating/application/classes/Controller/Handler/XMLCreator.php
+++ b/~dev_rating/application/classes/Controller/Handler/XMLCreator.php
@@ -4,124 +4,25 @@ class Controller_Handler_XMLCreator extends Controller_Handler
 {
     // Таблица баллов (со страницы оценивания) [dev version]
     public function action_GenerateXMLFile() {
-        $disciplineID = $this->post['disciplineID'];
+        $discipline = Model_Discipline::load($this->post['disciplineID']);
         $groupID = $this->post['studyGroupID'];
 
-        // make form from template
-        $info = Model_Rating::getFinalFormInfo($disciplineID, $groupID);
-        $info['DisciplineID'] = $disciplineID;
-
-        // prepare filename
-        $grade = $info["GradeNum"];
-        $group = $info["GroupNum"];
-        $faculty = $info['FacultyName'];
-        $subjName = $info["SubjectName"];
-
-
-        $type = $info['ExamType'];
-
         $stage = 1; // TODO: user should provide stage
-        $rates = Model_Rating::getRatesForStudentsGroupByStage($disciplineID, $groupID, $stage);
-
-        $examHold = 1;
-        // if ($type === 'exam')
-        // $examHold = $this->checkExamIsHold($rates);
-
-        $filename = $type . "_" . $grade . "_" . $group;
-
-        $fp = fopen("test.xml", "w+");
-        $degree = $info['Degree'];
-
-        if ($degree == "bachelor")
-            $degree = "Бакалавр";
-        else if ($degree == "master")
-            $degree = "Магистр";
-        else
-            $degree = "Специалист";
-
-        if ($type == "exam")
-            $type = "Экзамен";
-        else if ($type == "credit")
-            $type = "Зачет";
-        else
-            $type = "Дифференцированный зачет";
-
-        $gradeNum = $info['GradeNum'];
-        fputs($fp, "<?xml version='1.0' encoding='UTF-8'?>\n");
-        fputs($fp, "<document\n>");
-        fputs($fp, "<header\n>");
-        $semester = $info['SemesterNum'];
-        fputs($fp, "<semester>\n" . $semester . "</semester>\n");
-        fputs($fp, "<faculty>\n" . $faculty . "</faculty>\n");
-        fputs($fp, "<grade>\n" . $gradeNum . "</grade>\n");
-        fputs($fp, "<degree>\n" . $degree . "</degree>\n");
-        fputs($fp, "<spec>\n" . $info['SpecName'] . " " . $info['SpecCode'] . "</spec>\n");
-        fputs($fp, "<group>\n" . $group . "</group>\n");
-        fputs($fp, "<subjName>\n" . $subjName . "</subjName>\n");
 
-        fputs($fp, "<examtype>\n" . $type . "</examtype>\n");
-
-        $countteachers = Model_Teachers::getNamesForDiscipline($info['DisciplineID'], true, true);
-        $teachers = Model_Teachers::ofDiscipline($info['DisciplineID']);
-
-
-        $teachersStr = '';
-        fputs($fp, "<teachers>\n");
-        if (count($countteachers) > 1) {
-            foreach ($teachers as $row) {
-                fputs($fp, "<teacher>\n");
-                fputs($fp, "<lastName>\n" . $row['LastName'] . "</lastName>\n");
-                fputs($fp, "<firstName>\n" . $row['FirstName'] . "</firstName>\n");
-                fputs($fp, "<secondName>\n" . $row['SecondName'] . "</secondName>\n");
-                fputs($fp, "</teacher>\n");
-            }
-            /*for($i=0;$i<count($teachers);++$i)
-            {
-                fputs($fp,$teachers[$i].' ');
-            }*/
-        } else {
-            fputs($fp, "<teacher>\n");
-            fputs($fp, "<lastName>\n" . $info['LastName'] . "</lastName>\n");
-            fputs($fp, "<firstName>\n" . $info['FirstName'] . "</firstName>\n");
-            fputs($fp, "<secondName>\n" . $info['SecondName'] . "</secondName>\n");
-            fputs($fp, "</teacher>\n");
-        }
-
-        fputs($fp, "</teachers>\n");
-        fputs($fp, "</header\n>");
-
-
-        fputs($fp, "<rates\n>");
-        foreach ($rates as $studentRates) {
-            fputs($fp, "<student\n>");
-            $lastName = $studentRates['LastName'];
-            $firstName = $studentRates['FirstName'];
-            $secondName = $studentRates['SecondName'];
-            $fullName = $lastName . " " . $firstName . " " . $secondName;
-            fputs($fp, "<lastName>\n" . $lastName . "</lastName>\n");
-            fputs($fp, "<firstName>\n" . $firstName . "</firstName>\n");
-            fputs($fp, "<secondName>\n" . $secondName . "</secondName>\n");
-            $ratesSet['semester'] = (int) $studentRates['regular'];
-            $ratesSet['bonus'] = (int) $studentRates['bonus'];
-            $ratesSet['extra'] = (int) $studentRates['extra'];
-            $ratesSet['exam'] = (int) $studentRates['exam'];
-
-            $totalRate = $ratesSet['bonus'] + $ratesSet['exam'] + $ratesSet['semester'] + $ratesSet['extra'];
-
-            fputs($fp, "<totalRate>\n" . $totalRate . "</totalRate>\n");
-            //fputs($fp,"<semesterRate>\n".$ratesSet['semester']."</semesterRate>\n");
-            // fputs($fp,"<bonusRate>\n".$ratesSet['bonus']."</bonusRate>\n");
-            fputs($fp, "</student\n>");
-        }
-        fputs($fp, "</rates\n>");
-        fputs($fp, "</document\n>");
-        fclose($fp);
+        $document = Twig::factory('pdf/xmlRate', [
+            'Teachers' => $discipline->getTeachers(),
+            'Info'     => Model_Rating::getFinalFormInfo($discipline->ID, $groupID),
+            'Rates'    => Model_Rating::getRatesForStudentsGroupByStage($discipline->ID, $groupID, $stage),
+        ]);
 
         $options = [
             'mime_type' => File::mime_by_ext('xml'),
             'delete'    => true
         ];
-        //$filename=self::encodestring($filename);
-        $this->response->send_file('test.xml', $filename . '.xml', $options);
+
+        // todo: add some extra headers to download file
+        // https://tournasdimitrios1.wordpress.com/2012/01/10/how-to-make-files-downloadable-with-php/
+
+        $this->response->headers($options)->body((string) $document);
     }
 }
\ No newline at end of file
diff --git a/~dev_rating/application/views/pdf/xmlRate.twig b/~dev_rating/application/views/pdf/xmlRate.twig
new file mode 100644
index 000000000..372569a89
--- /dev/null
+++ b/~dev_rating/application/views/pdf/xmlRate.twig
@@ -0,0 +1,32 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<document>
+    <header>
+        <semester>{{ Info.SemesterNum }}</semester>
+        <faculty>{{ Info.FacultyName }}</faculty>
+        <grade>{{ Info.GradeNum }}</grade>
+        <degree>{{ Rus[Info.Degree] }}</degree>
+        <spec>{{ Info.SpecName }} {{ Info.SpecCode }}</spec>
+        <group>{{ Info.GroupNum }}</group>
+        <subjName>{{ Info.SubjectName }}</subjName>
+        <examType>{{ Rus[Info.ExamType] }}</examType>
+        <teachers>
+        {% for Teacher in Teachers %}
+            <teacher>
+                <lastName>Teacher.LastName</lastName>
+                <firstName>Teacher.LastName</firstName>
+                <secondName>Teacher.LastName</secondName>
+            </teacher>
+        {% endfor %}
+        </teachers>
+    </header>
+    <rates>
+        {% for Student in Rates %}
+        <student>
+            <lastName>{{ Student.LastName }}</lastName>
+            <firstName>{{ Student.FirstName }}</firstName>
+            <secondName>{{ Student.SecondName }}</secondName>
+            <totalRate>{{ Student.regular + Student.bonus + Student.extra + Student.exam }}</totalRate>
+        </student>
+        {% endfor %}
+    </rates>
+</document>
\ No newline at end of file
-- 
GitLab