/** * Method to add ELIS entity's custom fields to entity forms * * @param string $entity ELIS entity name: Eg. 'curriculum', 'track', 'course', 'class', 'cluster', 'user' * @param string $editcap The required edit capability * @param string $viewcap The required view capability * @param string $parententity The entity name of the parent object (optional) */ function add_custom_fields($entity, $editcap, $viewcap, $parententity = '') { $mform =& $this->_form; $fields = field::get_for_context_level($entity); $fields = $fields ? $fields : array(); if (isset($this->_customdata['obj'])) { if (isset($this->_customdata['obj']->id)) { // Use the current (existing) entity's context instance $contextlevel = \local_eliscore\context\helper::get_level_from_name($entity); $contextclass = \local_eliscore\context\helper::get_class_for_level($contextlevel); $context = $contextclass::instance($this->_customdata['obj']->id); } else { if (isset($this->_customdata['obj']->parent) && $parententity != '') { // ELIS-6498 -- Specify the parent entity type to get the correct parent context instance as we are // adding a new "child" entity $contextlevel = \local_eliscore\context\helper::get_level_from_name($parententity); $contextclass = \local_eliscore\context\helper::get_class_for_level($contextlevel); $context = $contextclass::instance($this->_customdata['obj']->parent); } else { if (isset($this->_customdata['obj']->courseid) && $parententity == 'course') { // ELIS-6498 -- Special handling of the course -> class hierarchy is required here $context = \local_elisprogram\context\course::instance($this->_customdata['obj']->courseid); } else { $context = context_system::instance(); } } } } else { $context = context_system::instance(); } require_once elis::plugin_file('elisfields_manual', 'custom_fields.php'); $lastcat = null; $entityid = isset($this->_customdata['obj']->id) ? $this->_customdata['obj']->id : 0; foreach ($fields as $rec) { $field = new field($rec); if (!isset($field->owners['manual'])) { continue; } // Capabilities for editing / viewing this context if (manual_field_is_view_or_editable($field, $context, $editcap, $viewcap, $entity, $entityid) != MANUAL_FIELD_NO_VIEW_OR_EDIT) { if ($lastcat != $rec->categoryid) { $lastcat = $rec->categoryid; $mform->addElement('header', "category_{$lastcat}", htmlspecialchars($rec->categoryname)); } manual_field_add_form_element($this, $mform, $context, $this->_customdata, $field, true, $editcap, $viewcap, $entity, $entityid); } } }
/** * Test available table only shows courses 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, 'course' => array(100, 101)) * @param int $tableprogramid The ID of the program 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, $tableprogramid, $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 'course': $context = \local_elisprogram\context\course::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_programcourse_available_mock($DB, 'test', 'http://localhost', 'testuniqid'); $table->set_programid($tableprogramid); // Perform test. $actualresults = $table->get_search_results(array(), array(), 0, 20); // Verify result. $this->assert_search_results($expectedresults, $expectedtotal, $actualresults); // Restore user. $USER = $userbackup; }
/** * Performs course update * @throws moodle_exception If there was an error in passed parameters. * @throws data_object_exception If there was an error updating the entity. * @param array $data The incoming data parameter. * @return array An array of parameters, if successful. */ public static function course_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::course_update_parameters(), array('data' => $data)); // Context validation. $context = context_user::instance($USER->id); self::validate_context($context); $data = (object) $data; // Validate course exists if (!($crsid = $DB->get_field(course::TABLE, 'id', array('idnumber' => $data->idnumber)))) { throw new data_object_exception('ws_course_update_fail_invalid_idnumber', 'local_datahub', '', $data); } // Capability checking. require_capability('local/elisprogram:course_edit', \local_elisprogram\context\course::instance($crsid)); // Initialize version1elis importplugin for utility functions. $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); // Validate credits. if (isset($data->credits) && !(is_numeric($data->credits) && $data->credits >= 0)) { throw new data_object_exception('ws_course_update_fail_invalid_credits', 'local_datahub', '', $data); } // Validate completion grade. if (isset($data->completion_grade) && !(is_numeric($data->completion_grade) && $data->completion_grade >= 0 && $data->completion_grade <= 100)) { throw new data_object_exception('ws_course_update_fail_invalid_completion_grade', 'local_datahub', '', $data); } // Handle assignment to program. if (isset($data->assignment) && !empty($data->assignment)) { $programid = $DB->get_field(curriculum::TABLE, 'id', array('idnumber' => $data->assignment)); if ($programid) { $curriculumcourseid = $DB->get_field(curriculumcourse::TABLE, 'id', array('curriculumid' => $programid, 'courseid' => $crsid)); // Only assign if it is not already assigned if (!$curriculumcourseid) { $data->curriculum = array($programid); } } else { throw new data_object_exception('ws_course_update_fail_invalid_assignment', 'local_datahub', '', $data); } } $course = new course($crsid); $course->load(); $course->set_from_data($data); $course->save(); // Handle linking to Moodle course. if (isset($data->link) && !empty($data->link)) { $moodlecourseid = $DB->get_field('course', 'id', array('shortname' => $data->link)); if ($moodlecourseid) { $importplugin->associate_course_to_moodle_course($data, $course->id); } else { throw new data_object_exception('ws_course_update_fail_invalid_link', 'local_datahub', '', $data); } } // Respond. if (!empty($course->id)) { $courserec = (array) $DB->get_record(course::TABLE, array('id' => $course->id)); $courseobj = $course->to_array(); // convert multi-valued custom field arrays to comma-separated listing $fields = self::get_course_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($courseobj[$fullfieldname]) && is_array($courseobj[$fullfieldname])) { $courseobj[$fullfieldname] = implode(',', $courseobj[$fullfieldname]); } } return array('messagecode' => get_string('ws_course_update_success_code', 'local_datahub'), 'message' => get_string('ws_course_update_success_msg', 'local_datahub'), 'record' => array_merge($courserec, $courseobj)); } else { throw new data_object_exception('ws_course_update_fail', 'local_datahub'); } }
/** * Test that a new Class context instance can be created and saved to the database. */ public function test_classcontext() { $course = $this->initcourse(); $course->save(); $newobj = $this->initclass($course->id); $newobj->save(); $context = \local_elisprogram\context\pmclass::instance($newobj->id); // Validate that a context record was actually created with correct values. $this->assertGreaterThan(0, $context->id); $this->assertEquals(CONTEXT_ELIS_CLASS, $context->contextlevel); $this->assertEquals($newobj->id, $context->instanceid); // Create the context path to validate this in the returned context object. $cctx = \local_elisprogram\context\course::instance($course->id); $path = '/' . SYSCONTEXTID . '/' . $cctx->id . '/' . $context->id; $this->assertEquals($path, $context->path); $this->assertEquals(substr_count($path, '/'), $context->depth); }
/** * Create and update course 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_course_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/course.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]); } $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $fieldid = $this->create_test_field($name, $datatype, $control, $inctime, $maxlength, $options, CONTEXT_ELIS_COURSE); $record = new stdClass(); $record->action = 'create'; $record->context = 'course'; $record->name = 'testcourse'; $record->idnumber = 'testcourseid'; $record->{$name} = $data; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->process_record('course', (object) $record, 'bogus'); $coursecontext = \local_elisprogram\context\course::instance($DB->get_field(course::TABLE, 'id', array('idnumber' => 'testcourseid'))); $this->assert_field_values($datatype, $control, $fieldid, $coursecontext->id, $expected); // Update. $record = new stdClass(); $record->action = 'update'; $record->context = 'course'; $record->idnumber = 'testcourseid'; $record->name = 'testcoure'; $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, $coursecontext->id, $updateexpected); }
/** * Test creating a new course entity with a default role assignment defined. */ public function test_createcoursewithdefaultroleassignment() { global $DB; list($rcid, $reid) = $this->create_roles('course'); // Setup the global USER object to be our test user. $USER = $DB->get_record('user', array('id' => 100)); // Setup the editor role to be the default role for the course context. elis::$config->local_elisprogram->default_course_role_id = $reid; $sysctx = context_system::instance(); // Assign the test user the creator role. role_assign($rcid, $USER->id, $sysctx->id); // Create a new course entity. $data = array('idnumber' => 'course100', 'name' => 'course100', 'syllabus' => 'course100'); $obj = new course($data); $obj->save(); // Initialize a new course management page and invoke the code that handles default role assignments. $page = new coursepage(); $page->after_cm_entity_add($obj); $coursectx = \local_elisprogram\context\course::instance($obj->id); $params = array('roleid' => $reid, 'userid' => $USER->id, 'contextid' => $coursectx->id); $this->assertTrue($DB->record_exists('role_assignments', $params)); }
function display_create() { // action_create() global $CFG, $DB; $target = $this->get_new_page(array('action' => 'create'), true); $form = new create_form($target->url); $data = $form->get_data(); if ($form->is_cancelled()) { redirect($this->url); // TBV return; } else { if ($data) { global $USER; require_once $CFG->dirroot . '/local/elisprogram/lib/data/course.class.php'; $request = new stdClass(); $request->userid = $USER->id; $request->firstname = $data->first; $request->lastname = $data->last; $request->email = $data->email; if (empty($data->title)) { $course_record = $DB->get_record(course::TABLE, array('id' => $data->courseid)); $data->title = $course_record->name; } $request->title = $data->title; $request->courseid = $data->courseid; $request->usecoursetemplate = empty($data->usecoursetemplate) ? 0 : 1; $request->requeststatus = 'pending'; $request->timemodified = $request->timecreated = time(); $id = $DB->insert_record('block_courserequest', $request); $fields = $DB->get_records('block_courserequest_fields'); $fields = $fields ? $fields : array(); foreach ($fields as $reqfield) { $field = new field($reqfield->fieldid); if (!$field->id || !isset($field->owners['manual'])) { // skip nonexistent fields, or fields without manual editing continue; } $fielddata = new stdClass(); $fielddata->requestid = $id; $fielddata->fieldid = $reqfield->fieldid; // key that represents the appropriate attribute on the object $field_key = "field_{$field->shortname}"; // make sure the field was enabled, especially for preventing the // storage of course fields when using an existing course if (isset($data->{$field_key})) { // check for multiple fields if (is_array($data->{$field_key})) { $fielddata->data = serialize($data->{$field_key}); $fielddata->multiple = '1'; } else { $fielddata->data = $data->{$field_key}; } // remember the context level that the field corresponds to $fielddata->contextlevel = $reqfield->contextlevel; $DB->insert_record('block_courserequest_data', $fielddata); } } require_once $CFG->dirroot . '/local/elisprogram/lib/notifications.php'; $syscontext = context_system::instance(); $config = get_config('block_courserequest'); if (has_capability('block/courserequest:approve', $syscontext)) { // Since we want to automatically approve requests for people approval permission, let's go ahead and create the course/class $requestid = $id; if (!($request = $DB->get_record('block_courserequest', array('id' => $requestid)))) { $target = str_replace($CFG->wwwroot, '', $this->url); print_error('errorinvalidrequestid', 'block_courserequest', $target, $requestid); } $target = $this->get_new_page(array('action' => 'approveconfirm')); $approveform = new pending_request_approve_form($target->url, $request); $request->request = $request->id; $approveform->set_data($request); // We're not actually approving the request, redirect back to the approval table. if ($approveform->is_cancelled()) { redirect($this->url, '', 0); } // Do we have to create a brand new course? if (empty($request->courseid)) { $crsdata = array('name' => $request->title, 'idnumber' => $data->crsidnumber, 'syllabus' => ''); $newcourse = new course($crsdata); if (!empty($config->use_course_fields)) { // course fields are enabled, so add the relevant data $this->add_custom_fields($request->id, 'course', $newcourse); } $newcourse->save(); // ->add() // do the course role assignment, if applicable if (!empty($config->course_role)) { if ($context = \local_elisprogram\context\course::instance($newcourse->id)) { // TBD: role_assign() now throws exceptions! $result = role_assign($config->course_role, $request->userid, $context->id, ECR_CD_ROLE_COMPONENT); } } $courseid = $newcourse->id; } else { $courseid = $request->courseid; // TBV: addslashes() } // Create the new class if we are using an existing course, or if // create_class_with_course is on. if (!empty($request->courseid) || !empty($config->create_class_with_course)) { require_once $CFG->dirroot . '/local/elisprogram/lib/data/pmclass.class.php'; $clsdata = array('name' => $request->title, 'courseid' => $courseid, 'idnumber' => $data->clsidnumber, 'starttimehour' => 25, 'starttimeminute' => 61, 'endtimehour' => 25, 'endtimeminute' => 61); $newclass = new pmclass($clsdata); $newclass->autocreate = false; if (!empty($config->use_class_fields)) { // class fields are enabled, so add the relevant data $this->add_custom_fields($request->id, 'class', $newclass); } $newclass->save(); // ->add() } // Update the request record to mark it as being approved. $request->requeststatus = 'approved'; $request->statusnote = ''; $DB->update_record('block_courserequest', $request); // TBV: addslashes_object() // assign role to requester in the newly created class if (!empty($newclass->id)) { if (!empty($config->class_role)) { $context = \local_elisprogram\context\pmclass::instance($newclass->id); // TBD: role_assign() now throws exceptions! role_assign($config->class_role, $request->userid, $context->id, ECR_CI_ROLE_COMPONENT); } } // create a new Moodle course from the course template if applicable if (!empty($data->usecoursetemplate) && !empty($newclass->id)) { moodle_attach_class($newclass->id, 0, '', true, true, true); // copy role over into Moodle course if (!empty($config->class_role)) { require_once $CFG->dirroot . '/local/elisprogram/lib/data/classmoodlecourse.class.php'; if ($class_moodle_record = $DB->get_record(classmoodlecourse::TABLE, array('classid' => $newclass->id))) { $context = context_course::instance($class_moodle_record->moodlecourseid); // TBD: role_assign() now throws exceptions! role_assign($config->class_role, $request->userid, $context->id, ECR_MC_ROLE_COMPONENT); } } } // Send a notification to the requesting user that their course / class is ready. // set additional course / class information for use in the message if (isset($newclass->idnumber) && isset($newclass->id)) { $request->classidnumber = $newclass->idnumber; $request->classid = $newclass->id; } $request->newcourseid = $courseid; // calculate the actual message $notice = block_courserequest_get_approval_message($request); // send it to the requester notification::notify($notice, $DB->get_record('user', array('id' => $request->userid))); print_string('request_submitted_and_auto_approved', 'block_courserequest'); } else { // find users with approve capabilities in the system context $admin = get_users_by_capability($syscontext, 'block/courserequest:approve'); foreach ($admin as $userto) { notification::notify(get_string('new_request_notification', 'block_courserequest', $data), $userto); } print_string('request_submitted', 'block_courserequest'); } redirect($this->url); return; } } $form->display(); }
/** * Test course capability check. */ public function test_coursecapabilitycheck() { global $DB, $USER; $role = $DB->get_record('role', array('shortname' => 'editingteacher')); // Assign the test user the editing teacher role on a test curriculum. $ctx = \local_elisprogram\context\course::instance($this->tcourseid); $this->assertNotEmpty(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 'course' level. $contextscourse = new pm_context_set(); $contextscourse->contexts = array('course' => array($this->tcourseid)); $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('course' => array($this->tcourseid)); $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('course', $this->tcourseid, 'local/elisprogram:userset_enrol_userset_user'); $this->assertEquals($this->mdluserid, current($users)->id); }
/** * Get context * * @return object The context */ protected function get_context() { if (!isset($this->context)) { $id = $this->required_param('id', PARAM_INT); $this->set_context(\local_elisprogram\context\course::instance($id)); } return $this->context; }
/** * Takes a record and transforms it into an appropriate format * This method is set up as a hook to be implented by actual report class * * @param stdClass $record The current report record * @param string $export_format The format being used to render the report * * @return stdClass The reformatted record */ function transform_record($record, $export_format) { $record->associatedcluster = empty($record->associatedcluster) ? get_string('no') : get_string('yes'); //make sure this is set to something so that the horizontal bar graph doesn't disappear if (empty($record->stucompletedprogress)) { $record->stucompletedprogress = 0; } if (isset($record->studentspassing) && isset($record->numstudents) && $record->numstudents > 0) { $record->studentspassing = round($record->studentspassing / $record->numstudents * 100) . '%'; } //TBD: following block doesn't appear to do anything!?!? $a = new stdClass(); if (isset($record->stucompletedprogress)) { $a->value = $record->stucompletedprogress; $a->total = $record->numprogress; } else { $a->value = 0; $a->total = 0; } // Check for unset fields for N/A display of progress or students passing if (!isset($record->studentspassing)) { $record->studentspassing = get_string('na', 'rlreport_course_progress_summary'); } // We must manually get multi-valued custom field data // Since report SQL requires COUNTs & SUMs $filter_params = php_report_filtering_get_active_filter_values($this->get_report_shortname(), 'field' . $this->get_report_shortname(), $this->filter); $filter_params = $filter_params[0]['value']; $filter_params = $filter_params ? explode(',', $filter_params) : array(); if (isset($filter_params) && is_array($filter_params)) { foreach ($filter_params as $custom_course_id) { $custom_course_field = new field($custom_course_id); if ($custom_course_field->multivalued) { $context = \local_elisprogram\context\course::instance($record->courseid); $field_data = "custom_data_{$custom_course_id}"; $record->{$field_data} = ''; $datarecs = field_data::get_for_context_and_field($context, $custom_course_field, false); foreach ($datarecs as $rec) { if (!empty($record->{$field_data})) { $record->{$field_data} .= $this->get_multivalued_separator($export_format); } $record->{$field_data} .= $rec->data; } if (empty($record->{$field_data})) { unset($record->{$field_data}); } } } } // Default values for custom fields IF not set foreach ($this->field_default as $key => $value) { // error_log("CPSR:transform_record(), checking default for {$key} => {$value}"); if (!isset($record->{$key})) { $record->{$key} = $this->format_default_data($value); } } return $record; }
/** * Hook that gets called after a CM entity is added through this page * * @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_course_role_id) && $DB->record_exists('role', array('id' => elis::$config->local_elisprogram->default_course_role_id))) { //get the context instance for capability checking $context_instance = \local_elisprogram\context\course::instance($cm_entity->id); //assign the appropriate role if the user does not have the edit capability if (!has_capability('local/elisprogram:course_edit', $context_instance)) { role_assign(elis::$config->local_elisprogram->default_course_role_id, $USER->id, $context_instance->id); } } }
public function delete() { //delete associated classes $filter = new field_filter('courseid', $this->id); pmclass::delete_records($filter, $this->_db); //clean up associated records curriculumcourse::delete_records($filter, $this->_db); coursetemplate::delete_records($filter, $this->_db); // ELIS-8528: Remove LOs/completion elements. coursecompletion::delete_records($filter, $this->_db); parent::delete(); $context = \local_elisprogram\context\course::instance($this->id); $context->delete(); }
/** * Returns ELIS class context instance. * * @static * @param int $instanceid The ELIS class 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\pmclass|bool 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)) { $class = $DB->get_record(\pmclass::TABLE, array('id' => $instanceid), 'id,idnumber,courseid', $strictness); if (!empty($class)) { $parentcontext = \local_elisprogram\context\course::instance($class->courseid); $record = \local_eliscore\context\base::insert_context_record($contextlevel, $class->id, $parentcontext->path); } } if (!empty($record)) { $context = new \local_elisprogram\context\pmclass($record); \local_eliscore\context\base::cache_add($context); return $context; } return false; }
/** * Performs course 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 course_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::course_delete_parameters(), array('data' => $data)); // Context validation. $context = context_user::instance($USER->id); self::validate_context($context); $data = (object) $data; // Validate course exists. if (!($crsid = $DB->get_field(course::TABLE, 'id', array('idnumber' => $data->idnumber)))) { throw new data_object_exception('ws_course_delete_fail_invalid_idnumber', 'local_datahub', '', $data); } // Capability checking. require_capability('local/elisprogram:course_delete', \local_elisprogram\context\course::instance($crsid)); $course = new course($crsid); $course->delete(); // Confirm delete & respond. if (!$DB->record_exists(course::TABLE, array('id' => $crsid))) { return array('messagecode' => get_string('ws_course_delete_success_code', 'local_datahub'), 'message' => get_string('ws_course_delete_success_msg', 'local_datahub')); } else { throw new data_object_exception('ws_course_delete_fail', 'local_datahub'); } }
/** * Performs course-specific actions as a part of the back-end approval process * * @param object $request the request record * @param object $formdata the submitted form data * @uses $CFG * @return int the id of the course we are currently working with */ function approve_request_course_actions(&$request, $formdata) { global $CFG; if (empty($request->courseid)) { require_once $CFG->dirroot . '/local/elisprogram/lib/data/course.class.php'; // create a new course $crsdata = array('name' => $request->title, 'idnumber' => $formdata->crsidnumber, 'syllabus' => ''); $newcourse = new course($crsdata); // make sure custom field data is included $usecoursefields = get_config('block_courserequest', 'use_course_fields'); if (!empty($usecoursefields)) { // course fields are enabled, so add the relevant data $this->add_custom_fields($formdata, 'course', $newcourse); } $newcourse->save(); // ->add() // do the course role assignment, if applicable $courserole = get_config('block_courserequest', 'course_role'); if (!empty($courserole)) { if ($context = \local_elisprogram\context\course::instance($newcourse->id)) { role_assign($courserole, $request->userid, $context->id, ECR_CD_ROLE_COMPONENT); } } return $newcourse->id; } else { return $request->courseid; } }
/** * Test the get_child_contexts() function of \local_elisprogram\context\course */ public function test_course_child_contexts() { $this->fixture_course_class(); $ctx = \local_elisprogram\context\course::instance(100); $children = $ctx->get_child_contexts(); $count = 0; foreach ($children as $child) { $this->assertTrue($child instanceof \local_elisprogram\context\pmclass); $this->assertEquals(100, $child->instanceid); $count++; break; } $this->assertEquals(1, $count); }
/** * ELIS-4747: Test for assigning a user a role on a course context */ public function test_assignuserforcoursectx() { 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_COURSE)); foreach ($rolesctx as $rolectx) { $roleid = $rolectx->roleid; break; } // Get user to assign role. $user = new user(103); $muser = $user->get_moodleuser(); // Get specific context. $crs = new course(100); $context = \local_elisprogram\context\course::instance($crs->id); // Assign role. $this->assertGreaterThan(0, role_assign($roleid, $muser->id, $context->id)); }
/** * Validate that mappings are applied during the course description update action */ public function test_mapping_applied_during_course_update() { global $CFG, $DB; require_once $CFG->dirroot . '/course/lib.php'; 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'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/curriculumcourse.class.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/course.class.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/coursetemplate.class.php'; $this->init_mapping(); $customfieldid = $this->create_custom_field(CONTEXT_ELIS_COURSE); set_config('defaultblocks_override', ' '); $program = new curriculum(array('idnumber' => 'testprogramidnumber')); $program->save(); $category = new stdClass(); $category->name = 'testcategoryname'; $category->id = $DB->insert_record('course_categories', $category); $course = new stdClass(); $course->category = $category->id; $course->shortname = 'testcourseshortname'; $course->fullname = 'testcoursefullname'; $course = create_course($course); $pmcourse = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => 'testcoursesyllabus')); $pmcourse->save(); // Run the course update action. $record = new stdClass(); $record->customaction = 'update'; $record->customcontext = 'course'; $record->customname = 'updatedtestcoursename'; $record->customcode = 'updatedtestcoursecode'; $record->customidnumber = 'testcourseidnumber'; $record->customsyllabus = 'updatedtestcoursesyllabus'; $record->customlengthdescription = 'updatedtestcourselengthdescription'; $record->customlength = '2'; $record->customcredits = '2'; $record->customcompletion_grade = '100'; $record->customcost = '10'; $record->customversion = '2'; $record->customassignment = 'testprogramidnumber'; $record->customlink = 'testcourseshortname'; $record->customtestfieldshortname = '1'; $this->run_pmentity_import((array) $record); // Validation. $data = array('name' => 'updatedtestcoursename', 'code' => 'updatedtestcoursecode', 'idnumber' => 'testcourseidnumber', 'lengthdescription' => 'updatedtestcourselengthdescription', 'length' => 2, 'credits' => 2, 'completion_grade' => 100, 'cost' => '10', 'version' => '2'); $this->assertTrue($DB->record_exists(course::TABLE, $data)); $record = $DB->get_record(course::TABLE, array('idnumber' => 'testcourseidnumber')); $this->assertEquals('updatedtestcoursesyllabus', $record->syllabus); $this->assertTrue($DB->record_exists(curriculumcourse::TABLE, array('curriculumid' => $program->id, 'courseid' => $record->id))); $this->assertTrue($DB->record_exists(coursetemplate::TABLE, array('courseid' => $record->id, 'location' => $course->id))); $instance = \local_elisprogram\context\course::instance(1); $this->assertTrue($DB->record_exists(field_data_int::TABLE, array('fieldid' => $customfieldid, 'contextid' => $instance->id, 'data' => 1))); }
/** * 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); }
/** * Test that the pm_migrate_environments() function works correctly */ public function test_pmmigrateenvironments() { global $DB; $this->load_csv_data(); // Migrate the legacy environment data to new ELIS filelds. pm_migrate_environments(); // Initialize the course object. $course = new course(100); $course->reset_custom_field_list(); $course->load(); $course = $course->to_object(); // Get the field data from the object. $this->assertObjectHasAttribute('field__19upgrade_course_environment', $course); $this->assertEquals('Testing', $course->field__19upgrade_course_environment); // Let's do some extra DB-level validation (though it's probably not necessary). $field = $DB->get_record(field::TABLE, array('shortname' => '_19upgrade_course_environment')); $this->assertGreaterThan(0, $field->id); $context = \local_elisprogram\context\course::instance($course->id); $this->assertTrue($DB->record_exists(field_data_char::TABLE, array('contextid' => $context->id, 'fieldid' => $field->id))); }