Skip to content
Permalink
Browse files
Merge branch 'MDL-59081-master' of git://github.com/abgreeve/moodle
  • Loading branch information
David Monllaó committed Apr 10, 2019
2 parents 11bfc07 + e39991c commit f7c77ea189ed6bdddf09312ac052631f2e3a9741
@@ -117,9 +117,14 @@
}

$url = new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id, 'pageid'=>$result->newpageid));

if ($lesson->review && !$result->correctanswer && !$result->noanswer && !$result->isessayquestion && !$result->maxattemptsreached) {
// Button to continue the lesson (the page to go is configured by the teacher).
echo $OUTPUT->single_button($url, get_string('reviewquestioncontinue', 'lesson'));
// If both the "Yes, I'd like to try again" and "No, I just want to go on to the next question" point to the same
// page then don't show the "No, I just want to go on to the next question" button. It's confusing.
if ($page->id != $result->newpageid) {
// Button to continue the lesson (the page to go is configured by the teacher).
echo $OUTPUT->single_button($url, get_string('reviewquestioncontinue', 'lesson'));
}
} else {
// Normal continue button
echo $OUTPUT->single_button($url, get_string('continue', 'lesson'));
@@ -137,6 +137,8 @@
}
$answerscount++;
}
// Let the lesson pages make updates if required.
$data = $editpage->update_form_data($data);

$mform->set_data($data);
$PAGE->navbar->add(get_string('edit'), new moodle_url('/mod/lesson/edit.php', array('id'=>$id)));
@@ -59,6 +59,9 @@
$string['addpage'] = 'Add a page';
$string['addshortanswer'] = 'Create a Short answer question page';
$string['addtruefalse'] = 'Create a True/false question page';
$string['allotheranswers'] = 'All other answers';
$string['allotheranswersjump'] = 'All other answers jump';
$string['allotheranswersscore'] = 'All other answers score';
$string['allowofflineattempts'] = 'Allow lesson to be attempted offline using the mobile app';
$string['allowofflineattempts_help'] = 'If enabled, a mobile app user can download the lesson and attempt it offline.
All the possible answers and correct responses will be downloaded as well.
@@ -162,7 +165,7 @@
$string['displayleftmenu_help'] = 'If enabled, a menu allowing users to navigate through the list of pages is displayed.';
$string['displayofgrade'] = 'Display of grade (for students only)';
$string['displayreview'] = 'Provide option to try a question again';
$string['displayreview_help'] = 'If enabled, when a question is answered incorrectly, the student is given the option to try it again for no point credit, or continue with the lesson.';
$string['displayreview_help'] = 'If enabled, when a question is answered incorrectly, the student is given the option to try it again for no point credit, or continue with the lesson. If the student clicks to move on to another question then the selected (wrong) answer will be followed. By default wrong answer jumps are set to "this page" and have a score of 0, so it is recommended that you set the wrong answer jump to a different page to avoid confusion with your students.';
$string['displayscorewithessays'] = '<p>You earned {$a->score} out of {$a->tempmaxgrade} for the automatically graded questions.</p>
<p>Your {$a->essayquestions} essay question(s) will be graded and added into your final score at a later date.</p>
<p>Your current grade without the essay question(s) is {$a->score} out of {$a->grade}.</p>';
@@ -61,6 +61,9 @@
/** Answer format is HTML */
define("LESSON_ANSWER_HTML", "HTML");

/** Placeholder answer for all other answers. */
define("LESSON_OTHER_ANSWERS", "@#wronganswer#@");

