2005-03-22 18:10:06 +00:00
< ? php // $Id$
2004-09-23 02:48:41 +00:00
/**
* Library of functions for database manipulation .
*
* 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-23 02:48:41 +00:00
* @ version $Id $
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
*/
2002-12-17 04:41:18 +00:00
2004-09-09 06:59:48 +00:00
/// GLOBAL CONSTANTS /////////////////////////////////////////////////////////
2004-09-23 02:48:41 +00:00
2004-09-09 06:59:48 +00:00
if ( $SITE = get_site ()) {
2004-09-23 02:48:41 +00:00
/**
2004-09-24 21:28:22 +00:00
* If $SITE global from { @ link get_site ()} is set then SITEID to $SITE -> id , otherwise set to 1.
2004-09-23 02:48:41 +00:00
*/
2004-09-09 06:59:48 +00:00
define ( 'SITEID' , $SITE -> id );
} else {
2004-09-23 02:48:41 +00:00
/**
* @ ignore
*/
2004-09-09 06:59:48 +00:00
define ( 'SITEID' , 1 );
}
2004-08-29 14:15:40 +00:00
2002-12-17 04:41:18 +00:00
/// FUNCTIONS FOR DATABASE HANDLING ////////////////////////////////
2004-08-29 14:15:40 +00:00
2003-09-14 04:04:15 +00:00
/**
2004-09-23 02:48:41 +00:00
* Execute a given sql command string
*
* Completely general function - it just runs some SQL and reports success .
*
2004-09-24 21:28:22 +00:00
* @ uses $db
* @ param string $command The sql string you wish to be executed .
2005-07-12 02:23:58 +00:00
* @ param bool $feedback Set this argument to true if the results generated should be printed . Default is true .
2004-09-23 02:48:41 +00:00
* @ return string
*/
2002-12-17 04:41:18 +00:00
function execute_sql ( $command , $feedback = true ) {
/// Completely general function - it just runs some SQL and reports success.
2005-11-17 02:27:05 +00:00
global $db , $CFG ;
2004-09-21 11:41:58 +00:00
2004-11-18 03:36:52 +00:00
$olddebug = $db -> debug ;
if ( ! $feedback ) {
$db -> debug = false ;
}
2005-04-07 00:11:28 +00:00
if ( defined ( 'MDL_PERFDB' )) { global $PERF ; $PERF -> dbqueries ++ ; };
2004-11-18 03:36:52 +00:00
2004-09-22 16:15:23 +00:00
$result = $db -> Execute ( $command );
2002-12-17 04:41:18 +00:00
2004-11-18 03:36:52 +00:00
$db -> debug = $olddebug ;
2002-12-17 04:41:18 +00:00
if ( $result ) {
if ( $feedback ) {
2005-02-08 17:09:41 +00:00
notify ( get_string ( 'success' ), 'notifysuccess' );
2002-12-17 04:41:18 +00:00
}
return true ;
} else {
if ( $feedback ) {
2004-09-24 21:28:22 +00:00
echo '<p><font color="red"><strong>' . get_string ( 'error' ) . '</strong></font></p>' ;
2002-12-17 04:41:18 +00:00
}
2005-05-11 23:34:46 +00:00
if ( ! empty ( $CFG -> dblogerror )) {
2005-05-19 21:59:00 +00:00
$debug = array_shift ( debug_backtrace ());
2005-05-11 23:34:46 +00:00
error_log ( " SQL " . $db -> ErrorMsg () . " in { $debug [ 'file' ] } on line { $debug [ 'line' ] } . STATEMENT: $command " );
}
2002-12-17 04:41:18 +00:00
return false ;
}
}
2004-11-17 05:31:21 +00:00
/**
* on DBs that support it , switch to transaction mode and begin a transaction
* you ' ll need to ensure you call commit_sql () or your changes * will * be lost
* this is _very_ useful for massive updates
*/
function begin_sql () {
/// Completely general function - it just runs some SQL and reports success.
global $CFG ;
if ( $CFG -> dbtype === 'postgres7' ) {
return execute_sql ( 'BEGIN' , false );
}
return true ;
}
2005-08-26 04:02:06 +00:00
/**
* on DBs that support it , commit the transaction
*/
function rollback_sql () {
/// Completely general function - it just runs some SQL and reports success.
global $CFG ;
if ( $CFG -> dbtype === 'postgres7' ) {
return execute_sql ( 'ROLLBACK' , false );
}
return true ;
}
2004-11-23 23:05:06 +00:00
/**
* returns db specific uppercase function
*/
function db_uppercase () {
global $CFG ;
switch ( strtolower ( $CFG -> dbtype )) {
case " postgres7 " :
return " upper " ;
2006-01-16 06:04:09 +00:00
2004-11-23 23:05:06 +00:00
case " mysql " :
default :
return " ucase " ;
2006-01-16 06:04:09 +00:00
2004-11-23 23:05:06 +00:00
}
}
/**
* returns db specific lowercase function
*/
function db_lowercase () {
global $CFG ;
switch ( strtolower ( $CFG -> dbtype )) {
case " postgres7 " :
return " lower " ;
2006-01-16 06:04:09 +00:00
2004-11-23 23:05:06 +00:00
case " mysql " :
default :
return " lcase " ;
2006-01-16 06:04:09 +00:00
2004-11-23 23:05:06 +00:00
}
}
2004-11-17 05:31:21 +00:00
/**
* on DBs that support it , commit the transaction
*/
function commit_sql () {
/// Completely general function - it just runs some SQL and reports success.
global $CFG ;
if ( $CFG -> dbtype === 'postgres7' ) {
return execute_sql ( 'COMMIT' , false );
}
return true ;
}
2002-12-17 04:41:18 +00:00
2004-09-23 02:48:41 +00:00
/**
* Run an arbitrary sequence of semicolon - delimited SQL commands
*
* Assumes that the input text ( file or string ) consists of
* a number of SQL statements ENDING WITH SEMICOLONS . The
* semicolons MUST be the last character in a line .
2005-02-01 20:45:46 +00:00
* Lines that are blank or that start with " # " or " -- " ( postgres ) are ignored .
2004-09-23 02:48:41 +00:00
* Only tested with mysql dump files ( mysqldump - p - d moodle )
*
2004-09-24 21:28:22 +00:00
* @ uses $CFG
2004-09-23 02:48:41 +00:00
* @ param string $sqlfile The path where a file with sql commands can be found on the server .
* @ param string $sqlstring If no path is supplied then a string with semicolon delimited sql
* commands can be supplied in this argument .
2005-07-12 02:23:58 +00:00
* @ return bool Returns true if databse was modified successfully .
2004-09-23 02:48:41 +00:00
*/
2004-09-22 16:15:23 +00:00
function modify_database ( $sqlfile = '' , $sqlstring = '' ) {
2002-12-17 04:41:18 +00:00
global $CFG ;
2004-09-21 11:41:58 +00:00
$success = true ; // Let's be optimistic
2003-02-24 09:36:15 +00:00
if ( ! empty ( $sqlfile )) {
if ( ! is_readable ( $sqlfile )) {
$success = false ;
2004-09-22 16:15:23 +00:00
echo '<p>Tried to modify database, but "' . $sqlfile . '" doesn\'t exist!</p>' ;
2003-02-24 09:36:15 +00:00
return $success ;
} else {
$lines = file ( $sqlfile );
}
} else {
2004-11-22 04:11:07 +00:00
$sqlstring = trim ( $sqlstring );
if ( $sqlstring { strlen ( $sqlstring ) - 1 } != " ; " ) {
$sqlstring .= " ; " ; // add it in if it's not there.
}
2003-02-24 09:36:15 +00:00
$lines [] = $sqlstring ;
}
2004-09-22 16:15:23 +00:00
$command = '' ;
2003-02-24 09:36:15 +00:00
foreach ( $lines as $line ) {
$line = rtrim ( $line );
$length = strlen ( $line );
2005-02-01 20:45:46 +00:00
if ( $length and $line [ 0 ] <> '#' and $line [ 0 ] . $line [ 1 ] <> '--' ) {
2004-09-22 16:15:23 +00:00
if ( substr ( $line , $length - 1 , 1 ) == ';' ) {
2003-02-24 09:36:15 +00:00
$line = substr ( $line , 0 , $length - 1 ); // strip ;
$command .= $line ;
2004-09-22 16:15:23 +00:00
$command = str_replace ( 'prefix_' , $CFG -> prefix , $command ); // Table prefixes
2003-02-24 09:36:15 +00:00
if ( ! execute_sql ( $command )) {
$success = false ;
2002-12-17 04:41:18 +00:00
}
2004-09-22 16:15:23 +00:00
$command = '' ;
2003-02-24 09:36:15 +00:00
} else {
$command .= $line ;
2002-12-17 04:41:18 +00:00
}
}
}
return $success ;
2003-02-24 09:36:15 +00:00
2002-12-17 04:41:18 +00:00
}
2003-01-03 15:31:30 +00:00
/// FUNCTIONS TO MODIFY TABLES ////////////////////////////////////////////
2003-09-14 04:04:15 +00:00
/**
2004-09-24 21:28:22 +00:00
* Add a new field to a table , or modify an existing one ( if oldfield is defined ) .
*
* @ uses $CFG
* @ uses $db
2004-09-25 05:29:21 +00:00
* @ param string $table ?
* @ param string $oldfield ?
* @ param string $field ?
2005-01-31 23:24:43 +00:00
* @ param string $type ?
2004-09-25 05:29:21 +00:00
* @ param string $size ?
* @ param string $signed ?
* @ param string $default ?
* @ param string $null ?
2004-09-24 21:28:22 +00:00
* @ todo Finish documenting this function
*/
2003-09-14 04:04:15 +00:00
2004-09-22 16:15:23 +00:00
function table_column ( $table , $oldfield , $field , $type = 'integer' , $size = '10' ,
$signed = 'unsigned' , $default = '0' , $null = 'not null' , $after = '' ) {
2003-05-04 16:03:45 +00:00
global $CFG , $db ;
2003-01-03 15:31:30 +00:00
switch ( strtolower ( $CFG -> dbtype )) {
2004-09-22 16:15:23 +00:00
case 'mysql' :
case 'mysqlt' :
2003-01-03 15:31:30 +00:00
switch ( strtolower ( $type )) {
2004-09-22 16:15:23 +00:00
case 'text' :
$type = 'TEXT' ;
$signed = '' ;
2003-07-30 13:02:45 +00:00
break ;
2004-09-22 16:15:23 +00:00
case 'integer' :
$type = 'INTEGER(' . $size . ')' ;
2003-01-03 15:31:30 +00:00
break ;
2004-09-22 16:15:23 +00:00
case 'varchar' :
$type = 'VARCHAR(' . $size . ')' ;
$signed = '' ;
2003-01-03 15:31:30 +00:00
break ;
2005-07-20 17:47:05 +00:00
case 'char' :
$type = 'CHAR(' . $size . ')' ;
$signed = '' ;
break ;
2003-01-03 15:31:30 +00:00
}
if ( ! empty ( $oldfield )) {
2004-09-22 16:15:23 +00:00
$operation = 'CHANGE ' . $oldfield . ' ' . $field ;
2003-01-03 15:31:30 +00:00
} else {
2004-09-22 16:15:23 +00:00
$operation = 'ADD ' . $field ;
2003-01-03 15:31:30 +00:00
}
2004-09-22 16:15:23 +00:00
$default = 'DEFAULT \'' . $default . '\'' ;
2003-01-03 15:31:30 +00:00
if ( ! empty ( $after )) {
2004-09-22 16:15:23 +00:00
$after = 'AFTER `' . $after . '`' ;
2003-01-03 15:31:30 +00:00
}
2004-09-22 16:15:23 +00:00
return execute_sql ( 'ALTER TABLE ' . $CFG -> prefix . $table . ' ' . $operation . ' ' . $type . ' ' . $signed . ' ' . $default . ' ' . $null . ' ' . $after );
2003-01-03 15:31:30 +00:00
2004-09-22 16:15:23 +00:00
case 'postgres7' : // From Petri Asikainen
2003-05-04 16:03:45 +00:00
//Check db-version
$dbinfo = $db -> ServerInfo ();
2003-09-03 08:02:01 +00:00
$dbver = substr ( $dbinfo [ 'version' ], 0 , 3 );
2004-09-21 11:41:58 +00:00
2003-02-18 07:44:26 +00:00
//to prevent conflicts with reserved words
2004-09-22 16:15:23 +00:00
$realfield = '"' . $field . '"' ;
$field = '"' . $field . '_alter_column_tmp"' ;
$oldfield = '"' . $oldfield . '"' ;
2003-02-18 07:44:26 +00:00
switch ( strtolower ( $type )) {
2005-04-27 19:24:48 +00:00
case 'tinyint' :
2004-09-22 16:15:23 +00:00
case 'integer' :
2004-08-03 09:30:04 +00:00
if ( $size <= 4 ) {
2004-09-22 16:15:23 +00:00
$type = 'INT2' ;
2004-09-21 11:41:58 +00:00
}
2004-08-03 09:30:04 +00:00
if ( $size <= 10 ) {
2004-09-22 16:15:23 +00:00
$type = 'INT' ;
2003-02-18 07:44:26 +00:00
}
2004-08-03 09:30:04 +00:00
if ( $size > 10 ) {
2004-09-22 16:15:23 +00:00
$type = 'INT8' ;
2003-02-18 07:44:26 +00:00
}
break ;
2004-09-22 16:15:23 +00:00
case 'varchar' :
$type = 'VARCHAR(' . $size . ')' ;
2003-02-18 07:44:26 +00:00
break ;
2005-07-20 17:47:05 +00:00
case 'char' :
$type = 'CHAR(' . $size . ')' ;
$signed = '' ;
break ;
2003-02-18 07:44:26 +00:00
}
2004-09-22 16:15:23 +00:00
$default = '\'' . $default . '\'' ;
2003-02-18 07:44:26 +00:00
//After is not implemented in postgesql
//if (!empty($after)) {
// $after = "AFTER '$after'";
//}
2004-08-09 16:00:10 +00:00
//Use transactions
2004-09-22 16:15:23 +00:00
execute_sql ( 'BEGIN' );
2004-08-09 16:00:10 +00:00
2005-01-31 23:24:43 +00:00
//Always use temporary column
2004-09-22 16:15:23 +00:00
execute_sql ( 'ALTER TABLE ' . $CFG -> prefix . $table . ' ADD COLUMN ' . $field . ' ' . $type );
2004-08-09 16:00:10 +00:00
//Add default values
2004-09-22 16:15:23 +00:00
execute_sql ( 'UPDATE ' . $CFG -> prefix . $table . ' SET ' . $field . '=' . $default );
2004-09-21 11:41:58 +00:00
2003-02-18 07:44:26 +00:00
2004-09-22 16:15:23 +00:00
if ( $dbver >= '7.3' ) {
2003-05-04 16:03:45 +00:00
// modifying 'not null' is posible before 7.3
//update default values to table
2005-08-11 23:52:11 +00:00
if ( strtoupper ( $null ) == 'NOT NULL' ) {
2004-09-22 16:15:23 +00:00
execute_sql ( 'UPDATE ' . $CFG -> prefix . $table . ' SET ' . $field . '=' . $default . ' WHERE ' . $field . ' IS NULL' );
execute_sql ( 'ALTER TABLE ' . $CFG -> prefix . $table . ' ALTER COLUMN ' . $field . ' SET ' . $null );
2003-05-04 16:03:45 +00:00
} else {
2004-09-22 16:15:23 +00:00
execute_sql ( 'ALTER TABLE ' . $CFG -> prefix . $table . ' ALTER COLUMN ' . $field . ' DROP NOT NULL' );
2003-05-04 16:03:45 +00:00
}
2003-02-18 07:44:26 +00:00
}
2003-05-04 16:03:45 +00:00
2004-09-22 16:15:23 +00:00
execute_sql ( 'ALTER TABLE ' . $CFG -> prefix . $table . ' ALTER COLUMN ' . $field . ' SET DEFAULT ' . $default );
2004-09-21 11:41:58 +00:00
2004-09-22 16:15:23 +00:00
if ( $oldfield != '""' ) {
2005-01-31 23:24:43 +00:00
// We are changing the type of a column. This may require doing some casts...
$casting = '' ;
$oldtype = column_type ( $table , $oldfield );
$newtype = column_type ( $table , $field );
// Do we need a cast?
if ( $newtype == 'N' && $oldtype == 'C' ) {
$casting = 'CAST(CAST(' . $oldfield . ' AS TEXT) AS REAL)' ;
}
2005-05-19 05:08:03 +00:00
else if ( $newtype == 'I' && $oldtype == 'C' ) {
2005-01-31 23:24:43 +00:00
$casting = 'CAST(CAST(' . $oldfield . ' AS TEXT) AS INTEGER)' ;
}
else {
$casting = $oldfield ;
}
// Run the update query, casting as necessary
execute_sql ( 'UPDATE ' . $CFG -> prefix . $table . ' SET ' . $field . ' = ' . $casting );
2004-09-22 16:15:23 +00:00
execute_sql ( 'ALTER TABLE ' . $CFG -> prefix . $table . ' DROP COLUMN ' . $oldfield );
2004-08-09 16:00:10 +00:00
}
2003-02-18 07:44:26 +00:00
2004-09-23 02:48:41 +00:00
execute_sql ( 'ALTER TABLE ' . $CFG -> prefix . $table . ' RENAME COLUMN ' . $field . ' TO ' . $realfield );
2004-09-21 11:41:58 +00:00
2004-09-22 16:15:23 +00:00
return execute_sql ( 'COMMIT' );
2003-01-03 15:31:30 +00:00
default :
switch ( strtolower ( $type )) {
2004-09-22 16:15:23 +00:00
case 'integer' :
$type = 'INTEGER' ;
2003-01-03 15:31:30 +00:00
break ;
2004-09-22 16:15:23 +00:00
case 'varchar' :
$type = 'VARCHAR' ;
2003-01-24 07:51:55 +00:00
break ;
2003-01-03 15:31:30 +00:00
}
2004-09-22 16:15:23 +00:00
$default = 'DEFAULT \'' . $default . '\'' ;
2003-01-03 15:31:30 +00:00
if ( ! empty ( $after )) {
2004-09-22 16:15:23 +00:00
$after = 'AFTER ' . $after ;
2003-01-03 15:31:30 +00:00
}
if ( ! empty ( $oldfield )) {
2004-09-22 16:15:23 +00:00
execute_sql ( 'ALTER TABLE ' . $CFG -> prefix . $table . ' RENAME COLUMN ' . $oldfield . ' ' . $field );
2003-01-03 15:31:30 +00:00
} else {
2004-09-22 16:15:23 +00:00
execute_sql ( 'ALTER TABLE ' . $CFG -> prefix . $table . ' ADD COLUMN ' . $field . ' ' . $type );
2003-01-03 15:31:30 +00:00
}
2004-09-22 16:15:23 +00:00
execute_sql ( 'ALTER TABLE ' . $CFG -> prefix . $table . ' ALTER COLUMN ' . $field . ' SET ' . $null );
return execute_sql ( 'ALTER TABLE ' . $CFG -> prefix . $table . ' ALTER COLUMN ' . $field . ' SET ' . $default );
2003-01-03 15:31:30 +00:00
}
}
2005-01-31 23:24:43 +00:00
/**
* Get the data type of a table column , using an ADOdb MetaType () call .
*
* @ uses $CFG
* @ uses $db
2005-04-11 10:18:33 +00:00
* @ param string $table The name of the database table
* @ param string $column The name of the field in the table
* @ return string Field type or false if error
2005-01-31 23:24:43 +00:00
*/
function column_type ( $table , $column ) {
global $CFG , $db ;
2005-04-07 00:11:28 +00:00
if ( defined ( 'MDL_PERFDB' )) { global $PERF ; $PERF -> dbqueries ++ ; };
2005-02-01 00:28:02 +00:00
if ( ! $rs = $db -> Execute ( 'SELECT ' . $column . ' FROM ' . $CFG -> prefix . $table . ' LIMIT 0' )) {
2005-01-31 23:24:43 +00:00
return false ;
}
$field = $rs -> FetchField ( 0 );
return $rs -> MetaType ( $field -> type );
}
2003-01-03 15:31:30 +00:00
/// GENERIC FUNCTIONS TO CHECK AND COUNT RECORDS ////////////////////////////////////////
2002-12-17 04:41:18 +00:00
2003-09-14 04:04:15 +00:00
/**
2006-01-16 06:07:57 +00:00
* Test whether a record exists in a table where all the given fields match the given values .
*
* The record to test is specified by giving up to three fields that must
* equal the corresponding values .
2004-09-24 21:28:22 +00:00
*
* @ uses $CFG
2006-01-16 06:07:57 +00:00
* @ param string $table The table to check .
* @ param string $field1 the first field to check ( optional ) .
* @ param string $value1 the value field1 must have ( requred if field1 is given , else optional ) .
* @ param string $field2 the second field to check ( optional ) .
* @ param string $value2 the value field2 must have ( requred if field2 is given , else optional ) .
* @ param string $field3 the third field to check ( optional ) .
* @ param string $value3 the value field3 must have ( requred if field3 is given , else optional ) .
* @ return bool true if a matching record exists , else false .
2004-09-24 21:28:22 +00:00
*/
2004-09-22 16:15:23 +00:00
function record_exists ( $table , $field1 = '' , $value1 = '' , $field2 = '' , $value2 = '' , $field3 = '' , $value3 = '' ) {
2002-12-17 04:41:18 +00:00
global $CFG ;
2006-01-16 06:07:57 +00:00
$select = where_clause ( $field1 , $value1 , $field2 , $value2 , $field3 , $value3 );
2002-12-17 04:41:18 +00:00
2004-09-22 16:15:23 +00:00
return record_exists_sql ( 'SELECT * FROM ' . $CFG -> prefix . $table . ' ' . $select . ' LIMIT 1' );
2002-12-17 04:41:18 +00:00
}
2003-09-14 04:04:15 +00:00
/**
2006-01-16 06:24:53 +00:00
* Test whether a SQL SELECT statement returns any records .
*
* This function returns true if the SQL statement executes
* without any errors and returns at least one record .
*
* @ param string $sql The SQL statement to execute .
* @ return bool true if the SQL executes without errors and returns at least one record .
*/
2002-12-17 04:41:18 +00:00
function record_exists_sql ( $sql ) {
2006-01-16 06:24:53 +00:00
$rs = get_recordset_sql ( $sql );
2002-12-17 04:41:18 +00:00
2006-01-16 06:24:53 +00:00
if ( $rs && $rs -> RecordCount () > 0 ) {
2002-12-17 04:41:18 +00:00
return true ;
} else {
return false ;
}
2006-01-16 06:24:53 +00:00
}
2002-12-17 04:41:18 +00:00
2003-09-14 04:04:15 +00:00
/**
2006-01-16 06:07:57 +00:00
* Count the records in a table where all the given fields match the given values .
2004-09-24 21:28:22 +00:00
*
* @ uses $CFG
2006-01-16 06:07:57 +00:00
* @ param string $table The table to query .
* @ param string $field1 the first field to check ( optional ) .
* @ param string $value1 the value field1 must have ( requred if field1 is given , else optional ) .
* @ param string $field2 the second field to check ( optional ) .
* @ param string $value2 the value field2 must have ( requred if field2 is given , else optional ) .
* @ param string $field3 the third field to check ( optional ) .
* @ param string $value3 the value field3 must have ( requred if field3 is given , else optional ) .
2004-09-24 21:28:22 +00:00
* @ return int The count of records returned from the specified criteria .
*/
2004-09-22 16:15:23 +00:00
function count_records ( $table , $field1 = '' , $value1 = '' , $field2 = '' , $value2 = '' , $field3 = '' , $value3 = '' ) {
2002-12-17 04:41:18 +00:00
global $CFG ;
2006-01-16 06:07:57 +00:00
$select = where_clause ( $field1 , $value1 , $field2 , $value2 , $field3 , $value3 );
2002-12-17 04:41:18 +00:00
2004-09-22 16:15:23 +00:00
return count_records_sql ( 'SELECT COUNT(*) FROM ' . $CFG -> prefix . $table . ' ' . $select );
2002-12-17 04:41:18 +00:00
}
2003-09-14 04:04:15 +00:00
/**
2006-01-16 06:24:53 +00:00
* Count the records in a table which match a particular WHERE clause .
2004-09-24 21:28:22 +00:00
*
* @ uses $CFG
* @ param string $table The database table to be checked against .
2006-01-16 06:24:53 +00:00
* @ param string $select A fragment of SQL to be used in a WHERE clause in the SQL call .
2004-09-24 21:28:22 +00:00
* @ param string $countitem The count string to be used in the SQL call . Default is COUNT ( * ) .
* @ return int The count of records returned from the specified criteria .
2004-09-26 02:10:38 +00:00
*/
2004-09-22 16:15:23 +00:00
function count_records_select ( $table , $select = '' , $countitem = 'COUNT(*)' ) {
2002-12-20 14:44:14 +00:00
global $CFG ;
2002-12-22 06:15:44 +00:00
if ( $select ) {
2004-09-22 16:15:23 +00:00
$select = 'WHERE ' . $select ;
2002-12-22 06:15:44 +00:00
}
2004-09-22 16:15:23 +00:00
return count_records_sql ( 'SELECT ' . $countitem . ' FROM ' . $CFG -> prefix . $table . ' ' . $select );
2002-12-20 14:44:14 +00:00
}
2003-09-14 04:04:15 +00:00
/**
2006-01-16 06:24:53 +00:00
* Get the result of a SQL SELECT COUNT ( ... ) query .
*
* Given a query that counts rows , return that count . ( In fact ,
* given any query , return the first field of the first record
* returned . However , this method should only be used for the
* intended purpose . ) If an error occurrs , 0 is returned .
2004-09-24 21:28:22 +00:00
*
* @ uses $CFG
* @ uses $db
* @ param string $sql The SQL string you wish to be executed .
2006-01-16 06:24:53 +00:00
* @ return int the count . If an error occurrs , 0 is returned .
2004-09-24 21:28:22 +00:00
*/
2002-12-17 04:41:18 +00:00
function count_records_sql ( $sql ) {
2006-01-16 06:24:53 +00:00
$rs = get_recordset_sql ( $sql );
if ( $rs ) {
return $rs -> fields [ 0 ];
} else {
return 0 ;
2004-03-09 16:32:24 +00:00
}
2002-12-17 04:41:18 +00:00
}
2003-01-03 15:31:30 +00:00
/// GENERIC FUNCTIONS TO GET, INSERT, OR UPDATE DATA ///////////////////////////////////
2003-09-14 04:04:15 +00:00
/**
2004-09-24 21:28:22 +00:00
* Get a single record as an object
*
* @ uses $CFG
2006-01-16 06:07:57 +00:00
* @ param string $table The table to select from .
* @ param string $field1 the first field to check ( optional ) .
* @ param string $value1 the value field1 must have ( requred if field1 is given , else optional ) .
* @ param string $field2 the second field to check ( optional ) .
* @ param string $value2 the value field2 must have ( requred if field2 is given , else optional ) .
* @ param string $field3 the third field to check ( optional ) .
* @ param string $value3 the value field3 must have ( requred if field3 is given , else optional ) .
* @ return mixed a fieldset object containing the first mathcing record , or false if none found .
2004-09-24 21:28:22 +00:00
*/
2005-03-10 14:13:26 +00:00
function get_record ( $table , $field1 , $value1 , $field2 = '' , $value2 = '' , $field3 = '' , $value3 = '' , $fields = '*' ) {
2004-09-21 11:41:58 +00:00
2006-01-16 06:07:57 +00:00
global $CFG ;
2002-12-17 04:41:18 +00:00
2006-01-16 06:07:57 +00:00
$select = where_clause ( $field1 , $value1 , $field2 , $value2 , $field3 , $value3 );
2002-12-17 04:41:18 +00:00
2005-03-10 14:13:26 +00:00
return get_record_sql ( 'SELECT ' . $fields . ' FROM ' . $CFG -> prefix . $table . ' ' . $select );
2002-12-17 04:41:18 +00:00
}
2003-09-14 04:04:15 +00:00
/**
2004-09-24 21:28:22 +00:00
* Get a single record as an object using the specified SQL statement
*
* A LIMIT is normally added to only look for 1 record
2005-04-11 10:18:33 +00:00
* If debugging is OFF only the first record is returned even if there is
* more than one matching record !
2004-09-24 21:28:22 +00:00
*
* @ uses $CFG
* @ uses $db
* @ param string $sql The SQL string you wish to be executed .
2005-04-11 10:18:33 +00:00
* @ return Found record as object . False if not found or error
2004-09-24 21:28:22 +00:00
*/
2005-11-19 15:39:31 +00:00
function get_record_sql ( $sql , $expectmultiple = false , $nolimit = false ) {
2002-12-17 04:41:18 +00:00
2006-01-16 06:24:53 +00:00
global $CFG ;
2002-12-17 04:41:18 +00:00
2005-04-11 12:07:49 +00:00
if ( isset ( $CFG -> debug ) && $CFG -> debug > 7 && ! $expectmultiple ) { // Debugging mode - don't use limit
2004-09-22 16:15:23 +00:00
$limit = '' ;
2005-11-19 15:39:31 +00:00
} else if ( $nolimit ) {
$limit = '' ;
2003-07-23 04:33:40 +00:00
} else {
2004-09-22 16:15:23 +00:00
$limit = ' LIMIT 1' ; // Workaround - limit to one record
2003-07-23 04:33:40 +00:00
}
2006-01-16 06:24:53 +00:00
if ( ! $rs = get_recordset_sql ( $sql . $limit )) {
return false ;
2003-07-23 06:30:26 +00:00
}
2006-01-16 06:24:53 +00:00
$recordcount = $rs -> RecordCount ();
2003-07-23 04:33:40 +00:00
2006-01-16 06:24:53 +00:00
if ( $recordcount == 0 ) { // Found no records
return false ;
2002-12-17 04:41:18 +00:00
2006-01-16 06:24:53 +00:00
} else if ( $recordcount == 1 ) { // Found one record
2002-12-17 04:41:18 +00:00
return ( object ) $rs -> fields ;
2003-07-23 04:33:40 +00:00
2003-07-23 06:30:26 +00:00
} else { // Error: found more than one record
2004-09-22 16:15:23 +00:00
notify ( 'Error: Turn off debugging to hide this error.' );
notify ( $sql . $limit );
2003-07-23 04:33:40 +00:00
if ( $records = $rs -> GetAssoc ( true )) {
2004-09-22 16:15:23 +00:00
notify ( 'Found more than one record in get_record_sql !' );
2003-07-23 04:33:40 +00:00
print_object ( $records );
} else {
2004-09-22 16:15:23 +00:00
notify ( 'Very strange error in get_record_sql !' );
2003-07-23 06:30:26 +00:00
print_object ( $rs );
2003-07-23 04:33:40 +00:00
}
2004-12-31 15:41:23 +00:00
print_continue ( " $CFG->wwwroot / $CFG->admin /config.php " );
2002-12-17 04:41:18 +00:00
}
}
2003-09-14 04:04:15 +00:00
/**
2004-09-24 21:28:22 +00:00
* Gets one record from a table , as an object
*
* @ uses $CFG
* @ param string $table The database table to be checked against .
* @ param string $select A fragment of SQL to be used in a where clause in the SQL call .
* @ 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 Returns an array of found records ( as objects ) or false if no records or error occured .
2004-09-26 02:10:38 +00:00
*/
2004-09-22 16:15:23 +00:00
function get_record_select ( $table , $select = '' , $fields = '*' ) {
2002-12-29 16:28:09 +00:00
global $CFG ;
if ( $select ) {
2004-09-22 16:15:23 +00:00
$select = 'WHERE ' . $select ;
2002-12-29 16:28:09 +00:00
}
2004-09-22 16:15:23 +00:00
return get_record_sql ( 'SELECT ' . $fields . ' FROM ' . $CFG -> prefix . $table . ' ' . $select );
2002-12-29 16:28:09 +00:00
}
2006-01-16 06:05:43 +00:00
/**
* Get a number of records as an ADODB RecordSet .
*
* Selects records from the table $table .
*
* If specified , only records where the field $field has value $value are retured .
*
* If specified , the results will be sorted as specified by $sort . This
* is added to the SQL as " ORDER BY $sort " . Example values of $sort
* mightbe " time ASC " or " time DESC " .
*
* If $fields is specified , only those fields are returned .
* Use this wherever possible to reduce memory requirements .
*
* If you only want some of the records , specify $limitfrom and $limitnum .
* The query will skip the first $limitfrom records ( according to the sort
* order ) and then return the next $limitnum records . If either of $limitfrom
* or $limitnum is specified , both must be present .
*
* The return value is an ADODB RecordSet object
* @ link http :// phplens . com / adodb / reference . functions . adorecordset . html
* if the query succeeds . If an error occurrs , false is returned .
*
* @ param string $table the table to query .
* @ param string $field a field to check ( optional ) .
* @ param string $value the value the field must have ( requred if field1 is given , else optional ) .
* @ param string $sort an order to sort the results in ( optional , a valid SQL ORDER BY parameter ) .
* @ param string $fields a comma separated list of fields to return ( optional , by default all fields are returned ) .
* @ param int $limitfrom return a subset of records , starting at this point ( optional , required if $limitnum is set ) .
* @ param int $limitnum return a subset comprising this many records ( optional , required if $limitfrom is set ) .
* @ return mixed an ADODB RecordSet object , or false if an error occured .
*/
function get_recordset ( $table , $field = '' , $value = '' , $sort = '' , $fields = '*' , $limitfrom = '' , $limitnum = '' ) {
if ( $field ) {
$select = " $field = ' $value ' " ;
} else {
$select = '' ;
}
get_recordset_select ( $table , $select , $sort , $fields , $limitfrom , $limitnum );
}
/**
* Get a number of records as an ADODB RecordSet .
*
* If given , $select is used as the SELECT parameter in the SQL query ,
* otherwise all records from the table are returned .
*
* Other arguments and the return type as for @ see function get_recordset .
*
* @ uses $CFG
* @ param string $table the table to query .
* @ param string $select A fragment of SQL to be used in a where clause in the SQL call .
* @ param string $sort an order to sort the results in ( optional , a valid SQL ORDER BY parameter ) .
* @ param string $fields a comma separated list of fields to return ( optional , by default all fields are returned ) .
* @ param int $limitfrom return a subset of records , starting at this point ( optional , required if $limitnum is set ) .
* @ param int $limitnum return a subset comprising this many records ( optional , required if $limitfrom is set ) .
* @ return mixed an ADODB RecordSet object , or false if an error occured .
*/
function get_recordset_select ( $table , $select = '' , $sort = '' , $fields = '*' , $limitfrom = '' , $limitnum = '' ) {
global $CFG ;
if ( $select ) {
$select = ' WHERE ' . $select ;
}
if ( $limitfrom !== '' ) {
$limit = sql_paging_limit ( $limitfrom , $limitnum );
} else {
$limit = '' ;
}
if ( $sort ) {
$sort = ' ORDER BY ' . $sort ;
}
return get_recordset_sql ( 'SELECT ' . $fields . ' FROM ' . $CFG -> prefix . $table . $select . $sort . ' ' . $limit );
}
/**
* Get a number of records as an ADODB RecordSet .
*
* Only records where $field takes one of the values $values are returned .
* $values should be a comma - separated list of values , for example " 4,5,6,10 "
* or " 'foo','bar','baz' " .
*
* Other arguments and the return type as for @ see function get_recordset .
*
* @ param string $table the table to query .
* @ param string $field a field to check ( optional ) .
* @ param string $values the value the field must have ( requred if field1 is given , else optional ) .
* @ param string $sort an order to sort the results in ( optional , a valid SQL ORDER BY parameter ) .
* @ param string $fields a comma separated list of fields to return ( optional , by default all fields are returned ) .
* @ param int $limitfrom return a subset of records , starting at this point ( optional , required if $limitnum is set ) .
* @ param int $limitnum return a subset comprising this many records ( optional , required if $limitfrom is set ) .
* @ return mixed an ADODB RecordSet object , or false if an error occured .
*/
function get_recordset_list ( $table , $field = '' , $values = '' , $sort = '' , $fields = '*' , $limitfrom = '' , $limitnum = '' ) {
global $CFG ;
if ( $field ) {
$select = " $field IN ( $values ) " ;
} else {
$select = '' ;
}
get_recordset_select ( $table , $select , $sort , $fields , $limitfrom , $limitnum );
}
/**
* Get a number of records as an ADODB RecordSet .
*
* $sql must be a complete SQL query .
*
* The return type is as for @ see function get_recordset .
*
* @ uses $CFG
* @ uses $db
* @ param string $sql the SQL select query to execute .
* @ return mixed an ADODB RecordSet object , or false if an error occured .
*/
function get_recordset_sql ( $sql ) {
global $CFG , $db ;
if ( defined ( 'MDL_PERFDB' )) { global $PERF ; $PERF -> dbqueries ++ ; };
if ( ! $rs = $db -> Execute ( $sql )) {
if ( isset ( $CFG -> debug ) and $CFG -> debug > 7 ) {
notify ( $db -> ErrorMsg () . '<br /><br />' . $sql );
}
if ( ! empty ( $CFG -> dblogerror )) {
$debug = array_shift ( debug_backtrace ());
error_log ( " SQL " . $db -> ErrorMsg () . " in { $debug [ 'file' ] } on line { $debug [ 'line' ] } . STATEMENT: $sql " );
}
return false ;
}
return $rs ;
}
2002-12-29 16:28:09 +00:00
2003-09-14 04:04:15 +00:00
/**
2006-01-16 06:17:38 +00:00
* Utility function used by the following 4 methods .
*
* @ param object an ADODB RecordSet object .
* @ return mixed mixed an array of objects , or false if an error occured or the RecordSet was empty .
*/
function recordset_to_array ( $rs ) {
if ( $rs && $rs -> RecordCount () > 0 ) {
if ( $records = $rs -> GetAssoc ( true )) {
foreach ( $records as $key => $record ) {
$objects [ $key ] = ( object ) $record ;
}
return $objects ;
} else {
return false ;
}
} else {
return false ;
}
}
/**
* Get a number of records as an array of objects .
2004-09-24 21:28:22 +00:00
*
2006-01-16 06:17:38 +00:00
* Convenience call -- use only for small datasets .
* Consider using @ see function get_recordset instead .
2004-09-24 21:28:22 +00:00
*
2006-01-16 06:17:38 +00:00
* Arguments as for @ see function get_recordset .
*
* If the query succeeds and returns at least one record , the
* return value is an array of objects , one object for each
* record found . The array key is the value from the first
* column of the result set . The object associated with that key
* has a member variable for each column of the results .
*
* @ param string $table the table to query .
2006-01-16 06:07:57 +00:00
* @ param string $field a field to check ( optional ) .
* @ param string $value the value the field must have ( requred if field1 is given , else optional ) .
2006-01-16 06:17:38 +00:00
* @ param string $sort an order to sort the results in ( optional , a valid SQL ORDER BY parameter ) .
* @ param string $fields a comma separated list of fields to return ( optional , by default all fields are returned ) .
* @ param int $limitfrom return a subset of records , starting at this point ( optional , required if $limitnum is set ) .
* @ param int $limitnum return a subset comprising this many records ( optional , required if $limitfrom is set ) .
* @ return mixed an array of objects , or false if no records were found or an error occured .
2004-09-24 21:28:22 +00:00
*/
2004-09-22 16:15:23 +00:00
function get_records ( $table , $field = '' , $value = '' , $sort = '' , $fields = '*' , $limitfrom = '' , $limitnum = '' ) {
2006-01-16 06:17:38 +00:00
$rs = get_recordset ( $table , $field , $value , $sort , $fields , $limitfrom , $limitnum );
return recordset_to_array ( $rs );
2002-12-17 04:41:18 +00:00
}
2003-09-14 04:04:15 +00:00
/**
2006-01-16 06:17:38 +00:00
* Get a number of records as an array of objects .
2004-09-24 21:28:22 +00:00
*
2006-01-16 06:17:38 +00:00
* Convenience call -- use only for small datasets .
* Consider using @ see function get_recordset_select instead .
2004-09-24 21:28:22 +00:00
*
2006-01-16 06:17:38 +00:00
* Arguments as for @ see function get_recordset_select .
* Return value as for @ see function get_records .
*
* @ param string $table the table to query .
2004-09-24 21:28:22 +00:00
* @ param string $select A fragment of SQL to be used in a where clause in the SQL call .
2006-01-16 06:17:38 +00:00
* @ param string $sort an order to sort the results in ( optional , a valid SQL ORDER BY parameter ) .
* @ param string $fields a comma separated list of fields to return ( optional , by default all fields are returned ) .
* @ param int $limitfrom return a subset of records , starting at this point ( optional , required if $limitnum is set ) .
* @ param int $limitnum return a subset comprising this many records ( optional , required if $limitfrom is set ) .
* @ return mixed an array of objects , or false if no records were found or an error occured .
2004-09-24 21:28:22 +00:00
*/
2004-09-22 16:15:23 +00:00
function get_records_select ( $table , $select = '' , $sort = '' , $fields = '*' , $limitfrom = '' , $limitnum = '' ) {
2006-01-16 06:17:38 +00:00
$rs = get_recordset_select ( $table , $select , $sort , $fields , $limitfrom , $limitnum );
return recordset_to_array ( $rs );
2002-12-20 14:44:14 +00:00
}
2003-09-14 04:04:15 +00:00
/**
2006-01-16 06:17:38 +00:00
* Get a number of records as an array of objects .
2004-09-24 21:28:22 +00:00
*
2006-01-16 06:17:38 +00:00
* Convenience call -- use only for small datasets .
* Consider using @ see function get_recordset_list instead .
*
* Arguments as for @ see function get_recordset_list .
* Return value as for @ see function get_records .
2004-09-24 21:28:22 +00:00
*
* @ param string $table The database table to be checked against .
2005-04-11 10:18:33 +00:00
* @ param string $field The field to search
* @ param string $values Comma separated list of possible value
* @ param string $sort Sort order ( as valid SQL sort parameter )
2004-09-24 21:28:22 +00:00
* @ param string $fields A comma separated list of fields to be returned from the chosen table .
2006-01-16 06:17:38 +00:00
* @ return mixed an array of objects , or false if no records were found or an error occured .
2004-09-24 21:28:22 +00:00
*/
2006-01-16 06:17:38 +00:00
function get_records_list ( $table , $field = '' , $values = '' , $sort = '' , $fields = '*' , $limitfrom = '' , $limitnum = '' ) {
$rs = get_recordset_list ( $table , $field , $values , $sort , $fields , $limitfrom , $limitnum );
return recordset_to_array ( $rs );
2002-12-17 04:41:18 +00:00
}
2003-09-14 04:04:15 +00:00
/**
2006-01-16 06:17:38 +00:00
* Get a number of records as an array of objects .
2004-09-24 21:28:22 +00:00
*
2006-01-16 06:17:38 +00:00
* Convenience call -- use only for small datasets .
* Consider using @ see function get_recordset_sql instead .
2004-09-24 21:28:22 +00:00
*
2006-01-16 06:17:38 +00:00
* Arguments as for @ see function get_recordset_sql .
* Return value as for @ see function get_records .
*
* @ param string $sql the SQL select query to execute .
* @ return mixed an array of objects , or false if no records were found or an error occured .
2004-09-24 21:28:22 +00:00
*/
2002-12-17 04:41:18 +00:00
function get_records_sql ( $sql ) {
2006-01-16 06:17:38 +00:00
$rs = get_recordset_sql ( $sql );
return recordset_to_array ( $rs );
2002-12-17 04:41:18 +00:00
}
2003-09-14 04:04:15 +00:00
/**
2006-01-16 06:23:17 +00:00
* Utility function used by the following 3 methods .
*
* @ param object an ADODB RecordSet object with two columns .
* @ return mixed an associative array , or false if an error occured or the RecordSet was empty .
*/
function recordset_to_menu ( $rs ) {
if ( $rs && $rs -> RecordCount () > 0 ) {
while ( ! $rs -> EOF ) {
$menu [ $rs -> fields [ 0 ]] = $rs -> fields [ 1 ];
$rs -> MoveNext ();
}
return $menu ;
} else {
return false ;
}
}
/**
* Get the first two columns from a number of records as an associative array .
2006-01-16 06:07:57 +00:00
*
2006-01-16 06:23:17 +00:00
* Arguments as for @ see function get_recordset .
*
* If no errors occur , and at least one records is found , the return value
* is an associative whose keys come from the first field of each record ,
* and whose values are the corresponding second fields . If no records are found ,
* or an error occurs , false is returned .
2006-01-16 06:07:57 +00:00
*
2006-01-16 06:23:17 +00:00
* @ param string $table the table to query .
2006-01-16 06:07:57 +00:00
* @ param string $field a field to check ( optional ) .
* @ param string $value the value the field must have ( requred if field1 is given , else optional ) .
2006-01-16 06:23:17 +00:00
* @ param string $sort an order to sort the results in ( optional , a valid SQL ORDER BY parameter ) .
* @ param string $fields a comma separated list of fields to return ( optional , by default all fields are returned ) .
* @ return mixed an associative array , or false if no records were found or an error occured .
2006-01-16 06:07:57 +00:00
*/
2004-09-22 16:15:23 +00:00
function get_records_menu ( $table , $field = '' , $value = '' , $sort = '' , $fields = '*' ) {
2006-01-16 06:23:17 +00:00
$rs = get_recordset ( $table , $field , $value , $sort , $fields );
return recordset_to_menu ( $rs );
2002-12-20 14:44:14 +00:00
}
2003-09-14 04:04:15 +00:00
/**
2006-01-16 06:23:17 +00:00
* Get the first two columns from a number of records as an associative array .
*
* Arguments as for @ see function get_recordset_select .
* Return value as for @ see function get_records_menu .
2004-09-24 21:28:22 +00:00
*
* @ param string $table The database table to be checked against .
* @ param string $select A fragment of SQL to be used in a where clause in the SQL call .
2005-04-11 10:18:33 +00:00
* @ param string $sort Sort order ( optional ) - a valid SQL order parameter
2004-09-24 21:28:22 +00:00
* @ param string $fields A comma separated list of fields to be returned from the chosen table .
2006-01-16 06:23:17 +00:00
* @ return mixed an associative array , or false if no records were found or an error occured .
2004-09-24 21:28:22 +00:00
*/
2004-09-22 16:15:23 +00:00
function get_records_select_menu ( $table , $select = '' , $sort = '' , $fields = '*' ) {
2006-01-16 06:23:17 +00:00
$rs = get_recordset_select ( $table , $select , $sort , $fields );
return recordset_to_menu ( $rs );
2002-12-20 14:44:14 +00:00
}
2003-09-14 04:04:15 +00:00
/**
2006-01-16 06:23:17 +00:00
* Get the first two columns from a number of records as an associative array .
2004-09-24 21:28:22 +00:00
*
2006-01-16 06:23:17 +00:00
* Arguments as for @ see function get_recordset_sql .
* Return value as for @ see function get_records_menu .
2004-09-24 21:28:22 +00:00
*
* @ param string $sql The SQL string you wish to be executed .
2006-01-16 06:23:17 +00:00
* @ return mixed an associative array , or false if no records were found or an error occured .
2004-09-24 21:28:22 +00:00
*/
2002-12-17 04:41:18 +00:00
function get_records_sql_menu ( $sql ) {
2006-01-16 06:23:17 +00:00
$rs = get_recordset_sql ( $sql );
return recordset_to_menu ( $rs );
2002-12-17 04:41:18 +00:00
}
2003-09-14 04:04:15 +00:00
/**
2006-01-16 06:07:57 +00:00
* Get a single value from a table row where all the given fields match the given values .
2004-09-24 21:28:22 +00:00
*
2006-01-16 06:07:57 +00:00
* @ param string $table the table to query .
* @ param string $return the field to return the value of .
* @ param string $field1 the first field to check ( optional ) .
* @ param string $value1 the value field1 must have ( requred if field1 is given , else optional ) .
* @ param string $field2 the second field to check ( optional ) .
* @ param string $value2 the value field2 must have ( requred if field2 is given , else optional ) .
* @ param string $field3 the third field to check ( optional ) .
* @ param string $value3 the value field3 must have ( requred if field3 is given , else optional ) .
* @ return mixed the specified value , or false if an error occured .
2004-09-24 21:28:22 +00:00
*/
2004-09-22 16:15:23 +00:00
function get_field ( $table , $return , $field1 , $value1 , $field2 = '' , $value2 = '' , $field3 = '' , $value3 = '' ) {
2006-01-16 06:24:53 +00:00
2006-01-16 06:07:57 +00:00
$select = where_clause ( $field1 , $value1 , $field2 , $value2 , $field3 , $value3 );
2003-04-26 12:38:04 +00:00
2006-01-16 06:24:53 +00:00
$rs = get_recordset_sql ( 'SELECT ' . $return . ' FROM ' . $CFG -> prefix . $table . ' ' . $select );
2005-04-07 00:11:28 +00:00
2006-01-16 06:24:53 +00:00
if ( $rs && $rs -> RecordCount () == 1 ) {
2004-09-22 16:15:23 +00:00
return $rs -> fields [ $return ];
2002-12-17 04:41:18 +00:00
} else {
return false ;
}
}
2004-07-25 13:47:38 +00:00
/**
2006-01-16 06:24:53 +00:00
* Get a single value from a table .
2004-09-24 21:28:22 +00:00
*
2006-01-16 06:24:53 +00:00
* @ param string $sql an SQL statement expected to return a single value .
* @ return mixed the specified value , or false if an error occured .
2004-09-24 21:28:22 +00:00
*/
2004-07-25 13:47:38 +00:00
function get_field_sql ( $sql ) {
2006-01-16 06:24:53 +00:00
$rs = get_recordset_sql ( $sql );
2004-07-25 13:47:38 +00:00
2006-01-16 06:24:53 +00:00
if ( $rs && $rs -> RecordCount () == 1 ) {
2004-07-25 13:47:38 +00:00
return $rs -> fields [ 0 ];
} else {
return false ;
}
}
2006-01-16 03:07:23 +00:00
/**
* Get an array of data from one or more fields from a database
* use to get a column , or a series of distinct values
*
* @ uses $CFG
* @ uses $db
* @ param string $sql The SQL string you wish to be executed .
* @ return mixed | false Returns the value return from the SQL statment or false if an error occured .
* @ todo Finish documenting this function
*/
function get_fieldset_sql ( $sql ) {
global $db , $CFG ;
if ( defined ( 'MDL_PERFDB' )) { global $PERF ; $PERF -> dbqueries ++ ; };
$rs = $db -> Execute ( $sql );
if ( ! $rs ) {
if ( isset ( $CFG -> debug ) and $CFG -> debug > 7 ) {
notify ( $db -> ErrorMsg () . '<br /><br />' . $sql );
}
if ( ! empty ( $CFG -> dblogerror )) {
$debug = array_shift ( debug_backtrace ());
error_log ( " SQL " . $db -> ErrorMsg () . " in { $debug [ 'file' ] } on line { $debug [ 'line' ] } . STATEMENT: $sql " );
}
return false ;
}
if ( $rs -> RecordCount () > 0 ) {
$results = array ();
while ( ! $rs -> EOF ) {
array_push ( $results , $rs -> fields [ 0 ]);
$rs -> MoveNext ();
}
return $results ;
} else {
return false ;
}
}
2003-09-14 04:04:15 +00:00
/**
2006-01-16 06:07:57 +00:00
* Set a single field in the table row where all the given fields match the given values .
2004-09-24 21:28:22 +00:00
*
* @ uses $CFG
* @ uses $db
* @ param string $table The database table to be checked against .
2006-01-16 06:07:57 +00:00
* @ param string $newfield the field to set .
* @ param string $newvalue the value to set the field to .
* @ param string $field1 the first field to check ( optional ) .
* @ param string $value1 the value field1 must have ( requred if field1 is given , else optional ) .
* @ param string $field2 the second field to check ( optional ) .
* @ param string $value2 the value field2 must have ( requred if field2 is given , else optional ) .
* @ param string $field3 the third field to check ( optional ) .
* @ param string $value3 the value field3 must have ( requred if field3 is given , else optional ) .
* @ return mixed An ADODB RecordSet object with the results from the SQL call or false .
2004-09-24 21:28:22 +00:00
*/
2004-09-22 16:15:23 +00:00
function set_field ( $table , $newfield , $newvalue , $field1 , $value1 , $field2 = '' , $value2 = '' , $field3 = '' , $value3 = '' ) {
2002-12-17 04:41:18 +00:00
global $db , $CFG ;
2005-04-07 00:11:28 +00:00
if ( defined ( 'MDL_PERFDB' )) { global $PERF ; $PERF -> dbqueries ++ ; };
2006-01-16 06:07:57 +00:00
$select = where_clause ( $field1 , $value1 , $field2 , $value2 , $field3 , $value3 );
2003-04-26 12:38:04 +00:00
2004-09-22 16:15:23 +00:00
return $db -> Execute ( 'UPDATE ' . $CFG -> prefix . $table . ' SET ' . $newfield . ' = \'' . $newvalue . '\' ' . $select );
2002-12-17 04:41:18 +00:00
}
2003-09-14 04:04:15 +00:00
/**
2006-01-16 06:07:57 +00:00
* Delete the records from a table where all the given fields match the given values .
2004-09-24 21:28:22 +00:00
*
* @ uses $CFG
* @ uses $db
2006-01-16 06:07:57 +00:00
* @ param string $table the table to delete from .
* @ param string $field1 the first field to check ( optional ) .
* @ param string $value1 the value field1 must have ( requred if field1 is given , else optional ) .
* @ param string $field2 the second field to check ( optional ) .
* @ param string $value2 the value field2 must have ( requred if field2 is given , else optional ) .
* @ param string $field3 the third field to check ( optional ) .
* @ param string $value3 the value field3 must have ( requred if field3 is given , else optional ) .
* @ return mixed An ADODB RecordSet object with the results from the SQL call or false .
2004-09-24 21:28:22 +00:00
*/
2004-09-22 16:15:23 +00:00
function delete_records ( $table , $field1 = '' , $value1 = '' , $field2 = '' , $value2 = '' , $field3 = '' , $value3 = '' ) {
2002-12-17 04:41:18 +00:00
global $db , $CFG ;
2005-04-07 00:11:28 +00:00
if ( defined ( 'MDL_PERFDB' )) { global $PERF ; $PERF -> dbqueries ++ ; };
2006-01-16 06:07:57 +00:00
$select = where_clause ( $field1 , $value1 , $field2 , $value2 , $field3 , $value3 );
2002-12-17 04:41:18 +00:00
2004-09-22 16:15:23 +00:00
return $db -> Execute ( 'DELETE FROM ' . $CFG -> prefix . $table . ' ' . $select );
2002-12-17 04:41:18 +00:00
}
2003-09-14 04:04:15 +00:00
/**
2004-09-24 21:28:22 +00:00
* Delete one or more records from a table
*
* @ uses $CFG
* @ uses $db
* @ param string $table The database table to be checked against .
* @ param string $select A fragment of SQL to be used in a where clause in the SQL call ( used to define the selection criteria ) .
2005-07-12 02:23:58 +00:00
* @ return object A PHP standard object with the results from the SQL call .
2004-09-24 21:28:22 +00:00
* @ todo Verify return type .
*/
2004-09-22 16:15:23 +00:00
function delete_records_select ( $table , $select = '' ) {
2003-04-27 06:18:03 +00:00
global $CFG , $db ;
2005-04-07 00:11:28 +00:00
if ( defined ( 'MDL_PERFDB' )) { global $PERF ; $PERF -> dbqueries ++ ; };
2003-04-27 06:18:03 +00:00
if ( $select ) {
2004-09-22 16:15:23 +00:00
$select = 'WHERE ' . $select ;
2003-04-27 06:18:03 +00:00
}
2004-09-22 16:15:23 +00:00
return $db -> Execute ( 'DELETE FROM ' . $CFG -> prefix . $table . ' ' . $select );
2003-04-27 06:18:03 +00:00
}
2003-09-14 04:04:15 +00:00
/**
2004-09-24 21:28:22 +00:00
* Insert a record into a table and return the " id " field if required
*
* If the return ID isn ' t required , then this just reports success as true / false .
* $dataobject is an object containing needed data
*
* @ uses $db
* @ uses $CFG
* @ param string $table The database table to be checked against .
2005-04-01 06:57:26 +00:00
* @ param array $dataobject A data object with values for one or more fields in the record
2005-07-12 02:23:58 +00:00
* @ param bool $returnid Should the id of the newly created record entry be returned ? If this option is not requested then true / false is returned .
2005-04-01 06:57:26 +00:00
* @ param string $primarykey The primary key of the table we are inserting into ( almost always " id " )
2004-09-24 21:28:22 +00:00
*/
2004-01-30 09:04:31 +00:00
function insert_record ( $table , $dataobject , $returnid = true , $primarykey = 'id' ) {
2004-09-21 11:41:58 +00:00
2004-01-29 15:27:21 +00:00
global $db , $CFG ;
2005-10-31 06:25:17 +00:00
static $empty_rs_cache ;
2004-01-29 15:27:21 +00:00
2005-04-07 00:11:28 +00:00
if ( defined ( 'MDL_PERFDB' )) { global $PERF ; $PERF -> dbqueries ++ ; };
2005-10-31 06:25:17 +00:00
/// Get an empty recordset. Cache for multiple inserts.
2004-09-21 11:41:58 +00:00
2005-10-31 06:25:17 +00:00
if ( empty ( $empty_rs_cache [ $table ])) {
/// Execute a dummy query to get an empty recordset
if ( ! $empty_rs_cache [ $table ] = $db -> Execute ( 'SELECT * FROM ' . $CFG -> prefix . $table . ' WHERE ' . $primarykey . ' = \'-1\'' )) {
return false ;
}
/// In Moodle we always use auto-numbering fields for the primary ID
/// so let's unset it now before it causes any trouble
unset ( $dataobject -> { $primarykey });
}
2005-03-02 06:33:04 +00:00
2005-10-31 06:25:17 +00:00
$rs = $empty_rs_cache [ $table ];
2005-03-02 06:33:04 +00:00
2005-04-01 05:55:10 +00:00
/// Postgres doesn't have the concept of primary key built in
/// and will return the OID which isn't what we want.
/// The efficient and transaction-safe strategy is to
/// move the sequence forward first, and make the insert
/// with an explicit id.
2005-04-06 22:25:31 +00:00
if ( empty ( $dataobject -> { $primarykey })
2005-04-01 05:55:10 +00:00
&& $CFG -> dbtype === 'postgres7'
&& $returnid == true ) {
2005-04-03 21:04:02 +00:00
if ( $nextval = ( int ) get_field_sql ( " SELECT NEXTVAL(' { $CFG -> prefix } { $table } _ { $primarykey } _seq') " )) {
2005-04-01 05:55:10 +00:00
$dataobject -> { $primarykey } = $nextval ;
}
}
2004-02-03 15:18:41 +00:00
/// Get the correct SQL from adoDB
if ( ! $insertSQL = $db -> GetInsertSQL ( $rs , ( array ) $dataobject , true )) {
return false ;
}
2004-09-21 11:41:58 +00:00
2004-02-03 15:18:41 +00:00
/// Run the SQL statement
if ( ! $rs = $db -> Execute ( $insertSQL )) {
2004-03-10 07:59:32 +00:00
if ( isset ( $CFG -> debug ) and $CFG -> debug > 7 ) {
2004-09-22 16:15:23 +00:00
notify ( $db -> ErrorMsg () . '<br /><br />' . $insertSQL );
2004-03-09 16:32:24 +00:00
}
2005-05-11 23:34:46 +00:00
if ( ! empty ( $CFG -> dblogerror )) {
2005-05-19 21:59:00 +00:00
$debug = array_shift ( debug_backtrace ());
2005-05-11 23:34:46 +00:00
error_log ( " SQL " . $db -> ErrorMsg () . " in { $debug [ 'file' ] } on line { $debug [ 'line' ] } . STATEMENT: $insertSQL " );
}
2004-01-29 15:27:21 +00:00
return false ;
}
2004-09-21 11:41:58 +00:00
2004-02-03 15:18:41 +00:00
/// If a return ID is not needed then just return true now
2004-09-21 11:41:58 +00:00
if ( ! $returnid ) {
2004-01-29 15:27:21 +00:00
return true ;
}
2005-04-01 06:57:26 +00:00
/// We already know the record PK if it's been passed explicitly,
/// or if we've retrieved it from a sequence (Postgres).
2005-04-06 22:25:31 +00:00
if ( ! empty ( $dataobject -> { $primarykey })) {
2005-04-01 05:55:10 +00:00
return $dataobject -> { $primarykey };
}
2004-01-29 15:27:21 +00:00
2005-04-01 05:55:10 +00:00
/// This only gets triggered with non-Postgres databases
/// however we have some postgres fallback in case we failed
/// to find the sequence.
$id = $db -> Insert_ID ();
if ( $CFG -> dbtype === 'postgres7' ) {
// try to get the primary key based on id
if ( ( $rs = $db -> Execute ( 'SELECT ' . $primarykey . ' FROM ' . $CFG -> prefix . $table . ' WHERE oid = ' . $id ))
&& ( $rs -> RecordCount () == 1 ) ) {
trigger_error ( " Retrieved $primarykey from oid on table $table because we could not find the sequence. " );
return ( integer ) $rs -> fields [ 0 ];
}
2005-04-01 06:57:26 +00:00
trigger_error ( 'Failed to retrieve primary key after insert: SELECT ' . $primarykey .
' FROM ' . $CFG -> prefix . $table . ' WHERE oid = ' . $id );
2005-04-01 05:55:10 +00:00
return false ;
2004-01-29 15:27:21 +00:00
}
2005-04-01 05:55:10 +00:00
return ( integer ) $id ;
2004-01-29 15:27:21 +00:00
}
2005-04-11 12:29:17 +00:00
/**
* Escape all dangerous characters in a data record
*
* $dataobject is an object containing needed data
* Run over each field exectuting addslashes () function
* to escape SQL unfriendly characters ( e . g . quotes )
* Handy when writing back data read from the database
*
* @ param $dataobject Object containing the database record
* @ return object Same object with neccessary characters escaped
*/
function addslashes_object ( $dataobject ) {
$a = get_object_vars ( $dataobject );
foreach ( $a as $key => $value ) {
$a [ $key ] = addslashes ( $value );
}
return ( object ) $a ;
}
2004-01-29 15:27:21 +00:00
2003-09-14 04:04:15 +00:00
/**
2004-09-24 21:28:22 +00:00
* Update a record in a table
*
* $dataobject is an object containing needed data
* Relies on $dataobject having a variable " id " to
* specify the record to update
*
* @ uses $CFG
* @ uses $db
* @ param string $table The database table to be checked against .
2005-04-11 12:29:17 +00:00
* @ param array $dataobject An object with contents equal to fieldname => fieldvalue . Must have an entry for 'id' to map to the table specified .
2005-07-12 02:23:58 +00:00
* @ return bool
2004-09-24 21:28:22 +00:00
* @ todo Finish documenting this function . Dataobject is actually an associateive array , correct ?
*/
2002-12-17 04:41:18 +00:00
function update_record ( $table , $dataobject ) {
global $db , $CFG ;
if ( ! isset ( $dataobject -> id ) ) {
return false ;
}
// Determine all the fields in the table
2004-09-22 16:15:23 +00:00
if ( ! $columns = $db -> MetaColumns ( $CFG -> prefix . $table )) {
2002-12-17 04:41:18 +00:00
return false ;
}
$data = ( array ) $dataobject ;
2005-04-07 00:11:28 +00:00
if ( defined ( 'MDL_PERFDB' )) { global $PERF ; $PERF -> dbqueries ++ ; };
2002-12-17 04:41:18 +00:00
// Pull out data matching these fields
foreach ( $columns as $column ) {
2004-09-22 16:15:23 +00:00
if ( $column -> name <> 'id' and isset ( $data [ $column -> name ]) ) {
2002-12-17 04:41:18 +00:00
$ddd [ $column -> name ] = $data [ $column -> name ];
}
}
// Construct SQL queries
$numddd = count ( $ddd );
$count = 0 ;
2004-09-22 16:15:23 +00:00
$update = '' ;
2002-12-17 04:41:18 +00:00
foreach ( $ddd as $key => $value ) {
$count ++ ;
2005-04-22 06:24:57 +00:00
$update .= $key . ' = \'' . $value . '\'' ; // All incoming data is already quoted
2002-12-17 04:41:18 +00:00
if ( $count < $numddd ) {
2004-09-22 16:15:23 +00:00
$update .= ', ' ;
2002-12-17 04:41:18 +00:00
}
}
2004-09-22 16:15:23 +00:00
if ( $rs = $db -> Execute ( 'UPDATE ' . $CFG -> prefix . $table . ' SET ' . $update . ' WHERE id = \'' . $dataobject -> id . '\'' )) {
2002-12-17 04:41:18 +00:00
return true ;
} else {
2004-03-10 07:59:32 +00:00
if ( isset ( $CFG -> debug ) and $CFG -> debug > 7 ) {
2004-09-22 16:15:23 +00:00
notify ( $db -> ErrorMsg () . '<br /><br />UPDATE ' . $CFG -> prefix . $table . ' SET ' . $update . ' WHERE id = \'' . $dataobject -> id . '\'' );
2004-03-09 16:32:24 +00:00
}
2005-05-11 23:34:46 +00:00
if ( ! empty ( $CFG -> dblogerror )) {
2005-05-19 21:59:00 +00:00
$debug = array_shift ( debug_backtrace ());
2005-05-11 23:34:46 +00:00
error_log ( " SQL " . $db -> ErrorMsg () . " in { $debug [ 'file' ] } on line { $debug [ 'line' ] } . STATEMENT: UPDATE $CFG->prefix $table SET $update WHERE id = ' $dataobject->id ' " );
}
2002-12-17 04:41:18 +00:00
return false ;
}
}
/// USER DATABASE ////////////////////////////////////////////////
2003-09-14 04:04:15 +00:00
/**
2004-09-24 21:28:22 +00:00
* Does this username and password specify a valid admin user ?
*
* @ uses $CFG
* @ param string $username The name of the user to be tested for admin rights
* @ param string $md5password The password supplied by the user in md5 encrypted format .
2005-07-12 02:23:58 +00:00
* @ return bool
2004-09-24 21:28:22 +00:00
*/
2002-12-17 04:41:18 +00:00
function adminlogin ( $username , $md5password ) {
global $CFG ;
2004-09-21 11:41:58 +00:00
return record_exists_sql ( " SELECT u.id
FROM { $CFG -> prefix } user u ,
{ $CFG -> prefix } user_admins a
WHERE u . id = a . userid
AND u . username = '$username'
2002-12-17 04:41:18 +00:00
AND u . password = '$md5password' " );
}
2003-09-14 04:04:15 +00:00
/**
2004-09-24 21:28:22 +00:00
* Get the guest user information from the database
*
* @ return object ( user ) An associative array with the details of the guest user account .
* @ todo Is object ( user ) a correct return type ? Or is array the proper return type with a note that the contents include all details for a user .
*/
2002-12-20 14:44:14 +00:00
function get_guest () {
2005-04-17 12:08:46 +00:00
return get_complete_user_data ( 'username' , 'guest' );
2002-12-20 14:44:14 +00:00
}
2003-09-14 04:04:15 +00:00
/**
2004-09-24 21:28:22 +00:00
* Returns $user object of the main admin user
*
* @ uses $CFG
* @ return object ( admin ) An associative array representing the admin user .
* @ todo Verify documentation of this function
*/
2002-12-17 04:41:18 +00:00
function get_admin () {
global $CFG ;
if ( $admins = get_admins () ) {
foreach ( $admins as $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
/**
2004-09-24 21:28:22 +00:00
* Returns list of all admins
*
* @ 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 () {
global $CFG ;
2004-09-21 11:41:58 +00:00
return get_records_sql ( " SELECT u.*, a.id as adminid
FROM { $CFG -> prefix } user u ,
2002-12-23 09:39:26 +00:00
{ $CFG -> prefix } user_admins a
WHERE a . userid = u . id
2004-03-24 09:38:47 +00:00
ORDER BY a . id ASC " );
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 list of all creators
*
* @ uses $CFG
2005-07-12 02:23:58 +00:00
* @ return object
2004-09-24 21:28:22 +00:00
*/
2003-02-24 18:48:55 +00:00
function get_creators () {
global $CFG ;
return get_records_sql ( " SELECT u.*
FROM { $CFG -> prefix } user u ,
{ $CFG -> prefix } user_coursecreators a
WHERE a . userid = u . id
ORDER BY u . id ASC " );
}
2002-12-17 04:41:18 +00:00
2005-01-24 22:21:28 +00:00
function get_courses_in_metacourse ( $metacourseid ) {
global $CFG ;
2005-01-25 04:33:02 +00:00
$sql = " SELECT c.id,c.shortname,c.fullname FROM { $CFG -> prefix } course c, { $CFG -> prefix } course_meta mc WHERE mc.parent_course = $metacourseid
2005-08-11 04:24:57 +00:00
AND mc . child_course = c . id ORDER BY c . shortname " ;
2005-01-24 22:21:28 +00:00
return get_records_sql ( $sql );
}
function get_courses_notin_metacourse ( $metacourseid , $count = false ) {
global $CFG ;
if ( $count ) {
$sql = " SELECT COUNT(c.id) " ;
2005-03-03 04:41:46 +00:00
} else {
2005-01-24 22:21:28 +00:00
$sql = " SELECT c.id,c.shortname,c.fullname " ;
}
2005-02-07 09:22:07 +00:00
2005-02-07 09:32:24 +00:00
$alreadycourses = get_courses_in_metacourse ( $metacourseid );
2005-03-03 04:41:46 +00:00
$sql .= " FROM { $CFG -> prefix } course c WHERE " . (( ! empty ( $alreadycourses )) ? " c.id NOT IN ( " . implode ( ',' , array_keys ( $alreadycourses )) . " )
2005-08-11 04:24:57 +00:00
AND " : " " ). " c . id != $metacourseid and c . id != " .SITEID. " and c . metacourse != 1 " .((empty( $count )) ? " ORDER BY c . shortname " : " " );
2005-01-24 22:21:28 +00:00
return get_records_sql ( $sql );
}
2003-09-14 04:04:15 +00:00
/**
2004-09-24 21:28:22 +00:00
* Returns $user object of the main teacher for a course
*
* @ uses $CFG
* @ param int $courseid The course in question .
2004-09-25 05:29:21 +00:00
* @ return user | false A { @ link $USER } record of the main teacher for the specified course or false if error .
2004-09-24 21:28:22 +00:00
* @ todo Finish documenting this function
*/
2002-12-17 04:41:18 +00:00
function get_teacher ( $courseid ) {
global $CFG ;
2004-09-22 16:15:23 +00:00
if ( $teachers = get_course_teachers ( $courseid , 't.authority ASC' )) {
2002-12-17 04:41:18 +00:00
foreach ( $teachers as $teacher ) {
if ( $teacher -> authority ) {
return $teacher ; // the highest authority teacher
}
}
} else {
return false ;
}
}
2003-09-14 04:04:15 +00:00
/**
2004-09-24 21:28:22 +00:00
* Searches logs to find all enrolments since a certain date
*
* used to print recent activity
*
* @ uses $CFG
* @ param int $courseid The course in question .
2005-07-12 02:23:58 +00:00
* @ return object | false { @ link $USER } records or false if error .
2004-09-24 21:28:22 +00:00
* @ todo Finish documenting this function
*/
2003-08-17 01:45:26 +00:00
function get_recent_enrolments ( $courseid , $timestart ) {
global $CFG ;
2004-08-31 12:31:59 +00:00
return get_records_sql ( " SELECT DISTINCT u.id, u.firstname, u.lastname, l.time
2003-08-17 01:45:26 +00:00
FROM { $CFG -> prefix } user u ,
{ $CFG -> prefix } user_students s ,
{ $CFG -> prefix } log l
2004-09-21 11:41:58 +00:00
WHERE l . time > '$timestart'
2003-08-17 01:45:26 +00:00
AND l . course = '$courseid'
2004-09-21 11:41:58 +00:00
AND l . module = 'course'
2003-08-17 01:45:26 +00:00
AND l . action = 'enrol'
AND l . info = u . id
AND u . id = s . userid
2003-08-17 01:49:15 +00:00
AND s . course = '$courseid'
2003-08-17 01:45:26 +00:00
ORDER BY l . time ASC " );
}
2003-09-14 04:04:15 +00:00
/**
2004-09-24 21:28:22 +00:00
* Returns array of userinfo of all students in this course
* or on this site if courseid is id of site
*
* @ uses $CFG
* @ uses SITEID
* @ param int $courseid The course in question .
* @ param string $sort ?
* @ param string $dir ?
* @ param int $page ?
* @ param int $recordsperpage ?
* @ param string $firstinitial ?
* @ param string $lastinitial ?
* @ param ? $group ?
* @ param string $search ?
* @ param string $fields A comma separated list of fields to be returned from the chosen table .
* @ param string $exceptions ?
2005-07-12 02:23:58 +00:00
* @ return object
2004-09-24 21:28:22 +00:00
* @ todo Finish documenting this function
*/
2004-09-22 16:15:23 +00:00
function get_course_students ( $courseid , $sort = 's.timeaccess' , $dir = '' , $page = 0 , $recordsperpage = 99999 ,
$firstinitial = '' , $lastinitial = '' , $group = NULL , $search = '' , $fields = '' , $exceptions = '' ) {
2002-12-17 04:41:18 +00:00
global $CFG ;
2004-09-21 11:41:58 +00:00
if ( $courseid == SITEID and $CFG -> allusersaresitestudents ) {
2004-08-30 17:27:00 +00:00
// return users with confirmed, undeleted accounts who are not site teachers
// the following is a mess because of different conventions in the different user functions
$sort = str_replace ( 's.timeaccess' , 'lastaccess' , $sort ); // site users can't be sorted by timeaccess
$sort = str_replace ( 'timeaccess' , 'lastaccess' , $sort ); // site users can't be sorted by timeaccess
$sort = str_replace ( 'u.' , '' , $sort ); // the get_user function doesn't use the u. prefix to fields
2004-09-21 11:41:58 +00:00
$fields = str_replace ( 'u.' , '' , $fields );
2004-08-30 17:27:00 +00:00
if ( $sort ) {
2004-09-22 16:15:23 +00:00
$sort = $sort . ' ' . $dir ;
2004-08-30 17:27:00 +00:00
}
// Now we have to make sure site teachers are excluded
if ( $teachers = get_records ( 'user_teachers' , 'course' , SITEID )) {
foreach ( $teachers as $teacher ) {
2004-09-22 16:15:23 +00:00
$exceptions .= ',' . $teacher -> userid ;
2004-08-30 17:27:00 +00:00
}
$exceptions = ltrim ( $exceptions , ',' );
}
2004-09-21 11:41:58 +00:00
return get_users ( true , $search , true , $exceptions , $sort , $firstinitial , $lastinitial ,
2004-08-30 17:27:00 +00:00
$page , $recordsperpage , $fields ? $fields : '*' );
}
2002-12-17 04:41:18 +00:00
2005-02-26 09:20:31 +00:00
$limit = sql_paging_limit ( $page , $recordsperpage );
$LIKE = sql_ilike ();
$fullname = sql_fullname ( 'u.firstname' , 'u.lastname' );
2003-11-04 12:09:52 +00:00
2004-01-31 03:30:31 +00:00
$groupmembers = '' ;
2004-08-22 14:38:47 +00:00
// make sure it works on the site course
2004-09-22 16:15:23 +00:00
$select = 's.course = \'' . $courseid . '\' AND ' ;
2004-08-29 14:15:40 +00:00
if ( $courseid == SITEID ) {
2004-08-22 14:38:47 +00:00
$select = '' ;
}
2004-09-22 16:15:23 +00:00
$select .= 's.userid = u.id AND u.deleted = \'0\' ' ;
2004-03-19 04:26:35 +00:00
2004-08-07 06:05:37 +00:00
if ( ! $fields ) {
$fields = 'u.id, u.confirmed, u.username, u.firstname, u.lastname, ' .
'u.maildisplay, u.mailformat, u.maildigest, u.email, u.city, ' .
'u.country, u.picture, u.idnumber, u.department, u.institution, ' .
'u.emailstop, u.lang, u.timezone, s.timeaccess as lastaccess' ;
}
2004-03-19 04:26:35 +00:00
if ( $search ) {
2004-09-22 16:15:23 +00:00
$search = ' AND (' . $fullname . ' ' . $LIKE . '\'%' . $search . '%\' OR email ' . $LIKE . '\'%' . $search . '%\') ' ;
2004-03-19 04:26:35 +00:00
}
2003-11-04 12:09:52 +00:00
if ( $firstinitial ) {
2004-09-22 16:15:23 +00:00
$select .= ' AND u.firstname ' . $LIKE . '\'' . $firstinitial . '%\' ' ;
2003-11-04 12:09:52 +00:00
}
if ( $lastinitial ) {
2004-09-22 16:15:23 +00:00
$select .= ' AND u.lastname ' . $LIKE . '\'' . $lastinitial . '%\' ' ;
2003-09-18 04:46:34 +00:00
}
2004-01-31 04:03:46 +00:00
if ( $group === 0 ) { /// Need something here to get all students not in a group
return array ();
} else if ( $group !== NULL ) {
2004-09-22 16:15:23 +00:00
$groupmembers = ', ' . $CFG -> prefix . 'groups_members gm ' ;
$select .= ' AND u.id = gm.userid AND gm.groupid = \'' . $group . '\'' ;
2004-01-31 03:30:31 +00:00
}
2004-09-21 11:41:58 +00:00
2004-08-21 12:41:40 +00:00
if ( ! empty ( $exceptions )) {
2004-09-22 16:15:23 +00:00
$select .= ' AND u.id NOT IN (' . $exceptions . ')' ;
2004-08-21 12:41:40 +00:00
}
2004-01-31 03:30:31 +00:00
2004-02-19 17:54:32 +00:00
if ( $sort ) {
2004-09-22 16:15:23 +00:00
$sort = ' ORDER BY ' . $sort . ' ' ;
2004-02-19 17:54:32 +00:00
}
2004-08-30 17:27:00 +00:00
$students = get_records_sql ( " SELECT $fields
2004-03-20 06:58:52 +00:00
FROM { $CFG -> prefix } user u ,
{ $CFG -> prefix } user_students s
$groupmembers
2004-03-19 04:26:35 +00:00
WHERE $select $search $sort $dir $limit " );
2004-08-30 17:27:00 +00:00
if ( $courseid != SITEID ) {
return $students ;
}
2004-09-21 11:41:58 +00:00
2004-08-30 17:27:00 +00:00
// We are here because we need the students for the site.
// These also include teachers on real courses minus those on the site
if ( $teachers = get_records ( 'user_teachers' , 'course' , SITEID )) {
foreach ( $teachers as $teacher ) {
2004-09-22 16:15:23 +00:00
$exceptions .= ',' . $teacher -> userid ;
2004-08-30 17:27:00 +00:00
}
$exceptions = ltrim ( $exceptions , ',' );
2004-09-22 16:15:23 +00:00
$select .= ' AND u.id NOT IN (' . $exceptions . ')' ;
2004-08-30 17:27:00 +00:00
}
if ( ! $teachers = get_records_sql ( " SELECT $fields
FROM { $CFG -> prefix } user u ,
{ $CFG -> prefix } user_teachers s
$groupmembers
WHERE $select $search $sort $dir $limit " )) {
return $students ;
}
if ( ! $students ) {
return $teachers ;
}
return $teachers + $students ;
2002-12-17 04:41:18 +00:00
}
2004-09-24 21:28:22 +00:00
2003-11-04 12:09:52 +00:00
/**
2004-09-24 21:28:22 +00:00
* Counts the students in a given course ( or site ), or a subset of them
*
* @ param object $course The course in question as a course object .
* @ param string $search ?
* @ param string $firstinitial ?
* @ param string $lastinitial ?
* @ param ? $group ?
* @ param string $exceptions ?
* @ return int
* @ todo Finish documenting this function
*/
2004-09-22 16:15:23 +00:00
function count_course_students ( $course , $search = '' , $firstinitial = '' , $lastinitial = '' , $group = NULL , $exceptions = '' ) {
2003-11-04 12:09:52 +00:00
2004-08-30 17:27:00 +00:00
if ( $students = get_course_students ( $course -> id , '' , '' , 0 , 999999 , $firstinitial , $lastinitial , $group , $search , '' , $exceptions )) {
return count ( $students );
2003-11-04 12:09:52 +00:00
}
2004-08-30 17:27:00 +00:00
return 0 ;
2003-11-04 12:09:52 +00:00
}
2003-09-14 04:04:15 +00:00
/**
2004-09-24 21:28:22 +00:00
* Returns list of all teachers in this course
*
* If $courseid matches the site id then this function
* returns a list of all teachers for the site .
*
* @ uses $CFG
* @ param int $courseid The course in question .
* @ param string $sort ?
* @ param string $exceptions ?
2005-07-12 02:23:58 +00:00
* @ return object
2004-09-24 21:28:22 +00:00
* @ todo Finish documenting this function
*/
2004-09-22 16:15:23 +00:00
function get_course_teachers ( $courseid , $sort = 't.authority ASC' , $exceptions = '' ) {
2002-12-17 04:41:18 +00:00
global $CFG ;
2004-08-21 12:41:40 +00:00
if ( ! empty ( $exceptions )) {
2004-09-25 15:19:40 +00:00
$exceptions = ' AND u.id NOT IN (' . $exceptions . ') ' ;
}
if ( ! empty ( $sort )) {
$sort = ' ORDER by ' . $sort ;
2004-08-21 12:41:40 +00:00
}
2004-06-01 07:28:14 +00:00
return get_records_sql ( " SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, u.maildigest,
2004-09-21 11:41:58 +00:00
u . email , u . city , u . country , u . lastlogin , u . picture , u . lang , u . timezone ,
2004-03-07 06:32:36 +00:00
u . emailstop , t . authority , t . role , t . editall , t . timeaccess as lastaccess
2004-09-21 11:41:58 +00:00
FROM { $CFG -> prefix } user u ,
2003-08-04 05:48:39 +00:00
{ $CFG -> prefix } user_teachers t
2004-09-21 11:41:58 +00:00
WHERE t . course = '$courseid' AND t . userid = u . id
2004-09-25 15:19:40 +00:00
AND u . deleted = '0' AND u . confirmed = '1' $exceptions $sort " );
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 all the users of a course : students and teachers
*
* @ param int $courseid The course in question .
* @ param string $sort ?
* @ param string $exceptions ?
* @ 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
2004-09-24 21:28:22 +00:00
* @ todo Finish documenting this function
*/
2004-09-22 16:15:23 +00:00
function get_course_users ( $courseid , $sort = 'timeaccess DESC' , $exceptions = '' , $fields = '' ) {
2003-05-20 09:33:30 +00:00
2004-08-21 12:41:40 +00:00
/// Using this method because the single SQL is too inefficient
// Note that this has the effect that teachers and students are
// sorted individually. Returns first all teachers, then all students
2004-01-31 03:30:31 +00:00
2004-08-21 12:41:40 +00:00
if ( ! $teachers = get_course_teachers ( $courseid , $sort , $exceptions )) {
$teachers = array ();
}
2004-09-08 15:46:43 +00:00
if ( ! $students = get_course_students ( $courseid , $sort , '' , 0 , 99999 , '' , '' , NULL , '' , $fields , $exceptions )) {
2004-08-21 12:41:40 +00:00
$students = array ();
}
2002-12-17 04:41:18 +00:00
2004-08-21 12:41:40 +00:00
return $teachers + $students ;
2004-01-31 03:30:31 +00:00
2004-08-21 12:41:40 +00:00
}
2004-01-31 03:30:31 +00:00
2004-08-21 12:41:40 +00:00
/**
2004-09-24 21:28:22 +00:00
* Search through course users
*
* If $coursid specifies the site course then this function searches
* through all undeleted and confirmed users
*
* @ uses $CFG
* @ uses SITEID
* @ param int $courseid The course in question .
* @ param int $groupid The group in question .
* @ param string $searchtext ?
* @ param string $sort ?
* @ param string $exceptions ?
2005-07-12 02:23:58 +00:00
* @ return object
2004-09-24 21:28:22 +00:00
* @ todo Finish documenting this function
*/
2004-08-21 12:41:40 +00:00
function search_users ( $courseid , $groupid , $searchtext , $sort = '' , $exceptions = '' ) {
global $CFG ;
2004-01-31 03:30:31 +00:00
2005-02-26 09:20:31 +00:00
$LIKE = sql_ilike ();
$fullname = 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 )) {
2004-09-22 16:15:23 +00:00
$except = ' AND u.id NOT IN (' . $exceptions . ') ' ;
2004-08-21 12:41:40 +00:00
} else {
$except = '' ;
}
2004-08-30 17:27:00 +00:00
2004-08-21 12:41:40 +00:00
if ( ! empty ( $sort )) {
2004-09-22 16:15:23 +00:00
$order = ' ORDER BY ' . $sort ;
2004-08-21 12:41:40 +00:00
} else {
$order = '' ;
}
2004-09-21 11:41:58 +00:00
2004-09-22 16:15:23 +00:00
$select = 'u.deleted = \'0\' AND u.confirmed = \'1\'' ;
2004-08-30 17:27:00 +00:00
2004-08-29 14:15:40 +00:00
if ( ! $courseid or $courseid == SITEID ) {
2004-08-30 17:27:00 +00:00
return get_records_sql ( " SELECT u.id, u.firstname, u.lastname, u.email
FROM { $CFG -> prefix } user u
WHERE $select
2004-08-21 12:41:40 +00:00
AND ( $fullname $LIKE '%$searchtext%' OR u . email $LIKE '%$searchtext%' )
2004-08-30 17:27:00 +00:00
$except $order " );
2004-09-21 11:41:58 +00:00
} else {
2004-08-30 17:27:00 +00:00
2004-08-21 12:41:40 +00:00
if ( $groupid ) {
return get_records_sql ( " SELECT u.id, u.firstname, u.lastname, u.email
2004-09-21 11:41:58 +00:00
FROM { $CFG -> prefix } user u ,
2004-08-21 12:41:40 +00:00
{ $CFG -> prefix } groups_members g
2004-08-30 17:27:00 +00:00
WHERE $select AND g . groupid = '$groupid' AND g . userid = u . id
2004-08-21 12:41:40 +00:00
AND ( $fullname $LIKE '%$searchtext%' OR u . email $LIKE '%$searchtext%' )
$except $order " );
} else {
if ( ! $teachers = get_records_sql ( " SELECT u.id, u.firstname, u.lastname, u.email
2004-09-21 11:41:58 +00:00
FROM { $CFG -> prefix } user u ,
2004-08-21 12:41:40 +00:00
{ $CFG -> prefix } user_teachers s
2004-08-30 17:27:00 +00:00
WHERE $select AND s . course = '$courseid' AND s . userid = u . id
2004-08-21 12:41:40 +00:00
AND ( $fullname $LIKE '%$searchtext%' OR u . email $LIKE '%$searchtext%' )
$except $order " )) {
$teachers = array ();
}
if ( ! $students = get_records_sql ( " SELECT u.id, u.firstname, u.lastname, u.email
2004-09-21 11:41:58 +00:00
FROM { $CFG -> prefix } user u ,
2004-08-21 12:41:40 +00:00
{ $CFG -> prefix } user_students s
2004-08-30 17:27:00 +00:00
WHERE $select AND s . course = '$courseid' AND s . userid = u . id
2004-08-21 12:41:40 +00:00
AND ( $fullname $LIKE '%$searchtext%' OR u . email $LIKE '%$searchtext%' )
$except $order " )) {
$students = array ();
}
return $teachers + $students ;
}
}
2002-12-17 04:41:18 +00:00
}
2004-08-30 17:27:00 +00:00
2003-09-14 04:04:15 +00:00
/**
2004-09-24 21:28:22 +00:00
* Returns a list of all site users
* Obsolete , just calls get_course_users ( SITEID )
*
* @ uses SITEID
2004-09-25 01:29:37 +00:00
* @ deprecated Use { @ link get_course_users ()} instead .
2004-09-24 21:28:22 +00:00
* @ 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 { @ link $USER } records or false if error .
2004-09-24 21:28:22 +00:00
* @ todo Finish documenting this function . The return type need to be better defined .
*/
2004-09-22 16:15:23 +00:00
function get_site_users ( $sort = 'u.lastaccess DESC' , $fields = '*' , $exceptions = '' ) {
2003-03-29 04:08:51 +00:00
2004-09-08 15:46:43 +00:00
return get_course_users ( SITEID , $sort , $exceptions , $fields );
2003-03-29 04:08:51 +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
* 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
* @ todo Finish documenting this function . The return type needs to be better defined .
*/
2004-09-22 16:15:23 +00:00
function get_users ( $get = true , $search = '' , $confirmed = false , $exceptions = '' , $sort = 'firstname ASC' ,
$firstinitial = '' , $lastinitial = '' , $page = 0 , $recordsperpage = 99999 , $fields = '*' ) {
2003-09-14 04:04:15 +00:00
global $CFG ;
2005-02-26 09:20:31 +00:00
$limit = sql_paging_limit ( $page , $recordsperpage );
$LIKE = sql_ilike ();
$fullname = sql_fullname ();
2003-03-21 10:10:21 +00:00
2004-09-22 16:15:23 +00:00
$select = 'username <> \'guest\' AND deleted = 0' ;
2004-03-20 06:58:52 +00:00
2004-12-22 23:11:27 +00:00
if ( ! empty ( $search )){
$search = trim ( $search );
2004-03-20 06:58:52 +00:00
$select .= " AND ( $fullname $LIKE '% $search %' OR email $LIKE '% $search %') " ;
2003-03-21 10:10:21 +00:00
}
2003-05-14 15:19:04 +00:00
if ( $confirmed ) {
2004-09-22 16:15:23 +00:00
$select .= ' AND confirmed = \'1\' ' ;
2003-05-14 15:19:04 +00:00
}
if ( $exceptions ) {
2004-09-22 16:15:23 +00:00
$select .= ' AND id NOT IN (' . $exceptions . ') ' ;
2003-05-14 15:19:04 +00:00
}
2004-03-20 06:58:52 +00:00
if ( $firstinitial ) {
2004-09-22 16:15:23 +00:00
$select .= ' AND firstname ' . $LIKE . ' \'' . $firstinitial . '%\'' ;
2004-09-21 11:41:58 +00:00
}
2004-03-20 06:58:52 +00:00
if ( $lastinitial ) {
2004-09-22 16:15:23 +00:00
$select .= ' AND lastname ' . $LIKE . ' \'' . $lastinitial . '%\'' ;
2004-09-21 11:41:58 +00:00
}
2004-03-20 06:58:52 +00:00
2003-05-14 15:19:04 +00:00
if ( $sort and $get ) {
2004-09-22 16:15:23 +00:00
$sort = ' ORDER BY ' . $sort . ' ' ;
2003-05-14 15:19:04 +00:00
} else {
2004-09-22 16:15:23 +00:00
$sort = '' ;
2003-05-14 15:19:04 +00:00
}
if ( $get ) {
2004-09-22 16:15:23 +00:00
return get_records_select ( 'user' , $select . ' ' . $sort . ' ' . $limit , '' , $fields );
2003-05-14 15:19:04 +00:00
} else {
2004-09-22 16:15:23 +00:00
return count_records_select ( 'user' , $select . ' ' . $sort . ' ' . $limit );
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
*
* @ uses $CFG
* @ 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
*/
2004-09-22 16:15:23 +00:00
function get_users_listing ( $sort = 'lastaccess' , $dir = 'ASC' , $page = 0 , $recordsperpage = 99999 ,
2004-09-23 02:48:41 +00:00
$search = '' , $firstinitial = '' , $lastinitial = '' ) {
2004-03-20 06:58:52 +00:00
2002-12-20 14:44:14 +00:00
global $CFG ;
2002-12-23 13:48:31 +00:00
2005-02-26 09:20:31 +00:00
$limit = sql_paging_limit ( $page , $recordsperpage );
$LIKE = sql_ilike ();
$fullname = sql_fullname ();
2003-02-18 03:16:07 +00:00
2004-03-20 06:58:52 +00:00
$select = 'deleted <> 1' ;
2004-12-22 23:11:27 +00:00
if ( ! empty ( $search )) {
$search = trim ( $search );
2004-03-20 06:58:52 +00:00
$select .= " AND ( $fullname $LIKE '% $search %' OR email $LIKE '% $search %') " ;
}
if ( $firstinitial ) {
2004-09-22 16:15:23 +00:00
$select .= ' AND firstname ' . $LIKE . ' \'' . $firstinitial . '%\' ' ;
2004-03-20 06:58:52 +00:00
}
if ( $lastinitial ) {
2004-09-22 16:15:23 +00:00
$select .= ' AND lastname ' . $LIKE . ' \'' . $lastinitial . '%\' ' ;
2003-03-21 09:42:42 +00:00
}
2004-03-20 06:58:52 +00:00
if ( $sort ) {
2004-09-22 16:15:23 +00:00
$sort = ' ORDER BY ' . $sort . ' ' . $dir ;
2004-03-20 06:58:52 +00:00
}
/// warning: will return UNCONFIRMED USERS
2004-03-09 21:12:54 +00:00
return get_records_sql ( " SELECT id, username, email, firstname, lastname, city, country, lastaccess, confirmed
2004-09-21 11:41:58 +00:00
FROM { $CFG -> prefix } user
2004-03-20 06:58:52 +00:00
WHERE $select $sort $limit " );
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
*
* @ uses $CFG
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_users_confirmed () {
global $CFG ;
2004-09-21 11:41:58 +00:00
return get_records_sql ( " SELECT *
FROM { $CFG -> prefix } user
WHERE confirmed = 1
2002-12-20 14:44:14 +00:00
AND deleted = 0
2004-09-21 11:41:58 +00:00
AND username <> 'guest'
2002-12-20 14:44:14 +00:00
AND username <> 'changeme' " );
}
2003-09-14 04:04:15 +00:00
/**
2005-07-12 02:23:58 +00:00
* Full list of users that have not yet confirmed their accounts .
2004-09-24 21:28:22 +00:00
*
* @ uses $CFG
* @ param string $cutofftime ?
2005-07-12 02:23:58 +00:00
* @ return object { @ link $USER } records
2004-09-24 21:28:22 +00:00
* @ todo Finish documenting this function
*/
2002-12-23 14:19:45 +00:00
function get_users_unconfirmed ( $cutofftime = 2000000000 ) {
2002-12-20 14:44:14 +00:00
global $CFG ;
2004-09-21 11:41:58 +00:00
return get_records_sql ( " SELECT *
FROM { $CFG -> prefix } user
2002-12-20 14:44:14 +00:00
WHERE confirmed = 0
2004-09-21 11:41:58 +00:00
AND firstaccess > 0
2002-12-20 14:44:14 +00:00
AND firstaccess < '$cutofftime' " );
}
2005-11-08 07:19:27 +00:00
/**
* Full list of bogus accounts that are probably not ever going to be used
*
* @ uses $CFG
* @ param string $cutofftime ?
* @ return object { @ link $USER } records
* @ todo Finish documenting this function
*/
function get_users_not_fully_set_up ( $cutofftime = 2000000000 ) {
global $CFG ;
return get_records_sql ( " SELECT *
FROM { $CFG -> prefix } user
WHERE confirmed = 1
AND lastaccess > 0
AND lastaccess < '$cutofftime'
AND deleted = 0
AND ( lastname = '' OR firstname = '' OR email = '' ) " );
}
2003-09-14 04:04:15 +00:00
/**
2004-09-24 21:28:22 +00:00
* shortdesc ( optional )
*
* longdesc
*
* @ uses $CFG
* @ param string $cutofftime ?
2005-07-12 02:23:58 +00:00
* @ return object { @ link $USER } records
2004-09-24 21:28:22 +00:00
* @ todo Finish documenting this function
*/
2002-12-20 14:44:14 +00:00
function get_users_longtimenosee ( $cutofftime ) {
global $CFG ;
2003-10-27 14:01:29 +00:00
return get_records_sql ( " SELECT DISTINCT *
FROM { $CFG -> prefix } user_students
2004-09-21 11:41:58 +00:00
WHERE timeaccess > '0'
2003-10-27 14:01:29 +00:00
AND timeaccess < '$cutofftime' " );
2002-12-20 14:44:14 +00:00
}
2003-12-30 17:18:06 +00:00
/**
2004-09-24 21:28:22 +00:00
* Returns an array of group objects that the user is a member of
* in the given course . If userid isn ' t specified , then return a
* list of all groups in the course .
*
* @ uses $CFG
2004-09-25 05:29:21 +00:00
* @ param int $courseid The id of the course in question .
2004-09-24 21:28:22 +00:00
* @ param int $userid The id of the user in question as found in the 'user' table 'id' field .
2005-07-12 02:23:58 +00:00
* @ return object
2004-09-24 21:28:22 +00:00
*/
2003-12-30 17:18:06 +00:00
function get_groups ( $courseid , $userid = 0 ) {
global $CFG ;
if ( $userid ) {
2004-09-22 16:15:23 +00:00
$dbselect = ', ' . $CFG -> prefix . 'groups_members m' ;
$userselect = 'AND m.groupid = g.id AND m.userid = \'' . $userid . '\'' ;
2003-12-30 18:19:15 +00:00
} else {
$dbselect = '' ;
$userselect = '' ;
2003-12-30 17:18:06 +00:00
}
return get_records_sql ( " SELECT DISTINCT g.*
2003-12-30 18:19:15 +00:00
FROM { $CFG -> prefix } groups g $dbselect
2003-12-30 17:18:06 +00:00
WHERE g . courseid = '$courseid' $userselect " );
}
/**
2004-09-24 21:28:22 +00:00
* Returns an array of user objects
*
* @ uses $CFG
* @ param int $groupid The group in question .
* @ param string $sort ?
* @ param string $exceptions ?
2005-07-12 02:23:58 +00:00
* @ return object
2004-09-24 21:28:22 +00:00
* @ todo Finish documenting this function
*/
2005-02-19 03:00:07 +00:00
function get_group_users ( $groupid , $sort = 'u.lastaccess DESC' , $exceptions = '' , $fields = 'u.*' ) {
2003-12-30 17:18:06 +00:00
global $CFG ;
2004-08-21 12:41:40 +00:00
if ( ! empty ( $exceptions )) {
2004-09-22 16:15:23 +00:00
$except = ' AND u.id NOT IN (' . $exceptions . ') ' ;
2004-08-21 12:41:40 +00:00
} else {
$except = '' ;
}
2005-08-17 23:17:05 +00:00
// in postgres, you can't have things in sort that aren't in the select, so...
$extrafield = str_replace ( 'ASC' , '' , $sort );
2005-08-24 23:48:12 +00:00
$extrafield = str_replace ( 'DESC' , '' , $extrafield );
2005-08-17 23:17:05 +00:00
$extrafield = trim ( $extrafield );
if ( ! empty ( $extrafield )) {
$extrafield = ',' . $extrafield ;
}
return get_records_sql ( " SELECT DISTINCT $fields $extrafield
2003-12-30 17:18:06 +00:00
FROM { $CFG -> prefix } user u ,
2004-09-21 11:41:58 +00:00
{ $CFG -> prefix } groups_members m
2003-12-30 17:18:06 +00:00
WHERE m . groupid = '$groupid'
2004-08-21 12:41:40 +00:00
AND m . userid = u . id $except
2004-01-10 16:52:33 +00:00
ORDER BY $sort " );
2003-12-30 17:18:06 +00:00
}
/**
2004-09-24 21:28:22 +00:00
* An efficient way of finding all the users who aren ' t in groups yet
*
* Currently unimplemented .
* @ uses $CFG
* @ param int $courseid The course in question .
2005-07-12 02:23:58 +00:00
* @ return object
2004-09-24 21:28:22 +00:00
*/
2003-12-30 17:18:06 +00:00
function get_users_not_in_group ( $courseid ) {
global $CFG ;
return array (); /// XXX TO BE DONE
}
2004-03-13 14:25:27 +00:00
/**
2004-09-24 21:28:22 +00:00
* Returns an array of user objects
*
* @ uses $CFG
2005-11-08 07:19:27 +00:00
* @ param int $groupid The group ( s ) in question .
2005-04-16 12:21:59 +00:00
* @ param string $sort How to sort the results
2005-11-08 07:19:27 +00:00
* @ return object ( changed to groupids )
2004-09-24 21:28:22 +00:00
*/
2005-11-08 07:19:27 +00:00
function get_group_students ( $groupids , $sort = 'u.lastaccess DESC' ) {
2004-03-13 14:25:27 +00:00
global $CFG ;
2005-11-08 07:19:27 +00:00
if ( is_array ( $groupids )){
$groups = $groupids ;
$groupstr = '(m.groupid = ' . array_shift ( $groups );
foreach ( $groups as $index => $value ){
$groupstr .= ' OR m.groupid = ' . $value ;
}
$groupstr .= ')' ;
}
else {
$groupstr = 'm.groupid = ' . $groupids ;
}
2004-03-13 14:25:27 +00:00
return get_records_sql ( " SELECT DISTINCT u.*
FROM { $CFG -> prefix } user u ,
{ $CFG -> prefix } groups_members m ,
{ $CFG -> prefix } groups g ,
{ $CFG -> prefix } user_students s
2005-11-08 07:19:27 +00:00
WHERE $groupstr
2004-09-21 11:41:58 +00:00
AND m . userid = u . id
AND m . groupid = g . id
2004-03-13 14:25:27 +00:00
AND g . courseid = s . course
AND s . userid = u . id
ORDER BY $sort " );
}
2005-04-16 12:21:59 +00:00
/**
* Returns list of all the teachers who can access a group
*
* @ uses $CFG
* @ param int $courseid The course in question .
* @ param int $groupid The group in question .
2005-07-12 02:23:58 +00:00
* @ return object
2005-04-16 12:21:59 +00:00
*/
function get_group_teachers ( $courseid , $groupid ) {
/// Returns a list of all the teachers who can access a group
if ( $teachers = get_course_teachers ( $courseid )) {
foreach ( $teachers as $key => $teacher ) {
if ( $teacher -> editall ) { // These can access anything
continue ;
}
if (( $teacher -> authority > 0 ) and ismember ( $groupid , $teacher -> id )) { // Specific group teachers
continue ;
}
2005-07-10 17:32:03 +00:00
unset ( $teachers [ $key ]);
2005-04-16 12:21:59 +00:00
}
}
return $teachers ;
}
2003-12-30 17:18:06 +00:00
/**
2004-09-24 21:28:22 +00:00
* Returns the user ' s group in a particular course
*
* @ uses $CFG
* @ param int $courseid The course in question .
* @ param int $userid The id of the user as found in the 'user' table .
2005-11-08 07:19:27 +00:00
* @ param int $groupid The id of the group the user is in .
2005-07-12 02:23:58 +00:00
* @ return object
2004-09-24 21:28:22 +00:00
* @ todo Finish documenting this function
*/
2003-12-30 17:18:06 +00:00
function user_group ( $courseid , $userid ) {
global $CFG ;
2005-11-08 07:19:27 +00:00
return get_records_sql ( " SELECT g.*
2003-12-30 18:07:09 +00:00
FROM { $CFG -> prefix } groups g ,
{ $CFG -> prefix } groups_members m
2003-12-30 17:18:06 +00:00
WHERE g . courseid = '$courseid'
AND g . id = m . groupid
2005-11-08 07:19:27 +00:00
AND m . userid = '$userid'
ORDER BY name ASC " );
2003-12-30 17:18:06 +00:00
}
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
* @ todo Finish documenting this function .
2004-09-24 21:28:22 +00:00
*/
2005-03-03 04:41:46 +00:00
function get_site () {
global $SITE ;
if ( ! empty ( $SITE -> id )) { // We already have a global to use, so return that
return $SITE ;
}
2003-08-15 13:59:24 +00:00
2005-03-03 04:41:46 +00:00
if ( $course = get_record ( 'course' , 'category' , 0 )) {
2003-08-15 13:59:24 +00:00
return $course ;
} else {
return false ;
}
}
2003-09-14 04:04:15 +00:00
/**
2004-11-18 02:31:53 +00:00
* Returns list of courses , for whole site , or category
*
* Returns list of courses , for whole site , or category
*
* @ param type description
*
* Important : Using c .* for fields is extremely expensive because
* we are using distinct . You almost _NEVER_ need all the fields
* in such a large SELECT
*/
function get_courses ( $categoryid = " all " , $sort = " c.sortorder ASC " , $fields = " c.* " ) {
2003-08-15 13:59:24 +00:00
2003-08-21 09:33:07 +00:00
global $USER , $CFG ;
2004-11-18 02:31:53 +00:00
$categoryselect = " " ;
if ( $categoryid != " all " && is_numeric ( $categoryid )) {
$categoryselect = " c.category = ' $categoryid ' " ;
}
$teachertable = " " ;
$visiblecourses = " " ;
$sqland = " " ;
if ( ! empty ( $categoryselect )) {
$sqland = " AND " ;
}
if ( ! empty ( $USER -> id )) { // May need to check they are a teacher
if ( ! iscreator ()) {
$visiblecourses = " $sqland ((c.visible > 0) OR t.userid = ' $USER->id ') " ;
$teachertable = " LEFT JOIN { $CFG -> prefix } user_teachers t ON t.course = c.id " ;
}
} else {
$visiblecourses = " $sqland c.visible > 0 " ;
2003-08-21 09:33:07 +00:00
}
2004-11-18 02:31:53 +00:00
if ( $categoryselect or $visiblecourses ) {
$selectsql = " { $CFG -> prefix } course c $teachertable WHERE $categoryselect $visiblecourses " ;
2004-01-08 04:50:43 +00:00
} else {
2004-11-18 02:31:53 +00:00
$selectsql = " { $CFG -> prefix } course c $teachertable " ;
2004-01-08 04:50:43 +00:00
}
2005-08-25 00:03:14 +00:00
$extrafield = str_replace ( 'ASC' , '' , $sort );
$extrafield = str_replace ( 'DESC' , '' , $extrafield );
$extrafield = trim ( $extrafield );
if ( ! empty ( $extrafield )) {
$extrafield = ',' . $extrafield ;
}
return get_records_sql ( " SELECT " . (( ! empty ( $teachertable )) ? " DISTINCT " : " " ) . " $fields $extrafield FROM $selectsql " . (( ! empty ( $sort )) ? " ORDER BY $sort " : " " ));
2003-08-21 14:04:04 +00:00
}
2004-11-18 02:31:53 +00:00
/**
* Returns list of courses , for whole site , or category
*
* Similar to get_courses , but allows paging
*
* @ param type description
*
* Important : Using c .* for fields is extremely expensive because
* we are using distinct . You almost _NEVER_ need all the fields
* in such a large SELECT
*/
function get_courses_page ( $categoryid = " all " , $sort = " c.sortorder ASC " , $fields = " c.* " ,
& $totalcount , $limitfrom = " " , $limitnum = " " ) {
2004-09-23 07:46:41 +00:00
2003-08-21 14:04:04 +00:00
global $USER , $CFG ;
2004-11-18 02:31:53 +00:00
$categoryselect = " " ;
2004-11-22 04:24:30 +00:00
if ( $categoryid != " all " && is_numeric ( $categoryid )) {
2004-11-18 02:31:53 +00:00
$categoryselect = " c.category = ' $categoryid ' " ;
2003-08-21 14:04:04 +00:00
}
2004-11-18 02:31:53 +00:00
$teachertable = " " ;
$visiblecourses = " " ;
$sqland = " " ;
if ( ! empty ( $categoryselect )) {
$sqland = " AND " ;
2004-09-23 07:46:41 +00:00
}
2004-12-15 13:24:19 +00:00
if ( ! empty ( $USER ) and ! empty ( $USER -> id )) { // May need to check they are a teacher
2004-11-18 02:31:53 +00:00
if ( ! iscreator ()) {
$visiblecourses = " $sqland ((c.visible > 0) OR t.userid = ' $USER->id ') " ;
$teachertable = " LEFT JOIN { $CFG -> prefix } user_teachers t ON t.course=c.id " ;
}
2003-08-21 14:04:04 +00:00
} else {
2004-11-18 02:31:53 +00:00
$visiblecourses = " $sqland c.visible > 0 " ;
2003-08-21 14:04:04 +00:00
}
2004-11-18 02:31:53 +00:00
if ( $limitfrom !== " " ) {
2005-02-26 09:20:31 +00:00
$limit = sql_paging_limit ( $limitfrom , $limitnum );
2004-11-18 02:31:53 +00:00
} else {
$limit = " " ;
2003-08-15 13:59:24 +00:00
}
2003-08-21 09:33:07 +00:00
2004-11-18 02:31:53 +00:00
$selectsql = " { $CFG -> prefix } course c $teachertable WHERE $categoryselect $visiblecourses " ;
2003-08-21 09:33:07 +00:00
2004-11-18 02:31:53 +00:00
$totalcount = count_records_sql ( " SELECT COUNT(DISTINCT c.id) FROM $selectsql " );
2003-08-21 09:33:07 +00:00
2004-11-18 02:31:53 +00:00
return get_records_sql ( " SELECT DISTINCT $fields FROM $selectsql " . (( ! empty ( $sort )) ? " ORDER BY $sort " : " " ) . " $limit " );
2003-08-15 13:59:24 +00:00
}
2003-09-14 04:04:15 +00:00
/**
2005-07-12 02:23:58 +00:00
* List of courses that a user is a member of .
2004-09-24 21:28:22 +00:00
*
* @ uses $CFG
2005-07-12 02:23:58 +00:00
* @ param int $userid The user of interest
2004-09-24 21:28:22 +00:00
* @ param string $sort ?
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_my_courses ( $userid , $sort = 'visible DESC,sortorder ASC' ) {
2003-08-21 17:24:40 +00:00
2005-12-13 04:05:04 +00:00
global $CFG , $USER ;
2003-08-15 13:59:24 +00:00
2003-08-21 17:24:40 +00:00
$course = array ();
2004-09-22 16:15:23 +00:00
if ( $students = get_records ( 'user_students' , 'userid' , $userid , '' , 'id, course' )) {
2003-08-21 17:24:40 +00:00
foreach ( $students as $student ) {
$course [ $student -> course ] = $student -> course ;
}
}
2006-01-12 02:55:59 +00:00
if ( count ( $course ) > 0 ) {
if ( $courses = get_records_list ( 'course' , 'id' , implode ( ',' , $course ))) {
foreach ( $courses as $k => $c ) {
if ( empty ( $USER -> admin ) && ( ! $c -> visible || ! course_parent_visible ( $c ))) {
unset ( $course [ $c -> id ]);
}
}
2005-12-13 04:05:04 +00:00
}
}
2004-09-22 16:15:23 +00:00
if ( $teachers = get_records ( 'user_teachers' , 'userid' , $userid , '' , 'id, course' )) {
2003-08-21 17:24:40 +00:00
foreach ( $teachers as $teacher ) {
$course [ $teacher -> course ] = $teacher -> course ;
}
}
if ( empty ( $course )) {
return $course ;
}
$courseids = implode ( ',' , $course );
2004-09-22 16:15:23 +00:00
return get_records_list ( 'course' , 'id' , $courseids , $sort );
2003-08-21 17:24:40 +00:00
// The following is correct but VERY slow with large datasets
//
2004-09-21 11:41:58 +00:00
// return get_records_sql("SELECT c.*
// FROM {$CFG->prefix}course c,
// {$CFG->prefix}user_students s,
// {$CFG->prefix}user_teachers t
2003-08-21 17:24:40 +00:00
// WHERE (s.userid = '$userid' AND s.course = c.id)
// OR (t.userid = '$userid' AND t.course = c.id)
2004-09-21 11:41:58 +00:00
// GROUP BY c.id
2003-08-21 17:24:40 +00:00
// ORDER BY $sort");
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
* @ todo Finish documenting this function
*/
2004-09-22 16:15:23 +00:00
function get_courses_search ( $searchterms , $sort = 'fullname ASC' , $page = 0 , $recordsperpage = 50 , & $totalcount ) {
2003-08-15 13:59:24 +00:00
global $CFG ;
2005-02-26 09:20:31 +00:00
$limit = sql_paging_limit ( $page , $recordsperpage );
2003-08-15 13:59:24 +00:00
2003-09-14 04:04:15 +00:00
//to allow case-insensitive search for postgesql
2004-09-22 16:15:23 +00:00
if ( $CFG -> dbtype == 'postgres7' ) {
$LIKE = 'ILIKE' ;
$NOTLIKE = 'NOT ILIKE' ; // case-insensitive
$REGEXP = '~*' ;
$NOTREGEXP = '!~*' ;
2003-08-15 13:59:24 +00:00
} else {
2004-09-22 16:15:23 +00:00
$LIKE = 'LIKE' ;
$NOTLIKE = 'NOT LIKE' ;
$REGEXP = 'REGEXP' ;
$NOTREGEXP = 'NOT REGEXP' ;
2003-08-15 13:59:24 +00:00
}
2004-09-22 16:15:23 +00:00
$fullnamesearch = '' ;
$summarysearch = '' ;
2003-08-15 13:59:24 +00:00
foreach ( $searchterms as $searchterm ) {
if ( $fullnamesearch ) {
2004-09-22 16:15:23 +00:00
$fullnamesearch .= ' AND ' ;
2003-08-15 13:59:24 +00:00
}
if ( $summarysearch ) {
2004-09-22 16:15:23 +00:00
$summarysearch .= ' AND ' ;
2003-08-15 13:59:24 +00:00
}
2003-08-19 05:32:20 +00:00
2004-09-22 16:15:23 +00:00
if ( substr ( $searchterm , 0 , 1 ) == '+' ) {
2003-08-19 05:32:20 +00:00
$searchterm = substr ( $searchterm , 1 );
$summarysearch .= " summary $REGEXP '(^|[^a-zA-Z0-9]) $searchterm ([^a-zA-Z0-9]| $ )' " ;
$fullnamesearch .= " fullname $REGEXP '(^|[^a-zA-Z0-9]) $searchterm ([^a-zA-Z0-9]| $ )' " ;
} else if ( substr ( $searchterm , 0 , 1 ) == " - " ) {
$searchterm = substr ( $searchterm , 1 );
$summarysearch .= " summary $NOTREGEXP '(^|[^a-zA-Z0-9]) $searchterm ([^a-zA-Z0-9]| $ )' " ;
$fullnamesearch .= " fullname $NOTREGEXP '(^|[^a-zA-Z0-9]) $searchterm ([^a-zA-Z0-9]| $ )' " ;
} else {
2004-09-22 16:15:23 +00:00
$summarysearch .= ' summary ' . $LIKE . '\'%' . $searchterm . '%\' ' ;
$fullnamesearch .= ' fullname ' . $LIKE . '\'%' . $searchterm . '%\' ' ;
2003-08-19 05:32:20 +00:00
}
2003-08-15 13:59:24 +00:00
}
2004-09-22 16:15:23 +00:00
$selectsql = $CFG -> prefix . 'course WHERE (' . $fullnamesearch . ' OR ' . $summarysearch . ') AND category > \'0\'' ;
2003-08-19 05:32:20 +00:00
2004-09-22 16:15:23 +00:00
$totalcount = count_records_sql ( 'SELECT COUNT(*) FROM ' . $selectsql );
2003-08-15 13:59:24 +00:00
2004-09-22 16:15:23 +00:00
$courses = get_records_sql ( 'SELECT * FROM ' . $selectsql . ' ORDER BY ' . $sort . ' ' . $limit );
2003-08-15 13:59:24 +00:00
if ( $courses ) { /// Remove unavailable courses from the list
foreach ( $courses as $key => $course ) {
2005-12-13 04:05:04 +00:00
if ( ! $course -> visible || ! course_parent_visible ( $course )) {
2003-08-15 13:59:24 +00:00
if ( ! isteacher ( $course -> id )) {
unset ( $courses [ $key ]);
2003-08-19 05:32:20 +00:00
$totalcount -- ;
2003-08-15 13:59:24 +00:00
}
}
}
}
return $courses ;
}
2003-09-14 04:04:15 +00:00
/**
2004-09-24 21:28:22 +00:00
* Returns a sorted list of categories
*
* @ param string $parent ?
* @ param string $sort ?
* @ return ?
* @ todo Finish documenting this function
*/
2004-09-22 16:15:23 +00:00
function get_categories ( $parent = 'none' , $sort = 'sortorder ASC' ) {
2003-08-15 13:59:24 +00:00
2005-02-23 01:49:22 +00:00
if ( $parent === 'none' ) {
2004-09-22 16:15:23 +00:00
$categories = get_records ( 'course_categories' , '' , '' , $sort );
2003-08-15 13:59:24 +00:00
} else {
2004-09-22 16:15:23 +00:00
$categories = get_records ( 'course_categories' , 'parent' , $parent , $sort );
2003-08-15 13:59:24 +00:00
}
if ( $categories ) { /// Remove unavailable categories from the list
2003-09-03 08:29:05 +00:00
$creator = iscreator ();
2003-08-15 13:59:24 +00:00
foreach ( $categories as $key => $category ) {
2005-12-13 04:05:04 +00:00
if ( ! $category -> visible || ! category_parent_visible ( $category -> parent )) {
2003-09-03 08:29:05 +00:00
if ( ! $creator ) {
2003-08-15 13:59:24 +00:00
unset ( $categories [ $key ]);
}
}
}
}
return $categories ;
}
2003-09-14 04:04:15 +00:00
/**
2004-11-17 06:57:28 +00:00
* This recursive function makes sure that the courseorder is consecutive
*
* @ param type description
*
* $n is the starting point , offered only for compatilibity -- will be ignored !
* $safe ( bool ) prevents it from assuming category - sortorder is unique , used to upgrade
* safely from 1.4 to 1.5
*/
2005-08-16 23:15:58 +00:00
function fix_course_sortorder ( $categoryid = 0 , $n = 0 , $safe = 0 , $depth = 0 , $path = '' ) {
2004-11-17 06:57:28 +00:00
global $CFG ;
2004-09-21 11:41:58 +00:00
2003-08-15 13:59:24 +00:00
$count = 0 ;
2004-11-17 06:57:28 +00:00
2005-08-16 23:15:58 +00:00
$catgap = 1000 ; // "standard" category gap
$tolerance = 200 ; // how "close" categories can get
if ( $categoryid > 0 ){
// update depth and path
$cat = get_record ( 'course_categories' , 'id' , $categoryid );
if ( $cat -> parent == 0 ) {
$depth = 0 ;
$path = '' ;
} else if ( $depth == 0 ) { // doesn't make sense; get from DB
// this is only called if the $depth parameter looks dodgy
$parent = get_record ( 'course_categories' , 'id' , $cat -> parent );
$path = $parent -> path ;
$depth = $parent -> depth ;
}
$path = $path . '/' . $categoryid ;
$depth = $depth + 1 ;
2004-11-17 06:57:28 +00:00
2005-08-16 23:15:58 +00:00
set_field ( 'course_categories' , 'path' , addslashes ( $path ), 'id' , $categoryid );
set_field ( 'course_categories' , 'depth' , $depth , 'id' , $categoryid );
}
2005-01-13 02:34:45 +00:00
// get some basic info about courses in the category
2004-11-17 06:57:28 +00:00
$info = get_record_sql ( ' SELECT MIN ( sortorder ) AS min ,
MAX ( sortorder ) AS max ,
2005-08-16 23:15:58 +00:00
COUNT ( sortorder ) AS count
2004-11-17 06:57:28 +00:00
FROM ' . $CFG->prefix . ' course
WHERE category = ' . $categoryid );
if ( is_object ( $info )) { // no courses?
$max = $info -> max ;
$count = $info -> count ;
$min = $info -> min ;
unset ( $info );
}
2005-02-23 01:49:22 +00:00
if ( $categoryid > 0 && $n == 0 ) { // only passed category so don't shift it
$n = $min ;
}
2005-01-13 02:34:45 +00:00
// $hasgap flag indicates whether there's a gap in the sequence
$hasgap = false ;
if ( $max - $min + 1 != $count ) {
$hasgap = true ;
}
// $mustshift indicates whether the sequence must be shifted to
// meet its range
$mustshift = false ;
if ( $min < $n + $tolerance || $min > $n + $tolerance + $catgap ) {
$mustshift = true ;
}
2004-11-17 06:57:28 +00:00
// actually sort only if there are courses,
// and we meet one ofthe triggers:
// - safe flag
// - they are not in a continuos block
// - they are too close to the 'bottom'
2005-01-13 02:34:45 +00:00
if ( $count && ( $safe || $hasgap || $mustshift ) ) {
// special, optimized case where all we need is to shift
if ( $mustshift && ! $safe && ! $hasgap ) {
$shift = $n + $catgap - $min ;
// UPDATE course SET sortorder=sortorder+$shift
execute_sql ( " UPDATE { $CFG -> prefix } course
SET sortorder = sortorder + $shift
WHERE category = $categoryid " , 0);
$n = $n + $catgap + $count ;
} else { // do it slowly
$n = $n + $catgap ;
// if the new sequence overlaps the current sequence, lack of transactions
// will stop us -- shift things aside for a moment...
2005-02-01 01:37:14 +00:00
if ( $safe || ( $n >= $min && $n + $count + 1 < $min && $CFG -> dbtype === 'mysql' )) {
2005-02-08 03:42:20 +00:00
$shift = $max + $n + 1000 ;
2005-01-13 02:34:45 +00:00
execute_sql ( " UPDATE { $CFG -> prefix } course
SET sortorder = sortorder + $shift
WHERE category = $categoryid " , 0);
2004-11-17 06:57:28 +00:00
}
2005-01-13 02:34:45 +00:00
$courses = get_courses ( $categoryid , 'c.sortorder ASC' , 'c.id,c.sortorder' );
begin_sql ();
2004-11-17 06:57:28 +00:00
foreach ( $courses as $course ) {
if ( $course -> sortorder != $n ) { // save db traffic
set_field ( 'course' , 'sortorder' , $n , 'id' , $course -> id );
}
$n ++ ;
}
commit_sql ();
}
2003-08-15 13:59:24 +00:00
}
2004-09-22 16:15:23 +00:00
set_field ( 'course_categories' , 'coursecount' , $count , 'id' , $categoryid );
2004-09-21 11:41:58 +00:00
2005-02-23 01:49:22 +00:00
// $n could need updating
$max = get_field_sql ( " SELECT MAX(sortorder) from { $CFG -> prefix } course WHERE category= $categoryid " );
if ( $max > $n ) {
$n = $max ;
}
2005-01-25 05:27:41 +00:00
2004-05-30 00:33:45 +00:00
if ( $categories = get_categories ( $categoryid )) {
foreach ( $categories as $category ) {
2005-08-16 23:15:58 +00:00
$n = fix_course_sortorder ( $category -> id , $n , $safe , $depth , $path );
2004-05-30 00:33:45 +00:00
}
}
2004-09-21 11:41:58 +00:00
2005-01-13 02:34:45 +00:00
return $n + 1 ;
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
* 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 .
*
* @ uses $CFG
*/
2003-08-15 13:59:24 +00:00
function make_default_scale () {
global $CFG ;
$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.
2004-09-22 16:15:23 +00:00
$parentlang = get_string ( 'parentlang' );
if ( is_readable ( $CFG -> dirroot . '/lang/' . $CFG -> lang . '/help/forum/ratings.html' )) {
$file = file ( $CFG -> dirroot . '/lang/' . $CFG -> lang . '/help/forum/ratings.html' );
} else if ( $parentlang and is_readable ( $CFG -> dirroot . '/lang/' . $parentlang . '/help/forum/ratings.html' )) {
$file = file ( $CFG -> dirroot . '/lang/' . $parentlang . '/help/forum/ratings.html' );
} else if ( is_readable ( $CFG -> dirroot . '/lang/en/help/forum/ratings.html' )) {
$file = file ( $CFG -> dirroot . '/lang/en/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
}
2004-09-22 16:15:23 +00:00
$defaultscale -> description = addslashes ( implode ( '' , $file ));
2003-08-15 13:59:24 +00:00
2004-09-22 16:15:23 +00:00
if ( $defaultscale -> id = insert_record ( 'scale' , $defaultscale )) {
execute_sql ( 'UPDATE ' . $CFG -> prefix . 'forum SET scale = \'' . $defaultscale -> id . '\'' , false );
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 ) {
global $CFG ;
2004-09-21 11:41:58 +00:00
$sql = " SELECT id, name FROM { $CFG -> prefix } scale
WHERE courseid = '0' or courseid = '$courseid'
2003-08-15 13:59:24 +00:00
ORDER BY courseid ASC , name ASC " ;
2004-09-22 16:15:23 +00:00
if ( $scales = get_records_sql_menu ( $sql )) {
2003-08-15 13:59:24 +00:00
return $scales ;
}
make_default_scale ();
2004-09-22 16:15:23 +00:00
return get_records_sql_menu ( $sql );
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 ) {
/// Given a set of timezone records, put them in the database
global $CFG ;
/// Clear out all the old stuff
execute_sql ( 'TRUNCATE TABLE ' . $CFG -> prefix . 'timezone' , false );
/// Insert all the new stuff
foreach ( $timezones as $timezone ) {
insert_record ( 'timezone' , $timezone );
}
}
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
*
* @ 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
* @ todo Finish documenting this function
*/
2002-12-20 14:44:14 +00:00
function get_course_mods ( $courseid ) {
global $CFG ;
2003-04-23 16:57:35 +00:00
return get_records_sql ( " SELECT cm.*, m.name as modname
2004-09-21 11:41:58 +00:00
FROM { $CFG -> prefix } modules m ,
2003-04-23 16:57:35 +00:00
{ $CFG -> prefix } course_modules cm
2004-09-21 11:41:58 +00:00
WHERE cm . course = '$courseid'
2002-12-20 14:44:14 +00:00
AND cm . module = m . id " );
}
2004-09-24 21:28:22 +00:00
2003-09-14 04:04:15 +00:00
/**
2004-09-24 21:28:22 +00:00
* Given an instance of a module , finds the coursemodule description
*
* @ uses $CFG
* @ param string $modulename ?
* @ param string $instance ?
* @ 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
* @ todo Finish documenting this function
*/
2004-12-26 13:58:07 +00:00
function get_coursemodule_from_instance ( $modulename , $instance , $courseid = 0 ) {
2002-12-17 04:41:18 +00:00
global $CFG ;
2004-12-26 13:58:07 +00:00
$courseselect = ( $courseid ) ? " cm.course = ' $courseid ' AND " : '' ;
2002-12-17 04:41:18 +00:00
return get_record_sql ( " SELECT cm.*, m.name
2004-09-21 11:41:58 +00:00
FROM { $CFG -> prefix } course_modules cm ,
{ $CFG -> prefix } modules md ,
{ $CFG -> prefix } $modulename m
2004-12-26 13:58:07 +00:00
WHERE $courseselect
2004-09-21 11:41:58 +00:00
cm . instance = m . id AND
md . name = '$modulename' AND
2002-12-17 04:41:18 +00:00
md . id = cm . module AND
m . id = '$instance' " );
}
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 an array of all the active instances of a particular module in a given course , sorted in the order they are defined
*
* Returns an array of all the active instances of a particular
* module in a given course , sorted in the order they are defined
* in the course . Returns false on any errors .
*
* @ uses $CFG
* @ param string $modulename The name of the module to get instances for
* @ param object ( course ) $course This depends on an accurate $course -> modinfo
* @ todo Finish documenting this function . Is a course object to be documented as object ( course ) or array ( course ) since a coures object is really just an associative array , not a php object ?
*/
2003-07-12 05:19:18 +00:00
function get_all_instances_in_course ( $modulename , $course ) {
2002-12-17 04:41:18 +00:00
global $CFG ;
2003-07-12 05:19:18 +00:00
if ( ! $modinfo = unserialize ( $course -> modinfo )) {
return array ();
2003-04-14 15:11:09 +00:00
}
2004-01-26 09:13:47 +00:00
if ( ! $rawmods = get_records_sql ( " SELECT cm.id as coursemodule, m.*,cw.section,cm.visible as visible,cm.groupmode
2004-09-21 11:41:58 +00:00
FROM { $CFG -> prefix } course_modules cm ,
{ $CFG -> prefix } course_sections cw ,
{ $CFG -> prefix } modules md ,
{ $CFG -> prefix } $modulename m
WHERE cm . course = '$course->id' AND
cm . instance = m . id AND
cm . section = cw . id AND
md . name = '$modulename' AND
2003-07-12 05:19:18 +00:00
md . id = cm . module " )) {
return array ();
}
// Hide non-visible instances from students
if ( isteacher ( $course -> id )) {
$invisible = - 1 ;
} else {
$invisible = 0 ;
}
foreach ( $modinfo as $mod ) {
if ( $mod -> mod == $modulename and $mod -> visible > $invisible ) {
2003-08-26 13:47:46 +00:00
$instance = $rawmods [ $mod -> cm ];
if ( ! empty ( $mod -> extra )) {
$instance -> extra = $mod -> extra ;
}
$outputarray [] = $instance ;
2003-07-12 05:19:18 +00:00
}
}
return $outputarray ;
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
* is visible or not
*
* @ uses $CFG
* @ param $moduletype ?
* @ param $module ?
2005-07-12 02:23:58 +00:00
* @ return bool
2004-09-24 21:28:22 +00:00
* @ todo Finish documenting this function
*/
2003-04-25 05:24:29 +00:00
function instance_is_visible ( $moduletype , $module ) {
global $CFG ;
2004-11-18 02:37:52 +00:00
if ( ! empty ( $module -> id )) {
if ( $records = get_records_sql ( " SELECT cm.instance, cm.visible
FROM { $CFG -> prefix } course_modules cm ,
{ $CFG -> prefix } modules m
WHERE cm . course = '$module->course' AND
cm . module = m . id AND
m . name = '$moduletype' AND
cm . instance = '$module->id' " )) {
foreach ( $records as $record ) { // there should only be one - use the first one
return $record -> visible ;
}
2003-04-25 05:24:29 +00:00
}
}
return true ; // visible by default!
}
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 $db
* @ 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
* @ param string $action View , edit , post ( often but not always the same as the file . php )
* @ 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 ) {
2002-12-20 14:44:14 +00:00
2005-04-01 07:40:07 +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 {
if ( isset ( $USER -> realuser )) { // Don't log
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 ();
$info = addslashes ( $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
if ( defined ( 'MDL_PERFDB' )) { global $PERF ; $PERF -> dbqueries ++ ; $PERF -> logwrites ++ ;};
2004-09-22 16:15:23 +00:00
$result = $db -> Execute ( 'INSERT INTO ' . $CFG -> prefix . ' log ( time , userid , course , ip , module , cmid , action , url , info )
VALUES ( ' . "' $timenow ', ' $userid ', ' $courseid ', ' $REMOTE_ADDR ', ' $module ', ' $cm ', ' $action ', ' $url ', ' $info ' ) " );
2002-12-23 09:39:26 +00:00
2003-01-06 13:55:37 +00:00
if ( ! $result and ( $CFG -> debug > 7 )) {
2004-09-22 16:15:23 +00:00
echo '<p>Error: Could not insert a new entry to the Moodle log</p>' ; // Don't throw an error
2004-09-21 11:41:58 +00:00
}
2005-03-23 07:07:47 +00:00
if ( isset ( $USER ) && ( empty ( $user ) || $user == $USER -> id ) ) {
2004-12-28 14:59:29 +00:00
$db -> Execute ( 'UPDATE ' . $CFG -> prefix . 'user SET lastIP=\'' . $REMOTE_ADDR . '\', lastaccess=\'' . $timenow . ' \ '
2005-03-26 18:26:49 +00:00
WHERE id = \ '' . $userid . '\' ' );
2005-01-27 02:29:53 +00:00
if ( $courseid != SITEID && ! empty ( $courseid )) { // logins etc dont't have a courseid and isteacher will break without it.
2005-04-07 00:11:28 +00:00
if ( defined ( 'MDL_PERFDB' )) { global $PERF ; $PERF -> dbqueries ++ ;};
2004-12-28 14:59:29 +00:00
if ( isstudent ( $courseid )) {
$db -> Execute ( 'UPDATE ' . $CFG -> prefix . 'user_students SET timeaccess = \'' . $timenow . '\' ' .
'WHERE course = \'' . $courseid . '\' AND userid = \'' . $userid . '\'' );
} else if ( isteacher ( $courseid , false , false )) {
$db -> Execute ( 'UPDATE ' . $CFG -> prefix . 'user_teachers SET timeaccess = \'' . $timenow . '\' ' .
'WHERE course = \'' . $courseid . '\' AND userid = \'' . $userid . '\'' );
}
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
*
* @ uses $CFG
* @ param string $select SQL select criteria
* @ 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
*/
2004-09-22 16:15:23 +00:00
function get_logs ( $select , $order = 'l.time DESC' , $limitfrom = '' , $limitnum = '' , & $totalcount ) {
2002-12-20 14:44:14 +00:00
global $CFG ;
2004-09-22 16:15:23 +00:00
if ( $limitfrom !== '' ) {
2005-02-26 09:20:31 +00:00
$limit = sql_paging_limit ( $limitfrom , $limitnum );
2003-08-22 06:07:18 +00:00
} else {
2004-09-22 16:15:23 +00:00
$limit = '' ;
2003-08-22 06:07:18 +00:00
}
if ( $order ) {
2004-09-22 16:15:23 +00:00
$order = 'ORDER BY ' . $order ;
2003-08-22 06:07:18 +00:00
}
2004-09-24 21:28:22 +00:00
$selectsql = $CFG -> prefix . 'log l LEFT JOIN ' . $CFG -> prefix . 'user u ON l.userid = u.id ' . (( strlen ( $select ) > 0 ) ? 'WHERE ' . $select : '' );
2004-11-25 21:56:32 +00:00
$countsql = $CFG -> prefix . 'log l ' . (( strlen ( $select ) > 0 ) ? ' WHERE ' . $select : '' );
$totalcount = count_records_sql ( " SELECT COUNT(*) FROM $countsql " );
2003-08-22 06:07:18 +00:00
2004-09-22 16:15:23 +00:00
return get_records_sql ( ' SELECT l .* , u . firstname , u . lastname , u . picture
FROM '. $selectsql .' '. $order .' ' . $limit );
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 ) {
global $CFG ;
2003-07-25 13:23:28 +00:00
if ( $courseid ) {
2004-09-22 16:15:23 +00:00
$courseselect = ' AND course = \'' . $courseid . '\' ' ;
2004-08-30 17:27:00 +00:00
} else {
$courseselect = '' ;
2003-07-25 13:23:28 +00:00
}
2004-11-23 02:05:47 +00:00
return get_records_sql ( " SELECT floor((time - $coursestart )/ " . DAYSECS . " ) as day, count(*) as num
2004-09-21 11:41:58 +00:00
FROM { $CFG -> prefix } log
WHERE userid = '$userid'
2004-11-23 02:05:47 +00:00
AND time > '$coursestart' $courseselect
2002-12-20 14:44:14 +00:00
GROUP BY day " );
}
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 ) {
global $CFG ;
2003-07-24 01:54:06 +00:00
if ( $courseid ) {
2004-09-22 16:15:23 +00:00
$courseselect = ' AND course = \'' . $courseid . '\' ' ;
2004-08-30 17:27:00 +00:00
} else {
$courseselect = '' ;
2003-07-24 01:54:06 +00:00
}
2004-11-23 02:05:47 +00:00
return get_records_sql ( " SELECT floor((time - $daystart )/ " . HOURSECS . " ) as hour, count(*) as num
2002-12-20 14:44:14 +00:00
FROM { $CFG -> prefix } log
2004-09-21 11:41:58 +00:00
WHERE userid = '$userid'
2004-11-23 02:05:47 +00:00
AND time > '$daystart' $courseselect
2002-12-20 14:44:14 +00:00
GROUP BY hour " );
}
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 ) {
2004-09-22 16:15:23 +00:00
$select = 'module=\'login\' AND action=\'error\' AND time > ' . $lastlogin ;
2004-07-25 13:47:38 +00:00
if ( isadmin ()) { // Return information about all accounts
if ( $count -> attempts = count_records_select ( 'log' , $select )) {
$count -> accounts = count_records_select ( 'log' , $select , 'COUNT(DISTINCT info)' );
return $count ;
}
2005-01-23 21:38:01 +00:00
} else if ( $mode == 'everybody' or ( $mode == 'teacher' and isteacherinanycourse ())) {
2004-09-22 16:15:23 +00:00
if ( $count -> attempts = count_records_select ( 'log' , $select . ' AND info = \'' . $username . '\'' )) {
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
* @ todo add example usage and example output
*/
2003-01-03 15:31:30 +00:00
function print_object ( $object ) {
2004-09-22 16:15:23 +00:00
echo '<pre>' ;
2003-02-24 09:36:15 +00:00
print_r ( $object );
2004-09-22 16:15:23 +00:00
echo '</pre>' ;
2003-01-03 15:31:30 +00:00
}
2005-02-26 09:20:31 +00:00
/**
* Returns the proper SQL to do paging
*
2005-07-12 02:23:58 +00:00
* @ uses $CFG
* @ param string $page Offset page number
* @ param string $recordsperpage Number of records per page
* @ return string
2005-02-26 09:20:31 +00:00
*/
function sql_paging_limit ( $page , $recordsperpage ) {
global $CFG ;
switch ( $CFG -> dbtype ) {
case 'postgres7' :
return 'LIMIT ' . $recordsperpage . ' OFFSET ' . $page ;
default :
return 'LIMIT ' . $page . ',' . $recordsperpage ;
}
}
/**
* Returns the proper SQL to do LIKE in a case - insensitive way
*
2005-07-12 02:23:58 +00:00
* @ uses $CFG
* @ return string
2005-02-26 09:20:31 +00:00
*/
function sql_ilike () {
global $CFG ;
2003-01-03 15:31:30 +00:00
2005-02-26 09:20:31 +00:00
switch ( $CFG -> dbtype ) {
case 'mysql' :
return 'LIKE' ;
default :
return 'ILIKE' ;
}
}
/**
* Returns the proper SQL to do LIKE in a case - insensitive way
*
2005-07-12 02:23:58 +00:00
* @ uses $CFG
* @ param string $firstname User ' s first name
* @ param string $lastname User ' s last name
* @ return string
2005-02-26 09:20:31 +00:00
*/
2005-07-12 02:23:58 +00:00
function sql_fullname ( $firstname = 'firstname' , $lastname = 'lastname' ) {
2005-02-26 09:20:31 +00:00
global $CFG ;
switch ( $CFG -> dbtype ) {
case 'mysql' :
2005-07-12 02:23:58 +00:00
return ' CONCAT(' . $firstname . '," ",' . $lastname . ') ' ;
2005-02-26 09:20:31 +00:00
case 'postgres7' :
2005-07-12 02:23:58 +00:00
return " " . $firstname . " ||' '|| " . $lastname . " " ;
2005-02-26 09:20:31 +00:00
default :
2005-07-12 02:23:58 +00:00
return ' ' . $firstname . '||" "||' . $lastname . ' ' ;
2005-02-26 09:20:31 +00:00
}
}
2002-12-20 14:44:14 +00:00
2005-03-07 16:58:00 +00:00
/**
* Returns the proper SQL to do IS NULL
2005-07-12 02:23:58 +00:00
* @ uses $CFG
* @ param string $fieldname The field to add IS NULL to
* @ return string
2005-03-07 16:58:00 +00:00
*/
function sql_isnull ( $fieldname ) {
global $CFG ;
switch ( $CFG -> dbtype ) {
case 'mysql' :
return $fieldname . ' IS NULL' ;
default :
return $fieldname . ' IS NULL' ;
}
}
2006-01-16 06:07:57 +00:00
/**
* Prepare a SQL WHERE clause to select records where the given fields match the given values .
*
* Prepares a where clause of the form
* WHERE field1 = value1 AND field2 = value2 AND field3 = value3
* except that you need only specify as many arguments ( zero to three ) as you need .
*
* @ param string $field1 the first field to check ( optional ) .
* @ param string $value1 the value field1 must have ( requred if field1 is given , else optional ) .
* @ param string $field2 the second field to check ( optional ) .
* @ param string $value2 the value field2 must have ( requred if field2 is given , else optional ) .
* @ param string $field3 the third field to check ( optional ) .
* @ param string $value3 the value field3 must have ( requred if field3 is given , else optional ) .
*/
function where_clause ( $field1 = '' , $value1 = '' , $field2 = '' , $value2 = '' , $field3 = '' , $value3 = '' ) {
if ( $field1 ) {
$select = " WHERE $field1 = ' $value1 ' " ;
if ( $field2 ) {
$select .= " AND $field2 = ' $value2 ' " ;
if ( $field3 ) {
$select .= " AND $field3 = ' $value3 ' " ;
}
}
} else {
$select = '' ;
}
return $select ;
}
2005-09-01 00:28:45 +00:00
/**
* Checks for pg or mysql > 4
*/
function check_db_compat () {
global $CFG , $db ;
if ( $CFG -> dbtype == 'postgres7' ) {
return true ;
}
if ( ! $rs = $db -> Execute ( " SELECT version(); " )) {
return false ;
}
if ( intval ( $rs -> fields [ 0 ]) <= 3 ) {
return false ;
}
return true ;
}
2005-12-13 19:31:48 +00:00
function course_parent_visible ( $course = null ) {
return category_parent_visible ( $course -> category );
}
function category_parent_visible ( $parent = 0 ) {
if ( ! $parent ) {
return true ;
}
$category = get_record ( 'course_categories' , 'id' , $parent );
$list = explode ( '/' , preg_replace ( '/^\/(.*)$/' , '$1' , $category -> path ));
$list [] = $parent ;
$parents = get_records_list ( 'course_categories' , 'id' , implode ( ',' , $list ), 'depth DESC' );
foreach ( $parents as $parent ) {
if ( ! $parent -> visible ) {
return false ;
}
}
return true ;
}
2003-03-14 07:55:22 +00:00
// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
2004-09-25 15:19:40 +00:00
?>