mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
Merged from HEAD
This commit is contained in:
parent
e6e5113794
commit
2700d113a2
2 changed files with 84 additions and 124 deletions
190
lib/datalib.php
190
lib/datalib.php
|
@ -36,7 +36,7 @@ function execute_sql($command, $feedback=true) {
|
|||
/**
|
||||
* Run an arbitrary sequence of semicolon-delimited SQL commands
|
||||
*
|
||||
* Assumes that the input text (file or string consists of
|
||||
* Assumes that the input text (file or string) consists of
|
||||
* a number of SQL statements ENDING WITH SEMICOLONS. The
|
||||
* semicolons MUST be the last character in a line.
|
||||
* Lines that are blank or that start with "#" are ignored.
|
||||
|
@ -49,7 +49,7 @@ function modify_database($sqlfile="", $sqlstring="") {
|
|||
|
||||
global $CFG;
|
||||
|
||||
$success = true; // Let's be optimistic :-)
|
||||
$success = true; // Let's be optimistic
|
||||
|
||||
if (!empty($sqlfile)) {
|
||||
if (!is_readable($sqlfile)) {
|
||||
|
@ -1134,6 +1134,27 @@ function get_course_students($courseid, $sort="s.timeaccess", $dir="", $page=0,
|
|||
|
||||
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
|
||||
if ($teachers = get_records('user_teachers', 'course', 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 : '*');
|
||||
}
|
||||
|
||||
switch ($CFG->dbtype) {
|
||||
case "mysql":
|
||||
$fullname = " CONCAT(firstname,\" \",lastname) ";
|
||||
|
@ -1196,11 +1217,36 @@ function get_course_students($courseid, $sort="s.timeaccess", $dir="", $page=0,
|
|||
$sort = " ORDER BY $sort ";
|
||||
}
|
||||
|
||||
return get_records_sql("SELECT $fields
|
||||
$students = get_records_sql("SELECT $fields
|
||||
FROM {$CFG->prefix}user u,
|
||||
{$CFG->prefix}user_students s
|
||||
$groupmembers
|
||||
WHERE $select $search $sort $dir $limit");
|
||||
|
||||
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)) {
|
||||
foreach ($teachers as $teacher) {
|
||||
$exceptions .= ",$teacher->userid";
|
||||
}
|
||||
$exceptions = ltrim($exceptions, ',');
|
||||
$select .= " AND u.id NOT IN ($exceptions)";
|
||||
}
|
||||
if (!$teachers = get_records_sql("SELECT $fields
|
||||
FROM {$CFG->prefix}user u,
|
||||
{$CFG->prefix}user_teachers s
|
||||
$groupmembers
|
||||
WHERE $select $search $sort $dir $limit")) {
|
||||
return $students;
|
||||
}
|
||||
if (!$students) {
|
||||
return $teachers;
|
||||
}
|
||||
return $teachers + $students;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1210,52 +1256,10 @@ function get_course_students($courseid, $sort="s.timeaccess", $dir="", $page=0,
|
|||
*/
|
||||
function count_course_students($course, $search="", $firstinitial="", $lastinitial="", $group=NULL, $exceptions='') {
|
||||
|
||||
global $CFG;
|
||||
|
||||
if (!$course->category) {
|
||||
return count(get_site_users($sort, '', $exceptions));
|
||||
if ($students = get_course_students($course->id, '', '', 0, 999999, $firstinitial, $lastinitial, $group, $search, '', $exceptions)) {
|
||||
return count($students);
|
||||
}
|
||||
|
||||
switch ($CFG->dbtype) {
|
||||
case "mysql":
|
||||
$fullname = " CONCAT(firstname,\" \",lastname) ";
|
||||
$LIKE = "LIKE";
|
||||
break;
|
||||
default:
|
||||
$fullname = " firstname||\' \'||lastname ";
|
||||
$LIKE = "ILIKE";
|
||||
}
|
||||
|
||||
$groupmembers = "";
|
||||
|
||||
$select = "s.course = '$course->id' AND s.userid = u.id AND u.deleted = '0'";
|
||||
|
||||
if ($search) {
|
||||
$search = " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%') ";
|
||||
}
|
||||
if ($firstinitial) {
|
||||
$select .= " AND u.firstname $LIKE '$firstinitial%'";
|
||||
}
|
||||
if ($lastinitial) {
|
||||
$select .= " AND u.lastname $LIKE '$lastinitial%'";
|
||||
}
|
||||
|
||||
if ($group === 0) { /// Need something here to get all students not in a group
|
||||
return 0;
|
||||
|
||||
} else if ($group !== NULL) {
|
||||
$groupmembers = ", {$CFG->prefix}groups_members gm ";
|
||||
$select .= " AND u.id = gm.userid AND gm.groupid = '$group'";
|
||||
}
|
||||
|
||||
if (!empty($exceptions)) {
|
||||
$select .= " AND u.id NOT IN ($exceptions)";
|
||||
}
|
||||
|
||||
return count_records_sql("SELECT COUNT(*)
|
||||
FROM {$CFG->prefix}user u,
|
||||
{$CFG->prefix}user_students s $groupmembers
|
||||
WHERE $select");
|
||||
}
|
||||
|
||||
|
||||
|
@ -1280,7 +1284,8 @@ function get_course_teachers($courseid, $sort="t.authority ASC", $exceptions='')
|
|||
u.emailstop, t.authority,t.role,t.editall,t.timeaccess as lastaccess
|
||||
FROM {$CFG->prefix}user u,
|
||||
{$CFG->prefix}user_teachers t
|
||||
WHERE t.course = '$courseid' AND t.userid = u.id AND u.deleted = '0' $except
|
||||
WHERE t.course = '$courseid' AND t.userid = u.id
|
||||
AND u.deleted = '0' AND u.confirmed = '1' $except
|
||||
ORDER BY $sort");
|
||||
}
|
||||
|
||||
|
@ -1304,18 +1309,12 @@ function get_course_users($courseid, $sort="timeaccess DESC", $exceptions='') {
|
|||
|
||||
return $teachers + $students;
|
||||
|
||||
// This is too inefficient on large sites.
|
||||
// return get_records_sql("SELECT DISTINCT u.*
|
||||
// FROM mdl_user u
|
||||
// LEFT JOIN mdl_user_students s ON s.course = '$courseid'
|
||||
// LEFT JOIN mdl_user_teachers t ON t.course = '$courseid'
|
||||
// WHERE (u.id = t.userid OR u.id = s.userid)
|
||||
// ORDER BY $sort");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Search through course users
|
||||
* If used for the site course searches through all undeleted, confirmed users
|
||||
*
|
||||
* @param type description
|
||||
*/
|
||||
|
@ -1348,47 +1347,28 @@ function search_users($courseid, $groupid, $searchtext, $sort='', $exceptions=''
|
|||
$order = '';
|
||||
}
|
||||
|
||||
if (!$courseid or $courseid == SITEID) {
|
||||
if (!$admins = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
|
||||
FROM {$CFG->prefix}user u,
|
||||
{$CFG->prefix}user_admins s
|
||||
WHERE s.userid = u.id AND u.deleted = '0'
|
||||
AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
|
||||
$except $order")) {
|
||||
$admins = array();
|
||||
}
|
||||
if (!$teachers = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
|
||||
FROM {$CFG->prefix}user u,
|
||||
{$CFG->prefix}user_teachers s
|
||||
WHERE s.userid = u.id AND u.deleted = '0'
|
||||
AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
|
||||
$except $order")) {
|
||||
$teachers = array();
|
||||
}
|
||||
if (!$students = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
|
||||
FROM {$CFG->prefix}user u,
|
||||
{$CFG->prefix}user_students s
|
||||
WHERE s.userid = u.id AND u.deleted = '0'
|
||||
AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
|
||||
$except $order")) {
|
||||
$students = array();
|
||||
}
|
||||
return $admins + $teachers + $students;
|
||||
$select = "u.deleted = '0' AND u.confirmed = '1'";
|
||||
|
||||
if (!$courseid or $courseid == SITEID) {
|
||||
return get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
|
||||
FROM {$CFG->prefix}user u
|
||||
WHERE $select
|
||||
AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
|
||||
$except $order");
|
||||
} else {
|
||||
|
||||
if ($groupid) {
|
||||
return get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
|
||||
FROM {$CFG->prefix}user u,
|
||||
{$CFG->prefix}groups_members g
|
||||
WHERE g.groupid = '$groupid' AND g.userid = u.id AND u.deleted = '0'
|
||||
WHERE $select AND g.groupid = '$groupid' AND g.userid = u.id
|
||||
AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
|
||||
$except $order");
|
||||
} else {
|
||||
if (!$teachers = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
|
||||
FROM {$CFG->prefix}user u,
|
||||
{$CFG->prefix}user_teachers s
|
||||
WHERE s.course = '$courseid' AND s.userid = u.id AND u.deleted = '0'
|
||||
WHERE $select AND s.course = '$courseid' AND s.userid = u.id
|
||||
AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
|
||||
$except $order")) {
|
||||
$teachers = array();
|
||||
|
@ -1396,7 +1376,7 @@ function search_users($courseid, $groupid, $searchtext, $sort='', $exceptions=''
|
|||
if (!$students = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
|
||||
FROM {$CFG->prefix}user u,
|
||||
{$CFG->prefix}user_students s
|
||||
WHERE s.course = '$courseid' AND s.userid = u.id AND u.deleted = '0'
|
||||
WHERE $select AND s.course = '$courseid' AND s.userid = u.id
|
||||
AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
|
||||
$except $order")) {
|
||||
$students = array();
|
||||
|
@ -1406,46 +1386,16 @@ function search_users($courseid, $groupid, $searchtext, $sort='', $exceptions=''
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a list of all active users who are enrolled
|
||||
*
|
||||
* or teaching in courses on this server
|
||||
* Returns a list of all site users
|
||||
* Obsolete, just calls get_course_users(SITEID)
|
||||
*
|
||||
* @param type description
|
||||
*/
|
||||
function get_site_users($sort="u.lastaccess DESC", $select="", $exceptions='') {
|
||||
|
||||
global $CFG;
|
||||
|
||||
if (!empty($exceptions)) {
|
||||
$except = " AND u.id NOT IN ($exceptions) ";
|
||||
} else {
|
||||
$except = '';
|
||||
}
|
||||
|
||||
if ($select) {
|
||||
$selectinfo = $select;
|
||||
} else {
|
||||
$selectinfo = "u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, u.maildigest,".
|
||||
"u.email, u.emailstop, u.city, u.country, u.lastaccess, u.lastlogin, u.picture, u.lang, u.timezone";
|
||||
}
|
||||
|
||||
if (!$students = get_records_sql("SELECT DISTINCT $selectinfo from {$CFG->prefix}user u, {$CFG->prefix}user_students s
|
||||
WHERE s.userid = u.id $except ORDER BY $sort")) {
|
||||
$students = array();
|
||||
}
|
||||
|
||||
if (!$teachers = get_records_sql("SELECT DISTINCT $selectinfo from {$CFG->prefix}user u, {$CFG->prefix}user_teachers t
|
||||
WHERE t.userid = u.id $except ORDER BY $sort")) {
|
||||
$teachers = array();
|
||||
}
|
||||
|
||||
if (!$admins = get_records_sql("SELECT DISTINCT $selectinfo from {$CFG->prefix}user u, {$CFG->prefix}user_admins a
|
||||
WHERE a.userid = u.id $except ORDER BY $sort")) {
|
||||
$admins = array();
|
||||
}
|
||||
|
||||
return $admins + $teachers + $students;
|
||||
return get_course_users(SITEID, $sort, '', 0, 999999, '', '', NULL, '', $select, $exceptions);
|
||||
}
|
||||
|
||||
|
||||
|
@ -2323,6 +2273,8 @@ function get_logs_usercourse($userid, $courseid, $coursestart) {
|
|||
|
||||
if ($courseid) {
|
||||
$courseselect = " AND course = '$courseid' ";
|
||||
} else {
|
||||
$courseselect = '';
|
||||
}
|
||||
|
||||
return get_records_sql("SELECT floor((`time` - $coursestart)/86400) as day, count(*) as num
|
||||
|
@ -2344,6 +2296,8 @@ function get_logs_userday($userid, $courseid, $daystart) {
|
|||
|
||||
if ($courseid) {
|
||||
$courseselect = " AND course = '$courseid' ";
|
||||
} else {
|
||||
$courseselect = '';
|
||||
}
|
||||
|
||||
return get_records_sql("SELECT floor((`time` - $daystart)/3600) as hour, count(*) as num
|
||||
|
|
|
@ -579,7 +579,7 @@ function isstudent($courseid, $userid=0) {
|
|||
/// If course is site, is the user a confirmed user on the site?
|
||||
global $USER;
|
||||
|
||||
if (empty($USER->id)) {
|
||||
if (empty($USER->id) and !$userid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -590,7 +590,13 @@ function isstudent($courseid, $userid=0) {
|
|||
if (isguest($userid)) {
|
||||
return false;
|
||||
}
|
||||
return record_exists('user_students', 'userid', $userid);
|
||||
if ($CFG->allusersaresitestudents) {
|
||||
return record_exists('user', 'id', $userid);
|
||||
} else {
|
||||
return (record_exists('user_students', 'userid', $userid)
|
||||
or (record_exists('user_teachers', 'userid', $userid)
|
||||
and !record_exists('user_teachers', 'userid', $userid, 'course', SITEID)));
|
||||
}
|
||||
}
|
||||
|
||||
if (!$userid) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue