weekly release 2.1.7+
[moodle.git] / course / modduplicate.php
bloba9006b11ad3263b1ca1b60cf18fc59ed50506b92
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 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');
58 // backup the activity
60 $bc = new backup_controller(backup::TYPE_1ACTIVITY, $cm->id, backup::FORMAT_MOODLE,
61 backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);
63 $backupid = $bc->get_backupid();
64 $backupbasepath = $bc->get_plan()->get_basepath();
66 $bc->execute_plan();
68 $bc->destroy();
70 // restore the backup immediately
72 $rc = new restore_controller($backupid, $courseid,
73 backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, backup::TARGET_CURRENT_ADDING);
75 if (!$rc->execute_precheck()) {
76 $precheckresults = $rc->get_precheck_results();
77 if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
78 if (empty($CFG->keeptempdirectoriesonbackup)) {
79 fulldelete($backupbasepath);
82 echo $output->header();
83 echo $output->precheck_notices($precheckresults);
84 echo $output->continue_button(new moodle_url('/course/view.php', array('id' => $course->id)));
85 echo $output->footer();
86 die();
90 $rc->execute_plan();
92 // now a bit hacky part follows - we try to get the cmid of the newly
93 // restored copy of the module
94 $newcmid = null;
95 $tasks = $rc->get_plan()->get_tasks();
96 foreach ($tasks as $task) {
97 if (is_subclass_of($task, 'restore_activity_task')) {
98 if ($task->get_old_contextid() == $cmcontext->id) {
99 $newcmid = $task->get_moduleid();
100 break;
105 // if we know the cmid of the new course module, let us move it
106 // right below the original one. otherwise it will stay at the
107 // end of the section
108 if ($newcmid) {
109 $newcm = get_coursemodule_from_id('', $newcmid, $course->id, true, MUST_EXIST);
110 moveto_module($newcm, $section, $cm);
111 moveto_module($cm, $section, $newcm);
114 $rc->destroy();
116 if (empty($CFG->keeptempdirectoriesonbackup)) {
117 fulldelete($backupbasepath);
120 $a = new stdClass();
121 $a->modtype = get_string('modulename', $cm->modname);
122 $a->modname = format_string($cm->name);
124 echo $output->header();
126 if ($newcmid) {
127 echo $output->confirm(
128 get_string('duplicatesuccess', 'core', $a),
129 new single_button(
130 new moodle_url('/course/modedit.php', array('update' => $newcmid)),
131 get_string('duplicatecontedit'),
132 'get'),
133 new single_button(
134 new moodle_url('/course/view.php#section-' . $cm->sectionnum, array('id' => $cm->course)),
135 get_string('duplicatecontcourse'),
136 'get')
139 } else {
140 echo $output->notification(get_string('duplicatesuccess', 'core', $a), 'notifysuccess');
141 echo $output->continue_button(
142 new moodle_url('/course/view.php#section-' . $cm->sectionnum, array('id' => $course->id))
146 echo $output->footer();