2005-03-22 18:10:06 +00:00
|
|
|
<?php // $Id$
|
2008-04-15 21:46:04 +00:00
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Library of functions for database manipulation.
|
2007-08-02 23:39:28 +00:00
|
|
|
*
|
2004-09-23 02:48:41 +00:00
|
|
|
* Other main libraries:
|
|
|
|
* - weblib.php - functions that produce web output
|
|
|
|
* - moodlelib.php - general-purpose Moodle functions
|
2004-09-29 09:12:55 +00:00
|
|
|
* @author Martin Dougiamas and many others
|
2004-09-25 05:29:21 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
2004-09-23 02:48:41 +00:00
|
|
|
* @package moodlecore
|
|
|
|
*/
|
|
|
|
|
2008-06-16 14:25:53 +00:00
|
|
|
define('MAX_COURSES_IN_CATEGORY', 10000); // MAX_COURSES_IN_CATEGORY * MAX_COURSE_CATEGORIES must not be more than max integer!
|
|
|
|
define('MAX_COURSE_CATEGORIES', 10000);
|
|
|
|
|
2008-06-15 11:35:25 +00:00
|
|
|
/**
|
|
|
|
* Sets up global $DB moodle_database instance
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function setup_DB() {
|
|
|
|
global $CFG, $DB;
|
|
|
|
|
|
|
|
if (isset($DB)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($CFG->dbuser)) {
|
|
|
|
$CFG->dbuser = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($CFG->dbpass)) {
|
|
|
|
$CFG->dbpass = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($CFG->dbname)) {
|
|
|
|
$CFG->dbname = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($CFG->dbpersist)) {
|
|
|
|
$CFG->dbpersist = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($CFG->dblibrary)) {
|
|
|
|
$CFG->dblibrary = 'adodb';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($CFG->dboptions)) {
|
|
|
|
$CFG->dboptions = array();
|
|
|
|
}
|
|
|
|
|
2008-06-20 07:05:46 +00:00
|
|
|
$classname = $CFG->dbtype.'_'.$CFG->dblibrary.'_moodle_database';
|
|
|
|
require_once($CFG->libdir.'/dml/'.$classname.'.php');
|
|
|
|
$DB = new $classname();
|
2008-06-15 11:35:25 +00:00
|
|
|
|
|
|
|
$CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now
|
|
|
|
|
|
|
|
$driverstatus = $DB->driver_installed();
|
|
|
|
|
|
|
|
if ($driverstatus !== true) {
|
|
|
|
print_error('dbdriverproblem', 'error', '', $driverstatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debugging('', DEBUG_ALL)) {
|
|
|
|
// catch errors
|
|
|
|
ob_start();
|
|
|
|
} else {
|
|
|
|
$prevdebug = error_reporting(0);
|
|
|
|
}
|
|
|
|
if (!$DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->dbpersist, $CFG->prefix, $CFG->dboptions)) {
|
|
|
|
if (debugging('', DEBUG_ALL)) {
|
|
|
|
if ($dberr = ob_get_contents()) {
|
|
|
|
$dberr = '<p><em>'.$dberr.'</em></p>';
|
|
|
|
}
|
|
|
|
ob_end_clean();
|
|
|
|
} else {
|
|
|
|
$dberr = '';
|
|
|
|
}
|
|
|
|
if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) {
|
2008-07-02 04:54:37 +00:00
|
|
|
if (file_exists($CFG->dataroot.'/emailcount')){
|
|
|
|
$fp = fopen($CFG->dataroot.'/emailcount', 'r');
|
|
|
|
$content = fread($fp, 24);
|
2008-07-09 10:35:25 +00:00
|
|
|
fclose($fp);
|
2008-07-02 04:54:37 +00:00
|
|
|
if((time() - (int)$content) > 600){
|
|
|
|
@mail($CFG->emailconnectionerrorsto,
|
|
|
|
'WARNING: Database connection error: '.$CFG->wwwroot,
|
|
|
|
'Connection error: '.$CFG->wwwroot);
|
|
|
|
$fp = fopen($CFG->dataroot.'/emailcount', 'w');
|
|
|
|
fwrite($fp, time());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
@mail($CFG->emailconnectionerrorsto,
|
|
|
|
'WARNING: Database connection error: '.$CFG->wwwroot,
|
|
|
|
'Connection error: '.$CFG->wwwroot);
|
|
|
|
$fp = fopen($CFG->dataroot.'/emailcount', 'w');
|
|
|
|
fwrite($fp, time());
|
|
|
|
}
|
2008-06-15 11:35:25 +00:00
|
|
|
}
|
|
|
|
print_error('dbconnectionfailed', 'error', '', $dberr);
|
|
|
|
}
|
|
|
|
if (debugging('', DEBUG_ALL)) {
|
|
|
|
ob_end_clean();
|
|
|
|
} else {
|
|
|
|
error_reporting($prevdebug);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-04-15 21:46:04 +00:00
|
|
|
/// Some constants
|
|
|
|
define('LASTACCESS_UPDATE_SECS', 60); /// Number of seconds to wait before
|
|
|
|
/// updating lastaccess information in DB.
|
2002-12-17 04:41:18 +00:00
|
|
|
|
|
|
|
/// USER DATABASE ////////////////////////////////////////////////
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2004-09-24 21:28:22 +00:00
|
|
|
* Returns $user object of the main admin user
|
2006-09-01 09:25:34 +00:00
|
|
|
* primary admin = admin with lowest role_assignment id among admins
|
2004-09-24 21:28:22 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @return object(admin) An associative array representing the admin user.
|
|
|
|
*/
|
2002-12-17 04:41:18 +00:00
|
|
|
function get_admin () {
|
|
|
|
|
|
|
|
global $CFG;
|
2007-09-19 07:22:01 +00:00
|
|
|
static $myadmin;
|
|
|
|
|
|
|
|
if (isset($myadmin)) {
|
|
|
|
return $myadmin;
|
|
|
|
}
|
2002-12-17 04:41:18 +00:00
|
|
|
|
|
|
|
if ( $admins = get_admins() ) {
|
|
|
|
foreach ($admins as $admin) {
|
2007-09-19 07:22:01 +00:00
|
|
|
$myadmin = $admin;
|
2004-09-21 11:41:58 +00:00
|
|
|
return $admin; // ie the first one
|
2002-12-17 04:41:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2008-01-06 23:24:50 +00:00
|
|
|
* Returns list of all admins, using 1 DB query. It depends on DB schema v1.7
|
|
|
|
* but does not depend on the v1.9 datastructures (context.path, etc).
|
2004-09-24 21:28:22 +00:00
|
|
|
*
|
|
|
|
* @uses $CFG
|
2005-07-12 02:23:58 +00:00
|
|
|
* @return object
|
2004-09-24 21:28:22 +00:00
|
|
|
*/
|
2002-12-17 04:41:18 +00:00
|
|
|
function get_admins() {
|
2008-05-25 09:39:02 +00:00
|
|
|
global $DB;
|
2007-08-02 23:39:28 +00:00
|
|
|
|
2008-01-06 23:24:50 +00:00
|
|
|
$sql = "SELECT ra.userid, SUM(rc.permission) AS permission, MIN(ra.id) AS adminid
|
2008-05-25 09:39:02 +00:00
|
|
|
FROM {role_capabilities} rc
|
|
|
|
JOIN {context} ctx ON ctx.id=rc.contextid
|
|
|
|
JOIN {role_assignments} ra ON ra.roleid=rc.roleid AND ra.contextid=ctx.id
|
2008-05-30 16:51:01 +00:00
|
|
|
WHERE ctx.contextlevel=10 AND rc.capability IN (?, ?, ?)
|
2008-05-25 09:39:02 +00:00
|
|
|
GROUP BY ra.userid
|
2008-01-06 23:24:50 +00:00
|
|
|
HAVING SUM(rc.permission) > 0";
|
2008-05-25 09:39:02 +00:00
|
|
|
$params = array('moodle/site:config', 'moodle/legacy:admin', 'moodle/site:doanything');
|
2008-01-06 23:24:50 +00:00
|
|
|
|
|
|
|
$sql = "SELECT u.*, ra.adminid
|
2008-05-25 09:39:02 +00:00
|
|
|
FROM {user} u
|
|
|
|
JOIN ($sql) ra
|
|
|
|
ON u.id=ra.userid
|
|
|
|
ORDER BY ra.adminid ASC";
|
2007-08-02 23:39:28 +00:00
|
|
|
|
2008-05-25 09:39:02 +00:00
|
|
|
return $DB->get_records_sql($sql, $params);
|
2002-12-17 04:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-24 22:21:28 +00:00
|
|
|
function get_courses_in_metacourse($metacourseid) {
|
2008-05-25 09:39:02 +00:00
|
|
|
global $DB;
|
2005-01-24 22:21:28 +00:00
|
|
|
|
2008-05-25 09:39:02 +00:00
|
|
|
$sql = "SELECT c.id, c.shortname, c.fullname
|
|
|
|
FROM {course} c, {course_meta} mc
|
|
|
|
WHERE mc.parent_course = ? AND mc.child_course = c.id
|
|
|
|
ORDER BY c.shortname";
|
|
|
|
$params = array($metacourseid);
|
2005-01-24 22:21:28 +00:00
|
|
|
|
2008-05-25 09:39:02 +00:00
|
|
|
return $DB->get_records_sql($sql, $params);
|
2005-01-24 22:21:28 +00:00
|
|
|
}
|
|
|
|
|
2008-05-25 09:39:02 +00:00
|
|
|
function get_courses_notin_metacourse($metacourseid) {
|
|
|
|
global $DB;
|
2005-01-24 22:21:28 +00:00
|
|
|
|
2008-05-25 09:39:02 +00:00
|
|
|
if ($alreadycourses = get_courses_in_metacourse($metacourseid)) {
|
|
|
|
$alreadycourses = implode(',',array_keys($alreadycourses));
|
|
|
|
$alreadycourses = "AND c.id NOT IN ($alreadycourses)";
|
2005-03-03 04:41:46 +00:00
|
|
|
} else {
|
2008-05-25 09:39:02 +00:00
|
|
|
$alreadycourses = "";
|
2005-01-24 22:21:28 +00:00
|
|
|
}
|
2005-02-07 09:22:07 +00:00
|
|
|
|
2008-05-25 09:39:02 +00:00
|
|
|
$sql = "SELECT c.id,c.shortname,c.fullname
|
|
|
|
FROM {course} c
|
|
|
|
WHERE c.id != ? and c.id != ".SITEID." and c.metacourse != 1
|
|
|
|
$alreadycourses
|
|
|
|
ORDER BY c.shortname";
|
|
|
|
$params = array($metacourseid);
|
2007-08-02 23:39:28 +00:00
|
|
|
|
2008-05-25 09:39:02 +00:00
|
|
|
return $DB->get_records_sql($sql, $params);
|
2005-01-24 22:21:28 +00:00
|
|
|
}
|
|
|
|
|
2006-11-09 21:12:31 +00:00
|
|
|
function count_courses_notin_metacourse($metacourseid) {
|
2008-05-25 09:39:02 +00:00
|
|
|
global $DB;
|
2006-11-09 21:12:31 +00:00
|
|
|
|
2008-05-25 09:39:02 +00:00
|
|
|
if ($alreadycourses = get_courses_in_metacourse($metacourseid)) {
|
|
|
|
$alreadycourses = implode(',',array_keys($alreadycourses));
|
|
|
|
$alreadycourses = "AND c.id NOT IN ($alreadycourses)";
|
|
|
|
} else {
|
|
|
|
$alreadycourses = "";
|
2006-11-09 21:12:31 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 16:51:01 +00:00
|
|
|
$sql = "SELECT COUNT(c.id)
|
2008-05-25 09:39:02 +00:00
|
|
|
FROM {course} c
|
|
|
|
WHERE c.id != ? and c.id != ".SITEID." and c.metacourse != 1
|
|
|
|
$alreadycourses";
|
|
|
|
$params = array($metacourseid);
|
|
|
|
|
|
|
|
return $DB->count_records_sql($sql, $params);
|
2006-11-09 21:12:31 +00:00
|
|
|
}
|
|
|
|
|
2004-08-21 12:41:40 +00:00
|
|
|
/**
|
2004-09-24 21:28:22 +00:00
|
|
|
* Search through course users
|
|
|
|
*
|
2007-08-02 23:39:28 +00:00
|
|
|
* If $coursid specifies the site course then this function searches
|
2004-09-24 21:28:22 +00:00
|
|
|
* through all undeleted and confirmed users
|
|
|
|
*
|
|
|
|
* @param int $courseid The course in question.
|
|
|
|
* @param int $groupid The group in question.
|
|
|
|
* @param string $searchtext ?
|
|
|
|
* @param string $sort ?
|
2008-05-25 09:39:02 +00:00
|
|
|
* @param array $exceptions ?
|
2005-07-12 02:23:58 +00:00
|
|
|
* @return object
|
2004-09-24 21:28:22 +00:00
|
|
|
*/
|
2008-05-25 09:39:02 +00:00
|
|
|
function search_users($courseid, $groupid, $searchtext, $sort='', array $exceptions=null) {
|
|
|
|
global $DB;
|
2004-01-31 03:30:31 +00:00
|
|
|
|
2008-06-09 19:48:24 +00:00
|
|
|
$LIKE = $DB->sql_ilike();
|
|
|
|
$fullname = $DB->sql_fullname('u.firstname', 'u.lastname');
|
2004-09-21 11:41:58 +00:00
|
|
|
|
2004-08-21 12:41:40 +00:00
|
|
|
if (!empty($exceptions)) {
|
2008-05-25 09:39:02 +00:00
|
|
|
list($exceptions, $params) = $DB->get_in_or_equal($exceptions, SQL_PARAMS_NAMED, 'ex0000', false);
|
|
|
|
$except = "AND u.id $exceptions";
|
2004-08-21 12:41:40 +00:00
|
|
|
} else {
|
2008-05-25 09:39:02 +00:00
|
|
|
$except = "";
|
|
|
|
$params = array();
|
2004-08-21 12:41:40 +00:00
|
|
|
}
|
2004-08-30 17:27:00 +00:00
|
|
|
|
2004-08-21 12:41:40 +00:00
|
|
|
if (!empty($sort)) {
|
2008-05-25 09:39:02 +00:00
|
|
|
$order = "ORDER BY $sort";
|
2004-08-21 12:41:40 +00:00
|
|
|
} else {
|
2008-05-25 09:39:02 +00:00
|
|
|
$order = "";
|
2004-08-21 12:41:40 +00:00
|
|
|
}
|
2004-09-21 11:41:58 +00:00
|
|
|
|
2008-05-25 09:39:02 +00:00
|
|
|
$select = "u.deleted = 0 AND u.confirmed = 1 AND ($fullname $LIKE :search1 OR u.email $LIKE :search2)";
|
|
|
|
$params['search1'] = "%$searchtext%";
|
|
|
|
$params['search2'] = "%$searchtext%";
|
2004-08-30 17:27:00 +00:00
|
|
|
|
2004-08-29 14:15:40 +00:00
|
|
|
if (!$courseid or $courseid == SITEID) {
|
2008-05-25 09:39:02 +00:00
|
|
|
$sql = "SELECT u.id, u.firstname, u.lastname, u.email
|
|
|
|
FROM {user} u
|
|
|
|
WHERE $select
|
|
|
|
$except
|
|
|
|
$order";
|
|
|
|
return $DB->get_records_sql($sql, $params);
|
2004-08-30 17:27:00 +00:00
|
|
|
|
2008-05-25 09:39:02 +00:00
|
|
|
} else {
|
2004-08-21 12:41:40 +00:00
|
|
|
if ($groupid) {
|
2008-05-25 09:39:02 +00:00
|
|
|
$sql = "SELECT u.id, u.firstname, u.lastname, u.email
|
|
|
|
FROM {user} u
|
|
|
|
JOIN {groups_members} gm ON gm.userid = u.id
|
|
|
|
WHERE $select AND gm.groupid = :groupid
|
|
|
|
$except
|
|
|
|
$order";
|
|
|
|
$params['groupid'] = $groupid;
|
|
|
|
return $DB->get_records_sql($sql, $params);
|
|
|
|
|
2004-08-21 12:41:40 +00:00
|
|
|
} else {
|
2006-09-14 09:08:07 +00:00
|
|
|
$context = get_context_instance(CONTEXT_COURSE, $courseid);
|
|
|
|
$contextlists = get_related_contexts_string($context);
|
2008-05-25 09:39:02 +00:00
|
|
|
|
|
|
|
$sql = "SELECT u.id, u.firstname, u.lastname, u.email
|
|
|
|
FROM {user} u
|
|
|
|
JOIN {role_assignments} ra ON ra.userid = u.id
|
|
|
|
WHERE $select AND ra.contextid $contextlists
|
|
|
|
$except
|
|
|
|
$order";
|
|
|
|
return $DB->get_records_sql($sql, $params);
|
2004-08-21 12:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
2002-12-17 04:41:18 +00:00
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2004-09-24 21:28:22 +00:00
|
|
|
* Returns a subset of users
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
2005-07-12 02:23:58 +00:00
|
|
|
* @param bool $get If false then only a count of the records is returned
|
2004-09-24 21:28:22 +00:00
|
|
|
* @param string $search A simple string to search for
|
2005-07-12 02:23:58 +00:00
|
|
|
* @param bool $confirmed A switch to allow/disallow unconfirmed users
|
2004-09-24 21:28:22 +00:00
|
|
|
* @param array(int) $exceptions A list of IDs to ignore, eg 2,4,5,8,9,10
|
|
|
|
* @param string $sort A SQL snippet for the sorting criteria to use
|
|
|
|
* @param string $firstinitial ?
|
|
|
|
* @param string $lastinitial ?
|
|
|
|
* @param string $page ?
|
|
|
|
* @param string $recordsperpage ?
|
|
|
|
* @param string $fields A comma separated list of fields to be returned from the chosen table.
|
2005-07-12 02:23:58 +00:00
|
|
|
* @return object|false|int {@link $USER} records unless get is false in which case the integer count of the records found is returned. False is returned if an error is encountered.
|
2004-09-24 21:28:22 +00:00
|
|
|
*/
|
2008-05-25 09:39:02 +00:00
|
|
|
function get_users($get=true, $search='', $confirmed=false, array $exceptions=null, $sort='firstname ASC',
|
|
|
|
$firstinitial='', $lastinitial='', $page='', $recordsperpage='', $fields='*', $extraselect='', array $extraparams=null) {
|
|
|
|
global $DB;
|
2007-08-02 23:39:28 +00:00
|
|
|
|
2006-09-15 14:32:35 +00:00
|
|
|
if ($get && !$recordsperpage) {
|
|
|
|
debugging('Call to get_users with $get = true no $recordsperpage limit. ' .
|
|
|
|
'On large installations, this will probably cause an out of memory error. ' .
|
|
|
|
'Please think again and change your code so that it does not try to ' .
|
2006-10-01 06:39:20 +00:00
|
|
|
'load so much data into memory.', DEBUG_DEVELOPER);
|
2006-09-15 14:32:35 +00:00
|
|
|
}
|
2003-09-14 04:04:15 +00:00
|
|
|
|
2008-06-09 19:48:24 +00:00
|
|
|
$LIKE = $DB->sql_ilike();
|
|
|
|
$fullname = $DB->sql_fullname();
|
2003-03-21 10:10:21 +00:00
|
|
|
|
2008-05-25 09:39:02 +00:00
|
|
|
$select = " username <> :guest AND deleted = 0";
|
|
|
|
$params = array('guest'=>'guest');
|
2004-03-20 06:58:52 +00:00
|
|
|
|
2004-12-22 23:11:27 +00:00
|
|
|
if (!empty($search)){
|
|
|
|
$search = trim($search);
|
2008-05-25 09:39:02 +00:00
|
|
|
$select .= " AND ($fullname $LIKE :search1 OR email $LIKE :search2 OR username = :search3)";
|
|
|
|
$params['search1'] = "%$search%";
|
|
|
|
$params['search2'] = "%$search%";
|
|
|
|
$params['search3'] = "$search";
|
2003-03-21 10:10:21 +00:00
|
|
|
}
|
|
|
|
|
2003-05-14 15:19:04 +00:00
|
|
|
if ($confirmed) {
|
2008-05-25 09:39:02 +00:00
|
|
|
$select .= " AND confirmed = 1";
|
2003-05-14 15:19:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($exceptions) {
|
2008-05-25 09:39:02 +00:00
|
|
|
list($exceptions, $eparams) = $DB->get_in_or_equal($exceptions, SQL_PARAMS_NAMED, 'ex0000', false);
|
|
|
|
$params = $params + $eparams;
|
|
|
|
$except = " AND id $exceptions";
|
2003-05-14 15:19:04 +00:00
|
|
|
}
|
|
|
|
|
2004-03-20 06:58:52 +00:00
|
|
|
if ($firstinitial) {
|
2008-05-25 09:39:02 +00:00
|
|
|
$select .= " AND firstname $LIKE :fni";
|
|
|
|
$params['fni'] = "$firstinitial%";
|
2004-09-21 11:41:58 +00:00
|
|
|
}
|
2004-03-20 06:58:52 +00:00
|
|
|
if ($lastinitial) {
|
2008-05-25 09:39:02 +00:00
|
|
|
$select .= " AND lastname $LIKE :lni";
|
|
|
|
$params['lni'] = "$lastinitial%";
|
2004-09-21 11:41:58 +00:00
|
|
|
}
|
2004-03-20 06:58:52 +00:00
|
|
|
|
2007-11-13 08:43:20 +00:00
|
|
|
if ($extraselect) {
|
2008-05-25 09:39:02 +00:00
|
|
|
$select .= " AND $extraselect";
|
|
|
|
$params = $params + (array)$extraparams;
|
2007-11-13 08:43:20 +00:00
|
|
|
}
|
|
|
|
|
2003-05-14 15:19:04 +00:00
|
|
|
if ($get) {
|
2008-05-25 09:39:02 +00:00
|
|
|
return $DB->get_records_select('user', $select, $params, $sort, $fields, $page, $recordsperpage);
|
2003-05-14 15:19:04 +00:00
|
|
|
} else {
|
2008-05-25 09:39:02 +00:00
|
|
|
return $DB->count_records_select('user', $select, $params);
|
2003-05-14 15:19:04 +00:00
|
|
|
}
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
2003-05-14 15:19:04 +00:00
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2004-09-24 21:28:22 +00:00
|
|
|
* shortdesc (optional)
|
|
|
|
*
|
|
|
|
* longdesc
|
|
|
|
*
|
|
|
|
* @param string $sort ?
|
|
|
|
* @param string $dir ?
|
|
|
|
* @param int $categoryid ?
|
|
|
|
* @param int $categoryid ?
|
|
|
|
* @param string $search ?
|
|
|
|
* @param string $firstinitial ?
|
|
|
|
* @param string $lastinitial ?
|
2005-07-12 02:23:58 +00:00
|
|
|
* @returnobject {@link $USER} records
|
2004-09-24 21:28:22 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
|
|
|
|
2006-09-15 14:32:35 +00:00
|
|
|
function get_users_listing($sort='lastaccess', $dir='ASC', $page=0, $recordsperpage=0,
|
2008-05-25 09:39:02 +00:00
|
|
|
$search='', $firstinitial='', $lastinitial='', $extraselect='', array $extraparams=null) {
|
|
|
|
global $DB;
|
2002-12-23 13:48:31 +00:00
|
|
|
|
2008-06-09 19:48:24 +00:00
|
|
|
$LIKE = $DB->sql_ilike();
|
|
|
|
$fullname = $DB->sql_fullname();
|
2003-02-18 03:16:07 +00:00
|
|
|
|
2008-05-25 09:39:02 +00:00
|
|
|
$select = "deleted <> 1";
|
|
|
|
$params = array();
|
2004-03-20 06:58:52 +00:00
|
|
|
|
2004-12-22 23:11:27 +00:00
|
|
|
if (!empty($search)) {
|
|
|
|
$search = trim($search);
|
2008-05-25 09:39:02 +00:00
|
|
|
$select .= " AND ($fullname $LIKE :search1 OR email $LIKE :search2 OR username = :search3)";
|
|
|
|
$params['search1'] = "%$search%";
|
|
|
|
$params['search2'] = "%$search%";
|
|
|
|
$params['search3'] = "$search";
|
2004-03-20 06:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($firstinitial) {
|
2008-05-25 09:39:02 +00:00
|
|
|
$select .= " AND firstname $LIKE :fni";
|
|
|
|
$params['fni'] = "$firstinitial%";
|
2004-03-20 06:58:52 +00:00
|
|
|
}
|
|
|
|
if ($lastinitial) {
|
2008-05-25 09:39:02 +00:00
|
|
|
$select .= " AND lastname $LIKE :lni";
|
|
|
|
$params['lni'] = "$lastinitial%";
|
2003-03-21 09:42:42 +00:00
|
|
|
}
|
|
|
|
|
2007-11-13 08:43:20 +00:00
|
|
|
if ($extraselect) {
|
2008-05-25 09:39:02 +00:00
|
|
|
$select .= " AND $extraselect";
|
|
|
|
$params = $params + (array)$extraparams;
|
2007-11-13 08:43:20 +00:00
|
|
|
}
|
2007-01-04 02:52:44 +00:00
|
|
|
|
2004-03-20 06:58:52 +00:00
|
|
|
if ($sort) {
|
2008-05-25 09:39:02 +00:00
|
|
|
$sort = " ORDER BY $sort $dir";
|
2004-03-20 06:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// warning: will return UNCONFIRMED USERS
|
2008-05-25 09:39:02 +00:00
|
|
|
return $DB->get_records_sql("SELECT id, username, email, firstname, lastname, city, country, lastaccess, confirmed, mnethostid
|
|
|
|
FROM {user}
|
|
|
|
WHERE $select
|
|
|
|
$sort", $params, $page, $recordsperpage);
|
2002-12-20 14:44:14 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-03-20 06:58:52 +00:00
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2005-07-12 02:23:58 +00:00
|
|
|
* Full list of users that have confirmed their accounts.
|
2004-09-24 21:28:22 +00:00
|
|
|
*
|
2008-05-25 09:39:02 +00:00
|
|
|
* @return array of unconfirmed users
|
2004-09-24 21:28:22 +00:00
|
|
|
*/
|
2002-12-20 14:44:14 +00:00
|
|
|
function get_users_confirmed() {
|
2008-05-25 09:39:02 +00:00
|
|
|
global $DB;
|
|
|
|
return $DB->get_records_sql("SELECT *
|
|
|
|
FROM {user}
|
|
|
|
WHERE confirmed = 1 AND deleted = 0 AND username <> ?", array('guest'));
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-15 13:59:24 +00:00
|
|
|
/// OTHER SITE AND COURSE FUNCTIONS /////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2004-09-24 21:28:22 +00:00
|
|
|
* Returns $course object of the top-level site.
|
|
|
|
*
|
2004-09-25 05:29:21 +00:00
|
|
|
* @return course A {@link $COURSE} object for the site
|
2004-09-24 21:28:22 +00:00
|
|
|
*/
|
2005-03-03 04:41:46 +00:00
|
|
|
function get_site() {
|
2008-05-25 09:39:02 +00:00
|
|
|
global $SITE, $DB;
|
2005-03-03 04:41:46 +00:00
|
|
|
|
|
|
|
if (!empty($SITE->id)) { // We already have a global to use, so return that
|
|
|
|
return $SITE;
|
|
|
|
}
|
2003-08-15 13:59:24 +00:00
|
|
|
|
2008-05-25 09:39:02 +00:00
|
|
|
if ($course = $DB->get_record('course', array('category'=>0))) {
|
2003-08-15 13:59:24 +00:00
|
|
|
return $course;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2006-09-10 07:07:52 +00:00
|
|
|
* Returns list of courses, for whole site, or category
|
|
|
|
*
|
|
|
|
* Returns list of courses, for whole site, or category
|
2007-09-20 13:15:26 +00:00
|
|
|
* Important: Using c.* for fields is extremely expensive because
|
2006-09-10 07:07:52 +00:00
|
|
|
* we are using distinct. You almost _NEVER_ need all the fields
|
|
|
|
* in such a large SELECT
|
|
|
|
*
|
|
|
|
* @param type description
|
2008-05-25 15:16:17 +00:00
|
|
|
* @return array of courses
|
2006-09-10 07:07:52 +00:00
|
|
|
*/
|
2004-11-18 02:31:53 +00:00
|
|
|
function get_courses($categoryid="all", $sort="c.sortorder ASC", $fields="c.*") {
|
2003-08-15 13:59:24 +00:00
|
|
|
|
2008-05-25 15:16:17 +00:00
|
|
|
global $USER, $CFG, $DB;
|
2007-08-02 23:39:28 +00:00
|
|
|
|
2008-05-25 15:16:17 +00:00
|
|
|
$params = array();
|
|
|
|
|
|
|
|
if ($categoryid !== "all" && is_numeric($categoryid)) {
|
|
|
|
$categoryselect = "WHERE c.category = :catid";
|
|
|
|
$params['catid'] = $categoryid;
|
2006-09-15 09:08:48 +00:00
|
|
|
} else {
|
2007-08-02 23:39:28 +00:00
|
|
|
$categoryselect = "";
|
2006-09-17 18:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($sort)) {
|
|
|
|
$sortstatement = "";
|
|
|
|
} else {
|
|
|
|
$sortstatement = "ORDER BY $sort";
|
|
|
|
}
|
|
|
|
|
|
|
|
$visiblecourses = array();
|
2007-08-02 23:39:28 +00:00
|
|
|
|
2008-05-25 15:16:17 +00:00
|
|
|
$sql = "SELECT $fields,
|
|
|
|
ctx.id AS ctxid, ctx.path AS ctxpath,
|
|
|
|
ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
|
|
|
|
FROM {course} c
|
|
|
|
JOIN {context} ctx
|
|
|
|
ON (c.id = ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
|
|
|
|
$categoryselect
|
|
|
|
$sortstatement";
|
|
|
|
|
2006-09-15 09:08:48 +00:00
|
|
|
// pull out all course matching the cat
|
2008-05-25 15:16:17 +00:00
|
|
|
if ($courses = $DB->get_records_sql($sql, $params)) {
|
2006-09-17 18:07:35 +00:00
|
|
|
|
|
|
|
// loop throught them
|
|
|
|
foreach ($courses as $course) {
|
2008-02-26 21:48:08 +00:00
|
|
|
$course = make_context_subobj($course);
|
2006-09-19 08:16:26 +00:00
|
|
|
if (isset($course->visible) && $course->visible <= 0) {
|
2006-09-17 18:07:35 +00:00
|
|
|
// for hidden courses, require visibility check
|
2008-02-26 21:48:08 +00:00
|
|
|
if (has_capability('moodle/course:viewhiddencourses', $course->context)) {
|
2008-05-25 15:16:17 +00:00
|
|
|
$visiblecourses [$course->id] = $course;
|
2006-09-17 18:07:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
2008-05-25 15:16:17 +00:00
|
|
|
$visiblecourses [$course->id] = $course;
|
2007-08-02 23:39:28 +00:00
|
|
|
}
|
2006-09-17 18:07:35 +00:00
|
|
|
}
|
2004-11-18 02:31:53 +00:00
|
|
|
}
|
2006-09-15 09:08:48 +00:00
|
|
|
return $visiblecourses;
|
2003-08-21 14:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-18 02:31:53 +00:00
|
|
|
/**
|
2006-09-10 07:07:52 +00:00
|
|
|
* Returns list of courses, for whole site, or category
|
|
|
|
*
|
|
|
|
* Similar to get_courses, but allows paging
|
2007-08-02 23:39:28 +00:00
|
|
|
* Important: Using c.* for fields is extremely expensive because
|
2006-09-10 07:07:52 +00:00
|
|
|
* we are using distinct. You almost _NEVER_ need all the fields
|
|
|
|
* in such a large SELECT
|
|
|
|
*
|
|
|
|
* @param type description
|
2008-05-25 15:16:17 +00:00
|
|
|
* @return array of courses
|
2006-09-10 07:07:52 +00:00
|
|
|
*/
|
2004-11-18 02:31:53 +00:00
|
|
|
function get_courses_page($categoryid="all", $sort="c.sortorder ASC", $fields="c.*",
|
|
|
|
&$totalcount, $limitfrom="", $limitnum="") {
|
2008-05-25 15:16:17 +00:00
|
|
|
global $USER, $CFG, $DB;
|
2004-09-23 07:46:41 +00:00
|
|
|
|
2008-05-25 15:16:17 +00:00
|
|
|
$params = array();
|
2007-08-02 23:39:28 +00:00
|
|
|
|
2006-09-15 09:08:48 +00:00
|
|
|
$categoryselect = "";
|
|
|
|
if ($categoryid != "all" && is_numeric($categoryid)) {
|
2008-05-25 15:16:17 +00:00
|
|
|
$categoryselect = "WHERE c.category = :catid";
|
|
|
|
$params['catid'] = $categoryid;
|
2006-09-15 09:08:48 +00:00
|
|
|
} else {
|
2007-08-02 23:39:28 +00:00
|
|
|
$categoryselect = "";
|
|
|
|
}
|
|
|
|
|
2008-05-25 15:16:17 +00:00
|
|
|
$sql = "SELECT $fields,
|
|
|
|
ctx.id AS ctxid, ctx.path AS ctxpath,
|
|
|
|
ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
|
|
|
|
FROM {course} c
|
|
|
|
JOIN {context} ctx
|
|
|
|
ON (c.id = ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
|
|
|
|
$categoryselect
|
|
|
|
ORDER BY $sort";
|
|
|
|
|
2006-09-15 09:08:48 +00:00
|
|
|
// pull out all course matching the cat
|
2008-05-25 15:16:17 +00:00
|
|
|
if (!$rs = $DB->get_recordset_sql($sql, $params)) {
|
|
|
|
return array();
|
2006-09-22 11:15:10 +00:00
|
|
|
}
|
2006-09-15 09:08:48 +00:00
|
|
|
$totalcount = 0;
|
2007-08-02 23:39:28 +00:00
|
|
|
|
2006-09-19 08:16:26 +00:00
|
|
|
if (!$limitfrom) {
|
2007-08-02 23:39:28 +00:00
|
|
|
$limitfrom = 0;
|
2006-09-15 09:08:48 +00:00
|
|
|
}
|
2007-08-02 23:39:28 +00:00
|
|
|
|
2006-09-15 09:08:48 +00:00
|
|
|
// iteration will have to be done inside loop to keep track of the limitfrom and limitnum
|
2008-05-25 15:16:17 +00:00
|
|
|
$visiblecourses = array();
|
|
|
|
foreach($rs as $course) {
|
2007-10-10 12:19:27 +00:00
|
|
|
$course = make_context_subobj($course);
|
|
|
|
if ($course->visible <= 0) {
|
|
|
|
// for hidden courses, require visibility check
|
|
|
|
if (has_capability('moodle/course:viewhiddencourses', $course->context)) {
|
2006-09-15 09:08:48 +00:00
|
|
|
$totalcount++;
|
2007-10-10 12:19:27 +00:00
|
|
|
if ($totalcount > $limitfrom && (!$limitnum or count($visiblecourses) < $limitnum)) {
|
2008-05-25 15:16:17 +00:00
|
|
|
$visiblecourses [$course->id] = $course;
|
2006-09-15 09:08:48 +00:00
|
|
|
}
|
|
|
|
}
|
2007-10-10 12:19:27 +00:00
|
|
|
} else {
|
|
|
|
$totalcount++;
|
|
|
|
if ($totalcount > $limitfrom && (!$limitnum or count($visiblecourses) < $limitnum)) {
|
2008-05-25 15:16:17 +00:00
|
|
|
$visiblecourses [$course->id] = $course;
|
2007-10-10 12:19:27 +00:00
|
|
|
}
|
2007-08-02 23:39:28 +00:00
|
|
|
}
|
2006-09-15 09:08:48 +00:00
|
|
|
}
|
2008-05-25 15:16:17 +00:00
|
|
|
$rs->close();
|
2006-09-15 09:08:48 +00:00
|
|
|
return $visiblecourses;
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
|
2008-05-25 09:39:02 +00:00
|
|
|
/**
|
2007-09-19 07:26:42 +00:00
|
|
|
* Retrieve course records with the course managers and other related records
|
|
|
|
* that we need for print_course(). This allows print_courses() to do its job
|
|
|
|
* in a constant number of DB queries, regardless of the number of courses,
|
|
|
|
* role assignments, etc.
|
2007-09-20 13:15:26 +00:00
|
|
|
*
|
2007-09-19 07:26:42 +00:00
|
|
|
* The returned array is indexed on c.id, and each course will have
|
|
|
|
* - $course->context - a context obj
|
|
|
|
* - $course->managers - array containing RA objects that include a $user obj
|
|
|
|
* with the minimal fields needed for fullname()
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function get_courses_wmanagers($categoryid=0, $sort="c.sortorder ASC", $fields=array()) {
|
|
|
|
/*
|
2007-09-20 13:15:26 +00:00
|
|
|
* The plan is to
|
2007-09-19 07:26:42 +00:00
|
|
|
*
|
|
|
|
* - Grab the courses JOINed w/context
|
|
|
|
*
|
|
|
|
* - Grab the interesting course-manager RAs
|
|
|
|
* JOINed with a base user obj and add them to each course
|
|
|
|
*
|
|
|
|
* So as to do all the work in 2 DB queries. The RA+user JOIN
|
|
|
|
* ends up being pretty expensive if it happens over _all_
|
|
|
|
* courses on a large site. (Are we surprised!?)
|
|
|
|
*
|
|
|
|
* So this should _never_ get called with 'all' on a large site.
|
|
|
|
*
|
|
|
|
*/
|
2008-05-25 15:16:17 +00:00
|
|
|
global $USER, $CFG, $DB;
|
2007-09-19 07:26:42 +00:00
|
|
|
|
2008-05-25 15:16:17 +00:00
|
|
|
$params = array();
|
2007-09-19 07:26:42 +00:00
|
|
|
$allcats = false; // bool flag
|
|
|
|
if ($categoryid === 'all') {
|
|
|
|
$categoryclause = '';
|
|
|
|
$allcats = true;
|
|
|
|
} elseif (is_numeric($categoryid)) {
|
2008-05-25 15:16:17 +00:00
|
|
|
$categoryclause = "c.category = :catid";
|
|
|
|
$params['catid'] = $categoryid;
|
2007-09-19 07:26:42 +00:00
|
|
|
} else {
|
|
|
|
debugging("Could not recognise categoryid = $categoryid");
|
|
|
|
$categoryclause = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$basefields = array('id', 'category', 'sortorder',
|
|
|
|
'shortname', 'fullname', 'idnumber',
|
|
|
|
'teacher', 'teachers', 'student', 'students',
|
|
|
|
'guest', 'startdate', 'visible',
|
|
|
|
'newsitems', 'cost', 'enrol',
|
|
|
|
'groupmode', 'groupmodeforce');
|
|
|
|
|
|
|
|
if (!is_null($fields) && is_string($fields)) {
|
|
|
|
if (empty($fields)) {
|
|
|
|
$fields = $basefields;
|
|
|
|
} else {
|
2007-09-20 13:15:26 +00:00
|
|
|
// turn the fields from a string to an array that
|
2007-09-19 07:26:42 +00:00
|
|
|
// get_user_courses_bycap() will like...
|
|
|
|
$fields = explode(',',$fields);
|
|
|
|
$fields = array_map('trim', $fields);
|
|
|
|
$fields = array_unique(array_merge($basefields, $fields));
|
|
|
|
}
|
|
|
|
} elseif (is_array($fields)) {
|
|
|
|
$fields = array_merge($basefields,$fields);
|
|
|
|
}
|
|
|
|
$coursefields = 'c.' .join(',c.', $fields);
|
|
|
|
|
|
|
|
if (empty($sort)) {
|
|
|
|
$sortstatement = "";
|
|
|
|
} else {
|
|
|
|
$sortstatement = "ORDER BY $sort";
|
|
|
|
}
|
|
|
|
|
2007-09-19 07:49:10 +00:00
|
|
|
$where = 'WHERE c.id != ' . SITEID;
|
2007-09-19 07:26:42 +00:00
|
|
|
if ($categoryclause !== ''){
|
2007-09-19 07:49:10 +00:00
|
|
|
$where = "$where AND $categoryclause";
|
2007-09-19 07:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pull out all courses matching the cat
|
|
|
|
$sql = "SELECT $coursefields,
|
2007-09-19 07:54:37 +00:00
|
|
|
ctx.id AS ctxid, ctx.path AS ctxpath,
|
|
|
|
ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
|
2008-05-25 15:16:17 +00:00
|
|
|
FROM {course} c
|
|
|
|
JOIN {context} ctx
|
|
|
|
ON (c.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
|
|
|
|
$where
|
|
|
|
$sortstatement";
|
2007-09-19 07:26:42 +00:00
|
|
|
|
|
|
|
$catpaths = array();
|
|
|
|
$catpath = NULL;
|
2008-05-25 15:16:17 +00:00
|
|
|
if ($courses = $DB->get_records_sql($sql, $params)) {
|
2007-09-19 07:26:42 +00:00
|
|
|
// loop on courses materialising
|
2007-09-20 13:15:26 +00:00
|
|
|
// the context, and prepping data to fetch the
|
2007-09-19 07:26:42 +00:00
|
|
|
// managers efficiently later...
|
|
|
|
foreach ($courses as $k => $course) {
|
|
|
|
$courses[$k] = make_context_subobj($courses[$k]);
|
|
|
|
$courses[$k]->managers = array();
|
|
|
|
if ($allcats === false) {
|
|
|
|
// single cat, so take just the first one...
|
|
|
|
if ($catpath === NULL) {
|
|
|
|
$catpath = preg_replace(':/\d+$:', '',$courses[$k]->context->path);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// chop off the contextid of the course itself
|
|
|
|
// like dirname() does...
|
|
|
|
$catpaths[] = preg_replace(':/\d+$:', '',$courses[$k]->context->path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return array(); // no courses!
|
|
|
|
}
|
|
|
|
|
2007-09-19 07:54:23 +00:00
|
|
|
$CFG->coursemanager = trim($CFG->coursemanager);
|
|
|
|
if (empty($CFG->coursemanager)) {
|
|
|
|
return $courses;
|
|
|
|
}
|
|
|
|
|
2007-09-19 07:26:42 +00:00
|
|
|
$managerroles = split(',', $CFG->coursemanager);
|
|
|
|
$catctxids = '';
|
|
|
|
if (count($managerroles)) {
|
|
|
|
if ($allcats === true) {
|
|
|
|
$catpaths = array_unique($catpaths);
|
|
|
|
$ctxids = array();
|
|
|
|
foreach ($catpaths as $cpath) {
|
|
|
|
$ctxids = array_merge($ctxids, explode('/',substr($cpath,1)));
|
|
|
|
}
|
|
|
|
$ctxids = array_unique($ctxids);
|
|
|
|
$catctxids = implode( ',' , $ctxids);
|
2008-03-20 07:20:00 +00:00
|
|
|
unset($catpaths);
|
|
|
|
unset($cpath);
|
2007-09-19 07:26:42 +00:00
|
|
|
} else {
|
|
|
|
// take the ctx path from the first course
|
|
|
|
// as all categories will be the same...
|
|
|
|
$catpath = substr($catpath,1);
|
|
|
|
$catpath = preg_replace(':/\d+$:','',$catpath);
|
|
|
|
$catctxids = str_replace('/',',',$catpath);
|
|
|
|
}
|
|
|
|
if ($categoryclause !== '') {
|
|
|
|
$categoryclause = "AND $categoryclause";
|
|
|
|
}
|
|
|
|
/*
|
2007-09-20 13:15:26 +00:00
|
|
|
* Note: Here we use a LEFT OUTER JOIN that can
|
2007-09-19 07:26:42 +00:00
|
|
|
* "optionally" match to avoid passing a ton of context
|
|
|
|
* ids in an IN() clause. Perhaps a subselect is faster.
|
|
|
|
*
|
|
|
|
* In any case, this SQL is not-so-nice over large sets of
|
|
|
|
* courses with no $categoryclause.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
$sql = "SELECT ctx.path, ctx.instanceid, ctx.contextlevel,
|
2007-09-20 13:15:26 +00:00
|
|
|
ra.hidden,
|
2007-09-19 07:26:42 +00:00
|
|
|
r.id AS roleid, r.name as rolename,
|
|
|
|
u.id AS userid, u.firstname, u.lastname
|
2008-05-25 15:16:17 +00:00
|
|
|
FROM {role_assignments} ra
|
|
|
|
JOIN {context} ctx ON ra.contextid = ctx.id
|
|
|
|
JOIN {user} u ON ra.userid = u.id
|
|
|
|
JOIN {role} r ON ra.roleid = r.id
|
|
|
|
LEFT OUTER JOIN {course} c
|
|
|
|
ON (ctx.instanceid=c.id AND ctx.contextlevel=".CONTEXT_COURSE.")
|
2008-03-20 07:20:00 +00:00
|
|
|
WHERE ( c.id IS NOT NULL";
|
|
|
|
// under certain conditions, $catctxids is NULL
|
|
|
|
if($catctxids == NULL){
|
|
|
|
$sql .= ") ";
|
|
|
|
}else{
|
|
|
|
$sql .= " OR ra.contextid IN ($catctxids) )";
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= "AND ra.roleid IN ({$CFG->coursemanager})
|
2007-09-19 07:26:42 +00:00
|
|
|
$categoryclause
|
|
|
|
ORDER BY r.sortorder ASC, ctx.contextlevel ASC, ra.sortorder ASC";
|
2008-05-25 15:16:17 +00:00
|
|
|
$rs = $DB->get_recordset_sql($sql, $params);
|
2007-09-20 13:15:26 +00:00
|
|
|
|
2007-09-19 07:26:42 +00:00
|
|
|
// This loop is fairly stupid as it stands - might get better
|
|
|
|
// results doing an initial pass clustering RAs by path.
|
2008-05-25 15:16:17 +00:00
|
|
|
foreach($rs as $ra) {
|
2007-10-10 12:19:27 +00:00
|
|
|
$user = new StdClass;
|
|
|
|
$user->id = $ra->userid; unset($ra->userid);
|
|
|
|
$user->firstname = $ra->firstname; unset($ra->firstname);
|
|
|
|
$user->lastname = $ra->lastname; unset($ra->lastname);
|
|
|
|
$ra->user = $user;
|
|
|
|
if ($ra->contextlevel == CONTEXT_SYSTEM) {
|
|
|
|
foreach ($courses as $k => $course) {
|
|
|
|
$courses[$k]->managers[] = $ra;
|
|
|
|
}
|
|
|
|
} elseif ($ra->contextlevel == CONTEXT_COURSECAT) {
|
|
|
|
if ($allcats === false) {
|
|
|
|
// It always applies
|
2007-09-19 07:26:42 +00:00
|
|
|
foreach ($courses as $k => $course) {
|
|
|
|
$courses[$k]->managers[] = $ra;
|
|
|
|
}
|
2007-10-10 12:19:27 +00:00
|
|
|
} else {
|
|
|
|
foreach ($courses as $k => $course) {
|
|
|
|
// Note that strpos() returns 0 as "matched at pos 0"
|
|
|
|
if (strpos($course->context->path, $ra->path.'/')===0) {
|
|
|
|
// Only add it to subpaths
|
2007-09-19 07:26:42 +00:00
|
|
|
$courses[$k]->managers[] = $ra;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-10 12:19:27 +00:00
|
|
|
} else { // course-level
|
|
|
|
if(!array_key_exists($ra->instanceid, $courses)) {
|
|
|
|
//this course is not in a list, probably a frontpage course
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$courses[$ra->instanceid]->managers[] = $ra;
|
2007-09-19 07:26:42 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-25 15:16:17 +00:00
|
|
|
$rs->close();
|
2007-09-19 07:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $courses;
|
|
|
|
}
|
2003-08-15 13:59:24 +00:00
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2007-09-20 13:15:26 +00:00
|
|
|
* Convenience function - lists courses that a user has access to view.
|
2004-09-24 21:28:22 +00:00
|
|
|
*
|
2007-09-19 07:18:54 +00:00
|
|
|
* For admins and others with access to "every" course in the system, we should
|
|
|
|
* try to get courses with explicit RAs.
|
|
|
|
*
|
|
|
|
* NOTE: this function is heavily geared towards the perspective of the user
|
2007-09-20 13:15:26 +00:00
|
|
|
* passed in $userid. So it will hide courses that the user cannot see
|
2007-09-19 07:18:54 +00:00
|
|
|
* (for any reason) even if called from cron or from another $USER's
|
|
|
|
* perspective.
|
2007-09-20 13:15:26 +00:00
|
|
|
*
|
2007-09-19 07:18:54 +00:00
|
|
|
* If you really want to know what courses are assigned to the user,
|
2007-09-20 13:15:26 +00:00
|
|
|
* without any hiding or scheming, call the lower-level
|
2007-09-19 07:18:54 +00:00
|
|
|
* get_user_courses_bycap().
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Notes inherited from get_user_courses_bycap():
|
2007-09-19 07:03:49 +00:00
|
|
|
*
|
|
|
|
* - $fields is an array of fieldnames to ADD
|
|
|
|
* so name the fields you really need, which will
|
|
|
|
* be added and uniq'd
|
|
|
|
*
|
|
|
|
* - the course records have $c->context which is a fully
|
|
|
|
* valid context object. Saves you a query per course!
|
|
|
|
*
|
2007-09-19 07:04:59 +00:00
|
|
|
* @uses $CFG,$USER
|
2005-07-12 02:23:58 +00:00
|
|
|
* @param int $userid The user of interest
|
2006-09-19 14:23:43 +00:00
|
|
|
* @param string $sort the sortorder in the course table
|
2007-09-19 07:03:49 +00:00
|
|
|
* @param array $fields - names of _additional_ fields to return (also accepts a string)
|
2007-01-12 17:05:32 +00:00
|
|
|
* @param bool $doanything True if using the doanything flag
|
|
|
|
* @param int $limit Maximum number of records to return, or 0 for unlimited
|
2006-09-19 14:23:43 +00:00
|
|
|
* @return array {@link $COURSE} of course objects
|
2004-09-24 21:28:22 +00:00
|
|
|
*/
|
2007-09-19 07:03:49 +00:00
|
|
|
function get_my_courses($userid, $sort='visible DESC,sortorder ASC', $fields=NULL, $doanything=false,$limit=0) {
|
2008-05-25 15:16:17 +00:00
|
|
|
global $CFG, $USER, $DB;
|
2007-08-02 23:39:28 +00:00
|
|
|
|
2007-05-06 05:28:39 +00:00
|
|
|
// Guest's do not have any courses
|
2008-05-01 06:07:24 +00:00
|
|
|
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
|
2008-05-25 15:16:17 +00:00
|
|
|
if (has_capability('moodle/legacy:guest', $sitecontext, $userid, false)) {
|
2007-05-06 05:28:39 +00:00
|
|
|
return(array());
|
|
|
|
}
|
2007-05-06 05:26:59 +00:00
|
|
|
|
2007-09-19 07:04:59 +00:00
|
|
|
$basefields = array('id', 'category', 'sortorder',
|
|
|
|
'shortname', 'fullname', 'idnumber',
|
|
|
|
'teacher', 'teachers', 'student', 'students',
|
|
|
|
'guest', 'startdate', 'visible',
|
|
|
|
'newsitems', 'cost', 'enrol',
|
|
|
|
'groupmode', 'groupmodeforce');
|
|
|
|
|
2007-09-19 07:03:49 +00:00
|
|
|
if (!is_null($fields) && is_string($fields)) {
|
|
|
|
if (empty($fields)) {
|
2007-09-19 07:04:59 +00:00
|
|
|
$fields = $basefields;
|
2007-09-19 07:03:49 +00:00
|
|
|
} else {
|
2007-09-20 13:15:26 +00:00
|
|
|
// turn the fields from a string to an array that
|
2007-09-19 07:04:23 +00:00
|
|
|
// get_user_courses_bycap() will like...
|
2007-09-19 07:04:59 +00:00
|
|
|
$fields = explode(',',$fields);
|
|
|
|
$fields = array_map('trim', $fields);
|
|
|
|
$fields = array_unique(array_merge($basefields, $fields));
|
|
|
|
}
|
2007-09-19 07:51:36 +00:00
|
|
|
} elseif (is_array($fields)) {
|
2007-09-20 13:15:26 +00:00
|
|
|
$fields = array_unique(array_merge($basefields, $fields));
|
2007-09-19 07:04:59 +00:00
|
|
|
} else {
|
|
|
|
$fields = $basefields;
|
|
|
|
}
|
|
|
|
|
2007-09-19 07:51:19 +00:00
|
|
|
$orderby = '';
|
|
|
|
$sort = trim($sort);
|
|
|
|
if (!empty($sort)) {
|
2008-02-18 20:14:16 +00:00
|
|
|
$rawsorts = explode(',', $sort);
|
|
|
|
$sorts = array();
|
|
|
|
foreach ($rawsorts as $rawsort) {
|
|
|
|
$rawsort = trim($rawsort);
|
2008-02-23 19:29:25 +00:00
|
|
|
if (strpos($rawsort, 'c.') === 0) {
|
2008-02-18 20:14:16 +00:00
|
|
|
$rawsort = substr($rawsort, 2);
|
|
|
|
}
|
|
|
|
$sorts[] = trim($rawsort);
|
|
|
|
}
|
|
|
|
$sort = 'c.'.implode(',c.', $sorts);
|
2007-09-19 07:51:19 +00:00
|
|
|
$orderby = "ORDER BY $sort";
|
|
|
|
}
|
|
|
|
|
2007-09-19 07:04:59 +00:00
|
|
|
//
|
|
|
|
// Logged-in user - Check cached courses
|
|
|
|
//
|
|
|
|
// NOTE! it's a _string_ because
|
|
|
|
// - it's all we'll ever use
|
|
|
|
// - it serialises much more compact than an array
|
2007-09-19 07:18:54 +00:00
|
|
|
// this a big concern here - cost of serialise
|
|
|
|
// and unserialise gets huge as the session grows
|
2007-09-19 07:04:59 +00:00
|
|
|
//
|
|
|
|
// If the courses are too many - it won't be set
|
|
|
|
// for large numbers of courses, caching in the session
|
|
|
|
// has marginal benefits (costs too much, not
|
|
|
|
// worthwhile...) and we may hit SQL parser limits
|
|
|
|
// because we use IN()
|
|
|
|
//
|
2007-09-19 07:07:21 +00:00
|
|
|
if ($userid === $USER->id) {
|
2007-09-20 13:15:26 +00:00
|
|
|
if (isset($USER->loginascontext)
|
2007-09-19 07:07:34 +00:00
|
|
|
&& $USER->loginascontext->contextlevel == CONTEXT_COURSE) {
|
2007-09-19 07:07:21 +00:00
|
|
|
// list _only_ this course
|
|
|
|
// anything else is asking for trouble...
|
|
|
|
$courseids = $USER->loginascontext->instanceid;
|
2007-09-20 13:15:26 +00:00
|
|
|
} elseif (isset($USER->mycourses)
|
2007-09-19 07:07:21 +00:00
|
|
|
&& is_string($USER->mycourses)) {
|
|
|
|
if ($USER->mycourses === '') {
|
|
|
|
// empty str means: user has no courses
|
|
|
|
// ... so do the easy thing...
|
|
|
|
return array();
|
|
|
|
} else {
|
|
|
|
$courseids = $USER->mycourses;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($courseids)) {
|
2007-09-20 13:15:26 +00:00
|
|
|
// The data massaging here MUST be kept in sync with
|
2007-09-19 07:04:59 +00:00
|
|
|
// get_user_courses_bycap() so we return
|
|
|
|
// the same...
|
|
|
|
// (but here we don't need to check has_cap)
|
|
|
|
$coursefields = 'c.' .join(',c.', $fields);
|
|
|
|
$sql = "SELECT $coursefields,
|
2007-09-19 07:54:37 +00:00
|
|
|
ctx.id AS ctxid, ctx.path AS ctxpath,
|
|
|
|
ctx.depth as ctxdepth, ctx.contextlevel AS ctxlevel,
|
2007-09-19 07:18:54 +00:00
|
|
|
cc.path AS categorypath
|
2008-05-25 15:16:17 +00:00
|
|
|
FROM {course} c
|
|
|
|
JOIN {course_categories} cc ON c.category=cc.id
|
|
|
|
JOIN {context} ctx
|
|
|
|
ON (c.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
|
|
|
|
WHERE c.id IN ($courseids)
|
|
|
|
$orderby";
|
|
|
|
$rs = $DB->get_recordset_sql($sql);
|
2007-09-19 07:04:59 +00:00
|
|
|
$courses = array();
|
|
|
|
$cc = 0; // keep count
|
2008-05-25 15:16:17 +00:00
|
|
|
foreach ($rs as $c) {
|
2007-10-10 12:19:27 +00:00
|
|
|
// build the context obj
|
|
|
|
$c = make_context_subobj($c);
|
2007-09-19 07:08:12 +00:00
|
|
|
|
2007-10-10 12:19:27 +00:00
|
|
|
$courses[$c->id] = $c;
|
|
|
|
if ($limit > 0 && $cc++ > $limit) {
|
|
|
|
break;
|
2007-09-19 07:04:59 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-25 15:16:17 +00:00
|
|
|
$rs->close();
|
2007-09-19 07:04:59 +00:00
|
|
|
return $courses;
|
2003-08-21 17:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-02 07:56:25 +00:00
|
|
|
|
2007-09-19 07:04:59 +00:00
|
|
|
// Non-cached - get accessinfo
|
2007-09-19 07:03:49 +00:00
|
|
|
if ($userid === $USER->id && isset($USER->access)) {
|
2007-09-19 07:04:10 +00:00
|
|
|
$accessinfo = $USER->access;
|
2007-07-23 08:56:26 +00:00
|
|
|
} else {
|
2007-09-19 07:03:49 +00:00
|
|
|
$accessinfo = get_user_access_sitewide($userid);
|
2007-09-19 07:04:10 +00:00
|
|
|
}
|
2007-09-19 07:04:59 +00:00
|
|
|
|
2007-09-20 13:15:26 +00:00
|
|
|
|
2007-09-19 07:04:23 +00:00
|
|
|
$courses = get_user_courses_bycap($userid, 'moodle/course:view', $accessinfo,
|
|
|
|
$doanything, $sort, $fields,
|
|
|
|
$limit);
|
2007-09-19 07:04:59 +00:00
|
|
|
|
2007-09-19 07:18:54 +00:00
|
|
|
$cats = NULL;
|
|
|
|
// If we have to walk category visibility
|
|
|
|
// to eval course visibility, get the categories
|
|
|
|
if (empty($CFG->allowvisiblecoursesinhiddencategories)) {
|
|
|
|
$sql = "SELECT cc.id, cc.path, cc.visible,
|
2007-09-19 07:54:37 +00:00
|
|
|
ctx.id AS ctxid, ctx.path AS ctxpath,
|
|
|
|
ctx.depth as ctxdepth, ctx.contextlevel AS ctxlevel
|
2008-05-25 15:16:17 +00:00
|
|
|
FROM {course_categories} cc
|
|
|
|
JOIN {context} ctx ON (cc.id = ctx.instanceid)
|
|
|
|
WHERE ctx.contextlevel = ".CONTEXT_COURSECAT."
|
|
|
|
ORDER BY cc.id";
|
|
|
|
$rs = $DB->get_recordset_sql($sql);
|
2007-09-20 13:15:26 +00:00
|
|
|
|
|
|
|
// Using a temporary array instead of $cats here, to avoid a "true" result when isnull($cats) further down
|
|
|
|
$categories = array();
|
2008-05-25 15:16:17 +00:00
|
|
|
foreach($rs as $course_cat) {
|
2007-10-10 12:19:27 +00:00
|
|
|
// build the context obj
|
|
|
|
$course_cat = make_context_subobj($course_cat);
|
|
|
|
$categories[$course_cat->id] = $course_cat;
|
2007-09-19 07:18:54 +00:00
|
|
|
}
|
2008-05-25 15:16:17 +00:00
|
|
|
$rs->close();
|
2007-09-20 13:15:26 +00:00
|
|
|
|
|
|
|
if (!empty($categories)) {
|
|
|
|
$cats = $categories;
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($course_cat);
|
2007-09-19 07:18:54 +00:00
|
|
|
}
|
2007-09-19 07:04:59 +00:00
|
|
|
//
|
|
|
|
// Strangely, get_my_courses() is expected to return the
|
2007-09-19 07:04:10 +00:00
|
|
|
// array keyed on id, which messes up the sorting
|
2007-09-19 07:04:59 +00:00
|
|
|
// So do that, and also cache the ids in the session if appropriate
|
|
|
|
//
|
2007-09-19 07:04:10 +00:00
|
|
|
$kcourses = array();
|
2007-09-20 13:15:26 +00:00
|
|
|
$courses_count = count($courses);
|
2007-09-19 07:04:59 +00:00
|
|
|
$cacheids = NULL;
|
2007-09-19 07:18:54 +00:00
|
|
|
$vcatpaths = array();
|
2007-09-20 13:15:26 +00:00
|
|
|
if ($userid === $USER->id && $courses_count < 500) {
|
2007-09-19 07:04:59 +00:00
|
|
|
$cacheids = array();
|
|
|
|
}
|
2007-09-20 13:15:26 +00:00
|
|
|
for ($n=0; $n<$courses_count; $n++) {
|
2007-09-19 07:18:54 +00:00
|
|
|
|
|
|
|
//
|
2007-09-19 07:21:35 +00:00
|
|
|
// Check whether $USER (not $userid) can _actually_ see them
|
2007-09-19 07:18:54 +00:00
|
|
|
// Easy if $CFG->allowvisiblecoursesinhiddencategories
|
|
|
|
// is set, and we don't have to care about categories.
|
|
|
|
// Lots of work otherwise... (all in mem though!)
|
|
|
|
//
|
2007-09-20 13:15:26 +00:00
|
|
|
$cansee = false;
|
2007-09-19 07:18:54 +00:00
|
|
|
if (is_null($cats)) { // easy rules!
|
|
|
|
if ($courses[$n]->visible == true) {
|
|
|
|
$cansee = true;
|
|
|
|
} elseif (has_capability('moodle/course:viewhiddencourses',
|
2007-09-19 07:21:35 +00:00
|
|
|
$courses[$n]->context, $USER->id)) {
|
2007-09-19 07:18:54 +00:00
|
|
|
$cansee = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// Is the cat visible?
|
|
|
|
// we have to assume it _is_ visible
|
|
|
|
// so we can shortcut when we find a hidden one
|
|
|
|
//
|
|
|
|
$viscat = true;
|
|
|
|
$cpath = $courses[$n]->categorypath;
|
|
|
|
if (isset($vcatpaths[$cpath])) {
|
|
|
|
$viscat = $vcatpaths[$cpath];
|
|
|
|
} else {
|
|
|
|
$cpath = substr($cpath,1); // kill leading slash
|
|
|
|
$cpath = explode('/',$cpath);
|
|
|
|
$ccct = count($cpath);
|
|
|
|
for ($m=0;$m<$ccct;$m++) {
|
|
|
|
$ccid = $cpath[$m];
|
|
|
|
if ($cats[$ccid]->visible==false) {
|
|
|
|
$viscat = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$vcatpaths[$courses[$n]->categorypath] = $viscat;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2007-09-19 07:21:35 +00:00
|
|
|
// Perhaps it's actually visible to $USER
|
2007-09-19 07:18:54 +00:00
|
|
|
// check moodle/category:visibility
|
2007-09-20 13:15:26 +00:00
|
|
|
//
|
2007-09-19 07:18:54 +00:00
|
|
|
// The name isn't obvious, but the description says
|
|
|
|
// "See hidden categories" so the user shall see...
|
2007-09-20 13:15:26 +00:00
|
|
|
// But also check if the allowvisiblecoursesinhiddencategories setting is true, and check for course visibility
|
2007-09-19 07:18:54 +00:00
|
|
|
if ($viscat === false) {
|
2007-09-20 13:15:26 +00:00
|
|
|
$catctx = $cats[$courses[$n]->category]->context;
|
|
|
|
if (has_capability('moodle/category:visibility', $catctx, $USER->id)) {
|
2007-09-19 07:18:54 +00:00
|
|
|
$vcatpaths[$courses[$n]->categorypath] = true;
|
|
|
|
$viscat = true;
|
2007-09-20 13:15:26 +00:00
|
|
|
} elseif ($CFG->allowvisiblecoursesinhiddencategories && $courses[$n]->visible == true) {
|
|
|
|
$viscat = true;
|
2007-09-19 07:18:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Decision matrix
|
|
|
|
//
|
|
|
|
if ($viscat === true) {
|
|
|
|
if ($courses[$n]->visible == true) {
|
|
|
|
$cansee = true;
|
|
|
|
} elseif (has_capability('moodle/course:viewhiddencourses',
|
2007-09-19 07:21:35 +00:00
|
|
|
$courses[$n]->context, $USER->id)) {
|
2007-09-19 07:18:54 +00:00
|
|
|
$cansee = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($cansee === true) {
|
|
|
|
$kcourses[$courses[$n]->id] = $courses[$n];
|
|
|
|
if (is_array($cacheids)) {
|
|
|
|
$cacheids[] = $courses[$n]->id;
|
|
|
|
}
|
2007-09-19 07:04:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_array($cacheids)) {
|
|
|
|
// Only happens
|
|
|
|
// - for the logged in user
|
|
|
|
// - below the threshold (500)
|
|
|
|
// empty string is _valid_
|
|
|
|
$USER->mycourses = join(',',$cacheids);
|
|
|
|
} elseif ($userid === $USER->id && isset($USER->mycourses)) {
|
|
|
|
// cheap sanity check
|
|
|
|
unset($USER->mycourses);
|
2007-09-19 07:04:10 +00:00
|
|
|
}
|
2007-09-19 07:04:59 +00:00
|
|
|
|
2007-09-19 07:04:10 +00:00
|
|
|
return $kcourses;
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2005-07-12 02:23:58 +00:00
|
|
|
* A list of courses that match a search
|
2004-09-24 21:28:22 +00:00
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @param array $searchterms ?
|
|
|
|
* @param string $sort ?
|
|
|
|
* @param int $page ?
|
|
|
|
* @param int $recordsperpage ?
|
|
|
|
* @param int $totalcount Passed in by reference. ?
|
2005-07-12 02:23:58 +00:00
|
|
|
* @return object {@link $COURSE} records
|
2004-09-24 21:28:22 +00:00
|
|
|
*/
|
2004-09-22 16:15:23 +00:00
|
|
|
function get_courses_search($searchterms, $sort='fullname ASC', $page=0, $recordsperpage=50, &$totalcount) {
|
2008-05-25 15:16:17 +00:00
|
|
|
global $CFG, $DB;
|
2003-08-15 13:59:24 +00:00
|
|
|
|
2008-05-25 20:43:46 +00:00
|
|
|
if ($DB->sql_regex_supported()) {
|
|
|
|
$REGEXP = $DB->sql_regex(true);
|
|
|
|
$NOTREGEXP = $DB->sql_regex(false);
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
2008-05-25 15:16:17 +00:00
|
|
|
$LIKE = $DB->sql_ilike(); // case-insensitive
|
2003-08-15 13:59:24 +00:00
|
|
|
|
2008-05-25 20:43:46 +00:00
|
|
|
$searchcond = array();
|
|
|
|
$params = array();
|
|
|
|
$i = 0;
|
2003-08-15 13:59:24 +00:00
|
|
|
|
2008-05-25 20:43:46 +00:00
|
|
|
$concat = $DB->sql_concat('c.summary', "' '", 'c.fullname');
|
2008-05-25 15:16:17 +00:00
|
|
|
|
2003-08-15 13:59:24 +00:00
|
|
|
foreach ($searchterms as $searchterm) {
|
2008-05-25 20:43:46 +00:00
|
|
|
$i++;
|
2006-10-31 19:54:46 +00:00
|
|
|
|
2008-01-01 12:03:00 +00:00
|
|
|
$NOT = ''; /// Initially we aren't going to perform NOT LIKE searches, only MSSQL and Oracle
|
|
|
|
/// will use it to simulate the "-" operator with LIKE clause
|
|
|
|
|
2006-10-31 19:54:46 +00:00
|
|
|
/// Under Oracle and MSSQL, trim the + and - operators and perform
|
2008-01-01 12:03:00 +00:00
|
|
|
/// simpler LIKE (or NOT LIKE) queries
|
2008-05-25 20:43:46 +00:00
|
|
|
if (!$DB->sql_regex_supported()) {
|
2008-01-01 12:03:00 +00:00
|
|
|
if (substr($searchterm, 0, 1) == '-') {
|
|
|
|
$NOT = ' NOT ';
|
|
|
|
}
|
2006-10-31 19:54:46 +00:00
|
|
|
$searchterm = trim($searchterm, '+-');
|
|
|
|
}
|
|
|
|
|
2008-05-25 20:43:46 +00:00
|
|
|
// TODO: +- may not work for non latin languages
|
2008-05-25 15:16:17 +00:00
|
|
|
|
2004-09-22 16:15:23 +00:00
|
|
|
if (substr($searchterm,0,1) == '+') {
|
2008-05-25 20:43:46 +00:00
|
|
|
$searchterm = trim($searchterm, '+-');
|
|
|
|
$searchterm = preg_quote($searchterm, '|');
|
|
|
|
$searchcond[] = "$concat $REGEXP :ss$i";
|
|
|
|
$params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)";
|
|
|
|
|
2003-08-19 05:32:20 +00:00
|
|
|
} else if (substr($searchterm,0,1) == "-") {
|
2008-05-25 20:43:46 +00:00
|
|
|
$searchterm = trim($searchterm, '+-');
|
|
|
|
$searchterm = preg_quote($searchterm, '|');
|
|
|
|
$searchcond[] = "$concat $NOTREGEXP :ss$i";
|
|
|
|
$params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)";
|
|
|
|
|
2003-08-19 05:32:20 +00:00
|
|
|
} else {
|
2008-05-25 20:43:46 +00:00
|
|
|
$searchcond[] = "$concat $NOT $LIKE :ss$i";
|
|
|
|
$params['ss'.$i] = "%$searchterm%";
|
2003-08-19 05:32:20 +00:00
|
|
|
}
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
|
2008-05-25 20:43:46 +00:00
|
|
|
if (empty($searchcond)) {
|
|
|
|
$totalcount = 0;
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$searchcond = implode(" AND ", $searchcond);
|
|
|
|
|
2007-09-19 07:08:37 +00:00
|
|
|
$sql = "SELECT c.*,
|
2007-09-19 07:54:37 +00:00
|
|
|
ctx.id AS ctxid, ctx.path AS ctxpath,
|
|
|
|
ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
|
2008-05-25 15:16:17 +00:00
|
|
|
FROM {course} c
|
|
|
|
JOIN {context} ctx
|
|
|
|
ON (c.id = ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
|
2008-05-25 20:43:46 +00:00
|
|
|
WHERE $searchcond AND c.id <> ".SITEID."
|
|
|
|
ORDER BY $sort";
|
2007-09-19 07:08:37 +00:00
|
|
|
$courses = array();
|
2008-05-25 15:16:17 +00:00
|
|
|
$c = 0; // counts how many visible courses we've seen
|
2003-08-15 13:59:24 +00:00
|
|
|
|
2008-05-25 15:16:17 +00:00
|
|
|
if ($rs = $DB->get_recordset_sql($sql, $params)) {
|
2007-09-19 07:08:37 +00:00
|
|
|
// Tiki pagination
|
|
|
|
$limitfrom = $page * $recordsperpage;
|
|
|
|
$limitto = $limitfrom + $recordsperpage;
|
|
|
|
|
2008-05-25 15:16:17 +00:00
|
|
|
foreach($rs as $course) {
|
2007-09-19 07:08:37 +00:00
|
|
|
$course = make_context_subobj($course);
|
|
|
|
if ($course->visible || has_capability('moodle/course:viewhiddencourses', $course->context)) {
|
|
|
|
// Don't exit this loop till the end
|
|
|
|
// we need to count all the visible courses
|
|
|
|
// to update $totalcount
|
|
|
|
if ($c >= $limitfrom && $c < $limitto) {
|
2008-05-25 15:16:17 +00:00
|
|
|
$courses[$course->id] = $course;
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
2007-09-19 07:08:37 +00:00
|
|
|
$c++;
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-25 15:16:17 +00:00
|
|
|
$rs->close();
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
|
2007-09-19 07:08:37 +00:00
|
|
|
// our caller expects 2 bits of data - our return
|
|
|
|
// array, and an updated $totalcount
|
|
|
|
$totalcount = $c;
|
2003-08-15 13:59:24 +00:00
|
|
|
return $courses;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2007-09-19 07:27:20 +00:00
|
|
|
* Returns a sorted list of categories. Each category object has a context
|
|
|
|
* property that is a context object.
|
2007-09-20 13:15:26 +00:00
|
|
|
*
|
2007-09-19 07:27:20 +00:00
|
|
|
* When asking for $parent='none' it will return all the categories, regardless
|
|
|
|
* of depth. Wheen asking for a specific parent, the default is to return
|
|
|
|
* a "shallow" resultset. Pass false to $shallow and it will return all
|
2007-09-20 13:15:26 +00:00
|
|
|
* the child categories as well.
|
|
|
|
*
|
2004-09-24 21:28:22 +00:00
|
|
|
*
|
2006-09-10 07:07:52 +00:00
|
|
|
* @param string $parent The parent category if any
|
|
|
|
* @param string $sort the sortorder
|
2007-09-19 07:27:20 +00:00
|
|
|
* @param bool $shallow - set to false to get the children too
|
2006-09-10 07:07:52 +00:00
|
|
|
* @return array of categories
|
2004-09-24 21:28:22 +00:00
|
|
|
*/
|
2007-09-19 07:27:20 +00:00
|
|
|
function get_categories($parent='none', $sort=NULL, $shallow=true) {
|
2008-05-25 15:16:17 +00:00
|
|
|
global $DB;
|
2007-09-19 07:27:20 +00:00
|
|
|
|
|
|
|
if ($sort === NULL) {
|
|
|
|
$sort = 'ORDER BY cc.sortorder ASC';
|
|
|
|
} elseif ($sort ==='') {
|
|
|
|
// leave it as empty
|
|
|
|
} else {
|
|
|
|
$sort = "ORDER BY $sort";
|
|
|
|
}
|
2003-08-15 13:59:24 +00:00
|
|
|
|
2005-02-23 01:49:22 +00:00
|
|
|
if ($parent === 'none') {
|
2007-09-19 07:27:20 +00:00
|
|
|
$sql = "SELECT cc.*,
|
2008-05-25 15:16:17 +00:00
|
|
|
ctx.id AS ctxid, ctx.path AS ctxpath,
|
|
|
|
ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
|
|
|
|
FROM {course_categories} cc
|
|
|
|
JOIN {context} ctx
|
|
|
|
ON cc.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSECAT."
|
2007-09-19 07:27:20 +00:00
|
|
|
$sort";
|
2008-05-25 15:16:17 +00:00
|
|
|
$params = array();
|
|
|
|
|
2007-09-19 07:27:20 +00:00
|
|
|
} elseif ($shallow) {
|
|
|
|
$sql = "SELECT cc.*,
|
2007-09-19 07:54:37 +00:00
|
|
|
ctx.id AS ctxid, ctx.path AS ctxpath,
|
|
|
|
ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
|
2008-05-25 15:16:17 +00:00
|
|
|
FROM {course_categories} cc
|
|
|
|
JOIN {context} ctx
|
|
|
|
ON cc.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSECAT."
|
|
|
|
WHERE cc.parent=?
|
2007-09-19 07:27:20 +00:00
|
|
|
$sort";
|
2008-05-25 15:16:17 +00:00
|
|
|
$params = array($parent);
|
|
|
|
|
2003-08-15 13:59:24 +00:00
|
|
|
} else {
|
2007-09-19 07:27:20 +00:00
|
|
|
$sql = "SELECT cc.*,
|
2007-09-19 07:54:37 +00:00
|
|
|
ctx.id AS ctxid, ctx.path AS ctxpath,
|
|
|
|
ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
|
2008-05-25 15:16:17 +00:00
|
|
|
FROM {course_categories} cc
|
|
|
|
JOIN {context} ctx
|
|
|
|
ON cc.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSECAT."
|
|
|
|
JOIN {course_categories} ccp
|
|
|
|
ON (cc.path LIKE ".$DB->sql_concat('ccp.path',"'%'").")
|
|
|
|
WHERE ccp.id=?
|
2007-09-19 07:27:20 +00:00
|
|
|
$sort";
|
2008-05-25 15:16:17 +00:00
|
|
|
$params = array($parent);
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
2007-09-19 07:27:20 +00:00
|
|
|
$categories = array();
|
|
|
|
|
2008-05-25 15:16:17 +00:00
|
|
|
if( $rs = $DB->get_recordset_sql($sql, $params) ){
|
|
|
|
foreach($rs as $cat) {
|
2007-09-19 07:27:20 +00:00
|
|
|
$cat = make_context_subobj($cat);
|
2008-07-16 15:12:09 +00:00
|
|
|
if ($cat->visible || has_capability('moodle/category:visibility',$cat->context)) {
|
2007-09-19 07:27:20 +00:00
|
|
|
$categories[$cat->id] = $cat;
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-25 15:16:17 +00:00
|
|
|
$rs->close();
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
return $categories;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-08 08:06:08 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of category ids of all the subcategories for a given
|
|
|
|
* category.
|
|
|
|
* @param $catid - The id of the category whose subcategories we want to find.
|
|
|
|
* @return array of category ids.
|
|
|
|
*/
|
|
|
|
function get_all_subcategories($catid) {
|
2008-05-25 15:16:17 +00:00
|
|
|
global $DB;
|
2007-02-08 08:06:08 +00:00
|
|
|
|
|
|
|
$subcats = array();
|
|
|
|
|
2008-05-25 15:16:17 +00:00
|
|
|
if ($categories = $DB->get_records('course_categories', array('parent'=>$catid))) {
|
2007-02-08 08:06:08 +00:00
|
|
|
foreach ($categories as $cat) {
|
|
|
|
array_push($subcats, $cat->id);
|
|
|
|
$subcats = array_merge($subcats, get_all_subcategories($cat->id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $subcats;
|
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2008-06-16 14:25:53 +00:00
|
|
|
* Return specified category, default if given does not exist
|
|
|
|
* @param int $catid course category id
|
|
|
|
* @return object caregory
|
|
|
|
*/
|
|
|
|
function get_course_category($catid=0) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$category = false;
|
|
|
|
|
|
|
|
if (!empty($catid)) {
|
|
|
|
$category = $DB->get_record('course_categories', array('id'=>$catid));
|
|
|
|
}
|
2004-09-21 11:41:58 +00:00
|
|
|
|
2008-06-16 14:25:53 +00:00
|
|
|
if (!$category) {
|
|
|
|
// the first category is considered default for now
|
|
|
|
if ($category = $DB->get_records('course_categories', null, 'sortorder', '*', 0, 1)) {
|
|
|
|
$category = reset($category);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$cat = new object();
|
|
|
|
$cat->name = get_string('miscellaneous');
|
|
|
|
$cat->depth = 1;
|
|
|
|
$cat->sortorder = MAX_COURSES_IN_CATEGORY;
|
|
|
|
$cat->timemodified = time();
|
|
|
|
if (!$catid = $DB->insert_record('course_categories', $cat)) {
|
|
|
|
print_error('cannotsetupcategory', 'error');
|
|
|
|
}
|
|
|
|
// make sure category context exists
|
|
|
|
get_context_instance(CONTEXT_COURSECAT, $catid);
|
|
|
|
mark_context_dirty('/'.SYSCONTEXTID);
|
|
|
|
$category = $DB->get_record('course_categories', array('id'=>$catid));
|
2005-08-16 23:15:58 +00:00
|
|
|
}
|
2008-06-16 14:25:53 +00:00
|
|
|
}
|
2004-11-17 06:57:28 +00:00
|
|
|
|
2008-06-16 14:25:53 +00:00
|
|
|
return $category;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fixes course category and course sortorder, also verifies category and course parents and paths.
|
|
|
|
* (circular references are not fixed)
|
|
|
|
*/
|
|
|
|
function fix_course_sortorder() {
|
|
|
|
global $DB, $SITE;
|
|
|
|
|
|
|
|
//WARNING: this is PHP5 only code!
|
|
|
|
|
|
|
|
if ($unsorted = $DB->get_records('course_categories', array('sortorder'=>0))) {
|
|
|
|
//move all categories that are not sorted yet to the end
|
|
|
|
$DB->set_field('course_categories', 'sortorder', MAX_COURSES_IN_CATEGORY*MAX_COURSE_CATEGORIES, array('sortorder'=>0));
|
|
|
|
}
|
|
|
|
|
|
|
|
$allcats = $DB->get_records('course_categories', null, 'sortorder, id', 'id, sortorder, parent, depth, path');
|
|
|
|
$topcats = array();
|
|
|
|
$brokencats = array();
|
|
|
|
foreach ($allcats as $cat) {
|
|
|
|
$sortorder = (int)$cat->sortorder;
|
|
|
|
if (!$cat->parent) {
|
|
|
|
while(isset($topcats[$sortorder])) {
|
|
|
|
$sortorder++;
|
|
|
|
}
|
|
|
|
$topcats[$sortorder] = $cat;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!isset($allcats[$cat->parent])) {
|
|
|
|
$brokencats[] = $cat;
|
|
|
|
continue;
|
2007-09-19 07:27:08 +00:00
|
|
|
}
|
2008-06-16 14:25:53 +00:00
|
|
|
if (!isset($allcats[$cat->parent]->children)) {
|
|
|
|
$allcats[$cat->parent]->children = array();
|
2007-09-19 07:27:08 +00:00
|
|
|
}
|
2008-06-16 14:25:53 +00:00
|
|
|
while(isset($allcats[$cat->parent]->children[$sortorder])) {
|
|
|
|
$sortorder++;
|
|
|
|
}
|
|
|
|
$allcats[$cat->parent]->children[$sortorder] = $cat;
|
2005-08-16 23:15:58 +00:00
|
|
|
}
|
2008-06-16 14:25:53 +00:00
|
|
|
unset($allcats);
|
2005-01-13 02:34:45 +00:00
|
|
|
|
2008-06-16 14:25:53 +00:00
|
|
|
// add broken cats to category tree
|
|
|
|
if ($brokencats) {
|
|
|
|
$defaultcat = reset($topcats);
|
|
|
|
foreach ($brokencats as $cat) {
|
|
|
|
$topcats[] = $cat;
|
|
|
|
}
|
2004-11-17 06:57:28 +00:00
|
|
|
}
|
|
|
|
|
2008-06-16 14:25:53 +00:00
|
|
|
// now walk recursively the tree and fix any problems found
|
|
|
|
$sortorder = 0;
|
|
|
|
$fixcontexts = array();
|
|
|
|
_fix_course_cats($topcats, $sortorder, 0, 0, '', $fixcontexts);
|
|
|
|
|
|
|
|
// detect if there are "multiple" frontpage courses and fix them if needed
|
|
|
|
$frontcourses = $DB->get_records('course', array('category'=>0), 'id');
|
|
|
|
if (count($frontcourses) > 1) {
|
|
|
|
if (isset($frontcourses[SITEID])) {
|
|
|
|
$frontcourse = $frontcourses[SITEID];
|
|
|
|
unset($frontcourses[SITEID]);
|
|
|
|
} else {
|
|
|
|
$frontcourse = array_shift($frontcourses);
|
|
|
|
}
|
|
|
|
$defaultcat = reset($topcats);
|
|
|
|
foreach ($frontcourses as $course) {
|
|
|
|
$DB->set_field('course', 'category', $defaultcat->id, array('id'=>$course->id));
|
|
|
|
$context = get_context_instance(CONTEXT_COURSE, $course->id);
|
|
|
|
$fixcontexts[$context->id] = $context;
|
|
|
|
}
|
|
|
|
unset($frontcourses);
|
|
|
|
} else {
|
|
|
|
$frontcourse = reset($frontcourses);
|
2005-02-23 01:49:22 +00:00
|
|
|
}
|
|
|
|
|
2008-06-16 14:25:53 +00:00
|
|
|
// now fix the paths and depths in context table if needed
|
|
|
|
if ($fixcontexts) {
|
|
|
|
rebuild_contexts($fixcontexts);
|
2005-01-13 02:34:45 +00:00
|
|
|
}
|
2007-08-02 23:39:28 +00:00
|
|
|
|
2008-06-16 14:25:53 +00:00
|
|
|
// release memory
|
|
|
|
unset($topcats);
|
|
|
|
unset($brokencats);
|
|
|
|
unset($fixcontexts);
|
|
|
|
|
|
|
|
// fix frontpage course sortorder
|
|
|
|
if ($frontcourse->sortorder != 1) {
|
|
|
|
$DB->set_field('course', 'sortorder', 1, array('id'=>$frontcourse->id));
|
2005-01-13 02:34:45 +00:00
|
|
|
}
|
|
|
|
|
2008-06-16 14:25:53 +00:00
|
|
|
// now fix the course counts in category records if needed
|
|
|
|
$sql = "SELECT cc.id, cc.coursecount, COUNT(c.id) AS newcount
|
|
|
|
FROM {course_categories} cc
|
|
|
|
LEFT JOIN {course} c ON c.category = cc.id
|
|
|
|
GROUP BY cc.id, cc.coursecount
|
|
|
|
HAVING cc.coursecount <> COUNT(c.id)";
|
2004-11-17 06:57:28 +00:00
|
|
|
|
2008-06-16 14:25:53 +00:00
|
|
|
if ($updatecounts = $DB->get_records_sql($sql)) {
|
|
|
|
foreach ($updatecounts as $cat) {
|
|
|
|
$cat->coursecount = $cat->newcount;
|
|
|
|
unset($cat->newcount);
|
|
|
|
$DB->update_record_raw('course_categories', $cat, true);
|
|
|
|
}
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
2004-09-21 11:41:58 +00:00
|
|
|
|
2008-06-16 14:25:53 +00:00
|
|
|
// now make sure that sortorders in course table are withing the category sortorder ranges
|
|
|
|
$sql = "SELECT cc.id, cc.sortorder
|
|
|
|
FROM {course_categories} cc
|
|
|
|
JOIN {course} c ON c.category = cc.id
|
|
|
|
WHERE c.sortorder < cc.sortorder OR c.sortorder > cc.sortorder + ".MAX_COURSES_IN_CATEGORY;
|
|
|
|
|
|
|
|
if ($fixcategories = $DB->get_records_sql($sql)) {
|
|
|
|
//fix the course sortorder ranges
|
|
|
|
foreach ($fixcategories as $cat) {
|
|
|
|
$sql = "UPDATE {course}
|
|
|
|
SET sortorder = (sortorder % ".MAX_COURSES_IN_CATEGORY.") + ?
|
|
|
|
WHERE category = ?";
|
|
|
|
$DB->execute($sql, array($cat->sortorder, $cat->id));
|
|
|
|
}
|
2005-02-23 01:49:22 +00:00
|
|
|
}
|
2008-06-16 14:25:53 +00:00
|
|
|
unset($fixcategories);
|
|
|
|
|
|
|
|
// categories having courses with sortorder duplicates or having gaps in sortorder
|
|
|
|
$sql = "SELECT DISTINCT c1.category AS id , cc.sortorder
|
|
|
|
FROM {course} c1
|
|
|
|
JOIN {course} c2 ON c1.sortorder = c2.sortorder
|
|
|
|
JOIN {course_categories} cc ON (c1.category = cc.id)
|
|
|
|
WHERE c1.id <> c2.id";
|
|
|
|
$fixcategories = $DB->get_records_sql($sql);
|
|
|
|
|
|
|
|
$sql = "SELECT cc.id, cc.sortorder, cc.coursecount, MAX(c.sortorder) AS maxsort, MIN(c.sortorder) AS minsort
|
|
|
|
FROM {course_categories} cc
|
|
|
|
JOIN {course} c ON c.category = cc.id
|
|
|
|
GROUP BY cc.id, cc.sortorder, cc.coursecount
|
|
|
|
HAVING (MAX(c.sortorder) <> cc.sortorder + cc.coursecount) OR (MIN(c.sortorder) <> cc.sortorder + 1)";
|
|
|
|
$gapcategories = $DB->get_records_sql($sql);
|
|
|
|
|
|
|
|
foreach ($gapcategories as $cat) {
|
|
|
|
if (isset($fixcategories[$cat->id])) {
|
|
|
|
// duplicates detected already
|
|
|
|
|
|
|
|
} else if ($cat->minsort == $cat->sortorder and $cat->maxsort == $cat->sortorder + $cat->coursecount - 1) {
|
|
|
|
// easy - new course inserted with sortorder 0, the rest is ok
|
|
|
|
$sql = "UPDATE {course}
|
|
|
|
SET sortorder = sortorder + 1
|
|
|
|
WHERE category = ?";
|
|
|
|
$DB->execute($sql, array($cat->id));
|
2005-01-25 05:27:41 +00:00
|
|
|
|
2008-06-16 14:25:53 +00:00
|
|
|
} else {
|
|
|
|
// it needs full resorting
|
|
|
|
$fixcategories[$cat->id] = $cat;
|
2004-05-30 00:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
2008-06-16 14:25:53 +00:00
|
|
|
unset($gapcategories);
|
2004-09-21 11:41:58 +00:00
|
|
|
|
2008-06-16 14:25:53 +00:00
|
|
|
// fix course sortorders in problematic categories only
|
|
|
|
foreach ($fixcategories as $cat) {
|
|
|
|
$i = 1;
|
|
|
|
$courses = $DB->get_records('course', array('category'=>$cat->id), 'sortorder ASC, id DESC', 'id, sortorder');
|
|
|
|
foreach ($courses as $course) {
|
|
|
|
if ($course->sortorder != $cat->sortorder + $i) {
|
|
|
|
$course->sortorder = $cat->sortorder + $i;
|
|
|
|
$DB->update_record_raw('course', $course, true);
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
|
2007-09-12 02:56:36 +00:00
|
|
|
/**
|
2008-06-16 14:25:53 +00:00
|
|
|
* Internal recursive category verification function, do not use directly!
|
|
|
|
*/
|
|
|
|
function _fix_course_cats($children, &$sortorder, $parent, $depth, $path, &$fixcontexts) {
|
2008-05-30 16:47:21 +00:00
|
|
|
global $DB;
|
2007-09-12 02:56:36 +00:00
|
|
|
|
2008-06-16 14:25:53 +00:00
|
|
|
$depth++;
|
2008-05-30 16:47:21 +00:00
|
|
|
|
2008-06-16 14:25:53 +00:00
|
|
|
foreach ($children as $cat) {
|
|
|
|
$sortorder = $sortorder + MAX_COURSES_IN_CATEGORY;
|
|
|
|
$update = false;
|
|
|
|
if ($parent != $cat->parent or $depth != $cat->depth or $path.'/'.$cat->id != $cat->path) {
|
|
|
|
$cat->parent = $parent;
|
|
|
|
$cat->depth = $depth;
|
|
|
|
$cat->path = $path.'/'.$cat->id;
|
|
|
|
$update = true;
|
2008-05-30 16:47:21 +00:00
|
|
|
|
2008-06-16 14:25:53 +00:00
|
|
|
// make sure context caches are rebuild and dirty contexts marked
|
|
|
|
$context = get_context_instance(CONTEXT_COURSECAT, $cat->id);
|
|
|
|
$fixcontexts[$context->id] = $context;
|
|
|
|
}
|
|
|
|
if ($cat->sortorder != $sortorder) {
|
|
|
|
$cat->sortorder = $sortorder;
|
|
|
|
$update = true;
|
|
|
|
}
|
|
|
|
if ($update) {
|
|
|
|
$DB->update_record('course_categories', $cat, true);
|
|
|
|
}
|
|
|
|
if (isset($cat->children)) {
|
|
|
|
_fix_course_cats($cat->children, $sortorder, $cat->id, $cat->depth, $cat->path, $fixcontexts);
|
2007-09-12 02:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-19 08:57:13 +00:00
|
|
|
/**
|
|
|
|
* List of remote courses that a user has access to via MNET.
|
|
|
|
* Works only on the IDP
|
|
|
|
*
|
|
|
|
* @uses $CFG, $USER
|
|
|
|
* @return array {@link $COURSE} of course objects
|
|
|
|
*/
|
|
|
|
function get_my_remotecourses($userid=0) {
|
2008-05-30 16:47:21 +00:00
|
|
|
global $DB, $USER;
|
2007-01-19 08:57:13 +00:00
|
|
|
|
|
|
|
if (empty($userid)) {
|
|
|
|
$userid = $USER->id;
|
|
|
|
}
|
|
|
|
|
2007-08-02 23:39:28 +00:00
|
|
|
$sql = "SELECT c.remoteid, c.shortname, c.fullname,
|
2007-01-19 09:23:47 +00:00
|
|
|
c.hostid, c.summary, c.cat_name,
|
|
|
|
h.name AS hostname
|
2008-05-30 16:47:21 +00:00
|
|
|
FROM {mnet_enrol_course} c
|
|
|
|
JOIN {mnet_enrol_assignments} a ON c.id=a.courseid
|
|
|
|
JOIN {mnet_host} h ON c.hostid=h.id
|
|
|
|
WHERE a.userid=?";
|
2007-01-19 08:57:13 +00:00
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
return $DB->get_records_sql($sql, array($userid));
|
2007-01-19 08:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of remote hosts that a user has access to via MNET.
|
|
|
|
* Works on the SP
|
|
|
|
*
|
|
|
|
* @uses $CFG, $USER
|
|
|
|
* @return array of host objects
|
|
|
|
*/
|
|
|
|
function get_my_remotehosts() {
|
|
|
|
global $CFG, $USER;
|
|
|
|
|
|
|
|
if ($USER->mnethostid == $CFG->mnet_localhost_id) {
|
|
|
|
return false; // Return nothing on the IDP
|
|
|
|
}
|
|
|
|
if (!empty($USER->mnet_foreign_host_array) && is_array($USER->mnet_foreign_host_array)) {
|
|
|
|
return $USER->mnet_foreign_host_array;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2004-09-24 21:28:22 +00:00
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2004-09-24 21:28:22 +00:00
|
|
|
* This function creates a default separated/connected scale
|
|
|
|
*
|
|
|
|
* This function creates a default separated/connected scale
|
|
|
|
* so there's something in the database. The locations of
|
|
|
|
* strings and files is a bit odd, but this is because we
|
|
|
|
* need to maintain backward compatibility with many different
|
|
|
|
* existing language translations and older sites.
|
|
|
|
*/
|
2003-08-15 13:59:24 +00:00
|
|
|
function make_default_scale() {
|
2008-05-30 16:47:21 +00:00
|
|
|
global $CFG, $DB;
|
2003-08-15 13:59:24 +00:00
|
|
|
|
|
|
|
$defaultscale = NULL;
|
|
|
|
$defaultscale->courseid = 0;
|
|
|
|
$defaultscale->userid = 0;
|
2004-09-22 16:15:23 +00:00
|
|
|
$defaultscale->name = get_string('separateandconnected');
|
|
|
|
$defaultscale->scale = get_string('postrating1', 'forum').','.
|
|
|
|
get_string('postrating2', 'forum').','.
|
|
|
|
get_string('postrating3', 'forum');
|
2003-08-15 13:59:24 +00:00
|
|
|
$defaultscale->timemodified = time();
|
|
|
|
|
2004-09-21 11:41:58 +00:00
|
|
|
/// Read in the big description from the file. Note this is not
|
2003-08-15 13:59:24 +00:00
|
|
|
/// HTML (despite the file extension) but Moodle format text.
|
2008-02-26 07:29:09 +00:00
|
|
|
$parentlang = get_string('parentlanguage');
|
|
|
|
if ($parentlang[0] == '[') {
|
|
|
|
$parentlang = '';
|
|
|
|
}
|
2006-06-04 15:19:05 +00:00
|
|
|
if (is_readable($CFG->dataroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html')) {
|
|
|
|
$file = file($CFG->dataroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html');
|
|
|
|
} else if (is_readable($CFG->dirroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html')) {
|
2004-09-22 16:15:23 +00:00
|
|
|
$file = file($CFG->dirroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html');
|
2006-06-04 15:19:05 +00:00
|
|
|
} else if ($parentlang and is_readable($CFG->dataroot .'/lang/'. $parentlang .'/help/forum/ratings.html')) {
|
|
|
|
$file = file($CFG->dataroot .'/lang/'. $parentlang .'/help/forum/ratings.html');
|
2004-09-22 16:15:23 +00:00
|
|
|
} else if ($parentlang and is_readable($CFG->dirroot .'/lang/'. $parentlang .'/help/forum/ratings.html')) {
|
|
|
|
$file = file($CFG->dirroot .'/lang/'. $parentlang .'/help/forum/ratings.html');
|
2006-06-04 15:19:05 +00:00
|
|
|
} else if (is_readable($CFG->dirroot .'/lang/en_utf8/help/forum/ratings.html')) {
|
|
|
|
$file = file($CFG->dirroot .'/lang/en_utf8/help/forum/ratings.html');
|
2003-08-15 13:59:24 +00:00
|
|
|
} else {
|
2004-09-22 16:15:23 +00:00
|
|
|
$file = '';
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
$defaultscale->description = implode('', $file);
|
2003-08-15 13:59:24 +00:00
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
if ($defaultscale->id = $DB->insert_record('scale', $defaultscale)) {
|
|
|
|
$DB->execute("UPDATE {forum} SET scale = ?", array($defaultscale->id));
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-24 21:28:22 +00:00
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2004-09-24 21:28:22 +00:00
|
|
|
* Returns a menu of all available scales from the site as well as the given course
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @param int $courseid The id of the course as found in the 'course' table.
|
2005-07-12 02:23:58 +00:00
|
|
|
* @return object
|
2004-09-24 21:28:22 +00:00
|
|
|
*/
|
2003-08-15 13:59:24 +00:00
|
|
|
function get_scales_menu($courseid=0) {
|
2008-05-30 16:47:21 +00:00
|
|
|
global $DB;
|
2003-08-15 13:59:24 +00:00
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
$sql = "SELECT id, name
|
|
|
|
FROM {scale}
|
|
|
|
WHERE courseid = 0 or courseid = ?
|
2003-08-15 13:59:24 +00:00
|
|
|
ORDER BY courseid ASC, name ASC";
|
2008-05-30 16:47:21 +00:00
|
|
|
$params = array($courseid);
|
2003-08-15 13:59:24 +00:00
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
if ($scales = $DB->get_records_sql_menu($sql, $params)) {
|
2003-08-15 13:59:24 +00:00
|
|
|
return $scales;
|
|
|
|
}
|
|
|
|
|
|
|
|
make_default_scale();
|
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
return $DB->get_records_sql_menu($sql, $params);
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
|
2005-04-10 09:15:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a set of timezone records, put them in the database, replacing what is there
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @param array $timezones An array of timezone records
|
|
|
|
*/
|
|
|
|
function update_timezone_records($timezones) {
|
2008-05-30 16:47:21 +00:00
|
|
|
global $DB;
|
2005-04-10 09:15:15 +00:00
|
|
|
|
|
|
|
/// Clear out all the old stuff
|
2008-05-31 15:32:28 +00:00
|
|
|
$DB->delete_records('timezone');
|
2005-04-10 09:15:15 +00:00
|
|
|
|
|
|
|
/// Insert all the new stuff
|
|
|
|
foreach ($timezones as $timezone) {
|
2007-12-31 15:20:02 +00:00
|
|
|
if (is_array($timezone)) {
|
|
|
|
$timezone = (object)$timezone;
|
|
|
|
}
|
2008-05-30 16:47:21 +00:00
|
|
|
$DB->insert_record('timezone', $timezone);
|
2005-04-10 09:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-17 04:41:18 +00:00
|
|
|
/// MODULE FUNCTIONS /////////////////////////////////////////////////
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2004-09-24 21:28:22 +00:00
|
|
|
* Just gets a raw list of all modules in a course
|
|
|
|
*
|
|
|
|
* @param int $courseid The id of the course as found in the 'course' table.
|
2005-07-12 02:23:58 +00:00
|
|
|
* @return object
|
2004-09-24 21:28:22 +00:00
|
|
|
*/
|
2002-12-20 14:44:14 +00:00
|
|
|
function get_course_mods($courseid) {
|
2008-05-30 16:47:21 +00:00
|
|
|
global $DB;
|
2002-12-20 14:44:14 +00:00
|
|
|
|
2006-02-14 02:01:31 +00:00
|
|
|
if (empty($courseid)) {
|
|
|
|
return false; // avoid warnings
|
|
|
|
}
|
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
return $DB->get_records_sql("SELECT cm.*, m.name as modname
|
|
|
|
FROM {modules} m, {course_modules} cm
|
|
|
|
WHERE cm.course = ? AND cm.module = m.id AND m.visible = 1",
|
|
|
|
array($courseid)); // no disabled mods
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
2004-09-24 21:28:22 +00:00
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2006-08-08 22:09:55 +00:00
|
|
|
* Given an id of a course module, finds the coursemodule description
|
2004-09-24 21:28:22 +00:00
|
|
|
*
|
2006-08-08 22:09:55 +00:00
|
|
|
* @param string $modulename name of module type, eg. resource, assignment,...
|
|
|
|
* @param int $cmid course module id (id in course_modules table)
|
|
|
|
* @param int $courseid optional course id for extra validation
|
|
|
|
* @return object course module instance with instance and module name
|
|
|
|
*/
|
|
|
|
function get_coursemodule_from_id($modulename, $cmid, $courseid=0) {
|
2008-05-30 16:47:21 +00:00
|
|
|
global $DB;
|
2006-08-08 22:09:55 +00:00
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
$params = array();
|
2008-05-30 16:51:01 +00:00
|
|
|
$courseselect = "";
|
2006-08-08 22:09:55 +00:00
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
if ($courseid) {
|
|
|
|
$courseselect = "cm.course = :courseid AND ";
|
|
|
|
$params['courseid'] = $courseid;
|
2008-05-30 16:51:01 +00:00
|
|
|
}
|
2008-05-30 16:47:21 +00:00
|
|
|
$params['cmid'] = $cmid;
|
|
|
|
$params['modulename'] = $modulename;
|
|
|
|
|
|
|
|
return $DB->get_record_sql("SELECT cm.*, m.name, md.name as modname
|
|
|
|
FROM {course_modules} cm, {modules} md, {".$modulename."} m
|
|
|
|
WHERE $courseselect
|
|
|
|
cm.id = :cmid AND
|
|
|
|
cm.instance = m.id AND
|
|
|
|
md.name = :modulename AND
|
|
|
|
md.id = cm.module", $params);
|
2006-08-08 22:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an instance number of a module, finds the coursemodule description
|
|
|
|
*
|
|
|
|
* @param string $modulename name of module type, eg. resource, assignment,...
|
|
|
|
* @param int $instance module instance number (id in resource, assignment etc. table)
|
|
|
|
* @param int $courseid optional course id for extra validation
|
|
|
|
* @return object course module instance with instance and module name
|
2004-09-24 21:28:22 +00:00
|
|
|
*/
|
2004-12-26 13:58:07 +00:00
|
|
|
function get_coursemodule_from_instance($modulename, $instance, $courseid=0) {
|
2008-05-30 16:47:21 +00:00
|
|
|
global $DB;
|
2002-12-17 04:41:18 +00:00
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
$params = array();
|
2008-05-30 16:51:01 +00:00
|
|
|
$courseselect = "";
|
2002-12-17 04:41:18 +00:00
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
if ($courseid) {
|
|
|
|
$courseselect = "cm.course = :courseid AND ";
|
|
|
|
$params['courseid'] = $courseid;
|
2008-05-30 16:51:01 +00:00
|
|
|
}
|
2008-05-30 16:47:21 +00:00
|
|
|
$params['instance'] = $instance;
|
|
|
|
$params['modulename'] = $modulename;
|
|
|
|
|
|
|
|
return $DB->get_record_sql("SELECT cm.*, m.name, md.name as modname
|
|
|
|
FROM {course_modules} cm, {modules} md, {".$modulename."} m
|
|
|
|
WHERE $courseselect
|
|
|
|
cm.instance = m.id AND
|
2008-06-02 14:18:18 +00:00
|
|
|
md.name = :modulename AND
|
2008-05-30 16:47:21 +00:00
|
|
|
md.id = cm.module AND
|
2008-06-02 14:18:18 +00:00
|
|
|
m.id = :instance", $params);
|
2002-12-17 04:41:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-01-24 20:33:50 +00:00
|
|
|
/**
|
|
|
|
* Returns all course modules of given activity in course
|
|
|
|
* @param string $modulename (forum, quiz, etc.)
|
|
|
|
* @param int $courseid
|
|
|
|
* @param string $extrafields extra fields starting with m.
|
|
|
|
* @return array of cm objects, false if not found or error
|
|
|
|
*/
|
|
|
|
function get_coursemodules_in_course($modulename, $courseid, $extrafields='') {
|
2008-05-30 16:47:21 +00:00
|
|
|
global $DB;
|
2008-01-24 20:33:50 +00:00
|
|
|
|
|
|
|
if (!empty($extrafields)) {
|
|
|
|
$extrafields = ", $extrafields";
|
|
|
|
}
|
2008-05-30 16:47:21 +00:00
|
|
|
$params = array();
|
|
|
|
$params['courseid'] = $courseid;
|
|
|
|
$params['modulename'] = $modulename;
|
|
|
|
|
|
|
|
|
|
|
|
return $DB->get_records_sql("SELECT cm.*, m.name, md.name as modname $extrafields
|
|
|
|
FROM {course_modules} cm, {modules} md, {".$modulename."} m
|
|
|
|
WHERE cm.course = :courseid AND
|
|
|
|
cm.instance = m.id AND
|
|
|
|
md.name = :modulename AND
|
2008-06-02 10:41:59 +00:00
|
|
|
md.id = cm.module", $params);
|
2008-01-24 20:33:50 +00:00
|
|
|
}
|
2008-02-05 21:34:58 +00:00
|
|
|
|
2006-01-17 20:49:43 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of all the active instances of a particular module in given courses, sorted in the order they are defined
|
|
|
|
*
|
|
|
|
* Returns an array of all the active instances of a particular
|
|
|
|
* module in given courses, sorted in the order they are defined
|
2008-02-05 21:34:58 +00:00
|
|
|
* in the course. Returns an empty array on any errors.
|
2006-01-17 20:49:43 +00:00
|
|
|
*
|
2008-02-05 21:34:58 +00:00
|
|
|
* The returned objects includle the columns cw.section, cm.visible,
|
|
|
|
* cm.groupmode and cm.groupingid, cm.groupmembersonly, and are indexed by cm.id.
|
|
|
|
*
|
|
|
|
* @param string $modulename The name of the module to get instances for
|
|
|
|
* @param array $courses an array of course objects.
|
|
|
|
* @return array of module instance objects, including some extra fields from the course_modules
|
|
|
|
* and course_sections tables, or an empty array if an error occurred.
|
2006-01-17 20:49:43 +00:00
|
|
|
*/
|
2006-10-24 20:22:30 +00:00
|
|
|
function get_all_instances_in_courses($modulename, $courses, $userid=NULL, $includeinvisible=false) {
|
2008-05-30 16:47:21 +00:00
|
|
|
global $CFG, $DB;
|
2008-02-05 21:34:58 +00:00
|
|
|
|
|
|
|
$outputarray = array();
|
|
|
|
|
2006-01-17 20:49:43 +00:00
|
|
|
if (empty($courses) || !is_array($courses) || count($courses) == 0) {
|
2008-02-05 21:34:58 +00:00
|
|
|
return $outputarray;
|
2006-01-17 20:49:43 +00:00
|
|
|
}
|
2008-02-05 21:34:58 +00:00
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
list($coursessql, $params) = $DB->get_in_or_equal(array_keys($courses), SQL_PARAMS_NAMED, 'c0');
|
|
|
|
$params['modulename'] = $modulename;
|
|
|
|
|
|
|
|
if (!$rawmods = $DB->get_records_sql("SELECT cm.id AS coursemodule, m.*, cw.section, cm.visible AS visible,
|
|
|
|
cm.groupmode, cm.groupingid, cm.groupmembersonly
|
|
|
|
FROM {course_modules} cm, {course_sections} cw, {modules} md,
|
|
|
|
{".$modulename."} m
|
|
|
|
WHERE cm.course $coursessql AND
|
|
|
|
cm.instance = m.id AND
|
|
|
|
cm.section = cw.id AND
|
|
|
|
md.name = :modulename AND
|
|
|
|
md.id = cm.module", $params)) {
|
2008-02-05 21:34:58 +00:00
|
|
|
return $outputarray;
|
2006-01-17 20:49:43 +00:00
|
|
|
}
|
|
|
|
|
2008-02-05 21:34:58 +00:00
|
|
|
require_once($CFG->dirroot.'/course/lib.php');
|
2006-01-17 20:49:43 +00:00
|
|
|
|
|
|
|
foreach ($courses as $course) {
|
2008-02-05 21:34:58 +00:00
|
|
|
$modinfo = get_fast_modinfo($course, $userid);
|
2006-09-04 21:12:37 +00:00
|
|
|
|
2008-02-05 21:34:58 +00:00
|
|
|
if (empty($modinfo->instances[$modulename])) {
|
2006-01-17 20:49:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-02-05 21:34:58 +00:00
|
|
|
|
|
|
|
foreach ($modinfo->instances[$modulename] as $cm) {
|
|
|
|
if (!$includeinvisible and !$cm->uservisible) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!isset($rawmods[$cm->id])) {
|
|
|
|
continue;
|
2006-01-17 20:49:43 +00:00
|
|
|
}
|
2008-02-05 21:34:58 +00:00
|
|
|
$instance = $rawmods[$cm->id];
|
|
|
|
if (!empty($cm->extra)) {
|
|
|
|
$instance->extra = urlencode($cm->extra); // bc compatibility
|
|
|
|
}
|
|
|
|
$outputarray[] = $instance;
|
2006-01-17 20:49:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $outputarray;
|
|
|
|
}
|
2004-09-24 21:28:22 +00:00
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2007-12-12 17:10:48 +00:00
|
|
|
* Returns an array of all the active instances of a particular module in a given course,
|
|
|
|
* sorted in the order they are defined.
|
2004-09-24 21:28:22 +00:00
|
|
|
*
|
|
|
|
* Returns an array of all the active instances of a particular
|
|
|
|
* module in a given course, sorted in the order they are defined
|
2007-12-12 17:10:48 +00:00
|
|
|
* in the course. Returns an empty array on any errors.
|
|
|
|
*
|
|
|
|
* The returned objects includle the columns cw.section, cm.visible,
|
2008-02-05 21:34:58 +00:00
|
|
|
* cm.groupmode and cm.groupingid, cm.groupmembersonly, and are indexed by cm.id.
|
2004-09-24 21:28:22 +00:00
|
|
|
*
|
2007-12-12 17:10:48 +00:00
|
|
|
* @param string $modulename The name of the module to get instances for
|
2008-02-05 21:34:58 +00:00
|
|
|
* @param object $course The course obect.
|
2007-12-12 17:10:48 +00:00
|
|
|
* @return array of module instance objects, including some extra fields from the course_modules
|
|
|
|
* and course_sections tables, or an empty array if an error occurred.
|
2004-09-24 21:28:22 +00:00
|
|
|
*/
|
2006-10-24 20:22:30 +00:00
|
|
|
function get_all_instances_in_course($modulename, $course, $userid=NULL, $includeinvisible=false) {
|
2008-02-05 21:34:58 +00:00
|
|
|
return get_all_instances_in_courses($modulename, array($course->id => $course), $userid, $includeinvisible);
|
2002-12-17 04:41:18 +00:00
|
|
|
}
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2004-09-24 21:28:22 +00:00
|
|
|
* Determine whether a module instance is visible within a course
|
|
|
|
*
|
|
|
|
* Given a valid module object with info about the id and course,
|
|
|
|
* and the module's type (eg "forum") returns whether the object
|
2008-01-24 20:33:50 +00:00
|
|
|
* is visible or not, groupmembersonly visibility not tested
|
2004-09-24 21:28:22 +00:00
|
|
|
*
|
2006-09-10 07:07:52 +00:00
|
|
|
* @param $moduletype Name of the module eg 'forum'
|
|
|
|
* @param $module Object which is the instance of the module
|
2005-07-12 02:23:58 +00:00
|
|
|
* @return bool
|
2004-09-24 21:28:22 +00:00
|
|
|
*/
|
2003-04-25 05:24:29 +00:00
|
|
|
function instance_is_visible($moduletype, $module) {
|
2008-05-30 16:47:21 +00:00
|
|
|
global $DB;
|
2003-04-25 05:24:29 +00:00
|
|
|
|
2004-11-18 02:37:52 +00:00
|
|
|
if (!empty($module->id)) {
|
2008-05-30 16:51:01 +00:00
|
|
|
$params = array('courseid'=>$module->course, 'moduletype'=>$moduletype, 'moduleid'=>$module->id);
|
2008-05-30 16:47:21 +00:00
|
|
|
if ($records = $DB->get_records_sql("SELECT cm.instance, cm.visible, cm.groupingid, cm.id, cm.groupmembersonly, cm.course
|
|
|
|
FROM {course_modules} cm, {modules} m
|
|
|
|
WHERE cm.course = :courseid AND
|
|
|
|
cm.module = m.id AND
|
|
|
|
m.name = :moduletype AND
|
2008-06-04 07:54:58 +00:00
|
|
|
cm.instance = :moduleid", $params)) {
|
2007-08-02 23:39:28 +00:00
|
|
|
|
2004-11-18 02:37:52 +00:00
|
|
|
foreach ($records as $record) { // there should only be one - use the first one
|
2008-01-24 20:33:50 +00:00
|
|
|
return $record->visible;
|
2004-11-18 02:37:52 +00:00
|
|
|
}
|
2003-04-25 05:24:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true; // visible by default!
|
|
|
|
}
|
|
|
|
|
2008-01-24 20:33:50 +00:00
|
|
|
/**
|
|
|
|
* Determine whether a course module is visible within a course,
|
|
|
|
* this is different from instance_is_visible() - faster and visibility for user
|
|
|
|
*
|
|
|
|
* @param object $cm object
|
|
|
|
* @param int $userid empty means current user
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function coursemodule_visible_for_user($cm, $userid=0) {
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if (empty($cm->id)) {
|
|
|
|
debugging("Incorrect course module parameter!", DEBUG_DEVELOPER);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (empty($userid)) {
|
|
|
|
$userid = $USER->id;
|
|
|
|
}
|
|
|
|
if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_MODULE, $cm->id), $userid)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return groups_course_module_visible($cm, $userid);
|
|
|
|
}
|
|
|
|
|
2003-01-03 15:31:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
/// LOG FUNCTIONS /////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2004-09-24 21:28:22 +00:00
|
|
|
* Add an entry to the log table.
|
|
|
|
*
|
|
|
|
* Add an entry to the log table. These are "action" focussed rather
|
|
|
|
* than web server hits, and provide a way to easily reconstruct what
|
|
|
|
* any particular student has been doing.
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @uses $USER
|
|
|
|
* @uses $REMOTE_ADDR
|
|
|
|
* @uses SITEID
|
2004-09-25 05:29:21 +00:00
|
|
|
* @param int $courseid The course id
|
2004-09-24 21:28:22 +00:00
|
|
|
* @param string $module The module name - e.g. forum, journal, resource, course, user etc
|
2006-03-21 14:09:55 +00:00
|
|
|
* @param string $action 'view', 'update', 'add' or 'delete', possibly followed by another word to clarify.
|
2004-09-24 21:28:22 +00:00
|
|
|
* @param string $url The file and parameters used to see the results of the action
|
|
|
|
* @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
|
|
|
|
*/
|
2004-09-22 16:15:23 +00:00
|
|
|
function add_to_log($courseid, $module, $action, $url='', $info='', $cm=0, $user=0) {
|
2006-03-13 10:37:21 +00:00
|
|
|
// Note that this function intentionally does not follow the normal Moodle DB access idioms.
|
|
|
|
// This is for a good reason: it is the most frequently used DB update function,
|
|
|
|
// so it has been optimised for speed.
|
2008-05-15 21:40:00 +00:00
|
|
|
global $DB, $CFG, $USER;
|
2002-12-20 14:44:14 +00:00
|
|
|
|
2005-07-18 22:21:56 +00:00
|
|
|
if ($cm === '' || is_null($cm)) { // postgres won't translate empty string to its default
|
2005-03-23 07:07:47 +00:00
|
|
|
$cm = 0;
|
|
|
|
}
|
|
|
|
|
2004-01-30 18:21:56 +00:00
|
|
|
if ($user) {
|
|
|
|
$userid = $user;
|
|
|
|
} else {
|
2006-09-13 05:31:12 +00:00
|
|
|
if (!empty($USER->realuser)) { // Don't log
|
2004-01-30 18:21:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2004-09-22 16:15:23 +00:00
|
|
|
$userid = empty($USER->id) ? '0' : $USER->id;
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
2005-04-01 07:40:07 +00:00
|
|
|
$REMOTE_ADDR = getremoteaddr();
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
$timenow = time();
|
2008-05-30 17:00:25 +00:00
|
|
|
$info = $info;
|
2005-06-16 02:58:24 +00:00
|
|
|
if (!empty($url)) { // could break doing html_entity_decode on an empty var.
|
|
|
|
$url = html_entity_decode($url); // for php < 4.3.0 this is defined in moodlelib.php
|
|
|
|
}
|
2005-04-07 00:11:28 +00:00
|
|
|
|
2008-03-13 15:32:08 +00:00
|
|
|
// Restrict length of log lines to the space actually available in the
|
|
|
|
// database so that it doesn't cause a DB error. Log a warning so that
|
|
|
|
// developers can avoid doing things which are likely to cause this on a
|
|
|
|
// routine basis.
|
2008-05-30 17:00:25 +00:00
|
|
|
$tl = textlib_get_instance();
|
2008-03-13 15:32:08 +00:00
|
|
|
if(!empty($info) && $tl->strlen($info)>255) {
|
2008-05-30 17:00:25 +00:00
|
|
|
$info = $tl->substr($info,0,252).'...';
|
2008-03-13 15:32:08 +00:00
|
|
|
debugging('Warning: logged very long info',DEBUG_DEVELOPER);
|
|
|
|
}
|
2008-05-30 17:00:25 +00:00
|
|
|
|
2008-03-13 15:32:08 +00:00
|
|
|
// If the 100 field size is changed, also need to alter print_log in course/lib.php
|
|
|
|
if(!empty($url) && $tl->strlen($url)>100) {
|
|
|
|
$url=$tl->substr($url,0,97).'...';
|
|
|
|
debugging('Warning: logged very long URL',DEBUG_DEVELOPER);
|
|
|
|
}
|
2008-06-02 14:18:18 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
if (defined('MDL_PERFDB')) { global $PERF ; $PERF->logwrites++;};
|
2005-04-07 00:11:28 +00:00
|
|
|
|
2006-10-30 19:53:40 +00:00
|
|
|
if ($CFG->type = 'oci8po') {
|
2008-05-15 21:40:00 +00:00
|
|
|
if ($info == '') {
|
2006-10-30 19:53:40 +00:00
|
|
|
$info = ' ';
|
|
|
|
}
|
|
|
|
}
|
2008-05-15 21:40:00 +00:00
|
|
|
$log = array('time'=>$timenow, 'userid'=>$userid, 'course'=>$courseid, 'ip'=>$REMOTE_ADDR, 'module'=>$module,
|
|
|
|
'cmid'=>$cm, 'action'=>$action, 'url'=>$url, 'info'=>$info);
|
|
|
|
$result = $DB->insert_record_raw('log', $log, false);
|
2002-12-23 09:39:26 +00:00
|
|
|
|
2007-10-25 04:02:41 +00:00
|
|
|
// MDL-11893, alert $CFG->supportemail if insert into log failed
|
2008-05-15 21:40:00 +00:00
|
|
|
if (!$result and $CFG->supportemail and empty($CFG->noemailever)) {
|
|
|
|
// email_to_user is not usable because email_to_user tries to write to the logs table,
|
|
|
|
// and this will get caught in an infinite loop, if disk is full
|
2007-10-25 04:02:41 +00:00
|
|
|
$site = get_site();
|
|
|
|
$subject = 'Insert into log failed at your moodle site '.$site->fullname;
|
2008-04-15 20:02:59 +00:00
|
|
|
$message = "Insert into log table failed at ". date('l dS \of F Y h:i:s A') .".\n It is possible that your disk is full.\n\n";
|
2008-05-15 21:40:00 +00:00
|
|
|
$message .= "The failed query parameters are:\n\n" . var_export($log, true);
|
2008-04-15 20:02:59 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
$lasttime = get_config('admin', 'lastloginserterrormail');
|
|
|
|
if(empty($lasttime) || time() - $lasttime > 60*60*24) { // limit to 1 email per day
|
|
|
|
mail($CFG->supportemail, $subject, $message);
|
|
|
|
set_config('lastloginserterrormail', time(), 'admin');
|
2008-02-25 14:05:06 +00:00
|
|
|
}
|
2007-10-25 04:02:41 +00:00
|
|
|
}
|
|
|
|
|
2008-04-15 21:32:06 +00:00
|
|
|
if (!$result) {
|
|
|
|
debugging('Error: Could not insert a new entry to the Moodle log', DEBUG_ALL);
|
2004-09-21 11:41:58 +00:00
|
|
|
}
|
2006-09-13 05:31:12 +00:00
|
|
|
|
2008-04-15 21:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store user last access times - called when use enters a course or site
|
|
|
|
*
|
|
|
|
* @param int $courseid, empty means site
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function user_accesstime_log($courseid=0) {
|
2008-05-15 21:40:00 +00:00
|
|
|
global $USER, $CFG, $DB;
|
2008-04-15 21:46:04 +00:00
|
|
|
|
|
|
|
if (!isloggedin() or !empty($USER->realuser)) {
|
|
|
|
// no access tracking
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($courseid)) {
|
|
|
|
$courseid = SITEID;
|
|
|
|
}
|
|
|
|
|
|
|
|
$timenow = time();
|
|
|
|
|
|
|
|
/// Store site lastaccess time for the current user
|
|
|
|
if ($timenow - $USER->lastaccess > LASTACCESS_UPDATE_SECS) {
|
|
|
|
/// Update $USER->lastaccess for next checks
|
|
|
|
$USER->lastaccess = $timenow;
|
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
$last = new object();
|
|
|
|
$last->id = $USER->id;
|
|
|
|
$last->lastip = getremoteaddr();
|
|
|
|
$last->lastaccess = $timenow;
|
|
|
|
|
|
|
|
if (!$DB->update_record_raw('user', $last)) {
|
|
|
|
debugging('Error: Could not update global user lastaccess information', DEBUG_ALL); // Don't throw an error
|
2008-04-15 21:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($courseid == SITEID) {
|
|
|
|
/// no user_lastaccess for frontpage
|
|
|
|
return;
|
|
|
|
}
|
2008-04-08 23:17:20 +00:00
|
|
|
|
2008-04-15 21:46:04 +00:00
|
|
|
/// Store course lastaccess times for the current user
|
|
|
|
if (empty($USER->currentcourseaccess[$courseid]) or ($timenow - $USER->currentcourseaccess[$courseid] > LASTACCESS_UPDATE_SECS)) {
|
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
$lastaccess = $DB->get_field('user_lastaccess', 'timeaccess', array('userid'=>$USER->id, 'courseid'=>$courseid));
|
2008-04-15 21:46:04 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
if ($lastaccess === false) {
|
|
|
|
// Update course lastaccess for next checks
|
|
|
|
$USER->currentcourseaccess[$courseid] = $timenow;
|
|
|
|
|
|
|
|
$last = new object();
|
|
|
|
$last->userid = $USER->id;
|
|
|
|
$last->courseid = $courseid;
|
|
|
|
$last->timeaccess = $timenow;
|
|
|
|
if (!$DB->insert_record_raw('user_lastaccess', $last, false)) {
|
|
|
|
debugging('Error: Could not insert course user lastaccess information', DEBUG_ALL); // Don't throw an error
|
2008-04-10 21:00:38 +00:00
|
|
|
}
|
2008-05-30 16:51:01 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
} else if ($timenow - $lastaccess < LASTACCESS_UPDATE_SECS) {
|
|
|
|
// no need to update now, it was updated recently in concurrent login ;-)
|
2008-04-15 21:46:04 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
} else {
|
|
|
|
// Update course lastaccess for next checks
|
|
|
|
$USER->currentcourseaccess[$courseid] = $timenow;
|
|
|
|
|
|
|
|
if (!$DB->set_field('user_lastaccess', 'timeaccess', $timenow, array('userid'=>$USER->id, 'courseid'=>$courseid))) {
|
|
|
|
debugging('Error: Could not update course user lastacess information'); // Don't throw an error
|
2004-12-28 14:59:29 +00:00
|
|
|
}
|
2004-01-30 18:21:56 +00:00
|
|
|
}
|
2004-09-21 11:41:58 +00:00
|
|
|
}
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2004-09-24 21:28:22 +00:00
|
|
|
* Select all log records based on SQL criteria
|
|
|
|
*
|
|
|
|
* @param string $select SQL select criteria
|
2008-05-30 16:47:21 +00:00
|
|
|
* @param array $params named sql type params
|
2004-09-24 21:28:22 +00:00
|
|
|
* @param string $order SQL order by clause to sort the records returned
|
|
|
|
* @param string $limitfrom ?
|
|
|
|
* @param int $limitnum ?
|
|
|
|
* @param int $totalcount Passed in by reference.
|
2005-07-12 02:23:58 +00:00
|
|
|
* @return object
|
2004-09-24 21:28:22 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2008-05-30 16:47:21 +00:00
|
|
|
function get_logs($select, array $params=null, $order='l.time DESC', $limitfrom='', $limitnum='', &$totalcount) {
|
|
|
|
global $DB;
|
2002-12-20 14:44:14 +00:00
|
|
|
|
2003-08-22 06:07:18 +00:00
|
|
|
if ($order) {
|
2008-05-30 16:47:21 +00:00
|
|
|
$order = "ORDER BY $order";
|
|
|
|
}
|
|
|
|
|
|
|
|
$selectsql = "";
|
|
|
|
$countsql = "";
|
|
|
|
|
|
|
|
if ($select) {
|
|
|
|
$select = "WHERE $select";
|
2003-08-22 06:07:18 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
$sql = "SELECT COUNT(*)
|
|
|
|
FROM {log} l
|
|
|
|
$select";
|
|
|
|
|
|
|
|
$totalcount = $DB->count_records_sql($sql, $params);
|
2004-11-25 21:56:32 +00:00
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
$sql = "SELECT l.*, u.firstname, u.lastname, u.picture
|
2008-05-30 16:51:01 +00:00
|
|
|
FROM {log} l
|
2008-05-30 16:47:21 +00:00
|
|
|
LEFT JOIN {user} u ON l.userid = u.id
|
2008-05-30 16:51:01 +00:00
|
|
|
$select
|
2008-05-30 16:47:21 +00:00
|
|
|
$order";
|
2003-08-22 06:07:18 +00:00
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
return $DB->get_records_sql($sql, $params, $limitfrom, $limitnum) ;
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
2003-08-22 06:07:18 +00:00
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2004-09-24 21:28:22 +00:00
|
|
|
* Select all log records for a given course and user
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
2004-09-29 18:56:50 +00:00
|
|
|
* @uses DAYSECS
|
2004-09-24 21:28:22 +00:00
|
|
|
* @param int $userid The id of the user as found in the 'user' table.
|
|
|
|
* @param int $courseid The id of the course as found in the 'course' table.
|
|
|
|
* @param string $coursestart ?
|
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2002-12-20 14:44:14 +00:00
|
|
|
function get_logs_usercourse($userid, $courseid, $coursestart) {
|
2008-05-30 16:47:21 +00:00
|
|
|
global $DB;
|
2002-12-20 14:44:14 +00:00
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
$params = array();
|
|
|
|
|
|
|
|
$courseselect = '';
|
2003-07-25 13:23:28 +00:00
|
|
|
if ($courseid) {
|
2008-05-30 16:47:21 +00:00
|
|
|
$courseselect = "AND course = :courseid";
|
2008-05-30 16:51:01 +00:00
|
|
|
$params['courseid'] = $courseid;
|
2003-07-25 13:23:28 +00:00
|
|
|
}
|
2008-05-30 16:47:21 +00:00
|
|
|
$params['userid'] = $userid;
|
2008-05-30 16:51:01 +00:00
|
|
|
$params['coursestart'] = $coursestart;
|
2003-07-25 13:23:28 +00:00
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
return $DB->get_records_sql("SELECT FLOOR((time - :coursestart)/". DAYSECS .") AS day, COUNT(*) AS num
|
|
|
|
FROM {log}
|
|
|
|
WHERE userid = :userid
|
|
|
|
AND time > :coursestart $courseselect
|
|
|
|
GROUP BY FLOOR((time - :coursestart)/". DAYSECS .")", $params);
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2004-09-24 21:28:22 +00:00
|
|
|
* Select all log records for a given course, user, and day
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
2004-09-29 18:56:50 +00:00
|
|
|
* @uses HOURSECS
|
2004-09-24 21:28:22 +00:00
|
|
|
* @param int $userid The id of the user as found in the 'user' table.
|
|
|
|
* @param int $courseid The id of the course as found in the 'course' table.
|
|
|
|
* @param string $daystart ?
|
2005-07-12 02:23:58 +00:00
|
|
|
* @return object
|
2004-09-24 21:28:22 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2002-12-20 14:44:14 +00:00
|
|
|
function get_logs_userday($userid, $courseid, $daystart) {
|
2008-05-30 16:47:21 +00:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$params = array();
|
2002-12-20 14:44:14 +00:00
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
$courseselect = '';
|
2003-07-24 01:54:06 +00:00
|
|
|
if ($courseid) {
|
2008-05-30 16:47:21 +00:00
|
|
|
$courseselect = "AND course = :courseid";
|
2008-05-30 16:51:01 +00:00
|
|
|
$params['courseid'] = $courseid;
|
2003-07-24 01:54:06 +00:00
|
|
|
}
|
2008-05-30 16:47:21 +00:00
|
|
|
$params['userid'] = $userid;
|
2008-05-30 16:51:01 +00:00
|
|
|
$params['daystart'] = $daystart;
|
2003-07-24 01:54:06 +00:00
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
return $DB->get_records_sql("SELECT FLOOR((time - :daystart)/". HOURSECS .") AS hour, COUNT(*) AS num
|
|
|
|
FROM {log}
|
|
|
|
WHERE userid = :userid
|
|
|
|
AND time > :daystart $courseselect
|
|
|
|
GROUP BY FLOOR((time - :daystart)/". HOURSECS .") ");
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
2004-07-25 13:47:38 +00:00
|
|
|
/**
|
|
|
|
* Returns an object with counts of failed login attempts
|
|
|
|
*
|
2004-09-21 11:41:58 +00:00
|
|
|
* Returns information about failed login attempts. If the current user is
|
|
|
|
* an admin, then two numbers are returned: the number of attempts and the
|
2004-07-25 13:47:38 +00:00
|
|
|
* number of accounts. For non-admins, only the attempts on the given user
|
|
|
|
* are shown.
|
|
|
|
*
|
2004-09-24 21:28:22 +00:00
|
|
|
* @param string $mode Either 'admin', 'teacher' or 'everybody'
|
|
|
|
* @param string $username The username we are searching for
|
|
|
|
* @param string $lastlogin The date from which we are searching
|
|
|
|
* @return int
|
2004-07-25 13:47:38 +00:00
|
|
|
*/
|
|
|
|
function count_login_failures($mode, $username, $lastlogin) {
|
2008-05-30 16:47:21 +00:00
|
|
|
global $DB;
|
2004-07-25 13:47:38 +00:00
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
$params = array('mode'=>$mode, 'username'=>$username, 'lastlogin'=>$lastlogin);
|
|
|
|
$select = "module='login' AND action='error' AND time > :lastlogin";
|
|
|
|
|
|
|
|
$count = new object();
|
2004-07-25 13:47:38 +00:00
|
|
|
|
2008-05-01 06:07:24 +00:00
|
|
|
if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) { // Return information about all accounts
|
2008-05-30 16:47:21 +00:00
|
|
|
if ($count->attempts = $DB->count_records_select('log', $select, $params)) {
|
|
|
|
$count->accounts = $DB->count_records_select('log', $select, $params, 'COUNT(DISTINCT info)');
|
2004-07-25 13:47:38 +00:00
|
|
|
return $count;
|
|
|
|
}
|
2005-01-23 21:38:01 +00:00
|
|
|
} else if ($mode == 'everybody' or ($mode == 'teacher' and isteacherinanycourse())) {
|
2008-05-30 16:47:21 +00:00
|
|
|
if ($count->attempts = $DB->count_records_select('log', "$select AND info = :username", $params)) {
|
2004-07-25 13:47:38 +00:00
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-03 15:31:30 +00:00
|
|
|
/// GENERAL HELPFUL THINGS ///////////////////////////////////
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2004-09-24 21:28:22 +00:00
|
|
|
* Dump a given object's information in a PRE block.
|
|
|
|
*
|
|
|
|
* Mostly just used for debugging.
|
|
|
|
*
|
|
|
|
* @param mixed $object The data to be printed
|
|
|
|
*/
|
2003-01-03 15:31:30 +00:00
|
|
|
function print_object($object) {
|
2007-04-02 15:28:43 +00:00
|
|
|
echo '<pre class="notifytiny">' . htmlspecialchars(print_r($object,true)) . '</pre>';
|
2003-01-03 15:31:30 +00:00
|
|
|
}
|
|
|
|
|
2008-05-25 09:39:02 +00:00
|
|
|
/**
|
2007-09-19 07:19:05 +00:00
|
|
|
* Check whether a course is visible through its parents
|
2007-09-20 13:15:26 +00:00
|
|
|
* path.
|
2007-09-19 07:19:05 +00:00
|
|
|
*
|
|
|
|
* Notes:
|
|
|
|
*
|
|
|
|
* - All we need from the course is ->category. _However_
|
|
|
|
* if the course object has a categorypath property,
|
|
|
|
* we'll save a dbquery
|
|
|
|
*
|
|
|
|
* - If we return false, you'll still need to check if
|
|
|
|
* the user can has the 'moodle/category:visibility'
|
|
|
|
* capability...
|
|
|
|
*
|
2007-09-20 13:15:26 +00:00
|
|
|
* - Will generate 2 DB calls.
|
2007-09-19 07:19:05 +00:00
|
|
|
*
|
|
|
|
* - It does have a small local cache, however...
|
|
|
|
*
|
|
|
|
* - Do NOT call this over many courses as it'll generate
|
|
|
|
* DB traffic. Instead, see what get_my_courses() does.
|
|
|
|
*
|
|
|
|
* @param mixed $object A course object
|
|
|
|
* @return bool
|
|
|
|
*/
|
2005-12-13 19:31:48 +00:00
|
|
|
function course_parent_visible($course = null) {
|
2008-05-30 16:47:21 +00:00
|
|
|
global $CFG, $DB;
|
2007-09-19 07:19:05 +00:00
|
|
|
//return true;
|
|
|
|
static $mycache;
|
2006-07-19 10:21:52 +00:00
|
|
|
|
2007-09-19 07:19:05 +00:00
|
|
|
if (!is_object($course)) {
|
2006-07-13 14:18:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!empty($CFG->allowvisiblecoursesinhiddencategories)) {
|
|
|
|
return true;
|
|
|
|
}
|
2005-12-13 19:31:48 +00:00
|
|
|
|
2007-09-19 07:19:05 +00:00
|
|
|
if (!isset($mycache)) {
|
|
|
|
$mycache = array();
|
|
|
|
} else {
|
|
|
|
// cast to force assoc array
|
2007-09-20 13:15:26 +00:00
|
|
|
$k = (string)$course->category;
|
2007-09-19 07:19:05 +00:00
|
|
|
if (isset($mycache[$k])) {
|
|
|
|
return $mycache[$k];
|
|
|
|
}
|
2005-12-13 19:31:48 +00:00
|
|
|
}
|
2007-08-02 23:39:28 +00:00
|
|
|
|
2007-09-19 07:19:05 +00:00
|
|
|
if (isset($course->categorypath)) {
|
|
|
|
$path = $course->categorypath;
|
|
|
|
} else {
|
2008-05-30 16:47:21 +00:00
|
|
|
$path = $DB->get_field('course_categories', 'path', array('id'=>$course->category));
|
2006-01-17 23:46:42 +00:00
|
|
|
}
|
2007-09-19 07:19:05 +00:00
|
|
|
$catids = substr($path,1); // strip leading slash
|
|
|
|
$catids = str_replace('/',',',$catids);
|
2006-01-17 23:46:42 +00:00
|
|
|
|
2007-09-19 07:19:05 +00:00
|
|
|
$sql = "SELECT MIN(visible)
|
2008-05-30 16:47:21 +00:00
|
|
|
FROM {course_categories}
|
|
|
|
WHERE id IN ($catids)";
|
|
|
|
$vis = $DB->get_field_sql($sql);
|
2007-08-02 23:39:28 +00:00
|
|
|
|
2007-09-19 07:19:05 +00:00
|
|
|
// cast to force assoc array
|
|
|
|
$k = (string)$course->category;
|
|
|
|
$mycache[$k] = $vis;
|
|
|
|
|
|
|
|
return $vis;
|
2005-12-13 19:31:48 +00:00
|
|
|
}
|
|
|
|
|
2006-10-28 15:20:14 +00:00
|
|
|
/**
|
2007-08-02 23:39:28 +00:00
|
|
|
* This function is the official hook inside XMLDB stuff to delegate its debug to one
|
2006-10-28 15:20:14 +00:00
|
|
|
* external function.
|
|
|
|
*
|
|
|
|
* Any script can avoid calls to this function by defining XMLDB_SKIP_DEBUG_HOOK before
|
|
|
|
* using XMLDB classes. Obviously, also, if this function doesn't exist, it isn't invoked ;-)
|
|
|
|
*
|
|
|
|
* @param $message string contains the error message
|
|
|
|
* @param $object object XMLDB object that fired the debug
|
|
|
|
*/
|
|
|
|
function xmldb_debug($message, $object) {
|
|
|
|
|
2007-08-12 15:58:08 +00:00
|
|
|
debugging($message, DEBUG_DEVELOPER);
|
2006-10-28 15:20:14 +00:00
|
|
|
}
|
|
|
|
|
2007-04-03 09:19:09 +00:00
|
|
|
/**
|
|
|
|
* true or false function to see if user can create any courses at all
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function user_can_create_courses() {
|
|
|
|
global $USER;
|
|
|
|
// if user has course creation capability at any site or course cat, then return true;
|
2007-08-02 23:39:28 +00:00
|
|
|
|
2008-05-01 06:07:24 +00:00
|
|
|
if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
|
2007-08-02 23:39:28 +00:00
|
|
|
return true;
|
2007-04-03 09:19:09 +00:00
|
|
|
} else {
|
2007-08-02 23:39:28 +00:00
|
|
|
return (bool) count(get_creatable_categories());
|
2007-04-03 09:19:09 +00:00
|
|
|
}
|
2007-08-02 23:39:28 +00:00
|
|
|
|
2007-04-03 09:19:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-25 09:39:02 +00:00
|
|
|
* Get the list of categories the current user can create courses in
|
2007-04-03 09:19:09 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function get_creatable_categories() {
|
2008-05-30 16:47:21 +00:00
|
|
|
global $DB;
|
2007-08-02 23:39:28 +00:00
|
|
|
|
2007-04-03 09:19:09 +00:00
|
|
|
$creatablecats = array();
|
2008-05-30 16:47:21 +00:00
|
|
|
if ($cats = $DB->get_records('course_categories')) {
|
2007-04-03 09:19:09 +00:00
|
|
|
foreach ($cats as $cat) {
|
|
|
|
if (has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $cat->id))) {
|
|
|
|
$creatablecats[$cat->id] = $cat->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $creatablecats;
|
|
|
|
}
|
|
|
|
|
2006-10-01 06:39:20 +00:00
|
|
|
?>
|