/** * Test validation that class duplicate with autocreate creates and links to a moodle course */ public function test_autocreatemoodlecourse_createsandlinksmoodlecourse() { global $DB; $class = new pmclass(1); $classmoodle = new classmoodlecourse(array('moodlecourseid' => 2, 'classid' => 1)); $classmoodle->save(); $userset = new stdClass(); $userset->name = 'test'; $options = array(); $options['targetcluster'] = $userset; $options['classes'] = 1; $options['moodlecourses'] = 'copyalways'; $options['classmap'] = array(); $return = $class->duplicate($options); // Make sure that a we get a class returned. $this->assertTrue(is_array($return['classes'])); // Get the new returned id. $id = $return['classes'][1]; $recordexists = $DB->record_exists('local_elisprogram_cls_mdl', array('classid' => $id)); // We want to validate that a link to the new moodle course was created. $this->assertTrue($recordexists); // Get the new course id. $record = $DB->get_record('local_elisprogram_cls_mdl', array('classid' => $id)); $courseexists = $DB->record_exists('course', array('id' => $record->moodlecourseid)); // We want to validate that new moodle course was created. $this->assertTrue($recordexists); ini_set('max_execution_time', '0'); }
/** * 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(array $options = array()) { $objs = array('errors' => array()); if (isset($options['targetcluster'])) { $userset = $options['targetcluster']; if (!is_object($userset) || !is_a($userset, 'userset')) { $options['targetcluster'] = $userset = new userset($userset); } } // Due to lazy loading, we need to pre-load this object $this->load(); // clone main track object $clone = new track($this); unset($clone->id); if (isset($options['targetcurriculum'])) { $clone->curid = $options['targetcurriculum']; } $idnumber = $clone->idnumber; $name = $clone->name; if (isset($userset)) { $to_append = ' - ' . $userset->name; // if cluster specified, append cluster's name to course $idnumber = append_once($idnumber, $to_append, array('maxlength' => 95)); $name = append_once($name, $to_append, array('maxlength' => 250)); } //get a unique idnumber $clone->idnumber = generate_unique_identifier(track::TABLE, 'idnumber', $idnumber, array('idnumber' => $idnumber)); if ($clone->idnumber != $idnumber) { //get the suffix appended and add it to the name $parts = explode('.', $clone->idnumber); $suffix = end($parts); $clone->name = $name . '.' . $suffix; } else { $clone->name = $name; } $clone->autocreate = false; // avoid warnings $clone->save(); $objs['tracks'] = array($this->id => $clone->id); // associate with target cluster (if any) if (isset($userset)) { clustertrack::associate($userset->id, $clone->id); } // copy classes $clstrks = track_assignment_get_listing($this->id); if ($clstrks->valid() === true) { $objs['classes'] = array(); if (!isset($options['classmap'])) { $options['classmap'] = array(); } foreach ($clstrks as $clstrkdata) { $newclstrk = new trackassignment($clstrkdata); $newclstrk->trackid = $clone->id; unset($newclstrk->id); if (isset($options['classmap'][$clstrkdata->clsid])) { // use existing duplicate class $class = new pmclass($options['classmap'][$clstrkdata->clsid]); } else { // no existing duplicate -> duplicate class $class = new pmclass($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->save(); } } unset($clstrks); return $objs; }
/** * Test validation of duplicate pm classes. */ public function test_classvalidation_preventsduplicates() { global $DB; $this->load_csv_data(); $class = new pmclass(array('courseid' => 1, 'idnumber' => 'test')); $userset = new stdClass(); $userset->name = 'test'; $options = array(); $options['targetcluster'] = $userset; $options['tracks'] = 1; $options['classes'] = 1; $options['moodlecourses'] = 'copyalways'; $options['classmap'] = array(); $return = $class->duplicate($options); // Make sure that a we get a class returned. $this->assertTrue(is_array($return['classes'])); $id = $return['classes']['']; $record = $DB->get_record('local_elisprogram_cls', array('id' => $id)); // We want to validate that the unique idnumber is "test - test_3". $expectedvalue = 'test - test.3'; $this->assertEquals($expectedvalue, $record->idnumber); }
/** * 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(array $options) { require_once elispm::lib('data/pmclass.class.php'); require_once elispm::lib('data/coursetemplate.class.php'); $objs = array('errors' => array()); if (isset($options['targetcluster'])) { $userset = $options['targetcluster']; if (!is_object($userset) || !is_a($userset, 'userset')) { $options['targetcluster'] = $userset = new userset($userset); } } // Due to lazy loading, we need to pre-load this object $this->load(); // clone main course object $clone = new course($this); unset($clone->id); $idnumber = $clone->idnumber; $name = $clone->name; if (isset($userset)) { $to_append = ' - ' . $userset->name; // if cluster specified, append cluster's name to course $idnumber = append_once($idnumber, $to_append, array('maxlength' => 95)); $name = append_once($name, $to_append, array('maxlength' => 250)); } //get a unique idnumber $clone->idnumber = generate_unique_identifier(course::TABLE, 'idnumber', $idnumber, array('idnumber' => $idnumber)); if ($clone->idnumber != $idnumber) { //get the suffix appended and add it to the name $parts = explode('.', $clone->idnumber); $suffix = end($parts); $clone->name = $name . '.' . $suffix; } else { $clone->name = $name; } $clone->save(); $objs['courses'] = array($this->id => $clone->id); $options['targetcourse'] = $clone->id; // copy completion elements $compelems = $this->get_completion_elements(); foreach ($compelems as $compelem) { unset($compelem->id); $clone->save_completion_element($compelem); } unset($compelems); // copy template $template = $this->_db->get_record(coursetemplate::TABLE, array('courseid' => $this->id)); $template = new coursetemplate($template); unset($template->id); $template->courseid = $clone->id; $template->save(); // copy the classes if (!empty($options['classes'])) { $classes = pmclass_get_record_by_courseid($this->id); if (!empty($classes)) { $objs['classes'] = array(); foreach ($classes as $class) { $class = new pmclass($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; }