From 71c411414ebcc73e94119e6ff5fcbe2ac0f960b6 Mon Sep 17 00:00:00 2001 From: xamgore <xamgore@ya.ru> Date: Fri, 19 Jun 2015 12:16:42 +0300 Subject: [PATCH] Refactoring of students upload page --- .../classes/Controller/Admin/Students.php | 10 +-- .../application/classes/FileParser.php | 84 +++++++++++-------- .../views/admin/students/upload.twig | 58 +++++++------ ~dev_rating/media/js/admin/students/upload.js | 13 ++- 4 files changed, 91 insertions(+), 74 deletions(-) diff --git a/~dev_rating/application/classes/Controller/Admin/Students.php b/~dev_rating/application/classes/Controller/Admin/Students.php index 4d9d7dbf4..b7cfcb695 100644 --- a/~dev_rating/application/classes/Controller/Admin/Students.php +++ b/~dev_rating/application/classes/Controller/Admin/Students.php @@ -17,16 +17,16 @@ class Controller_Admin_Students extends Controller_Environment_Admin } public function action_upload() { - $result['Success'] = false; + $errors = []; if (!empty($_FILES['students']) && $this->request->method() == 'POST') { - $file = $_FILES["students"]["tmp_name"]; - $result = FileParser::StudentsList($file, $_POST['facultyID']); + $file = $_FILES['students']["tmp_name"]; + $errors = FileParser::uploadStudents($file, $_POST['facultyID']); } $this->twig->set([ - 'UploadingResult' => $result, - 'Faculties' => Model_Faculties::load(), + 'Errors' => $errors, + 'Faculties' => Model_Faculties::load(), ])->set_filename('admin/students/upload'); } diff --git a/~dev_rating/application/classes/FileParser.php b/~dev_rating/application/classes/FileParser.php index 73032ba16..ec2f9b9fd 100644 --- a/~dev_rating/application/classes/FileParser.php +++ b/~dev_rating/application/classes/FileParser.php @@ -2,34 +2,42 @@ class FileParser { - public static function StudentsList($filename, $facultyID) - { - if(File::mime($filename) != 'text/plain') - return true; - $file = fopen($filename, "r"); - $i = $j = 0; $resultArr = array(); - while ($line = fgetcsv($file, 0, ";")) - { - // РРјСЏ, фамилия, отчество - list($lastName, $firstName, $secondName) = self::parsePeopleName($line[0]); - // РљСѓСЂСЃ, степень подготовки - $gradeNum = $line[1]; - $groupNum = $line[2]; - $degree = Model_Grades::getDegreeType($line[3]); - $specialization = $line[4]; + /** + * Parse students' info & synchronize it with database. + * @param $filename string File with source data + * @param $facultyID int + * @return array errors + */ + public static function uploadStudents($filename, $facultyID) { + if (File::mime($filename) != 'text/plain') + return []; + + $file = fopen($filename, "r"); + + $i = 0; + $errors = []; + + while ($line = fgetcsv($file, 0, ";")) { + // Р¤РРћ, РєСѓСЂСЃ, степень подготовки, + list($name, $gradeNum, $groupNum, $degree, $spec) = $line; + + // Фамилия, РёРјСЏ, отчество + list($lastName, $firstName, $secondName) = self::parsePeopleName($name); + + $degree = Model_Grades::getDegreeType($degree); + $attempt = Model_Account::createStudentEx( - $lastName, $firstName, $secondName, $gradeNum, $groupNum, $degree, $specialization, $facultyID + $lastName, $firstName, $secondName, $gradeNum, $groupNum, $degree, $spec, $facultyID ); - if($attempt == -1) - { - $resultArr[$j]['Row'] = $i; - $resultArr[$j]['Info'] = implode(';', $line); - $j++; + if ($attempt == -1) { + $errors[] = ['Row' => $i, 'Info' => implode(';', $line)]; } + $i++; } - return ($j > 0) ? false : $resultArr; + + return $errors; } public static function SubjectsList($filename, $facultyID) @@ -39,9 +47,9 @@ class FileParser $model = new Model_Students(); if(File::mime($filename) != 'text/plain') return $resultArr; - $file = fopen($filename, "r"); + $file = fopen($filename, "r"); $i = $errorsCount = $exists = 0; - while ($line = fgetcsv($file, 0, ";")) + while ($line = fgetcsv($file, 0, ";")) { $attempt = Model_Subject::create($line[0], $line[1], $facultyID); if((int)$attempt < 0) @@ -62,7 +70,7 @@ class FileParser $resultArr['RecordsCount'] = $i; return $resultArr; } - + public static function TeachersList($filename) { // get faculties list @@ -77,7 +85,7 @@ class FileParser return true; $file = fopen($filename, "r"); $i = $j = 0; - while ($line = fgetcsv($file, 0, ";")) + while ($line = fgetcsv($file, 0, ";")) { // РРјСЏ, фамилия, отчество list($lastName, $firstName, $secondName) = self::parsePeopleName($line[0]); @@ -98,22 +106,26 @@ class FileParser } return ($j > 0) ? false : $resultArr; } - - protected static function parsePeopleName($name) - { - $nameExploded = explode(' ', $name); - $idx = 0; $nameHandled = array(); - if(UTF8::substr($nameExploded[1], 0, 1) == '(') - { + + private static function parsePeopleName($name) { + $nameExploded = explode(' ', $name); + + $idx = 0; + $nameHandled = []; + + // Енотова (Сенченко) Наталья Лин Чу Геннадьевна + if (UTF8::substr($nameExploded[1], 0, 1) == '(') { $idx = 1; } + $nameHandled[0] = $nameExploded[0]; $nameHandled[1] = $nameExploded[1 + $idx]; $nameHandled[2] = ''; - for($i = 2 + $idx; $i < count($nameExploded); $i++) - { - $nameHandled[2] .= $nameExploded[$i].' '; + + for ($i = 2 + $idx; $i < count($nameExploded); $i++) { + $nameHandled[2] .= $nameExploded[$i] . ' '; } + return Arr::map('trim', $nameHandled); } } \ No newline at end of file diff --git a/~dev_rating/application/views/admin/students/upload.twig b/~dev_rating/application/views/admin/students/upload.twig index 9a8b3e236..abc7b15af 100644 --- a/~dev_rating/application/views/admin/students/upload.twig +++ b/~dev_rating/application/views/admin/students/upload.twig @@ -9,35 +9,41 @@ {% endblock %} {% block main_content %} - {% if UploadingResult is not empty %} - {% set res = '<ul>' %} - {% for item in UploadingResult %} - {{ res ~ '<li>Строка #' ~ item.Row ~ ': ' ~ item.Info ~ '</li>' }} - {% endfor %} - {{ admin.message(warning, 'Возникли проблемы!', res ~ '</ul>') }} + {% if Errors is not empty %} + {% set res %} + {% for item in Errors %} + <li>Строка #{{ item.Row }}: {{ item.Info }}</li> + {% endfor %} + {% endset %} + + {{ admin.message(warning, 'Возникли проблемы!', '<ul>' ~ res ~ '</ul>') }} {% endif %} + <form enctype="multipart/form-data" action="" method="POST"> - <div class="stepBox" id="FirstStep"> - <div class="step_title"> - <span class='step_title_count'>Шаг 1:</span> <span class='step_title_description'>Выберите подразделение университета</span> - </div> - <div class="step_body"> - <select id="facultySelect" name="facultyID"> - <option value="0">— Подразделение ЮФУ —</option> - {% for row in Faculties %} - <option value="{{ row.ID }}">{{ row.Name }} ({{ row.Abbr }})</option> - {% endfor %} - </select> + <div class="stepBox" id="FirstStep"> + <div class="step_title"> + <span class='step_title_count'>Шаг 1:</span> + <span class='step_title_description'>Выберите подразделение университета</span> + </div> + <div class="step_body"> + <select id="facultySelect" name="facultyID"> + <option value="0" selected="selected">— Подразделение ЮФУ —</option> + {% for row in Faculties %} + <option value="{{ row.ID }}">{{ row.Name }} ({{ row.Abbr }})</option> + {% endfor %} + </select> + </div> </div> - </div> - <div class="stepBox" id="SecondStep"> - <div class="step_title"> - <span class='step_title_count'>Шаг 2:</span> <span class='step_title_description'>Выберите файл для загрузки</span> - </div> - <div class="step_body"> - <input name="students" type="file"> - <input type="submit" value="Send"> + + <div class="stepBox" id="SecondStep"> + <div class="step_title"> + <span class='step_title_count'>Шаг 2:</span> + <span class='step_title_description'>Выберите файл для загрузки</span> + </div> + <div class="step_body"> + <input name="students" type="file"> + <input type="submit" value="Send"> + </div> </div> - </div> </form> {% endblock %} \ No newline at end of file diff --git a/~dev_rating/media/js/admin/students/upload.js b/~dev_rating/media/js/admin/students/upload.js index 00b3f58d5..b7113be60 100644 --- a/~dev_rating/media/js/admin/students/upload.js +++ b/~dev_rating/media/js/admin/students/upload.js @@ -1,10 +1,9 @@ $(function() { - $("#facultySelect [value='0']").attr('selected', 'selected'); - - $('#facultySelect').change(function(){ - if (($('#facultySelect option:selected').val()!= '0')) { - $('#SecondStep').css('display', 'block'); - } - }); + var jFaculty = $('#facultySelect'); + + jFaculty.change(function() { + var item = jFaculty.find('option:selected').val(); + $('#SecondStep').css('display', Number(item) ? 'block' : 'none'); + }); }); \ No newline at end of file -- GitLab