2002-12-17 04:41:18 +00:00
|
|
|
<?PHP // $Id$
|
|
|
|
|
2004-08-29 14:15:40 +00:00
|
|
|
/// CONSTANTS /////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
define('SITEID', 1);
|
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* execute a given sql command string
|
|
|
|
*
|
|
|
|
* Completely general function - it just runs some SQL and reports success.
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
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.
|
|
|
|
|
|
|
|
global $db;
|
|
|
|
|
|
|
|
$result = $db->Execute("$command");
|
|
|
|
|
|
|
|
if ($result) {
|
|
|
|
if ($feedback) {
|
|
|
|
echo "<P><FONT COLOR=green><B>".get_string("success")."</B></FONT></P>";
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if ($feedback) {
|
|
|
|
echo "<P><FONT COLOR=red><B>".get_string("error")."</B></FONT></P>";
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2003-09-14 04:04:15 +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.
|
|
|
|
* Lines that are blank or that start with "#" are ignored.
|
|
|
|
* Only tested with mysql dump files (mysqldump -p -d moodle)
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-17 04:41:18 +00:00
|
|
|
|
2003-02-24 09:36:15 +00:00
|
|
|
function modify_database($sqlfile="", $sqlstring="") {
|
2002-12-17 04:41:18 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2003-02-24 09:36:15 +00:00
|
|
|
$success = true; // Let's be optimistic :-)
|
|
|
|
|
|
|
|
if (!empty($sqlfile)) {
|
|
|
|
if (!is_readable($sqlfile)) {
|
|
|
|
$success = false;
|
|
|
|
echo "<P>Tried to modify database, but \"$sqlfile\" doesn't exist!</P>";
|
|
|
|
return $success;
|
|
|
|
} else {
|
|
|
|
$lines = file($sqlfile);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$lines[] = $sqlstring;
|
|
|
|
}
|
|
|
|
|
|
|
|
$command = "";
|
|
|
|
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$line = rtrim($line);
|
|
|
|
$length = strlen($line);
|
|
|
|
|
|
|
|
if ($length and $line[0] <> "#") {
|
|
|
|
if (substr($line, $length-1, 1) == ";") {
|
|
|
|
$line = substr($line, 0, $length-1); // strip ;
|
|
|
|
$command .= $line;
|
|
|
|
$command = str_replace("prefix_", $CFG->prefix, $command); // Table prefixes
|
|
|
|
if (! execute_sql($command)) {
|
|
|
|
$success = false;
|
2002-12-17 04:41:18 +00:00
|
|
|
}
|
2003-02-24 09:36:15 +00:00
|
|
|
$command = "";
|
|
|
|
} 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
|
|
|
/**
|
|
|
|
* Add a new field to a table, or modify an existing one (if oldfield is defined).
|
|
|
|
*
|
|
|
|
* Add a new field to a table, or modify an existing one (if oldfield is defined).
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
|
|
|
|
2003-01-24 07:51:55 +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)) {
|
|
|
|
|
|
|
|
case "mysql":
|
|
|
|
case "mysqlt":
|
|
|
|
|
|
|
|
switch (strtolower($type)) {
|
2003-07-30 13:02:45 +00:00
|
|
|
case "text":
|
|
|
|
$type = "TEXT";
|
2003-08-10 14:38:47 +00:00
|
|
|
$signed = "";
|
2003-07-30 13:02:45 +00:00
|
|
|
break;
|
2003-01-03 15:31:30 +00:00
|
|
|
case "integer":
|
2003-01-24 07:51:55 +00:00
|
|
|
$type = "INTEGER($size)";
|
2003-01-03 15:31:30 +00:00
|
|
|
break;
|
2003-01-24 07:51:55 +00:00
|
|
|
case "varchar":
|
|
|
|
$type = "VARCHAR($size)";
|
2003-08-10 14:38:47 +00:00
|
|
|
$signed = "";
|
2003-01-03 15:31:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($oldfield)) {
|
|
|
|
$operation = "CHANGE $oldfield $field";
|
|
|
|
} else {
|
|
|
|
$operation = "ADD $field";
|
|
|
|
}
|
|
|
|
|
|
|
|
$default = "DEFAULT '$default'";
|
|
|
|
|
|
|
|
if (!empty($after)) {
|
2003-03-03 17:46:53 +00:00
|
|
|
$after = "AFTER `$after`";
|
2003-01-03 15:31:30 +00:00
|
|
|
}
|
|
|
|
|
2003-08-18 05:10:35 +00:00
|
|
|
return execute_sql("ALTER TABLE {$CFG->prefix}$table $operation $type $signed $default $null $after");
|
2003-01-03 15:31:30 +00:00
|
|
|
break;
|
|
|
|
|
2003-02-18 07:44:26 +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);
|
2003-05-04 16:03:45 +00:00
|
|
|
|
2003-02-18 07:44:26 +00:00
|
|
|
//to prevent conflicts with reserved words
|
2004-08-09 16:00:10 +00:00
|
|
|
$realfield = "\"$field\"";
|
|
|
|
$field = "\"${field}_alter_column_tmp\"";
|
2003-02-18 07:44:26 +00:00
|
|
|
$oldfield = "\"$oldfield\"";
|
|
|
|
|
|
|
|
switch (strtolower($type)) {
|
|
|
|
case "integer":
|
2004-08-03 09:30:04 +00:00
|
|
|
if ($size <= 4) {
|
2003-02-18 07:44:26 +00:00
|
|
|
$type = "INT2";
|
|
|
|
}
|
2004-08-03 09:30:04 +00:00
|
|
|
if ($size <= 10) {
|
2003-02-18 07:44:26 +00:00
|
|
|
$type = "INT";
|
|
|
|
}
|
2004-08-03 09:30:04 +00:00
|
|
|
if ($size > 10) {
|
2003-02-18 07:44:26 +00:00
|
|
|
$type = "INT8";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "varchar":
|
|
|
|
$type = "VARCHAR($size)";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-05-04 16:03:45 +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
|
|
|
|
execute_sql("BEGIN");
|
|
|
|
|
|
|
|
//Allways use temporaly column
|
|
|
|
execute_sql("ALTER TABLE {$CFG->prefix}$table ADD COLUMN $field $type");
|
|
|
|
//Add default values
|
|
|
|
execute_sql("UPDATE {$CFG->prefix}$table SET $field=$default");
|
|
|
|
|
2003-02-18 07:44:26 +00:00
|
|
|
|
2003-05-04 16:03:45 +00:00
|
|
|
if ($dbver >= "7.3") {
|
|
|
|
// modifying 'not null' is posible before 7.3
|
|
|
|
//update default values to table
|
|
|
|
if ($null == "NOT NULL") {
|
|
|
|
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");
|
|
|
|
} else {
|
|
|
|
execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field DROP NOT NULL");
|
|
|
|
}
|
2003-02-18 07:44:26 +00:00
|
|
|
}
|
2003-05-04 16:03:45 +00:00
|
|
|
|
2004-08-09 16:00:10 +00:00
|
|
|
execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field SET DEFAULT $default");
|
|
|
|
|
|
|
|
if ( $oldfield != "\"\"" ) {
|
|
|
|
execute_sql("UPDATE {$CFG->prefix}$table SET $field = $oldfield");
|
|
|
|
execute_sql("ALTER TABLE {$CFG->prefix}$table drop column $oldfield");
|
|
|
|
}
|
2003-02-18 07:44:26 +00:00
|
|
|
|
2004-08-09 16:00:10 +00:00
|
|
|
execute_sql("ALTER TABLE {$CFG->prefix}$table RENAME COLUMN $field TO $realfield");
|
|
|
|
|
|
|
|
return execute_sql("COMMIT");
|
2003-02-18 07:44:26 +00:00
|
|
|
break;
|
2003-01-03 15:31:30 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
switch (strtolower($type)) {
|
|
|
|
case "integer":
|
|
|
|
$type = "INTEGER";
|
|
|
|
break;
|
2003-01-24 07:51:55 +00:00
|
|
|
case "varchar":
|
|
|
|
$type = "VARCHAR";
|
|
|
|
break;
|
2003-01-03 15:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$default = "DEFAULT '$default'";
|
|
|
|
|
|
|
|
if (!empty($after)) {
|
2003-03-03 17:46:53 +00:00
|
|
|
$after = "AFTER $after";
|
2003-01-03 15:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($oldfield)) {
|
|
|
|
execute_sql("ALTER TABLE {$CFG->prefix}$table RENAME COLUMN $oldfield $field");
|
|
|
|
} else {
|
|
|
|
execute_sql("ALTER TABLE {$CFG->prefix}$table ADD COLUMN $field $type");
|
|
|
|
}
|
|
|
|
|
|
|
|
execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field SET $null");
|
2003-08-18 05:10:35 +00:00
|
|
|
return execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field SET $default");
|
2003-01-03 15:31:30 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// GENERIC FUNCTIONS TO CHECK AND COUNT RECORDS ////////////////////////////////////////
|
2002-12-17 04:41:18 +00:00
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Returns true or false depending on whether the specified record exists
|
|
|
|
*
|
|
|
|
* Returns true or false depending on whether the specified record exists
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-29 04:02:42 +00:00
|
|
|
function record_exists($table, $field1="", $value1="", $field2="", $value2="", $field3="", $value3="") {
|
2002-12-17 04:41:18 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2002-12-29 04:02:42 +00:00
|
|
|
if ($field1) {
|
|
|
|
$select = "WHERE $field1 = '$value1'";
|
2002-12-20 14:44:14 +00:00
|
|
|
if ($field2) {
|
2002-12-17 04:41:18 +00:00
|
|
|
$select .= " AND $field2 = '$value2'";
|
2002-12-20 14:44:14 +00:00
|
|
|
if ($field3) {
|
2002-12-17 04:41:18 +00:00
|
|
|
$select .= " AND $field3 = '$value3'";
|
|
|
|
}
|
|
|
|
}
|
2002-12-29 04:02:42 +00:00
|
|
|
} else {
|
|
|
|
$select = "";
|
2002-12-17 04:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return record_exists_sql("SELECT * FROM $CFG->prefix$table $select LIMIT 1");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Returns true or false depending on whether the specified record exists
|
|
|
|
*
|
|
|
|
* The sql statement is provided as a string.
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-17 04:41:18 +00:00
|
|
|
function record_exists_sql($sql) {
|
|
|
|
|
2004-03-13 16:04:52 +00:00
|
|
|
global $CFG, $db;
|
2002-12-17 04:41:18 +00:00
|
|
|
|
2004-03-09 16:32:24 +00:00
|
|
|
if (!$rs = $db->Execute($sql)) {
|
2004-03-10 07:59:32 +00:00
|
|
|
if (isset($CFG->debug) and $CFG->debug > 7) {
|
2004-03-09 16:32:24 +00:00
|
|
|
notify($db->ErrorMsg()."<br /><br />$sql");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2002-12-17 04:41:18 +00:00
|
|
|
|
|
|
|
if ( $rs->RecordCount() ) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get all the records and count them
|
|
|
|
*
|
|
|
|
* Get all the records and count them
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-29 04:02:42 +00:00
|
|
|
function count_records($table, $field1="", $value1="", $field2="", $value2="", $field3="", $value3="") {
|
2002-12-17 04:41:18 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2002-12-29 04:02:42 +00:00
|
|
|
if ($field1) {
|
|
|
|
$select = "WHERE $field1 = '$value1'";
|
2002-12-20 14:44:14 +00:00
|
|
|
if ($field2) {
|
2002-12-17 04:41:18 +00:00
|
|
|
$select .= " AND $field2 = '$value2'";
|
2002-12-20 14:44:14 +00:00
|
|
|
if ($field3) {
|
2002-12-17 04:41:18 +00:00
|
|
|
$select .= " AND $field3 = '$value3'";
|
|
|
|
}
|
|
|
|
}
|
2002-12-29 04:02:42 +00:00
|
|
|
} else {
|
|
|
|
$select = "";
|
2002-12-17 04:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return count_records_sql("SELECT COUNT(*) FROM $CFG->prefix$table $select");
|
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get all the records and count them
|
|
|
|
*
|
|
|
|
* Get all the records and count them
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*
|
|
|
|
*/
|
2004-07-25 13:47:38 +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) {
|
|
|
|
$select = "WHERE $select";
|
|
|
|
}
|
|
|
|
|
2004-07-25 13:47:38 +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
|
|
|
/**
|
|
|
|
* Get all the records and count them
|
|
|
|
*
|
|
|
|
* The sql statement is provided as a string.
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-17 04:41:18 +00:00
|
|
|
function count_records_sql($sql) {
|
|
|
|
|
2004-03-13 16:04:52 +00:00
|
|
|
global $CFG, $db;
|
2002-12-17 04:41:18 +00:00
|
|
|
|
|
|
|
$rs = $db->Execute("$sql");
|
2004-03-09 16:32:24 +00:00
|
|
|
if (!$rs) {
|
2004-03-10 07:59:32 +00:00
|
|
|
if (isset($CFG->debug) and $CFG->debug > 7) {
|
2004-03-09 16:32:24 +00:00
|
|
|
notify($db->ErrorMsg()."<br /><br />$sql");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2002-12-17 04:41:18 +00:00
|
|
|
|
|
|
|
return $rs->fields[0];
|
|
|
|
}
|
|
|
|
|
2003-01-03 15:31:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// GENERIC FUNCTIONS TO GET, INSERT, OR UPDATE DATA ///////////////////////////////////
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get a single record as an object
|
|
|
|
*
|
|
|
|
* Get a single record as an object
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param string $table the name of the table to select from
|
|
|
|
* @param string $field1 the name of the field for the first criteria
|
|
|
|
* @param string $value1 the value of the field for the first criteria
|
|
|
|
* @param string $field2 the name of the field for the second criteria
|
|
|
|
* @param string $value2 the value of the field for the second criteria
|
|
|
|
* @param string $field3 the name of the field for the third criteria
|
|
|
|
* @param string $value3 the value of the field for the third criteria
|
|
|
|
* @return object(fieldset) a fieldset object containing the first record selected
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-29 04:02:42 +00:00
|
|
|
function get_record($table, $field1, $value1, $field2="", $value2="", $field3="", $value3="") {
|
2004-05-30 00:33:45 +00:00
|
|
|
|
2002-12-17 04:41:18 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2002-12-29 04:02:42 +00:00
|
|
|
$select = "WHERE $field1 = '$value1'";
|
2002-12-17 04:41:18 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
if ($field2) {
|
2002-12-17 04:41:18 +00:00
|
|
|
$select .= " AND $field2 = '$value2'";
|
2002-12-20 14:44:14 +00:00
|
|
|
if ($field3) {
|
2002-12-17 04:41:18 +00:00
|
|
|
$select .= " AND $field3 = '$value3'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_record_sql("SELECT * FROM $CFG->prefix$table $select");
|
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get a single record as an object
|
|
|
|
*
|
|
|
|
* The sql statement is provided as a string.
|
|
|
|
* A LIMIT is normally added to only look for 1 record
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-17 04:41:18 +00:00
|
|
|
function get_record_sql($sql) {
|
|
|
|
|
2003-07-23 04:33:40 +00:00
|
|
|
global $db, $CFG;
|
2002-12-17 04:41:18 +00:00
|
|
|
|
2004-03-10 07:59:32 +00:00
|
|
|
if (isset($CFG->debug) and $CFG->debug > 7) { // Debugging mode - don't use limit
|
2003-07-23 04:33:40 +00:00
|
|
|
$limit = "";
|
|
|
|
} else {
|
|
|
|
$limit = " LIMIT 1"; // Workaround - limit to one record
|
|
|
|
}
|
|
|
|
|
2003-07-23 06:30:26 +00:00
|
|
|
if (!$rs = $db->Execute("$sql$limit")) {
|
2004-03-10 07:59:32 +00:00
|
|
|
if (isset($CFG->debug) and $CFG->debug > 7) { // Debugging mode - print checks
|
2004-03-09 16:32:24 +00:00
|
|
|
notify( $db->ErrorMsg() . "<br /><br />$sql$limit" );
|
2003-07-23 06:30:26 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2003-07-23 04:33:40 +00:00
|
|
|
|
2003-07-23 06:30:26 +00:00
|
|
|
if (!$recordcount = $rs->RecordCount()) {
|
|
|
|
return false; // Found no records
|
2003-07-23 04:33:40 +00:00
|
|
|
}
|
2002-12-17 04:41:18 +00:00
|
|
|
|
2003-07-23 06:30:26 +00:00
|
|
|
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
|
|
|
|
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)) {
|
2003-07-23 06:30:26 +00:00
|
|
|
notify("Found more than one record in get_record_sql !");
|
2003-07-23 04:33:40 +00:00
|
|
|
print_object($records);
|
|
|
|
} else {
|
2003-07-23 06:30:26 +00:00
|
|
|
notify("Very strange error in get_record_sql !");
|
|
|
|
print_object($rs);
|
2003-07-23 04:33:40 +00:00
|
|
|
}
|
2003-07-23 06:30:26 +00:00
|
|
|
print_continue("$CFG->wwwroot/admin/config.php");
|
2002-12-17 04:41:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Gets one record from a table, as an object
|
|
|
|
*
|
|
|
|
* "select" is a fragment of SQL to define the selection criteria
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-29 16:28:09 +00:00
|
|
|
function get_record_select($table, $select="", $fields="*") {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if ($select) {
|
|
|
|
$select = "WHERE $select";
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_record_sql("SELECT $fields FROM $CFG->prefix$table $select");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get a number of records as an array of objects
|
|
|
|
*
|
|
|
|
* Can optionally be sorted eg "time ASC" or "time DESC"
|
|
|
|
* If "fields" is specified, only those fields are returned
|
|
|
|
* The "key" is the first column returned, eg usually "id"
|
|
|
|
* limitfrom and limitnum must both be specified or not at all
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2003-07-31 12:18:22 +00:00
|
|
|
function get_records($table, $field="", $value="", $sort="", $fields="*", $limitfrom="", $limitnum="") {
|
2002-12-17 04:41:18 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
if ($field) {
|
2002-12-17 04:41:18 +00:00
|
|
|
$select = "WHERE $field = '$value'";
|
2002-12-29 04:02:42 +00:00
|
|
|
} else {
|
|
|
|
$select = "";
|
2002-12-17 04:41:18 +00:00
|
|
|
}
|
2002-12-29 04:02:42 +00:00
|
|
|
|
2003-08-03 21:49:19 +00:00
|
|
|
if ($limitfrom !== "") {
|
2003-07-31 12:18:22 +00:00
|
|
|
switch ($CFG->dbtype) {
|
|
|
|
case "mysql":
|
|
|
|
$limit = "LIMIT $limitfrom,$limitnum";
|
|
|
|
break;
|
|
|
|
case "postgres7":
|
|
|
|
$limit = "LIMIT $limitnum OFFSET $limitfrom";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$limit = "LIMIT $limitnum,$limitfrom";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$limit = "";
|
|
|
|
}
|
|
|
|
|
2002-12-17 04:41:18 +00:00
|
|
|
if ($sort) {
|
2002-12-29 04:02:42 +00:00
|
|
|
$sort = "ORDER BY $sort";
|
2002-12-17 04:41:18 +00:00
|
|
|
}
|
|
|
|
|
2003-07-31 12:18:22 +00:00
|
|
|
return get_records_sql("SELECT $fields FROM $CFG->prefix$table $select $sort $limit");
|
2002-12-17 04:41:18 +00:00
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get a number of records as an array of objects
|
|
|
|
*
|
|
|
|
* Can optionally be sorted eg "time ASC" or "time DESC"
|
|
|
|
* "select" is a fragment of SQL to define the selection criteria
|
|
|
|
* The "key" is the first column returned, eg usually "id"
|
2004-02-13 16:32:02 +00:00
|
|
|
* limitfrom and limitnum must both be specified or not at all
|
2003-09-14 04:04:15 +00:00
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2004-02-13 16:32:02 +00:00
|
|
|
function get_records_select($table, $select="", $sort="", $fields="*", $limitfrom="", $limitnum="") {
|
2002-12-20 14:44:14 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2002-12-22 06:15:44 +00:00
|
|
|
if ($select) {
|
|
|
|
$select = "WHERE $select";
|
2002-12-29 04:02:42 +00:00
|
|
|
}
|
|
|
|
|
2004-02-13 16:32:02 +00:00
|
|
|
if ($limitfrom !== "") {
|
|
|
|
switch ($CFG->dbtype) {
|
|
|
|
case "mysql":
|
|
|
|
$limit = "LIMIT $limitfrom,$limitnum";
|
|
|
|
break;
|
|
|
|
case "postgres7":
|
|
|
|
$limit = "LIMIT $limitnum OFFSET $limitfrom";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$limit = "LIMIT $limitnum,$limitfrom";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$limit = "";
|
|
|
|
}
|
|
|
|
|
2002-12-29 04:02:42 +00:00
|
|
|
if ($sort) {
|
|
|
|
$sort = "ORDER BY $sort";
|
2002-12-22 06:15:44 +00:00
|
|
|
}
|
|
|
|
|
2004-02-13 16:32:02 +00:00
|
|
|
return get_records_sql("SELECT $fields FROM $CFG->prefix$table $select $sort $limit");
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
2002-12-17 04:41:18 +00:00
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get a number of records as an array of objects
|
|
|
|
*
|
|
|
|
* Differs from get_records() in that the values variable
|
|
|
|
* can be a comma-separated list of values eg "4,5,6,10"
|
|
|
|
* Can optionally be sorted eg "time ASC" or "time DESC"
|
|
|
|
* The "key" is the first column returned, eg usually "id"
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-17 04:41:18 +00:00
|
|
|
function get_records_list($table, $field="", $values="", $sort="", $fields="*") {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
if ($field) {
|
2002-12-17 04:41:18 +00:00
|
|
|
$select = "WHERE $field in ($values)";
|
2002-12-29 04:02:42 +00:00
|
|
|
} else {
|
|
|
|
$select = "";
|
2002-12-17 04:41:18 +00:00
|
|
|
}
|
2002-12-29 04:02:42 +00:00
|
|
|
|
2002-12-17 04:41:18 +00:00
|
|
|
if ($sort) {
|
2002-12-29 04:02:42 +00:00
|
|
|
$sort = "ORDER BY $sort";
|
2002-12-17 04:41:18 +00:00
|
|
|
}
|
|
|
|
|
2002-12-29 04:02:42 +00:00
|
|
|
return get_records_sql("SELECT $fields FROM $CFG->prefix$table $select $sort");
|
2002-12-17 04:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get a number of records as an array of objects
|
|
|
|
*
|
|
|
|
* The "key" is the first column returned, eg usually "id"
|
|
|
|
* The sql statement is provided as a string.
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-17 04:41:18 +00:00
|
|
|
function get_records_sql($sql) {
|
|
|
|
|
2004-03-09 16:32:24 +00:00
|
|
|
global $CFG,$db;
|
2002-12-17 04:41:18 +00:00
|
|
|
|
2004-03-09 16:32:24 +00:00
|
|
|
if (!$rs = $db->Execute($sql)) {
|
2004-03-10 07:59:32 +00:00
|
|
|
if (isset($CFG->debug) and $CFG->debug > 7) {
|
2004-03-09 16:32:24 +00:00
|
|
|
notify($db->ErrorMsg()."<br /><br />$sql");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2004-07-25 13:47:38 +00:00
|
|
|
|
2002-12-17 04:41:18 +00:00
|
|
|
if ( $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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get a number of records as an array of objects
|
|
|
|
*
|
|
|
|
* Can optionally be sorted eg "time ASC" or "time DESC"
|
|
|
|
* If "fields" is specified, only those fields are returned
|
|
|
|
* The "key" is the first column returned, eg usually "id"
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-20 14:44:14 +00:00
|
|
|
function get_records_menu($table, $field="", $value="", $sort="", $fields="*") {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if ($field) {
|
|
|
|
$select = "WHERE $field = '$value'";
|
2002-12-29 04:02:42 +00:00
|
|
|
} else {
|
|
|
|
$select = "";
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
2002-12-29 04:02:42 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
if ($sort) {
|
2002-12-29 04:02:42 +00:00
|
|
|
$sort = "ORDER BY $sort";
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
2002-12-29 04:02:42 +00:00
|
|
|
return get_records_sql_menu("SELECT $fields FROM $CFG->prefix$table $select $sort");
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get a number of records as an array of objects
|
|
|
|
*
|
|
|
|
* Can optionally be sorted eg "time ASC" or "time DESC"
|
|
|
|
* "select" is a fragment of SQL to define the selection criteria
|
|
|
|
* Returns associative array of first two fields
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-20 14:44:14 +00:00
|
|
|
function get_records_select_menu($table, $select="", $sort="", $fields="*") {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2002-12-22 06:15:44 +00:00
|
|
|
if ($select) {
|
|
|
|
$select = "WHERE $select";
|
|
|
|
}
|
|
|
|
|
2002-12-29 04:02:42 +00:00
|
|
|
if ($sort) {
|
|
|
|
$sort = "ORDER BY $sort";
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_records_sql_menu("SELECT $fields FROM $CFG->prefix$table $select $sort");
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Given an SQL select, this function returns an associative
|
|
|
|
*
|
|
|
|
* array of the first two columns. This is most useful in
|
|
|
|
* combination with the choose_from_menu function to create
|
|
|
|
* a form menu.
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-17 04:41:18 +00:00
|
|
|
function get_records_sql_menu($sql) {
|
|
|
|
|
2004-03-13 16:04:52 +00:00
|
|
|
global $CFG, $db;
|
2002-12-17 04:41:18 +00:00
|
|
|
|
2004-03-09 16:32:24 +00:00
|
|
|
if (!$rs = $db->Execute($sql)) {
|
2004-03-10 07:59:32 +00:00
|
|
|
if (isset($CFG->debug) and $CFG->debug > 7) {
|
2004-03-09 16:32:24 +00:00
|
|
|
notify($db->ErrorMsg()."<br /><br />$sql");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2002-12-17 04:41:18 +00:00
|
|
|
|
|
|
|
if ( $rs->RecordCount() > 0 ) {
|
|
|
|
while (!$rs->EOF) {
|
|
|
|
$menu[$rs->fields[0]] = $rs->fields[1];
|
|
|
|
$rs->MoveNext();
|
|
|
|
}
|
|
|
|
return $menu;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get a single field from a database record
|
|
|
|
*
|
|
|
|
* longdesc
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2003-04-26 12:38:04 +00:00
|
|
|
function get_field($table, $return, $field1, $value1, $field2="", $value2="", $field3="", $value3="") {
|
2002-12-17 04:41:18 +00:00
|
|
|
|
|
|
|
global $db, $CFG;
|
|
|
|
|
2003-04-26 12:38:04 +00:00
|
|
|
$select = "WHERE $field1 = '$value1'";
|
|
|
|
|
|
|
|
if ($field2) {
|
|
|
|
$select .= " AND $field2 = '$value2'";
|
|
|
|
if ($field3) {
|
|
|
|
$select .= " AND $field3 = '$value3'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$rs = $db->Execute("SELECT $return FROM $CFG->prefix$table $select");
|
2004-03-09 16:32:24 +00:00
|
|
|
if (!$rs) {
|
2004-03-10 07:59:32 +00:00
|
|
|
if (isset($CFG->debug) and $CFG->debug > 7) {
|
2004-03-09 16:32:24 +00:00
|
|
|
notify($db->ErrorMsg()."<br /><br />SELECT $return FROM $CFG->prefix$table $select");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2002-12-17 04:41:18 +00:00
|
|
|
|
|
|
|
if ( $rs->RecordCount() == 1 ) {
|
|
|
|
return $rs->fields["$return"];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-25 13:47:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a single field from a database record
|
|
|
|
*
|
|
|
|
* longdesc
|
|
|
|
*
|
|
|
|
* @param type description
|
|
|
|
*/
|
|
|
|
function get_field_sql($sql) {
|
|
|
|
|
|
|
|
global $db, $CFG;
|
|
|
|
|
|
|
|
$rs = $db->Execute($sql);
|
|
|
|
if (!$rs) {
|
|
|
|
if (isset($CFG->debug) and $CFG->debug > 7) {
|
|
|
|
notify($db->ErrorMsg()."<br /><br />$sql");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $rs->RecordCount() == 1 ) {
|
|
|
|
return $rs->fields[0];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Set a single field in a database record
|
|
|
|
*
|
|
|
|
* longdesc
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2003-04-26 12:38:04 +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;
|
|
|
|
|
2003-04-26 12:38:04 +00:00
|
|
|
$select = "WHERE $field1 = '$value1'";
|
|
|
|
|
|
|
|
if ($field2) {
|
|
|
|
$select .= " AND $field2 = '$value2'";
|
|
|
|
if ($field3) {
|
|
|
|
$select .= " AND $field3 = '$value3'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Delete one or more records from a table
|
|
|
|
*
|
|
|
|
* Delete one or more records from a table
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-29 04:02:42 +00:00
|
|
|
function delete_records($table, $field1="", $value1="", $field2="", $value2="", $field3="", $value3="") {
|
2002-12-17 04:41:18 +00:00
|
|
|
|
|
|
|
global $db, $CFG;
|
|
|
|
|
2002-12-29 04:02:42 +00:00
|
|
|
if ($field1) {
|
|
|
|
$select = "WHERE $field1 = '$value1'";
|
2002-12-20 14:44:14 +00:00
|
|
|
if ($field2) {
|
2002-12-17 04:41:18 +00:00
|
|
|
$select .= " AND $field2 = '$value2'";
|
2002-12-20 14:44:14 +00:00
|
|
|
if ($field3) {
|
2002-12-17 04:41:18 +00:00
|
|
|
$select .= " AND $field3 = '$value3'";
|
|
|
|
}
|
|
|
|
}
|
2002-12-29 04:02:42 +00:00
|
|
|
} else {
|
|
|
|
$select = "";
|
2002-12-17 04:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $db->Execute("DELETE FROM $CFG->prefix$table $select");
|
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Delete one or more records from a table
|
|
|
|
*
|
|
|
|
* "select" is a fragment of SQL to define the selection criteria
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2003-04-27 06:18:03 +00:00
|
|
|
function delete_records_select($table, $select="") {
|
|
|
|
|
|
|
|
global $CFG, $db;
|
|
|
|
|
|
|
|
if ($select) {
|
|
|
|
$select = "WHERE $select";
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2004-01-30 09:04:31 +00:00
|
|
|
function insert_record($table, $dataobject, $returnid=true, $primarykey='id') {
|
2004-01-29 15:27:21 +00:00
|
|
|
|
|
|
|
global $db, $CFG;
|
|
|
|
|
2004-02-03 15:18:41 +00:00
|
|
|
/// Execute a dummy query to get an empty recordset
|
|
|
|
if (!$rs = $db->Execute("SELECT * FROM $CFG->prefix$table WHERE $primarykey ='-1'")) {
|
|
|
|
return false;
|
|
|
|
}
|
2004-01-29 15:27:21 +00:00
|
|
|
|
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-01-29 15:27:21 +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-03-09 16:32:24 +00:00
|
|
|
notify($db->ErrorMsg()."<br /><br />$insertSQL");
|
|
|
|
}
|
2004-01-29 15:27:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-02-03 15:18:41 +00:00
|
|
|
/// If a return ID is not needed then just return true now
|
|
|
|
if (!$returnid) {
|
2004-01-29 15:27:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-02-03 15:18:41 +00:00
|
|
|
/// Find the return ID of the newly inserted record
|
2004-01-29 15:27:21 +00:00
|
|
|
switch ($CFG->dbtype) {
|
2004-02-03 15:18:41 +00:00
|
|
|
case "postgres7": // Just loves to be special
|
2004-01-29 15:27:21 +00:00
|
|
|
$oid = $db->Insert_ID();
|
2004-01-30 09:04:31 +00:00
|
|
|
if ($rs = $db->Execute("SELECT $primarykey FROM $CFG->prefix$table WHERE oid = $oid")) {
|
2004-01-29 15:27:21 +00:00
|
|
|
if ($rs->RecordCount() == 1) {
|
2004-01-30 09:04:31 +00:00
|
|
|
return (integer) $rs->fields[0];
|
2004-01-29 15:27:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
2004-02-03 15:18:41 +00:00
|
|
|
return $db->Insert_ID(); // Should work on most databases, but not all!
|
2004-01-29 15:27:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +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
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
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
|
|
|
|
if (!$columns = $db->MetaColumns("$CFG->prefix$table")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$data = (array)$dataobject;
|
|
|
|
|
|
|
|
// Pull out data matching these fields
|
|
|
|
foreach ($columns as $column) {
|
2003-01-24 07:51:55 +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;
|
|
|
|
$update = "";
|
|
|
|
|
|
|
|
foreach ($ddd as $key => $value) {
|
|
|
|
$count++;
|
|
|
|
$update .= "$key = '$value'";
|
|
|
|
if ($count < $numddd) {
|
|
|
|
$update .= ", ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($rs = $db->Execute("UPDATE $CFG->prefix$table SET $update WHERE id = '$dataobject->id'")) {
|
|
|
|
return true;
|
|
|
|
} else {
|
2004-03-10 07:59:32 +00:00
|
|
|
if (isset($CFG->debug) and $CFG->debug > 7) {
|
2004-03-09 16:32:24 +00:00
|
|
|
notify($db->ErrorMsg()."<br /><br />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
|
|
|
/**
|
|
|
|
* Get a complete user record, which includes all the info
|
|
|
|
*
|
|
|
|
* in the user record, as well as membership information
|
|
|
|
* Suitable for setting as $USER session cookie.
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-17 04:41:18 +00:00
|
|
|
function get_user_info_from_db($field, $value) {
|
|
|
|
|
2004-06-25 06:30:48 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2002-12-29 16:28:09 +00:00
|
|
|
if (!$field or !$value) {
|
2002-12-17 04:41:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-06-25 06:30:48 +00:00
|
|
|
/// Get all the basic user data
|
|
|
|
|
2002-12-29 16:28:09 +00:00
|
|
|
if (! $user = get_record_select("user", "$field = '$value' AND deleted <> '1'")) {
|
|
|
|
return false;
|
|
|
|
}
|
2002-12-17 04:41:18 +00:00
|
|
|
|
2004-06-25 06:30:48 +00:00
|
|
|
/// Add membership information
|
|
|
|
|
|
|
|
if ($admins = get_records("user_admins", "userid", $user->id)) {
|
|
|
|
$user->admin = true;
|
|
|
|
}
|
2002-12-17 04:41:18 +00:00
|
|
|
|
2004-08-29 14:15:40 +00:00
|
|
|
$user->student[SITEID] = isstudent(SITEID, $user->id);
|
2002-12-17 04:41:18 +00:00
|
|
|
|
2004-06-25 06:30:48 +00:00
|
|
|
/// Determine enrolments based on current enrolment module
|
2002-12-17 04:41:18 +00:00
|
|
|
|
2004-06-25 06:30:48 +00:00
|
|
|
require_once("$CFG->dirroot/enrol/$CFG->enrol/enrol.php");
|
|
|
|
$enrol = new enrolment_plugin();
|
|
|
|
$enrol->get_student_courses($user);
|
|
|
|
$enrol->get_teacher_courses($user);
|
2002-12-17 04:41:18 +00:00
|
|
|
|
2004-06-25 06:30:48 +00:00
|
|
|
/// Get various settings and preferences
|
2002-12-29 16:28:09 +00:00
|
|
|
|
2003-04-28 13:29:26 +00:00
|
|
|
if ($displays = get_records("course_display", "userid", $user->id)) {
|
|
|
|
foreach ($displays as $display) {
|
|
|
|
$user->display[$display->course] = $display->display;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-25 06:30:48 +00:00
|
|
|
if ($preferences = get_records('user_preferences', 'userid', $user->id)) {
|
|
|
|
foreach ($preferences as $preference) {
|
|
|
|
$user->preference[$preference->name] = $preference->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-30 18:07:09 +00:00
|
|
|
if ($groups = get_records("groups_members", "userid", $user->id)) {
|
2003-12-30 19:06:53 +00:00
|
|
|
foreach ($groups as $groupmember) {
|
|
|
|
$courseid = get_field("groups", "courseid", "id", $groupmember->groupid);
|
|
|
|
$user->groupmember[$courseid] = $groupmember->groupid;
|
2003-12-30 17:18:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-28 14:14:19 +00:00
|
|
|
|
2002-12-29 16:28:09 +00:00
|
|
|
return $user;
|
2002-12-17 04:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Does this username and password specify a valid admin user?
|
|
|
|
*
|
|
|
|
* longdesc
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-17 04:41:18 +00:00
|
|
|
function adminlogin($username, $md5password) {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
return record_exists_sql("SELECT u.id
|
|
|
|
FROM {$CFG->prefix}user u,
|
|
|
|
{$CFG->prefix}user_admins a
|
2002-12-23 09:39:26 +00:00
|
|
|
WHERE u.id = a.userid
|
2002-12-17 04:41:18 +00:00
|
|
|
AND u.username = '$username'
|
|
|
|
AND u.password = '$md5password'");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get the guest user information from the database
|
|
|
|
*
|
|
|
|
* longdesc
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-20 14:44:14 +00:00
|
|
|
function get_guest() {
|
|
|
|
return get_user_info_from_db("username", "guest");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Returns $user object of the main admin user
|
|
|
|
*
|
|
|
|
* longdesc
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-17 04:41:18 +00:00
|
|
|
function get_admin () {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if ( $admins = get_admins() ) {
|
|
|
|
foreach ($admins as $admin) {
|
|
|
|
return $admin; // ie the first one
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Returns list of all admins
|
|
|
|
*
|
|
|
|
* longdesc
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-17 04:41:18 +00:00
|
|
|
function get_admins() {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2004-03-24 09:38:47 +00:00
|
|
|
return get_records_sql("SELECT u.*, a.id as adminid
|
2002-12-23 09:39:26 +00:00
|
|
|
FROM {$CFG->prefix}user u,
|
|
|
|
{$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
|
|
|
/**
|
|
|
|
* Returns list of all creators
|
|
|
|
*
|
|
|
|
* longdesc
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +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
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Returns $user object of the main teacher for a course
|
|
|
|
*
|
|
|
|
* longdesc
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-17 04:41:18 +00:00
|
|
|
function get_teacher($courseid) {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if ( $teachers = get_course_teachers($courseid, "t.authority ASC")) {
|
|
|
|
foreach ($teachers as $teacher) {
|
|
|
|
if ($teacher->authority) {
|
|
|
|
return $teacher; // the highest authority teacher
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Searches logs to find all enrolments since a certain date
|
|
|
|
*
|
|
|
|
* used to print recent activity
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2003-08-17 01:45:26 +00:00
|
|
|
function get_recent_enrolments($courseid, $timestart) {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2004-04-09 02:24:02 +00:00
|
|
|
return get_records_sql("SELECT DISTINCT u.id, u.firstname, u.lastname
|
2003-08-17 01:45:26 +00:00
|
|
|
FROM {$CFG->prefix}user u,
|
|
|
|
{$CFG->prefix}user_students s,
|
|
|
|
{$CFG->prefix}log l
|
|
|
|
WHERE l.time > '$timestart'
|
|
|
|
AND l.course = '$courseid'
|
|
|
|
AND l.module = 'course'
|
|
|
|
AND l.action = 'enrol'
|
|
|
|
AND l.info = u.id
|
|
|
|
AND u.id = s.userid
|
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-07-01 21:00:07 +00:00
|
|
|
* Returns array of userinfo of all students in this course
|
|
|
|
* or on this site if courseid is id of site
|
2003-09-14 04:04:15 +00:00
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2003-11-04 12:09:52 +00:00
|
|
|
function get_course_students($courseid, $sort="s.timeaccess", $dir="", $page=0, $recordsperpage=99999,
|
2004-08-21 12:41:40 +00:00
|
|
|
$firstinitial="", $lastinitial="", $group=NULL, $search="", $fields='', $exceptions='') {
|
2002-12-17 04:41:18 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2003-09-18 04:46:34 +00:00
|
|
|
switch ($CFG->dbtype) {
|
|
|
|
case "mysql":
|
2004-03-19 04:26:35 +00:00
|
|
|
$fullname = " CONCAT(firstname,\" \",lastname) ";
|
2003-09-18 04:46:34 +00:00
|
|
|
$limit = "LIMIT $page,$recordsperpage";
|
2003-11-04 12:09:52 +00:00
|
|
|
$LIKE = "LIKE";
|
2003-09-18 04:46:34 +00:00
|
|
|
break;
|
|
|
|
case "postgres7":
|
2004-03-19 04:26:35 +00:00
|
|
|
$fullname = " firstname||' '||lastname ";
|
2003-09-18 04:46:34 +00:00
|
|
|
$limit = "LIMIT $recordsperpage OFFSET ".($page);
|
2003-11-04 12:09:52 +00:00
|
|
|
$LIKE = "ILIKE";
|
2003-09-18 04:46:34 +00:00
|
|
|
break;
|
|
|
|
default:
|
2004-03-19 04:26:35 +00:00
|
|
|
$fullname = " firstname||\" \"||lastname ";
|
2003-09-18 04:46:34 +00:00
|
|
|
$limit = "LIMIT $recordsperpage,$page";
|
2003-11-04 12:09:52 +00:00
|
|
|
$LIKE = "ILIKE";
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
$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 = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$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) {
|
|
|
|
$search = " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%') ";
|
|
|
|
}
|
2003-11-04 12:09:52 +00:00
|
|
|
|
|
|
|
if ($firstinitial) {
|
|
|
|
$select .= " AND u.firstname $LIKE '$firstinitial%' ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($lastinitial) {
|
|
|
|
$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-01-31 03:30:31 +00:00
|
|
|
$groupmembers = ", {$CFG->prefix}groups_members gm ";
|
|
|
|
$select .= " AND u.id = gm.userid AND gm.groupid = '$group'";
|
|
|
|
}
|
2004-08-21 12:41:40 +00:00
|
|
|
|
|
|
|
if (!empty($exceptions)) {
|
|
|
|
$select .= " AND u.id NOT IN ($exceptions)";
|
|
|
|
}
|
2004-01-31 03:30:31 +00:00
|
|
|
|
2004-02-19 17:54:32 +00:00
|
|
|
if ($sort) {
|
|
|
|
$sort = " ORDER BY $sort ";
|
|
|
|
}
|
|
|
|
|
2004-08-07 06:05:37 +00:00
|
|
|
return 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");
|
2002-12-17 04:41:18 +00:00
|
|
|
}
|
|
|
|
|
2003-11-04 12:09:52 +00:00
|
|
|
/**
|
2004-07-01 21:00:07 +00:00
|
|
|
* Counts the students in a given course (or site), or a subset of them
|
2003-11-04 12:09:52 +00:00
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-11-04 12:09:52 +00:00
|
|
|
*/
|
2004-08-21 12:41:40 +00:00
|
|
|
function count_course_students($course, $search="", $firstinitial="", $lastinitial="", $group=NULL, $exceptions='') {
|
2003-11-04 12:09:52 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2004-07-01 21:00:07 +00:00
|
|
|
if (!$course->category) {
|
2004-08-21 12:41:40 +00:00
|
|
|
return count(get_site_users($sort, '', $exceptions));
|
2004-07-01 21:00:07 +00:00
|
|
|
}
|
|
|
|
|
2003-11-04 12:09:52 +00:00
|
|
|
switch ($CFG->dbtype) {
|
|
|
|
case "mysql":
|
2004-03-20 06:58:52 +00:00
|
|
|
$fullname = " CONCAT(firstname,\" \",lastname) ";
|
2003-11-04 12:09:52 +00:00
|
|
|
$LIKE = "LIKE";
|
|
|
|
break;
|
|
|
|
default:
|
2004-03-20 06:58:52 +00:00
|
|
|
$fullname = " firstname||\' \'||lastname ";
|
2003-11-04 12:09:52 +00:00
|
|
|
$LIKE = "ILIKE";
|
|
|
|
}
|
2004-03-19 04:26:35 +00:00
|
|
|
|
2004-01-31 03:30:31 +00:00
|
|
|
$groupmembers = "";
|
2004-03-19 04:26:35 +00:00
|
|
|
|
2004-03-20 06:58:52 +00:00
|
|
|
$select = "s.course = '$course->id' AND s.userid = u.id AND u.deleted = '0'";
|
2003-11-04 12:09:52 +00:00
|
|
|
|
|
|
|
if ($search) {
|
2004-03-20 06:58:52 +00:00
|
|
|
$search = " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%') ";
|
2003-11-04 12:09:52 +00:00
|
|
|
}
|
|
|
|
if ($firstinitial) {
|
|
|
|
$select .= " AND u.firstname $LIKE '$firstinitial%'";
|
|
|
|
}
|
|
|
|
if ($lastinitial) {
|
|
|
|
$select .= " AND u.lastname $LIKE '$lastinitial%'";
|
|
|
|
}
|
2004-01-31 04:03:46 +00:00
|
|
|
|
|
|
|
if ($group === 0) { /// Need something here to get all students not in a group
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
} else if ($group !== NULL) {
|
2004-01-31 03:30:31 +00:00
|
|
|
$groupmembers = ", {$CFG->prefix}groups_members gm ";
|
|
|
|
$select .= " AND u.id = gm.userid AND gm.groupid = '$group'";
|
|
|
|
}
|
2004-08-21 12:41:40 +00:00
|
|
|
|
|
|
|
if (!empty($exceptions)) {
|
|
|
|
$select .= " AND u.id NOT IN ($exceptions)";
|
|
|
|
}
|
2003-11-04 12:09:52 +00:00
|
|
|
|
2004-03-19 04:26:35 +00:00
|
|
|
return count_records_sql("SELECT COUNT(*)
|
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");
|
2003-11-04 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
2004-07-01 21:00:07 +00:00
|
|
|
* Returns list of all teachers in this course
|
|
|
|
* (also works for site)
|
2003-09-14 04:04:15 +00:00
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2004-08-21 12:41:40 +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)) {
|
|
|
|
$except = " AND u.id NOT IN ($exceptions) ";
|
|
|
|
} else {
|
|
|
|
$except = '';
|
|
|
|
}
|
|
|
|
|
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-01-25 19:37:04 +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
|
2003-08-04 05:48:39 +00:00
|
|
|
FROM {$CFG->prefix}user u,
|
|
|
|
{$CFG->prefix}user_teachers t
|
2004-08-21 12:41:40 +00:00
|
|
|
WHERE t.course = '$courseid' AND t.userid = u.id AND u.deleted = '0' $except
|
2002-12-17 04:41:18 +00:00
|
|
|
ORDER BY $sort");
|
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Returns all the users of a course: students and teachers
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2004-08-21 12:41:40 +00:00
|
|
|
function get_course_users($courseid, $sort="timeaccess DESC", $exceptions='') {
|
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();
|
|
|
|
}
|
|
|
|
if (!$students = get_course_students($courseid, $sort, "", 0, 99999, "", "", NULL, "", '', $exceptions)) {
|
|
|
|
$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
|
|
|
// This is too inefficient on large sites.
|
|
|
|
// return get_records_sql("SELECT DISTINCT u.*
|
|
|
|
// FROM mdl_user u
|
|
|
|
// LEFT JOIN mdl_user_students s ON s.course = '$courseid'
|
|
|
|
// LEFT JOIN mdl_user_teachers t ON t.course = '$courseid'
|
|
|
|
// WHERE (u.id = t.userid OR u.id = s.userid)
|
|
|
|
// ORDER BY $sort");
|
|
|
|
}
|
2004-01-31 03:30:31 +00:00
|
|
|
|
2002-12-17 04:41:18 +00:00
|
|
|
|
2004-08-21 12:41:40 +00:00
|
|
|
/**
|
|
|
|
* Search through course users
|
|
|
|
*
|
|
|
|
* @param type description
|
|
|
|
*/
|
|
|
|
function search_users($courseid, $groupid, $searchtext, $sort='', $exceptions='') {
|
|
|
|
global $CFG;
|
2004-01-31 03:30:31 +00:00
|
|
|
|
2004-08-21 12:41:40 +00:00
|
|
|
switch ($CFG->dbtype) {
|
|
|
|
case "mysql":
|
|
|
|
$fullname = " CONCAT(u.firstname,\" \",u.lastname) ";
|
|
|
|
$LIKE = "LIKE";
|
|
|
|
break;
|
|
|
|
case "postgres7":
|
|
|
|
$fullname = " u.firstname||' '||u.lastname ";
|
|
|
|
$LIKE = "ILIKE";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$fullname = " u.firstname||\" \"||u.lastname ";
|
|
|
|
$LIKE = "ILIKE";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($exceptions)) {
|
|
|
|
$except = " AND u.id NOT IN ($exceptions) ";
|
|
|
|
} else {
|
|
|
|
$except = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($sort)) {
|
|
|
|
$order = " ORDER by $sort";
|
|
|
|
} else {
|
|
|
|
$order = '';
|
|
|
|
}
|
|
|
|
|
2004-08-29 14:15:40 +00:00
|
|
|
if (!$courseid or $courseid == SITEID) {
|
2004-08-21 12:41:40 +00:00
|
|
|
if (!$admins = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
|
|
|
|
FROM {$CFG->prefix}user u,
|
|
|
|
{$CFG->prefix}user_admins s
|
|
|
|
WHERE s.userid = u.id AND u.deleted = '0'
|
|
|
|
AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
|
|
|
|
$except $order")) {
|
|
|
|
$admins = array();
|
|
|
|
}
|
|
|
|
if (!$teachers = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
|
|
|
|
FROM {$CFG->prefix}user u,
|
|
|
|
{$CFG->prefix}user_teachers s
|
|
|
|
WHERE s.userid = u.id AND u.deleted = '0'
|
|
|
|
AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
|
|
|
|
$except $order")) {
|
|
|
|
$teachers = array();
|
|
|
|
}
|
|
|
|
if (!$students = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
|
|
|
|
FROM {$CFG->prefix}user u,
|
|
|
|
{$CFG->prefix}user_students s
|
|
|
|
WHERE s.userid = u.id AND u.deleted = '0'
|
|
|
|
AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
|
|
|
|
$except $order")) {
|
|
|
|
$students = array();
|
|
|
|
}
|
|
|
|
return $admins + $teachers + $students;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if ($groupid) {
|
|
|
|
return get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
|
|
|
|
FROM {$CFG->prefix}user u,
|
|
|
|
{$CFG->prefix}groups_members g
|
|
|
|
WHERE g.groupid = '$groupid' AND g.userid = u.id AND u.deleted = '0'
|
|
|
|
AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
|
|
|
|
$except $order");
|
|
|
|
} else {
|
|
|
|
if (!$teachers = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
|
|
|
|
FROM {$CFG->prefix}user u,
|
|
|
|
{$CFG->prefix}user_teachers s
|
|
|
|
WHERE s.course = '$courseid' AND s.userid = u.id AND u.deleted = '0'
|
|
|
|
AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
|
|
|
|
$except $order")) {
|
|
|
|
$teachers = array();
|
|
|
|
}
|
|
|
|
if (!$students = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
|
|
|
|
FROM {$CFG->prefix}user u,
|
|
|
|
{$CFG->prefix}user_students s
|
|
|
|
WHERE s.course = '$courseid' AND s.userid = u.id AND u.deleted = '0'
|
|
|
|
AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
|
|
|
|
$except $order")) {
|
|
|
|
$students = array();
|
|
|
|
}
|
|
|
|
return $teachers + $students;
|
|
|
|
}
|
|
|
|
}
|
2002-12-17 04:41:18 +00:00
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Returns a list of all active users who are enrolled
|
|
|
|
*
|
|
|
|
* or teaching in courses on this server
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2004-08-21 12:41:40 +00:00
|
|
|
function get_site_users($sort="u.lastaccess DESC", $select="", $exceptions='') {
|
2003-03-29 04:08:51 +00:00
|
|
|
|
2004-03-09 12:18:52 +00:00
|
|
|
global $CFG;
|
2003-05-20 09:33:30 +00:00
|
|
|
|
2004-08-21 12:41:40 +00:00
|
|
|
if (!empty($exceptions)) {
|
|
|
|
$except = " AND u.id NOT IN ($exceptions) ";
|
|
|
|
} else {
|
|
|
|
$except = '';
|
|
|
|
}
|
2003-03-29 04:08:51 +00:00
|
|
|
|
2003-08-23 06:19:05 +00:00
|
|
|
if ($select) {
|
|
|
|
$selectinfo = $select;
|
|
|
|
} else {
|
2004-06-01 07:28:14 +00:00
|
|
|
$selectinfo = "u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, u.maildigest,".
|
2004-03-07 06:32:36 +00:00
|
|
|
"u.email, u.emailstop, u.city, u.country, u.lastaccess, u.lastlogin, u.picture, u.lang, u.timezone";
|
2003-08-23 06:19:05 +00:00
|
|
|
}
|
|
|
|
|
2004-08-21 12:41:40 +00:00
|
|
|
if (!$students = get_records_sql("SELECT DISTINCT $selectinfo from {$CFG->prefix}user u, {$CFG->prefix}user_students s
|
|
|
|
WHERE s.userid = u.id $except ORDER BY $sort")) {
|
|
|
|
$students = array();
|
2003-08-23 06:19:05 +00:00
|
|
|
}
|
2004-03-09 12:18:52 +00:00
|
|
|
|
2004-08-21 12:41:40 +00:00
|
|
|
if (!$teachers = get_records_sql("SELECT DISTINCT $selectinfo from {$CFG->prefix}user u, {$CFG->prefix}user_teachers t
|
|
|
|
WHERE t.userid = u.id $except ORDER BY $sort")) {
|
|
|
|
$teachers = array();
|
2003-08-23 06:19:05 +00:00
|
|
|
}
|
2004-03-09 12:18:52 +00:00
|
|
|
|
2004-08-21 12:41:40 +00:00
|
|
|
if (!$admins = get_records_sql("SELECT DISTINCT $selectinfo from {$CFG->prefix}user u, {$CFG->prefix}user_admins a
|
|
|
|
WHERE a.userid = u.id $except ORDER BY $sort")) {
|
|
|
|
$admins = array();
|
2003-08-23 06:19:05 +00:00
|
|
|
}
|
2004-03-09 12:18:52 +00:00
|
|
|
|
2004-08-21 12:41:40 +00:00
|
|
|
return $admins + $teachers + $students;
|
2003-03-29 04:08:51 +00:00
|
|
|
}
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Returns a subset of users
|
|
|
|
*
|
|
|
|
* longdesc
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param bookean $get if false then only a count of the records is returned
|
|
|
|
* @param string $search a simple string to search for
|
|
|
|
* @param boolean $confirmed a switch to allow/disallow unconfirmed users
|
|
|
|
* @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
|
2004-08-07 06:05:37 +00:00
|
|
|
* @param string $firstinitial
|
|
|
|
* @param string $lastinitial
|
|
|
|
* @param string $page
|
|
|
|
* @param string $recordsperpage
|
|
|
|
* @param string $fields a comma separated list of fields
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2004-03-20 06:58:52 +00:00
|
|
|
function get_users($get=true, $search="", $confirmed=false, $exceptions="", $sort="firstname ASC",
|
2004-08-07 06:05:37 +00:00
|
|
|
$firstinitial="", $lastinitial="", $page=0, $recordsperpage=99999, $fields="*") {
|
2003-09-14 04:04:15 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
switch ($CFG->dbtype) {
|
|
|
|
case "mysql":
|
2004-05-30 18:46:42 +00:00
|
|
|
$limit = "LIMIT $page,$recordsperpage";
|
2003-09-14 04:04:15 +00:00
|
|
|
$fullname = " CONCAT(firstname,\" \",lastname) ";
|
2003-09-19 08:33:27 +00:00
|
|
|
$LIKE = "LIKE";
|
|
|
|
break;
|
|
|
|
case "postgres7":
|
2004-05-30 18:46:42 +00:00
|
|
|
$limit = "LIMIT $recordsperpage OFFSET ".($page);
|
2004-03-09 12:46:03 +00:00
|
|
|
$fullname = " firstname||' '||lastname ";
|
2003-09-19 08:33:27 +00:00
|
|
|
$LIKE = "ILIKE";
|
2003-09-14 04:04:15 +00:00
|
|
|
break;
|
|
|
|
default:
|
2004-05-30 18:46:42 +00:00
|
|
|
$limit = "LIMIT $recordsperpage,$page";
|
2003-09-14 04:04:15 +00:00
|
|
|
$fullname = " firstname||\" \"||lastname ";
|
2003-09-19 08:33:27 +00:00
|
|
|
$LIKE = "ILIKE";
|
2003-09-14 04:04:15 +00:00
|
|
|
}
|
2003-03-21 10:10:21 +00:00
|
|
|
|
2004-03-20 06:58:52 +00:00
|
|
|
$select = "username <> 'guest' AND deleted = 0";
|
|
|
|
|
2003-03-21 10:10:21 +00:00
|
|
|
if ($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-03-20 06:58:52 +00:00
|
|
|
$select .= " AND confirmed = '1' ";
|
2003-05-14 15:19:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($exceptions) {
|
2004-03-20 06:58:52 +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) {
|
|
|
|
$select .= " AND firstname $LIKE '$firstinitial%'";
|
|
|
|
}
|
|
|
|
if ($lastinitial) {
|
|
|
|
$select .= " AND lastname $LIKE '$lastinitial%'";
|
|
|
|
}
|
|
|
|
|
2003-05-14 15:19:04 +00:00
|
|
|
if ($sort and $get) {
|
|
|
|
$sort = " ORDER BY $sort ";
|
|
|
|
} else {
|
|
|
|
$sort = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($get) {
|
2004-08-07 06:05:37 +00:00
|
|
|
return get_records_select("user", "$select $sort $limit", '', $fields);
|
2003-05-14 15:19:04 +00:00
|
|
|
} else {
|
2004-05-30 18:46:42 +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
|
|
|
/**
|
|
|
|
* shortdesc
|
|
|
|
*
|
|
|
|
* longdesc
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2004-03-20 06:58:52 +00:00
|
|
|
function get_users_listing($sort="lastaccess", $dir="ASC", $page=0, $recordsperpage=99999,
|
|
|
|
$search="", $firstinitial="", $lastinitial="") {
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
global $CFG;
|
2002-12-23 13:48:31 +00:00
|
|
|
|
2003-02-18 03:16:07 +00:00
|
|
|
switch ($CFG->dbtype) {
|
|
|
|
case "mysql":
|
|
|
|
$limit = "LIMIT $page,$recordsperpage";
|
2003-09-14 04:04:15 +00:00
|
|
|
$fullname = " CONCAT(firstname,\" \",lastname) ";
|
2003-09-19 08:33:27 +00:00
|
|
|
$LIKE = "LIKE";
|
2003-02-18 03:16:07 +00:00
|
|
|
break;
|
|
|
|
case "postgres7":
|
2003-05-05 16:13:45 +00:00
|
|
|
$limit = "LIMIT $recordsperpage OFFSET ".($page);
|
2004-03-09 12:46:03 +00:00
|
|
|
$fullname = " firstname||' '||lastname ";
|
2003-09-19 08:33:27 +00:00
|
|
|
$LIKE = "ILIKE";
|
2003-02-18 03:16:07 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$limit = "LIMIT $recordsperpage,$page";
|
2004-03-20 06:58:52 +00:00
|
|
|
$fullname = " firstname||' '||lastname ";
|
2003-09-19 08:33:27 +00:00
|
|
|
$LIKE = "LIKE";
|
2002-12-23 13:48:31 +00:00
|
|
|
}
|
2003-02-18 03:16:07 +00:00
|
|
|
|
2004-03-20 06:58:52 +00:00
|
|
|
$select = 'deleted <> 1';
|
|
|
|
|
2003-03-21 09:42:42 +00:00
|
|
|
if ($search) {
|
2004-03-20 06:58:52 +00:00
|
|
|
$select .= " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%') ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($firstinitial) {
|
|
|
|
$select .= " AND firstname $LIKE '$firstinitial%' ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($lastinitial) {
|
|
|
|
$select .= " AND lastname $LIKE '$lastinitial%' ";
|
2003-03-21 09:42:42 +00:00
|
|
|
}
|
|
|
|
|
2004-03-20 06:58:52 +00:00
|
|
|
if ($sort) {
|
|
|
|
$sort = " ORDER BY $sort $dir";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
2002-12-20 14:44:14 +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
|
|
|
/**
|
|
|
|
* shortdesc
|
|
|
|
*
|
|
|
|
* longdesc
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-20 14:44:14 +00:00
|
|
|
function get_users_confirmed() {
|
|
|
|
global $CFG;
|
|
|
|
return get_records_sql("SELECT *
|
|
|
|
FROM {$CFG->prefix}user
|
|
|
|
WHERE confirmed = 1
|
|
|
|
AND deleted = 0
|
|
|
|
AND username <> 'guest'
|
|
|
|
AND username <> 'changeme'");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* shortdesc
|
|
|
|
*
|
|
|
|
* longdesc
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-23 14:19:45 +00:00
|
|
|
function get_users_unconfirmed($cutofftime=2000000000) {
|
2002-12-20 14:44:14 +00:00
|
|
|
global $CFG;
|
|
|
|
return get_records_sql("SELECT *
|
|
|
|
FROM {$CFG->prefix}user
|
|
|
|
WHERE confirmed = 0
|
|
|
|
AND firstaccess > 0
|
|
|
|
AND firstaccess < '$cutofftime'");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* shortdesc
|
|
|
|
*
|
|
|
|
* longdesc
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
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
|
|
|
|
WHERE timeaccess > '0'
|
|
|
|
AND timeaccess < '$cutofftime' ");
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
2003-12-30 17:18:06 +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.
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-12-30 17:18:06 +00:00
|
|
|
*/
|
|
|
|
function get_groups($courseid, $userid=0) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if ($userid) {
|
2003-12-30 18:19:15 +00:00
|
|
|
$dbselect = ", {$CFG->prefix}groups_members m";
|
2003-12-30 17:18:06 +00:00
|
|
|
$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 ");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of user objects
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-12-30 17:18:06 +00:00
|
|
|
*/
|
2004-08-21 12:41:40 +00:00
|
|
|
function get_group_users($groupid, $sort="u.lastaccess DESC", $exceptions='') {
|
2003-12-30 17:18:06 +00:00
|
|
|
global $CFG;
|
2004-08-21 12:41:40 +00:00
|
|
|
if (!empty($exceptions)) {
|
|
|
|
$except = " AND u.id NOT IN ($exceptions) ";
|
|
|
|
} else {
|
|
|
|
$except = '';
|
|
|
|
}
|
2003-12-30 17:18:06 +00:00
|
|
|
return get_records_sql("SELECT DISTINCT u.*
|
|
|
|
FROM {$CFG->prefix}user u,
|
2003-12-30 18:07:09 +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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An efficient way of finding all the users who aren't in groups yet
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of user objects
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2004-03-13 14:25:27 +00:00
|
|
|
*/
|
|
|
|
function get_group_students($groupid, $sort="u.lastaccess DESC") {
|
|
|
|
global $CFG;
|
|
|
|
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
|
|
|
|
WHERE m.groupid = '$groupid'
|
|
|
|
AND m.userid = u.id
|
|
|
|
AND m.groupid = g.id
|
|
|
|
AND g.courseid = s.course
|
|
|
|
AND s.userid = u.id
|
|
|
|
ORDER BY $sort");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-30 17:18:06 +00:00
|
|
|
/**
|
|
|
|
* Returns the user's group in a particular course
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-12-30 17:18:06 +00:00
|
|
|
*/
|
|
|
|
function user_group($courseid, $userid) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
return get_record_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
|
|
|
|
AND m.userid = '$userid'");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Returns $course object of the top-level site.
|
|
|
|
*
|
|
|
|
* Returns $course object of the top-level site.
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2003-08-15 13:59:24 +00:00
|
|
|
function get_site () {
|
|
|
|
|
|
|
|
if ( $course = get_record("course", "category", 0)) {
|
|
|
|
return $course;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Returns list of courses, for whole site, or category
|
|
|
|
*
|
|
|
|
* Returns list of courses, for whole site, or category
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2003-08-21 14:04:04 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
$categoryselect = "";
|
|
|
|
if ($categoryid != "all") {
|
2004-01-08 04:50:43 +00:00
|
|
|
$categoryselect = "c.category = '$categoryid'";
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
|
2003-08-21 09:33:07 +00:00
|
|
|
$teachertable = "";
|
2003-08-21 09:39:07 +00:00
|
|
|
$visiblecourses = "";
|
2004-05-19 07:12:40 +00:00
|
|
|
$sqland = "";
|
|
|
|
if (!empty($categoryselect)) {
|
|
|
|
$sqland = "AND ";
|
|
|
|
}
|
2004-08-07 13:36:57 +00:00
|
|
|
if (!empty($USER->id)) { // May need to check they are a teacher
|
2003-09-03 08:29:05 +00:00
|
|
|
if (!iscreator()) {
|
2004-05-19 07:12:40 +00:00
|
|
|
$visiblecourses = "$sqland ((c.visible > 0) OR (t.userid = '$USER->id' AND t.course = c.id))";
|
2003-08-21 09:33:07 +00:00
|
|
|
$teachertable = ", {$CFG->prefix}user_teachers t";
|
|
|
|
}
|
2003-08-21 09:39:07 +00:00
|
|
|
} else {
|
2004-05-19 07:12:40 +00:00
|
|
|
$visiblecourses = "$sqland c.visible > 0";
|
2003-08-21 09:33:07 +00:00
|
|
|
}
|
|
|
|
|
2004-01-08 04:50:43 +00:00
|
|
|
if ($categoryselect or $visiblecourses) {
|
|
|
|
$selectsql = "{$CFG->prefix}course c $teachertable WHERE $categoryselect $visiblecourses";
|
|
|
|
} else {
|
|
|
|
$selectsql = "{$CFG->prefix}course c $teachertable";
|
|
|
|
}
|
|
|
|
|
2003-08-21 14:04:04 +00:00
|
|
|
|
2003-09-10 13:17:13 +00:00
|
|
|
return get_records_sql("SELECT DISTINCT $fields FROM $selectsql ORDER BY $sort");
|
2003-08-21 14:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Returns list of courses, for whole site, or category
|
|
|
|
*
|
|
|
|
* Similar to get_courses, but allows paging
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2003-08-21 14:04:04 +00:00
|
|
|
function get_courses_page($categoryid="all", $sort="c.sortorder ASC", $fields="c.*",
|
|
|
|
&$totalcount, $limitfrom="", $limitnum="") {
|
|
|
|
|
|
|
|
global $USER, $CFG;
|
|
|
|
|
|
|
|
$categoryselect = "";
|
|
|
|
if ($categoryid != "all") {
|
|
|
|
$categoryselect = "c.category = '$categoryid'";
|
|
|
|
}
|
|
|
|
|
|
|
|
$teachertable = "";
|
|
|
|
$visiblecourses = "";
|
2004-05-19 07:12:40 +00:00
|
|
|
$sqland = "";
|
|
|
|
if (!empty($categoryselect)) {
|
|
|
|
$sqland = "AND ";
|
|
|
|
}
|
2003-08-21 14:04:04 +00:00
|
|
|
if (!empty($USER)) { // May need to check they are a teacher
|
2003-09-03 08:29:05 +00:00
|
|
|
if (!iscreator()) {
|
2004-05-19 07:12:40 +00:00
|
|
|
$visiblecourses = "$sqland ((c.visible > 0) OR (t.userid = '$USER->id' AND t.course = c.id))";
|
2003-08-21 14:04:04 +00:00
|
|
|
$teachertable = ", {$CFG->prefix}user_teachers t";
|
|
|
|
}
|
|
|
|
} else {
|
2004-05-19 07:12:40 +00:00
|
|
|
$visiblecourses = "$sqland c.visible > 0";
|
2003-08-21 14:04:04 +00:00
|
|
|
}
|
|
|
|
|
2003-08-21 09:33:07 +00:00
|
|
|
if ($limitfrom !== "") {
|
|
|
|
switch ($CFG->dbtype) {
|
|
|
|
case "mysql":
|
|
|
|
$limit = "LIMIT $limitfrom,$limitnum";
|
|
|
|
break;
|
|
|
|
case "postgres7":
|
|
|
|
$limit = "LIMIT $limitnum OFFSET $limitfrom";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$limit = "LIMIT $limitnum,$limitfrom";
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
2003-08-21 09:33:07 +00:00
|
|
|
} else {
|
|
|
|
$limit = "";
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
2003-08-21 09:33:07 +00:00
|
|
|
|
2003-08-21 12:19:09 +00:00
|
|
|
$selectsql = "{$CFG->prefix}course c $teachertable WHERE $categoryselect $visiblecourses";
|
2003-08-21 09:33:07 +00:00
|
|
|
|
2003-08-21 12:19:09 +00:00
|
|
|
$totalcount = count_records_sql("SELECT COUNT(DISTINCT c.id) FROM $selectsql");
|
2003-08-21 09:33:07 +00:00
|
|
|
|
2003-09-10 13:17:13 +00:00
|
|
|
return get_records_sql("SELECT DISTINCT $fields FROM $selectsql ORDER BY $sort $limit");
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* shortdesc
|
|
|
|
*
|
|
|
|
* longdesc
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2004-05-30 00:33:45 +00:00
|
|
|
function get_my_courses($userid, $sort="visible DESC,sortorder ASC") {
|
2003-08-21 17:24:40 +00:00
|
|
|
|
2003-08-15 13:59:24 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2003-08-21 17:24:40 +00:00
|
|
|
$course = array();
|
|
|
|
|
|
|
|
if ($students = get_records("user_students", "userid", $userid, "", "id, course")) {
|
|
|
|
foreach ($students as $student) {
|
|
|
|
$course[$student->course] = $student->course;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($teachers = get_records("user_teachers", "userid", $userid, "", "id, course")) {
|
|
|
|
foreach ($teachers as $teacher) {
|
|
|
|
$course[$teacher->course] = $teacher->course;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($course)) {
|
|
|
|
return $course;
|
|
|
|
}
|
|
|
|
|
|
|
|
$courseids = implode(',', $course);
|
|
|
|
|
|
|
|
return get_records_list("course", "id", $courseids, $sort);
|
|
|
|
|
|
|
|
// The following is correct but VERY slow with large datasets
|
|
|
|
//
|
|
|
|
// return get_records_sql("SELECT c.*
|
|
|
|
// FROM {$CFG->prefix}course c,
|
|
|
|
// {$CFG->prefix}user_students s,
|
|
|
|
// {$CFG->prefix}user_teachers t
|
|
|
|
// WHERE (s.userid = '$userid' AND s.course = c.id)
|
|
|
|
// OR (t.userid = '$userid' AND t.course = c.id)
|
|
|
|
// GROUP BY c.id
|
|
|
|
// ORDER BY $sort");
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Returns a list of courses that match a search
|
|
|
|
*
|
|
|
|
* Returns a list of courses that match a search
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2003-08-19 05:32:20 +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;
|
|
|
|
|
|
|
|
switch ($CFG->dbtype) {
|
|
|
|
case "mysql":
|
|
|
|
$limit = "LIMIT $page,$recordsperpage";
|
|
|
|
break;
|
|
|
|
case "postgres7":
|
|
|
|
$limit = "LIMIT $recordsperpage OFFSET ".($page * $recordsperpage);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$limit = "LIMIT $recordsperpage,$page";
|
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
//to allow case-insensitive search for postgesql
|
2003-08-15 13:59:24 +00:00
|
|
|
if ($CFG->dbtype == "postgres7") {
|
2003-08-19 05:32:20 +00:00
|
|
|
$LIKE = "ILIKE";
|
|
|
|
$NOTLIKE = "NOT ILIKE"; // case-insensitive
|
|
|
|
$REGEXP = "~*";
|
|
|
|
$NOTREGEXP = "!~*";
|
2003-08-15 13:59:24 +00:00
|
|
|
} else {
|
2003-08-19 05:32:20 +00:00
|
|
|
$LIKE = "LIKE";
|
|
|
|
$NOTLIKE = "NOT LIKE";
|
|
|
|
$REGEXP = "REGEXP";
|
|
|
|
$NOTREGEXP = "NOT REGEXP";
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$fullnamesearch = "";
|
|
|
|
$summarysearch = "";
|
|
|
|
|
|
|
|
foreach ($searchterms as $searchterm) {
|
|
|
|
if ($fullnamesearch) {
|
|
|
|
$fullnamesearch .= " AND ";
|
|
|
|
}
|
|
|
|
if ($summarysearch) {
|
|
|
|
$summarysearch .= " AND ";
|
|
|
|
}
|
2003-08-19 05:32:20 +00:00
|
|
|
|
|
|
|
if (substr($searchterm,0,1) == "+") {
|
|
|
|
$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 {
|
|
|
|
$summarysearch .= " summary $LIKE '%$searchterm%' ";
|
|
|
|
$fullnamesearch .= " fullname $LIKE '%$searchterm%' ";
|
|
|
|
}
|
|
|
|
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
|
2003-08-21 08:20:20 +00:00
|
|
|
$selectsql = "{$CFG->prefix}course WHERE ($fullnamesearch OR $summarysearch) AND category > '0'";
|
2003-08-19 05:32:20 +00:00
|
|
|
|
|
|
|
$totalcount = count_records_sql("SELECT COUNT(*) FROM $selectsql");
|
2003-08-15 13:59:24 +00:00
|
|
|
|
2003-08-19 05:32:20 +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) {
|
|
|
|
if (!$course->visible) {
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Returns a sorted list of categories
|
|
|
|
*
|
|
|
|
* Returns a sorted list of categories
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2003-08-15 13:59:24 +00:00
|
|
|
function get_categories($parent="none", $sort="sortorder ASC") {
|
|
|
|
|
|
|
|
if ($parent == "none") {
|
|
|
|
$categories = get_records("course_categories", "", "", $sort);
|
|
|
|
} else {
|
|
|
|
$categories = get_records("course_categories", "parent", $parent, $sort);
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
if (!$category->visible) {
|
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-05-30 00:33:45 +00:00
|
|
|
* This recursive function makes sure that the courseorder is consecutive
|
2003-09-14 04:04:15 +00:00
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2004-05-30 00:33:45 +00:00
|
|
|
function fix_course_sortorder($categoryid=0, $n=0) {
|
|
|
|
|
2003-08-15 13:59:24 +00:00
|
|
|
$count = 0;
|
2004-05-30 00:33:45 +00:00
|
|
|
if ($courses = get_courses($categoryid)) {
|
|
|
|
foreach ($courses as $course) {
|
|
|
|
set_field('course', 'sortorder', $n, 'id', $course->id);
|
|
|
|
$n++;
|
|
|
|
$count++;
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
2003-08-19 09:26:36 +00:00
|
|
|
set_field("course_categories", "coursecount", $count, "id", $categoryid);
|
2004-05-30 00:33:45 +00:00
|
|
|
|
|
|
|
if ($categories = get_categories($categoryid)) {
|
|
|
|
foreach ($categories as $category) {
|
|
|
|
$n = fix_course_sortorder($category->id, $n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $n;
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +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.
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2003-08-15 13:59:24 +00:00
|
|
|
function make_default_scale() {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$defaultscale = NULL;
|
|
|
|
$defaultscale->courseid = 0;
|
|
|
|
$defaultscale->userid = 0;
|
|
|
|
$defaultscale->name = get_string("separateandconnected");
|
|
|
|
$defaultscale->scale = get_string("postrating1", "forum").",".
|
|
|
|
get_string("postrating2", "forum").",".
|
|
|
|
get_string("postrating3", "forum");
|
|
|
|
$defaultscale->timemodified = time();
|
|
|
|
|
|
|
|
/// Read in the big description from the file. Note this is not
|
|
|
|
/// HTML (despite the file extension) but Moodle format text.
|
|
|
|
$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");
|
|
|
|
} else {
|
|
|
|
$file = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
$defaultscale->description = addslashes(implode("", $file));
|
|
|
|
|
|
|
|
if ($defaultscale->id = insert_record("scale", $defaultscale)) {
|
2003-08-17 01:55:03 +00:00
|
|
|
execute_sql("UPDATE {$CFG->prefix}forum SET scale = '$defaultscale->id'", false);
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Returns a menu of all available scales from the site as well as the given course
|
|
|
|
*
|
|
|
|
* Returns a menu of all available scales from the site as well as the given course
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2003-08-15 13:59:24 +00:00
|
|
|
function get_scales_menu($courseid=0) {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$sql = "SELECT id, name FROM {$CFG->prefix}scale
|
|
|
|
WHERE courseid = '0' or courseid = '$courseid'
|
|
|
|
ORDER BY courseid ASC, name ASC";
|
|
|
|
|
|
|
|
if ($scales = get_records_sql_menu("$sql")) {
|
|
|
|
return $scales;
|
|
|
|
}
|
|
|
|
|
|
|
|
make_default_scale();
|
|
|
|
|
|
|
|
return get_records_sql_menu("$sql");
|
|
|
|
}
|
|
|
|
|
2002-12-17 04:41:18 +00:00
|
|
|
/// MODULE FUNCTIONS /////////////////////////////////////////////////
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Just gets a raw list of all modules in a course
|
|
|
|
*
|
|
|
|
* Just gets a raw list of all modules in a course
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
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
|
|
|
|
FROM {$CFG->prefix}modules m,
|
|
|
|
{$CFG->prefix}course_modules cm
|
2002-12-20 14:44:14 +00:00
|
|
|
WHERE cm.course = '$courseid'
|
|
|
|
AND cm.deleted = '0'
|
|
|
|
AND cm.module = m.id ");
|
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* Given an instance of a module, finds the coursemodule description
|
|
|
|
*
|
|
|
|
* Given an instance of a module, finds the coursemodule description
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2002-12-17 04:41:18 +00:00
|
|
|
function get_coursemodule_from_instance($modulename, $instance, $courseid) {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
return get_record_sql("SELECT cm.*, m.name
|
2003-04-23 16:57:35 +00:00
|
|
|
FROM {$CFG->prefix}course_modules cm,
|
|
|
|
{$CFG->prefix}modules md,
|
|
|
|
{$CFG->prefix}$modulename m
|
2002-12-17 04:41:18 +00:00
|
|
|
WHERE cm.course = '$courseid' AND
|
|
|
|
cm.deleted = '0' AND
|
|
|
|
cm.instance = m.id AND
|
|
|
|
md.name = '$modulename' AND
|
|
|
|
md.id = cm.module AND
|
|
|
|
m.id = '$instance'");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2003-09-14 04:04:15 +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.
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param string $modulename the name of the module to get instances for
|
|
|
|
* @param object(course) $course this depends on an accurate $course->modinfo
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
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
|
2003-04-23 16:57:35 +00:00
|
|
|
FROM {$CFG->prefix}course_modules cm,
|
|
|
|
{$CFG->prefix}course_sections cw,
|
|
|
|
{$CFG->prefix}modules md,
|
|
|
|
{$CFG->prefix}$modulename m
|
2003-07-12 05:19:18 +00:00
|
|
|
WHERE cm.course = '$course->id' AND
|
2002-12-17 04:41:18 +00:00
|
|
|
cm.instance = m.id AND
|
|
|
|
cm.deleted = '0' 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
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2003-04-25 05:24:29 +00:00
|
|
|
function instance_is_visible($moduletype, $module) {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2003-04-26 09:34:11 +00:00
|
|
|
if ($records = get_records_sql("SELECT cm.instance, cm.visible
|
2003-04-25 05:24:29 +00:00
|
|
|
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
|
2003-04-26 09:34:11 +00:00
|
|
|
cm.instance = '$module->id'")) {
|
2003-04-25 05:24:29 +00:00
|
|
|
|
|
|
|
foreach ($records as $record) { // there should only be one - use the first one
|
|
|
|
return $record->visible;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param int $course the course id
|
|
|
|
* @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
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2004-01-31 14:47:57 +00:00
|
|
|
function add_to_log($courseid, $module, $action, $url="", $info="", $cm=0, $user=0) {
|
2002-12-20 14:44:14 +00:00
|
|
|
|
2002-12-23 13:48:31 +00:00
|
|
|
global $db, $CFG, $USER, $REMOTE_ADDR;
|
2002-12-20 14:44:14 +00:00
|
|
|
|
2004-01-30 18:21:56 +00:00
|
|
|
if ($user) {
|
|
|
|
$userid = $user;
|
|
|
|
} else {
|
|
|
|
if (isset($USER->realuser)) { // Don't log
|
|
|
|
return;
|
|
|
|
}
|
2004-07-25 13:47:38 +00:00
|
|
|
$userid = empty($USER->id) ? "0" : $USER->id;
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$timenow = time();
|
|
|
|
$info = addslashes($info);
|
|
|
|
|
2004-01-31 14:47:57 +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)) {
|
2002-12-20 14:44:14 +00:00
|
|
|
echo "<P>Error: Could not insert a new entry to the Moodle log</P>"; // Don't throw an error
|
|
|
|
}
|
2004-08-29 11:41:55 +00:00
|
|
|
if (!$user and isset($USER->id)) {
|
2004-08-29 14:15:40 +00:00
|
|
|
if ($courseid == SITEID) {
|
|
|
|
$db->Execute("UPDATE {$CFG->prefix}user SET lastIP='$REMOTE_ADDR', lastaccess='$timenow'
|
|
|
|
WHERE id = '$USER->id' ");
|
2004-08-29 11:41:55 +00:00
|
|
|
} else if (isstudent($courseid)) {
|
2004-01-30 18:21:56 +00:00
|
|
|
$db->Execute("UPDATE {$CFG->prefix}user_students SET timeaccess = '$timenow' ".
|
|
|
|
"WHERE course = '$courseid' AND userid = '$userid'");
|
2004-08-29 11:41:55 +00:00
|
|
|
} else if (isteacher($courseid, false, false)) {
|
2004-01-30 18:21:56 +00:00
|
|
|
$db->Execute("UPDATE {$CFG->prefix}user_teachers SET timeaccess = '$timenow' ".
|
|
|
|
"WHERE course = '$courseid' AND userid = '$userid'");
|
|
|
|
}
|
|
|
|
}
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* select all log records based on SQL criteria
|
|
|
|
*
|
|
|
|
* select all log records based on SQL criteria
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param string $select SQL select criteria
|
|
|
|
* @param string $order SQL order by clause to sort the records returned
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2003-08-22 06:07:18 +00:00
|
|
|
function get_logs($select, $order="l.time DESC", $limitfrom="", $limitnum="", &$totalcount) {
|
2002-12-20 14:44:14 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2003-08-22 06:07:18 +00:00
|
|
|
if ($limitfrom !== "") {
|
|
|
|
switch ($CFG->dbtype) {
|
|
|
|
case "mysql":
|
|
|
|
$limit = "LIMIT $limitfrom,$limitnum";
|
|
|
|
break;
|
|
|
|
case "postgres7":
|
|
|
|
$limit = "LIMIT $limitnum OFFSET $limitfrom";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$limit = "LIMIT $limitnum,$limitfrom";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$limit = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($order) {
|
|
|
|
$order = "ORDER BY $order";
|
|
|
|
}
|
|
|
|
|
2004-07-25 13:47:38 +00:00
|
|
|
$selectsql = "{$CFG->prefix}log l LEFT JOIN {$CFG->prefix}user u ON l.userid = u.id ".((strlen($select) > 0) ? "WHERE $select" : "");
|
2003-08-22 06:07:18 +00:00
|
|
|
$totalcount = count_records_sql("SELECT COUNT(*) FROM $selectsql");
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
return get_records_sql("SELECT l.*, u.firstname, u.lastname, u.picture
|
2003-08-22 06:07:18 +00:00
|
|
|
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
|
|
|
/**
|
|
|
|
* select all log records for a given course and user
|
|
|
|
*
|
|
|
|
* select all log records for a given course and user
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
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) {
|
|
|
|
$courseselect = " AND course = '$courseid' ";
|
|
|
|
}
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
return get_records_sql("SELECT floor((`time` - $coursestart)/86400) as day, count(*) as num
|
|
|
|
FROM {$CFG->prefix}log
|
2002-12-23 09:39:26 +00:00
|
|
|
WHERE userid = '$userid'
|
2003-07-25 13:23:28 +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
|
|
|
/**
|
|
|
|
* select all log records for a given course, user, and day
|
|
|
|
*
|
|
|
|
* select all log records for a given course, user, and day
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
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) {
|
|
|
|
$courseselect = " AND course = '$courseid' ";
|
|
|
|
}
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
return get_records_sql("SELECT floor((`time` - $daystart)/3600) as hour, count(*) as num
|
|
|
|
FROM {$CFG->prefix}log
|
2002-12-23 09:39:26 +00:00
|
|
|
WHERE userid = '$userid'
|
2003-07-24 01:54:06 +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
|
|
|
|
*
|
|
|
|
* Returns information about failed login attempts. If the current user is
|
|
|
|
* an admin, then two numbers are returned: the number of attempts and the
|
|
|
|
* number of accounts. For non-admins, only the attempts on the given user
|
|
|
|
* are shown.
|
|
|
|
*
|
|
|
|
* @param mode - admin, teacher or everybody
|
|
|
|
* @param username - the username we are searching for
|
|
|
|
* @param lastlogin - the date from which we are searching
|
|
|
|
*/
|
|
|
|
|
|
|
|
function count_login_failures($mode, $username, $lastlogin) {
|
|
|
|
|
|
|
|
$select = "module='login' AND action='error' AND time > $lastlogin";
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
} else if ($mode == 'everybody' or ($mode == 'teacher' and isteacher())) {
|
|
|
|
if ($count->attempts = count_records_select('log', "$select AND info = '$username'")) {
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-03 15:31:30 +00:00
|
|
|
/// GENERAL HELPFUL THINGS ///////////////////////////////////
|
|
|
|
|
2003-09-14 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* dump a given object's information in a PRE block
|
|
|
|
*
|
|
|
|
* dump a given object's information in a PRE block
|
|
|
|
* Mostly just for debugging
|
|
|
|
*
|
2004-05-30 00:33:45 +00:00
|
|
|
* @param type description
|
2003-09-14 04:04:15 +00:00
|
|
|
*/
|
2003-01-03 15:31:30 +00:00
|
|
|
function print_object($object) {
|
|
|
|
|
2003-02-24 09:36:15 +00:00
|
|
|
echo "<PRE>";
|
|
|
|
print_r($object);
|
|
|
|
echo "</PRE>";
|
2003-01-03 15:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
|
2003-03-14 07:55:22 +00:00
|
|
|
// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
|
2002-12-17 04:41:18 +00:00
|
|
|
?>
|