function action_copycurr() { global $CFG; // TODO: replace print_object messages with notice messages $sesskey = required_param('sesskey', PARAM_TEXT); if (!confirm_sesskey($sesskey)) { print_error('invalidsesskey', 'error', 'index.php'); } $data = (array) data_submitted(); $clusterid = $this->required_param('id', PARAM_INT); if (empty($data)) { notify(get_string('nodatasubmit', 'block_curr_admin'), 'red'); } $targetcluster = new cluster($clusterid); // Retrieve all of the curriculums that need to be copied and assigned $prefixlen = strlen(self::CPY_CURR_PREFIX); foreach ($data as $datakey => $datavalue) { if (0 === strncmp($datakey, self::CPY_CURR_PREFIX, $prefixlen)) { $currid = (int) substr($datakey, $prefixlen); if (!$currid) { continue; } $curr = new curriculum($currid); $options = array('targetcluster' => $targetcluster); if ($this->optional_param(self::CPY_CURR_TRK_PREFIX . $currid, 0, PARAM_INT)) { $options['tracks'] = true; } if ($this->optional_param(self::CPY_CURR_CRS_PREFIX . $currid, 0, PARAM_INT)) { $options['courses'] = true; } if ($this->optional_param(self::CPY_CURR_CLS_PREFIX . $currid, 0, PARAM_INT)) { $options['classes'] = true; } if ($this->optional_param(self::CPY_CURR_TRK_PREFIX . $currid, 0, PARAM_INT)) { $options['tracks'] = true; } $options['moodlecourses'] = $this->optional_param(self::CPY_CURR_MDLCRS_PREFIX . $currid, 'copyalways', PARAM_ALPHA); $rv = $curr->duplicate($options); if (!empty($rv['errors'])) { foreach ($rv['errors'] as $error) { notify($error); } } /** * The following block of code performs any necessary post-processing, * primarily used for copying role assignments */ //we need to handle curricula first in case role assignments //at lower levels become redundant if (!empty($rv['curricula'])) { $curriculum = new stdClass(); $curriculum->id = $rv['curricula'][$curr->id]; curriculumpage::after_cm_entity_add($curriculum); } if (!empty($rv['tracks'])) { foreach ($rv['tracks'] as $trackid) { $track = new stdClass(); $track->id = $trackid; trackpage::after_cm_entity_add($track); } } if (!empty($rv['courses'])) { foreach ($rv['courses'] as $courseid) { $course = new stdClass(); $course->id = $courseid; coursepage::after_cm_entity_add($course); } } if (!empty($rv['classes'])) { foreach ($rv['classes'] as $classid) { $class = new stdClass(); $class->id = $classid; cmclasspage::after_cm_entity_add($class); } } if (!empty($rv['curricula'])) { $newcurr = new curriculum($rv['curricula'][$curr->id]); $curr->newname = $newcurr->name; notify(get_string('clustcpycurr', 'block_curr_admin', $curr), 'notifysuccess'); } } } redirect($CFG->wwwroot . '/curriculum/index.php?id=' . $data['id'] . '&s=clstcur', '', 2); }
/** * Test validation of duplicate programs. */ public function test_programvalidation_preventsduplicates() { global $DB; $this->load_csv_data(); // Need program and userset. $userset = new stdClass(); $userset->id = 1; $userset->name = 'test'; $program = new curriculum(array('idnumber' => 'test', 'name' => 'test')); $options = array(); $options['targetcluster'] = $userset; $options['moodlecourses'] = 'copyalways'; $options['classmap'] = array(); $return = $program->duplicate($options); // Make sure that a we get a program returned. $this->assertTrue(is_array($return['curricula'])); $id = $return['curricula']['']; $record = $DB->get_record('local_elisprogram_pgm', 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); }