/**
* 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
*/
protected function can_add_new_instances($courseid)
{
global $DB;
$coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
if (!has_capability('moodle/course:enrolconfig', $coursecontext) or !has_capability('enrol/cohort:config', $coursecontext)) {
return false;
}
list($sqlparents, $params) = $DB->get_in_or_equal(get_parent_contexts($coursecontext));
$sql = "SELECT id, contextid\n FROM {cohort}\n WHERE contextid {$sqlparents}\n ORDER BY name ASC";
$cohorts = $DB->get_records_sql($sql, $params);
foreach ($cohorts as $c) {
$context = get_context_instance_by_id($c->contextid);
if (has_capability('moodle/cohort:view', $context)) {
return true;
}
}
return false;
}
开发者ID:nmicha,项目名称:moodle,代码行数:25,代码来源:lib.php
示例4: get_newinstance_link
/**
* 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)
{
global $DB;
$coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
if (!has_capability('moodle/course:enrolconfig', $coursecontext) or !has_capability('enrol/cohort:config', $coursecontext)) {
return NULL;
}
list($sqlparents, $params) = $DB->get_in_or_equal(get_parent_contexts($coursecontext));
$sql = "SELECT id, contextid\n FROM {cohort}\n WHERE contextid {$sqlparents}\n ORDER BY name ASC";
$cohorts = $DB->get_records_sql($sql, $params);
$found = false;
foreach ($cohorts as $c) {
$context = get_context_instance_by_id($c->contextid);
if (has_capability('moodle/cohort:view', $context)) {
$found = true;
break;
}
}
if (!$found) {
return NULL;
}
// multiple instances supported - multiple parent courses linked
return new moodle_url('/enrol/cohort/addinstance.php', array('id' => $courseid));
}
开发者ID:vuchannguyen,项目名称:web,代码行数:29,代码来源:lib.php
示例5: role_unassigned
public function role_unassigned($ra)
{
global $DB;
if (!enrol_is_enabled('category')) {
return true;
}
// only category level roles are interesting
$parentcontext = get_context_instance_by_id($ra->contextid);
if ($parentcontext->contextlevel != CONTEXT_COURSECAT) {
return true;
}
// now this is going to be a bit slow, take all enrolments in child courses and verify each separately
$syscontext = get_context_instance(CONTEXT_SYSTEM);
if (!($roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext))) {
return true;
}
$plugin = enrol_get_plugin('category');
$sql = "SELECT e.*\n FROM {course} c\n JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :courselevel AND ctx.path LIKE :match)\n JOIN {enrol} e ON (e.courseid = c.id AND e.enrol = 'category')\n JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = :userid)";
$params = array('courselevel' => CONTEXT_COURSE, 'match' => $parentcontext->path . '/%', 'userid' => $ra->userid);
$rs = $DB->get_recordset_sql($sql, $params);
list($roleids, $params) = $DB->get_in_or_equal(array_keys($roles), SQL_PARAMS_NAMED, 'r');
$params['userid'] = $ra->userid;
foreach ($rs as $instance) {
$coursecontext = get_context_instance(CONTEXT_COURSE, $instance->courseid);
$contextids = get_parent_contexts($coursecontext);
array_pop($contextids);
// remove system context, we are interested in categories only
list($contextids, $contextparams) = $DB->get_in_or_equal($contextids, SQL_PARAMS_NAMED, 'c');
$params = array_merge($params, $contextparams);
$sql = "SELECT ra.id\n FROM {role_assignments} ra\n WHERE ra.userid = :userid AND ra.contextid {$contextids} AND ra.roleid {$roleids}";
if (!$DB->record_exists_sql($sql, $params)) {
// user does not have any interesting role in any parent context, let's unenrol
$plugin->unenrol_user($instance, $ra->userid);
}
}
$rs->close();
return true;
}
/**
* Counts all the users assigned this role in this context or higher
* @param int roleid
* @param int contextid
* @param bool parent if true, get list of users assigned in higher context too
* @return array()
*/
function count_role_users($roleid, $context, $parent = false)
{
global $CFG;
if ($parent) {
if ($contexts = get_parent_contexts($context)) {
$parentcontexts = ' OR r.contextid IN (' . implode(',', $contexts) . ')';
} else {
$parentcontexts = '';
}
} else {
$parentcontexts = '';
}
$SQL = "SELECT count(*)\n FROM {$CFG->prefix}role_assignments r\n WHERE (r.contextid = {$context->id} {$parentcontexts})\n AND r.roleid = {$roleid}";
return count_records_sql($SQL);
}
/**
* Prepare list of roles for display, apply aliases and format text
* @param array $roleoptions array roleid => rolename or roleid => roleobject
* @param object $context a context
* @return array of context-specific role names, or role objexts with a ->localname field added.
*/
function role_fix_names($roleoptions, $context, $rolenamedisplay = ROLENAME_ALIAS)
{
global $DB;
// Make sure we are working with an array roleid => name. Normally we
// want to use the unlocalised name if the localised one is not present.
$newnames = array();
foreach ($roleoptions as $rid => $roleorname) {
if ($rolenamedisplay != ROLENAME_ALIAS_RAW) {
if (is_object($roleorname)) {
$newnames[$rid] = $roleorname->name;
} else {
$newnames[$rid] = $roleorname;
}
} else {
$newnames[$rid] = '';
}
}
// If necessary, get the localised names.
if ($rolenamedisplay != ROLENAME_ORIGINAL && !empty($context->id)) {
// Make sure we have a course context.
if ($context->contextlevel == CONTEXT_MODULE || $context->contextlevel == CONTEXT_BLOCK) {
// find the parent course context
if ($parentcontextid = array_shift(get_parent_contexts($context))) {
$context = get_context_instance_by_id($parentcontextid);
}
}
// The get the relevant renames, and use them.
$aliasnames = $DB->get_records('role_names', array('contextid' => $context->id));
foreach ($aliasnames as $alias) {
if (isset($newnames[$alias->roleid])) {
if ($rolenamedisplay == ROLENAME_ALIAS || $rolenamedisplay == ROLENAME_ALIAS_RAW) {
$newnames[$alias->roleid] = $alias->name;
} else {
if ($rolenamedisplay == ROLENAME_BOTH) {
$newnames[$alias->roleid] = $alias->name . ' (' . $roleoptions[$alias->roleid] . ')';
}
}
}
}
}
// Finally, apply format_string and put the result in the right place.
foreach ($roleoptions as $rid => $roleorname) {
if ($rolenamedisplay != ROLENAME_ALIAS_RAW) {
$newnames[$rid] = strip_tags(format_string($newnames[$rid]));
}
if (is_object($roleorname)) {
$roleoptions[$rid]->localname = $newnames[$rid];
} else {
$roleoptions[$rid] = $newnames[$rid];
}
}
return $roleoptions;
}
/**
* 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(get_parent_contexts($this->context, 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\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, ul.timeaccess AS lastseen\n FROM {user_enrolments} ue\n LEFT JOIN {enrol} e ON e.id=ue.enrolid\n LEFT JOIN {user_lastaccess} ul ON (ul.courseid = e.courseid AND ul.userid = ue.userid)\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 u.{$sort} {$direction}, ctx.depth DESC";
$this->otherusers[$key] = $DB->get_records_sql($sql, $params, $page * $perpage, $perpage);
}
return $this->otherusers[$key];
}
请发表评论