MDL-20636 Updated code to include html writer

This commit is contained in:
Dean Lennard
2011-05-09 12:30:45 +01:00
committed by Tim Hunt
parent 9f335e76fa
commit bcd42560e3
6 changed files with 68 additions and 80 deletions
-21
View File
@@ -95,27 +95,6 @@ class quiz_access_manager {
}
}
/**
* Print each message in an array, surrounded by <p>, </p> tags.
*
* @param array $messages the array of message strings.
* @param bool $return if true, return a string, instead of outputting.
*
* @return mixed, if $return is true, return the string that would have been output, otherwise
* return null.
*/
public function print_messages($messages, $return=false) {
$output = '';
foreach ($messages as $message) {
$output .= '<p>' . $message . "</p>\n";
}
if ($return) {
return $output;
} else {
echo $output;
}
}
/**
* Provide a description of the rules that apply to this quiz, such
* as is shown at the top of the quiz view page. Note that not all
+3 -3
View File
@@ -27,8 +27,6 @@
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
$output = $PAGE->get_renderer('mod_quiz');
// Look for old-style URLs, such as may be in the logs, and redirect them to startattemtp.php
if ($id = optional_param('id', 0, PARAM_INTEGER)) {
redirect($CFG->wwwroot . '/mod/quiz/startattempt.php?cmid=' . $id . '&sesskey=' . sesskey());
@@ -78,8 +76,10 @@ if ($attemptobj->is_finished()) {
// Check the access rules.
$accessmanager = $attemptobj->get_access_manager(time());
$messages = $accessmanager->prevent_access();
$output = $PAGE->get_renderer('mod_quiz');
if (!$attemptobj->is_preview_user() && $messages) {
$output->print_message($attemptobj, $accessmanager, $messages);
print_error('attempterror', 'quiz', $attemptobj->view_url(),
$output->print_messages($messages));
}
$accessmanager->do_password_check($attemptobj->is_preview_user());
+56 -50
View File
@@ -62,8 +62,8 @@ class mod_quiz_renderer extends plugin_renderer_base {
}
public function review_summary_table($summarydata, $page) {
$summarydata = $this->filter_summary_table($summarydata,
$page);
$summarydata = $this->filter_summary_table($summarydata,
$page);
if (empty($summarydata)) {
return '';
}
@@ -97,7 +97,7 @@ class mod_quiz_renderer extends plugin_renderer_base {
}
public function questions(quiz_attempt $attemptobj, $reviewing, $slots, $page, $showall,
mod_quiz_display_options $displayoptions) {
mod_quiz_display_options $displayoptions) {
$output = '';
foreach ($slots as $slot) {
$output .= $attemptobj->render_question($slot, $reviewing,
@@ -116,15 +116,18 @@ class mod_quiz_renderer extends plugin_renderer_base {
// TODO fix this to use html_writer.
$output = '';
$output .= '<form action="' . $attemptobj->review_url(0, $page, $showall) .
'" method="post" class="questionflagsaveform"><div>';
$output .= '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
$output .= $content;
$output .= '<div class="submitbtns">' . "\n" .
'<input type="submit" class="questionflagsavebutton" name="savingflags" value="' .
get_string('saveflags', 'question') . '" />' .
"</div>\n" .
"\n</div></form>\n";
$output .= html_writer::start_tag('form', array('action' => $attemptobj->review_url(0,
$page, $showall), 'method' => 'post', 'class' => 'questionflagsaveform'));
$output .= html_writer::start_tag('div');
$output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey',
'value' => sesskey()));
$output .= html_writer::start_tag('div', array('class' => 'submitbtns'));
$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');
return $output;
}
@@ -229,8 +232,7 @@ class mod_quiz_renderer extends plugin_renderer_base {
}
protected function render_mod_quiz_links_to_other_attempts(
mod_quiz_links_to_other_attempts
$links) {
mod_quiz_links_to_other_attempts $links) {
$attemptlinks = array();
foreach ($links->links as $attempt => $url) {
if ($url) {
@@ -246,33 +248,29 @@ class mod_quiz_renderer extends plugin_renderer_base {
* Attempt Page
*/
public function attempt_page($attemptobj, $page, $accessmanager, $messages, $slots, $id,
$nextpage) {
$nextpage) {
$output = '';
$output .= $this->quiz_notices($attemptobj, $accessmanager, $messages);
$output .= $this->quiz_notices($messages);
$output .= $this->attempt_form($attemptobj, $page, $slots, $id, $nextpage);
return $output;
}
private function quiz_notices($attemptobj, $accessmanager, $messages) {
if ($attemptobj->is_preview_user() && $messages) {
// Inform teachers of any restrictions that would apply to students at this point.
$output = $this->box_start('quizaccessnotices');
$output .= $this->heading(get_string('accessnoticesheader', 'quiz'), 3);
$accessmanager->print_messages($messages);
$output .= $this->box_end();
return $output;
private function quiz_notices($messages) {
if (!$messages) {
return '';
}
return $this->box($this->heading(get_string('accessnoticesheader', 'quiz'), 3) .
$this->access_messages($messages), 'quizaccessnotices');
}
private function attempt_form($attemptobj, $page, $slots, $id, $nextpage) {
// Start the form
//TODO: Convert all html to html:writer
$output = '';
$output .= '<form id="responseform" method="post" action="'.
s($attemptobj->processattempt_url()).
'" enctype="multipart/form-data" accept-charset="utf-8">'. "\n";
$output .= '<div>';
//Start Form
$output .= html_writer::start_tag('form', array('action' => s($attemptobj->processattempt_url()), 'method' => 'post', 'enctype' => 'multipart/form-data', 'accept-charset' => 'utf-8', 'id' => 'responseform'));
$output .= html_writer::start_tag('div');
// Print all the questions
foreach ($slots as $slot) {
@@ -280,37 +278,45 @@ class mod_quiz_renderer extends plugin_renderer_base {
$page));
}
// Print a link to the next page.
$output .= '<div class="submitbtns">';
$output .= '<input type="submit" name="next" value="' . get_string('next') . '" />';
$output .= "</div>";
$output .= html_writer::start_tag('div', array('class' => 'submitbtns'));
$output .= html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'next', 'value' => get_string('next')));
$output .= html_writer::end_tag('div');
// Some hidden fields to trach what is going on.
$output .= '<input type="hidden" name="attempt" value="' . $attemptobj->get_attemptid() .
'" />';
$output .= '<input type="hidden" name="thispage" id="followingpage" value="' . $page .
'" />';
$output .= '<input type="hidden" name="nextpage" value="' . $nextpage . '" />';
$output .= '<input type="hidden" name="timeup" id="timeup" value="0" />';
$output .= '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
$output .= '<input type="hidden" name="scrollpos" id="scrollpos" value="" />';
$output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'attempt', 'value' => $attemptobj->get_attemptid()));
$output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'thispage', 'value' => $page, 'id' => 'followingpage'));
$output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'nextpage', 'value' => $nextpage));
$output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'timeup', 'value' => '0', 'id' => 'timeup'));
$output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
$output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'scrollpos', 'value' => '', 'id' => 'scrollpos'));
// Add a hidden field with questionids. Do this at the end of the form, so
// if you navigate before the form has finished loading, it does not wipe all
// the student's answers.
$output .= '<input type="hidden" name="slots" value="' .
implode(',', $slots) . "\" />\n";
// Finish the form
$output .= '</div>';
$output .= "</form>\n";
$output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'slots', 'value' => implode(',', $slots)));
//Finish form
$output .= html_writer::end_tag('div');
$output .= html_writer::end_tag('form');
return $output;
}
public function print_message($attemptobj, $accessmanager, $messages) {
print_error('attempterror', 'quiz', $attemptobj->view_url(),
$accessmanager->print_messages($messages, true));
/**
* Print each message in an array, surrounded by &lt;p>, &lt;/p> tags.
*
* @param array $messages the array of message strings.
* @param bool $return if true, return a string, instead of outputting.
*
* @return mixed, if $return is true, return the string that would have been output, otherwise
* return null.
*/
public function print_messages($messages) {
$output = '';
foreach ($messages as $message) {
$output .= html_writer::tag('p', $message) . "\n";
}
return $output;
}
}
+3 -2
View File
@@ -97,10 +97,11 @@ if ($lastattempt && !$lastattempt->preview && !$quizobj->is_preview_user()) {
// Check access.
$messages = $accessmanager->prevent_access() +
$accessmanager->prevent_new_attempt($attemptnumber - 1, $lastattempt);
$accessmanager->prevent_new_attempt($attemptnumber - 1, $lastattempt);
$output = $PAGE->get_renderer('mod_quiz');
if (!$quizobj->is_preview_user() && $messages) {
print_error('attempterror', 'quiz', $quizobj->view_url(),
$accessmanager->print_messages($messages, true));
$output->print_messages($messages));
}
$accessmanager->do_password_check($quizobj->is_preview_user());
+2 -1
View File
@@ -59,9 +59,10 @@ if ($attemptobj->is_preview_user()) {
// Check access.
$accessmanager = $attemptobj->get_access_manager(time());
$messages = $accessmanager->prevent_access();
$output = $PAGE->get_renderer('mod_quiz');
if (!$attemptobj->is_preview_user() && $messages) {
print_error('attempterror', 'quiz', $attemptobj->view_url(),
$accessmanager->print_messages($messages, true));
$output->print_messages($messages));
}
$accessmanager->do_password_check($attemptobj->is_preview_user());
+4 -3
View File
@@ -85,6 +85,7 @@ if ($edit != -1 && $PAGE->user_allowed_editing()) {
$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
@@ -100,7 +101,7 @@ if ($quiz->attempts != 1) {
quiz_get_grading_option_name($quiz->grademethod));
}
echo $OUTPUT->box_start('quizinfo');
$accessmanager->print_messages($messages);
$output->print_messages($messages);
echo $OUTPUT->box_end();
// Show number of attempts summary to those who can view reports.
@@ -367,7 +368,7 @@ if (!quiz_clean_layout($quiz->questions, true)) {
if ($canattempt) {
$messages = $accessmanager->prevent_new_attempt($numattempts, $lastfinishedattempt);
if ($messages) {
$accessmanager->print_messages($messages);
$output->print_messages($messages);
} else if ($numattempts == 0) {
$buttontext = get_string('attemptquiznow', 'quiz');
} else {
@@ -385,7 +386,7 @@ if (!quiz_clean_layout($quiz->questions, true)) {
if (!$moreattempts) {
$buttontext = '';
} else if ($canattempt && $messages = $accessmanager->prevent_access()) {
$accessmanager->print_messages($messages);
$output->print_messages($messages);
$buttontext = '';
}
}