/**
  * Test that the pm_migrate_tags() function works correctly
  */
 public function test_pmmigratetags()
 {
     global $DB;
     $this->load_csv_data();
     // ELIS-7599: create bogus tag instance data (ala 1.9).
     $tag = (object) array('name' => 'bogus_tag', 'description' => 'Bogus Tag Description', 'timecreated' => 1327958800, 'timemodified' => 1327958800);
     $tagid = $DB->insert_record('local_elisprogram_tag', $tag);
     $taginstance = (object) array('instancetype' => 'cur', 'instanceid' => 999999, 'tagid' => $tagid, 'data' => '', 'timecreated' => 1327958800, 'timemodified' => 132795880);
     $DB->insert_record('local_elisprogram_tag_inst', $taginstance);
     // Migrate the legacy tag data to new ELIS fields.
     pm_migrate_tags();
     $this->assertTrue(!$DB->get_records('local_elisprogram_tag_inst', array('tagid' => $tagid)));
     // Initialize the program object.
     $program = new curriculum(1);
     $program->reset_custom_field_list();
     $program->load();
     $program = $program->to_object();
     // Get the field data from the object.
     $this->assertObjectHasAttribute('field__19upgrade_curriculum_tags', $program);
     $this->assertEquals(1, count($program->field__19upgrade_curriculum_tags));
     $this->assertEquals('Testing data', $program->field__19upgrade_curriculum_tag_data_Test_tag);
     // Let's do some extra DB-level validation (though it's probably not necessary).
     $field = $DB->get_record(field::TABLE, array('shortname' => '_19upgrade_curriculum_tags'));
     $this->assertGreaterThan(0, $field->id);
     $context = \local_elisprogram\context\program::instance($program->id);
     $this->assertTrue($DB->record_exists(field_data_char::TABLE, array('contextid' => $context->id, 'fieldid' => $field->id)));
 }
Example #2
0
 function delete()
 {
     // delete associated data
     require_once elis::lib('data/data_filter.class.php');
     //filter specific for tracks, due to different field name
     $filter = new field_filter('curid', $this->id);
     track::delete_records($filter, $this->_db);
     //filter for all other associations
     $filter = new field_filter('curriculumid', $this->id);
     clustercurriculum::delete_records($filter, $this->_db);
     curriculumcourse::delete_records($filter, $this->_db);
     curriculumstudent::delete_records($filter, $this->_db);
     parent::delete();
     //clean up the curriculum context instance
     $context = \local_elisprogram\context\program::instance($this->id);
     $context->delete();
 }
Example #3
0
 /**
  * ELIS-4745: Test for assigning a user a role on a program context
  */
 public function test_assignuserforprogramctx()
 {
     global $DB;
     // Get role to assign (we'll just take the first one returned).
     $rolesctx = $DB->get_records('role_context_levels', array('contextlevel' => CONTEXT_ELIS_PROGRAM));
     foreach ($rolesctx as $rolectx) {
         $roleid = $rolectx->roleid;
         break;
     }
     // Get user to assign role.
     $user = new user(103);
     $muser = $user->get_moodleuser();
     // Get specific context.
     $cur = new curriculum(1);
     $context = \local_elisprogram\context\program::instance($cur->id);
     // Assign role.
     $this->assertGreaterThan(0, role_assign($roleid, $muser->id, $context->id));
 }
Example #4
0
 /**
  * Test creating a new program entity with a default role assignment defined.
  */
 public function test_createprogramwithdefaultroleassignment()
 {
     global $DB, $USER;
     list($rcid, $reid) = $this->create_roles('program');
     // Setup the editor role to be the default role for the program context.
     elis::$config->local_elisprogram->default_curriculum_role_id = $reid;
     $sysctx = context_system::instance();
     // Assign the test user the creator role.
     role_assign($rcid, 100, $sysctx->id);
     // Create a new program entity.
     $data = array('idnumber' => 'program100', 'name' => 'program100', 'description' => 'program100');
     $obj = new curriculum($data);
     $obj->save();
     // Initialize a new program management page and invoke the code that handles default role assignments.
     $page = new curriculumpage();
     $page->after_cm_entity_add($obj);
     $programctx = \local_elisprogram\context\program::instance($obj->id);
     $params = array('roleid' => $reid, 'userid' => $USER->id, 'contextid' => $programctx->id);
     $this->assertTrue($DB->record_exists('role_assignments', $params));
 }
