Exemple #1
0
 function init()
 {
     $this->exam_hash = array_shift($this->param);
     $this->page = array_shift($this->param);
     $course = new Course();
     $training = new Training();
     $t_data = $training->get(array('exam_hash' => $this->exam_hash, 'user_id' => $_SESSION['user_id']));
     $c_data = $course->get(array('id' => $t_data['course_id']));
     switch ($t_data['status_id']) {
         case Training::EXAM:
             // fill table "exams" with questions from table "questions"
             // via cross select course_id and question_id
             if (intval($c_data['exam_duration']) < 1) {
                 $c_data['exam_duration'] = 30;
             }
             $training->update($t_data['id'], array('status_id' => Training::EXAM_STARTED, 'exam_started' => date('Y-m-d H:i', time()), 'finished' => date('Y-m-d H:i', time() + $c_data['exam_duration'] * 60)));
             $question = new Question();
             $enum = $question->enumerateIDs(array('course_id' => $t_data['course_id']));
             foreach ($enum as $val) {
                 $questions[] = $val['id'];
             }
             $exam = new Exam();
             srand();
             shuffle($questions);
             for ($i = 0; $i < $c_data['questions']; $i++) {
                 $question_id = array_shift($questions);
                 if (!isset($question_id)) {
                     break;
                 }
                 $exam->add(array('question_id' => $question_id, 'training_id' => $t_data['id']));
             }
             header("Location: /exam/{$this->exam_hash}/0");
             exit;
             break;
         case Training::EXAM_STARTED:
             if (isset($_POST['answer'])) {
                 if (is_array($_POST['answer'])) {
                     $_POST['answer'] = implode(',', $_POST['answer']);
                 }
                 $exam = new Exam();
                 $e_data = $exam->get(array('id' => $_POST['id'], 'training_id' => $t_data['id']));
                 $exam->update($e_data['id'], array('answer' => $_POST['answer']));
                 $this->page++;
                 header("Location: /exam/{$this->exam_hash}/{$this->page}");
                 exit;
             }
             break;
         default:
             header('Location: /403');
             exit;
     }
 }