/** * Test enrol function */ public function test_enrol() { global $DB; // Fixture. $elisgen = new elis_program_datagenerator($DB); $pgm = $elisgen->create_program(); $track = $elisgen->create_track(array('curid' => $pgm->id)); $course = $elisgen->create_course(); $pmclass = $elisgen->create_pmclass(array('courseid' => $course->id)); $user = $elisgen->create_user(); $elisgen->assign_class_to_track($pmclass->id, $course->id, $track->id, true); $result = usertrack::enrol($user->id, $track->id); $this->assertTrue($result); // Validate curriculumstudent rec. $rec = $DB->get_record(curriculumstudent::TABLE, array('curriculumid' => $pgm->id, 'userid' => $user->id)); $this->assertNotEmpty($rec); // Validate student rec. $rec = $DB->get_record(student::TABLE, array('classid' => $pmclass->id, 'userid' => $user->id)); $this->assertNotEmpty($rec); }
/** * Enrols a user in a track. * * @param int $userid The user id * @param int $trackid The track id */ public static function enrol($userid, $trackid) { global $DB; // make sure we don't double-enrol if ($DB->record_exists(self::TABLE, array('userid' => $userid, 'trackid' => $trackid))) { return false; } $record = new usertrack(); $record->userid = $userid; $record->trackid = $trackid; $record->save(); $user = new user($userid); $track = new track($trackid); if (!$DB->record_exists(curriculumstudent::TABLE, array('userid' => $userid, 'curriculumid' => $track->curid))) { $curstu = new curriculumstudent(); $curstu->userid = $userid; $curstu->curriculumid = $track->curid; $curstu->completed = 0; $curstu->credits = 0; $curstu->locked = 0; $curstu->save(); } events_trigger('track_assigned', $record); /** * Get autoenrollable classes in the track. Classes are autoenrollable * if: * - the autoenrol flag is set * - it is the only class in that course slot for the track */ $sql = 'SELECT classid, courseid ' . 'FROM {' . trackassignment::TABLE . '} ' . 'WHERE trackid = ? ' . 'GROUP BY courseid ' . 'HAVING COUNT(*) = 1 AND MAX(autoenrol) = 1'; $params = array($trackid); $classes = $DB->get_recordset_sql($sql, $params); foreach ($classes as $class) { // ELIS-3460: check pre-requisites ... $curcrs = new curriculumcourse(array('courseid' => $class->courseid, 'curriculumid' => $track->curid)); if (!$curcrs->prerequisites_satisfied($userid)) { //error_log("/local/elisprogram/lib/data/usertrack.class.php::enrol({$userid}); pre-requisites NOT satisfied for course: {$class->courseid}, curriculum: {$track->curid}"); continue; } $now = time(); // enrol user in each autoenrolable class $stu_record = new object(); $stu_record->userid = $userid; $stu_record->classid = $class->classid; $stu_record->enrolmenttime = $now; $enrolment = new student($stu_record); // catch enrolment limits try { $status = $enrolment->save(); } catch (pmclass_enrolment_limit_validation_exception $e) { // autoenrol into waitlist $wait_record = new object(); $wait_record->userid = $userid; $wait_record->classid = $class->classid; $wait_record->enrolmenttime = $now; $wait_record->timecreated = $now; $wait_record->position = 0; $wait_list = new waitlist($wait_record); $wait_list->save(); $status = true; } catch (Exception $e) { $param = array('message' => $e->getMessage()); echo cm_error(get_string('record_not_created_reason', 'local_elisprogram', $param)); } } unset($classes); return true; }
static function cluster_assigned_handler($eventdata) { global $CURMAN, $CFG; // assign user to the curricula associated with the cluster /** * @todo we may need to change this if associating a user with a * curriculum does anything more complicated */ require_once CURMAN_DIRLOCATION . '/lib/user.class.php'; require_once CURMAN_DIRLOCATION . '/lib/clustercurriculum.class.php'; require_once CURMAN_DIRLOCATION . '/lib/curriculumstudent.class.php'; require_once CURMAN_DIRLOCATION . '/lib/usertrack.class.php'; $db = $CURMAN->db; $timenow = time(); $sql = 'INSERT INTO ' . $db->prefix_table(CURASSTABLE) . ' ' . '(userid, curriculumid, timecreated, timemodified) ' . 'SELECT DISTINCT u.id, clucur.curriculumid, ' . $timenow . ', ' . $timenow . ' ' . 'FROM ' . $db->prefix_table(CLSTUSERTABLE) . ' clu ' . 'INNER JOIN ' . $db->prefix_table(USRTABLE) . ' u ON u.id = clu.userid ' . 'INNER JOIN ' . $db->prefix_table(CLSTCURTABLE) . ' clucur ON clucur.clusterid = clu.clusterid ' . 'LEFT OUTER JOIN ' . $db->prefix_table(CURASSTABLE) . ' ca ON ca.userid = u.id AND ca.curriculumid = clucur.curriculumid ' . 'WHERE clu.clusterid = \'' . $eventdata->clusterid . '\' AND u.id = \'' . $eventdata->userid . '\' AND ca.curriculumid IS NULL ' . 'AND clucur.autoenrol = 1'; $db->execute_sql($sql, false); // enrol user in associated tracks if autoenrol flag is set if ($eventdata->autoenrol) { $tracks = clustertrack::get_tracks($eventdata->clusterid); if ($tracks) { foreach ($tracks as $track) { //make sure the cluster-track association is set up for autoenrol if (record_exists(CLSTTRKTABLE, 'clusterid', $eventdata->clusterid, 'trackid', $track->trackid, 'autoenrol', 1)) { usertrack::enrol($eventdata->userid, $track->trackid); } } } } return true; }
/** * Enrol the test user in the provided context * * @param string $contextlevel The string descriptor of the context level * @param string $role The shortname of the import record's role column */ private function create_enrolment($contextlevel, $role) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; switch ($contextlevel) { case 'curriculum': // Program enrolment. require_once elispm::lib('data/curriculumstudent.class.php'); $data = array('curriculumid' => 1, 'userid' => 1); $curriculumstudent = new curriculumstudent($data); $curriculumstudent->save(); break; case 'track': // Track enrolment. require_once elispm::lib('data/usertrack.class.php'); $data = array('trackid' => 1, 'userid' => 1); $usertrack = new usertrack($data); $usertrack->save(); break; case 'cluster': // User set enrolment. require_once elispm::lib('data/clusterassignment.class.php'); $data = array('clusterid' => 1, 'userid' => 1); $clusterassignment = new clusterassignment($data); $clusterassignment->save(); break; case 'class': if ($role == 'instructor') { // Class instance instructor enrolment. require_once elispm::lib('data/instructor.class.php'); $data = array('classid' => 1, 'userid' => 1); $instructor = new instructor($data); $instructor->save(); } else { // Class instance student enrolment. require_once elispm::lib('data/student.class.php'); $data = array('classid' => 1, 'userid' => 1); $student = new student($data); $student->save(); } break; case 'user': // Moodle user role assignment. $roleid = $DB->get_field('role', 'id', array('shortname' => $role)); $userid = $DB->get_field('user', 'id', array('idnumber' => 'testuseridnumber')); $context = context_user::instance($userid); role_assign($roleid, $userid, $context->id); break; default: break; } }
/** * Process all the students in this class * * Class properties required: * id - id of class * criteriatype - what mark to look at, 0 for final mark, anything else is an element id * engineid - id of results engine entry * scheduleddate - date when it was supposed to run * rundate - date when it is being run * * Class properties required by sub-functions: * eventtriggertype - what type of trigger the engine uses * lockedgrade - whether the grade must be locked if "set grade" trigger is used * * @param $class object The class object see above for required attributes * @return boolean Success/failure * @uses $CFG */ function results_engine_process($class) { global $CFG, $DB; $params = array('classid' => $class->id); $students = results_engine_get_students($class); if (sizeof($students) == 0) { return true; } $params = array('resultsid' => $class->engineid); $fields = 'id, actiontype, minimum, maximum, trackid, classid, fieldid, fielddata'; $actions = $DB->get_records('local_elisprogram_res_action', $params, '', $fields); $fieldids = array(); $classids = array(); $trackids = array(); foreach ($actions as $action) { if ($action->actiontype == RESULTS_ENGINE_UPDATE_PROFILE) { $fieldids[$action->fieldid] = $action->fieldid; } else { if ($action->actiontype == RESULTS_ENGINE_ASSIGN_CLASS) { $classids[$action->classid] = $action->classid; } else { if ($action->actiontype == RESULTS_ENGINE_ASSIGN_TRACK) { $trackids[$action->trackid] = $action->trackid; } } } } foreach ($fieldids as $id) { if ($record = $DB->get_record('local_eliscore_field', array('id' => $id))) { $userfields[$id] = new field($record, null, array(), true); } } $classes = $DB->get_records_list('local_elisprogram_cls', 'id', $classids); $tracks = $DB->get_records_list('local_elisprogram_trk', 'id', $trackids); // Log that the class has been processed $log = new stdClass(); $log->classid = $class->id; $log->datescheduled = $class->scheduleddate; $log->daterun = $class->rundate; $classlogid = $DB->insert_record('local_elisprogram_res_clslog', $log); $log = new stdClass(); $log->classlogid = $classlogid; $log->daterun = $class->rundate; // Find the correct action to take based on student marks foreach ($students as $student) { $do = null; foreach ($actions as $action) { if (elis_float_comp($student->grade, $action->minimum, '>=') && elis_float_comp($student->grade, $action->maximum, '<=')) { $do = $action; break; } } if ($do != null) { $obj = new object(); switch ($do->actiontype) { case RESULTS_ENGINE_ASSIGN_TRACK: usertrack::enrol($student->userid, $do->trackid); $message = 'results_action_assign_track'; $track = $tracks[$do->trackid]; $obj->name = $track->name . ' (' . $track->idnumber . ')'; break; case RESULTS_ENGINE_ASSIGN_CLASS: $enrol = new student(); $enrol->classid = $do->classid; $enrol->userid = $student->userid; $enrol->save(); $message = 'results_action_assign_class'; $obj->name = $classes[$do->classid]->idnumber; break; case RESULTS_ENGINE_UPDATE_PROFILE: if (!array_key_exists($do->fieldid, $userfields)) { print get_string('results_field_not_found', RESULTS_ENGINE_LANG_FILE, $do) . "\n"; break; } /* $context = \local_elisprogram\context\user::instance($student->userid); field_data::set_for_context_and_field($context, $userfields[$do->fieldid], $do->fielddata); */ //set field $filter = new select_filter('id = :userid', array('userid' => $student->userid)); if (user::exists($filter)) { //get user $user = user::find($filter); $user = $user->current(); //set field $field = 'field_' . $userfields[$do->fieldid]->shortname; $user->{$field} = $do->fielddata; $user->save(); } $message = 'results_action_update_profile'; $obj->name = $userfields[$do->fieldid]->shortname; $obj->value = $do->fielddata; break; default: // If we don't know what we're doing, do nothing. break; } $obj->id = $do->id; $log->action = get_string($message, RESULTS_ENGINE_LANG_FILE, $obj); $log->userid = $student->userid; $DB->insert_record('local_elisprogram_res_stulog', $log, false); } } if (isset($class->cron) && $class->cron) { print get_string('results_class_processed', RESULTS_ENGINE_LANG_FILE, $class) . "\n"; } return true; }
/** * Validate that mappings are applied during the track enrolment delete action */ public function test_mapping_applied_during_track_enrolment_delete() { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/data/curriculum.class.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/track.class.php'; $this->init_mapping(); $userid = $this->create_test_user(); $program = new curriculum(array('idnumber' => 'testprogramidnumber')); $program->save(); $track = new track(array('curid' => $program->id, 'idnumber' => 'testtrackidnumber')); $track->save(); $usertrack = new usertrack(array('trackid' => $track->id, 'userid' => $userid)); $usertrack->save(); // Run the track enrolment delete action. $record = new stdClass(); $record->customaction = 'delete'; $record->customcontext = 'track_testtrackidnumber'; $record->customuser_username = '******'; $record->customuser_email = '*****@*****.**'; $record->customuser_idnumber = 'testuseridnumber'; $this->run_enrolment_import((array) $record); // Validation. $this->assertEquals(0, $DB->count_records(usertrack::TABLE)); }
/** * Validate that the listing respects the local/elisprogram:track_enrol_userset_user * capability as long as the appropriate userset and track are associated to * one another and the target user is in the userset */ public function test_availableusersrespectsindirectusersetpermissions() { global $DB, $USER; $this->load_csv_data(); set_config('siteguest', ''); set_config('siteadmins', ''); accesslib_clear_all_caches_for_unit_testing(); // Create a test user to be in the userset. $usersetmember = new user(array('idnumber' => 'usersetmember', 'username' => 'usersetmember', 'firstname' => 'usersetmember', 'lastname' => 'usersetmember', 'email' => '*****@*****.**', 'country' => 'CA')); $usersetmember->save(); // Our test userset. $userset = new userset(array('name' => 'userset')); $userset->save(); // Assign the test user to the test userset. $clusterassignment = new clusterassignment(array('userid' => $usersetmember->id, 'clusterid' => $userset->id)); $clusterassignment->save(); // Assign the userset to our track. $clustertrack = new clustertrack(array('clusterid' => $userset->id, 'trackid' => 1)); $clustertrack->save(); // Set up a db record for the active user for permissions reasons. // (i.e. so they are not treated as an admin). $activeuser = new user(array('idnumber' => 'activeuser', 'username' => 'activeuser', 'firstname' => 'activeuser', 'lastname' => 'activeuser', 'email' => '*****@*****.**', 'country' => 'CA')); $activeuser->save(); // Set up our test role. $roleid = create_role('testrole', 'testrole', 'testrole'); $syscontext = context_system::instance(); assign_capability('local/elisprogram:track_enrol_userset_user', CAP_ALLOW, $roleid, $syscontext->id); // Perform the role necessary assignment. $moodleuser = $DB->get_record('user', array('username' => 'activeuser')); // Make sure all the contexts are created, so that we can find the children. $contextclass = \local_eliscore\context\helper::get_class_for_level(CONTEXT_ELIS_USERSET); $instance = $contextclass::instance($userset->id); role_assign($roleid, $moodleuser->id, $instance->id); // Assume the role of the user with the role assignment. $USER = $moodleuser; $usersrecset = usertrack::get_available_users(1); $users = array(); foreach ($usersrecset as $key => $user) { $users[$key] = $user; } unset($usersrecset); $this->assertEquals(1, count($users)); // Validate user. $this->assertArrayHasKey($usersetmember->id, $users); $user = $users[$usersetmember->id]; $this->assertEquals($usersetmember->username, 'usersetmember'); // Validate count. $count = usertrack::count_available_users(1); $this->assertEquals(1, $count); }
/** * Assign a class to a track, this function also creates * and assigns the class to the curriculum default track * * @return TODO: add meaningful return value */ function save() { //add() if (empty($this->courseid)) { $this->courseid = $this->_db->get_field(pmclass::TABLE, 'courseid', array('id' => $this->classid)); } if ((empty($this->trackid) or empty($this->classid) or empty($this->courseid)) and empty(elis::$config->local_elisprogram->userdefinedtrack)) { cm_error('trackid and classid have not been properly initialized'); return false; } else { if ((empty($this->courseid) or empty($this->classid)) and elis::$config->local_elisprogram->userdefinedtrack) { cm_error('courseid has not been properly initialized'); } } if (!isset($this->id) && $this->is_class_assigned_to_track()) { //trying to re-add an existing association return; } // Determine whether class is required $curcrsobj = new curriculumcourse(array('curriculumid' => $this->track->curid, 'courseid' => $this->courseid)); // TBV: was $this->classid // insert assignment record parent::save(); //updated for ELIS2 from $this->data_insert_record() if ($this->autoenrol && $this->is_autoenrollable()) { // autoenrol all users in the track // ELIS-7582 @set_time_limit(0); $users = usertrack::get_users($this->trackid); foreach ($users as $user) { // ELIS-3460: Must check pre-requisites ... if (!$curcrsobj->prerequisites_satisfied($user->userid)) { //error_log("/local/elisprogram/lib/data/track.class.php:trackassignment::save(); pre-requisites NOT satisfied for course: {$this->courseid}, curriculum: {$this->track->curid}"); continue; } $now = time(); $stu_record = new object(); $stu_record->userid = $user->userid; $stu_record->user_idnumber = $user->idnumber; $stu_record->classid = $this->classid; $stu_record->enrolmenttime = $now; $enrolment = new student($stu_record); // check enrolment limits try { $enrolment->save(); } catch (pmclass_enrolment_limit_validation_exception $e) { // autoenrol into waitlist $wait_record = new object(); $wait_record->userid = $user->userid; $wait_record->classid = $this->classid; $wait_record->enrolmenttime = $now; $wait_record->timecreated = $now; $wait_record->position = 0; $wait_list = new waitlist($wait_record); $wait_list->save(); } catch (Exception $e) { $param = array('message' => $e->getMessage()); echo cm_error(get_string('record_not_created_reason', 'local_elisprogram', $param)); } } } events_trigger('pm_track_class_associated', $this); }
/** * Validate that users can be unenrolled from tracks * * @param string $username A sample user's username, or null if not used in the import * @param string $email A sample user's email, or null if not used in the import * @param string $idnumber A sample user's idnumber, or null if not used in the import * @dataProvider user_identifier_provider */ public function test_elis_user_track_unenrolment_import($actioncreate, $actiondelete, $username, $email, $idnumber) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/curriculum.class.php'); require_once elispm::lib('data/track.class.php'); require_once elispm::lib('data/user.class.php'); require_once elispm::lib('data/usertrack.class.php'); $user = new user(array('idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'country' => 'CA')); $user->save(); $program = new curriculum(array('idnumber' => 'testprogramidnumber')); $program->save(); $track = new track(array('curid' => $program->id, 'idnumber' => 'testtrackidnumber')); $track->save(); $usertrack = new usertrack(array('userid' => $user->id, 'trackid' => $track->id)); $usertrack->save(); // Validate setup. $this->assertTrue($DB->record_exists(usertrack::TABLE, array('userid' => $user->id, 'trackid' => $track->id))); // Run the track enrolment delete action. $record = new stdClass(); $record->action = $actiondelete; $record->context = 'track_testtrackidnumber'; if ($username != null) { $record->user_username = $user->username; } if ($email != null) { $record->user_email = $user->email; } if ($idnumber != null) { $record->user_idnumber = $user->idnumber; } $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->process_record('enrolment', (object) $record, 'bogus'); // Validation. $this->assertEquals(0, $DB->count_records(usertrack::TABLE)); }
$context = get_context_instance(context_level_base::get_custom_context_level('track', 'block_curr_admin'), $trackid); //todo: integrate this better with user-track page? //this checks permissions at the track level if (!trackpage::can_enrol_into_track($trackid)) { //standard failure message require_capability('block/curr_admin:track:enrol', $context); } // add user to track if ($userid) { //todo: integrate this better with user-track page? //this checks permissions at the user-track association level if (!usertrack::can_manage_assoc($userid, $trackid)) { //standard failure message require_capability('block/curr_admin:track:enrol', $context); } usertrack::enrol($userid, $trackid); // reload the main page with the new assignments $target = new trackuserpage(array('id' => $trackid)); ?> <script type="text/javascript"> //<![CDATA[ window.opener.location = "<?php echo htmlspecialchars_decode($target->get_url()); ?> "; //]]> </script> <?php } // find all users not enrolled in the track $FULLNAME = sql_concat('usr.firstname', "' '", 'usr.lastname');
/** * Delete a track enrolment * * @param object $record One record of import data * @param string $filename The import file name, used for logging * @param string $idnumber The idnumber of the track * * @return boolean true on success, otherwise false */ function track_enrolment_delete($record, $filename, $idnumber) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/track.class.php'); require_once elispm::lib('data/usertrack.class.php'); if (!($trackid = $DB->get_field(track::TABLE, 'id', array('idnumber' => $idnumber)))) { $this->fslogger->log_failure("instance value of \"{$idnumber}\" does not refer to a valid instance of a track context.", 0, $filename, $this->linenumber, $record, "enrolment"); return false; } $userid = $this->get_userid_from_record($record, $filename); //string to describe the user $user_descriptor = $this->get_user_descriptor($record, false, 'user_'); if (!$DB->record_exists(usertrack::TABLE, array('trackid' => $trackid, 'userid' => $userid))) { $this->fslogger->log_failure("User with {$user_descriptor} is not enrolled in track \"{$idnumber}\".", 0, $filename, $this->linenumber, $record, "enrolment"); return false; } if (!$this->validate_track_enrolment_data('delete', $record, $filename)) { return false; } //obtain the track id $trackid = $DB->get_field(track::TABLE, 'id', array('idnumber' => $idnumber)); //delete the association $usertrackid = $DB->get_field(usertrack::TABLE, 'id', array('userid' => $userid, 'trackid' => $trackid)); $usertrack = new usertrack($usertrackid); $usertrack->delete(); //log success $success_message = "User with {$user_descriptor} successfully unenrolled from track \"{$idnumber}\"."; $this->fslogger->log_success($success_message, 0, $filename, $this->linenumber); return true; }
function __construct(&$items, $columns, $page, $decorators = array()) { $id = required_param('id', PARAM_INT); $users = usertrack::get_users($id); $this->numusers = empty($users) ? 0 : count($users); parent::__construct($items, $columns, $page, $decorators); }
/** * Updates the autoenrol flag for a particular cluster-track association * * @param int $association_id The id of the appropriate association record * @param int $autoenrol The new autoenrol value * * @return object The updated record */ public static function update_autoenrol($association_id, $autoenrol) { global $DB; $old_autoenrol = $DB->get_field(self::TABLE, 'autoenrol', array('id' => $association_id)); // update the flag on the association record $update_record = new stdClass(); $update_record->id = $association_id; $update_record->autoenrol = $autoenrol; $result = $DB->update_record(self::TABLE, $update_record); if (!empty($autoenrol) && empty($old_autoenrol) && ($cluster = $DB->get_field(self::TABLE, 'clusterid', array('id' => $association_id))) && ($track = $DB->get_field(self::TABLE, 'trackid', array('id' => $association_id)))) { // ELIS-7582 @set_time_limit(0); // Enrol all users in the cluster into track. $sql = 'SELECT uc.* FROM {' . clusterassignment::TABLE . '} as uc JOIN {' . user::TABLE . '} as u ON uc.userid = u.id WHERE uc.clusterid = ? ORDER BY u.lastname'; $params = array($cluster); $users = $DB->get_recordset_sql($sql, $params); foreach ($users as $user) { usertrack::enrol($user->userid, $track); } unset($users); } return $result; }
static function cluster_assigned_handler($eventdata) { require_once elispm::lib('data/clusterassignment.class.php'); require_once elispm::lib('data/clustercurriculum.class.php'); require_once elispm::lib('data/curriculumstudent.class.php'); require_once elispm::lib('data/clustertrack.class.php'); require_once elispm::lib('data/usertrack.class.php'); $assignment = new clusterassignment($eventdata); $userset = $assignment->cluster; // assign user to the curricula associated with the cluster /** * @todo we may need to change this if associating a user with a * curriculum does anything more complicated */ // enrol user in associated curricula $prog_assocs = $userset->clustercurriculum; foreach ($prog_assocs as $prog_assoc) { if ($prog_assoc->autoenrol && !curriculumstudent::exists(array(new field_filter('userid', $eventdata->userid), new field_filter('curriculumid', $prog_assoc->curriculumid)))) { $progass = new curriculumstudent(); $progass->userid = $eventdata->userid; $progass->curriculumid = $prog_assoc->curriculumid; $progass->timecreated = $progass->timemodified = time(); $progass->save(); } } // enrol user in associated tracks if autoenrol flag is set on the cluster-track associations $track_assocs = $userset->clustertrack; foreach ($track_assocs as $track_assoc) { if ($track_assoc->autoenrol && !usertrack::exists(array(new field_filter('userid', $eventdata->userid), new field_filter('trackid', $track_assoc->trackid)))) { usertrack::enrol($eventdata->userid, $track_assoc->trackid); } } return true; }
public function delete() { global $CFG; $muser = $this->get_moodleuser(); if (empty($muser) || !is_primary_admin($muser->id)) { // delete associated data require_once elis::lib('data/data_filter.class.php'); $filter = new field_filter('userid', $this->id); curriculumstudent::delete_records($filter, $this->_db); student::delete_records($filter, $this->_db); student_grade::delete_records($filter, $this->_db); waitlist::delete_records($filter, $this->_db); instructor::delete_records($filter, $this->_db); usertrack::delete_records($filter, $this->_db); clusterassignment::delete_records($filter, $this->_db); //delete association to Moodle user, if applicable require_once elispm::lib('data/usermoodle.class.php'); $filter = new field_filter('cuserid', $this->id); usermoodle::delete_records($filter, $this->_db); // Delete Moodle user. if (!empty($muser)) { delete_user($muser); } parent::delete(); $context = \local_elisprogram\context\user::instance($this->id); $context->delete(); } }
/** * Performs track_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 track_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::track_enrolment_delete_parameters(), array('data' => $data)); // Context validation. $context = context_user::instance($USER->id); self::validate_context($context); $data = (object) $data; // Parse track. if (empty($data->track_idnumber) || !($trackid = $DB->get_field(track::TABLE, 'id', array('idnumber' => $data->track_idnumber)))) { throw new data_object_exception('ws_track_enrolment_delete_fail_invalid_track', 'local_datahub', '', $data); } // Capability checking. require_capability('local/elisprogram:track_enrol', \local_elisprogram\context\track::instance($trackid)); // 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_track_enrolment_delete_fail_invalid_user', 'local_datahub', '', $a); } $retval = $DB->get_record(usertrack::TABLE, array('userid' => $userid, 'trackid' => $trackid)); // Respond. if (!empty($retval->id)) { $usertrack = new usertrack($retval->id); $usertrack->unenrol(); return array('messagecode' => get_string('ws_track_enrolment_delete_success_code', 'local_datahub'), 'message' => get_string('ws_track_enrolment_delete_success_msg', 'local_datahub')); } else { throw new data_object_exception('ws_track_enrolment_delete_fail', 'local_datahub'); } }
public function delete() { global $CFG; $result = false; $muser = cm_get_moodleuserid($this->id); if (empty($muser) || !is_primary_admin($muser)) { $level = context_level_base::get_custom_context_level('user', 'block_curr_admin'); $result = attendance::delete_for_user($this->id); $result = $result && curriculumstudent::delete_for_user($this->id); $result = $result && instructor::delete_for_user($this->id); $result = $result && student::delete_for_user($this->id); $result = $result && student_grade::delete_for_user($this->id); $result = $result && usertrack::delete_for_user($this->id); $result = $result && usercluster::delete_for_user($this->id); $result = $result && clusterassignment::delete_for_user($this->id); $result = $result && waitlist::delete_for_user($this->id); $result = $result && delete_context($level, $this->id); // Delete Moodle user. if ($muser = get_record('user', 'idnumber', $this->idnumber, 'mnethostid', $CFG->mnet_localhost_id, 'deleted', 0)) { $result = $result && delete_user($muser); } $result = $result && parent::delete(); } return $result; }
/** * Assign a class to a track, this function also creates * and assigns the class to the curriculum default track * * @return TODO: add meaningful return value */ function add() { global $CURMAN; if (empty($this->courseid)) { $this->courseid = $CURMAN->db->get_field(CLSTABLE, 'courseid', 'id', $this->classid); } if ((empty($this->trackid) or empty($this->classid) or empty($this->courseid)) and empty($CURMAN->config->userdefinedtrack)) { cm_error('trackid and classid have not been properly initialized'); return false; } elseif ((empty($this->courseid) or empty($this->classid)) and $CURMAN->config->userdefinedtrack) { cm_error('courseid has not been properly initialized'); } if (empty($CURMAN->config->userdefinedtrack)) { if ($this->is_class_assigned_to_track()) { return false; } // Determine whether class is required $curcrsobj = new curriculumcourse(array('curriculumid' => $this->track->curid, 'courseid' => $this->classid)); // insert assignment record $this->data_insert_record(); if ($this->autoenrol && $this->is_autoenrollable()) { // autoenrol all users in the track $users = usertrack::get_users($this->trackid); if (!empty($users)) { foreach ($users as $user) { $stu_record = new object(); $stu_record->userid = $user->userid; $stu_record->user_idnumber = $user->idnumber; $stu_record->classid = $this->classid; $stu_record->enrolmenttime = time(); $enrolment = new student($stu_record); // check prerequisites and enrolment limits $enrolment->add(array('prereq' => 1, 'waitlist' => 1)); } } } } else { // Look for all the curricula course is linked to - // then pull up the default system track for each curricula - // and add class to each default system track $currculums = curriculumcourse_get_list_by_course($this->courseid); $currculums = is_array($currculums) ? $currculums : array(); foreach ($currculums as $recid => $record) { // Create default track for curriculum $trkojb = new track(array('curid' => $record->curriculumid)); $trkid = $trkojb->create_default_track(); // Create track assignment object $trkassign = new trackassignmentclass(array('trackid' => $trkid, 'classid' => $this->classid, 'courseid' => $this->courseid)); // Check if class is already assigned to default track if (!$trkassign->is_class_assigned_to_default_track()) { // Determine whether class is required $curcrsobj = new curriculumcourse(array('curriculumid' => $trkassign->track->curid, 'courseid' => $trkassign->courseid)); // Get required field and determine if class is autoenrol eligible $trkassign->autoenrol = (1 == $trkassign->cmclass->count_course_assignments($trkassign->cmclass->courseid) and true === $curcrsobj->is_course_required()) ? 1 : 0; // assign class to the curriculum's default track $trkassign->assign_class_to_default_track(); } } } events_trigger('crlm_track_class_associated', $this); return true; }
/** * Determine whether the current user can unassign the user from the track. * @param int $userid The ID of the user (the assignee). * @param int $trackid The ID of the track. * @return bool Whether the current can unassign (true) or not (false) */ protected function can_unassign($userid, $trackid) { return usertrack::can_manage_assoc($userid, $trackid); }
/** * Test available table doesn't show assigned users. * @dataProvider dataprovider_available_doesnt_show_assigned_users * @param array $associations An array of arrays of parameters to construct usertrack associations. * @param int $tabletrackid The ID of the track user 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_doesnt_show_assigned_users($associations, $tabletrackid, $expectedresults, $expectedtotal) { global $USER, $DB, $CFG; $userbackup = $USER; // Set up permissions. $USER = $this->setup_permissions_test(); $this->give_permission_for_context($USER->id, 'local/elisprogram:track_enrol', context_system::instance()); foreach ($associations as $association) { $usertrack = new usertrack($association); $usertrack->save(); } // 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; }
function action_default() { $id = $this->required_param('id', PARAM_INT); $sort = $this->optional_param('sort', 'idnumber', PARAM_ALPHANUM); $dir = $this->optional_param('dir', 'ASC', PARAM_ALPHA); $page = $this->optional_param('page', 0, PARAM_INT); $perpage = $this->optional_param('perpage', 30, PARAM_INT); $columns = array('idnumber' => get_string('student_idnumber', 'block_curr_admin'), 'name' => get_string('tag_name', 'block_curr_admin'), 'email' => get_string('email', 'block_curr_admin'), 'manage' => ''); $items = usertrack::get_users($id); $count = $items ? count($items) : 0; $items = usertrack::get_users($id, $sort, $dir, $page, $perpage); $baseurl = new moodle_url($this->get_url(), array('id' => $id, 'sort' => $sort, 'dir' => $dir, 'page' => $page, 'perpage' => $perpage)); print_paging_bar($count, $page, $perpage, $baseurl); $formatters = $this->create_link_formatters(array('idnumber', 'name'), 'usermanagementpage', 'userid'); $this->print_list_view($items, $columns, $formatters, 'users'); if ($this->can_do_add()) { $this->print_assign_link(); } }
/** * Enrols a user in a track. * * @param int $userid The user id * @param int $trackid The track id */ static function enrol($userid, $trackid) { global $CURMAN; // make sure we don't double-enrol if ($CURMAN->db->record_exists(USRTRKTABLE, 'userid', $userid, 'trackid', $trackid)) { return false; } $record = new usertrack(); $record->userid = $userid; $record->trackid = $trackid; $record->data_insert_record(); $user = new user($userid); $track = new track($trackid); // add the student to the associated curriculum, if they're not already // enrolled if (!$CURMAN->db->record_exists(CURASSTABLE, 'userid', $userid, 'curriculumid', $track->curid)) { $curstu = new curriculumstudent(); $curstu->userid = $userid; $curstu->curriculumid = $track->curid; $curstu->completed = 0; $curstu->credits = 0; $curstu->locked = 0; $curstu->add(); } events_trigger('track_assigned', $record); /** * Get autoenrollable classes in the track. Classes are autoenrollable * if: * - the autoenrol flag is set * - it is the only class in that course slot for the track */ $sql = 'SELECT classid, courseid ' . 'FROM ' . $CURMAN->db->prefix_table(TRACKCLASSTABLE) . ' ' . 'WHERE trackid = \'' . $trackid . '\' ' . 'GROUP BY courseid ' . 'HAVING COUNT(*) = 1 AND MAX(autoenrol) = 1'; $classes = $CURMAN->db->get_records_sql($sql); if (!empty($classes)) { foreach ($classes as $class) { // enrol user in each autoenrolable class $stu_record = new object(); $stu_record->userid = $userid; $stu_record->classid = $class->classid; $stu_record->enrolmenttime = time(); $enrolment = new student($stu_record); // check prerequisites and enrolment limits $enrolment->add(array('prereq' => 1, 'waitlist' => 1), true); } } return true; }
/** * Updates the autoenrol flag for a particular cluster-track association * * @param int $association_id The id of the appropriate association record * @param int $autoenrol The new autoenrol value * * @return object The updated record */ public static function update_autoenrol($association_id, $autoenrol) { global $CURMAN; $db = $CURMAN->db; $old_autoenrol = get_field(CLSTTRKTABLE, 'autoenrol', 'id', $association_id); //update the flag on the association record $update_record = new stdClass(); $update_record->id = $association_id; $update_record->autoenrol = $autoenrol; $result = update_record(CLSTTRKTABLE, $update_record); if (!empty($autoenrol) and empty($old_autoenrol) and $cluster = get_field(CLSTTRKTABLE, 'clusterid', 'id', $association_id) and $track = get_field(CLSTTRKTABLE, 'trackid', 'id', $association_id)) { //Enrol all users in the cluster into track. $sql = 'SELECT uc.* FROM ' . $CURMAN->db->prefix_table(CLSTASSTABLE) . ' as uc JOIN ' . $CURMAN->db->prefix_table(USRTABLE) . ' as u ON uc.userid = u.id WHERE uc.clusterid = ' . $cluster . ' AND uc.autoenrol = 1 ORDER BY u.lastname'; $users = $db->get_records_sql($sql); if ($users) { foreach ($users as $user) { usertrack::enrol($user->userid, $track); } } } return $result; }
/** * Set up data that is needed for testing */ private function set_up_required_data($assignusertouserset = true, $assigncoursetoclass = true, $assigntracktoclass = true, $initclusterprofile = false, $initusersetfielddata = true) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once $CFG->dirroot . '/course/lib.php'; require_once $CFG->dirroot . '/lib/enrollib.php'; require_once elis::lib('data/customfield.class.php'); require_once elispm::file('accesslib.php'); require_once elispm::lib('data/classmoodlecourse.class.php'); require_once elispm::lib('data/clusterassignment.class.php'); require_once elispm::lib('data/clustertrack.class.php'); require_once elispm::lib('data/course.class.php'); require_once elispm::lib('data/curriculum.class.php'); require_once elispm::lib('data/pmclass.class.php'); require_once elispm::lib('data/track.class.php'); require_once elispm::lib('data/user.class.php'); require_once elispm::lib('data/userset.class.php'); require_once elispm::lib('data/usertrack.class.php'); $fieldcategoryid = $DB->get_field(field_category::TABLE, 'id', array('name' => 'Associated Group')); $this->assertNotEquals(false, $fieldcategoryid); $fieldcategory = new field_category($fieldcategoryid); $fieldcategory->load(); // Set up the test user. $user = new user(array('idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'country' => 'CA')); $user->save(); // Set up the test course description and class instance. $course = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => '')); $course->save(); $pmclass = new pmclass(array('courseid' => $course->id, 'idnumber' => 'testclassidnumber')); $pmclass->save(); $category = new stdClass(); $category->name = 'testcategoryname'; $category->id = $DB->insert_record('course_categories', $category); // Set up the test Moodle course. set_config('enrol_plugins_enabled', 'manual'); set_config('defaultenrol', 1, 'enrol_manual'); set_config('status', ENROL_INSTANCE_ENABLED, 'enrol_manual'); $course = new stdClass(); $course->category = $category->id; $course->shortname = 'testcourseshortname'; $course->fullname = 'testcoursefullname'; $course = create_course($course); if ($assigncoursetoclass) { // Assign the Moodle course to a class instance. $classmoodlecourse = new classmoodlecourse(array('classid' => $pmclass->id, 'moodlecourseid' => $course->id)); $classmoodlecourse->save(); } // Set up the test program and track. $curriculum = new curriculum(array('idnumber' => 'testcurriculumidnumber')); $curriculum->save(); $track = new track(array('curid' => $curriculum->id, 'idnumber' => 'testtrackidnumber')); $track->save(); if ($assigntracktoclass) { // Assign the test track to the test class instance. $trackassignment = new trackassignment(array('trackid' => $track->id, 'classid' => $pmclass->id, 'autoenrol' => 1)); $trackassignment->save(); } // Set up the test userset. $userset = new userset(); $usersetdata = array('name' => 'testusersetname'); if ($initusersetfielddata) { $usersetdata['field_userset_group'] = 1; $usersetdata['field_userset_groupings'] = 1; } $userset->set_from_data((object) $usersetdata); $userset->save(); // Assign the test user to the test track. $usertrack = new usertrack(array('userid' => $user->id, 'trackid' => $track->id)); $usertrack->save(); $clustertrack = new clustertrack(array('clusterid' => $userset->id, 'trackid' => $track->id)); $clustertrack->save(); if ($assignusertouserset) { // Assign the test user to the test userset. $clusterassignment = new clusterassignment(array('userid' => $user->id, 'clusterid' => $userset->id, 'plugin' => 'manual')); $clusterassignment->save(); } if ($initclusterprofile) { // Set up a file we can use to auto-associate users to a userset. $field = new field(array('categoryid' => $fieldcategory->id, 'shortname' => 'autoassociate', 'name' => 'autoassociate', 'datatype' => 'bool')); $field->save(); // Ensure manual field owner exists for syncing. field_owner::ensure_field_owner_exists($field, 'manual'); $ownerid = $DB->get_field(field_owner::TABLE, 'id', array('fieldid' => $field->id, 'plugin' => 'manual')); $owner = new field_owner($ownerid); $owner->param_control = 'checkbox'; $owner->save(); field_owner::ensure_field_owner_exists($field, 'moodle_profile'); $DB->execute("UPDATE {" . field_owner::TABLE . "} SET exclude = ?", array(pm_moodle_profile::sync_to_moodle)); $fieldcontextlevel = new field_contextlevel(array('fieldid' => $field->id, 'contextlevel' => CONTEXT_ELIS_USER)); $fieldcontextlevel->save(); // The associated Moodle user profile field. require_once $CFG->dirroot . '/user/profile/definelib.php'; require_once $CFG->dirroot . '/user/profile/field/checkbox/define.class.php'; $profiledefinecheckbox = new profile_define_checkbox(); $data = new stdClass(); $data->datatype = 'checkbox'; $data->categoryid = 99999; $data->shortname = 'autoassociate'; $data->name = 'autoassociate'; $profiledefinecheckbox->define_save($data); $mfield = $DB->get_record('user_info_field', array('shortname' => 'autoassociate')); // The "cluster-profile" association. $usersetprofile = new userset_profile(array('clusterid' => $userset->id, 'fieldid' => $mfield->id, 'value' => true)); $usersetprofile->save(); } // Enrol the user in the Moodle course. $mdluserid = $DB->get_field('user', 'id', array('username' => 'testuserusername')); $roleid = create_role('testrole', 'testrole', 'testrole'); enrol_try_internal_enrol($course->id, $mdluserid, $roleid); // Set up the necessary config data. set_config('userset_groups', 1, 'elisprogram_usetgroups'); set_config('siteguest', ''); // Validate setup. $this->assertEquals(0, $DB->count_records('groups')); }