Example #5
0
 /**
  * Obtain the appropriate information regarding program courses as needed
  * by the dashboard
  *
  * @param boolean $tab_sensitive true if we need to be concerned about whether
  *                               we are on the current or archived tab, otherwise
  *                               false
  * @param boolean $show_archived specifies whether we're showing archived or non-
  *                               archived courses (ignored if tab_sensitive is false)
  * @param boolean $showcompleted specifies whether we're showing passed and failed
  *                               courses in addition to ones in progress
  * @param mixed $programid a specific program id to look for data related to, or
  *                         NULL for all
  * @return array a list of values, where the first entry is the user's course list,
  *               the second is a mapping of programs to a course listing, the third
  *               is a list of classes handled, and the fourth is the number of programs
  *               handled, the fifth is a mapping of program ids to number of compelted
  *               courses, the sixth is a mapping of program ids to total number of courses
  */
 function get_dashboard_program_data($tab_sensitive, $show_archived, $showcompleted = false, $programid = NULL)
 {
     global $CFG, $DB;
     $archive_var = '_elis_program_archive';
     $classids = array();
     //track mapping of programs to course / class listings
     $curriculas = array();
     $totalcurricula = 0;
     //map program ids to appropriate counts
     $completecoursesmap = array();
     $totalcoursesmap = array();
     $params = array($this->id);
     //set up a condition for when handling a specific program
     $program_condition = '';
     if ($programid !== NULL) {
         $program_condition = 'AND cur.id = ?';
         $params[] = $programid;
     }
     $sql = 'SELECT curstu.id, curstu.curriculumid as curid, cur.name as name
               FROM {' . curriculumstudent::TABLE . '} curstu
               JOIN {' . curriculum::TABLE . '} cur
                 ON cur.id = curstu.curriculumid
              WHERE curstu.userid = ?
              ' . $program_condition . '
           ORDER BY cur.priority ASC, cur.name ASC';
     //mapping of completion status to display string
     $status_mapping = array(STUSTATUS_PASSED => get_string('passed', 'local_elisprogram'), STUSTATUS_FAILED => get_string('failed', 'local_elisprogram'), STUSTATUS_NOTCOMPLETE => get_string('n_completed', 'local_elisprogram'));
     if ($usercurs = $DB->get_records_sql($sql, $params)) {
         //^pre-ELIS-3615 WAS: if ($usercurs = curriculumstudent::get_curricula($this->id)) {
         foreach ($usercurs as $usercur) {
             // Check if this curricula is set as archived and whether we want to display it
             $crlm_context = \local_elisprogram\context\program::instance($usercur->curid);
             $data_array = field_data::get_for_context_and_field($crlm_context, $archive_var);
             $crlm_archived = 0;
             if (!empty($data_array) && is_object($data_array->rs) && !empty($data_array->rs)) {
                 $crlm_archived = !empty($data_array->rs->current()->data) ? 1 : 0;
             }
             //being insensitive to which tab we're on gets us this listing "for free"
             if (!$tab_sensitive || $show_archived == $crlm_archived) {
                 $totalcurricula++;
                 $curriculas[$usercur->curid]['id'] = $usercur->curid;
                 $curriculas[$usercur->curid]['name'] = $usercur->name;
                 $data = array();
                 //count our totals per-program
                 //note that "course" is really one enrolment, so this should
                 //count each enrolment, plus one for each unenrolled program course
                 $totalcourses = 0;
                 $completecourses = 0;
                 $courses = curriculumcourse_get_listing($usercur->curid, 'curcrs.position, crs.name', 'ASC');
                 foreach ($courses as $course) {
                     $course_obj = new course($course->courseid);
                     $coursedesc = $course_obj->syllabus;
                     $cdata = student_get_class_from_course($course->courseid, $this->id);
                     if ($cdata->valid() === true) {
                         foreach ($cdata as $classdata) {
                             //count each enrolment as one "course"
                             $totalcourses++;
                             if (!in_array($classdata->id, $classids)) {
                                 $classids[] = $classdata->id;
                             }
                             if ($classdata->completestatusid == STUSTATUS_PASSED || $classdata->completestatusid == STUSTATUS_FAILED) {
                                 //count completed enrolments
                                 $completecourses++;
                             }
                             if (!$showcompleted && ($classdata->completestatusid == STUSTATUS_PASSED || $classdata->completestatusid == STUSTATUS_FAILED)) {
                                 //not showing completed courses, so skip this course
                                 continue;
                             }
                             if ($mdlcrs = moodle_get_course($classdata->id)) {
                                 $coursename = '<a href="' . $CFG->wwwroot . '/course/view.php?id=' . $mdlcrs . '">' . $course->coursename . '</a>';
                             } else {
                                 $coursename = $course->coursename;
                             }
                             $data[] = array($coursename, $classdata->idnumber, $coursedesc, pm_display_grade($classdata->grade), $status_mapping[$classdata->completestatusid], $classdata->completestatusid == STUSTATUS_PASSED && !empty($classdata->completetime) ? userdate($classdata->completetime, get_string('pm_date_format', 'local_elisprogram')) : get_string('na', 'local_elisprogram'));
                         }
                     } else {
                         //count this unenrolled course toward the total
                         $totalcourses++;
                         $data[] = array($course->coursename, get_string('dashboard_na', 'local_elisprogram'), $coursedesc, 0, get_string('not_enrolled', 'local_elisprogram'), get_string('na', 'local_elisprogram'));
                     }
                     unset($cdata);
                 }
                 unset($courses);
                 $curriculas[$usercur->curid]['data'] = $data;
                 //associate this program id with the appropriate counts
                 $completecoursesmap[$usercur->curid] = $completecourses;
                 $totalcoursesmap[$usercur->curid] = $totalcourses;
             } else {
                 // Keep note of the classid's regardless if set archived or not for later use in determining non-curricula courses
                 $courses = curriculumcourse_get_listing($usercur->curid, 'curcrs.position, crs.name', 'ASC');
                 foreach ($courses as $course) {
                     $cdata = student_get_class_from_course($course->courseid, $this->id);
                     foreach ($cdata as $classdata) {
                         if (!in_array($classdata->id, $classids)) {
                             $classids[] = $classdata->id;
                         }
                     }
                     unset($cdata);
                 }
                 unset($courses);
             }
         }
     }
     return array($usercurs, $curriculas, $classids, $totalcurricula, $completecoursesmap, $totalcoursesmap);
 }
