/** * Test validation of duplicate tracks. */ public function test_trackvalidation_preventsduplicates() { global $DB; $this->load_csv_data(); // Need track and userset. $userset = new stdClass(); $userset->id = 1; $userset->name = 'test'; $track = new track(array('curid' => 4, 'name' => 'test', 'idnumber' => 'test')); $options = array(); $options['targetcluster'] = $userset; $options['targetcurriculum'] = 4; $options['moodlecourses'] = 'copyalways'; $options['tracks'] = 1; $options['classmap'] = array(); $return = $track->duplicate($options); // Make sure that a we get a program returned. $this->assertTrue(is_array($return['tracks'])); $id = $return['tracks']['']; $record = $DB->get_record('local_elisprogram_trk', array('id' => $id)); // We want to validate that the unique idnumber is "test - test.3". $expectedvalue = 'test - test.3'; $this->assertEquals($expectedvalue, $record->idnumber); // The name is also to be unique. $this->assertEquals($expectedvalue, $record->name); }
/** * Clone a curriculum. * @param array $options options for cloning. Valid options are: * - 'tracks': whether or not to clone tracks (default: false) * - 'courses': whether or not to clone courses (default: false) * - '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 = array()) { require_once CURMAN_DIRLOCATION . '/lib/track.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 curriculum object $clone = new curriculum($this); unset($clone->id); if (isset($cluster)) { // if cluster specified, append cluster's name to curriculum $clone->name = $clone->name . ' - ' . $cluster->name; $clone->idnumber = $clone->idnumber . ' - ' . $cluster->name; } $clone = new curriculum(addslashes_recursive($clone)); if (!$clone->add()) { $objs['errors'][] = get_string('failclustcpycurr', 'block_curr_admin', $this); return $objs; } $objs['curricula'] = array($this->id => $clone->id); $options['targetcurriculum'] = $clone->id; // associate with target cluster (if any) if (isset($cluster)) { clustercurriculum::associate($cluster->id, $clone->id); } if (!empty($options['courses'])) { // copy courses $currcrs = curriculumcourse_get_list_by_curr($this->id); if (!empty($currcrs)) { $objs['courses'] = array(); $objs['classes'] = array(); foreach ($currcrs as $currcrsdata) { $course = new course($currcrsdata->courseid); $rv = $course->duplicate($options); if (isset($rv['errors']) && !empty($rv['errors'])) { $objs['errors'] = array_merge($objs['errors'], $rv['errors']); } if (isset($rv['courses'])) { $objs['courses'] = $objs['courses'] + $rv['courses']; } if (isset($rv['classes'])) { $objs['classes'] = $objs['classes'] + $rv['classes']; } // associate with curriculum if (isset($rv['courses'][$course->id])) { $curcrs = new curriculumcourse($currcrsdata); unset($curcrs->id); $curcrs->courseid = $rv['courses'][$course->id]; $curcrs->curriculumid = $clone->id; $curcrs->add(); } } } } if (!empty($objs['errors'])) { return $objs; } if (!empty($options['tracks'])) { // copy tracks $tracks = track_get_listing('name', 'ASC', 0, 0, '', '', $this->id); if (isset($objs['courses'])) { $options['coursemap'] = $objs['courses']; } if (!empty($tracks)) { $objs['tracks'] = array(); if (isset($objs['courses'])) { $options['coursemap'] = $objs['courses']; } if (!isset($objs['classes'])) { $objs['classes'] = array(); } foreach ($tracks as $track) { $track = new track($track); $options['classmap'] = $objs['classes']; $rv = $track->duplicate($options); if (isset($rv['errors']) && !empty($rv['errors'])) { $objs['errors'] = array_merge($objs['errors'], $rv['errors']); } if (isset($rv['tracks'])) { $objs['tracks'] = $objs['tracks'] + $rv['tracks']; } if (isset($rv['classes'])) { $objs['classes'] = $objs['classes'] + $rv['classes']; } } } } return $objs; }
/** * Clone a curriculum. * @param array $options options for cloning. Valid options are: * - 'tracks': whether or not to clone tracks (default: false) * - 'courses': whether or not to clone courses (default: false) * - '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 = array()) { require_once elispm::lib('data/track.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 curriculum object $clone = new curriculum($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 curriculum $idnumber = append_once($idnumber, $to_append, array('maxlength' => 95)); $name = append_once($name, $to_append, array('maxlength' => 59)); } //get a unique idnumber $clone->idnumber = generate_unique_identifier(curriculum::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 = new curriculum($clone); $clone->save(); $objs['curricula'] = array($this->id => $clone->id); $options['targetcurriculum'] = $clone->id; // associate with target cluster (if any) if (isset($userset)) { clustercurriculum::associate($userset->id, $clone->id); } if (!empty($options['courses'])) { // copy courses $currcrs = curriculumcourse_get_list_by_curr($this->id); if ($currcrs->valid()) { $objs['courses'] = array(); $objs['classes'] = array(); foreach ($currcrs as $currcrsdata) { $course = new course($currcrsdata->courseid); $rv = $course->duplicate($options); if (isset($rv['errors']) && !empty($rv['errors'])) { $objs['errors'] = array_merge($objs['errors'], $rv['errors']); } if (isset($rv['courses'])) { $objs['courses'] = $objs['courses'] + $rv['courses']; } if (isset($rv['classes'])) { $objs['classes'] = $objs['classes'] + $rv['classes']; } // associate with curriculum if (isset($rv['courses'][$course->id])) { $curcrs = new curriculumcourse($currcrsdata); unset($curcrs->id); $curcrs->courseid = $rv['courses'][$course->id]; $curcrs->curriculumid = $clone->id; $curcrs->save(); } } } unset($currcrs); } if (!empty($objs['errors'])) { return $objs; } if (!empty($options['tracks'])) { // copy tracks $tracks = track_get_listing('name', 'ASC', 0, 0, '', '', $this->id); if (isset($objs['courses'])) { $options['coursemap'] = $objs['courses']; } if (!empty($tracks)) { $objs['tracks'] = array(); if (isset($objs['courses'])) { $options['coursemap'] = $objs['courses']; } if (!isset($objs['classes'])) { $objs['classes'] = array(); } foreach ($tracks as $track) { $track = new track($track); $options['classmap'] = $objs['classes']; $rv = $track->duplicate($options); if (isset($rv['errors']) && !empty($rv['errors'])) { $objs['errors'] = array_merge($objs['errors'], $rv['errors']); } if (isset($rv['tracks'])) { $objs['tracks'] = $objs['tracks'] + $rv['tracks']; } if (isset($rv['classes'])) { $objs['classes'] = $objs['classes'] + $rv['classes']; } } } } return $objs; }