From 71dea306b4d14c28ce4fbda040289405b890e087 Mon Sep 17 00:00:00 2001 From: toyomoyo Date: Fri, 15 Sep 2006 09:08:48 +0000 Subject: [PATCH] changing lib functions to work with roles, some functions might not work as expected now --- lib/accesslib.php | 52 +++++++++++++++-------- lib/datalib.php | 76 +++++++++++++++++++++++++++++++--- lib/deprecatedlib.php | 96 ++++++++++++++++++------------------------- 3 files changed, 147 insertions(+), 77 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index d19100a628..d9b6421440 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -2288,14 +2288,16 @@ function get_overridable_roles ($context) { * @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).')'; @@ -2307,7 +2309,18 @@ function get_users_by_capability($context, $capability, $fields='u.*', $sort='', $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) { @@ -2315,23 +2328,30 @@ function get_users_by_capability($context, $capability, $fields='u.*', $sort='', 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); } diff --git a/lib/datalib.php b/lib/datalib.php index a56e3e1e1a..2470df313c 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -330,7 +330,6 @@ function get_users_unconfirmed($cutofftime=2000000000) { * @uses $CFG * @param string $cutofftime ? * @return object {@link $USER} records - * @todo XXX Update for Roles */ function get_users_longtimenosee($cutofftime) { global $CFG; @@ -475,7 +474,6 @@ function get_site() { * * @param type description * - * @todo XXX Convert to Roles */ function get_courses($categoryid="all", $sort="c.sortorder ASC", $fields="c.*") { @@ -483,9 +481,32 @@ 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 = ""; @@ -514,6 +535,7 @@ function get_courses($categoryid="all", $sort="c.sortorder ASC", $fields="c.*") $extrafield = ','.$extrafield; } return get_records_sql("SELECT ".((!empty($teachertable)) ? " DISTINCT " : "")." $fields $extrafield FROM $selectsql ".((!empty($sort)) ? "ORDER BY $sort" : "")); + */ } @@ -527,12 +549,56 @@ function get_courses($categoryid="all", $sort="c.sortorder ASC", $fields="c.*") * * @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)) { @@ -565,6 +631,7 @@ function get_courses_page($categoryid="all", $sort="c.sortorder ASC", $fields="c $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"); + */ } @@ -1213,7 +1280,6 @@ function instance_is_visible($moduletype, $module) { * @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. diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index ec16fd636b..24bee40748 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -510,24 +510,9 @@ function remove_creator($userid) { * @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; } @@ -619,18 +604,20 @@ function get_teacher($courseid) { 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"); } @@ -654,7 +641,7 @@ function get_recent_enrolments($courseid, $timestart) { * @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; @@ -670,12 +657,16 @@ function get_course_students($courseid, $sort='s.timeaccess', $dir='', $page=0, $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 : '*'); } @@ -687,18 +678,19 @@ function get_course_students($courseid, $sort='s.timeaccess', $dir='', $page=0, $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) { @@ -717,8 +709,8 @@ function get_course_students($courseid, $sort='s.timeaccess', $dir='', $page=0, 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)) { @@ -730,15 +722,16 @@ function get_course_students($courseid, $sort='s.timeaccess', $dir='', $page=0, } $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)) { @@ -759,6 +752,7 @@ function get_course_students($courseid, $sort='s.timeaccess', $dir='', $page=0, return $teachers; } return $teachers + $students; + */ } @@ -799,15 +793,14 @@ function count_course_students($course, $search='', $firstinitial='', $lastiniti 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 @@ -815,6 +808,7 @@ function get_course_teachers($courseid, $sort='t.authority ASC', $exceptions='') {$CFG->prefix}user_teachers t WHERE t.course = '$courseid' AND t.userid = u.id AND u.deleted = '0' AND u.confirmed = '1' $exceptions $sort"); + */ } /** @@ -827,20 +821,10 @@ function get_course_teachers($courseid, $sort='t.authority ASC', $exceptions='') * @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); } -- 2.39.5