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
);
38 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST
);
39 $cm = get_coursemodule_from_id('', $cmid, $course->id
, true, MUST_EXIST
);
40 $cmcontext = get_context_instance(CONTEXT_MODULE
, $cm->id
);
41 $context = get_context_instance(CONTEXT_COURSE
, $courseid);
42 $section = $DB->get_record('course_sections', array('id' => $cm->section
, 'course' => $cm->course
));
44 require_login($course);
46 require_capability('moodle/course:manageactivities', $context);
47 // Require both target import caps to be able to duplicate, see make_editing_buttons()
48 require_capability('moodle/backup:backuptargetimport', $context);
49 require_capability('moodle/restore:restoretargetimport', $context);
51 $PAGE->set_title(get_string('duplicate'));
52 $PAGE->set_heading($course->fullname
);
53 $PAGE->set_url(new moodle_url('/course/modduplicate.php', array('cmid' => $cm->id
, 'courseid' => $course->id
)));
54 $PAGE->set_pagelayout('incourse');
56 $output = $PAGE->get_renderer('core', 'backup');
59 $a->modtype
= get_string('modulename', $cm->modname
);
60 $a->modname
= format_string($cm->name
);
62 if (!plugin_supports('mod', $cm->modname
, FEATURE_BACKUP_MOODLE2
)) {
63 $url = new moodle_url('/course/view.php#section-' . $cm->sectionnum
, array('id' => $course->id
));
64 print_error('duplicatenosupport', 'core', $url, $a);
67 // backup the activity
69 $bc = new backup_controller(backup
::TYPE_1ACTIVITY
, $cm->id
, backup
::FORMAT_MOODLE
,
70 backup
::INTERACTIVE_NO
, backup
::MODE_IMPORT
, $USER->id
);
72 $backupid = $bc->get_backupid();
73 $backupbasepath = $bc->get_plan()->get_basepath();
79 // restore the backup immediately
81 $rc = new restore_controller($backupid, $courseid,
82 backup
::INTERACTIVE_NO
, backup
::MODE_IMPORT
, $USER->id
, backup
::TARGET_CURRENT_ADDING
);
84 if (!$rc->execute_precheck()) {
85 $precheckresults = $rc->get_precheck_results();
86 if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
87 if (empty($CFG->keeptempdirectoriesonbackup
)) {
88 fulldelete($backupbasepath);
91 echo $output->header();
92 echo $output->precheck_notices($precheckresults);
93 echo $output->continue_button(new moodle_url('/course/view.php', array('id' => $course->id
)));
94 echo $output->footer();
101 // now a bit hacky part follows - we try to get the cmid of the newly
102 // restored copy of the module
104 $tasks = $rc->get_plan()->get_tasks();
105 foreach ($tasks as $task) {
106 if (is_subclass_of($task, 'restore_activity_task')) {
107 if ($task->get_old_contextid() == $cmcontext->id
) {
108 $newcmid = $task->get_moduleid();
114 // if we know the cmid of the new course module, let us move it
115 // right below the original one. otherwise it will stay at the
116 // end of the section
118 $newcm = get_coursemodule_from_id('', $newcmid, $course->id
, true, MUST_EXIST
);
119 moveto_module($newcm, $section, $cm);
120 moveto_module($cm, $section, $newcm);
125 if (empty($CFG->keeptempdirectoriesonbackup
)) {
126 fulldelete($backupbasepath);
129 echo $output->header();
132 echo $output->confirm(
133 get_string('duplicatesuccess', 'core', $a),
135 new moodle_url('/course/modedit.php', array('update' => $newcmid)),
136 get_string('duplicatecontedit'),
139 new moodle_url('/course/view.php#section-' . $cm->sectionnum
, array('id' => $cm->course
)),
140 get_string('duplicatecontcourse'),
145 echo $output->notification(get_string('duplicatesuccess', 'core', $a), 'notifysuccess');
146 echo $output->continue_button(
147 new moodle_url('/course/view.php#section-' . $cm->sectionnum
, array('id' => $course->id
))
151 echo $output->footer();