Example #6
0
 /**
  * Performs program delete
  * @throws moodle_exception If there was an error in passed parameters.
  * @throws data_object_exception If there was an error creating the entity.
  * @param array $data The incoming data parameter.
  * @return array An array of parameters, if successful.
  */
 public static function program_delete(array $data)
 {
     global $USER, $DB;
     if (static::require_elis_dependencies() !== true) {
         throw new moodle_exception('ws_function_requires_elis', 'local_datahub');
     }
     // Parameter validation.
     $params = self::validate_parameters(self::program_delete_parameters(), array('data' => $data));
     // Context validation.
     $context = context_user::instance($USER->id);
     self::validate_context($context);
     $data = (object) $data;
     // Validate program exists
     if (!($curid = $DB->get_field(curriculum::TABLE, 'id', array('idnumber' => $data->idnumber)))) {
         throw new data_object_exception('ws_program_delete_fail_invalid_idnumber', 'local_datahub', '', $data);
     }
     // Capability checking.
     require_capability('local/elisprogram:program_delete', \local_elisprogram\context\program::instance($curid));
     $prg = new curriculum($curid);
     $prg->delete();
     // Confirm delete & respond.
     if (!$DB->record_exists(curriculum::TABLE, array('id' => $curid))) {
         return array('messagecode' => get_string('ws_program_delete_success_code', 'local_datahub'), 'message' => get_string('ws_program_delete_success_msg', 'local_datahub'));
     } else {
         throw new data_object_exception('ws_program_delete_fail', 'local_datahub');
     }
 }
 /**
  * Test contexts in notifications.
  *
  * Covers:
  * local/elisprogram/lib/notifications.php:616
  * local/elisprogram/lib/notifications.php:617
  */
 public function test_notifications()
 {
     global $DB;
     $this->setup_users();
     context_helper::reset_caches();
     elis::$config->local_elisprogram->notify_classenrol_user = true;
     $rolesctx = $DB->get_records('role_context_levels', array('contextlevel' => CONTEXT_ELIS_PROGRAM));
     foreach ($rolesctx as $rolectx) {
         $roleid = $rolectx->roleid;
         break;
     }
     // Get user to assign role.
     $user = new user(103);
     $muser = $user->get_moodleuser();
     // Get specific context.
     $cur = $this->create_curriculum();
     $context = \local_elisprogram\context\program::instance($cur->id);
     // Assign role.
     $raid = role_assign($roleid, $muser->id, $context);
 }