//////////////////////////////////////////////////////////////////////////////////////
/// Any other lesson functions go here. Each of them must have a name that
/// starts with lesson_
@@ -4456,7 +4459,7 @@ public function update($properties, $context = null, $maxbytes = null) {
$DB->insert_record("lesson_answers", $answer);
}
} else {
for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
for ($i = 0; $i < count($properties->answer_editor); $i++) {
if (!array_key_exists($i, $this->answers)) {
$this->answers[$i] = new stdClass;
$this->answers[$i]->lessonid = $this->lesson->id;
@@ -4575,7 +4578,7 @@ public function create_answers($properties) {

$answers = array();

for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
for ($i = 0; $i < ($this->lesson->maxanswers + 1); $i++) {
$answer = clone($newanswer);

if (isset($properties->answer_editor[$i])) {
@@ -4610,8 +4613,6 @@ public function create_answers($properties) {
$properties->answer_editor[$i]);
}
$answers[$answer->id] = new lesson_page_answer($answer);
} else {
break;
}
}

@@ -4901,6 +4902,17 @@ public function get_files($includedirs = true, $updatedsince = 0) {
return $fs->get_area_files($this->lesson->context->id, 'mod_lesson', 'page_contents', $this->properties->id,
'itemid, filepath, filename', $includedirs, $updatedsince);
}

/**
* Make updates to the form data if required.
*
* @since Moodle 3.7
* @param stdClass $data The form data to update.
* @return stdClass The updated fom data.
*/
public function update_form_data(stdClass $data) : stdClass {
return $data;
}
}


@@ -71,6 +71,43 @@ public function display($renderer, $attempt) {
$event->trigger();
return $mform->display();
}

/**
* Creates answers for this page type.
*
* @param object $properties The answer properties.
*/
public function create_answers($properties) {
if (isset($properties->enableotheranswers) && $properties->enableotheranswers) {
$properties->response_editor = array_values($properties->response_editor);
$properties->jumpto = array_values($properties->jumpto);
$properties->score = array_values($properties->score);
$wrongresponse = end($properties->response_editor);
$wrongkey = key($properties->response_editor);
$properties->answer_editor[$wrongkey] = LESSON_OTHER_ANSWERS;
}
parent::create_answers($properties);
}

/**
* Update the answers for this page type.
*
* @param object $properties The answer properties.
* @param context $context The context for this module.
* @param int $maxbytes The maximum bytes for any uploades.
*/
public function update($properties, $context = null, $maxbytes = null) {
if ($properties->enableotheranswers) {
$properties->response_editor = array_values($properties->response_editor);
$properties->jumpto = array_values($properties->jumpto);
$properties->score = array_values($properties->score);
$wrongresponse = end($properties->response_editor);
$wrongkey = key($properties->response_editor);
$properties->answer_editor[$wrongkey] = LESSON_OTHER_ANSWERS;
}
parent::update($properties, $context, $maxbytes);
}

public function check_answer() {
global $CFG;
$result = parent::check_answer();
@@ -125,6 +162,22 @@ public function check_answer() {
return $result;
}
}
// We could check here to see if we have a wrong answer jump to use.
if ($result->answerid == 0) {
// Use the all other answers jump details if it is set up.
$lastanswer = end($answers);
// Double check that this is the @#wronganswer#@ answer.
if (strpos($lastanswer->answer, LESSON_OTHER_ANSWERS) !== false) {
$otheranswers = end($answers);
$result->newpageid = $otheranswers->jumpto;
$result->response = format_text($otheranswers->response, $otheranswers->responseformat, $formattextdefoptions);
// Does this also need to do the jumpto_is_correct?
if ($this->lesson->custom) {
$result->correctanswer = ($otheranswers->score > 0);
}
$result->answerid = $otheranswers->id;
}
}
return $result;
}

@@ -259,6 +312,35 @@ public function report_answers($answerpage, $answerdata, $useranswer, $pagestats
}
return $answerpage;
}

