Skip to content

Commit

Permalink
MDL-35851 Lesson module: fix page order id for import questions. Add …
Browse files Browse the repository at this point in the history
…upgrade script to fix lesson pages corrupted links
  • Loading branch information
Rossiani Wijaya authored and Damyon Wiese committed Jul 16, 2013
1 parent e780e5c commit eebc821
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
57 changes: 54 additions & 3 deletions mod/lesson/db/upgrade.php
Expand Up @@ -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;
}


16 changes: 14 additions & 2 deletions mod/lesson/format.php
Expand Up @@ -323,6 +323,13 @@ function importprocess($filename, $lesson, $pageid) {
$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;

Expand Down Expand Up @@ -378,7 +385,6 @@ function importprocess($filename, $lesson, $pageid) {
$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)
Expand All @@ -397,6 +403,7 @@ function importprocess($filename, $lesson, $pageid) {
$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;
Expand Down Expand Up @@ -424,7 +431,12 @@ function importprocess($filename, $lesson, $pageid) {
$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));
Expand Down
2 changes: 1 addition & 1 deletion mod/lesson/version.php
Expand Up @@ -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;

0 comments on commit eebc821

Please sign in to comment.