Example #8
0
 /**
  * Performs program update
  * @throws moodle_exception If there was an error in passed parameters.
  * @throws data_object_exception If there was an error creating the entity.
  * @param array $data The incoming data parameter.
  * @return array An array of parameters, if successful.
  */
 public static function program_update(array $data)
 {
     global $USER, $DB;
     if (static::require_elis_dependencies() !== true) {
         throw new moodle_exception('ws_function_requires_elis', 'local_datahub');
     }
     // Parameter validation.
     $params = self::validate_parameters(self::program_update_parameters(), array('data' => $data));
     // Context validation.
     $context = context_user::instance($USER->id);
     self::validate_context($context);
     $data = (object) $data;
     // Validate program exists
     if (!($curid = $DB->get_field(curriculum::TABLE, 'id', array('idnumber' => $data->idnumber)))) {
         throw new data_object_exception('ws_program_update_fail_invalid_idnumber', 'local_datahub', '', $data);
     }
     // Capability checking.
     require_capability('local/elisprogram:program_edit', \local_elisprogram\context\program::instance($curid));
     // More validation
     if (isset($data->reqcredits)) {
         $reqcredits = (string) $data->reqcredits;
         $digits = strlen($reqcredits);
         $decies = 0;
         if (($decpos = strpos($reqcredits, '.')) !== false) {
             $decies = $digits - $decpos - 1;
             $digits = $decpos;
         }
         if (!is_numeric($reqcredits) || $digits > 8 || $decies > 2) {
             throw new data_object_exception('ws_program_update_fail_invalid_reqcredits', 'local_datahub', '', $data);
         }
     }
     if (isset($data->timetocomplete)) {
         $datedelta = new datedelta($data->timetocomplete);
         if (!$datedelta->getDateString()) {
             throw new data_object_exception('ws_program_update_fail_invalid_timetocomplete', 'local_datahub', '', $data);
         }
     }
     if (isset($data->frequency)) {
         $datedelta = new datedelta($data->frequency);
         if (!$datedelta->getDateString()) {
             throw new data_object_exception('ws_program_update_fail_invalid_frequency', 'local_datahub', '', $data);
         }
     }
     if (isset($data->priority)) {
         if ($data->priority < 0 || $data->priority > 10) {
             throw new data_object_exception('ws_program_update_fail_invalid_priority', 'local_datahub', '', $data);
         }
     }
     $prg = new curriculum($curid);
     $prg->load();
     $prg->set_from_data($data);
     $prg->save();
     // Respond.
     if (!empty($prg->id)) {
         $prgrec = (array) $DB->get_record(curriculum::TABLE, array('id' => $prg->id));
         $prgobj = $prg->to_array();
         // convert multi-valued custom field arrays to comma-separated listing
         $fields = self::get_program_custom_fields();
         foreach ($fields as $field) {
             // Generate name using custom field prefix.
             $fullfieldname = data_object_with_custom_fields::CUSTOM_FIELD_PREFIX . $field->shortname;
             if ($field->multivalued && isset($prgobj[$fullfieldname]) && is_array($prgobj[$fullfieldname])) {
                 $prgobj[$fullfieldname] = implode(',', $prgobj[$fullfieldname]);
             }
         }
         return array('messagecode' => get_string('ws_program_update_success_code', 'local_datahub'), 'message' => get_string('ws_program_update_success_msg', 'local_datahub'), 'record' => array_merge($prgrec, $prgobj));
     } else {
         throw new data_object_exception('ws_program_update_fail', 'local_datahub');
     }
 }
