MDL-35851 Lesson module: fix page order id for import questions. Add upgrade script to fix lesson pages corrupted links

This commit is contained in:
Rossiani Wijaya 2013-06-20 13:10:02 +08:00 committed by Damyon Wiese
parent e780e5cb8f
commit eebc821c83
3 changed files with 68 additions and 5 deletions

View file

@ -72,8 +72,59 @@ function xmldb_lesson_upgrade($oldversion) {
// Moodle v2.5.0 release upgrade line.
// Put any upgrade step following this.
if ($oldversion < 2013050101) {
// Fixed page order for missing page in lesson
upgrade_set_timeout(600); // increase excution time for large sites
$lessons = $DB->get_records('lesson');
foreach ($lessons as $lesson) {
$pages = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id));
$iscorrupt = false;
// Validate lesson prev and next pages.
foreach ($pages as $id => $page) {
// Setting up prev and next id to 0 is only valid if lesson only has 1 page.
// Other than that, it indicates lesson page links are corrupted.
if ($page->prevpageid == 0 && $page->nextpageid == 0 && count($pages) != 1) {
$iscorrupt = true;
break;
// Make sure page links to an existing page within the lesson.
} else if (($page->prevpageid != 0 && !isset($pages[$page->prevpageid])) ||
($page->nextpageid != 0 && !isset($pages[$page->nextpageid]))) {
$iscorrupt = true;
break;
// Check the pages linked correctly
} else if((($page->prevpageid == 0 || $page->nextpageid != 0 ) && $pages[$page->nextpageid]->prevpageid != $page->id) ||
(($page->prevpageid != 0 || $page->nextpageid == 0) && $pages[$page->prevpageid]->nextpageid != $page->id)) {
$iscorrupt = true;
break;
}
}
// Process the update for the corrupted lesson pages.
$count = 0;
$lastpageid = 0;
if ($iscorrupt) {
foreach($pages as $page) {
$count++;
if ($lastpageid == 0) { // First page
$DB->set_field('lesson_pages', 'prevpageid', 0, array('id' => $page->id));
$DB->set_field('lesson_pages', 'nextpageid', 0, array('id' => $page->id));
} elseif (count($pages) == $count) {
$DB->set_field('lesson_pages', 'prevpageid', $lastpageid, array('id' => $page->id));
$DB->set_field('lesson_pages', 'nextpageid', 0, array('id' => $page->id));
$DB->set_field('lesson_pages', 'nextpageid', $page->id, array('id' => $lastpageid));
} else {
$DB->set_field('lesson_pages', 'prevpageid', $lastpageid, array('id' => $page->id));
$DB->set_field('lesson_pages', 'nextpageid', $page->id, array('id' => $lastpageid));
}
$lastpageid = $page->id;
}
}
}
upgrade_mod_savepoint(true, 2013050101, 'lesson');
}
return true;
}

View file

@ -323,6 +323,13 @@ class qformat_default {
$this->count_questions($questions)), 'notifysuccess');
$count = 0;
$addquestionontop = false;
if ($pageid == 0) {
$addquestionontop = true;
$updatelessonpage = $DB->get_record('lesson_pages', array('lessonid' => $lesson->id, 'prevpageid' => 0));
} else {
$updatelessonpage = $DB->get_record('lesson_pages', array('id' => $pageid));
}
$unsupportedquestions = 0;
@ -378,7 +385,6 @@ class qformat_default {
$newpageid = $DB->insert_record("lesson_pages", $newpage);
// update the linked list
$DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid));
} else {
// new page is the first page
// get the existing (first) page (if any)
@ -397,6 +403,7 @@ class qformat_default {
$DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->id));
}
}
// reset $pageid and put the page ID in $question, used in save_question_option()
$pageid = $newpageid;
$question->id = $newpageid;
@ -424,7 +431,12 @@ class qformat_default {
$unsupportedquestions++;
break;
}
}
// update the prev links
if ($addquestionontop) {
$DB->set_field("lesson_pages", "prevpageid", $pageid, array("id" => $updatelessonpage->id));
} else {
$DB->set_field("lesson_pages", "prevpageid", $pageid, array("id" => $updatelessonpage->nextpageid));
}
if ($unsupportedquestions) {
echo $OUTPUT->notification(get_string('unknownqtypesnotimported', 'lesson', $unsupportedquestions));

View file

@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();
$module->version = 2013050100; // The current module version (Date: YYYYMMDDXX)
$module->version = 2013050101; // The current module version (Date: YYYYMMDDXX)
$module->requires = 2013050100; // Requires this Moodle version
$module->component = 'mod_lesson'; // Full name of the plugin (used for diagnostics)
$module->cron = 0;