/** * Gets and array of other users. * * Other users are users who have been assigned roles or inherited roles * within this course but who have not been enrolled in the course * * @global moodle_database $DB * @param string $sort * @param string $direction * @param int $page * @param int $perpage * @return array */ public function get_other_users($sort, $direction='ASC', $page=0, $perpage=25) { global $DB; if ($direction !== 'ASC') { $direction = 'DESC'; } $key = md5("$sort-$direction-$page-$perpage"); if (!array_key_exists($key, $this->otherusers)) { list($ctxcondition, $params) = $DB->get_in_or_equal($this->context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'ctx'); $params['courseid'] = $this->course->id; $params['cid'] = $this->course->id; $sql = "SELECT ra.id as raid, ra.contextid, ra.component, ctx.contextlevel, ra.roleid, u.*, ue.lastseen FROM {role_assignments} ra JOIN {user} u ON u.id = ra.userid JOIN {context} ctx ON ra.contextid = ctx.id LEFT JOIN ( SELECT ue.id, ue.userid, ul.timeaccess AS lastseen FROM {user_enrolments} ue LEFT JOIN {enrol} e ON e.id=ue.enrolid LEFT JOIN {user_lastaccess} ul ON (ul.courseid = e.courseid AND ul.userid = ue.userid) WHERE e.courseid = :courseid ) ue ON ue.userid=u.id WHERE ctx.id $ctxcondition AND ue.id IS NULL ORDER BY u.$sort $direction, ctx.depth DESC"; $this->otherusers[$key] = $DB->get_records_sql($sql, $params, $page*$perpage, $perpage); } return $this->otherusers[$key]; }
/** * Gets and array of other users. * * Other users are users who have been assigned roles or inherited roles * within this course but who have not been enrolled in the course * * @global moodle_database $DB * @param string $sort * @param string $direction * @param int $page * @param int $perpage * @return array */ public function get_other_users($sort, $direction = 'ASC', $page = 0, $perpage = 25) { global $DB; if ($direction !== 'ASC') { $direction = 'DESC'; } $key = md5("{$sort}-{$direction}-{$page}-{$perpage}"); if (!array_key_exists($key, $this->otherusers)) { list($ctxcondition, $params) = $DB->get_in_or_equal($this->context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'ctx'); $params['courseid'] = $this->course->id; $params['cid'] = $this->course->id; $extrafields = get_extra_user_fields($this->get_context()); $ufields = user_picture::fields('u', $extrafields); $sql = "SELECT ra.id as raid, ra.contextid, ra.component, ctx.contextlevel, ra.roleid, {$ufields},\n coalesce(u.lastaccess,0) AS lastaccess\n FROM {role_assignments} ra\n JOIN {user} u ON u.id = ra.userid\n JOIN {context} ctx ON ra.contextid = ctx.id\n LEFT JOIN (\n SELECT ue.id, ue.userid\n FROM {user_enrolments} ue\n JOIN {enrol} e ON e.id = ue.enrolid\n WHERE e.courseid = :courseid\n ) ue ON ue.userid=u.id\n WHERE ctx.id {$ctxcondition} AND\n ue.id IS NULL\n ORDER BY {$sort} {$direction}, ctx.depth DESC"; $this->otherusers[$key] = $DB->get_records_sql($sql, $params, $page * $perpage, $perpage); } return $this->otherusers[$key]; }
/** * Obtains WHERE clause to filter results by defined search and role filter * (instance filter is handled separately in JOIN clause, see * get_instance_sql). * * @return array Two-element array with SQL and params for WHERE clause */ protected function get_filter_sql() { global $DB; // Search condition. $extrafields = get_extra_user_fields($this->get_context()); list($sql, $params) = users_search_sql($this->searchfilter, 'u', true, $extrafields); // Role condition. if ($this->rolefilter) { // Get context SQL. $contextids = $this->context->get_parent_context_ids(); $contextids[] = $this->context->id; list($contextsql, $contextparams) = $DB->get_in_or_equal($contextids, SQL_PARAMS_NAMED); $params += $contextparams; // Role check condition. $sql .= " AND (SELECT COUNT(1) FROM {role_assignments} ra WHERE ra.userid = u.id " . "AND ra.roleid = :roleid AND ra.contextid {$contextsql}) > 0"; $params['roleid'] = $this->rolefilter; } return array($sql, $params); }
/** * Get the roles that have a given capability assigned to it * * This function does not resolve the actual permission of the capability. * It just checks for permissions and overrides. * Use get_roles_with_cap_in_context() if resolution is required. * * @param string $capability capability name (string) * @param string $permission optional, the permission defined for this capability * either CAP_ALLOW, CAP_PREVENT or CAP_PROHIBIT. Defaults to null which means any. * @param stdClass $context null means any * @return array of role records */ function get_roles_with_capability($capability, $permission = null, $context = null) { global $DB; if ($context) { $contexts = $context->get_parent_context_ids(true); list($insql, $params) = $DB->get_in_or_equal($contexts, SQL_PARAMS_NAMED, 'ctx'); $contextsql = "AND rc.contextid $insql"; } else { $params = array(); $contextsql = ''; } if ($permission) { $permissionsql = " AND rc.permission = :permission"; $params['permission'] = $permission; } else { $permissionsql = ''; } $sql = "SELECT r.* FROM {role} r WHERE r.id IN (SELECT rc.roleid FROM {role_capabilities} rc WHERE rc.capability = :capname $contextsql $permissionsql)"; $params['capname'] = $capability; return $DB->get_records_sql($sql, $params); }
/** * Makes sure user may execute functions in this context. * * @param stdClass $context * @since Moodle 2.0 */ public static function validate_context($context) { global $CFG, $PAGE; if (empty($context)) { throw new invalid_parameter_exception('Context does not exist'); } if (empty(self::$contextrestriction)) { self::$contextrestriction = context_system::instance(); } $rcontext = self::$contextrestriction; if ($rcontext->contextlevel == $context->contextlevel) { if ($rcontext->id != $context->id) { throw new restricted_context_exception(); } } else { if ($rcontext->contextlevel > $context->contextlevel) { throw new restricted_context_exception(); } else { $parents = $context->get_parent_context_ids(); if (!in_array($rcontext->id, $parents)) { throw new restricted_context_exception(); } } } $PAGE->reset_theme_and_output(); list($unused, $course, $cm) = get_context_info_array($context->id); require_login($course, false, $cm, false, true); $PAGE->set_context($context); }
/** * Makes sure user may execute functions in this context. * * @param stdClass $context * @since Moodle 2.0 */ protected static function validate_context($context) { global $CFG; if (empty($context)) { throw new invalid_parameter_exception('Context does not exist'); } if (empty(self::$contextrestriction)) { self::$contextrestriction = context_system::instance(); } $rcontext = self::$contextrestriction; if ($rcontext->contextlevel == $context->contextlevel) { if ($rcontext->id != $context->id) { throw new restricted_context_exception(); } } else { if ($rcontext->contextlevel > $context->contextlevel) { throw new restricted_context_exception(); } else { $parents = $context->get_parent_context_ids(); if (!in_array($rcontext->id, $parents)) { throw new restricted_context_exception(); } } } if ($context->contextlevel >= CONTEXT_COURSE) { list($context, $course, $cm) = get_context_info_array($context->id); require_login($course, false, $cm, false, true); } }