Example #9
0
 protected function get_context()
 {
     if (!isset($this->_context)) {
         $id = $this->required_param('id', PARAM_INT);
         $context_instance = \local_elisprogram\context\program::instance($id);
         $this->set_context($context_instance);
     }
     return $this->_context;
 }
 /**
  * Performs program_enrolment deletion
  * @throws moodle_exception If there was an error in passed parameters.
  * @throws data_object_exception If there was an error deleting the association.
  * @param array $data The incoming data parameter.
  * @return array An array of parameters, if successful.
  */
 public static function program_enrolment_delete(array $data)
 {
     global $DB, $USER;
     if (static::require_elis_dependencies() !== true) {
         throw new moodle_exception('ws_function_requires_elis', 'local_datahub');
     }
     // Parameter validation.
     $params = self::validate_parameters(self::program_enrolment_delete_parameters(), array('data' => $data));
     // Context validation.
     $context = context_user::instance($USER->id);
     self::validate_context($context);
     $data = (object) $data;
     // Parse program
     if (empty($data->program_idnumber) || !($curid = $DB->get_field(curriculum::TABLE, 'id', array('idnumber' => $data->program_idnumber)))) {
         throw new data_object_exception('ws_program_enrolment_delete_fail_invalid_program', 'local_datahub', '', $data);
     }
     // Capability checking.
     require_capability('local/elisprogram:program_enrol', \local_elisprogram\context\program::instance($curid));
     // Initialize version1elis importplugin for utility functions.
     $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis');
     $userparams = array();
     $userid = $importplugin->get_userid_from_record($data, '', $userparams);
     if ($userid == false) {
         $a = new stdClass();
         if (empty($userparams)) {
             $a->userparams = '{empty}';
         } else {
             $a->userparams = '';
             foreach ($userparams as $userfield => $uservalue) {
                 $subfield = strpos($userfield, '_');
                 $userfield = substr($userfield, $subfield === false ? 0 : $subfield + 1);
                 if (!empty($a->userparams)) {
                     $a->userparams .= ', ';
                 }
                 $a->userparams .= "{$userfield}: '{$uservalue}'";
             }
         }
         throw new data_object_exception('ws_program_enrolment_delete_fail_invalid_user', 'local_datahub', '', $a);
     }
     $id = $DB->get_field(curriculumstudent::TABLE, 'id', array('curriculumid' => $curid, 'userid' => $userid));
     // Respond.
     if (!empty($id) && ($stucur = new curriculumstudent($id))) {
         $stucur->delete();
         return array('messagecode' => get_string('ws_program_enrolment_delete_success_code', 'local_datahub'), 'message' => get_string('ws_program_enrolment_delete_success_msg', 'local_datahub'));
     } else {
         throw new data_object_exception('ws_program_enrolment_delete_fail', 'local_datahub');
     }
 }
 /**
  * Test program capability check.
  */
 public function test_programcapabilitycheck()
 {
     global $DB, $USER;
     $role = $DB->get_record('role', array('shortname' => 'editingteacher'));
     // Assign the test user the editing teacher role on a test program.
     $ctx = \local_elisprogram\context\program::instance($this->tprogramid);
     $this->assertGreaterThan(0, role_assign($role->id, $this->mdluserid, $ctx->id));
     load_role_access_by_context($role->id, $ctx, $USER->access);
     // We need to force the accesslib cache to refresh.
     // Validate the return value when looking at the 'curriculum' level.
     $contextscurriculum = new pm_context_set();
     $contextscurriculum->contexts = array('curriculum' => array($this->tprogramid));
     $contextscurriculum->contextlevel = 'curriculum';
     $perm = 'local/elisprogram:userset_enrol_userset_user';
     $contexts = pm_context_set::for_user_with_capability('curriculum', $perm, $this->mdluserid, false);
     $this->assertEquals($contextscurriculum, $contexts);
     // Validate the return value when looking at the 'track' level.
     $contextstrack = new pm_context_set();
     $contextstrack->contexts = array('curriculum' => array($this->tprogramid));
     $contextstrack->contextlevel = 'track';
     $contexts = pm_context_set::for_user_with_capability('track', 'local/elisprogram:userset_enrol_userset_user', $this->mdluserid);
     $this->assertEquals($contextstrack, $contexts);
     // Validate the return value when looking at the 'course' level.
     $contextscourse = new pm_context_set();
     $contextscourse->contexts = array('curriculum' => array($this->tprogramid));
     $contextscourse->contextlevel = 'course';
     $contexts = pm_context_set::for_user_with_capability('course', 'local/elisprogram:userset_enrol_userset_user', $this->mdluserid);
     $this->assertEquals($contextscourse, $contexts);
     // Validate the return value when looking at the 'class' level.
     $contextsclass = new pm_context_set();
     $contextsclass->contexts = array('curriculum' => array($this->tprogramid));
     $contextsclass->contextlevel = 'class';
     $contexts = pm_context_set::for_user_with_capability('class', 'local/elisprogram:userset_enrol_userset_user', $this->mdluserid);
     $this->assertEquals($contextsclass, $contexts);
     // Validate checking for users with the given capability on this context.
     $users = pm_get_users_by_capability('curriculum', $this->tprogramid, 'local/elisprogram:userset_enrol_userset_user');
     $this->assertEquals($this->mdluserid, current($users)->id);
 }
 /**
  * Test available table shows only programs the assigner has permission to enrol into based on the
  * local/elisprogram:program_enrol permissions on the program or higher context level.
  *
  * @dataProvider dataprovider_available_permissions_program_enrol
  * @param array $programstoallow An array of program IDs to assign the local/elisprogram:program_enrol permission at.
  * @param int $tableuserid The ID of the user we're managing.
  * @param array $expectedresults The expected page of results.
  * @param int $expectedtotal The expected number of total results.
  */
 public function test_available_permissions_program_enrol($programstoallow, $tableuserid, $expectedresults, $expectedtotal)
 {
     global $USER, $DB, $CFG;
     $userbackup = $USER;
     // Set up permissions.
     $USER = $this->setup_permissions_test();
     foreach ($programstoallow as $programid) {
         $this->give_permission_for_context($USER->id, 'local/elisprogram:program_enrol', \local_elisprogram\context\program::instance($programid));
     }
     // Construct test table.
     $table = new deepsight_datatable_userprogram_available_mock($DB, 'test', 'http://localhost', 'testuniqid');
     $table->set_userid($tableuserid);
     // Perform test.
     $actualresults = $table->get_search_results(array(), array(), 0, 20);
     // Verify result.
     $this->assert_search_results($expectedresults, $expectedtotal, $actualresults);
     // Restore user.
     $USER = $userbackup;
 }
 /**
  * Validate that mappings are applied during the program update action
  */
 public function test_mapping_applied_during_program_update()
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/local/eliscore/lib/data/customfield.class.php';
     require_once $CFG->dirroot . '/local/elisprogram/accesslib.php';
     require_once $CFG->dirroot . '/local/elisprogram/lib/data/curriculum.class.php';
     $this->init_mapping();
     $customfieldid = $this->create_custom_field(CONTEXT_ELIS_PROGRAM);
     set_config('enable_curriculum_expiration', 1, 'local_elisprogram');
     $program = new curriculum(array('idnumber' => 'testprogramidnumber'));
     $program->save();
     // Run the program update action.
     $record = new stdClass();
     $record->customaction = 'update';
     $record->customcontext = 'curriculum';
     $record->customidnumber = 'testprogramidnumber';
     $record->customname = 'updatedtestprogramname';
     $record->customdescription = 'updatedtestprogramdescription';
     $record->customreqcredits = '1';
     $record->customtimetocomplete = '2d';
     $record->customfrequency = '2d';
     $record->custompriority = 1;
     $record->customtestfieldshortname = '1';
     $this->run_pmentity_import((array) $record);
     // Validation.
     $data = array('idnumber' => 'testprogramidnumber', 'name' => 'updatedtestprogramname', 'reqcredits' => 1, 'timetocomplete' => '2d', 'frequency' => '2d', 'priority' => 1);
     $this->assertTrue($DB->record_exists(curriculum::TABLE, $data));
     $record = $DB->get_record(curriculum::TABLE, array('idnumber' => 'testprogramidnumber'));
     $this->assertEquals('updatedtestprogramdescription', $record->description);
     $instance = \local_elisprogram\context\program::instance(1);
     $this->assertTrue($DB->record_exists(field_data_int::TABLE, array('fieldid' => $customfieldid, 'contextid' => $instance->id, 'data' => 1)));
 }
