* @param $limitfrom - number of records to skip (offset)
* @param $limitnum - number of records to fetch
* @param $groups - single group or array of groups - group(s) user is in
+ * @param $exceptions - list of users to exclude
*/
-function get_users_by_capability($context, $capability, $fields='u.*', $sort='', $limitfrom='', $limitnum='', $groups='') {
+function get_users_by_capability($context, $capability, $fields='u.*, ul.timeaccess as lastaccess', $sort='ul.timeaccess', $limitfrom='', $limitnum='', $groups='', $exceptions='') {
global $CFG;
+ /// sorting out groups
if ($groups) {
- $groupjoin = 'LEFT JOIN '.$CFG->prefix.'groups_members gm ON gm.userid = ra.userid';
+ $groupjoin = 'INNER JOIN '.$CFG->prefix.'groups_members gm ON gm.userid = ra.userid';
if (is_array($groups)) {
$groupsql = 'AND gm.id IN ('.implode(',', $groups).')';
$groupsql = '';
}
- // first get all roles with this capability in this context, or above
+ /// sorting out exceptions
+ if ($exceptions) {
+ $exceptionsql = "AND u.id NOT IN ($exceptions)";
+ }
+
+ /// if context is a course, then constrct sql for ul
+ if ($context->aggregatelevel == COURSE_CONTEXT) {
+ $courseid = $context->instanceid;
+ $coursesql = "AND (ul.courseid = $courseid OR ISNULL(ul.courseid)";
+ }
+
+ /// sorting out roles with this capability set
$possibleroles = get_roles_with_capability($capability, CAP_ALLOW, $context);
$validroleids = array();
foreach ($possibleroles as $prole) {
if ($caps[$capability] > 0) { // resolved capability > 0
$validroleids[] = $prole->id;
}
- }
+ }
+ $roleids = '('.implode(',', $validroleids).')';
- /// the following few lines may not be needed
- if ($usercontexts = get_parent_contexts($context)) {
- $listofcontexts = '('.implode(',', $usercontexts).')';
+ /// sorting out the sort order
+ if ($sort) {
+ $sortby = " ORDER BY $sort ";
} else {
- $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
- $listofcontexts = '('.$sitecontext->id.')'; // must be site
+ $sortby = "";
}
- $roleids = '('.implode(',', $validroleids).')';
-
- $select = ' SELECT '.$fields;
- $from = ' FROM '.$CFG->prefix.'user u LEFT JOIN '.$CFG->prefix.'role_assignments ra ON ra.userid = u.id '.$groupjoin;
- $where = ' WHERE (ra.contextid = '.$context->id.' OR ra.contextid in '.$listofcontexts.') AND u.deleted = 0 AND ra.roleid in '.$roleids.' '.$groupsql;
-
- return get_records_sql($select.$from.$where.$sort, $limitfrom, $limitnum);
+ /// Construct the main SQL
+ $select = " SELECT $fields";
+ $from = " FROM {$CFG->prefix}user u
+ INNER JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id
+ LEFT OUTER JOIN {$CFG->prefix}user_lastaccess ul ON ul.userid = u.id
+ $groupjoin";
+ $where = " WHERE ra.contextid ".get_related_contexts_string($context)."
+ AND u.deleted = 0
+ AND ra.roleid in $roleids
+ $exceptionsql
+ $coursesql
+ $groupsql";
+
+ return get_records_sql($select.$from.$where.$sortby, $limitfrom, $limitnum);
}
* @uses $CFG
* @param string $cutofftime ?
* @return object {@link $USER} records
- * @todo XXX Update for Roles
*/
function get_users_longtimenosee($cutofftime) {
global $CFG;
*
* @param type description
*
- * @todo XXX Convert to Roles
*/
function get_courses($categoryid="all", $sort="c.sortorder ASC", $fields="c.*") {
$categoryselect = "";
if ($categoryid != "all" && is_numeric($categoryid)) {
- $categoryselect = "c.category = '$categoryid'";
+ $categoryselect = "WHERE c.category = '$categoryid'";
+ } else {
+ $categoryselect = "";
+ }
+
+ // pull out all course matching the cat
+ $courses = get_records_sql("SELECT $fields
+ FROM {$CFG->prefix}course c
+ $categoryselect
+ ORDER BY $sort");
+ $visiblecourses = array();
+
+ // loop throught them
+ foreach ($courses as $course) {
+ if ($course->visible <= 0) {
+ // for hidden courses, require visibility check
+ if (has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) {
+ $visiblecourses [] = $course;
+ }
+ } else {
+ $visiblecourses [] = $course;
+ }
}
+ return $visiblecourses;
+/*
$teachertable = "";
$visiblecourses = "";
$sqland = "";
$extrafield = ','.$extrafield;
}
return get_records_sql("SELECT ".((!empty($teachertable)) ? " DISTINCT " : "")." $fields $extrafield FROM $selectsql ".((!empty($sort)) ? "ORDER BY $sort" : ""));
+ */
}
*
* @param type description
*
- * @todo XXX Convert to Roles
*/
function get_courses_page($categoryid="all", $sort="c.sortorder ASC", $fields="c.*",
&$totalcount, $limitfrom="", $limitnum="") {
global $USER, $CFG;
+
+ $categoryselect = "";
+ if ($categoryid != "all" && is_numeric($categoryid)) {
+ $categoryselect = "WHERE c.category = '$categoryid'";
+ } else {
+ $categoryselect = "";
+ }
+
+ // pull out all course matching the cat
+ $courses = get_records_sql("SELECT $fields
+ FROM {$CFG->prefix}course c
+ $categoryselect
+ ORDER BY $sort");
+ $visiblecourses = array();
+ $totalcount = 0;
+
+ if (!$limitnum) {
+ $limitnum = count($courses);
+ }
+
+ if (!limitfrom) {
+ $limitfrom = 0;
+ }
+
+ // iteration will have to be done inside loop to keep track of the limitfrom and limitnum
+ foreach ($courses as $course) {
+ if ($course->visible <= 0) {
+ // for hidden courses, require visibility check
+ if (has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) {
+ $totalcount++;
+ if ($totalcount > $limitfrom && count($visiblecourses) < $limitnum) {
+ $visiblecourses [] = $course;
+ }
+ }
+ } else {
+ $totalcount++;
+ if ($totalcount > $limitfrom && count($visiblecourses) < $limitnum) {
+ $visiblecourses [] = $course;
+ }
+ }
+ }
+
+ return $visiblecourses;
+
+/**
$categoryselect = "";
if ($categoryid != "all" && is_numeric($categoryid)) {
$totalcount = count_records_sql("SELECT COUNT(DISTINCT c.id) FROM $selectsql");
return get_records_sql("SELECT $fields FROM $selectsql ".((!empty($sort)) ? "ORDER BY $sort" : "")." $limit");
+ */
}
* @param string $info Additional description information
* @param string $cm The course_module->id if there is one
* @param string $user If log regards $user other than $USER
- * @todo XXX Convert to Roles
*/
function add_to_log($courseid, $module, $action, $url='', $info='', $cm=0, $user=0) {
// Note that this function intentionally does not follow the normal Moodle DB access idioms.
* @uses SITEID
* @param int $userid The id of the user that is being tested against.
* @return bool
+ * @TODO: remove from cvs
*/
function add_admin($userid) {
-
- if (!record_exists('user_admins', 'userid', $userid)) {
- if (record_exists('user', 'id', $userid)) {
- $admin->userid = $userid;
-
- // any admin is also a teacher on the site course
- if (!record_exists('user_teachers', 'course', SITEID, 'userid', $userid)) {
- if (!add_teacher($userid, SITEID)) {
- return false;
- }
- }
-
- return insert_record('user_admins', $admin);
- }
- return false;
- }
return true;
}
function get_recent_enrolments($courseid, $timestart) {
global $CFG;
+
+ $context = get_context_instance(CONTEXT_COURSE, $courseid);
return get_records_sql("SELECT DISTINCT u.id, u.firstname, u.lastname, l.time
FROM {$CFG->prefix}user u,
- {$CFG->prefix}user_students s,
+ {$CFG->prefix}role_assignments ra,
{$CFG->prefix}log l
WHERE l.time > '$timestart'
AND l.course = '$courseid'
AND l.module = 'course'
AND l.action = 'enrol'
AND l.info = u.id
- AND u.id = s.userid
- AND s.course = '$courseid'
+ AND u.id = ra.userid
+ AND ra.contextid ".get_related_contexts_string($context)."
ORDER BY l.time ASC");
}
* @return object
* @todo Finish documenting this function
*/
-function get_course_students($courseid, $sort='s.timeaccess', $dir='', $page=0, $recordsperpage=99999,
+function get_course_students($courseid, $sort='ul.timeaccess', $dir='', $page=0, $recordsperpage=99999,
$firstinitial='', $lastinitial='', $group=NULL, $search='', $fields='', $exceptions='') {
global $CFG;
$sort = $sort .' '. $dir;
}
// Now we have to make sure site teachers are excluded
- if ($teachers = get_records('user_teachers', 'course', SITEID)) {
+
+ $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
+ if ($teachers = get_users_by_capability($sitecontext, 'moodle/course:update')) {
foreach ($teachers as $teacher) {
$exceptions .= ','. $teacher->userid;
}
- $exceptions = ltrim($exceptions, ',');
- }
+ $exceptions = ltrim($exceptions, ',');
+
+ }
+
return get_users(true, $search, true, $exceptions, $sort, $firstinitial, $lastinitial,
$page, $recordsperpage, $fields ? $fields : '*');
}
$groupmembers = '';
// make sure it works on the site course
- $select = 's.course = \''. $courseid .'\' AND ';
+ $context = get_context_instance(CONTEXT_COURSE, $courseid);
+ $select = "(ul.courseid = '$courseid' OR ISNULL(ul.courseid)) AND ";
if ($courseid == SITEID) {
$select = '';
}
- $select .= 's.userid = u.id AND u.deleted = \'0\' ';
+ $select .= ' u.deleted = \'0\' ';
if (!$fields) {
$fields = 'u.id, u.confirmed, u.username, u.firstname, u.lastname, '.
'u.maildisplay, u.mailformat, u.maildigest, u.email, u.city, '.
'u.country, u.picture, u.idnumber, u.department, u.institution, '.
- 'u.emailstop, u.lang, u.timezone, s.timeaccess as lastaccess';
+ 'u.emailstop, u.lang, u.timezone, ul.timeaccess as lastaccess';
}
if ($search) {
return array();
} else if ($group !== NULL) {
- $groupmembers = ', '. $CFG->prefix .'groups_members gm ';
- $select .= ' AND u.id = gm.userid AND gm.groupid = \''. $group .'\'';
+ $groupmembers = "INNER JOIN {$CFG->prefix}groups_members gm on u.id=gm.userid";
+ $select .= ' AND gm.groupid = \''. $group .'\'';
}
if (!empty($exceptions)) {
}
$students = get_records_sql("SELECT $fields
- FROM {$CFG->prefix}user u,
- {$CFG->prefix}user_students s
+ FROM {$CFG->prefix}user u INNER JOIN
+ {$CFG->prefix}role_assignment ra on u.id=ra.userid LEFT OUTER JOIN
+ {$CFG->prefix}user_lastaccess ul on ul.userid=ra.userid
$groupmembers
WHERE $select $search $sort $dir $limit");
- if ($courseid != SITEID) {
- return $students;
- }
-
+ //if ($courseid != SITEID) {
+ return $students;
+ //}
+/*
// We are here because we need the students for the site.
// These also include teachers on real courses minus those on the site
if ($teachers = get_records('user_teachers', 'course', SITEID)) {
return $teachers;
}
return $teachers + $students;
+ */
}
function get_course_teachers($courseid, $sort='t.authority ASC', $exceptions='') {
global $CFG;
-
- if (!empty($exceptions)) {
- $exceptions = ' AND u.id NOT IN ('. $exceptions .') ';
- }
-
- if (!empty($sort)) {
- $sort = ' ORDER by '.$sort;
- }
-
+
+ $sort = 'ul.timeaccess DESC';
+
+ $context = get_context_instance(CONTEXT_COURSE, $courseid);
+ return get_users_by_capability($context, 'moodle/course:update', 'u.*, ul.timeaccess as lastaccess', $sort, '','','',$exceptions);
+
+ /// some fields will be missing, like authority, editall
+ /*
return get_records_sql("SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, u.maildigest,
u.email, u.city, u.country, u.lastlogin, u.picture, u.lang, u.timezone,
u.emailstop, t.authority,t.role,t.editall,t.timeaccess as lastaccess
{$CFG->prefix}user_teachers t
WHERE t.course = '$courseid' AND t.userid = u.id
AND u.deleted = '0' AND u.confirmed = '1' $exceptions $sort");
+ */
}
/**
* @return object
* @todo Finish documenting this function
*/
-function get_course_users($courseid, $sort='timeaccess DESC', $exceptions='', $fields='') {
-
- /// Using this method because the single SQL is too inefficient
- // Note that this has the effect that teachers and students are
- // sorted individually. Returns first all teachers, then all students
+function get_course_users($courseid, $sort='ul.timeaccess DESC', $exceptions='', $fields='') {
- if (!$teachers = get_course_teachers($courseid, $sort, $exceptions)) {
- $teachers = array();
- }
- if (!$students = get_course_students($courseid, $sort, '', 0, 99999, '', '', NULL, '', $fields, $exceptions)) {
- $students = array();
- }
-
- return $teachers + $students;
+ $context = get_context_instance(CONTEXT_COURSE, $courseid);
+ return get_users_by_capability($context, 'moodle/course:view', 'u.*, ul.timeaccess as lastaccess', $sort, '','','',$exceptions);
}