/**
* Make updates to the form data if required. In this case to put the all other answer data into the write section of the form.
*
* @param stdClass $data The form data to update.
* @return stdClass The updated fom data.
*/
public function update_form_data(stdClass $data) : stdClass {
$answercount = count($this->get_answers());
// Check for other answer entry.
$lastanswer = $data->{'answer_editor[' . ($answercount - 1) . ']'};
if (strpos($lastanswer, LESSON_OTHER_ANSWERS) !== false) {
$data->{'answer_editor[' . ($this->lesson->maxanswers + 1) . ']'} =
$data->{'answer_editor[' . ($answercount - 1) . ']'};
$data->{'response_editor[' . ($this->lesson->maxanswers + 1) . ']'} =
$data->{'response_editor[' . ($answercount - 1) . ']'};
$data->{'jumpto[' . ($this->lesson->maxanswers + 1) . ']'} = $data->{'jumpto[' . ($answercount - 1) . ']'};
$data->{'score[' . ($this->lesson->maxanswers + 1) . ']'} = $data->{'score[' . ($answercount - 1) . ']'};
$data->enableotheranswers = true;

// Unset the old values.
unset($data->{'answer_editor[' . ($answercount - 1) . ']'});
unset($data->{'response_editor[' . ($answercount - 1) . ']'});
unset($data->{'jumpto['. ($answercount - 1) . ']'});
unset($data->{'score[' . ($answercount - 1) . ']'});
}

return $data;
}
}

class lesson_add_page_form_numerical extends lesson_add_page_form_base {
@@ -269,13 +351,21 @@ class lesson_add_page_form_numerical extends lesson_add_page_form_base {
protected $responseformat = LESSON_ANSWER_HTML;

public function custom_definition() {
for ($i = 0; $i < $this->_customdata['lesson']->maxanswers; $i++) {
$answercount = $this->_customdata['lesson']->maxanswers;
for ($i = 0; $i < $answercount; $i++) {
$this->_form->addElement('header', 'answertitle'.$i, get_string('answer').' '.($i+1));
$this->add_answer($i, null, ($i < 1));
$this->add_response($i);
$this->add_jumpto($i, null, ($i == 0 ? LESSON_NEXTPAGE : LESSON_THISPAGE));
$this->add_score($i, null, ($i===0)?1:0);
}
// Wrong answer jump.
$this->_form->addElement('header', 'wronganswer', get_string('allotheranswers', 'lesson'));
$newcount = $answercount + 1;
$this->_form->addElement('advcheckbox', 'enableotheranswers', get_string('enabled', 'lesson'));
$this->add_response($newcount);
$this->add_jumpto($newcount, get_string('allotheranswersjump', 'lesson'), LESSON_NEXTPAGE);
$this->add_score($newcount, get_string('allotheranswersscore', 'lesson'), 0);
}
}

@@ -71,6 +71,44 @@ public function display($renderer, $attempt) {
$event->trigger();
return $mform->display();
}

/**
* Creates answers for this page type.
*
* @param object $properties The answer properties.
*/
public function create_answers($properties) {
if (isset($properties->enableotheranswers) && $properties->enableotheranswers) {
$properties->response_editor = array_values($properties->response_editor);
$properties->jumpto = array_values($properties->jumpto);
$properties->score = array_values($properties->score);
$wrongresponse = end($properties->response_editor);
$wrongkey = key($properties->response_editor);
$properties->answer_editor[$wrongkey] = LESSON_OTHER_ANSWERS;
}
parent::create_answers($properties);
}

/**
* Update the answers for this page type.
*
* @param object $properties The answer properties.
* @param context $context The context for this module.
* @param int $maxbytes The maximum bytes for any uploades.
*/
public function update($properties, $context = null, $maxbytes = null) {
if ($properties->enableotheranswers) {
$properties->response_editor = array_values($properties->response_editor);
$properties->jumpto = array_values($properties->jumpto);
$properties->score = array_values($properties->score);
$wrongresponse = end($properties->response_editor);
$wrongkey = key($properties->response_editor);
$properties->answer_editor[$wrongkey] = LESSON_OTHER_ANSWERS;
}
parent::update($properties, $context, $maxbytes);
}


public function check_answer() {
global $CFG;
$result = parent::check_answer();
@@ -176,6 +214,26 @@ public function check_answer() {
break; // quit answer analysis immediately after a match has been found
}
}

// We could check here to see if we have a wrong answer jump to use.
if ($result->answerid == 0) {
// Use the all other answers jump details if it is set up.
$lastanswer = end($answers);
// Double check that this is the @#wronganswer#@ answer.
if (strpos($lastanswer->answer, LESSON_OTHER_ANSWERS) !== false) {
$otheranswers = end($answers);
$result->newpageid = $otheranswers->jumpto;
$options = new stdClass();
$options->para = false;
$result->response = format_text($otheranswers->response, $otheranswers->responseformat, $options);
// Does this also need to do the jumpto_is_correct?
if ($this->lesson->custom) {
$result->correctanswer = ($otheranswers->score > 0);
}
$result->answerid = $otheranswers->id;
}
}

$result->userresponse = $studentanswer;
//clean student answer as it goes to output.
$result->studentanswer = s($studentanswer);
@@ -325,6 +383,33 @@ public function report_answers($answerpage, $answerdata, $useranswer, $pagestats
}
return $answerpage;
}

/**
* Make updates to the form data if required. In this case to put the all other answer data into the write section of the form.
*
* @param stdClass $data The form data to update.
* @return stdClass The updated fom data.
*/
public function update_form_data(stdClass $data) : stdClass {
$answercount = count($this->get_answers());
// Check for other answer entry.
$lastanswer = $data->{'answer_editor[' . ($answercount - 1) . ']'};
if (strpos($lastanswer, LESSON_OTHER_ANSWERS) !== false) {
$data->{'answer_editor[' . ($this->lesson->maxanswers + 1) . ']'} =
$data->{'answer_editor[' . ($answercount - 1) . ']'};
$data->{'response_editor[' . ($this->lesson->maxanswers + 1) . ']'} =
$data->{'response_editor[' . ($answercount - 1) . ']'};
$data->{'jumpto[' . ($this->lesson->maxanswers + 1) . ']'} = $data->{'jumpto[' . ($answercount - 1) . ']'};
$data->{'score[' . ($this->lesson->maxanswers + 1) . ']'} = $data->{'score[' . ($answercount - 1) . ']'};
$data->enableotheranswers = true;
// Unset the old values.
unset($data->{'answer_editor[' . ($answercount - 1) . ']'});
unset($data->{'response_editor[' . ($answercount - 1) . ']'});
unset($data->{'jumpto[' . ($answercount - 1) . ']'});
unset($data->{'score[' . ($answercount - 1) . ']'});
}
return $data;
}
}