Example #14
0
 /**
  * Test the get_child_contexts() function of \local_elisprogram\context\program
  */
 public function test_program_child_contexts()
 {
     $this->fixture_program_track();
     $ctx = \local_elisprogram\context\program::instance(1);
     $children = $ctx->get_child_contexts();
     $count = 0;
     foreach ($children as $child) {
         $this->assertTrue($child instanceof \local_elisprogram\context\track);
         $this->assertEquals(1, $child->instanceid);
         $count++;
         break;
     }
     $this->assertEquals(1, $count);
 }
 /**
  * Test available table shows correct search results based on local/elisprogram:track_enrol perms.
  *
  * @dataProvider dataprovider_available_track_enrol_perms
  * @param array $permcontexts An array of context objects for which to assign the local/elisprogram:track_enrol permission.
  * @param int $tabletrackid The ID of the track to use for the table.
  * @param array $expectedresults An array of expected search results.
  * @param int $expectedtotal The expected total number of search results.
  */
 public function test_available_track_enrol_perms($permcontexts, $tabletrackid, $expectedresults, $expectedtotal)
 {
     global $USER, $DB, $CFG;
     $userbackup = $USER;
     $USER = $this->setup_permissions_test();
     // Set up permissions.
     foreach ($permcontexts as $level => $id) {
         $context = null;
         switch ($level) {
             case 'system':
                 $permcontext = context_system::instance();
                 break;
             case 'program':
                 $permcontext = \local_elisprogram\context\program::instance($id);
                 break;
             case 'track':
                 $permcontext = \local_elisprogram\context\track::instance($id);
                 break;
         }
         $this->give_permission_for_context($USER->id, 'local/elisprogram:track_enrol', $permcontext);
     }
     // Construct test table.
     $table = new deepsight_datatable_trackuser_available_mock($DB, 'test', 'http://localhost', 'testuniqid');
     $table->set_trackid($tabletrackid);
     // Perform test.
     $actualresults = $table->get_search_results(array(), array(), 0, 20);
     // Verify result.
     $this->assert_search_results($expectedresults, $expectedtotal, $actualresults);
     // Restore user.
     $USER = $userbackup;
 }
