/** * Clone a course. * @param array $options options for cloning. Valid options are: * - 'classes': whether or not to clone classes (default: false) * - 'moodlecourses': whether or not to clone Moodle courses (if they were * autocreated). Values can be (default: "copyalways"): * - "copyalways": always copy course * - "copyautocreated": only copy autocreated courses * - "autocreatenew": autocreate new courses from course template * - "link": link to existing course * - 'targetcluster': the cluster id or cluster object (if any) to * associate the clones with (default: none) * @return array array of array of object IDs created. Key in outer array * is type of object (plural). Key in inner array is original object ID, * value is new object ID. Outer array also has an entry called 'errors', * which is an array of any errors encountered when duplicating the * object. */ function duplicate($options) { global $CURMAN; require_once CURMAN_DIRLOCATION . '/lib/cmclass.class.php'; require_once CURMAN_DIRLOCATION . '/lib/coursetemplate.class.php'; $objs = array('errors' => array()); if (isset($options['targetcluster'])) { $cluster = $options['targetcluster']; if (!is_object($cluster) || !is_a($cluster, 'cluster')) { $options['targetcluster'] = $cluster = new cluster($cluster); } } // clone main course object $clone = new course($this); unset($clone->id); if (isset($cluster)) { // if cluster specified, append cluster's name to course $clone->name = $clone->name . ' - ' . $cluster->name; $clone->idnumber = $clone->idnumber . ' - ' . $cluster->name; } $clone = new course(addslashes_recursive($clone)); if (!$clone->add()) { $objs['errors'][] = get_string('failclustcpycurrcrs', 'block_curr_admin', $this); return $objs; } $objs['courses'] = array($this->id => $clone->id); $options['targetcourse'] = $clone->id; // copy completion elements $compelems = $this->get_completion_elements(); if (!empty($compelems)) { foreach ($compelems as $compelem) { $compelem = addslashes_recursive($compelem); unset($compelem->id); $clone->save_completion_element($compelem); } } // copy template $template = $CURMAN->db->get_record(CTTABLE, 'courseid', $this->id); $template = new coursetemplate($template); unset($template->id); $template->courseid = $clone->id; $template->add(); // FIXME: copy tags // copy the classes if (!empty($options['classes'])) { $classes = cmclass_get_record_by_courseid($this->id); if (!empty($classes)) { $objs['classes'] = array(); foreach ($classes as $class) { $class = new cmclass($class); $rv = $class->duplicate($options); if (isset($rv['errors']) && !empty($rv['errors'])) { $objs['errors'] = array_merge($objs['errors'], $rv['errors']); } if (isset($rv['classes'])) { $objs['classes'] = $objs['classes'] + $rv['classes']; } } } } return $objs; }
/** * Clone a track * @param array $options options for cloning. Valid options are: * - 'targetcurriculum': the curriculum id to associate the clones with * (default: same as original track) * - 'classmap': a mapping of class IDs to use from the original track to * the cloned track. If a class from the original track is not mapped, a * new class will be created * - 'moodlecourse': whether or not to clone Moodle courses (if they were * autocreated). Values can be (default: "copyalways"): * - "copyalways": always copy course * - "copyautocreated": only copy autocreated courses * - "autocreatenew": autocreate new courses from course template * - "link": link to existing course * @return array array of array of object IDs created. Key in outer array * is type of object (plural). Key in inner array is original object ID, * value is new object ID. Outer array also has an entry called 'errors', * which is an array of any errors encountered when duplicating the * object. */ function duplicate($options = array()) { global $CURMAN; $objs = array('errors' => array()); if (isset($options['targetcluster'])) { $cluster = $options['targetcluster']; if (!is_object($cluster) || !is_a($cluster, 'cluster')) { $options['targetcluster'] = $cluster = new cluster($cluster); } } // clone main track object $clone = new track($this); unset($clone->id); if (isset($options['targetcurriculum'])) { $clone->curid = $options['targetcurriculum']; } if (isset($cluster)) { // if cluster specified, append cluster's name to track $clone->idnumber = $clone->idnumber . ' - ' . $cluster->name; $clone->name = $clone->name . ' - ' . $cluster->name; } $clone = new track(addslashes_recursive($clone)); $clone->autocreate = false; // avoid warnings if (!$clone->add()) { $objs['errors'][] = get_string('failclustcpytrk', 'block_curr_admin', $this); return $objs; } $objs['tracks'] = array($this->id => $clone->id); // associate with target cluster (if any) if (isset($cluster)) { clustertrack::associate($cluster->id, $clone->id); } // copy classes $clstrks = track_assignment_get_listing($this->id); if (!empty($clstrks)) { $objs['classes'] = array(); if (!isset($options['classmap'])) { $options['classmap'] = array(); } foreach ($clstrks as $clstrkdata) { $newclstrk = new trackassignmentclass($clstrkdata); $newclstrk->trackid = $clone->id; unset($newclstrk->id); if (isset($options['classmap'][$clstrkdata->clsid])) { // use existing duplicate class $class = new cmclass($options['classmap'][$clstrkdata->clsid]); } else { // no existing duplicate -> duplicate class $class = new cmclass($clstrkdata->clsid); $rv = $class->duplicate($options); if (isset($rv['errors']) && !empty($rv['errors'])) { $objs['errors'] = array_merge($objs['errors'], $rv['errors']); } if (isset($rv['classes'])) { $objs['classes'] = $objs['classes'] + $rv['classes']; } } $newclstrk->classid = $class->id; $newclstrk->courseid = $class->courseid; $newclstrk->add(); } } return $objs; }