Commit ab6e7971 authored by Petr Skoda's avatar Petr Skoda
Browse files

Merge branch 'MDL-25632' of git://github.com/samhemelryk/moodle

parents b72ca9f8 86d99db3
......@@ -67,7 +67,16 @@ if (!$canmanage) {
// record answer (if necessary) and show response (if none say if answer is correct or not)
$page = $lesson->load_page(required_param('pageid', PARAM_INT));
$result = $page->record_attempt($context);
// Check the page has answers [MDL-25632]
if (count($page->answers) > 0) {
$result = $page->record_attempt($context);
} else {
// The page has no answers so we will just progress to the next page in the
// sequence (as set by newpageid).
$result = new stdClass;
$result->newpageid = optional_param('newpageid', $page->nextpageid, PARAM_INT);
$result->nodefaultresponse = true;
}
if (isset($USER->modattempts[$lesson->id])) {
// make sure if the student is reviewing, that he/she sees the same pages/page path that he/she saw the first time
......
......@@ -44,6 +44,14 @@
defined('MOODLE_INTERNAL') || die();
/**
*
* @global stdClass $CFG
* @global moodle_database $DB
* @global core_renderer $OUTPUT
* @param int $oldversion
* @return bool
*/
function xmldb_lesson_upgrade($oldversion) {
global $CFG, $DB, $OUTPUT;
......@@ -236,7 +244,50 @@ function xmldb_lesson_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2010081200, 'lesson');
}
if ($oldversion < 2010121400) {
// Fix matching question pages.
// In Moodle 1.9 matching question pages stored the correct and incorrect
// jumps on the third and forth answers, in Moodle 2.0 they are stored
// in the first and second answers.
// This upgrade block detects matching questions where this is the case
// and fixed it by making firstjump = thirdjump && secondjump = forthjump.
$pages = $DB->get_recordset('lesson_pages', array('qtype'=>'5'));
foreach ($pages as $page) {
$answers = $DB->get_records('lesson_answers', array('pageid'=>$page->id), 'id', 'id, jumpto', 0, 4);
if (count($answers) < 4) {
// If there are less then four answers the problem wont exist.
// All Moodle 1.9 matching questions had a least 4 answers.
continue;
}
$first = array_shift($answers);
$second = array_shift($answers);
$third = array_shift($answers);
$forth = array_shift($answers);
if ($first->jumpto !== '0' || $second->jumpto !== '0') {
// If either are set to something other than the next page then
// there is no problem.
continue;
}
if ($third->jumpto !== '0') {
$first->jumpto = $third->jumpto;
$DB->update_record('lesson_answers', $first);
$third->jumpto = '0';
$DB->update_record('lesson_answers', $third);
}
if ($forth->jumpto !== '0') {
$second->jumpto = $forth->jumpto;
$DB->update_record('lesson_answers', $second);
$forth->jumpto = '0';
$DB->update_record('lesson_answers', $forth);
}
}
// Close the record set
$pages->close();
upgrade_mod_savepoint(true, 2010121400, 'lesson');
}
return true;
}
......
......@@ -1881,7 +1881,9 @@ abstract class lesson_page extends lesson_base {
$this->answers = array();
$answers = $DB->get_records('lesson_answers', array('pageid'=>$this->properties->id, 'lessonid'=>$this->lesson->id), 'id');
if (!$answers) {
debugging(get_string('cannotfindanswer', 'lesson'));
// It is possible that a lesson upgraded from Moodle 1.9 still
// contains questions without any answers [MDL-25632].
// debugging(get_string('cannotfindanswer', 'lesson'));
return array();
}
foreach ($answers as $answer) {
......@@ -1913,6 +1915,7 @@ abstract class lesson_page extends lesson_base {
* Records an attempt at this page
*
* @final
* @global moodle_database $DB
* @param stdClass $context
* @return stdClass Returns the result of the attempt
*/
......@@ -1958,7 +1961,7 @@ abstract class lesson_page extends lesson_base {
if (!$result->correctanswer && ($result->newpageid == 0)) {
// wrong answer and student is stuck on this page - check how many attempts
// the student has had at this page/question
$nattempts = $DB->count_records("lesson_attempts", array("pageid"=>$this->properties->id, "userid"=>$USER->id),"retry", $nretakes);
$nattempts = $DB->count_records("lesson_attempts", array("pageid"=>$this->properties->id, "userid"=>$USER->id, "retry" => $nretakes));
// retreive the number of attempts left counter for displaying at bottom of feedback page
if ($nattempts >= $this->lesson->maxattempts) {
if ($this->lesson->maxattempts > 1) { // don't bother with message if only one attempt
......@@ -2133,13 +2136,17 @@ abstract class lesson_page extends lesson_base {
$this->answers[$i]->pageid = $this->id;
$this->answers[$i]->timecreated = $this->timecreated;
}
if (!empty($properties->answer_editor[$i])) {
if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
$this->answers[$i]->answer = $properties->answer_editor[$i]['text'];
$this->answers[$i]->answerformat = $properties->answer_editor[$i]['format'];
if (isset($properties->response_editor[$i])) {
$this->answers[$i]->response = $properties->response_editor[$i]['text'];
$this->answers[$i]->responseformat = $properties->response_editor[$i]['format'];
}
}
if (!empty($properties->response_editor[$i]) && is_array($properties->response_editor[$i])) {
$this->answers[$i]->response = $properties->response_editor[$i]['text'];
$this->answers[$i]->responseformat = $properties->response_editor[$i]['format'];
}
if (!empty($this->answers[$i]->answer)) {
if (isset($properties->jumpto[$i])) {
$this->answers[$i]->jumpto = $properties->jumpto[$i];
}
......@@ -2152,8 +2159,9 @@ abstract class lesson_page extends lesson_base {
$DB->update_record("lesson_answers", $this->answers[$i]->properties());
}
} else {
break;
} else if (isset($this->answers[$i]->id)) {
$DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id));
unset($this->answers[$i]);
}
}
return true;
......@@ -2221,13 +2229,17 @@ abstract class lesson_page extends lesson_base {
for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
$answer = clone($newanswer);
if (!empty($properties->answer_editor[$i])) {
if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
$answer->answer = $properties->answer_editor[$i]['text'];
$answer->answerformat = $properties->answer_editor[$i]['format'];
if (isset($properties->response_editor[$i])) {
$answer->response = $properties->response_editor[$i]['text'];
$answer->responseformat = $properties->response_editor[$i]['format'];
}
}
if (!empty($properties->response_editor[$i]) && is_array($properties->response_editor[$i])) {
$answer->response = $properties->response_editor[$i]['text'];
$answer->responseformat = $properties->response_editor[$i]['format'];
}
if (!empty($answer->answer)) {
if (isset($properties->jumpto[$i])) {
$answer->jumpto = $properties->jumpto[$i];
}
......@@ -2470,6 +2482,8 @@ abstract class lesson_page extends lesson_base {
foreach ($answers as $answer) {
$jumps[] = $this->get_jump_name($answer->jumpto);
}
} else {
$jumps[] = $this->get_jump_name($this->properties->nextpageid);
}
return $jumps;
}
......
......@@ -64,6 +64,10 @@ class lesson_page_type_branchtable extends lesson_page {
}
$jumps[] = $this->get_jump_name($answer->jumpto);
}
} else {
// We get here is the lesson was created on a Moodle 1.9 site and
// the lesson contains question pages without any answers.
$jumps[] = $this->get_jump_name($this->properties->nextpageid);
}
return $jumps;
}
......
......@@ -105,13 +105,16 @@ class lesson_page_type_matching extends lesson_page {
$this->lesson->maxanswers = $this->lesson->maxanswers + 2;
for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
$answer = clone($newanswer);
if (!empty($properties->answer_editor[$i])) {
if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
$answer->answer = $properties->answer_editor[$i]['text'];
$answer->answerformat = $properties->answer_editor[$i]['format'];
if (isset($properties->response_editor[$i])) {
$answer->response = $properties->response_editor[$i]['text'];
$answer->responseformat = $properties->response_editor[$i]['format'];
}
}
if (!empty($properties->response_editor[$i]) && is_array($properties->response_editor[$i])) {
$answer->response = $properties->response_editor[$i]['text'];
$answer->responseformat = $properties->response_editor[$i]['format'];
}
if (!empty($answer->answer)) {
if (isset($properties->jumpto[$i])) {
$answer->jumpto = $properties->jumpto[$i];
}
......@@ -276,6 +279,14 @@ class lesson_page_type_matching extends lesson_page {
}
return $table;
}
/**
* Updates the page and its answers
*
* @global moodle_database $DB
* @global moodle_page $PAGE
* @param stdClass $properties
* @return bool
*/
public function update($properties) {
global $DB, $PAGE;
$answers = $this->get_answers();
......@@ -293,13 +304,17 @@ class lesson_page_type_matching extends lesson_page {
$this->answers[$i]->pageid = $this->id;
$this->answers[$i]->timecreated = $this->timecreated;
}
if (!empty($properties->answer_editor[$i])) {
if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
$this->answers[$i]->answer = $properties->answer_editor[$i]['text'];
$this->answers[$i]->answerformat = $properties->answer_editor[$i]['format'];
if (isset($properties->response_editor[$i])) {
$this->answers[$i]->response = $properties->response_editor[$i]['text'];
$this->answers[$i]->responseformat = $properties->response_editor[$i]['format'];
}
}
if (!empty($properties->response_editor[$i]) && is_array($properties->response_editor[$i])) {
$this->answers[$i]->response = $properties->response_editor[$i]['text'];
$this->answers[$i]->responseformat = $properties->response_editor[$i]['format'];
}
if (!empty($this->answers[$i]->answer)) {
if (isset($properties->jumpto[$i])) {
$this->answers[$i]->jumpto = $properties->jumpto[$i];
}
......@@ -319,8 +334,9 @@ class lesson_page_type_matching extends lesson_page {
$DB->update_record("lesson_answers", $this->answers[$i]->properties());
}
} else {
break;
} else if (isset($this->answers[$i]->id)) {
$DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id));
unset($this->answers[$i]);
}
}
return true;
......@@ -383,7 +399,11 @@ class lesson_page_type_matching extends lesson_page {
$data = "<select disabled=\"disabled\"><option selected=\"selected\">".strip_tags(format_string($answer->answer))."</option></select>";
if ($useranswer != NULL) {
$userresponse = explode(",", $useranswer->useranswer);
$data .= "<select disabled=\"disabled\"><option selected=\"selected\">".strip_tags(format_string($answers[$userresponse[$i]]->response))."</option></select>";
$data .= "<select disabled=\"disabled\"><option selected=\"selected\">";
if (array_key_exists($i, $userresponse)) {
$data .= strip_tags(format_string($answers[$userresponse[$i]]->response));
}
$data .= "</option></select>";
} else {
$data .= "<select disabled=\"disabled\"><option selected=\"selected\">".strip_tags(format_string($answer->response))."</option></select>";
}
......@@ -413,13 +433,14 @@ class lesson_page_type_matching extends lesson_page {
}
public function get_jumps() {
global $DB;
// The jumps for matching question type is stored
// in the 3rd and 4rth answer record.
// The jumps for matching question type are stored in the 1st and 2nd answer record.
$jumps = array();
if ($answers = $DB->get_records("lesson_answers", array("lessonid" => $this->lesson->id, "pageid" => $this->properties->id), 'id', '*', 0, 2)) {
foreach ($answers as $answer) {
$jumps[] = $this->get_jump_name($answer->jumpto);
}
} else {
$jumps[] = $this->get_jump_name($this->properties->nextpageid);
}
return $jumps;
}
......
......@@ -57,7 +57,6 @@ class lesson_page_type_multichoice extends lesson_page {
public function get_jumps() {
global $DB;
$jumps = array();
$params = array ("lessonid" => $this->lesson->id, "pageid" => $this->properties->id);
if ($answers = $this->get_answers()) {
foreach ($answers as $answer) {
if ($answer->answer === '') {
......@@ -66,6 +65,10 @@ class lesson_page_type_multichoice extends lesson_page {
}
$jumps[] = $this->get_jump_name($answer->jumpto);
}
} else {
// We get here is the lesson was created on a Moodle 1.9 site and
// the lesson contains question pages without any answers.
$jumps[] = $this->get_jump_name($this->properties->nextpageid);
}
return $jumps;
}
......
......@@ -437,7 +437,12 @@ if ($action === 'delete') {
$page = $lessonpages[$pageid];
$answerpage = new stdClass;
$data ='';
$answerdata = new stdClass;
// Set some defaults for the answer data.
$answerdata->score = NULL;
$answerdata->response = NULL;
$answerdata->responseformat = FORMAT_PLAIN;
$answerpage->title = format_string($page->title);
......@@ -452,9 +457,7 @@ if ($action === 'delete') {
if (empty($userid)) {
// there is no userid, so set these vars and display stats.
$answerpage->grayout = 0;
$useranswer = NULL;
$answerdata->score = NULL;
$answerdata->response = NULL;
$useranswer = NULL;
} elseif ($useranswers = $DB->get_records("lesson_attempts",array("lessonid"=>$lesson->id, "userid"=>$userid, "retry"=>$try,"pageid"=>$page->id), "timeseen")) {
// get the user's answer for this page
// need to find the right one
......@@ -470,9 +473,6 @@ if ($action === 'delete') {
// user did not answer this page, gray it out and set some nulls
$answerpage->grayout = 1;
$useranswer = NULL;
$answerdata->score = NULL;
$answerdata->response = NULL;
}
$i = 0;
$n = 0;
......
......@@ -27,7 +27,7 @@
defined('MOODLE_INTERNAL') || die();
$module->version = 2010081200; // The current module version (Date: YYYYMMDDXX)
$module->version = 2010121400; // The current module version (Date: YYYYMMDDXX)
$module->requires = 2010080300; // Requires this Moodle version
$module->cron = 0; // Period for cron to check this module (secs)
......
......@@ -359,11 +359,17 @@ if ($pageid != LESSON_EOL) {
} else {
$data = new stdClass;
$data->id = $PAGE->cm->id;
$data->pageid = $page->id;
$data->newpageid = LESSON_NEXTPAGE;
if ($nextpage = $lesson->get_next_page($page->nextpageid)) {
$data->newpageid = $nextpage->id;
}
$mform = new lesson_page_without_answers();
$customdata = array(
'title' => $page->title,
'contents' => $page->get_contents()
);
$mform = new lesson_page_without_answers($CFG->wwwroot.'/mod/lesson/continue.php', $customdata);
$mform->set_data($data);
ob_start();
$mform->display();
......
......@@ -41,15 +41,30 @@ require_once($CFG->libdir.'/formslib.php');
class lesson_page_without_answers extends moodleform {
public function definition() {
global $OUTPUT;
$mform = $this->_form;
$title = $this->_customdata['title'];
$contents = $this->_customdata['contents'];
if (!empty($title)) {
$mform->addElement('header', 'pageheader', $title);
}
if (!empty($contents)) {
$mform->addElement('html', $OUTPUT->box($contents, 'contents'));
}
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'pageid');
$mform->setType('pageid', PARAM_INT);
$mform->addElement('hidden', 'newpageid');
$mform->setType('newpageid', PARAM_INT);
$this->add_action_buttons(null, get_string("continue", "lesson"));
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment