From 817f613c9ed98468a3d8b98d54fcab704f57227c Mon Sep 17 00:00:00 2001
From: Victoria <pavlovika@yandex.ru>
Date: Sat, 20 Jun 2015 14:41:04 +0300
Subject: [PATCH] Support of xml import

---
 .../classes/Controller/Handler/XMLCreator.php | 131 ++++++++++++++++++
 .../application/views/dean_office/sheets.twig |   1 +
 .../media/js/dean_office/dean_office.js       |  30 +++-
 3 files changed, 160 insertions(+), 2 deletions(-)
 create mode 100644 ~dev_rating/application/classes/Controller/Handler/XMLCreator.php

diff --git a/~dev_rating/application/classes/Controller/Handler/XMLCreator.php b/~dev_rating/application/classes/Controller/Handler/XMLCreator.php
new file mode 100644
index 000000000..3f0e5fcdd
--- /dev/null
+++ b/~dev_rating/application/classes/Controller/Handler/XMLCreator.php
@@ -0,0 +1,131 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+class Controller_Handler_XMLCreator extends Controller_Handler
+{
+    // Таблица баллов (со страницы оценивания) [dev version]
+    public function action_GenerateXMLFile() {
+   
+         $disciplineID = $this->post['disciplineID'];
+        $groupID = $this->post['studyGroupID'];
+
+        // make form from template
+        $info = Model_Rating::getFinalFormInfo($disciplineID, $groupID)[0];
+        $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);
+
+        $options = [
+            'mime_type' => File::mime_by_ext('xml'),
+            'delete'=>true 
+        ];
+        //$filename=self::encodestring($filename);
+        $this->response->send_file('test.xml', $filename.'.xml', $options);
+        }
+}
\ No newline at end of file
diff --git a/~dev_rating/application/views/dean_office/sheets.twig b/~dev_rating/application/views/dean_office/sheets.twig
index 9e998bcf5..b5bec5207 100644
--- a/~dev_rating/application/views/dean_office/sheets.twig
+++ b/~dev_rating/application/views/dean_office/sheets.twig
@@ -82,6 +82,7 @@
                 <button class="defaultForm BlueButton HalfWidth" id="DownloadExamDocument" disabled="disabled">Скачать</button>
                 <button class="defaultForm BlueButton HalfWidth" id="BlockExamDiscipline" disabled="disabled">Блокировать</button>
                 <button class="defaultForm BlueButton HalfWidth" id="BlockAndPrintExam" disabled="disabled">Блокировать и Скачать</button>
+                <button class="defaultForm BlueButton HalfWidth" id="DownloadXML" disabled="disabled">Скачать в формате XML</button>
             </div>
         </div>
     </div>
diff --git a/~dev_rating/media/js/dean_office/dean_office.js b/~dev_rating/media/js/dean_office/dean_office.js
index 5ff232b93..5c9feb1d9 100644
--- a/~dev_rating/media/js/dean_office/dean_office.js
+++ b/~dev_rating/media/js/dean_office/dean_office.js
@@ -21,6 +21,7 @@ $(function() {
     var jDownloadExamDocument = $("#DownloadExamDocument");
 	var jSelectGroup = $("#SelectGroup");
 	var jSelectDiscipline = $("#SelectDiscipline");
+    var jDownloadXML = $("#DownloadXML");
 
     $("#ExamChoice").change(controlVisualization);
     $("#CreditChoice").change(controlVisualization);
@@ -61,6 +62,7 @@ $(function() {
         	jDownloadStatement.attr("disabled", "disabled");
         }
 		jDownloadExamDocument.attr("disabled", "disabled");
+        jDownloadXML.attr("disabled", "disabled");
     });
 
     $("#SelectGroup").change(function() {
@@ -94,14 +96,20 @@ $(function() {
         }
 	 	else jDownloadStatement.attr("disabled", "disabled");
 		
+        jDownloadXML.attr("disabled", "disabled");
 		jDownloadExamDocument.attr("disabled", "disabled");
 	});
 
     $("#SelectDiscipline").change(function() {
         var disciplineID = parseInt($(this).val()) || 0;
-        if (disciplineID > 0)
+        if (disciplineID > 0){
             jDownloadExamDocument.removeAttr("disabled");
-        else jDownloadExamDocument.attr("disabled", "disabled");
+            jDownloadXML.removeAttr("disabled");
+        }
+        else {
+            jDownloadExamDocument.attr("disabled", "disabled");
+            jDownloadXML.attr("disabled", "disabled");
+        }
     });
 
     // Скачать ведомость
@@ -140,6 +148,24 @@ $(function() {
         });
     });
 
+
+    $('body').on('click', '#DownloadXML', function(){
+        $.fileDownload(g_URLdir + 'handler/XMLCreator/GenerateXMLFile', {
+            httpMethod: "POST",
+            data:
+            {
+                "disciplineID": parseInt($("#SelectDiscipline").val()),
+                "studyGroupID": parseInt($("#SelectGroup").val())
+            },
+            successCallback: function () {
+
+            },
+            failCallback: function () {
+
+            }
+        });
+        });
+
     // Блокирование дисциплины
 
     // Печать ведомости и блокирование дисциплины
-- 
GitLab