Merge branch 'm21_MDL-28017' of git://github.com/danmarsden/moodle into MOODLE_21_STABLE
[moodle.git] / course / modduplicate.php
blob632f191853e34fb4a852aa2a2b0fc10e0d528a0e
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);
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);
45 require_sesskey();
46 require_capability('moodle/course:manageactivities', $context);
47 require_capability('moodle/backup:backuptargetimport', $context);
48 require_capability('moodle/restore:restoretargetimport', $context);
50 $PAGE->set_title(get_string('duplicate'));
51 $PAGE->set_heading($course->fullname);
52 $PAGE->set_url(new moodle_url('/course/modduplicate.php', array('cmid' => $cm->id, 'courseid' => $course->id)));
53 $PAGE->set_pagelayout('incourse');
55 $output = $PAGE->get_renderer('core', 'backup');
57 // backup the activity
59 $bc = new backup_controller(backup::TYPE_1ACTIVITY, $cm->id, backup::FORMAT_MOODLE,
60 backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);
62 $backupid = $bc->get_backupid();
63 $backupbasepath = $bc->get_plan()->get_basepath();
65 $bc->execute_plan();
67 $bc->destroy();
69 // restore the backup immediately
71 $rc = new restore_controller($backupid, $courseid,
72 backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, backup::TARGET_CURRENT_ADDING);
74 if (!$rc->execute_precheck()) {
75 $precheckresults = $rc->get_precheck_results();
76 if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
77 if (empty($CFG->keeptempdirectoriesonbackup)) {
78 fulldelete($backupbasepath);
81 echo $output->header();
82 echo $output->precheck_notices($precheckresults);
83 echo $output->continue_button(new moodle_url('/course/view.php', array('id' => $course->id)));
84 echo $output->footer();
85 die();
89 $rc->execute_plan();
91 // now a bit hacky part follows - we try to get the cmid of the newly
92 // restored copy of the module
93 $newcmid = null;
94 $tasks = $rc->get_plan()->get_tasks();
95 foreach ($tasks as $task) {
96 if (is_subclass_of($task, 'restore_activity_task')) {
97 if ($task->get_old_contextid() == $cmcontext->id) {
98 $newcmid = $task->get_moduleid();
99 break;
104 // if we know the cmid of the new course module, let us move it
105 // right below the original one. otherwise it will stay at the
106 // end of the section
107 if ($newcmid) {
108 $newcm = get_coursemodule_from_id('', $newcmid, $course->id, true, MUST_EXIST);
109 moveto_module($newcm, $section, $cm);
110 moveto_module($cm, $section, $newcm);
113 $rc->destroy();
115 if (empty($CFG->keeptempdirectoriesonbackup)) {
116 fulldelete($backupbasepath);
119 $a = new stdClass();
120 $a->modtype = get_string('modulename', $cm->modname);
121 $a->modname = format_string($cm->name);
123 echo $output->header();
125 if ($newcmid) {
126 echo $output->confirm(
127 get_string('duplicatesuccess', 'core', $a),
128 new single_button(
129 new moodle_url('/course/modedit.php', array('update' => $newcmid)),
130 get_string('duplicatecontedit'),
131 'get'),
132 new single_button(
133 new moodle_url('/course/view.php#section-' . $cm->sectionnum, array('id' => $cm->course)),
134 get_string('duplicatecontcourse'),
135 'get')
138 } else {
139 echo $output->notification(get_string('duplicatesuccess', 'core', $a), 'notifysuccess');
140 echo $output->continue_button(
141 new moodle_url('/course/view.php#section-' . $cm->sectionnum, array('id' => $course->id))
145 echo $output->footer();