Example #16
0
 /**
  * Returns ELIS track context instance.
  *
  * @static
  * @param int $instanceid The ELIS track id
  * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found;
  *                        IGNORE_MULTIPLE means return first, ignore multiple records found(not recommended);
  *                        MUST_EXIST means we will throw an exception if no record or multiple records found.
  * @return \local_elisprogram\context\track|bool A context instance or false if instance was not found.
  */
 public static function instance($instanceid, $strictness = MUST_EXIST)
 {
     global $DB;
     $contextlevel = \local_eliscore\context\helper::get_level_from_class_name(get_called_class());
     if ($context = \local_eliscore\context\base::cache_get($contextlevel, $instanceid)) {
         return $context;
     }
     $record = $DB->get_record('context', array('contextlevel' => $contextlevel, 'instanceid' => $instanceid));
     if (empty($record)) {
         $track = $DB->get_record(\track::TABLE, array('id' => $instanceid), 'id,idnumber,curid', $strictness);
         if (!empty($track)) {
             $parentcontext = \local_elisprogram\context\program::instance($track->curid);
             $record = \local_eliscore\context\base::insert_context_record($contextlevel, $track->id, $parentcontext->path);
         }
     }
     if (!empty($record)) {
         $context = new \local_elisprogram\context\track($record);
         \local_eliscore\context\base::cache_add($context);
         return $context;
     }
     return false;
 }
 /**
  * Test available table only shows programs that the assigner has the local/elisprogram::associate permission on.
  * @dataProvider dataprovider_available_permissions_associate
  * @param array $contextstoassign An array of information specifying the contexts to assign the associate permission on.
  *                                This is formatted like array('system' => true, 'program' => array(1, 2, 3))
  * @param int $tableusersetid The ID of the userset we're going to manage.
  * @param array $expectedresults The expected page of results.
  * @param int $expectedtotal The expected number of total results.
  */
 public function test_available_permissions_associate($contextstoassign, $tableusersetid, $expectedresults, $expectedtotal)
 {
     global $USER, $DB, $CFG;
     $userbackup = $USER;
     // Set up permissions.
     $USER = $this->setup_permissions_test();
     // Set up capabilities.
     foreach ($contextstoassign as $contexttype => $ids) {
         if ($contexttype === 'system') {
             $this->give_permission_for_context($USER->id, 'local/elisprogram:associate', context_system::instance());
         } else {
             foreach ($ids as $contextinstanceid) {
                 switch ($contexttype) {
                     case 'program':
                         $context = \local_elisprogram\context\program::instance($contextinstanceid);
                         break;
                 }
                 $this->give_permission_for_context($USER->id, 'local/elisprogram:associate', $context);
             }
         }
     }
     accesslib_clear_all_caches(true);
     // Construct test table.
     $table = new deepsight_datatable_usersetprogram_available_mock($DB, 'test', 'http://localhost', 'testuniqid');
     $table->set_usersetid($tableusersetid);
     // Perform test.
     $actualresults = $table->get_search_results(array(), array(), 0, 20);
     // Verify result.
     $this->assert_search_results($expectedresults, $expectedtotal, $actualresults);
     // Restore user.
     $USER = $userbackup;
 }
 /**
  * Create data for our test field field.
  *
  * @param int   $fieldid The database id of the PM custom field
  * @param array $data    The data to set
  */
 protected function create_field_data($fieldid, $data)
 {
     global $CFG, $DB;
     $field = new field($fieldid);
     $field->load();
     $fieldcontext = $DB->get_field(field_contextlevel::TABLE, 'contextlevel', array('fieldid' => $fieldid));
     // Obtain the PM user context.
     switch ($fieldcontext) {
         case CONTEXT_ELIS_USER:
             $context = \local_elisprogram\context\user::instance(200);
             break;
         case CONTEXT_ELIS_COURSE:
             $context = \local_elisprogram\context\course::instance(200);
             break;
         case CONTEXT_ELIS_CLASS:
             $context = \local_elisprogram\context\pmclass::instance(200);
             break;
         case CONTEXT_ELIS_PROGRAM:
             $context = \local_elisprogram\context\program::instance(200);
             break;
     }
     field_data_char::set_for_context_and_field($context, $field, $data);
 }
 /**
  * Create and update program custom fields
  *
  * @dataProvider ui_type_provider
  * @param string $uitype The string value representing a UI type
  * @param string $data Value for create
  * @param mixed $expected Expected value after create as a string or int
  * @param string $updateddata Value for update
  * @param mixed $updateexpected Expected value after update as string or int
  * @param string $name The name of the control
  * @param string $datatype The datatype of the field
  * @param mixed $maxlength The maxiumum length of the field as int or null
  * @param mixed $inctime Include time along with the date as string or null
  * @param mixed $options The options of the field as array or null
  */
 public function test_elis_program_custom_field_import($control, $data, $expected, $updateddata, $updateexpected, $name, $datatype, $maxlength, $inctime, $options)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php';
     require_once elis::lib('data/customfield.class.php');
     require_once elispm::lib('data/user.class.php');
     require_once elispm::lib('data/curriculum.class.php');
     if ($control === 'datetime' && is_array($expected)) {
         $expected = rlip_timestamp($expected[0], $expected[1], $expected[2], $expected[3], $expected[4], $expected[5]);
     }
     if ($control === 'datetime' && is_array($updateexpected)) {
         $updateexpected = rlip_timestamp($updateexpected[0], $updateexpected[1], $updateexpected[2], $updateexpected[3], $updateexpected[4], $updateexpected[5]);
     }
     $fieldid = $this->create_test_field($name, $datatype, $control, $inctime, $maxlength, $options, CONTEXT_ELIS_PROGRAM);
     $temp = new curriculum();
     // Prevent caching issues.
     $temp->reset_custom_field_list();
     $record = new stdClass();
     $record->action = 'create';
     $record->context = 'curriculum';
     $record->idnumber = 'testprogramid';
     $record->name = 'testprogram';
     $record->{$name} = $data;
     $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis');
     $importplugin->fslogger = new silent_fslogger(null);
     $importplugin->process_record('course', (object) $record, 'bogus');
     $programcontext = \local_elisprogram\context\program::instance($DB->get_field(curriculum::TABLE, 'id', array('idnumber' => 'testprogramid')));
     $this->assert_field_values($datatype, $control, $fieldid, $programcontext->id, $expected);
     // Update.
     $record = new stdClass();
     $record->action = 'update';
     $record->context = 'curriculum';
     $record->idnumber = 'testprogramid';
     $record->name = 'testprogram';
     $record->{$name} = $updateddata;
     $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis');
     $importplugin->fslogger = new silent_fslogger(null);
     $importplugin->process_record('course', (object) $record, 'bogus');
     $this->assert_field_values($datatype, $control, $fieldid, $programcontext->id, $updateexpected);
 }
