Skip to content
Snippets Groups Projects
Rating.php 10.2 KiB
Newer Older
<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Teacher_Rating extends Controller_Environment_Teacher
    public function before() {
        parent::before();

xamgore's avatar
xamgore committed
        // make it possible to download files
        Cookie::set('fD', 'true');

    // Получить из кука SGID выбранную ранее группу для данной дисциплины
PavelBegunkov's avatar
PavelBegunkov committed
    private function getFilterGroupID($disciplineID) {
        $groups = json_decode(Cookie::get('SGID', null), true);
        if ($groups && isset($groups[$disciplineID])) {
            return $groups[$disciplineID];
PavelBegunkov's avatar
PavelBegunkov committed

    private static function getTitle($pageType, $try, &$module) {
        if ($pageType == 'rate') {
            $title = $module['SubmoduleName'];
        } else if ($module['ModuleType'] == 'extra') {
            $title = 'Добор баллов';
        } else if ($try === 0) {
            $title = 'Основная сдача';
        } else {
            $title = 'Пересдача ' . $try;
        }
        return $title;
    }

    // Шапка таблицы: структура УКД (модули и мероприятия)
    private function getStructure($id, $pageType) {
        $structure = Model_Map::getRoadmap($id, $pageType);
PavelBegunkov's avatar
PavelBegunkov committed
        if ($structure->count() == 0) {
            throw HTTP_Exception::factory(404, "Страница не найдена");
PavelBegunkov's avatar
PavelBegunkov committed
        }
        $structureHandled = [];
PavelBegunkov's avatar
PavelBegunkov committed
        $maxRate = $i = $try = 0; // try = 1 - экзамен, = 2, 3 - пересдачи
        $lastModuleID = -1;

        foreach ($structure as $row) {
PavelBegunkov's avatar
PavelBegunkov committed

            if ($row['ModuleID'] != $lastModuleID) {
PavelBegunkov's avatar
PavelBegunkov committed
                $lastModuleID = $row['ModuleID'];
                $structureHandled[++$i] = [
                    'SubmodulesCount' => 0,
                    'MaxRate'         => 0,
                    'ModuleTitle'     => $row['ModuleName'],
                    'ModuleType'      => $row['ModuleType'],
PavelBegunkov's avatar
PavelBegunkov committed
                ];
PavelBegunkov's avatar
PavelBegunkov committed

            $j = $structureHandled[$i]['SubmodulesCount'] += 1;
            $structureHandled[$i]['MaxRate'] += (int) $row['MaxRate'];
PavelBegunkov's avatar
PavelBegunkov committed

PavelBegunkov's avatar
PavelBegunkov committed
            $curSubmodule = &$structureHandled[$i][$j];
PavelBegunkov's avatar
PavelBegunkov committed
            $curSubmodule = $row;
PavelBegunkov's avatar
PavelBegunkov committed
            $curSubmodule['MaxRate'] = (int) $curSubmodule['MaxRate'];
            $curSubmodule['Title'] = self::getTitle($pageType, $try, $row);
PavelBegunkov's avatar
PavelBegunkov committed

            if ($pageType == 'rate' || $row['ModuleType'] == 'extra') {
PavelBegunkov's avatar
PavelBegunkov committed
                $maxRate += $row['MaxRate'];
            } else {
                ++$try;
PavelBegunkov's avatar
PavelBegunkov committed
            }
        $structureHandled['ModulesCount'] = $i;
PavelBegunkov's avatar
PavelBegunkov committed
        $structureHandled['MaxRate'] = (int) $maxRate;
        return $structureHandled;
    }
PavelBegunkov's avatar
PavelBegunkov committed
    // TODO: extract to view
    private function processGroupInfo(&$groupInfo) {
PavelBegunkov's avatar
PavelBegunkov committed
        //GroupID, GroupNum, GradeNum, isAttached etc.
        $out = $groupInfo;
PavelBegunkov's avatar
PavelBegunkov committed
        //Формирование заголовка курса
        $gradeNum = $groupInfo['GradeNum'];
        $out['GradeTitle'] = ($groupInfo['Degree'] == 'master')
            ? 'Магистратура, ' . $gradeNum . ' год'
            : $gradeNum . ' курс';
PavelBegunkov's avatar
PavelBegunkov committed
        //Формирование заголовка группы
PavelBegunkov's avatar
PavelBegunkov committed
        $out['GroupTitle'] = $out['GradeTitle'] . " " . $groupInfo['GroupNum'] . " группа";
PavelBegunkov's avatar
PavelBegunkov committed
        return $out;
    }
PavelBegunkov's avatar
PavelBegunkov committed
    private function getRatesForRatingPage($info, &$rateResult) {
PavelBegunkov's avatar
PavelBegunkov committed
        $rateResult = 0;
PavelBegunkov's avatar
PavelBegunkov committed
        $rates = [];
PavelBegunkov's avatar
PavelBegunkov committed

PavelBegunkov's avatar
PavelBegunkov committed
        foreach ($info as $row) {
            $rateResult += $rate = $row['Rate'];
PavelBegunkov's avatar
PavelBegunkov committed
            $moduleType = $row['ModuleType'];
            $submoduleID =
                ($moduleType == 'exam') ? -1 :
                    (($moduleType == 'extra') ? -2
                        : $row['SubmoduleID']);
PavelBegunkov's avatar
PavelBegunkov committed
            $rates[] = [
                'Type'        => $moduleType,
                'SubmoduleID' => $submoduleID,
                'Rate'        => $rate,
PavelBegunkov's avatar
PavelBegunkov committed
            ];
PavelBegunkov's avatar
PavelBegunkov committed
        }
        return $rates;
    }
    private function correctExtra(&$curStudent, $examType, $lastExtraIndex, $firstEmptyExtraIndex, $totalExtraRate) {
PavelBegunkov's avatar
PavelBegunkov committed
        $bottomLimit = 0;
PavelBegunkov's avatar
PavelBegunkov committed
        $topLimit = ($examType == 'exam') ? 38 : 60;
PavelBegunkov's avatar
PavelBegunkov committed

        if ($curStudent['RateSemesterResult'] >= $bottomLimit &&
            $curStudent['RateSemesterResult'] < $topLimit
        ) // студент задолженик
PavelBegunkov's avatar
PavelBegunkov committed
        {
            $maxExtraRate = $topLimit - $curStudent['RateSemesterResult'];
PavelBegunkov's avatar
PavelBegunkov committed
        }
        if ($lastExtraIndex >= 0) {
            $curStudent['Rates'][$lastExtraIndex]['MaxRate'] = $maxExtraRate - $totalExtraRate
                + $curStudent['Rates'][$lastExtraIndex]['Rate'];
        if ($firstEmptyExtraIndex >= 0) {
            $curStudent['Rates'][$firstEmptyExtraIndex]['MaxRate'] = $maxExtraRate - $totalExtraRate;
        }
PavelBegunkov's avatar
PavelBegunkov committed
    }
PavelBegunkov's avatar
PavelBegunkov committed
    private function getRatesForExamPage(&$curStudent, $rateList, $examType, $disciplineID) {
        $rowIndex = 0;
        $lastExam = $lastExtraIndex = $lastNilExam = $firstEmptyExtra = -1;
        $rateExtra = 0;
PavelBegunkov's avatar
PavelBegunkov committed
        $curStudent['RateSemesterResult'] = 0;
PavelBegunkov's avatar
PavelBegunkov committed

        foreach ($rateList as $curRate) {
            $moduleType = $curRate['ModuleType'];
            if ($moduleType == 'exam' || $moduleType == 'extra') {
PavelBegunkov's avatar
PavelBegunkov committed
                $curStudent['Rates'][$rowIndex] = [
                    'SubmoduleID'      => $curRate['SubmoduleID'],
                    'Rate'             => $curRate['Rate'],
                    'ModuleType'       => $moduleType,
PavelBegunkov's avatar
PavelBegunkov committed
                    'ExamPeriodOption' => $curRate['ExamPeriodOption'],
                ];
PavelBegunkov's avatar
PavelBegunkov committed
            $points = $curRate['Rate'];
PavelBegunkov's avatar
PavelBegunkov committed
                case 'regular':
PavelBegunkov's avatar
PavelBegunkov committed
                    $curStudent['RateSemesterResult'] += $points;
PavelBegunkov's avatar
PavelBegunkov committed
                    break;
PavelBegunkov's avatar
PavelBegunkov committed
                case 'exam':
PavelBegunkov's avatar
PavelBegunkov committed
                    if (!is_null($points)) {
PavelBegunkov's avatar
PavelBegunkov committed
                        if ($lastExam >= 0) {
PavelBegunkov's avatar
PavelBegunkov committed
                            $curStudent['Rates'][$lastExam]['Blocked'] = true;
PavelBegunkov's avatar
PavelBegunkov committed
                        }
                        $lastExam = $rowIndex;
PavelBegunkov's avatar
PavelBegunkov committed
                    } else {
PavelBegunkov's avatar
PavelBegunkov committed
                        if ($lastNilExam < 0) {
                            $lastNilExam = $rowIndex;
                        } else {
PavelBegunkov's avatar
PavelBegunkov committed
                            $curStudent['Rates'][$rowIndex]['Blocked'] = true;
PavelBegunkov's avatar
PavelBegunkov committed
                    }
                    break;
PavelBegunkov's avatar
PavelBegunkov committed
                case 'bonus':
PavelBegunkov's avatar
PavelBegunkov committed
                    $curStudent['Bonus'] = $points;
PavelBegunkov's avatar
PavelBegunkov committed
                    break;
PavelBegunkov's avatar
PavelBegunkov committed
                case 'extra':
PavelBegunkov's avatar
PavelBegunkov committed
                    if ($points) {
PavelBegunkov's avatar
PavelBegunkov committed
                        if ($lastExtraIndex >= 0) {
PavelBegunkov's avatar
PavelBegunkov committed
                            $curStudent['Rates'][$lastExtraIndex]['Blocked'] = true;
PavelBegunkov's avatar
PavelBegunkov committed
                        }
                        $lastExtraIndex = $rowIndex;
                        $curStudent['Rate'] += $curRate['Rate'];
                        $rateExtra += $curRate['Rate'];
PavelBegunkov's avatar
PavelBegunkov committed
                    } else {
PavelBegunkov's avatar
PavelBegunkov committed
                        if ($firstEmptyExtra < 0) {
                            $firstEmptyExtra = $rowIndex;
                        } else {
PavelBegunkov's avatar
PavelBegunkov committed
                            $curStudent['Rates'][$rowIndex]['Blocked'] = true;
PavelBegunkov's avatar
PavelBegunkov committed
                    }
PavelBegunkov's avatar
PavelBegunkov committed
                    break;
                default:
                    throw HTTP_Exception::factory(500, "Некорректный тип модуля!");
PavelBegunkov's avatar
PavelBegunkov committed
//        $total = Model_Rating->GetStudentRate($curStudent['ID'], $disciplineID);
PavelBegunkov's avatar
PavelBegunkov committed
//        $total = ($total[0])?$total[0]['Num']:0;
//        if ($total)
//            $total = 0;
PavelBegunkov's avatar
PavelBegunkov committed
        $curStudent['RateResult'] = $curStudent['RateSemesterResult'] + $rateExtra;
PavelBegunkov's avatar
PavelBegunkov committed
        if ($lastExam >= 0) {
            $curStudent['RateResult'] += $curStudent['Rates'][$lastExam]['Rate'];
        }
        $this->correctExtra($curStudent, $examType, $lastExtraIndex, $firstEmptyExtra, $rateExtra);
    private function showRatingHistory($disciplineID) {
        $this->twig->set([
            'log' =>  Model_Rating::getHistory($disciplineID),
        ])->set_filename('teacher/history');
    }

xamgore's avatar
xamgore committed
    /**
     * @throws HTTP_Exception
     */
PavelBegunkov's avatar
PavelBegunkov committed
    protected function action_rate() {
xamgore's avatar
xamgore committed
        $id = $this->request->param('id');
        $pageType = $this->request->param('type'); // 'rate', 'exam', 'history'

        if ($pageType == 'history') {
            $this->showRatingHistory($id);
            return;
        }

pimka's avatar
pimka committed

xamgore's avatar
xamgore committed
        $discipline = Model_Discipline::load($id);
PavelBegunkov's avatar
PavelBegunkov committed
        $discipline['GroupID_Filter'] = $this->getFilterGroupID($id);
        $discipline['LocalizedExamType'] = RusLang::tr($discipline->Type);

xamgore's avatar
xamgore committed
        // Студенты и их баллы
PavelBegunkov's avatar
PavelBegunkov committed
        $students = Model_Rating::GetStudentsForRating($id);
PavelBegunkov's avatar
PavelBegunkov committed
        $rateHandled = [];
        $groupsHandled = [];
        $groupInd = $studentInd = $curGroupID = 0;

        foreach ($students as $row) {

            if ($curGroupID !== $row['GroupID']) {
                $curGroupID = $row['GroupID'];
                $studentInd = 0;
                $group = &$rateHandled[++$groupInd];
                $group = $this->processGroupInfo($row);
                $groupsHandled[$curGroupID] = $group['GroupTitle'];
PavelBegunkov's avatar
PavelBegunkov committed
            // Студенты
            $group['Students'][$studentInd] = $row;
            $curStudent = &$group['Students'][$studentInd++]; //ID, Name, isAttached
            // Баллы студента
PavelBegunkov's avatar
PavelBegunkov committed
            if ($pageType == 'rate') {
PavelBegunkov's avatar
PavelBegunkov committed
                $ratesRaw = Model_Rating::getRates($id, $row['ID']);
                $curStudent['Rates'] = $this->getRatesForRatingPage($ratesRaw, $curStudent['RateResult']);
PavelBegunkov's avatar
PavelBegunkov committed
            } else {
xamgore's avatar
xamgore committed
                $rate = Model_Rating::getExamRates($id, $row['ID']);
xamgore's avatar
xamgore committed
                $this->getRatesForExamPage($curStudent, $rate, $discipline->Type, $id);
xamgore's avatar
xamgore committed

PavelBegunkov's avatar
PavelBegunkov committed
        $twigType = ($pageType == 'rate') ? 'rating' : $pageType;
xamgore's avatar
xamgore committed
        // Шапка таблицы: структура УКД (модули и мероприятия)
        $this->twig->set([
PavelBegunkov's avatar
PavelBegunkov committed
            'groups'          => $groupsHandled,
            'rateTable'       => $rateHandled,
            'headerRate'      => $this->getStructure($id, $pageType),
xamgore's avatar
xamgore committed
            'Discipline'      => $discipline,
            'Discipline_JSON' => json_encode($discipline),
PavelBegunkov's avatar
PavelBegunkov committed
        ])->set_filename("teacher/" . $twigType);