diff --git a/mod/quiz/renderer.php b/mod/quiz/renderer.php index 955c8139dca..6b1d2a02f3f 100644 --- a/mod/quiz/renderer.php +++ b/mod/quiz/renderer.php @@ -39,7 +39,8 @@ class mod_quiz_renderer extends plugin_renderer_base { $output .= $this->header(); $output .= $this->review_summary_table($summarydata, $page); $output .= $this->review_form($page, $showall, $displayoptions, - $this->questions($attemptobj, true, $slots, $page, $showall, $displayoptions)); + $this->questions($attemptobj, true, $slots, $page, $showall, $displayoptions), + $attemptobj, $showall); $output .= $this->review_next_navigation($attemptobj, $page, $lastpage); $output .= $this->footer(); @@ -106,7 +107,8 @@ class mod_quiz_renderer extends plugin_renderer_base { return $output; } - public function review_form($summarydata, $page, $displayoptions, $content) { + public function review_form($summarydata, $page, $displayoptions, $content, $attemptobj, + $showall) { if ($displayoptions->flags != question_display_options::EDITABLE) { return $content; } @@ -124,9 +126,9 @@ class mod_quiz_renderer extends plugin_renderer_base { $output .= html_writer::empty_tag('input', array('type' => 'submit', 'class' => 'questionflagsavebutton', 'name' => 'savingflags', 'value' => get_string('saveflags', 'question'))); - $output .= html_writer::eng_tag('div'); - $output .= html_writer::eng_tag('div'); - $output .= html_writer::eng_tag('form'); + $output .= html_writer::end_tag('div'); + $output .= html_writer::end_tag('div'); + $output .= html_writer::end_tag('form'); return $output; } @@ -411,6 +413,305 @@ class mod_quiz_renderer extends plugin_renderer_base { private function summary_get_timer($attemptobj){ return $attemptobj->get_timer_html(); } + + /* + * View Page + */ + public function view_page($course, $quiz, $cm, $context, $viewobj){ + $output = ''; + $output .= $this->view_information($quiz, $cm, $context, $viewobj); + $output .= $this->view_table($quiz, $context, $viewobj); + $output .= $this->view_best_score($viewobj); + $output .= $this->view_result_info($quiz, $context, $cm, $viewobj); + $output .= $this->view_attempt_button($course, $quiz, $cm, $context, $viewobj); + return $output; + } + + private function view_information($quiz, $cm, $context, $viewobj){ + global $CFG; + $output = ''; + // Print quiz name and description + $output .= $this->heading(format_string($quiz->name)); + if (trim(strip_tags($quiz->intro))) { + $output .= $this->box(format_module_intro('quiz', $quiz, $cm->id), 'generalbox', 'intro'); + } + + // Display information about this quiz. + $messages = $viewobj->accessmanager->describe_rules(); + if ($quiz->attempts != 1) { + $messages[] = get_string('gradingmethod', 'quiz', + quiz_get_grading_option_name($quiz->grademethod)); + } + $output .= $this->box_start('quizinfo'); + $this->access_messages($messages); + $output .= $this->box_end(); + + // Show number of attempts summary to those who can view reports. + if (has_capability('mod/quiz:viewreports', $context)) { + if ($strattemptnum = quiz_attempt_summary_link_to_reports($quiz, $cm, $context)) { + $output .= '
' . get_string('guestsno', 'quiz') . "
\n\n" . + get_string('liketologin') . "
\n", get_login_url(), get_referer(false)); + echo $this->footer(); + exit; + } + + // If they are not enrolled in this course in a good enough role, tell them to enrol. + if (!($viewobj->canattempt || $viewobj->canpreview || $viewobj->canreviewmine)) { + echo $this->box('' . get_string('youneedtoenrol', 'quiz') . "
\n\n" . + $this->continue_button($CFG->wwwroot . '/course/view.php?id=' . $course->id) . + "
\n", 'generalbox', 'notice'); + echo $this->footer(); + exit; + } + return $output; + } + + private function view_table($quiz, $context, $viewobj){ + $output = ''; + if ($viewobj->attempts) { + $output .= $this->heading(get_string('summaryofattempts', 'quiz')); + + // Prepare table header + $table = new html_table(); + $table->attributes['class'] = 'generaltable quizattemptsummary'; + $table->head = array(); + $table->align = array(); + $table->size = array(); + if ($viewobj->attemptcolumn) { + $table->head[] = get_string('attemptnumber', 'quiz'); + $table->align[] = 'center'; + $table->size[] = ''; + } + $table->head[] = get_string('timecompleted', 'quiz'); + $table->align[] = 'left'; + $table->size[] = ''; + if ($viewobj->markcolumn) { + $table->head[] = get_string('marks', 'quiz') . ' / ' . + quiz_format_grade($quiz, $quiz->sumgrades); + $table->align[] = 'center'; + $table->size[] = ''; + } + if ($viewobj->gradecolumn) { + $table->head[] = get_string('grade') . ' / ' . + quiz_format_grade($quiz, $quiz->grade); + $table->align[] = 'center'; + $table->size[] = ''; + } + if ($viewobj->canreviewmine) { + $table->head[] = get_string('review', 'quiz'); + $table->align[] = 'center'; + $table->size[] = ''; + } + if ($viewobj->feedbackcolumn) { + $table->head[] = get_string('feedback', 'quiz'); + $table->align[] = 'left'; + $table->size[] = ''; + } + if (isset($quiz->showtimetaken)) { + $table->head[] = get_string('timetaken', 'quiz'); + $table->align[] = 'left'; + $table->size[] = ''; + } + + // One row for each attempt + foreach ($viewobj->attempts as $attempt) { + $attemptoptions = quiz_get_review_options($quiz, $attempt, $context); + $row = array(); + + // Add the attempt number, making it a link, if appropriate. + if ($viewobj->attemptcolumn) { + if ($attempt->preview) { + $row[] = get_string('preview', 'quiz'); + } else { + $row[] = $attempt->attempt; + } + } + + // prepare strings for time taken and date completed + $timetaken = ''; + $datecompleted = ''; + if ($attempt->timefinish > 0) { + // attempt has finished + $timetaken = format_time($attempt->timefinish - $attempt->timestart); + $datecompleted = userdate($attempt->timefinish); + } else if (!$quiz->timeclose || $viewobj->timenow < $quiz->timeclose) { + // The attempt is still in progress. + $timetaken = format_time($viewobj->timenow - $attempt->timestart); + $datecompleted = get_string('inprogress', 'quiz'); + } else { + $timetaken = format_time($quiz->timeclose - $attempt->timestart); + $datecompleted = userdate($quiz->timeclose); + } + $row[] = $datecompleted; + + if ($viewobj->markcolumn && $attempt->timefinish > 0) { + if ($attemptoptions->marks >= question_display_options::MARK_AND_MAX) { + $row[] = quiz_format_grade($quiz, $attempt->sumgrades); + } else { + $row[] = ''; + } + } + + // Ouside the if because we may be showing feedback but not grades. + $attemptgrade = quiz_rescale_grade($attempt->sumgrades, $quiz, false); + + if ($viewobj->gradecolumn) { + if ($attemptoptions->marks >= question_display_options::MARK_AND_MAX && + $attempt->timefinish > 0) { + $formattedgrade = quiz_format_grade($quiz, $attemptgrade); + // highlight the highest grade if appropriate + if ($viewobj->overallstats && !$attempt->preview && $viewobj->numattempts > 1 && !is_null($viewobj->mygrade) && + $attemptgrade == $viewobj->mygrade && $quiz->grademethod == QUIZ_GRADEHIGHEST) { + $table->rowclasses[$attempt->attempt] = 'bestrow'; + } + + $row[] = $formattedgrade; + } else { + $row[] = ''; + } + } + + if ($viewobj->canreviewmine) { + $row[] = $viewobj->accessmanager->make_review_link($attempt, $viewobj->canpreview, $attemptoptions); + } + + if ($viewobj->feedbackcolumn && $attempt->timefinish > 0) { + if ($attemptoptions->overallfeedback) { + $row[] = quiz_feedback_for_grade($attemptgrade, $quiz, $context, $cm); + } else { + $row[] = ''; + } + } + + if (isset($quiz->showtimetaken)) { + $row[] = $timetaken; + } + + if ($attempt->preview) { + $table->data['preview'] = $row; + } else { + $table->data[$attempt->attempt] = $row; + } + } // End of loop over attempts. + $output .= html_writer::table($table); + } + return $output; + } + + private function view_best_score($viewobj){ + $output = ''; + // Print information about the student's best score for this quiz if possible. + if (!$viewobj->moreattempts) { + $output .= $this->heading(get_string('nomoreattempts', 'quiz')); + } + return $output; + } + + private function view_result_info($quiz, $context, $cm, $viewobj){ + $output = ''; + if ($viewobj->numattempts && $viewobj->gradecolumn && !is_null($viewobj->mygrade)) { + $resultinfo = ''; + + if ($viewobj->overallstats) { + if ($viewobj->moreattempts) { + $a = new stdClass(); + $a->method = quiz_get_grading_option_name($quiz->grademethod); + $a->mygrade = quiz_format_grade($quiz, $viewobj->mygrade); + $a->quizgrade = quiz_format_grade($quiz, $quiz->grade); + $resultinfo .= $this->heading(get_string('gradesofar', 'quiz', $a), 2, 'main'); + } else { + $a = new stdClass(); + $a->grade = quiz_format_grade($quiz, $viewobj->mygrade); + $a->maxgrade = quiz_format_grade($quiz, $quiz->grade); + $a = get_string('outofshort', 'quiz', $a); + $resultinfo .= $this->heading(get_string('yourfinalgradeis', 'quiz', $a), 2, 'main'); + } + } + + if ($viewobj->mygradeoverridden) { + $resultinfo .= '' . + get_string('overriddennotice', 'grades') . "
\n"; + } + if ($viewobj->gradebookfeedback) { + $resultinfo .= $this->heading(get_string('comment', 'quiz'), 3, 'main'); + $resultinfo .= ''.$viewobj->gradebookfeedback."
\n"; + } + if ($viewobj->feedbackcolumn) { + $resultinfo .= $this->heading(get_string('overallfeedback', 'quiz'), 3, 'main'); + $resultinfo .= '' . + quiz_feedback_for_grade($viewobj->mygrade, $quiz, $context, $cm) . "
\n"; + } + + if ($resultinfo) { + $output .= $this->box($resultinfo, 'generalbox', 'feedback'); + } + } + return $output; + } + + private function view_attempt_button($course, $quiz, $cm, $context, $viewobj){ + $output = ''; + // Determine if we should be showing a start/continue attempt button, + // or a button to go back to the course page. + $output .= $this->box_start('quizattempt'); + $buttontext = ''; // This will be set something if as start/continue attempt button should appear. + if (!quiz_clean_layout($quiz->questions, true)) { + $output .= quiz_no_questions_message($quiz, $cm, $context); + $buttontext = ''; + + } else { + if ($viewobj->unfinished) { + if ($viewobj->canattempt) { + $buttontext = get_string('continueattemptquiz', 'quiz'); + } else if ($viewobj->canpreview) { + $buttontext = get_string('continuepreview', 'quiz'); + } + + } else { + if ($viewobj->canattempt) { + $messages = $viewobj->accessmanager->prevent_new_attempt($viewobj->numattempts, $viewobj->lastfinishedattempt); + if ($messages) { + $this->access_messages($messages); + } else if ($viewobj->numattempts == 0) { + $buttontext = get_string('attemptquiznow', 'quiz'); + } else { + $buttontext = get_string('reattemptquiz', 'quiz'); + } + + } else if ($viewobj->canpreview) { + $buttontext = get_string('previewquiznow', 'quiz'); + } + } + + // If, so far, we think a button should be printed, so check if they will be + // allowed to access it. + if ($buttontext) { + if (!$viewobj->moreattempts) { + $buttontext = ''; + } else if ($viewobj->canattempt && $messages = $viewobj->accessmanager->prevent_access()) { + $this->access_messages($messages); + $buttontext = ''; + } + } + } + + // Now actually print the appropriate button. + if ($buttontext) { + $viewobj->accessmanager->print_start_attempt_button($viewobj->canpreview, $buttontext, $viewobj->unfinished); + } else if ($buttontext === '') { + $output .= $this->single_button(new moodle_url('/course/view.php', array('id' => $course->id)), + get_string('backtocourse', 'quiz'), 'get', array('class' => 'continuebutton')); + } + $output .= $this->box_end(); + + return $output; + } } class mod_quiz_links_to_other_attempts implements renderable { diff --git a/mod/quiz/summary.php b/mod/quiz/summary.php index eab2ba2bbb1..25135d74db6 100644 --- a/mod/quiz/summary.php +++ b/mod/quiz/summary.php @@ -99,7 +99,7 @@ if ($accessmanager->securewindow_required($attemptobj->is_preview_user())) { echo $OUTPUT->heading(format_string($attemptobj->get_quiz_name())); echo $OUTPUT->heading($title, 3); -$output->summary_page($attemptobj, $displayoptions); +echo $output->summary_page($attemptobj, $displayoptions); // Finish the page $accessmanager->show_attempt_timer_if_needed($attemptobj->get_attempt(), time()); diff --git a/mod/quiz/view.php b/mod/quiz/view.php index db383a9da6d..35acd760c2e 100644 --- a/mod/quiz/view.php +++ b/mod/quiz/view.php @@ -82,52 +82,6 @@ if ($edit != -1 && $PAGE->user_allowed_editing()) { $USER->editing = $edit; } -$title = $course->shortname . ': ' . format_string($quiz->name); -$PAGE->set_title($title); -$PAGE->set_heading($course->fullname); -$output = $PAGE->get_renderer('mod_quiz'); -echo $OUTPUT->header(); - -// Print quiz name and description -echo $OUTPUT->heading(format_string($quiz->name)); -if (trim(strip_tags($quiz->intro))) { - echo $OUTPUT->box(format_module_intro('quiz', $quiz, $cm->id), 'generalbox', 'intro'); -} - -// Display information about this quiz. -$messages = $accessmanager->describe_rules(); -if ($quiz->attempts != 1) { - $messages[] = get_string('gradingmethod', 'quiz', - quiz_get_grading_option_name($quiz->grademethod)); -} -echo $OUTPUT->box_start('quizinfo'); -$output->access_messages($messages); -echo $OUTPUT->box_end(); - -// Show number of attempts summary to those who can view reports. -if (has_capability('mod/quiz:viewreports', $context)) { - if ($strattemptnum = quiz_attempt_summary_link_to_reports($quiz, $cm, $context)) { - echo '' . get_string('guestsno', 'quiz') . "
\n\n" . - get_string('liketologin') . "
\n", get_login_url(), get_referer(false)); - echo $OUTPUT->footer(); - exit; -} - -// If they are not enrolled in this course in a good enough role, tell them to enrol. -if (!($canattempt || $canpreview || $canreviewmine)) { - echo $OUTPUT->box('' . get_string('youneedtoenrol', 'quiz') . "
\n\n" . - $OUTPUT->continue_button($CFG->wwwroot . '/course/view.php?id=' . $course->id) . - "
\n", 'generalbox', 'notice'); - echo $OUTPUT->footer(); - exit; -} - // Update the quiz with overrides for the current user $quiz = quiz_update_effective_access($quiz, $USER->id); @@ -164,9 +118,6 @@ if (!empty($grading_info->items)) { // Print table with existing attempts if ($attempts) { - - echo $OUTPUT->heading(get_string('summaryofattempts', 'quiz')); - // Work out which columns we need, taking account what data is available in each attempt. list($someoptions, $alloptions) = quiz_get_combined_reviewoptions($quiz, $attempts, $context); @@ -178,228 +129,38 @@ if ($attempts) { $overallstats = $alloptions->marks >= question_display_options::MARK_AND_MAX; $feedbackcolumn = quiz_has_feedback($quiz) && $alloptions->overallfeedback; - - // Prepare table header - $table = new html_table(); - $table->attributes['class'] = 'generaltable quizattemptsummary'; - $table->head = array(); - $table->align = array(); - $table->size = array(); - if ($attemptcolumn) { - $table->head[] = get_string('attemptnumber', 'quiz'); - $table->align[] = 'center'; - $table->size[] = ''; - } - $table->head[] = get_string('timecompleted', 'quiz'); - $table->align[] = 'left'; - $table->size[] = ''; - if ($markcolumn) { - $table->head[] = get_string('marks', 'quiz') . ' / ' . - quiz_format_grade($quiz, $quiz->sumgrades); - $table->align[] = 'center'; - $table->size[] = ''; - } - if ($gradecolumn) { - $table->head[] = get_string('grade') . ' / ' . - quiz_format_grade($quiz, $quiz->grade); - $table->align[] = 'center'; - $table->size[] = ''; - } - if ($canreviewmine) { - $table->head[] = get_string('review', 'quiz'); - $table->align[] = 'center'; - $table->size[] = ''; - } - if ($feedbackcolumn) { - $table->head[] = get_string('feedback', 'quiz'); - $table->align[] = 'left'; - $table->size[] = ''; - } - if (isset($quiz->showtimetaken)) { - $table->head[] = get_string('timetaken', 'quiz'); - $table->align[] = 'left'; - $table->size[] = ''; - } - - // One row for each attempt - foreach ($attempts as $attempt) { - $attemptoptions = quiz_get_review_options($quiz, $attempt, $context); - $row = array(); - - // Add the attempt number, making it a link, if appropriate. - if ($attemptcolumn) { - if ($attempt->preview) { - $row[] = get_string('preview', 'quiz'); - } else { - $row[] = $attempt->attempt; - } - } - - // prepare strings for time taken and date completed - $timetaken = ''; - $datecompleted = ''; - if ($attempt->timefinish > 0) { - // attempt has finished - $timetaken = format_time($attempt->timefinish - $attempt->timestart); - $datecompleted = userdate($attempt->timefinish); - } else if (!$quiz->timeclose || $timenow < $quiz->timeclose) { - // The attempt is still in progress. - $timetaken = format_time($timenow - $attempt->timestart); - $datecompleted = get_string('inprogress', 'quiz'); - } else { - $timetaken = format_time($quiz->timeclose - $attempt->timestart); - $datecompleted = userdate($quiz->timeclose); - } - $row[] = $datecompleted; - - if ($markcolumn && $attempt->timefinish > 0) { - if ($attemptoptions->marks >= question_display_options::MARK_AND_MAX) { - $row[] = quiz_format_grade($quiz, $attempt->sumgrades); - } else { - $row[] = ''; - } - } - - // Ouside the if because we may be showing feedback but not grades. - $attemptgrade = quiz_rescale_grade($attempt->sumgrades, $quiz, false); - - if ($gradecolumn) { - if ($attemptoptions->marks >= question_display_options::MARK_AND_MAX && - $attempt->timefinish > 0) { - $formattedgrade = quiz_format_grade($quiz, $attemptgrade); - // highlight the highest grade if appropriate - if ($overallstats && !$attempt->preview && $numattempts > 1 && !is_null($mygrade) && - $attemptgrade == $mygrade && $quiz->grademethod == QUIZ_GRADEHIGHEST) { - $table->rowclasses[$attempt->attempt] = 'bestrow'; - } - - $row[] = $formattedgrade; - } else { - $row[] = ''; - } - } - - if ($canreviewmine) { - $row[] = $accessmanager->make_review_link($attempt, $canpreview, $attemptoptions); - } - - if ($feedbackcolumn && $attempt->timefinish > 0) { - if ($attemptoptions->overallfeedback) { - $row[] = quiz_feedback_for_grade($attemptgrade, $quiz, $context, $cm); - } else { - $row[] = ''; - } - } - - if (isset($quiz->showtimetaken)) { - $row[] = $timetaken; - } - - if ($attempt->preview) { - $table->data['preview'] = $row; - } else { - $table->data[$attempt->attempt] = $row; - } - } // End of loop over attempts. - echo html_writer::table($table); } -// Print information about the student's best score for this quiz if possible. $moreattempts = $unfinished || !$accessmanager->is_finished($numattempts, $lastfinishedattempt); -if (!$moreattempts) { - echo $OUTPUT->heading(get_string('nomoreattempts', 'quiz')); -} -if ($numattempts && $gradecolumn && !is_null($mygrade)) { - $resultinfo = ''; +$title = $course->shortname . ': ' . format_string($quiz->name); +$PAGE->set_title($title); +$PAGE->set_heading($course->fullname); +$output = $PAGE->get_renderer('mod_quiz'); +echo $OUTPUT->header(); - if ($overallstats) { - if ($moreattempts) { - $a = new stdClass(); - $a->method = quiz_get_grading_option_name($quiz->grademethod); - $a->mygrade = quiz_format_grade($quiz, $mygrade); - $a->quizgrade = quiz_format_grade($quiz, $quiz->grade); - $resultinfo .= $OUTPUT->heading(get_string('gradesofar', 'quiz', $a), 2, 'main'); - } else { - $a = new stdClass(); - $a->grade = quiz_format_grade($quiz, $mygrade); - $a->maxgrade = quiz_format_grade($quiz, $quiz->grade); - $a = get_string('outofshort', 'quiz', $a); - $resultinfo .= $OUTPUT->heading(get_string('yourfinalgradeis', 'quiz', $a), 2, 'main'); - } - } +$viewobj->attempts = $attempts; +$viewobj->accessmanager = $accessmanager; +$viewobj->canattempt = $canattempt; +$viewobj->canpreview = $canpreview; +$viewobj->canreviewmine = $canreviewmine; +$viewobj->attemptcolumn = $attemptcolumn; +$viewobj->markcolumn = $markcolumn; +$viewobj->gradecolumn = $gradecolumn; +$viewobj->feedbackcolumn = $feedbackcolumn; +$viewobj->timenow = $timenow; +$viewobj->overallstats = $overallstats; +$viewobj->numattempts = $numattempts; +$viewobj->mygrade = $mygrade; +$viewobj->moreattempts = $moreattempts; +$viewobj->numattempts = $numattempts; +$viewobj->mygradeoverridden = $mygradeoverridden; +$viewobj->gradebookfeedback = $gradebookfeedback; +$viewobj->feedbackcolumn = $feedbackcolumn; +$viewobj->unfinished = $unfinished; +$viewobj->lastfinishedattempt = $lastfinishedattempt; - if ($mygradeoverridden) { - $resultinfo .= '' . - get_string('overriddennotice', 'grades') . "
\n"; - } - if ($gradebookfeedback) { - $resultinfo .= $OUTPUT->heading(get_string('comment', 'quiz'), 3, 'main'); - $resultinfo .= ''.$gradebookfeedback."
\n"; - } - if ($feedbackcolumn) { - $resultinfo .= $OUTPUT->heading(get_string('overallfeedback', 'quiz'), 3, 'main'); - $resultinfo .= '' . - quiz_feedback_for_grade($mygrade, $quiz, $context, $cm) . "
\n"; - } - - if ($resultinfo) { - echo $OUTPUT->box($resultinfo, 'generalbox', 'feedback'); - } -} - -// Determine if we should be showing a start/continue attempt button, -// or a button to go back to the course page. -echo $OUTPUT->box_start('quizattempt'); -$buttontext = ''; // This will be set something if as start/continue attempt button should appear. -if (!quiz_clean_layout($quiz->questions, true)) { - echo quiz_no_questions_message($quiz, $cm, $context); - $buttontext = ''; - -} else { - if ($unfinished) { - if ($canattempt) { - $buttontext = get_string('continueattemptquiz', 'quiz'); - } else if ($canpreview) { - $buttontext = get_string('continuepreview', 'quiz'); - } - - } else { - if ($canattempt) { - $messages = $accessmanager->prevent_new_attempt($numattempts, $lastfinishedattempt); - if ($messages) { - $output->access_messages($messages); - } else if ($numattempts == 0) { - $buttontext = get_string('attemptquiznow', 'quiz'); - } else { - $buttontext = get_string('reattemptquiz', 'quiz'); - } - - } else if ($canpreview) { - $buttontext = get_string('previewquiznow', 'quiz'); - } - } - - // If, so far, we think a button should be printed, so check if they will be - // allowed to access it. - if ($buttontext) { - if (!$moreattempts) { - $buttontext = ''; - } else if ($canattempt && $messages = $accessmanager->prevent_access()) { - $output->access_messages($messages); - $buttontext = ''; - } - } -} - -// Now actually print the appropriate button. -if ($buttontext) { - $accessmanager->print_start_attempt_button($canpreview, $buttontext, $unfinished); -} else if ($buttontext === '') { - echo $OUTPUT->single_button(new moodle_url('/course/view.php', array('id' => $course->id)), - get_string('backtocourse', 'quiz'), 'get', array('class' => 'continuebutton')); -} -echo $OUTPUT->box_end(); +echo $output->view_page($course, $quiz, $cm, $context, $viewobj); // Mark module as viewed (note, we do this here and not in finish_page, // otherwise the 'not enrolled' error conditions would result in marking