@@ -340,14 +425,23 @@ public function custom_definition() {
$this->_form->setDefault('qoption', 0);
$this->_form->addHelpButton('qoption', 'casesensitive', 'lesson');

for ($i = 0; $i < $this->_customdata['lesson']->maxanswers; $i++) {
$answercount = $this->_customdata['lesson']->maxanswers;
for ($i = 0; $i < $answercount; $i++) {
$this->_form->addElement('header', 'answertitle'.$i, get_string('answer').' '.($i+1));
// Only first answer is required.
$this->add_answer($i, null, ($i < 1));
$this->add_response($i);
$this->add_jumpto($i, null, ($i == 0 ? LESSON_NEXTPAGE : LESSON_THISPAGE));
$this->add_score($i, null, ($i===0)?1:0);
}

// Other answer jump.
$this->_form->addElement('header', 'wronganswer', get_string('allotheranswers', 'lesson'));
$newcount = $answercount + 1;
$this->_form->addElement('advcheckbox', 'enableotheranswers', get_string('enabled', 'lesson'));
$this->add_response($newcount);
$this->add_jumpto($newcount, get_string('allotheranswersjump', 'lesson'), LESSON_NEXTPAGE);
$this->add_score($newcount, get_string('allotheranswersscore', 'lesson'), 0);
}
}

0 comments on commit f7c77ea

Please sign in to comment.