function restore_question_category($category, $restore) { $status = true; //Skip empty categories (some backups can contain them) if (!empty($category->id)) { //Get record from backup_ids $data = backup_getid($restore->backup_unique_code, "question_categories", $category->id); if ($data) { //Now get completed xmlized object $info = $data->info; //traverse_xmlize($info); //Debug //print_object ($GLOBALS['traverse_array']); //Debug //$GLOBALS['traverse_array']=""; //Debug //Now, build the question_categories record structure $question_cat = new stdClass(); $question_cat->name = backup_todb($info['QUESTION_CATEGORY']['#']['NAME']['0']['#']); $question_cat->info = backup_todb($info['QUESTION_CATEGORY']['#']['INFO']['0']['#']); $question_cat->stamp = backup_todb($info['QUESTION_CATEGORY']['#']['STAMP']['0']['#']); //parent is fixed after all categories are restored and we know all the new ids. $question_cat->parent = backup_todb($info['QUESTION_CATEGORY']['#']['PARENT']['0']['#']); $question_cat->sortorder = backup_todb($info['QUESTION_CATEGORY']['#']['SORTORDER']['0']['#']); if (!$question_cat->stamp) { $question_cat->stamp = make_unique_id_code(); } if (isset($info['QUESTION_CATEGORY']['#']['PUBLISH'])) { $course = $restore->course_id; $publish = backup_todb($info['QUESTION_CATEGORY']['#']['PUBLISH']['0']['#']); if ($publish) { $tocontext = get_context_instance(CONTEXT_SYSTEM); } else { $tocontext = get_context_instance(CONTEXT_COURSE, $course); } } else { if (!($tocontext = restore_question_get_best_category_context($restore, $info['QUESTION_CATEGORY']['#']['CONTEXT']['0']['#']))) { return $status; // context doesn't exist - a module has not been restored } } $question_cat->contextid = $tocontext->id; //does cat exist ?? if it does we check if the cat and questions already exist whether we have //add permission or not if we have no permission to add questions to SYSTEM or COURSECAT context //AND the question does not already exist then we create questions in COURSE context. if (!($fcat = get_record('question_categories', 'contextid', $question_cat->contextid, 'stamp', $question_cat->stamp))) { //no preexisting cat if (($tocontext->contextlevel == CONTEXT_SYSTEM || $tocontext->contextlevel == CONTEXT_COURSECAT) && !has_capability('moodle/question:add', $tocontext)) { //no preexisting cat and no permission to create questions here //must restore to course. $tocontext = get_context_instance(CONTEXT_COURSE, $restore->course_id); } $question_cat->contextid = $tocontext->id; if (!($fcat = get_record('question_categories', 'contextid', $question_cat->contextid, 'stamp', $question_cat->stamp))) { $question_cat->id = insert_record("question_categories", $question_cat); } else { $question_cat = $fcat; } //we'll be restoring all questions here. backup_putid($restore->backup_unique_code, "question_categories", $category->id, $question_cat->id); } else { $question_cat = $fcat; //we found an existing best category //but later if context is above course need to check if there are questions need creating in category //if we do need to create questions and permissions don't allow it create new category in course } //Do some output if (!defined('RESTORE_SILENTLY')) { echo "<li>" . get_string('category', 'quiz') . " \"" . $question_cat->name . "\"<br />"; } backup_flush(300); //start with questions if ($question_cat->id) { //We have the newid, update backup_ids //Now restore question $status = restore_questions($category->id, $question_cat, $info, $restore); } else { $status = false; } if (!defined('RESTORE_SILENTLY')) { echo '</li>'; } } else { echo 'Could not get backup info for question category' . $category->id; } } return $status; }
function restore_question_categories($category, $restore) { global $CFG; $status = true; //Hook to call Moodle < 1.5 Quiz Restore if ($restore->backup_version < 2005043000) { include_once $CFG->dirroot . '/mod/quiz/restorelibpre15.php'; return quiz_restore_pre15_question_categories($category, $restore); } //Get record from backup_ids $data = backup_getid($restore->backup_unique_code, "question_categories", $category->id); if ($data) { //Now get completed xmlized object $info = $data->info; //traverse_xmlize($info); //Debug //print_object ($GLOBALS['traverse_array']); //Debug //$GLOBALS['traverse_array']=""; //Debug //Now, build the question_categories record structure $question_cat = new stdClass(); $question_cat->course = $restore->course_id; $question_cat->name = backup_todb($info['QUESTION_CATEGORY']['#']['NAME']['0']['#']); $question_cat->info = backup_todb($info['QUESTION_CATEGORY']['#']['INFO']['0']['#']); $question_cat->publish = backup_todb($info['QUESTION_CATEGORY']['#']['PUBLISH']['0']['#']); $question_cat->stamp = backup_todb($info['QUESTION_CATEGORY']['#']['STAMP']['0']['#']); $question_cat->parent = backup_todb($info['QUESTION_CATEGORY']['#']['PARENT']['0']['#']); $question_cat->sortorder = backup_todb($info['QUESTION_CATEGORY']['#']['SORTORDER']['0']['#']); if ($catfound = restore_get_best_question_category($question_cat, $restore->course_id)) { $newid = $catfound; } else { if (!$question_cat->stamp) { $question_cat->stamp = make_unique_id_code(); } $newid = insert_record("question_categories", $question_cat); } //Do some output if ($newid) { if (!defined('RESTORE_SILENTLY')) { echo "<li>" . get_string('category', 'quiz') . " \"" . $question_cat->name . "\"<br />"; } } else { //We must never arrive here !! if (!defined('RESTORE_SILENTLY')) { echo "<li>" . get_string('category', 'quiz') . " \"" . $question_cat->name . "\" Error!<br />"; } $status = false; } backup_flush(300); //Here category has been created or selected, so save results in backup_ids and start with questions if ($newid and $status) { //We have the newid, update backup_ids backup_putid($restore->backup_unique_code, "question_categories", $category->id, $newid); //Now restore question $status = restore_questions($category->id, $newid, $info, $restore); } else { $status = false; } if (!defined('RESTORE_SILENTLY')) { echo '</li>'; } } else { echo 'Could not get backup info for question category' . $category->id; } return $status; }