Skip to content

Commit

Permalink
MDL-32690: Restore 1.9 backup into 2.X fails on missing assignment type
Browse files Browse the repository at this point in the history
Solution is as follows:
* Allow unsupported subplugins to convert from Moodle1 to Moodle2
* On Moodle2 restore hide unsupported subplugins
* Graceful error message in assignment of unsupported subplugins
  • Loading branch information
polothy committed Aug 29, 2013
1 parent d2aa53b commit 79547e5
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
9 changes: 8 additions & 1 deletion mod/assignment/backup/moodle1/lib.php
Expand Up @@ -194,7 +194,8 @@ protected function get_subplugin_handler($subplugin) {
}

if (!isset($this->subpluginhandlers[$subplugin])) {
throw new moodle1_convert_exception('unsupported_subplugin', 'assignment_'.$subplugin);
// Generic handling, prevents breaking conversion process...
$this->subpluginhandlers[$subplugin] = new moodle1_assignment_unsupported_subplugin_handler($this, $subplugin);
}

return $this->subpluginhandlers[$subplugin];
Expand Down Expand Up @@ -236,4 +237,10 @@ public function append_subplugin_data($data) {

//you will probably want to do stuff with $this->xmlwriter here (within your overridden method) to write plugin specific data.
}
}

/**
* This class handles subplugins that do not exist or that are not supported
*/
class moodle1_assignment_unsupported_subplugin_handler extends moodle1_assignment_subplugin_handler {
}
20 changes: 20 additions & 0 deletions mod/assignment/backup/moodle2/restore_assignment_stepslib.php
Expand Up @@ -72,6 +72,11 @@ protected function process_assignment($data) {
$newitemid = $DB->insert_record('assignment', $data);
// immediately after inserting "activity" record, call this
$this->apply_activity_instance($newitemid);

// Hide unsupported sub-plugins
if (!$this->is_valid_assignment_subplugin($data->assignmenttype)) {
$DB->set_field('course_modules', 'visible', 0, array('id' => $this->get_task()->get_moduleid()));
}
}

protected function process_assignment_submission($data) {
Expand Down Expand Up @@ -100,4 +105,19 @@ protected function after_execute() {
$this->add_related_files('mod_assignment', 'submission', 'assignment_submission');
$this->add_related_files('mod_assignment', 'response', 'assignment_submission');
}

/**
* Determine if a sub-plugin is supported or not
*
* @param string $type
* @return bool
*/
protected function is_valid_assignment_subplugin($type) {
static $subplugins = null;

if (is_null($subplugins)) {
$subplugins = get_plugin_list('assignment');
}
return array_key_exists($type, $subplugins);
}
}
1 change: 1 addition & 0 deletions mod/assignment/lang/en/assignment.php
Expand Up @@ -223,3 +223,4 @@
$string['viewmysubmission'] = 'View my submission';
$string['viewsubmissions'] = 'View {$a} submitted assignments';
$string['yoursubmission'] = 'Your submission';
$string['unsupportedsubplugin'] = 'The assignment type of \'{$a}\' is not currently supported. You may wait until the assignment type is made available, or delete the assignment.';
7 changes: 6 additions & 1 deletion mod/assignment/lib.php
Expand Up @@ -3948,7 +3948,12 @@ function assignment_extend_settings_navigation(settings_navigation $settings, na
global $PAGE, $DB, $USER, $CFG;

$assignmentrow = $DB->get_record("assignment", array("id" => $PAGE->cm->instance));
require_once "$CFG->dirroot/mod/assignment/type/$assignmentrow->assignmenttype/assignment.class.php";

$classfile = "$CFG->dirroot/mod/assignment/type/$assignmentrow->assignmenttype/assignment.class.php";
if (!file_exists($classfile)) {
return;
}
require_once($classfile);

$assignmentclass = 'assignment_'.$assignmentrow->assignmenttype;
$assignmentinstance = new $assignmentclass($PAGE->cm->id, $assignmentrow, $PAGE->cm, $PAGE->course);
Expand Down
8 changes: 6 additions & 2 deletions mod/assignment/mod_form.php
Expand Up @@ -9,7 +9,7 @@ class mod_assignment_mod_form extends moodleform_mod {
protected $_assignmentinstance = null;

function definition() {
global $CFG, $DB, $PAGE;
global $CFG, $DB, $PAGE, $COURSE;
$mform =& $this->_form;

// this hack is needed for different settings of each subtype
Expand All @@ -29,7 +29,11 @@ function definition() {
$mform->setType('type', PARAM_ALPHA);
$mform->setDefault('type', $type);

require_once($CFG->dirroot.'/mod/assignment/type/'.$type.'/assignment.class.php');
$classfile = $CFG->dirroot.'/mod/assignment/type/'.$type.'/assignment.class.php';
if (!file_exists($classfile)) {
throw new moodle_exception('unsupportedsubplugin', 'assignment', new moodle_url('/course/view.php', array('id' => $COURSE->id)), $type);
}
require_once($classfile);
$assignmentclass = 'assignment_'.$type;
$assignmentinstance = new $assignmentclass();

Expand Down
6 changes: 5 additions & 1 deletion mod/assignment/view.php
Expand Up @@ -40,7 +40,11 @@

$PAGE->requires->js('/mod/assignment/assignment.js');

require ("$CFG->dirroot/mod/assignment/type/$assignment->assignmenttype/assignment.class.php");
$classfile = "$CFG->dirroot/mod/assignment/type/$assignment->assignmenttype/assignment.class.php";
if (!file_exists($classfile)) {
throw new moodle_exception('unsupportedsubplugin', 'assignment', new moodle_url('/course/view.php', array('id' => $course->id)), $assignment->assignmenttype);
}
require ($classfile);
$assignmentclass = "assignment_$assignment->assignmenttype";
$assignmentinstance = new $assignmentclass($cm->id, $assignment, $cm, $course);

Expand Down

0 comments on commit 79547e5

Please sign in to comment.