/** * Test duplicate_module() */ public function test_duplicate_module() { $this->setAdminUser(); $this->resetAfterTest(); $course = self::getDataGenerator()->create_course(); $res = self::getDataGenerator()->create_module('resource', array('course' => $course)); $cm = get_coursemodule_from_id('resource', $res->cmid, 0, false, MUST_EXIST); $newcm = duplicate_module($course, $cm); // Make sure they are the same, except obvious id changes. foreach ($cm as $prop => $value) { if ($prop == 'id' || $prop == 'url' || $prop == 'instance' || $prop == 'added') { // Ignore obviously different properties. continue; } $this->assertEquals($value, $newcm->{$prop}); } }
/** * Duplicate a module on the course for ajax. * * @see mod_duplicate_module() * @param object $course The course * @param object $cm The course module to duplicate * @param int $sr The section to link back to (used for creating the links) * @throws moodle_exception if the plugin doesn't support duplication * @return Object containing: * - fullcontent: The HTML markup for the created CM * - cmid: The CMID of the newly created CM * - redirect: Whether to trigger a redirect following this change */ function mod_duplicate_activity($course, $cm, $sr = null) { global $PAGE; $newcm = duplicate_module($course, $cm); $resp = new stdClass(); if ($newcm) { $courserenderer = $PAGE->get_renderer('core', 'course'); $completioninfo = new completion_info($course); $modulehtml = $courserenderer->course_section_cm($course, $completioninfo, $newcm, null, array()); $resp->fullcontent = $courserenderer->course_section_cm_list_item($course, $completioninfo, $newcm, $sr); $resp->cmid = $newcm->id; } else { // Trigger a redirect. $resp->redirect = true; } return $resp; }
$section_mods = $modinfo->sections[$section]; if (is_array($section_mods)) { $progress_bar_elements = count($section_mods); $data_progress = new stdClass(); $data_progress->current = 0; $data_progress->size = $progress_bar_elements; $k = 0; $pbar->update_full(40, get_string('progress_counter', 'format_onetopic', $data_progress)); foreach ($section_mods as $modnumber) { $k++; $mod = $modinfo->cms[$modnumber]; $cm = get_coursemodule_from_id('', $mod->id, 0, true, MUST_EXIST); $modcontext = context_module::instance($cm->id); if (has_capability('moodle/course:manageactivities', $modcontext)) { // Duplicate the module. $newcm = duplicate_module($course, $cm); //Move new module to new section if ($newcm && is_object($newcm)) { moveto_module($newcm, $new_sectioninfo); } } $data_progress->current = $k; $percent = 40 + $k / $progress_bar_elements * 60; $pbar->update_full($percent, get_string('progress_counter', 'format_onetopic', $data_progress)); } } } else { $pbar->update_full(100, get_string('progress_full', 'format_onetopic')); } $section_togo = $num_newsection; } else {