diff --git a/lib.php b/lib.php index 8fce57a..41eef17 100644 --- a/lib.php +++ b/lib.php @@ -31,169 +31,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class enrol_attributes_plugin extends enrol_plugin { - /** - * Is it possible to delete enrol instance via standard UI? - * - * @param object $instance - * - * @return bool - */ - public function instance_deleteable($instance) { - return true; - } - - /** - * 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 - */ - public function get_newinstance_link($courseid) { - $context = context_course::instance($courseid); - - if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/attributes:config', - $context) - ) { - return null; - } - $configured_profilefields = explode(',', get_config('enrol_attributes', 'profilefields')); - if (!strlen(array_shift($configured_profilefields))) { - // no profile fields are configured for this plugin - return null; - } - - // multiple instances supported - different roles with different password - return new moodle_url('/enrol/attributes/edit.php', array('courseid' => $courseid)); - } - - /** - * Is it possible to delete enrol instance via standard UI? - * - * @param object $instance - * - * @return bool - */ - public function can_delete_instance($instance) { - $context = context_course::instance($instance->courseid); - - return has_capability('enrol/attributes:config', $context); - } - - /** - * Is it possible to hide/show enrol instance via standard UI? - * - * @param stdClass $instance - * - * @return bool - */ - public function can_hide_show_instance($instance) { - $context = context_course::instance($instance->courseid); - - return has_capability('enrol/attributes:config', $context); - } - - /** - * Returns edit icons for the page with list of instances - * - * @param stdClass $instance - * - * @return array - */ - public function get_action_icons(stdClass $instance) { - global $OUTPUT; - - if ($instance->enrol !== 'attributes') { - throw new coding_exception('invalid enrol instance!'); - } - $context = context_course::instance($instance->courseid); - - $icons = array(); - - if (has_capability('enrol/attributes:config', $context)) { - $editlink = new moodle_url("/enrol/attributes/edit.php", array( - 'courseid' => $instance->courseid, - 'id' => $instance->id - )); - $icons[] = $OUTPUT->action_icon($editlink, - new pix_icon('i/edit', get_string('edit'), 'core', array('class' => 'icon'))); - } - - return $icons; - } - - public static function attrsyntax_toarray($attrsyntax) { // TODO : protected - global $DB; - - $attrsyntax_object = json_decode($attrsyntax); - $rules = $attrsyntax_object->rules; - - $customuserfields = array(); - foreach ($DB->get_records('user_info_field') as $customfieldrecord) { - $customuserfields[$customfieldrecord->id] = $customfieldrecord->shortname; - } - - return array( - 'customuserfields' => $customuserfields, - 'rules' => $rules - ); - } - - public static function arraysyntax_tosql($arraysyntax, &$join_id = 0) { - global $DB; - $select = ''; - $where = '1=1'; - $params = array(); - $customuserfields = $arraysyntax['customuserfields']; - - foreach ($arraysyntax['rules'] as $rule) { - if (isset($rule->cond_op)) { - $where .= ' ' . strtoupper($rule->cond_op) . ' '; - } - else { - $where .= ' AND '; - } - // first just check if we have a value 'ANY' to enroll all people : - if (isset($rule->value) && $rule->value == 'ANY') { - $where .= '1=1'; - continue; - } - if (isset($rule->rules)) { - $sub_arraysyntax = array( - 'customuserfields' => $customuserfields, - 'rules' => $rule->rules - ); - $sub_sql = self::arraysyntax_tosql($sub_arraysyntax, $join_id); - $select .= ' ' . $sub_sql['select'] . ' '; - $where .= ' ( ' . $sub_sql['where'] . ' ) '; - $params = array_merge($params, $sub_sql['params']); - } - else { - if ($customkey = array_search($rule->param, $customuserfields, true)) { - // custom user field actually exists - $join_id++; - $data = 'd' . $join_id . '.data'; - $select .= ' RIGHT JOIN {user_info_data} d' . $join_id . ' ON d' . $join_id . '.userid = u.id AND d' . $join_id . '.fieldid = ' . $customkey; - $where .= ' (' . $DB->sql_compare_text($data) . ' = ' . $DB->sql_compare_text('?') . ' OR ' . $DB->sql_like($DB->sql_compare_text($data), - '?') . ' OR ' . $DB->sql_like($DB->sql_compare_text($data), - '?') . ' OR ' . $DB->sql_like($DB->sql_compare_text($data), '?') . ')'; - array_push($params, $rule->value, '%;' . $rule->value, $rule->value . ';%', - '%;' . $rule->value . ';%'); - } - } - } - - $where = preg_replace('/^1=1 AND/', '', $where); - $where = preg_replace('/^1=1 OR/', '', $where); - $where = preg_replace('/^1=1/', '', $where); - - return array( - 'select' => $select, - 'where' => $where, - 'params' => $params - ); - } - public static function process_login(\core\event\user_loggedin $event) { global $CFG, $DB; // we just received the event from the authentication system; check if well-formed: @@ -202,8 +39,7 @@ public static function process_login(\core\event\user_loggedin $event) { return true; } if (in_array('shibboleth', - get_enabled_auth_plugins()) && $_SERVER['SCRIPT_FILENAME'] == $CFG->dirroot . '/auth/shibboleth/index.php' - ) { + get_enabled_auth_plugins()) && $_SERVER['SCRIPT_FILENAME'] == $CFG->dirroot . '/auth/shibboleth/index.php') { // we did get this event from the Shibboleth authentication plugin, // so let's try to make the relevant mappings, ensuring that necessary profile fields exist and Shibboleth attributes are provided: $customfieldrecords = $DB->get_records('user_info_field'); @@ -215,8 +51,7 @@ public static function process_login(\core\event\user_loggedin $event) { $mappings_str = explode("\n", str_replace("\r", '', get_config('enrol_attributes', 'mappings'))); foreach ($mappings_str as $mapping_str) { if (preg_match('/^\s*([^: ]+)\s*:\s*([^: ]+)\s*$/', $mapping_str, $matches) && in_array($matches[2], - $customfields) && array_key_exists($matches[1], $_SERVER) - ) { + $customfields) && array_key_exists($matches[1], $_SERVER)) { $mapping[$matches[1]] = $matches[2]; } } @@ -295,9 +130,9 @@ public static function process_enrolments($event = null, $instanceid = null) { } $unenrol_attributes_record = $DB->get_record('enrol', array( - 'enrol' => 'attributes', - 'status' => 0, - 'id' => $user_enrolment->enrolid + 'enrol' => 'attributes', + 'status' => 0, + 'id' => $user_enrolment->enrolid )); if (!$unenrol_attributes_record) { continue; @@ -333,7 +168,8 @@ public static function process_enrolments($event = null, $instanceid = null) { $enrol_attributes_instance->unenrol_user($unenrol_attributes_record, (int)$userid); } else if ($unenrol_attributes_record->customint1 == ENROL_ATTRIBUTES_WHENEXPIREDSUSPEND) { - $enrol_attributes_instance->update_user_enrol($unenrol_attributes_record, (int)$userid, ENROL_USER_SUSPENDED); + $enrol_attributes_instance->update_user_enrol($unenrol_attributes_record, (int)$userid, + ENROL_USER_SUSPENDED); } $nbunenrolled++; } @@ -414,9 +250,78 @@ public static function process_enrolments($event = null, $instanceid = null) { return $nbenrolled; } - /* - * - */ + public static function attrsyntax_toarray($attrsyntax) { // TODO : protected + global $DB; + + $attrsyntax_object = json_decode($attrsyntax); + $rules = $attrsyntax_object->rules; + + $customuserfields = array(); + foreach ($DB->get_records('user_info_field') as $customfieldrecord) { + $customuserfields[$customfieldrecord->id] = $customfieldrecord->shortname; + } + + return array( + 'customuserfields' => $customuserfields, + 'rules' => $rules + ); + } + + public static function arraysyntax_tosql($arraysyntax, &$join_id = 0) { + global $DB; + $select = ''; + $where = '1=1'; + $params = array(); + $customuserfields = $arraysyntax['customuserfields']; + + foreach ($arraysyntax['rules'] as $rule) { + if (isset($rule->cond_op)) { + $where .= ' ' . strtoupper($rule->cond_op) . ' '; + } + else { + $where .= ' AND '; + } + // first just check if we have a value 'ANY' to enroll all people : + if (isset($rule->value) && $rule->value == 'ANY') { + $where .= '1=1'; + continue; + } + if (isset($rule->rules)) { + $sub_arraysyntax = array( + 'customuserfields' => $customuserfields, + 'rules' => $rule->rules + ); + $sub_sql = self::arraysyntax_tosql($sub_arraysyntax, $join_id); + $select .= ' ' . $sub_sql['select'] . ' '; + $where .= ' ( ' . $sub_sql['where'] . ' ) '; + $params = array_merge($params, $sub_sql['params']); + } + else { + if ($customkey = array_search($rule->param, $customuserfields, true)) { + // custom user field actually exists + $join_id++; + $data = 'd' . $join_id . '.data'; + $select .= ' RIGHT JOIN {user_info_data} d' . $join_id . ' ON d' . $join_id . '.userid = u.id AND d' . $join_id . '.fieldid = ' . $customkey; + $where .= ' (' . $DB->sql_compare_text($data) . ' = ' . $DB->sql_compare_text('?') . ' OR ' . $DB->sql_like($DB->sql_compare_text($data), + '?') . ' OR ' . $DB->sql_like($DB->sql_compare_text($data), + '?') . ' OR ' . $DB->sql_like($DB->sql_compare_text($data), '?') . ')'; + array_push($params, $rule->value, '%;' . $rule->value, $rule->value . ';%', + '%;' . $rule->value . ';%'); + } + } + } + + $where = preg_replace('/^1=1 AND/', '', $where); + $where = preg_replace('/^1=1 OR/', '', $where); + $where = preg_replace('/^1=1/', '', $where); + + return array( + 'select' => $select, + 'where' => $where, + 'params' => $params + ); + } + public static function purge_instance($instanceid, $context) { if (!$instanceid) { return false; @@ -425,8 +330,7 @@ public static function purge_instance($instanceid, $context) { if (!$DB->delete_records('role_assignments', array( 'component' => 'enrol_attributes', 'itemid' => $instanceid - )) - ) { + ))) { return false; } if (!$DB->delete_records('user_enrolments', array('enrolid' => $instanceid))) { @@ -437,6 +341,116 @@ public static function purge_instance($instanceid, $context) { return true; } + /** + * Is it possible to delete enrol instance via standard UI? + * + * @param object $instance + * + * @return bool + */ + public function instance_deleteable($instance) { + return true; + } + + /** + * 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 + */ + public function get_newinstance_link($courseid) { + $context = context_course::instance($courseid); + + if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/attributes:config', + $context)) { + return null; + } + $configured_profilefields = explode(',', get_config('enrol_attributes', 'profilefields')); + if (!strlen(array_shift($configured_profilefields))) { + // no profile fields are configured for this plugin + return null; + } + + // multiple instances supported - different roles with different password + return new moodle_url('/enrol/attributes/edit.php', array('courseid' => $courseid)); + } + + /** + * Restore instance and map settings. + * + * @param restore_enrolments_structure_step $step + * @param stdClass $data + * @param stdClass $course + * @param int $oldid + */ + public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) { + if ($step->get_task()->get_target() !== backup::TARGET_NEW_COURSE) { + return false; + } + $instanceid = $this->add_instance($course, (array)$data); + $step->set_mapping('enrol', $oldid, $instanceid); + } + + /** + * Is it possible to delete enrol instance via standard UI? + * + * @param object $instance + * + * @return bool + */ + public function can_delete_instance($instance) { + $context = context_course::instance($instance->courseid); + + return has_capability('enrol/attributes:config', $context); + } + + /** + * Is it possible to hide/show enrol instance via standard UI? + * + * @param stdClass $instance + * + * @return bool + */ + public function can_hide_show_instance($instance) { + $context = context_course::instance($instance->courseid); + + return has_capability('enrol/attributes:config', $context); + } + + /* + * + */ + + /** + * Returns edit icons for the page with list of instances + * + * @param stdClass $instance + * + * @return array + */ + public function get_action_icons(stdClass $instance) { + global $OUTPUT; + + if ($instance->enrol !== 'attributes') { + throw new coding_exception('invalid enrol instance!'); + } + $context = context_course::instance($instance->courseid); + + $icons = array(); + + if (has_capability('enrol/attributes:config', $context)) { + $editlink = new moodle_url("/enrol/attributes/edit.php", array( + 'courseid' => $instance->courseid, + 'id' => $instance->id + )); + $icons[] = $OUTPUT->action_icon($editlink, + new pix_icon('i/edit', get_string('edit'), 'core', array('class' => 'icon'))); + } + + return $icons; + } + /** * Returns enrolment instance manage link. *