Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/course/modduplicate.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00030 require_once(dirname(dirname(__FILE__)) . '/config.php');
00031 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
00032 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
00033 require_once($CFG->libdir . '/filelib.php');
00034 
00035 $cmid       = required_param('cmid',    PARAM_INT);
00036 $courseid   = required_param('course',  PARAM_INT);
00037 
00038 $course     = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
00039 $cm         = get_coursemodule_from_id('', $cmid, $course->id, true, MUST_EXIST);
00040 $cmcontext  = get_context_instance(CONTEXT_MODULE, $cm->id);
00041 $context    = get_context_instance(CONTEXT_COURSE, $courseid);
00042 $section    = $DB->get_record('course_sections', array('id' => $cm->section, 'course' => $cm->course));
00043 
00044 require_login($course);
00045 require_sesskey();
00046 require_capability('moodle/course:manageactivities', $context);
00047 // Require both target import caps to be able to duplicate, see make_editing_buttons()
00048 require_capability('moodle/backup:backuptargetimport', $context);
00049 require_capability('moodle/restore:restoretargetimport', $context);
00050 
00051 $PAGE->set_title(get_string('duplicate'));
00052 $PAGE->set_heading($course->fullname);
00053 $PAGE->set_url(new moodle_url('/course/modduplicate.php', array('cmid' => $cm->id, 'courseid' => $course->id)));
00054 $PAGE->set_pagelayout('incourse');
00055 
00056 $output = $PAGE->get_renderer('core', 'backup');
00057 
00058 // backup the activity
00059 
00060 $bc = new backup_controller(backup::TYPE_1ACTIVITY, $cm->id, backup::FORMAT_MOODLE,
00061         backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);
00062 
00063 $backupid       = $bc->get_backupid();
00064 $backupbasepath = $bc->get_plan()->get_basepath();
00065 
00066 $bc->execute_plan();
00067 
00068 $bc->destroy();
00069 
00070 // restore the backup immediately
00071 
00072 $rc = new restore_controller($backupid, $courseid,
00073         backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, backup::TARGET_CURRENT_ADDING);
00074 
00075 if (!$rc->execute_precheck()) {
00076     $precheckresults = $rc->get_precheck_results();
00077     if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
00078         if (empty($CFG->keeptempdirectoriesonbackup)) {
00079             fulldelete($backupbasepath);
00080         }
00081 
00082         echo $output->header();
00083         echo $output->precheck_notices($precheckresults);
00084         echo $output->continue_button(new moodle_url('/course/view.php', array('id' => $course->id)));
00085         echo $output->footer();
00086         die();
00087     }
00088 }
00089 
00090 $rc->execute_plan();
00091 
00092 // now a bit hacky part follows - we try to get the cmid of the newly
00093 // restored copy of the module
00094 $newcmid = null;
00095 $tasks = $rc->get_plan()->get_tasks();
00096 foreach ($tasks as $task) {
00097     if (is_subclass_of($task, 'restore_activity_task')) {
00098         if ($task->get_old_contextid() == $cmcontext->id) {
00099             $newcmid = $task->get_moduleid();
00100             break;
00101         }
00102     }
00103 }
00104 
00105 // if we know the cmid of the new course module, let us move it
00106 // right below the original one. otherwise it will stay at the
00107 // end of the section
00108 if ($newcmid) {
00109     $newcm = get_coursemodule_from_id('', $newcmid, $course->id, true, MUST_EXIST);
00110     moveto_module($newcm, $section, $cm);
00111     moveto_module($cm, $section, $newcm);
00112 }
00113 
00114 $rc->destroy();
00115 
00116 if (empty($CFG->keeptempdirectoriesonbackup)) {
00117     fulldelete($backupbasepath);
00118 }
00119 
00120 $a          = new stdClass();
00121 $a->modtype = get_string('modulename', $cm->modname);
00122 $a->modname = format_string($cm->name);
00123 
00124 echo $output->header();
00125 
00126 if ($newcmid) {
00127     echo $output->confirm(
00128         get_string('duplicatesuccess', 'core', $a),
00129         new single_button(
00130             new moodle_url('/course/modedit.php', array('update' => $newcmid)),
00131             get_string('duplicatecontedit'),
00132             'get'),
00133         new single_button(
00134             new moodle_url('/course/view.php#section-' . $cm->sectionnum, array('id' => $cm->course)),
00135             get_string('duplicatecontcourse'),
00136             'get')
00137     );
00138 
00139 } else {
00140     echo $output->notification(get_string('duplicatesuccess', 'core', $a), 'notifysuccess');
00141     echo $output->continue_button(
00142         new moodle_url('/course/view.php#section-' . $cm->sectionnum, array('id' => $course->id))
00143     );
00144 }
00145 
00146 echo $output->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations