Example #1
0
 /**
  * Displays the report.
  */
 function display($quiz, $cm, $course)
 {
     global $CFG, $QTYPES;
     $viewoptions = array('mode' => 'grading', 'q' => $quiz->id);
     if ($questionid = optional_param('questionid', 0, PARAM_INT)) {
         $viewoptions += array('questionid' => $questionid);
     }
     // grade question specific parameters
     $gradeungraded = optional_param('gradeungraded', 0, PARAM_INT);
     if ($userid = optional_param('userid', 0, PARAM_INT)) {
         $viewoptions += array('userid' => $userid);
     }
     if ($attemptid = optional_param('attemptid', 0, PARAM_INT)) {
         $viewoptions += array('attemptid' => $attemptid);
     }
     if ($gradeall = optional_param('gradeall', 0, PARAM_INT)) {
         $viewoptions += array('gradeall' => $gradeall);
     }
     if ($gradeungraded = optional_param('gradeungraded', 0, PARAM_INT)) {
         $viewoptions += array('gradeungraded' => $gradeungraded);
     }
     if ($gradenextungraded = optional_param('gradenextungraded', 0, PARAM_INT)) {
         $viewoptions += array('gradenextungraded' => $gradenextungraded);
     }
     $this->cm = $cm;
     $this->print_header_and_tabs($cm, $course, $quiz, $reportmode = "grading");
     // Check permissions
     $this->context = get_context_instance(CONTEXT_MODULE, $cm->id);
     if (!has_capability('mod/quiz:grade', $this->context)) {
         notify(get_string('gradingnotallowed', 'quiz_grading'));
         return true;
     }
     $gradeableqs = quiz_report_load_questions($quiz);
     $questionsinuse = implode(',', array_keys($gradeableqs));
     foreach ($gradeableqs as $qid => $question) {
         if (!$QTYPES[$question->qtype]->is_question_manual_graded($question, $questionsinuse)) {
             unset($gradeableqs[$qid]);
         }
     }
     if (empty($gradeableqs)) {
         print_heading(get_string('noessayquestionsfound', 'quiz'));
         return true;
     } else {
         if (count($gradeableqs) == 1) {
             $questionid = array_shift(array_keys($gradeableqs));
         }
     }
     $currentgroup = groups_get_activity_group($this->cm, true);
     $this->users = get_users_by_capability($this->context, array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'), '', '', '', '', $currentgroup, '', false);
     $this->userids = implode(',', array_keys($this->users));
     if (!empty($questionid)) {
         if (!isset($gradeableqs[$questionid])) {
             error("Gradeable question with id {$questionid} not found");
         } else {
             $question =& $gradeableqs[$questionid];
         }
         $question->maxgrade = get_field('quiz_question_instances', 'grade', 'quiz', $quiz->id, 'question', $question->id);
         // Some of the questions code is optimised to work with several questions
         // at once so it wants the question to be in an array. The array key
         // must be the question id.
         $key = $question->id;
         $questions[$key] =& $question;
         // We need to add additional questiontype specific information to
         // the question objects.
         if (!get_question_options($questions)) {
             error("Unable to load questiontype specific question information");
         }
         // This will have extended the question object so that it now holds
         // all the information about the questions that may be needed later.
     }
     add_to_log($course->id, "quiz", "manualgrading", "report.php?mode=grading&q={$quiz->id}", "{$quiz->id}", "{$cm->id}");
     echo '<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>';
     // for overlib
     if ($data = data_submitted()) {
         // post data submitted, process it
         require_sesskey();
         // now go through all of the responses and save them.
         $allok = true;
         foreach ($data->manualgrades as $uniqueid => $response) {
             // get our attempt
             $uniqueid = clean_param($uniqueid, PARAM_INT);
             if (!($attempt = get_record_sql("SELECT * FROM {$CFG->prefix}quiz_attempts " . "WHERE uniqueid = {$uniqueid} AND " . "userid IN ({$this->userids}) AND " . "quiz=" . $quiz->id))) {
                 error('No such attempt ID exists');
             }
             // Load the state for this attempt (The questions array was created earlier)
             $states = get_question_states($questions, $quiz, $attempt);
             // The $states array is indexed by question id but because we are dealing
             // with only one question there is only one entry in this array
             $state =& $states[$question->id];
             // the following will update the state and attempt
             $error = question_process_comment($question, $state, $attempt, $response['comment'], $response['grade']);
             if (is_string($error)) {
                 notify($error);
                 $allok = false;
             } else {
                 if ($state->changed) {
                     // If the state has changed save it and update the quiz grade
                     save_question_session($question, $state);
                     quiz_save_best_grade($quiz, $attempt->userid);
                 }
             }
         }
         if ($allok) {
             notify(get_string('changessaved', 'quiz'), 'notifysuccess');
         } else {
             notify(get_string('changessavedwitherrors', 'quiz'), 'notifysuccess');
         }
     }
     $this->viewurl = new moodle_url($CFG->wwwroot . '/mod/quiz/report.php', $viewoptions);
     /// find out current groups mode
     if ($groupmode = groups_get_activity_groupmode($this->cm)) {
         // Groups are being used
         groups_print_activity_menu($this->cm, $this->viewurl->out(false, array('userid' => 0, 'attemptid' => 0)));
     }
     echo '<div class="quizattemptcounts">' . quiz_num_attempt_summary($quiz, $cm, true, $currentgroup) . '</div>';
     if (empty($this->users)) {
         if ($currentgroup) {
             notify(get_string('nostudentsingroup'));
         } else {
             notify(get_string('nostudentsyet'));
         }
         return true;
     }
     $gradeablequestionids = implode(',', array_keys($gradeableqs));
     $qattempts = quiz_get_total_qas_graded_and_ungraded($quiz, $gradeablequestionids, $this->userids);
     if (empty($qattempts)) {
         notify(get_string('noattemptstoshow', 'quiz'));
         return true;
     }
     $qmenu = array();
     foreach ($gradeableqs as $qid => $questionformenu) {
         $a = new object();
         $a->number = $gradeableqs[$qid]->number;
         $a->name = $gradeableqs[$qid]->name;
         $a->gradedattempts = $qattempts[$qid]->gradedattempts;
         $a->totalattempts = $qattempts[$qid]->totalattempts;
         $a->openspan = '';
         $a->closespan = '';
         $qmenu[$qid] = get_string('questiontitle', 'quiz_grading', $a);
     }
     if (count($gradeableqs) != 1) {
         $qurl = fullclone($this->viewurl);
         $qurl->remove_params('questionid', 'attemptid', 'gradeall', 'gradeungraded', 'gradenextungraded');
         $menu = popup_form($qurl->out() . '&amp;questionid=', $qmenu, 'questionid', $questionid, 'choose', '', '', true);
         echo '<div class="mdl-align">' . $menu . '</div>';
     }
     if (!$questionid) {
         return true;
     }
     $a = new object();
     $a->number = $question->number;
     $a->name = $question->name;
     $a->gradedattempts = $qattempts[$question->id]->gradedattempts;
     $a->totalattempts = $qattempts[$question->id]->totalattempts;
     $a->openspan = '<span class="highlightgraded">';
     $a->closespan = '</span>';
     print_heading(get_string('questiontitle', 'quiz_grading', $a));
     // our 3 different views
     // the first one displays all of the manually graded questions in the quiz
     // with the number of ungraded attempts for each question
     // the second view displays the users who have answered the essay question
     // and all of their attempts at answering the question
     // the third prints the question with a comment
     // and grade form underneath it
     $ungraded = $qattempts[$questionid]->totalattempts - $qattempts[$questionid]->gradedattempts;
     if ($gradenextungraded || $gradeungraded || $gradeall || $userid || $attemptid) {
         $this->print_questions_and_form($quiz, $question, $userid, $attemptid, $gradeungraded, $gradenextungraded, $ungraded);
     } else {
         $this->view_question($quiz, $question, $qattempts[$questionid]->totalattempts, $ungraded);
     }
     return true;
 }
Example #2
0
 /**
  * Displays the report.
  */
 function display($quiz, $cm, $course)
 {
     global $CFG, $QTYPES, $DB, $OUTPUT, $PAGE;
     $viewoptions = array('mode' => 'grading', 'q' => $quiz->id);
     if ($questionid = optional_param('questionid', 0, PARAM_INT)) {
         $viewoptions += array('questionid' => $questionid);
     }
     // grade question specific parameters
     if ($userid = optional_param('userid', 0, PARAM_INT)) {
         $viewoptions += array('userid' => $userid);
     }
     if ($attemptid = optional_param('attemptid', 0, PARAM_INT)) {
         $viewoptions += array('attemptid' => $attemptid);
     }
     if ($gradeall = optional_param('gradeall', 0, PARAM_INT)) {
         $viewoptions += array('gradeall' => $gradeall);
     }
     if ($gradeungraded = optional_param('gradeungraded', 0, PARAM_INT)) {
         $viewoptions += array('gradeungraded' => $gradeungraded);
     }
     if ($gradenextungraded = optional_param('gradenextungraded', 0, PARAM_INT)) {
         $viewoptions += array('gradenextungraded' => $gradenextungraded);
     }
     $this->cm = $cm;
     $this->print_header_and_tabs($cm, $course, $quiz, $reportmode = "grading");
     // Check permissions
     $this->context = get_context_instance(CONTEXT_MODULE, $cm->id);
     if (!has_capability('mod/quiz:grade', $this->context)) {
         echo $OUTPUT->notification(get_string('gradingnotallowed', 'quiz_grading'));
         return true;
     }
     $gradeableqs = quiz_report_load_questions($quiz);
     $questionsinuse = implode(',', array_keys($gradeableqs));
     foreach ($gradeableqs as $qid => $question) {
         if (!$QTYPES[$question->qtype]->is_question_manual_graded($question, $questionsinuse)) {
             unset($gradeableqs[$qid]);
         }
     }
     if (empty($gradeableqs)) {
         echo $OUTPUT->heading(get_string('noessayquestionsfound', 'quiz'));
         return true;
     } else {
         if (count($gradeableqs) == 1) {
             $questionid = array_shift(array_keys($gradeableqs));
         }
     }
     $currentgroup = groups_get_activity_group($this->cm, true);
     $this->users = get_users_by_capability($this->context, array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'), '', '', '', '', $currentgroup, '', false);
     if (!empty($questionid)) {
         if (!isset($gradeableqs[$questionid])) {
             print_error('invalidquestionid', 'quiz_grading', '', $questionid);
         } else {
             $question =& $gradeableqs[$questionid];
         }
         // Some of the questions code is optimised to work with several questions
         // at once so it wants the question to be in an array. The array key
         // must be the question id.
         $key = $question->id;
         $questions[$key] =& $question;
         // We need to add additional questiontype specific information to
         // the question objects.
         if (!get_question_options($questions)) {
             print_error('cannotloadquestioninfo', 'quiz_grading');
         }
         // This will have extended the question object so that it now holds
         // all the information about the questions that may be needed later.
     }
     add_to_log($course->id, "quiz", "manualgrading", "report.php?mode=grading&amp;q={$quiz->id}", "{$quiz->id}", "{$cm->id}");
     if ($data = data_submitted()) {
         // post data submitted, process it
         if (confirm_sesskey() && $this->users) {
             // now go through all of the responses and save them.
             $allok = true;
             foreach ($data->manualgrades as $uniqueid => $response) {
                 // get our attempt
                 $uniqueid = clean_param($uniqueid, PARAM_INT);
                 list($usql, $params) = $DB->get_in_or_equal(array_keys($this->users));
                 if (!($attempt = $DB->get_record_sql("SELECT * FROM {quiz_attempts} " . "WHERE uniqueid = ? AND " . "userid {$usql} AND " . "quiz=?", array_merge(array($uniqueid), $params, array($quiz->id))))) {
                     print_error('invalidattemptid', 'quiz_grading');
                 }
                 // Load the state for this attempt (The questions array was created earlier)
                 $states = get_question_states($questions, $quiz, $attempt);
                 // The $states array is indexed by question id but because we are dealing
                 // with only one question there is only one entry in this array
                 $state =& $states[$question->id];
                 // the following will update the state and attempt
                 $error = question_process_comment($question, $state, $attempt, $response['comment'], FORMAT_HTML, $response['grade']);
                 if (is_string($error)) {
                     echo $OUTPUT->notification($error);
                     $allok = false;
                 } else {
                     if ($state->changed) {
                         // If the state has changed save it and update the quiz grade
                         save_question_session($question, $state);
                         quiz_save_best_grade($quiz, $attempt->userid);
                     }
                 }
             }
             if ($allok) {
                 echo $OUTPUT->notification(get_string('changessaved', 'quiz'), 'notifysuccess');
             } else {
                 echo $OUTPUT->notification(get_string('changessavedwitherrors', 'quiz'), 'notifysuccess');
             }
         }
     }
     $this->viewurl = new moodle_url('/mod/quiz/report.php', $viewoptions);
     /// find out current groups mode
     if ($groupmode = groups_get_activity_groupmode($this->cm)) {
         // Groups are being used
         groups_print_activity_menu($this->cm, $this->viewurl->out(true, array('userid' => 0, 'attemptid' => 0)));
     }
     if (empty($this->users)) {
         if ($currentgroup) {
             echo $OUTPUT->notification(get_string('nostudentsingroup'));
         } else {
             echo $OUTPUT->notification(get_string('nostudentsyet'));
         }
         return true;
     }
     $qattempts = quiz_get_total_qas_graded_and_ungraded($quiz, array_keys($gradeableqs), array_keys($this->users));
     if (empty($qattempts)) {
         echo $OUTPUT->notification(get_string('noattemptstoshow', 'quiz'));
         return true;
     }
     $qmenu = array();
     foreach ($gradeableqs as $qid => $questionformenu) {
         $a = new stdClass();
         $a->number = $gradeableqs[$qid]->number;
         $a->name = $gradeableqs[$qid]->name;
         $a->gradedattempts = $qattempts[$qid]->gradedattempts;
         $a->totalattempts = $qattempts[$qid]->totalattempts;
         $a->openspan = '';
         $a->closespan = '';
         $qmenu[$qid] = get_string('questiontitle', 'quiz_grading', $a);
     }
     if (count($gradeableqs) != 1) {
         $qurl = fullclone($this->viewurl);
         $qurl->remove_params('questionid', 'attemptid', 'gradeall', 'gradeungraded', 'gradenextungraded');
         $menu = $OUTPUT->single_select($qurl, 'questionid', $qmenu, $questionid, array('' => 'choosedots'), 'questionid');
         echo '<div class="mdl-align">' . $menu . '</div>';
     }
     if (!$questionid) {
         return true;
     }
     $a = new stdClass();
     $a->number = $question->number;
     $a->name = $question->name;
     $a->gradedattempts = $qattempts[$question->id]->gradedattempts;
     $a->totalattempts = $qattempts[$question->id]->totalattempts;
     $a->openspan = '<span class="highlightgraded">';
     $a->closespan = '</span>';
     echo $OUTPUT->heading(get_string('questiontitle', 'quiz_grading', $a));
     // our 2 different views
     // the first view allows a user to select a question and
     // displays the users who have answered the essay question
     // and all of their attempts at answering the question
     // the second prints selected attempt answer(s) with a comment
     // and grade form underneath them
     $ungraded = $qattempts[$questionid]->totalattempts - $qattempts[$questionid]->gradedattempts;
     if ($gradenextungraded || $gradeungraded || $gradeall || $userid || $attemptid) {
         $this->print_questions_and_form($quiz, $question, $userid, $attemptid, $gradeungraded, $gradenextungraded, $ungraded);
     } else {
         $this->view_question($quiz, $question, $qattempts[$questionid]->totalattempts, $ungraded);
     }
     return true;
 }