Example #1
0
 /**
  * Test creating a new class entity with a default role assignment defined.
  */
 public function test_createclasswithdefaultroleassignment()
 {
     global $USER, $DB;
     list($rcid, $reid) = $this->create_roles('class');
     // Setup the editor role to be the default role for the class context.
     elis::$config->local_elisprogram->default_class_role_id = $reid;
     $sysctx = context_system::instance();
     // Assign the test user the creator role.
     role_assign($rcid, $USER->id, $sysctx->id);
     // Create a new class entity.
     $data = array('courseid' => 100, 'idnumber' => 'program100', 'name' => 'program100', 'description' => 'program100');
     $obj = new pmclass($data);
     $obj->save();
     // Initialize a new class management page and invoke the code that handles default role assignments.
     $page = new pmclasspage();
     $sink = $this->redirectMessages();
     $page->after_cm_entity_add($obj);
     $classctx = \local_elisprogram\context\pmclass::instance($obj->id);
     $params = array('roleid' => $reid, 'userid' => $USER->id, 'contextid' => $classctx->id);
     $this->assertTrue($DB->record_exists('role_assignments', $params));
 }
 /**
  * Do copy curriculum.
  */
 public function do_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', 'local_elisprogram'), 'red');
     }
     $targetcluster = new userset($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;
             }
             $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;
                     pmclasspage::after_cm_entity_add($class);
                 }
             }
             if (!empty($rv['curricula'])) {
                 $newcurr = new curriculum($rv['curricula'][$curr->id]);
                 $tempcurr = new stdClass();
                 $tempcurr->name = $curr->name;
                 $tempcurr->newname = $newcurr->name;
                 notify(get_string('clustcpycurr', 'local_elisprogram', $tempcurr), 'notifysuccess');
             }
         }
     }
     $tmppage = new clustercurriculumpage(array('id' => $data['id']));
     redirect($tmppage->url, '', 2);
 }