diff --git a/ajax.php b/ajax.php index 7e70238..8f874a5 100644 --- a/ajax.php +++ b/ajax.php @@ -15,7 +15,7 @@ // along with Moodle. If not, see . /** - * This file processes AJAX enrolment actions and returns JSON for the ilios plugin + * This file processes AJAX enrolment actions and returns JSON for the ilios plugin. * * The general idea behind this file is that any errors should throw exceptions * which will be returned and acted upon by the calling AJAX script. @@ -33,7 +33,7 @@ require_once($CFG->dirroot.'/group/lib.php'); // Must have the sesskey. -$id = required_param('id', PARAM_INT); // course id +$id = required_param('id', PARAM_INT); // Course ID. $action = required_param('action', PARAM_ALPHANUMEXT); $PAGE->set_url(new moodle_url('/enrol/ilios/ajax.php', ['id' => $id, 'action' => $action])); @@ -81,7 +81,7 @@ case 'getselectprogramoptions': require_capability('moodle/course:enrolconfig', $context); - $sid = required_param('filterid', PARAM_INT); // school id + $sid = required_param('filterid', PARAM_INT); // School ID. $programs = []; $programs = $apiclient->get($accesstoken, 'programs', ['school' => $sid], ['title' => "ASC"]); $programarray = []; @@ -131,8 +131,8 @@ case 'getselectlearnergroupoptions': require_capability('moodle/course:enrolconfig', $context); - $cid = required_param('filterid', PARAM_INT); // cohort id - $usertype = optional_param('usertype', 0, PARAM_INT); // learner or instructor + $cid = required_param('filterid', PARAM_INT); // Cohort ID. + $usertype = optional_param('usertype', 0, PARAM_INT); // Learner or instructor. $learnergroups = $apiclient->get( $accesstoken, 'learnerGroups', @@ -150,8 +150,8 @@ case 'getselectsubgroupoptions': require_capability('moodle/course:enrolconfig', $context); - $gid = required_param('filterid', PARAM_INT); // group id - $usertype = optional_param('usertype', 0, PARAM_INT); // learner or instructor + $gid = required_param('filterid', PARAM_INT); // Group ID. + $usertype = optional_param('usertype', 0, PARAM_INT); // Learner or instructor. $subgroupoptions = []; $subgroups = $apiclient->get( $accesstoken, @@ -190,14 +190,13 @@ case 'getselectinstructorgroupoptions': require_capability('moodle/course:enrolconfig', $context); - $gid = required_param('filterid', PARAM_INT); // group id + $gid = required_param('filterid', PARAM_INT); // Group ID. $instructorgroupoptions = []; $learnergroup = $apiclient->get_by_id($accesstoken, 'learnerGroups', $gid); if (!empty($learnergroup->instructorGroups)) { $instructorgroups = $apiclient->get( $accesstoken, 'instructorGroups', - // array("id" => $learnergroup->instructorGroups), '', ["title" => "ASC"] ); diff --git a/classes/task/ilios_sync_task.php b/classes/task/ilios_sync_task.php index bbb652b..b223d52 100644 --- a/classes/task/ilios_sync_task.php +++ b/classes/task/ilios_sync_task.php @@ -24,27 +24,32 @@ namespace enrol_ilios\task; -defined('MOODLE_INTERNAL') || die; +use coding_exception; +use core\task\scheduled_task; /** * Simple task to run sync enrolments. * + * @package enrol_ilios * @copyright 2018 The Regents of the University of California * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class ilios_sync_task extends \core\task\scheduled_task { +class ilios_sync_task extends scheduled_task { /** - * @inheritdoc + * Gets the task name. + * + * @return string The task name. + * @throws coding_exception */ public function get_name() { return get_string('iliossync', 'enrol_ilios'); } /** - * @inheritdoc + * Executes the task. */ - public function execute() { + public function execute(): void { global $CFG; require_once($CFG->dirroot . '/enrol/ilios/lib.php'); @@ -54,8 +59,6 @@ public function execute() { } $plugin = enrol_get_plugin('ilios'); - $result = $plugin->sync(new \text_progress_trace()); - return $result; - + $plugin->sync(new \text_progress_trace()); } } diff --git a/db/uninstall.php b/db/uninstall.php index ea68eef..4af99f8 100644 --- a/db/uninstall.php +++ b/db/uninstall.php @@ -23,8 +23,14 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -defined('MOODLE_INTERNAL') || die(); - +/** + * Callback to run during plugin uninstallation. + * Deletes all Ilios enrolments and removes any user role assignments for this plugin. + * + * @return bool Always TRUE. + * @throws coding_exception + * @throws dml_exception + */ function xmldb_enrol_ilios_uninstall() { global $CFG, $DB; diff --git a/edit.php b/edit.php index 6e4bf88..25b262f 100644 --- a/edit.php +++ b/edit.php @@ -61,11 +61,11 @@ $instance->id = null; $instance->courseid = $course->id; $instance->enrol = 'ilios'; - $instance->customchar1 = ''; // cohort / learnerGroup - $instance->customint1 = 0; // cohort / leaner group id. - $instance->customint2 = 0; // 0 - learner / 1 - instructor - $instance->customtext1 = ''; // json string of all useful values - $instance->customint6 = 0; // group id. + $instance->customchar1 = ''; // Cohort / learnerGroup. + $instance->customint1 = 0; // Cohort / leaner group id. + $instance->customint2 = 0; // Learner = 0, Instructor = 1. + $instance->customtext1 = ''; // JSON string of all useful values. + $instance->customint6 = 0; // Group ID. } // Try and make the manage instances node on the navigation active. @@ -125,7 +125,14 @@ // NOTE: no cohort or learner group changes here!!! if ($data->roleid != $instance->roleid) { // The sync script can only add roles, for perf reasons it does not modify them. - role_unassign_all(['contextid' => $context->id, 'roleid' => $instance->roleid, 'component' => 'enrol_ilios', 'itemid' => $instance->id]); + role_unassign_all( + [ + 'contextid' => $context->id, + 'roleid' => $instance->roleid, + 'component' => 'enrol_ilios', + 'itemid' => $instance->id, + ] + ); } $instance->name = $data->name; @@ -140,13 +147,19 @@ $DB->update_record('enrol', $instance); } else { - $enrol->add_instance($course, ['name' => $data->name, 'status' => $data->status, - 'customchar1' => $synctype, - 'customint1' => $syncid, - 'customtext1' => json_encode($syncinfo), - 'roleid' => $data->roleid, - 'customint2' => $data->selectusertype, - 'customint6' => $data->customint6]); + $enrol->add_instance( + $course, + [ + 'name' => $data->name, + 'status' => $data->status, + 'customchar1' => $synctype, + 'customint1' => $syncid, + 'customtext1' => json_encode($syncinfo), + 'roleid' => $data->roleid, + 'customint2' => $data->selectusertype, + 'customint6' => $data->customint6, + ], + ); } $trace = new null_progress_trace(); diff --git a/edit_form.php b/edit_form.php index cc7bbc7..7aede27 100644 --- a/edit_form.php +++ b/edit_form.php @@ -15,7 +15,7 @@ // along with Moodle. If not, see . /** - * Adds instance form + * Edit instance form. * * @package enrol_ilios * @author Carson Tam @@ -28,18 +28,41 @@ require_once("$CFG->libdir/formslib.php"); require_once("lib.php"); +/** + * Edit instance form class. + * + * @package enrol_ilios + * @author Carson Tam + * @copyright 2017 The Regents of the University of California + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ class enrol_ilios_edit_form extends moodleform { - - function definition() { + /** + * Form definition. + * + * @return void + * @throws coding_exception + * @throws dml_exception + * @throws moodle_exception + */ + protected function definition(): void { global $CFG, $DB, $PAGE; $mform = $this->_form; - /** @var enrol_ilios_plugin $plugin */ + /* @var enrol_ilios_plugin $plugin This enrolment plugin. */ list($instance, $plugin, $course) = $this->_customdata; $coursecontext = context_course::instance($course->id); - $PAGE->requires->yui_module('moodle-enrol_ilios-groupchoosers', 'M.enrol_ilios.init_groupchoosers', - [['formid' => $mform->getAttribute('id'), 'courseid' => $course->id]]); + $PAGE->requires->yui_module( + 'moodle-enrol_ilios-groupchoosers', + 'M.enrol_ilios.init_groupchoosers', + [ + [ + 'formid' => $mform->getAttribute('id'), + 'courseid' => $course->id, + ], + ] + ); $enrol = $plugin; $apiclient = $plugin->get_api_client(); @@ -54,7 +77,10 @@ function definition() { ENROL_INSTANCE_DISABLED => get_string('no')]; $mform->addElement('select', 'status', get_string('status', 'enrol_ilios'), $options); - $usertypes = [get_string('learner', 'enrol_ilios'), get_string('instructor', 'enrol_ilios')]; + $usertypes = [ + get_string('learner', 'enrol_ilios'), + get_string('instructor', 'enrol_ilios'), + ]; $schooloptions = ['' => get_string('choosedots')]; $programoptions = ['' => get_string('choosedots')]; $cohortoptions = ['' => get_string('choosedots')]; @@ -112,7 +138,6 @@ function definition() { $grouptitle = $group->title. ' ('. count($group->children) .')'; $grouptitle .= ' ('. count($group->users) .')'; - // $grouptitle .= (empty($instance->customint2)) ? ' ('. count($group->users) .')' : ' ('. count($group->instructors) .')'; $learnergroupoptions = [$instance->selectlearnergroupindex => $grouptitle]; if (!empty($group->parent)) { @@ -126,8 +151,10 @@ function definition() { $parentgroup = $apiclient->get_by_id($accesstoken, 'learnerGroups', $child->parent); $instance->learnergroupid = $parentgroup->id; $instance->selectlearnergroupindex = "$instance->learnergroupid:$parentgroup->title"; - $learnergroupoptions = [ "$instance->learnergroupid:$parentgroup->title" => $parentgroup->title]; - if (!empty($parentgroup->parent)){ + $learnergroupoptions = [ + "$instance->learnergroupid:$parentgroup->title" => $parentgroup->title, + ]; + if (!empty($parentgroup->parent)) { $grouptitle = $parentgroup->title . ' / '. $grouptitle; $processparents($parentgroup); } @@ -149,14 +176,13 @@ function definition() { $mform->hardFreeze('selectusertype', $instance->customint2); } else { $mform->addElement('select', 'selectusertype', get_string('selectusertype', 'enrol_ilios'), $usertypes); - // $mform->addRule('selectusertype', get_string('required'), 'required', null, 'client'); $mform->addHelpButton('selectusertype', 'selectusertype', 'enrol_ilios'); } if ($instance->id) { $mform->addElement('select', 'selectschool', get_string('school', 'enrol_ilios'), $schooloptions); $mform->setConstant('selectschool', $instance->selectschoolindex); - $mform->hardFreeze('selectschool', $instance->selectschoolindex); + $mform->hardFreeze('selectschool'); } else { $mform->addElement('select', 'selectschool', get_string('school', 'enrol_ilios'), $schooloptions); $mform->addRule('selectschool', get_string('required'), 'required', null, 'client'); @@ -168,7 +194,7 @@ function definition() { if ($instance->id) { $mform->addElement('select', 'selectprogram', get_string('program', 'enrol_ilios'), $programoptions); $mform->setConstant('selectprogram', $instance->selectprogramindex); - $mform->hardFreeze('selectprogram', $instance->selectprogramindex); + $mform->hardFreeze('selectprogram'); } else { $mform->addElement('select', 'selectprogram', get_string('program', 'enrol_ilios'), $programoptions); @@ -182,7 +208,7 @@ function definition() { if ($instance->id) { $mform->addElement('select', 'selectcohort', get_string('cohort', 'enrol_ilios'), $cohortoptions); $mform->setConstant('selectcohort', $instance->selectcohortindex); - $mform->hardFreeze('selectcohort', $instance->selectcohortindex); + $mform->hardFreeze('selectcohort'); } else { $mform->addElement('select', 'selectcohort', get_string('cohort', 'enrol_ilios'), $cohortoptions); @@ -196,11 +222,10 @@ function definition() { if ($instance->id) { $mform->addElement('select', 'selectlearnergroup', get_string('learnergroup', 'enrol_ilios'), $learnergroupoptions); $mform->setConstant('selectlearnergroup', $instance->selectlearnergroupindex); - $mform->hardFreeze('selectlearnergroup', $instance->selectlearnergroupindex); + $mform->hardFreeze('selectlearnergroup'); } else { $mform->addElement('select', 'selectlearnergroup', get_string('learnergroup', 'enrol_ilios'), $learnergroupoptions); - // $mform->addRule('selectlearnergroup', get_string('required'), 'required', null, 'client'); $mform->addHelpButton('selectlearnergroup', 'learnergroup', 'enrol_ilios'); $mform->disabledIf('selectlearnergroup', 'selectcohort', 'eq', ''); $mform->registerNoSubmitButton('updatelearnergroupoptions'); @@ -218,18 +243,17 @@ function definition() { if ($instance->id) { $mform->addElement('select', 'selectsubgroup', get_string('subgroup', 'enrol_ilios'), $subgroupoptions); $mform->setConstant('selectsubgroup', $instance->selectsubgroupindex); - $mform->hardFreeze('selectsubgroup', $instance->selectsubgroupindex); + $mform->hardFreeze('selectsubgroup'); } else { $mform->addElement('select', 'selectsubgroup', get_string('subgroup', 'enrol_ilios'), $subgroupoptions); $mform->addHelpButton('selectsubgroup', 'subgroup', 'enrol_ilios'); $mform->disabledIf('selectsubgroup', 'selectlearnergroup', 'eq', ''); $mform->registerNoSubmitButton('updatesubgroupoptions'); - // $mform->addElement('submit', 'updatesubgroupoptions', get_string('subgroupoptionsupdate', 'enrol_ilios')); $mform->addElement('submit', 'updatesubgroupoptions', 'Update subgroup option'); } - // Role assignment + // Role assignment. $mform->addElement('header', 'roleassignments', get_string('roleassignments', 'role')); $roles = get_assignable_roles($coursecontext); @@ -249,9 +273,6 @@ function definition() { $mform->addElement('select', 'roleid', get_string('assignrole', 'enrol_ilios'), $roles); $mform->setDefault('roleid', $enrol->get_config('roleid')); - // Group assignment - // $mform->addElement('header','groupassignment', 'Group assignment'); - $groups = [0 => get_string('none')]; foreach (groups_get_all_groups($course->id) as $group) { $groups[$group->id] = format_string($group->name, true, ['context' => $coursecontext]); @@ -275,7 +296,16 @@ function definition() { $progel =& $mform->getElement('selectschool'); } - function definition_after_data() { + /** + * Set up the form depending on current values. + * This method is called after definition(), data submission and set_data(). + * All form setup that is dependent on form values should go in here. + * + * @return void + * @throws coding_exception + * @throws moodle_exception + */ + public function definition_after_data(): void { global $DB; $mform = $this->_form; @@ -284,7 +314,7 @@ function definition_after_data() { return; } - /** @var enrol_ilios_plugin $enrol */ + /* @var enrol_ilios_plugin $enrol This enrolment plugin. */ $enrol = enrol_get_plugin('ilios'); $apiclient = $enrol->get_api_client(); $accesstoken = $enrol->get_api_access_token(); @@ -310,8 +340,7 @@ function definition_after_data() { = [ $selectvalues[0], '', '']; } } else { - list($programid, $programshorttitle, $programtitle) - = [ '', '', '']; + list($programid, $programshorttitle, $programtitle) = [ '', '', '']; } $selectvalues = $mform->getElementValue('selectcohort'); @@ -341,8 +370,8 @@ function definition_after_data() { $schools = $apiclient->get($accesstoken, 'schools', '', ['title' => "ASC"]); $progel =& $mform->getElement('selectschool'); - if ($schools === null) { // no connection to the server - // @TODO: get from cache if possible + if ($schools === null) { // No connection to the server. + // Todo: get from cache if possible. $schooloptions = ['' => get_string('error')]; $progel->load($schooloptions); } else { @@ -387,7 +416,8 @@ function definition_after_data() { $accesstoken, 'programYears', ["program" => $pid], - ["startYear" => "ASC"]); + ["startYear" => "ASC"] + ); $programyeararray = []; foreach ($programyears as $progyear) { $programyeararray[] = $progyear->id; @@ -470,22 +500,31 @@ function definition_after_data() { } } - function validation($data, $files) { + /** + * Perform some extra validation. + * + * @param array $data array of ("fieldname"=>value) of submitted data + * @param array $files array of uploaded files "element_name"=>tmp_file_path + * @return array of "element_name"=>"error_description" if there are errors, or an empty array if everything is OK. + * @throws coding_exception + * @throws dml_exception + */ + public function validation($data, $files) { global $DB; $errors = parent::validation($data, $files); - // Make sure a learner group is selected if customint2 = 1 (instructor) + // Make sure a learner group is selected if customint2 = 1 (instructor). if (!empty($data['selectusertype'])) { if (empty($data['selectlearnergroup'])) { $errors['selectlearnergroup'] = get_string('requiredforinstructor', 'enrol_ilios'); } } - // Check for existing role + // Check for existing role. $selectgrouptype = 'cohort'; list($selectgroupid, $selecttitle) = explode(':', $data['selectcohort'], 2); - if (!empty($data['selectlearnergroup'])){ + if (!empty($data['selectlearnergroup'])) { $selectgrouptype = 'learnerGroup'; list($selectgroupid, $selecttitle) = explode(':', $data['selectlearnergroup'], 2); if (!empty($data['selectsubgroup'])) { @@ -493,12 +532,31 @@ function validation($data, $files) { } } - $params = ['roleid' => $data['roleid'], 'customchar1' => $selectgrouptype, 'customint1' => $selectgroupid, 'customint2' => $data['selectusertype'], 'courseid' => $data['courseid'], 'id' => $data['id']]; - // customint2 could be NULL or 0 on the database - if (empty($data['selectusertype']) && $DB->record_exists_select('enrol', "roleid = :roleid AND customchar1 = :customchar1 AND customint1 = :customint1 AND customint2 IS NULL AND courseid = :courseid AND enrol = 'ilios' AND id <> :id", $params)) { - $errors['roleid'] = get_string('instanceexists', 'enrol_ilios'); + $params = [ + 'roleid' => $data['roleid'], + 'customchar1' => $selectgrouptype, + 'customint1' => $selectgroupid, + 'customint2' => $data['selectusertype'], + 'courseid' => $data['courseid'], + 'id' => $data['id'], + ]; + // Customint2 could be NULL or 0 on the database. + if (empty($data['selectusertype']) + && $DB->record_exists_select( + 'enrol', + "roleid = :roleid AND customchar1 = :customchar1 AND customint1 = :customint1 " + . " AND customint2 IS NULL AND courseid = :courseid AND enrol = 'ilios' AND id <> :id", + $params + ) + ) { + $errors['roleid'] = get_string('instanceexists', 'enrol_ilios'); } else { - if ($DB->record_exists_select('enrol', "roleid = :roleid AND customchar1 = :customchar1 AND customint1 = :customint1 AND customint2 = :customint2 AND courseid = :courseid AND enrol = 'ilios' AND id <> :id", $params)) { + if ($DB->record_exists_select( + 'enrol', + "roleid = :roleid AND customchar1 = :customchar1 AND customint1 = :customint1 " . + " AND customint2 = :customint2 AND courseid = :courseid AND enrol = 'ilios' AND id <> :id", + $params + )) { $errors['roleid'] = get_string('instanceexists', 'enrol_ilios'); } } diff --git a/lib.php b/lib.php index 63bbab1..23f35c6 100644 --- a/lib.php +++ b/lib.php @@ -27,7 +27,7 @@ defined('MOODLE_INTERNAL') || die(); -require_once $CFG->libdir.'/filelib.php'; +require_once($CFG->libdir.'/filelib.php'); /** * Ilios enrolment plugin implementation. @@ -35,38 +35,51 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class enrol_ilios_plugin extends enrol_plugin { - + /** + * @var ilios_client The Ilios API client. + */ protected ilios_client $apiclient; + /** + * @var string the plugin settings key for the API access token. + */ public const SETTINGS_API_ACCESS_TOKEN = 'apikey'; /** - * Constructor + * Constructor. */ public function __construct() { $this->apiclient = new ilios_client($this->get_config('host_url', ''), new curl()); } /** - * @inheritdoc + * Is it possible to delete enrol instance via standard UI? + * + * @param stdClass $instance + * @return bool + * @throws coding_exception */ - public function can_delete_instance($instance) { + public function can_delete_instance($instance): bool { $context = context_course::instance($instance->courseid); return has_capability('enrol/ilios:config', $context); } /** - * @inheritdoc + * Is it possible to hide/show enrol instance via standard UI? + * + * @param stdClass $instance + * @return bool + * @throws coding_exception */ - public function can_hide_show_instance($instance) { + public function can_hide_show_instance($instance): bool { $context = context_course::instance($instance->courseid); return has_capability('enrol/ilios:config', $context); } /** - * Returns the Ilios Client for API access + * Returns the Ilios Client for API access. * - * @return ilios_client + * @return ilios_client The Ilios API client. */ public function get_api_client(): ilios_client { return $this->apiclient; @@ -74,7 +87,8 @@ public function get_api_client(): ilios_client { /** * Retrieves the Ilios API access token from the plugin configuration. - * @return string + * + * @return string The API access token. */ public function get_api_access_token(): string { return $this->get_config(self::SETTINGS_API_ACCESS_TOKEN, ''); @@ -83,9 +97,9 @@ public function get_api_access_token(): string { /** * Returns localised name of enrol instance. * - * @param stdClass $instance (null is accepted too) - * @return string - * @throws \Exception + * @param stdClass $instance An enrol instance (or NULL for *this* instance). + * @return string The name of this enrol instance. + * @throws Exception */ public function get_instance_name($instance) { global $DB; @@ -96,19 +110,9 @@ public function get_instance_name($instance) { } else if (empty($instance->name)) { $enrol = $this->get_name(); + $groupname = get_string('pluginshortname', 'enrol_'.$enrol); + $syncinfo = json_decode($instance->customtext1); - $syncfield = $instance->customchar1; - $syncid = $instance->customint1; - - // $groups = $this->iliosclient->get_by_ids($this->get_api_access_token(), $syncfield.'s', $syncid); - - // if (!empty($groups)) { - // $group = $groups[0]; - // $groupname = format_string($group->title, true, array('context'=>context::instance_by_id($instance->courseid))); - // } else - { - $groupname = get_string('pluginshortname', 'enrol_'.$enrol); - $syncinfo = json_decode($instance->customtext1); if (!empty($syncinfo)) { $schooltitle = $syncinfo->school->title; $programtitle = $syncinfo->program->shorttitle; @@ -123,7 +127,6 @@ public function get_instance_name($instance) { $groupname .= '/'.$grouptitle; } } - } if ($role = $DB->get_record('role', ['id' => $instance->roleid])) { $role = role_get_name($role, context_course::instance($instance->courseid, IGNORE_MISSING)); @@ -156,9 +159,10 @@ public function get_instance_name($instance) { /** * Returns link to page which may be used to add new instance of enrolment plugin in course. - * @param int $courseid - * @return moodle_url page url - * @throws \Exception + * + * @param int $courseid The course ID. + * @return moodle_url|null The page URL to the new instance form, or NULL if not allowed. + * @throws Exception */ public function get_newinstance_link($courseid) { if (!$this->can_add_new_instances($courseid)) { @@ -172,15 +176,15 @@ public function get_newinstance_link($courseid) { * Given a courseid this function returns true if the user is able to enrol or configure cohorts. * AND there are cohorts that the user can view. * - * @param int $courseid - * @return bool - * @throws \Exception + * @param int $courseid The course ID. + * @return bool TRUE if the current user can add new enrolment instances in the given course, FALSE otherwise. + * @throws Exception */ - protected function can_add_new_instances($courseid) { + protected function can_add_new_instances($courseid): bool { global $DB; $coursecontext = context_course::instance($courseid); - if (!has_capability('moodle/course:enrolconfig', $coursecontext) or !has_capability('enrol/ilios:config', $coursecontext)) { + if (!has_capability('moodle/course:enrolconfig', $coursecontext) || !has_capability('enrol/ilios:config', $coursecontext)) { return false; } return true; @@ -188,11 +192,12 @@ protected function can_add_new_instances($courseid) { /** * Returns edit icons for the page with list of instances. - * @param stdClass $instance - * @return array - * @throws \Exception + * + * @param stdClass $instance The enrol instance. + * @return array The list of edit icons und URLs. + * @throws Exception */ - public function get_action_icons(stdClass $instance) { + public function get_action_icons(stdClass $instance): array { global $OUTPUT; if ($instance->enrol !== 'ilios') { @@ -211,17 +216,17 @@ public function get_action_icons(stdClass $instance) { return $icons; } - /** - * Execute synchronisation. - * @param progress_trace $trace - * @param int|NULL $courseid one course, empty mean all + * Execute all sync jobs, or all sync jobs for a given course. + * + * @param progress_trace $trace The progress tracer for this task run. + * @param int|NULL $courseid The course ID, or NULL if all sync jobs should be executed. * @return int exit code, 0 means ok, 2 means plugin disabled. - * @throws \Exception + * @throws Exception */ - public function sync($trace, $courseid = null) { + public function sync($trace, $courseid = null): int { global $CFG, $DB; - require_once $CFG->dirroot . '/group/lib.php'; + require_once($CFG->dirroot . '/group/lib.php'); $apiclient = $this->get_api_client(); $accesstoken = $this->get_api_access_token(); @@ -240,7 +245,7 @@ public function sync($trace, $courseid = null) { $trace->output('Starting user enrolment synchronisation...'); $allroles = get_all_roles(); - $iliosusers = []; // cache user data + $iliosusers = []; // Cache user data. $unenrolaction = $this->get_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL); @@ -272,87 +277,138 @@ public function sync($trace, $courseid = null) { continue; } - if (!empty($group)) { - - $enrolleduserids = []; // keep a list of enrolled user's Moodle userid (both learners and instructors). - $users = []; // ilios users in that group - $suspendenrolments = []; // list of user enrollments to suspend - - $users = []; - - if (!empty($instance->customint2) && !empty($group->instructors)) { - $trace->output("Enrolling instructors to Course ID ".$instance->courseid." with Role ID ".$instance->roleid." through Ilios Sync ID ".$instance->id."."); - $users = $apiclient->get_by_ids($accesstoken, 'users', $group->instructors); - } else if (!empty($group->users)) { - $trace->output("Enrolling students to Course ID ".$instance->courseid." with Role ID ".$instance->roleid." through Ilios Sync ID ".$instance->id."."); - $users = $apiclient->get_by_ids($accesstoken, 'users', $group->users); - } - $trace->output(count($users) . " Ilios users found."); - - foreach ($users as $user) { - // Fetch user info if not cached in $iliosusers - if (!isset($iliosusers[$user->id])) { - $iliosusers[$user->id] = null; - if (!empty($user->campusId)) { - $urec = $DB->get_record('user', ["idnumber" => $user->campusId]); - if (!empty($urec)) { - $iliosusers[$user->id] = [ 'id' => $urec->id, - 'syncfield' => $urec->idnumber ]; - } + $enrolleduserids = []; // Keep a list of enrolled user's Moodle userid (both learners and instructors). + $users = []; // Ilios users in that group. + $suspendenrolments = []; // List of user enrollments to suspend. + + $users = []; + + if (!empty($instance->customint2) && !empty($group->instructors)) { + $trace->output( + "Enrolling instructors to Course ID " + . $instance->courseid + . " with Role ID " + . $instance->roleid + . " through Ilios Sync ID " + . $instance->id + . "." + ); + $users = $apiclient->get_by_ids($accesstoken, 'users', $group->instructors); + } else if (!empty($group->users)) { + $trace->output( + "Enrolling students to Course ID " + . $instance->courseid + . " with Role ID " + . $instance->roleid + . " through Ilios Sync ID " . + $instance->id + . "." + ); + $users = $apiclient->get_by_ids($accesstoken, 'users', $group->users); + } + $trace->output(count($users) . " Ilios users found."); + + foreach ($users as $user) { + // Fetch user info if not cached in $iliosusers. + if (!isset($iliosusers[$user->id])) { + $iliosusers[$user->id] = null; + if (!empty($user->campusId)) { + $urec = $DB->get_record('user', ["idnumber" => $user->campusId]); + if (!empty($urec)) { + $iliosusers[$user->id] = [ 'id' => $urec->id, 'syncfield' => $urec->idnumber ]; } } + } - if ($iliosusers[$user->id] === null) { - if (!empty($user->campusId)) { - $trace->output("skipping: Cannot find campusId ".$user->campusId." that matches Moodle user field 'idnumber'.", 1); - } else { - $trace->output("skipping: Ilios user ".$user->id." does not have a 'campusId' field.", 1); - } + if ($iliosusers[$user->id] === null) { + if (!empty($user->campusId)) { + $trace->output( + "skipping: Cannot find campusId " + . $user->campusId + . " that matches Moodle user field 'idnumber'." + , 1); } else { - $enrolleduserids[] = $userid = $iliosusers[$user->id]['id']; + $trace->output( + "skipping: Ilios user " + . $user->id + . " does not have a 'campusId' field." + , 1 + ); + } + } else { + $enrolleduserids[] = $userid = $iliosusers[$user->id]['id']; - $ue = $DB->get_record('user_enrolments', ['enrolid' => $instance->id, 'userid' => $userid]); + $ue = $DB->get_record('user_enrolments', ['enrolid' => $instance->id, 'userid' => $userid]); - // don't enroll disabled ilios users that are currently not enrolled. - if (empty($ue) && !$user->enabled) { - continue; - } + // Don't enroll disabled Ilios users that are currently not enrolled. + if (empty($ue) && !$user->enabled) { + continue; + } - // don't re-enroll suspended enrollments for disabled ilios users - if (!empty($ue) && ENROL_USER_SUSPENDED === (int) $ue->status && !$user->enabled) { - continue; - } + // Don't re-enroll suspended enrollments for disabled Ilios users. + if (!empty($ue) && ENROL_USER_SUSPENDED === (int) $ue->status && !$user->enabled) { + continue; + } - // flag actively enrolled users that are disabled in ilios - // for enrollment suspension further downstream - if (!empty($ue) && ENROL_USER_ACTIVE === (int) $ue->status && !$user->enabled) { - $suspendenrolments[] = $ue; - continue; - } + // Flag actively enrolled users that are disabled in Ilios + // for enrollment suspension further downstream. + if (!empty($ue) && ENROL_USER_ACTIVE === (int) $ue->status && !$user->enabled) { + $suspendenrolments[] = $ue; + continue; + } - // Continue if already enrolled with active status - if (!empty($ue) && ENROL_USER_ACTIVE === (int) $ue->status) { - continue; - } + // Continue if already enrolled with active status. + if (!empty($ue) && ENROL_USER_ACTIVE === (int) $ue->status) { + continue; + } - // Enroll user - $this->enrol_user($instance, $userid, $instance->roleid, 0, 0, ENROL_USER_ACTIVE); - if (!empty($ue) && ENROL_USER_ACTIVE !== (int) $ue->status) { - $trace->output("changing enrollment status to '" . ENROL_USER_ACTIVE . "' from '{$ue->status}': userid $userid ==> courseid ".$instance->courseid, 1); - } else { - $trace->output("enrolling with " . ENROL_USER_ACTIVE . " status: userid $userid ==> courseid ".$instance->courseid, 1); - } + // Enroll user. + $this->enrol_user( + $instance, + $userid, + $instance->roleid, + 0, + 0, + ENROL_USER_ACTIVE + ); + if (!empty($ue) && ENROL_USER_ACTIVE !== (int) $ue->status) { + $trace->output( + "changing enrollment status to '" + . ENROL_USER_ACTIVE + . "' from '{$ue->status}': userid $userid ==> courseid " + . $instance->courseid + , 1 + ); + } else { + $trace->output( + "enrolling with " + . ENROL_USER_ACTIVE + . " status: userid $userid ==> courseid " + . $instance->courseid + , 1 + ); } } - // suspend active enrollments for users that are disabled in ilios + // Suspend active enrollments for users that are disabled in Ilios. foreach ($suspendenrolments as $ue) { - $trace->output("Suspending enrollment for disabled Ilios user: userid {$ue->userid} ==> courseid {$instance->courseid}.", 1); + $trace->output( + "Suspending enrollment for disabled Ilios user: userid " + . " {$ue->userid} ==> courseid {$instance->courseid}." + , 1 + ); $this->update_user_enrol($instance, $ue->userid, ENROL_USER_SUSPENDED); } // Unenrol as necessary. - $trace->output("Unenrolling users from Course ID ".$instance->courseid." with Role ID ".$instance->roleid." that no longer associate with Ilios Sync ID ".$instance->id."."); + $trace->output( + "Unenrolling users from Course ID " + . $instance->courseid." with Role ID " + . $instance->roleid + . " that no longer associate with Ilios Sync ID " + . $instance->id + . "." + ); $sql = "SELECT ue.* FROM {user_enrolments} ue @@ -363,18 +419,34 @@ public function sync($trace, $courseid = null) { } $rs = $DB->get_recordset_sql($sql); - foreach($rs as $ue) { + foreach ($rs as $ue) { if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL) { // Remove enrolment together with group membership, grades, preferences, etc. $this->unenrol_user($instance, $ue->userid); - $trace->output("unenrolling: $ue->userid ==> ".$instance->courseid." via Ilios $synctype $syncid", 1); - } else { // ENROL_EXT_REMOVED_SUSPENDNOROLES + $trace->output( + "unenrolling: $ue->userid ==> " + . $instance->courseid + . " via Ilios $synctype $syncid" + , 1 + ); + } else { // Would be ENROL_EXT_REMOVED_SUSPENDNOROLES. // Just disable and ignore any changes. if ($ue->status != ENROL_USER_SUSPENDED) { $this->update_user_enrol($instance, $ue->userid, ENROL_USER_SUSPENDED); $context = context_course::instance($instance->courseid); - role_unassign_all(['userid' => $ue->userid, 'contextid' => $context->id, 'component' => 'enrol_ilios', 'itemid' => $instance->id]); - $trace->output("suspending and unsassigning all roles: userid ".$ue->userid." ==> courseid ".$instance->courseid, 1); + role_unassign_all([ + 'userid' => $ue->userid, + 'contextid' => $context->id, + 'component' => 'enrol_ilios', + 'itemid' => $instance->id, + ]); + $trace->output( + "suspending and unsassigning all roles: userid " + . $ue->userid + . " ==> courseid " + . $instance->courseid + , 1 + ); } } } @@ -392,8 +464,14 @@ public function sync($trace, $courseid = null) { JOIN {role} r ON (r.id = e.roleid) JOIN {context} c ON (c.instanceid = e.courseid AND c.contextlevel = :coursecontext) JOIN {user} u ON (u.id = ue.userid AND u.deleted = 0) - LEFT JOIN {role_assignments} ra ON (ra.contextid = c.id AND ra.userid = ue.userid AND ra.itemid = e.id AND ra.component = 'enrol_ilios' AND e.roleid = ra.roleid) - WHERE ue.status = :useractive AND ra.id IS NULL"; + LEFT JOIN {role_assignments} ra ON ( + ra.contextid = c.id + AND ra.userid = ue.userid + AND ra.itemid = e.id + AND ra.component = 'enrol_ilios' + AND e.roleid = ra.roleid + ) + WHERE ue.status = :useractive AND ra.id IS NULL"; $params = []; $params['statusenabled'] = ENROL_INSTANCE_ENABLED; $params['useractive'] = ENROL_USER_ACTIVE; @@ -401,7 +479,7 @@ public function sync($trace, $courseid = null) { $params['courseid'] = $courseid; $rs = $DB->get_recordset_sql($sql, $params); - foreach($rs as $ra) { + foreach ($rs as $ra) { role_assign($ra->roleid, $ra->userid, $ra->contextid, 'enrol_ilios', $ra->itemid); $trace->output("assigning role: $ra->userid ==> $ra->courseid as ".$allroles[$ra->roleid]->shortname, 1); } @@ -422,7 +500,7 @@ public function sync($trace, $courseid = null) { $params['courseid'] = $courseid; $rs = $DB->get_recordset_sql($sql, $params); - foreach($rs as $ra) { + foreach ($rs as $ra) { role_unassign($ra->roleid, $ra->userid, $ra->contextid, 'enrol_ilios', $ra->itemid); $trace->output("unassigning role: $ra->userid ==> $ra->courseid as ".$allroles[$ra->roleid]->shortname, 1); } @@ -442,7 +520,7 @@ public function sync($trace, $courseid = null) { $params['courseid'] = $courseid; $rs = $DB->get_recordset_sql($sql, $params); - foreach($rs as $gm) { + foreach ($rs as $gm) { groups_remove_member($gm->groupid, $gm->userid); $trace->output("removing user from group: $gm->userid ==> $gm->courseid - $gm->groupname", 1); } @@ -460,7 +538,7 @@ public function sync($trace, $courseid = null) { $params['courseid'] = $courseid; $rs = $DB->get_recordset_sql($sql, $params); - foreach($rs as $ue) { + foreach ($rs as $ue) { groups_add_member($ue->groupid, $ue->userid, 'enrol_ilios', $ue->enrolid); $trace->output("adding user to group: $ue->userid ==> $ue->courseid - $ue->groupname", 1); } @@ -471,25 +549,13 @@ public function sync($trace, $courseid = null) { return 0; } - // /** - // * Called after updating/inserting course. - // * - // * @param bool $inserted true if course just inserted - // * @param stdClass $course - // * @param stdClass $data form data - // * @return void - // */ - // public function course_updated($inserted, $course, $data) { - // It turns out there is no need for cohorts to deal with this hook, see MDL-34870. - // } - /** - * Update instance status + * Update instance status. * * @param stdClass $instance * @param int $newstatus ENROL_INSTANCE_ENABLED, ENROL_INSTANCE_DISABLED * @return void - * @throws \Exception + * @throws Exception */ public function update_status($instance, $newstatus) { parent::update_status($instance, $newstatus); @@ -501,14 +567,14 @@ public function update_status($instance, $newstatus) { /** * Does this plugin allow manual unenrolment of a specific user? - * Yes, but only if user suspended... + * Yes, but only if user suspended. * - * @param stdClass $instance course enrol instance - * @param stdClass $ue record from user_enrolments table + * @param stdClass $instance The course enrol instance. + * @param stdClass $ue A user enrolment record. * - * @return bool - true means user with 'enrol/xxx:unenrol' may unenrol this user, false means nobody may touch this user enrolment + * @return bool TRUE means that the current user may unenrol this user, FALSE otherwise. */ - public function allow_unenrol_user(stdClass $instance, stdClass $ue) { + public function allow_unenrol_user(stdClass $instance, stdClass $ue): bool { if ($ue->status == ENROL_USER_SUSPENDED) { return true; } @@ -519,12 +585,12 @@ public function allow_unenrol_user(stdClass $instance, stdClass $ue) { /** * Gets an array of the user enrolment actions. * - * @param course_enrolment_manager $manager - * @param stdClass $ue A user enrolment object - * @return array An array of user_enrolment_actions - * @throws \Exception + * @param course_enrolment_manager $manager The course enrolment manager. + * @param stdClass $ue A user enrolment object. + * @return array An array of user_enrolment_actions. + * @throws Exception */ - public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) { + public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue): array { $actions = []; $context = $manager->get_context(); $instance = $ue->enrolmentinstance; @@ -532,40 +598,26 @@ public function get_user_enrolment_actions(course_enrolment_manager $manager, $u $params['ue'] = $ue->id; if ($this->allow_unenrol_user($instance, $ue) && has_capability('enrol/ilios:unenrol', $context)) { $url = new moodle_url('/enrol/unenroluser.php', $params); - $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, ['class' => 'unenrollink', 'rel' => $ue->id]); + $actions[] = new user_enrolment_action( + new pix_icon('t/delete', ''), + get_string('unenrol', 'enrol'), + $url, + ['class' => 'unenrollink', 'rel' => $ue->id] + ); } return $actions; } - /** - * Returns a button to enrol a ilios or its users through the manual enrolment plugin. - * - * @param course_enrolment_manager $manager - * @return enrol_user_button - * @throws \Exception - */ - // public function get_manual_enrol_button(course_enrolment_manager $manager) { - // $course = $manager->get_course(); - // if (!$this->can_add_new_instances($course->id)) { - // return false; - // } - - // $iliosurl = new moodle_url('/enrol/ilios/edit.php', array('courseid' => $course->id)); - // $button = new enrol_user_button($iliosurl, get_string('enrol', 'enrol_ilios'), 'get'); - - // return $button; - // } - /** * Restore instance and map settings. * - * @param restore_enrolments_structure_step $step - * @param stdClass $data - * @param stdClass $course - * @param int $oldid - * @throws \Exception + * @param restore_enrolments_structure_step $step The restore enrolments structure step. + * @param stdClass $data The data object. + * @param stdClass $course The course object. + * @param int $oldid The old enrolment instance ID. + * @throws Exception */ - public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) { + public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid): void { global $DB; if (!$step->get_task()->is_samesite()) { @@ -578,9 +630,17 @@ public function restore_instance(restore_enrolments_structure_step $step, stdCla $data->customint6 = $step->get_mappingid('group', $data->customint6); } - // if ($data->roleid and $DB->record_exists('cohort', array('id'=>$data->customint1))) { if ($data->roleid) { - $instance = $DB->get_record('enrol', ['roleid' => $data->roleid, 'customint1' => $data->customint1, 'customchar1' => $data->customchar1, 'courseid' => $course->id, 'enrol' => $this->get_name()]); + $instance = $DB->get_record( + 'enrol', + [ + 'roleid' => $data->roleid, + 'customint1' => $data->customint1, + 'customchar1' => $data->customchar1, + 'courseid' => $course->id, + 'enrol' => $this->get_name(), + ], + ); if ($instance) { $instanceid = $instance->id; } else { @@ -593,7 +653,16 @@ public function restore_instance(restore_enrolments_structure_step $step, stdCla $trace->finished(); } else if ($this->get_config('unenrolaction') == ENROL_EXT_REMOVED_SUSPENDNOROLES) { - $instance = $DB->get_record('enrol', ['roleid' => $data->roleid, 'customint1' => $data->customint1, 'customerchar1' => $data->customchar1, 'courseid' => $course->id, 'enrol' => $this->get_name()]); + $instance = $DB->get_record( + 'enrol', + [ + 'roleid' => $data->roleid, + 'customint1' => $data->customint1, + 'customerchar1' => $data->customchar1, + 'courseid' => $course->id, + 'enrol' => $this->get_name(), + ], + ); if ($instance) { $instanceid = $instance->id; } else { @@ -614,14 +683,20 @@ public function restore_instance(restore_enrolments_structure_step $step, stdCla /** * Restore user enrolment. * - * @param restore_enrolments_structure_step $step - * @param stdClass $data - * @param stdClass $instance - * @param int $oldinstancestatus - * @param int $userid - * @throws \Exception + * @param restore_enrolments_structure_step $step The restore enrolment structure step. + * @param stdClass $data The data object. + * @param stdClass $instance The enrolment instance. + * @param int $userid The user ID. + * @param int $oldinstancestatus The old enrolment instance status. + * @throws Exception */ - public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) { + public function restore_user_enrolment( + restore_enrolments_structure_step $step, + $data, + $instance, + $userid, + $oldinstancestatus + ): void { global $DB; if ($this->get_config('unenrolaction') != ENROL_EXT_REMOVED_SUSPENDNOROLES) { @@ -638,29 +713,18 @@ public function restore_user_enrolment(restore_enrolments_structure_step $step, } /** - * Restore user group membership. - * @param stdClass $instance - * @param int $groupid - * @param int $userid - */ - public function restore_group_member($instance, $groupid, $userid) { - // Nothing to do here, the group members are added in $this->restore_group_restored() - return; - } - - /** - * Recursive get for learner group data with instructors info, to compensate + * Recursive get for learner group data with instructors info, to compensate for * something that the ILIOS API fails to do! * - * @param string $grouptype singular noun of the group type, e.g. cohort, learnerGroup - * @param string $groupid the id for the corresponding group type, e.g. cohort id, learner group id. + * @param string $grouptype Singular noun of the group type, e.g. cohort, learnerGroup. + * @param string $groupid The ID for the corresponding group type, e.g. cohort id, learner group id. * - * @return mixed returned by the ILIOS api in addition of populating + * @return mixed Returned by the ILIOS api in addition of populating * the instructor array with correct ids, which is to * iterate into offerings and ilmSessions and fetch the * associated instructors and instructor groups. Should * also iterate into subgroups. - * @throws \Exception + * @throws Exception */ public function get_group_data($grouptype, $groupid) { $apiclient = $this->get_api_client(); @@ -677,14 +741,14 @@ public function get_group_data($grouptype, $groupid) { } /** - * @param $grouptype - * @param $groupid - * - * @return array + * Retrieves a list instructors for a given type of group (learner group or instructor group) and given group ID. + + * @param string $grouptype The group type (either 'instructorgroup' or 'learnergroup'). + * @param string $groupid The group ID. + * @return array A list of user IDs. * @throws moodle_exception */ - private function get_instructor_ids_from_group($grouptype, $groupid) { - + private function get_instructor_ids_from_group($grouptype, $groupid): array { $apiclient = $this->get_api_client(); $accesstoken = $this->get_api_access_token(); @@ -694,56 +758,57 @@ private function get_instructor_ids_from_group($grouptype, $groupid) { $instructorgroupids = []; $instructorids = []; - // get instructors/instructor-groups from the offerings that this learner group is being taught in. + // Get instructors/instructor-groups from the offerings that this learner group is being taught in. if (!empty($group->offerings)) { $offerings = $apiclient->get_by_ids($accesstoken, 'offerings', $group->offerings); foreach ($offerings as $offering) { if (empty($offering->instructors)) { - // no instructor AND no instructor groups have been set for this offering. - // fall back to the default instructors/instructor-groups defined for the learner group. + // No instructor AND no instructor groups have been set for this offering. + // Fall back to the default instructors/instructor-groups defined for the learner group. $instructorids = array_merge($instructorids, $group->instructors); $instructorgroupids = array_merge($instructorgroupids, $group->instructorGroups); } else { - // if there are instructors and/or instructor-groups set on the offering, - // then use these. + // If there are instructors and/or instructor-groups set on the offering, then use these. $instructorids = array_merge($instructorids, $offering->instructors); $instructorgroupids = array_merge($instructorgroupids, $offering->instructorGroups); } } - } - // get instructors/instructor-groups from the ilm sessions that this learner group is being taught in. - // (this is a rinse/repeat from offerings-related code above) + // Get instructors/instructor-groups from the ilm sessions that this learner group is being taught in. + // This is a rinse/repeat from offerings-related code above. if (!empty($group->ilmSessions)) { $ilms = $apiclient->get_by_ids($accesstoken, 'ilmSessions', $group->ilmSessions); foreach ($ilms as $ilm) { if (empty($ilm->instructors) && empty($ilm->instructorGroups)) { - // no instructor AND no instructor groups have been set for this offering. - // fall back to the default instructors/instructor-groups defined for the learner group. + // No instructor AND no instructor groups have been set for this offering. + // Fall back to the default instructors/instructor-groups defined for the learner group. $instructorids = array_merge($instructorids, $group->instructors); $instructorgroupids = array_merge($instructorgroupids, $group->instructorGroups); } else { - // if there are instructors and/or instructor-groups set on the offering, - // then use these. + // If there are instructors and/or instructor-groups set on the offering, then use these. $instructorids = array_merge($instructorids, $ilm->instructors); $instructorgroupids = array_merge($instructorgroupids, $ilm->instructorGroups); } } } - // get instructors from sub-learnerGroups + // Get instructors from sub-learner-groups. if (!empty($group->children)) { - foreach($group->children as $subgroupid) { - $instructorids = array_merge($instructorids, $this->get_instructor_ids_from_group('learnerGroup', $subgroupid)); - // We don't care about instructor groups here, we will merge instructor groups into the $instructorIds array later. + foreach ($group->children as $subgroupid) { + $instructorids = array_merge( + $instructorids, + $this->get_instructor_ids_from_group('learnerGroup', $subgroupid) + ); + // We don't care about instructor groups here, + // we will merge instructor groups into the $instructorIds array later. } } - // next, get the ids of all instructors from the instructor-groups that we determined as relevant earlier. - // but first.. let's de-dupe them. + // Next, get the ids of all instructors from the instructor-groups that we determined as relevant earlier. + // But first let's de-dupe them. $instructorgroupids = array_unique($instructorgroupids); if (!empty($instructorgroupids)) { $instructorgroups = $apiclient->get_by_ids($accesstoken, 'instructorGroups', $instructorgroupids); @@ -752,7 +817,7 @@ private function get_instructor_ids_from_group($grouptype, $groupid) { } } - // finally, we retrieve all the users that were identified as relevant instructors earlier. + // Finally, we retrieve all the users that were identified as relevant instructors earlier. $instructorids = array_unique($instructorids); return $instructorids; @@ -760,12 +825,14 @@ private function get_instructor_ids_from_group($grouptype, $groupid) { } /** - * Prevent removal of enrol roles. - * @param int $itemid - * @param int $groupid - * @param int $userid - * @return bool + * Prevents the removal of enrol roles. + * Implements the allow_group_member_remove callback from the Group API. + * + * @param int $itemid The item ID. + * @param int $groupid The group ID. + * @param int $userid The user ID. + * @return bool Always FALSE. */ -function enrol_ilios_allow_group_member_remove($itemid, $groupid, $userid) { +function enrol_ilios_allow_group_member_remove($itemid, $groupid, $userid): bool { return false; } diff --git a/settings.php b/settings.php index 13004be..fbb6e55 100644 --- a/settings.php +++ b/settings.php @@ -27,26 +27,55 @@ if ($ADMIN->fulltree) { - // --- general settings ----------------------------------------------------------------------------------- + // General settings. $settings->add(new admin_setting_heading('enrol_ilios_settings', '', get_string('pluginname_desc', 'enrol_ilios'))); - // --- enrol instance defaults ---------------------------------------------------------------------------- + // Enrol instance defaults. if (!during_initial_install()) { - // FIX: Change host to host_url (more descriptive) - $settings->add(new admin_setting_configtext('enrol_ilios/host_url', get_string('host_url', 'enrol_ilios'), get_string('host_url_desc', 'enrol_ilios'), 'localhost')); - $settings->add(new admin_setting_configtext('enrol_ilios/apikey', get_string('apikey', 'enrol_ilios'), get_string('apikey_desc', 'enrol_ilios'), '')); + // FIX: Change host to host_url (more descriptive). + $settings->add( + new admin_setting_configtext( + 'enrol_ilios/host_url', + get_string('host_url', 'enrol_ilios'), + get_string('host_url_desc', 'enrol_ilios'), + 'localhost' + ) + ); + $settings->add( + new admin_setting_configtext( + 'enrol_ilios/apikey', + get_string('apikey', 'enrol_ilios'), + get_string('apikey_desc', 'enrol_ilios'), + '' + ) + ); $options = get_default_enrol_roles(context_system::instance()); $student = get_archetype_roles('student'); $student = reset($student); - $settings->add(new admin_setting_configselect('enrol_ilios/roleid', - get_string('defaultlearnerrole', 'enrol_ilios'), - '', $student->id, $options)); + $settings->add( + new admin_setting_configselect( + 'enrol_ilios/roleid', + get_string('defaultlearnerrole', 'enrol_ilios'), + '', + $student->id, + $options + ) + ); $options = [ - ENROL_EXT_REMOVED_UNENROL => get_string('extremovedunenrol', 'enrol'), - ENROL_EXT_REMOVED_SUSPENDNOROLES => get_string('extremovedsuspendnoroles', 'enrol')]; - $settings->add(new admin_setting_configselect('enrol_ilios/unenrolaction', get_string('extremovedaction', 'enrol'), get_string('extremovedaction_help', 'enrol'), ENROL_EXT_REMOVED_UNENROL, $options)); + ENROL_EXT_REMOVED_UNENROL => get_string('extremovedunenrol', 'enrol'), + ENROL_EXT_REMOVED_SUSPENDNOROLES => get_string('extremovedsuspendnoroles', 'enrol'), + ]; + $settings->add( + new admin_setting_configselect( + 'enrol_ilios/unenrolaction', + get_string('extremovedaction', 'enrol'), + get_string('extremovedaction_help', 'enrol'), + ENROL_EXT_REMOVED_UNENROL, + $options + ) + ); } } diff --git a/version.php b/version.php index d367691..724e615 100644 --- a/version.php +++ b/version.php @@ -25,9 +25,9 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2024053100; // The current plugin version (Date: YYYYMMDDXX) -$plugin->requires = 2023100400; // Requires this Moodle version -$plugin->component = 'enrol_ilios'; // Full name of the plugin (used for diagnostics) +$plugin->version = 2024053100; // The current plugin version (Date: YYYYMMDDXX). +$plugin->requires = 2023100400; // Requires this Moodle version. +$plugin->component = 'enrol_ilios'; // Full name of the plugin (used for diagnostics). $plugin->release = 'v4.3'; $plugin->supported = [403, 403]; $plugin->maturity = MATURITY_STABLE;