Example #20
0
 /**
  * Hook that gets called after a CM entity is added through this page
  * (Note: this function should only use the id field from the supplied cm entity
  *  as the rest of the data is not guaranteed to be there)
  *
  * @param  object  $cm_entity  The CM entity added
  * @uses $DB
  * @uses $USER
  */
 function after_cm_entity_add($cm_entity)
 {
     global $DB, $USER;
     //make sure a valid role is set
     if (!empty(elis::$config->local_elisprogram->default_curriculum_role_id) && $DB->record_exists('role', array('id' => elis::$config->local_elisprogram->default_curriculum_role_id))) {
         //get the context instance for capability checking
         $context_instance = \local_elisprogram\context\program::instance($cm_entity->id);
         //assign the appropriate role if the user does not have the edit capability
         if (!has_capability('local/elisprogram:program_edit', $context_instance)) {
             role_assign(elis::$config->local_elisprogram->default_curriculum_role_id, $USER->id, $context_instance->id);
         }
     }
 }
 /**
  * Test that a new Track context instance can be created and saved to the database.
  */
 public function test_trackcontext()
 {
     $program = $this->initprogram();
     $program->save();
     $newobj = $this->inittrack($program->id);
     $newobj->save();
     $context = \local_elisprogram\context\track::instance($newobj->id);
     // Validate that a context record was actually created with correct values.
     $this->assertGreaterThan(0, $context->id);
     $this->assertEquals(CONTEXT_ELIS_TRACK, $context->contextlevel);
     $this->assertEquals($newobj->id, $context->instanceid);
     // Create the context path to validate this in the returned context object.
     $pctx = \local_elisprogram\context\program::instance($program->id);
     $path = '/' . SYSCONTEXTID . '/' . $pctx->id . '/' . $context->id;
     $this->assertEquals($path, $context->path);
     $this->assertEquals(substr_count($path, '/'), $context->depth);
 }