Skip to content

Commit

Permalink
MDL-10161 - Since get_course_... (users, students, teachers) is still…
Browse files Browse the repository at this point in the history
… being used by a number of functions (such as groups), trying to make them work with the SITE course the way they did pre-1.7, with $CFG->allusersarestudents set to TRUE. This will also have a configuration option that can be used to disable returning large amounts of users for the site course, if a site happens to be using this functionality and they have a large number of users.
  • Loading branch information
mchurch committed Jun 19, 2007
1 parent cba168a commit b069fbd
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 23 deletions.
3 changes: 3 additions & 0 deletions admin/settings/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
get_string('configguestroleid', 'admin'), $guestrole->id, $allroles));
$temp->add(new admin_setting_configselect('defaultuserroleid', get_string('defaultuserroleid', 'admin'),
get_string('configdefaultuserroleid', 'admin'), $userrole->id, $allroles));

$temp->add(new admin_setting_configcheckbox('nodefaultuserrolelists', get_string('nodefaultuserrolelists', 'admin'), get_string('confignodefaultuserrolelists', 'admin'), 0));

$temp->add(new admin_setting_configselect('defaultcourseroleid', get_string('defaultcourseroleid', 'admin'),
get_string('configdefaultcourseroleid', 'admin'), $studentrole->id, $allroles));
$temp->add(new admin_setting_configselect('creatornewroleid', get_string('creatornewroleid', 'admin'),
Expand Down
2 changes: 2 additions & 0 deletions lang/en_utf8/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
$string['configminpasswordnonalphanum'] = 'Passwords must have at least these many non-alphanumeric characters.';
$string['configminpasswordupper'] = 'Passwords must have at least these many upper case letters.';
$string['configmymoodleredirect'] = 'This setting forces redirects to /my on login for non-admins and replaces the top level site breadcrumb with /my';
$string['confignodefaultuserrolelists'] = 'This setting prevents all users from being returned from the database from deprecated calls of get_course_user, etc., for the site course if the default role provides that access. Check this, if you suffer a performance hit.';
$string['confignonmetacoursesyncroleids'] = 'By default all enrolments from child courses are synchronised to metacourses. Roles that are selected here will not be included in the synchronisation process.';
$string['confignoreplyaddress'] = 'Emails are sometimes sent out on behalf of a user (eg forum posts). The email address you specify here will be used as the \"From\" address in those cases when the recipients should not be able to reply directly to the user (eg when a user chooses to keep their address private).';
$string['confignotifyloginfailures'] = 'If login failures have been recorded, email notifications can be sent out. Who should see these notifications?';
Expand Down Expand Up @@ -412,6 +413,7 @@
$string['mysql416bypassed'] = 'However, if your site is using iso-8859-1 (latin) languages ONLY, you may continue using your currently installed MySQL 4.1.12 (or higher).';
$string['mysql416required'] = 'MySQL 4.1.16 is the minimum version required for Moodle 1.6 in order to guarantee that all data can be converted to UTF-8 in the future.';
$string['nobookmarksforuser'] = 'You do not have any bookmarks.';
$string['nodefaultuserrolelists'] = 'Don\'t return all default role users';
$string['nolangupdateneeded'] = 'All your language packs are up to date, no update is needed';
$string['nonewsettings'] = 'No new settings were added during this upgrade.';
$string['nonexistentbookmark'] = 'The bookmark you requested does not exist.';
Expand Down
104 changes: 81 additions & 23 deletions lib/deprecatedlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -578,38 +578,52 @@ function get_course_students($courseid, $sort='ul.timeaccess', $dir='', $page=''

global $CFG;

if ($courseid == SITEID and $CFG->allusersaresitestudents) {
// return users with confirmed, undeleted accounts who are not site teachers
// the following is a mess because of different conventions in the different user functions
$sort = str_replace('s.timeaccess', 'lastaccess', $sort); // site users can't be sorted by timeaccess
$sort = str_replace('timeaccess', 'lastaccess', $sort); // site users can't be sorted by timeaccess
$sort = str_replace('u.', '', $sort); // the get_user function doesn't use the u. prefix to fields
$fields = str_replace('u.', '', $fields);
if ($sort) {
$sort = $sort .' '. $dir;
}
// Now we have to make sure site teachers are excluded
// make sure it works on the site course
$context = get_context_instance(CONTEXT_COURSE, $courseid);

if ($teachers = get_course_teachers(SITEID)) {
foreach ($teachers as $teacher) {
$exceptions .= ','. $teacher->userid;
/// For the site course, old way was to check if $CFG->allusersaresitestudents was set to true.
/// The closest comparible method using roles is if the $CFG->defaultuserroleid is set to the legacy
/// student role. This function should be replaced where it is used with something more meaningful.
if (($courseid == SITEID) && !empty($CFG->defaultuserroleid) && empty($CFG->nodefaultuserrolelists)) {
if ($roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW, $context)) {
$hascap = false;
foreach ($roles as $role) {
if ($role->id == $CFG->defaultuserroleid) {
$hascap = true;
break;
}
}
$exceptions = ltrim($exceptions, ',');

}

return get_users(true, $search, true, $exceptions, $sort, $firstinitial, $lastinitial,
$page, $recordsperpage, $fields ? $fields : '*');
if ($hascap) {
// return users with confirmed, undeleted accounts who are not site teachers
// the following is a mess because of different conventions in the different user functions
$sort = str_replace('s.timeaccess', 'lastaccess', $sort); // site users can't be sorted by timeaccess
$sort = str_replace('timeaccess', 'lastaccess', $sort); // site users can't be sorted by timeaccess
$sort = str_replace('u.', '', $sort); // the get_user function doesn't use the u. prefix to fields
$fields = str_replace('u.', '', $fields);
if ($sort) {
$sort = $sort .' '. $dir;
}
// Now we have to make sure site teachers are excluded

if ($teachers = get_course_teachers(SITEID)) {
foreach ($teachers as $teacher) {
$exceptions .= ','. $teacher->userid;
}
$exceptions = ltrim($exceptions, ',');

}

return get_users(true, $search, true, $exceptions, $sort, $firstinitial, $lastinitial,
$page, $recordsperpage, $fields ? $fields : '*');
}
}
}

$LIKE = sql_ilike();
$fullname = sql_fullname('u.firstname','u.lastname');

$groupmembers = '';

// make sure it works on the site course
$context = get_context_instance(CONTEXT_COURSE, $courseid);

$select = "c.contextlevel=".CONTEXT_COURSE." AND "; // Must be on a course
if ($courseid != SITEID) {
// If not site, require specific course
Expand Down Expand Up @@ -706,6 +720,28 @@ function get_course_teachers($courseid, $sort='t.authority ASC', $exceptions='')
$sort = 'ul.timeaccess DESC';

$context = get_context_instance(CONTEXT_COURSE, $courseid);

/// For the site course, if the $CFG->defaultuserroleid is set to the legacy teacher role, then all
/// users are teachers. This function should be replaced where it is used with something more
/// meaningful.
if (($courseid == SITEID) && !empty($CFG->defaultuserroleid) && empty($CFG->nodefaultuserrolelists)) {
if ($roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW, $context)) {
$hascap = false;
foreach ($roles as $role) {
if ($role->id == $CFG->defaultuserroleid) {
$hascap = true;
break;
}
}
if ($hascap) {
if (empty($fields)) {
$fields = '*';
}
return get_users(true, '', true, $exceptions, 'lastname ASC', '', '', '', '', $fields);
}
}
}

return get_users_by_capability($context, 'moodle/course:update', 'u.*, ul.timeaccess as lastaccess, ra.hidden', $sort, '','','',$exceptions, false);
/// some fields will be missing, like authority, editall
/*
Expand All @@ -730,8 +766,30 @@ function get_course_teachers($courseid, $sort='t.authority ASC', $exceptions='')
* @todo Finish documenting this function
*/
function get_course_users($courseid, $sort='ul.timeaccess DESC', $exceptions='', $fields='') {
global $CFG;

$context = get_context_instance(CONTEXT_COURSE, $courseid);

/// If the course id is the SITEID, we need to return all the users if the "defaultuserroleid"
/// has the capbility of accessing the site course. $CFG->nodefaultuserrolelists set to true can
/// over-rule using this.
if (($courseid == SITEID) && !empty($CFG->defaultuserroleid) && empty($CFG->nodefaultuserrolelists)) {
if ($roles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $context)) {
$hascap = false;
foreach ($roles as $role) {
if ($role->id == $CFG->defaultuserroleid) {
$hascap = true;
break;
}
}
if ($hascap) {
if (empty($fields)) {
$fields = '*';
}
return get_users(true, '', true, $exceptions, 'lastname ASC', '', '', '', '', $fields);
}
}
}
return get_users_by_capability($context, 'moodle/course:view', 'u.*, ul.timeaccess as lastaccess', $sort, '','','',$exceptions, false);

}
Expand Down

0 comments on commit b069fbd

Please sign in to comment.