2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Duplicates a given course module
20 * The script backups and restores a single activity as if it was imported
21 * from the same course, using the default import settings. The newly created
22 * copy of the activity is then moved right below the original one.
26 * @copyright 2011 David Mudrak <david@moodle.com>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 require_once(dirname(dirname(__FILE__
)) . '/config.php');
31 require_once($CFG->dirroot
. '/backup/util/includes/backup_includes.php');
32 require_once($CFG->dirroot
. '/backup/util/includes/restore_includes.php');
33 require_once($CFG->libdir
. '/filelib.php');
35 $cmid = required_param('cmid', PARAM_INT
);
36 $courseid = required_param('course', PARAM_INT
);
37 $sectionreturn = optional_param('sr', 0, PARAM_INT
);
39 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST
);
40 $cm = get_coursemodule_from_id('', $cmid, $course->id
, true, MUST_EXIST
);
41 $cmcontext = context_module
::instance($cm->id
);
42 $context = context_course
::instance($courseid);
43 $section = $DB->get_record('course_sections', array('id' => $cm->section
, 'course' => $cm->course
));
45 require_login($course);
47 require_capability('moodle/course:manageactivities', $context);
48 // Require both target import caps to be able to duplicate, see make_editing_buttons()
49 require_capability('moodle/backup:backuptargetimport', $context);
50 require_capability('moodle/restore:restoretargetimport', $context);
52 $PAGE->set_title(get_string('duplicate'));
53 $PAGE->set_heading($course->fullname
);
54 $PAGE->set_url(new moodle_url('/course/modduplicate.php', array('cmid' => $cm->id
, 'courseid' => $course->id
)));
55 $PAGE->set_pagelayout('incourse');
57 $output = $PAGE->get_renderer('core', 'backup');
60 $a->modtype
= get_string('modulename', $cm->modname
);
61 $a->modname
= format_string($cm->name
);
63 if (!plugin_supports('mod', $cm->modname
, FEATURE_BACKUP_MOODLE2
)) {
64 $url = new moodle_url('/course/view.php#section-' . $cm->sectionnum
, array('id' => $course->id
));
65 print_error('duplicatenosupport', 'error', $url, $a);
68 // backup the activity
70 $bc = new backup_controller(backup
::TYPE_1ACTIVITY
, $cm->id
, backup
::FORMAT_MOODLE
,
71 backup
::INTERACTIVE_NO
, backup
::MODE_IMPORT
, $USER->id
);
73 $backupid = $bc->get_backupid();
74 $backupbasepath = $bc->get_plan()->get_basepath();
80 // restore the backup immediately
82 $rc = new restore_controller($backupid, $courseid,
83 backup
::INTERACTIVE_NO
, backup
::MODE_IMPORT
, $USER->id
, backup
::TARGET_CURRENT_ADDING
);
85 if (!$rc->execute_precheck()) {
86 $precheckresults = $rc->get_precheck_results();
87 if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
88 if (empty($CFG->keeptempdirectoriesonbackup
)) {
89 fulldelete($backupbasepath);
92 echo $output->header();
93 echo $output->precheck_notices($precheckresults);
94 echo $output->continue_button(new moodle_url('/course/view.php', array('id' => $course->id
)));
95 echo $output->footer();
102 // now a bit hacky part follows - we try to get the cmid of the newly
103 // restored copy of the module
105 $tasks = $rc->get_plan()->get_tasks();
106 foreach ($tasks as $task) {
107 if (is_subclass_of($task, 'restore_activity_task')) {
108 if ($task->get_old_contextid() == $cmcontext->id
) {
109 $newcmid = $task->get_moduleid();
115 // if we know the cmid of the new course module, let us move it
116 // right below the original one. otherwise it will stay at the
117 // end of the section
119 $newcm = get_coursemodule_from_id('', $newcmid, $course->id
, true, MUST_EXIST
);
120 moveto_module($newcm, $section, $cm);
121 moveto_module($cm, $section, $newcm);
126 if (empty($CFG->keeptempdirectoriesonbackup
)) {
127 fulldelete($backupbasepath);
130 echo $output->header();
133 echo $output->confirm(
134 get_string('duplicatesuccess', 'core', $a),
136 new moodle_url('/course/modedit.php', array('update' => $newcmid)),
137 get_string('duplicatecontedit'),
140 course_get_url($course, $sectionreturn),
141 get_string('duplicatecontcourse'),
146 echo $output->notification(get_string('duplicatesuccess', 'core', $a), 'notifysuccess');
147 echo $output->continue_button(course_get_url($course, $sectionreturn));
150 echo $output->footer();