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 function getStructure($id, $type) {
        $structure = Model_Map::getRoadmap($id, $type);
PavelBegunkov's avatar
PavelBegunkov committed

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 ) {

            if ( $row['ModuleID'] != $lastModuleID ) {
                $lastModuleID = $row['ModuleID'];
                $structureHandled[++$i] = [
                    'SubmodulesCount' => 0,
                    'MaxRate' => 0,
                    'ModuleTitle' => $row['ModuleName'],
                    'ModuleType' =>  $row['ModuleType'],
                ];
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
            $structureHandled[$i][$j] = $row;
            $curSubmodule = &$structureHandled[$i][$j];
            $curSubmodule['MaxRate'] = (int) $curSubmodule['MaxRate'];
PavelBegunkov's avatar
PavelBegunkov committed

            if ($type == "rate") {
PavelBegunkov's avatar
PavelBegunkov committed
                $curSubmodule['Title'] = $row['SubmoduleName'];
PavelBegunkov's avatar
PavelBegunkov committed
                $maxRate += $row['MaxRate'];
            } else { //$type == "exam"
                if ($row['ModuleType'] == 'extra') {
PavelBegunkov's avatar
PavelBegunkov committed
                    $curSubmodule['Title'] = 'Добор баллов';
PavelBegunkov's avatar
PavelBegunkov committed
                    $maxRate += $row['MaxRate'];
PavelBegunkov's avatar
PavelBegunkov committed
                } else {
PavelBegunkov's avatar
PavelBegunkov committed
                    if ($try === 0) {
PavelBegunkov's avatar
PavelBegunkov committed
                        $curSubmodule['Title'] = 'Основная сдача';
PavelBegunkov's avatar
PavelBegunkov committed
                    } else {
PavelBegunkov's avatar
PavelBegunkov committed
                        $curSubmodule['Title'] = 'Пересдача ' . $try;
PavelBegunkov's avatar
PavelBegunkov committed
                    }
                    $try++;
                }
PavelBegunkov's avatar
PavelBegunkov committed
            }
        $structureHandled['ModulesCount'] = $i;
PavelBegunkov's avatar
PavelBegunkov committed
        $structureHandled['MaxRate'] = (int) $maxRate;
        return $structureHandled;
    }
    private function processGroupInfo($groupInfo) {
PavelBegunkov's avatar
PavelBegunkov committed
        //GroupID, GroupNum, GradeNum, isAttached etc.
        $out = $groupInfo;
PavelBegunkov's avatar
PavelBegunkov committed
        $degree = $groupInfo['Degree'];
PavelBegunkov's avatar
PavelBegunkov committed
        //Формирование заголовка курса
        $gradeNum = $groupInfo['GradeNum'];
PavelBegunkov's avatar
PavelBegunkov committed
        if ( $degree == 'bachelor' || $degree == 'specialist' )
        {
PavelBegunkov's avatar
PavelBegunkov committed
            $out['GradeTitle'] = $gradeNum . ' курс';
PavelBegunkov's avatar
PavelBegunkov committed
        } else if ( $degree == 'master' ) {
PavelBegunkov's avatar
PavelBegunkov committed
            $out['GradeTitle'] = 'Магистратура, ' . $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) {
PavelBegunkov's avatar
PavelBegunkov committed
            $rateResult += $row['Rate'];
PavelBegunkov's avatar
PavelBegunkov committed
            $moduleType = $row['ModuleType'];
PavelBegunkov's avatar
PavelBegunkov committed
            switch ( $moduleType ) {
                case 'exam':
                    $submoduleID = -1;
                break;
                case 'extra':
                    $submoduleID = -2;
                break;
                default:
                    $submoduleID = $row['SubmoduleID'];
            }
            $rates[] = [
                'Type'        => $moduleType,
                'SubmoduleID' => $submoduleID,
                'Rate'        => $row['Rate'],
            ];
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
    }
    private function getRatesForExamPage(&$curStudent, $rate, $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 ($rate as $curRate) {
            if (($curRate['ModuleType'] == 'exam') or ($curRate['ModuleType'] == 'extra')) {
PavelBegunkov's avatar
PavelBegunkov committed
                $curStudent['Rates'][$rowIndex] = [
                    'SubmoduleID'      => $curRate['SubmoduleID'],
                    'Rate'             => $curRate['Rate'],
                    'ModuleType'       => $curRate['ModuleType'],
                    'ExamPeriodOption' => $curRate['ExamPeriodOption'],
                ];
PavelBegunkov's avatar
PavelBegunkov committed
            $rate = $curRate['Rate'];

            switch ($curRate['ModuleType']) {
                case 'regular':
                    $curStudent['RateSemesterResult'] += $rate;
PavelBegunkov's avatar
PavelBegunkov committed
                    break;
PavelBegunkov's avatar
PavelBegunkov committed
                case 'exam':
                    if (!is_null($rate)) {
PavelBegunkov's avatar
PavelBegunkov committed
                        if ($lastExam >= 0) {
                            $curStudent['Rates'][$lastExam]['Block'] = 'True';
                        }
                        $lastExam = $rowIndex;
PavelBegunkov's avatar
PavelBegunkov committed
                    } else {
PavelBegunkov's avatar
PavelBegunkov committed
                        if ($lastNilExam < 0) {
                            $lastNilExam = $rowIndex;
                        } else {
                            $curStudent['Rates'][$rowIndex]['Block'] = 'True';
                        }
PavelBegunkov's avatar
PavelBegunkov committed
                    }
                    break;
PavelBegunkov's avatar
PavelBegunkov committed
                case 'bonus':
                    $curStudent['Bonus'] = $rate;
PavelBegunkov's avatar
PavelBegunkov committed
                    break;
PavelBegunkov's avatar
PavelBegunkov committed
                case 'extra':
                    if ($rate) {
                        if ($lastExtraIndex >= 0) {
                            $curStudent['Rates'][$lastExtraIndex]['Block'] = 'True';
                        }
                        $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 {
                            $curStudent['Rates'][$rowIndex]['Block'] = '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);
PavelBegunkov's avatar
PavelBegunkov committed
     * @param $page_type : rating, exam
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'
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);
pimka's avatar
pimka committed

PavelBegunkov's avatar
PavelBegunkov committed
        $rateHandled = [];
        $groupsHandled = [];
        $groupInd = $studentInd = $curGroupID = 0;

        foreach ($students as $row) {

            if ($curGroupID !== $row['GroupID']) {
                $curGroupID = $row['GroupID'];
                $groupInd++;
                $studentInd = 0;
                $rateHandled[$groupInd] = $this->processGroupInfo($row);
                $groupsHandled[$curGroupID] = $rateHandled[$groupInd]['GroupTitle'];
PavelBegunkov's avatar
PavelBegunkov committed
            // Студенты
PavelBegunkov's avatar
PavelBegunkov committed
            $rateHandled[$groupInd]['Students'][$studentInd] = $row;
            $curStudent = &$rateHandled[$groupInd]['Students'][$studentInd++]; //ID, Name, isAttached
PavelBegunkov's avatar
PavelBegunkov committed
            $curStudent['RateResult'] = 0;
            // Баллы студента
PavelBegunkov's avatar
PavelBegunkov committed
            if ($pageType == 'rate') {
xamgore's avatar
xamgore committed
                $rates_raw = Model_Rating::getRates($id, $row['ID']);
PavelBegunkov's avatar
PavelBegunkov committed
                $curStudent['Rates'] = $this->getRatesForRatingPage($rates_raw, $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);