Skip to content

Commit

Permalink
MDL-27979 Should be an edit link for each question when previewing or…
Browse files Browse the repository at this point in the history
… reviewing a quiz.
  • Loading branch information
timhunt committed Jun 22, 2011
1 parent 3552484 commit da72991
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 18 deletions.
1 change: 1 addition & 0 deletions lang/en/question.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
$string['editcategory'] = 'Edit category';
$string['editingcategory'] = 'Editing a category';
$string['editingquestion'] = 'Editing a question';
$string['editquestion'] = 'Edit question';
$string['editthiscategory'] = 'Edit this category';
$string['emptyxml'] = 'Unkown error - empty imsmanifest.xml';
$string['enabled'] = 'Enabled';
Expand Down
69 changes: 57 additions & 12 deletions mod/quiz/attemptlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,19 +283,27 @@ public function edit_url() {

/**
* @param int $attemptid the id of an attempt.
* @param int $page optional page number to go to in the attempt.
* @return string the URL of that attempt.
*/
public function attempt_url($attemptid) {
public function attempt_url($attemptid, $page = 0) {
global $CFG;
return $CFG->wwwroot . '/mod/quiz/attempt.php?attempt=' . $attemptid;
$url = $CFG->wwwroot . '/mod/quiz/attempt.php?attempt=' . $attemptid;
if ($page) {
$url .= '&page=' . $page;
}
return $url;
}

/**
* @return string the URL of this quiz's edit page. Needs to be POSTed to with a cmid parameter.
*/
public function start_attempt_url() {
return new moodle_url('/mod/quiz/startattempt.php',
array('cmid' => $this->cm->id, 'sesskey' => sesskey()));
public function start_attempt_url($page = 0) {
$params = array('cmid' => $this->cm->id, 'sesskey' => sesskey());
if ($page) {
$params['page'] = $page;
}
return new moodle_url('/mod/quiz/startattempt.php', $params);
}

/**
Expand Down Expand Up @@ -661,6 +669,40 @@ public function get_display_options($reviewing) {
}
}

/**
* Wrapper that the correct mod_quiz_display_options for this quiz at the
* moment.
*
* @param bool $reviewing true for review page, else attempt page.
* @param int $slot which question is being displayed.
* @param moodle_url $thispageurl to return to after the editing form is
* submitted or cancelled. If null, no edit link will be generated.
*
* @return question_display_options the render options for this user on this
* attempt, with extra info to generate an edit link, if applicable.
*/
public function get_display_options_with_edit_link($reviewing, $slot, $thispageurl) {
$options = clone($this->get_display_options($reviewing));

if (!$thispageurl) {
return $options;
}

if (!($reviewing || $this->is_preview())) {
return $options;
}

$question = $this->quba->get_question($slot);
if (!question_has_capability_on($question, 'edit', $question->category)) {
return $options;
}

$options->editquestionparams['cmid'] = $this->get_cmid();
$options->editquestionparams['returnurl'] = $thispageurl;

return $options;
}

/**
* @param int $page page number
* @return bool true if this is the last page of the quiz.
Expand Down Expand Up @@ -802,8 +844,13 @@ public function view_url() {
/**
* @return string the URL of this quiz's edit page. Needs to be POSTed to with a cmid parameter.
*/
public function start_attempt_url() {
return $this->quizobj->start_attempt_url();
public function start_attempt_url($slot = null, $page = -1) {
if ($page == -1 && !is_null($slot)) {
$page = $this->quba->get_question($slot)->_page;
} else {
$page = 0;
}
return $this->quizobj->start_attempt_url($page);
}

/**
Expand Down Expand Up @@ -896,12 +943,12 @@ public function restart_preview_button() {
*
* @param int $id the id of a question in this quiz attempt.
* @param bool $reviewing is the being printed on an attempt or a review page.
* @param string $thispageurl the URL of the page this question is being printed on.
* @param moodle_url $thispageurl the URL of the page this question is being printed on.
* @return string HTML for the question in its current state.
*/
public function render_question($slot, $reviewing, $thispageurl = '') {
public function render_question($slot, $reviewing, $thispageurl = null) {
return $this->quba->render_question($slot,
$this->get_display_options($reviewing),
$this->get_display_options_with_edit_link($reviewing, $slot, $thispageurl),
$this->quba->get_question($slot)->_number);
}

Expand All @@ -925,8 +972,6 @@ public function render_question_at_step($slot, $seq, $reviewing, $thispageurl =
* Wrapper round print_question from lib/questionlib.php.
*
* @param int $id the id of a question in this quiz attempt.
* @param bool $reviewing is the being printed on an attempt or a review page.
* @param string $thispageurl the URL of the page this question is being printed on.
*/
public function render_question_for_commenting($slot) {
$options = $this->get_display_options(true);
Expand Down
4 changes: 2 additions & 2 deletions mod/quiz/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ public function review_question_page(quiz_attempt $attemptobj, $slot, $seq,
$output .= $this->review_summary_table($summarydata, 0);

if (!is_null($seq)) {
$output .= $attemptobj->render_question_at_step($slot, $seq, true, $this->page->url);
$output .= $attemptobj->render_question_at_step($slot, $seq, true);
} else {
$output .= $attemptobj->render_question($slot, true, $this->page->url);
$output .= $attemptobj->render_question($slot, true);
}

$output .= $this->close_window_button();
Expand Down
7 changes: 4 additions & 3 deletions mod/quiz/startattempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
require_once($CFG->dirroot . '/mod/quiz/locallib.php');

// Get submitted parameters.
$id = required_param('cmid', PARAM_INT); // Course Module ID
$id = required_param('cmid', PARAM_INT); // Course module id
$forcenew = optional_param('forcenew', false, PARAM_BOOL); // Used to force a new preview
$page = optional_param('page', 0, PARAM_INT); // Page to jump to in the attempt.

if (!$cm = get_coursemodule_from_id('quiz', $id)) {
print_error('invalidcoursemodule');
Expand Down Expand Up @@ -83,7 +84,7 @@
if ($lastattempt && !$lastattempt->timefinish) {
// Continuation of an attempt - check password then redirect.
$accessmanager->do_password_check($quizobj->is_preview_user());
redirect($quizobj->attempt_url($lastattempt->id));
redirect($quizobj->attempt_url($lastattempt->id, $page));
}

// Get number for the next or unfinished attempt
Expand Down Expand Up @@ -219,4 +220,4 @@
$transaction->allow_commit();

// Redirect to the attempt page.
redirect($quizobj->attempt_url($attempt->id));
redirect($quizobj->attempt_url($attempt->id, $page));
14 changes: 14 additions & 0 deletions question/engine/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,20 @@ class question_display_options {
*/
public $history = self::HIDDEN;

/**
* If not empty, then a link to edit the question will be included in
* the info box for the question.
*
* If used, this array must contain an element courseid or cmid.
*
* It shoudl also contain a parameter returnurl => moodle_url giving a
* sensible URL to go back to when the editing form is submitted or cancelled.
*
* @var array url parameter for the edit link. id => questiosnid will be
* added automatically.
*/
public $editquestionparams = array();

/**
* @var int the context the attempt being output belongs to.
*/
Expand Down
23 changes: 23 additions & 0 deletions question/engine/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ protected function info(question_attempt $qa, qbehaviour_renderer $behaviouroutp
$output .= $this->status($qa, $behaviouroutput, $options);
$output .= $this->mark_summary($qa, $options);
$output .= $this->question_flag($qa, $options->flags);
$output .= $this->edit_question_link($qa, $options);
return $output;
}

Expand Down Expand Up @@ -254,6 +255,28 @@ protected function get_flag_html($flagged, $id = '') {
'" alt="' . get_string('flagthisquestion', 'question') . '" />';
}

protected function edit_question_link(question_attempt $qa,
question_display_options $options) {
global $CFG;

if (empty($options->editquestionparams)) {
return '';
}

$params = $options->editquestionparams;
if ($params['returnurl'] instanceof moodle_url) {
$params['returnurl'] = str_replace($CFG->wwwroot, '',
$params['returnurl']->out(false));
}
$params['id'] = $qa->get_question()->id;
$editurl = new moodle_url('/question/question.php', $params);

return html_writer::tag('div', html_writer::link(
$editurl, $this->pix_icon('i/edit', get_string('edit')) .
get_string('editquestion', 'question')),
array('class' => 'editquestion'));
}

/**
* Generate the display of the formulation part of the question. This is the
* area that contains the quetsion text, and the controls for students to
Expand Down
3 changes: 2 additions & 1 deletion theme/base/style/question.css
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ body.jsenabled #qtypechoicecontainer {display: block;}
.que h2.no {margin: 0;font-size: 0.8em;line-height: 1;}
.que span.qno {font-size: 1.5em;font-weight:bold;}
.que .state,
.que .grade {font-size: 0.8em; margin-top: 0.7em;}
.que .grade,
.que .editquestion {font-size: 0.8em; margin-top: 0.7em;}
.que .info .questionflag {margin-top: 1em;margin-right: 1em;text-align: center;}

.que .content {margin: 0 0 0 7.5em;}
Expand Down

0 comments on commit da72991

Please sign in to comment.