Permalink
Browse files

MDL-32233 Added usersdata support in WS method core_course_duplicate_…

…course
  • Loading branch information...
jleyva committed May 10, 2012
1 parent 3dc1d76 commit 9aa84e9168c9c52a9f4b28683e6ae43563d71c7f
Showing with 71 additions and 31 deletions.
  1. +71 −31 course/externallib.php
View
@@ -663,11 +663,20 @@ public static function duplicate_course_parameters() {
'options' => new external_multiple_structure(
new external_single_structure(
array(
'name' => new external_value(PARAM_ALPHA, 'The backup option name:
"activities" (int) Include course activites (default to 1 that is equal to yes),
"blocks" (int) Include course blocks (default to 1 that is equal to yes),
"filters" (int) Include course filters (default to 1 that is equal to yes),'),
'value' => new external_value(PARAM_RAW, 'the value for the option 1 (yes) or 0 (no)')
'name' => new external_value(PARAM_ALPHA, 'The backup option name:
"activities" (int) Include course activites (default to 1 that is equal to yes),
"blocks" (int) Include course blocks (default to 1 that is equal to yes),
"filters" (int) Include course filters (default to 1 that is equal to yes),
"users" (int) Include users (default to 0 that is equal to no),
"role_assignments" (int) Include role assignments (default to 0 that is equal to no),
"user_files" (int) Include user files (default to 0 that is equal to no),
"comments" (int) Include user comments (default to 0 that is equal to no),
"completion_information" (int) Include user course completion information (default to 0 that is equal to no),
"logs" (int) Include course logs (default to 0 that is equal to no),
"histories" (int) Include histories (default to 0 that is equal to no)'
),
'value' => new external_value(PARAM_RAW, 'the value for the option 1 (yes) or 0 (no)'
)
)
), VALUE_DEFAULT, array()
),
@@ -719,6 +728,39 @@ public static function duplicate_course($courseid, $fullname, $shortname, $categ
$coursecontext = context_course::instance($course->id);
self::validate_context($coursecontext);
$backupdefaults = array(
'activities' => 1,
'blocks' => 1,
'filters' => 1,
'users' => 0,
'role_assignments' => 0,
'user_files' => 0,
'comments' => 0,
'completion_information' => 0,
'logs' => 0,
'histories' => 0
);
$backupsettings = array();
// Check for backup and restore options.
if (!empty($params['options'])) {
foreach ($params['options'] as $option) {
// Strict check for a correct value (allways 1 or 0, true or false).
$value = clean_param($option['value'], PARAM_INT);
if ($value !== 0 and $value !== 1) {
throw new moodle_exception('invalidextparam', 'webservice', '', $option['name']);
}
if (!isset($backupdefaults[$option['name']])) {
throw new moodle_exception('invalidextparam', 'webservice', '', $option['name']);
}
$backupsettings[$option['name']] = $value;
}
}
// Capability checking.
// The backup controller check for this currently, this may be redundant.
@@ -729,6 +771,11 @@ public static function duplicate_course($courseid, $fullname, $shortname, $categ
require_capability('moodle/backup:backupcourse', $coursecontext);
require_capability('moodle/backup:backuptargetimport', $coursecontext);
if (!empty($backupsettings['users'])) {
require_capability('moodle/backup:userinfo', $coursecontext);
require_capability('moodle/restore:userinfo', $categorycontext);
}
// Check if the shortname is used.
if ($foundcourses = $DB->get_records('course', array('shortname'=>$shortname))) {
foreach ($foundcourses as $foundcourse) {
@@ -739,33 +786,15 @@ public static function duplicate_course($courseid, $fullname, $shortname, $categ
throw new moodle_exception('shortnametaken', '', '', $foundcoursenamestring);
}
$backupsettings = array();
// Check for backup and restore options.
if (!empty($params['options'])) {
foreach ($params['options'] as $option) {
// Strict check for a correct value (allways 1 or 0, true or false).
$value = clean_param($option['value'], PARAM_INT);
if ($value !== 0 and $value !== 1) {
throw new moodle_exception('invalidextparam', 'webservice', '', $option['name']);
}
switch ($option['name']) {
case 'activities':
case 'blocks':
case 'filters':
$backupsettings[$option['name']] = $value;
break;
default:
throw new moodle_exception('invalidextparam', 'webservice', '', $option['name']);
}
}
// Backup the course.
if (!empty($backupsettings['users'])) {
$mode = backup::MODE_SAMESITE;
} else {
$mode = backup::MODE_IMPORT;
}
// Backup the course.
$bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);
backup::INTERACTIVE_NO, $mode, $USER->id);
foreach ($backupsettings as $name => $value) {
$bc->get_plan()->get_setting($name)->set_value($value);
@@ -775,18 +804,29 @@ public static function duplicate_course($courseid, $fullname, $shortname, $categ
$backupbasepath = $bc->get_plan()->get_basepath();
$bc->execute_plan();
$results = $bc->get_results();
$file = $results['backup_destination'];
$bc->destroy();
// Restore the backup immediately.
// In a backup mode different than import, we need to unzip the file because the backup temp directory is deleted.
if (!empty($backupsettings['users'])) {
check_dir_exists($CFG->tempdir . '/backup');
$file->extract_to_pathname(get_file_packer(), $CFG->tempdir . '/backup/' . $backupid);
}
// Create new course.
$newcourseid = restore_dbops::create_new_course($params['fullname'], $params['shortname'], $params['categoryid']);
$rc = new restore_controller($backupid, $newcourseid,
backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, backup::TARGET_NEW_COURSE);
backup::INTERACTIVE_NO, $mode, $USER->id, backup::TARGET_NEW_COURSE);
foreach ($backupsettings as $name => $value) {
$rc->get_plan()->get_setting($name)->set_value($value);
$setting = $rc->get_plan()->get_setting($name);
if ($setting->get_status() == backup_setting::NOT_LOCKED) {
$setting->set_value($value);
}
}
if (!$rc->execute_precheck()) {

0 comments on commit 9aa84e9

Please sign in to comment.