From f62874d87b659050001521b42a9ef7008f74488c Mon Sep 17 00:00:00 2001 From: Mohammad Farouk Date: Thu, 3 Oct 2024 17:57:24 +0300 Subject: [PATCH] Add overrides class --- classes/restriction/overrides.php | 151 ++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 classes/restriction/overrides.php diff --git a/classes/restriction/overrides.php b/classes/restriction/overrides.php new file mode 100644 index 00000000..8505065b --- /dev/null +++ b/classes/restriction/overrides.php @@ -0,0 +1,151 @@ +. + +namespace enrol_wallet\restriction; + +/** + * Class overrides + * + * @package enrol_wallet + * @copyright 2024 Mohammad Farouk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class overrides { + /** + * The database table name. + * @var string + */ + public const TABLE = 'enrol_wallet_overrides'; + + /** + * Check if the user has an override from been restricted in this instance. + * @param int $instanceid + * @param int $userid + * @return bool + */ + public static function is_instance_overridden($instanceid, $userid = 0) { + $rules = self::get_instance_overridden_rules($instanceid, $userid); + return !empty($rules) && !empty($rules['restrictions']); + } + + /** + * Get restriction rules for an instance for a given user. + * @param int $instanceid + * @param int $userid + * @return array|null + */ + public static function get_instance_overridden_rules($instanceid, $userid = 0) { + global $DB, $USER, $CFG; + if (empty($userid)) { + $userid = $USER->id; + } + + if (isguestuser($userid)) { + return null; + } + + // Check if this user is a part of an overridden cohort. + require_once($CFG->dirroot . '/cohort/lib.php'); + $usercohorts = cohort_get_user_cohorts($userid); + $cohortsids = []; + foreach ($usercohorts as $cohort) { + $cohortsids[] = $cohort->id; + } + unset($usercohorts); + + $cohortsids = array_unique($cohortsids); + [$cohortsin, $cohortsparams] = $DB->get_in_or_equal($cohortsids, SQL_PARAMS_NAMED); + $sql = "SELECT id, rules + FROM {enrol_wallet_overrides} + WHERE (userid = :userid + OR cohortid $cohortsin) + AND thing = :enrol + AND thingid = :instanceid"; + + $params = [ + 'userid' => $userid, + 'thing' => 'enrol', + 'thingid' => $instanceid, + ] + $cohortsparams; + + $records = $DB->get_records_sql($sql, $params); + + if (count($records) > 1) { + $return = []; + foreach ($records as $r) { + $rules = json_decode($r->rules, true); + if (!empty($rules)) { + // Merge the rules. + foreach ($rules as $key => $bool) { + if ((bool)$bool) { + $return[$key] = true; + } + } + } + } + return $return; + } else { + $record = reset($records); + return json_decode($record->rules, true); + } + } + + /** + * Override a user from restriction for an enrol wallet instance. + * @param int $instanceid + * @param int $id user id or cohort id if cohort set to true. + * @param bool $iscohort if this is a cohort overriding + * @return bool + */ + public static function override_instance($instanceid, $id, $iscohort = false) { + global $DB, $USER; + $conditions = [ + 'thing' => 'enrol', + 'thingid' => $instanceid, + ]; + + if ($iscohort) { + $conditions['cohortid'] = $id; + } else { + $conditions['userid'] = $id; + } + + if ($record = $DB->get_record(self::TABLE, $conditions)) { + + $rules = json_decode($record->rules, true); + if (empty($rules)) { + $rules = []; + } + + $rules['restriction'] = true; + $record->rules = json_encode($rules); + $record->timemodified = time(); + $record->usermodified = $USER->id; + + return $DB->update_record(self::TABLE, $record); + } + $rules = [ + 'restriction' => true, + ]; + $record = $conditions + [ + 'timecreated' => time(), + 'timemodified' => time(), + 'usermodified' => $USER->id, + 'rules' => json_encode($rules), + ]; + return $DB->insert_record(self::TABLE, $record, false); + } +}