MDL-37774 Make moodle1_file_manager::migrate_directory() support trailing slash in...
[moodle.git] / course / modduplicate.php
blobd175a87bdd28ee904d7a7034d9815d3a028956aa
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
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.
24 * @package core
25 * @subpackage course
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', null, 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);
46 require_sesskey();
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');
59 $a = new stdClass();
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 = course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn));
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();
76 $bc->execute_plan();
78 $bc->destroy();
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 $url = course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn));
95 echo $output->continue_button($url);
96 echo $output->footer();
97 die();
101 $rc->execute_plan();
103 // now a bit hacky part follows - we try to get the cmid of the newly
104 // restored copy of the module
105 $newcmid = null;
106 $tasks = $rc->get_plan()->get_tasks();
107 foreach ($tasks as $task) {
108 if (is_subclass_of($task, 'restore_activity_task')) {
109 if ($task->get_old_contextid() == $cmcontext->id) {
110 $newcmid = $task->get_moduleid();
111 break;
116 // if we know the cmid of the new course module, let us move it
117 // right below the original one. otherwise it will stay at the
118 // end of the section
119 if ($newcmid) {
120 $newcm = get_coursemodule_from_id('', $newcmid, $course->id, true, MUST_EXIST);
121 moveto_module($newcm, $section, $cm);
122 moveto_module($cm, $section, $newcm);
125 $rc->destroy();
127 if (empty($CFG->keeptempdirectoriesonbackup)) {
128 fulldelete($backupbasepath);
131 echo $output->header();
133 if ($newcmid) {
134 echo $output->confirm(
135 get_string('duplicatesuccess', 'core', $a),
136 new single_button(
137 new moodle_url('/course/modedit.php', array('update' => $newcmid, 'sr' => $sectionreturn)),
138 get_string('duplicatecontedit'),
139 'get'),
140 new single_button(
141 course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)),
142 get_string('duplicatecontcourse'),
143 'get')
146 } else {
147 echo $output->notification(get_string('duplicatesuccess', 'core', $a), 'notifysuccess');
148 echo $output->continue_button(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
151 echo $output->footer();