/** * @todo Refactor this once we have a common save() method for datarecord subclasses. */ function action_savenew() { $trackid = $this->required_param('trackid', PARAM_INT); $userid = $this->required_param('userid', PARAM_INT); usertrack::enrol($userid, $trackid); return $this->action_default(); }
/** * Assign the tracks to the user. * @param array $elements An array containing information on tracks to assign to the user. * @param bool $bulkaction Whether this is a bulk-action or not. * @return array An array to format as JSON and return to the Javascript. */ protected function _respond_to_js(array $elements, $bulkaction) { global $DB; $userid = required_param('id', PARAM_INT); $user = new user($userid); foreach ($elements as $trackid => $label) { if ($this->can_assign($user->id, $trackid) === true) { usertrack::enrol($user->id, $trackid); } } return array('result' => 'success', 'msg' => 'Success'); }
/** * Assign the users to the track. * * @param array $elements An array of user information to assign to the track. * @param bool $bulkaction Whether this is a bulk-action or not. * @return array An array to format as JSON and return to the Javascript. */ protected function _respond_to_js(array $elements, $bulkaction) { global $DB; $trkid = required_param('id', PARAM_INT); $track = new track($trkid); // Permissions. if (trackpage::can_enrol_into_track($track->id) !== true) { return array('result' => 'fail', 'msg' => get_string('not_permitted', 'local_elisprogram')); } foreach ($elements as $userid => $label) { if ($this->can_assign($track->id, $userid) === true) { usertrack::enrol($userid, $track->id); } } return array('result' => 'success', 'msg' => 'Success'); }
/** * 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); }
/** * 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; }
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; }
/** * Create 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_create($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 already enrolled in track \"{$idnumber}\".", 0, $filename, $this->linenumber, $record, "enrolment"); return false; } if (!$this->validate_track_enrolment_data('create', $record, $filename)) { return false; } //obtain the track id $trackid = $DB->get_field(track::TABLE, 'id', array('idnumber' => $idnumber)); //create the association usertrack::enrol($userid, $trackid); //log success $success_message = "User with {$user_descriptor} successfully enrolled in track \"{$idnumber}\"."; $this->fslogger->log_success($success_message, 0, $filename, $this->linenumber); return true; }
$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');
/** * Performs track enrolment creation * @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 track_enrolment_create(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::track_enrolment_create_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_create_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_create_fail_invalid_user', 'local_datahub', '', $a); } // Respond. if (usertrack::enrol($userid, $trackid)) { $usertrackrecord = $DB->get_record(usertrack::TABLE, array('userid' => $userid, 'trackid' => $trackid)); return array('messagecode' => get_string('ws_track_enrolment_create_success_code', 'local_datahub'), 'message' => get_string('ws_track_enrolment_create_success_msg', 'local_datahub'), 'record' => (array) $usertrackrecord); } else { throw new data_object_exception('ws_track_enrolment_create_fail', 'local_datahub'); } }
/** * 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; }
/** * 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; }