";
foreach ($logs as $log) {
- if ($ld = get_record_sql("SELECT * FROM log_display WHERE module='$log->module' AND action='$log->action'")) {
+ if ($ld = get_record("log_display", "module", "$log->module", "action", "$log->action")) {
$log->info = get_field($ld->mtable, $ld->field, "id", $log->info);
}
@@ -179,11 +169,11 @@ function print_all_courses($category="all", $style="full", $maxcount=999, $width
global $CFG, $USER;
if ($category == "all") {
- $courses = get_records_sql("SELECT * FROM course WHERE category > 0 ORDER BY fullname ASC");
+ $courses = get_courses();
} else if ($category == "my") {
if (isset($USER->id)) {
- if ($courses = get_records_sql("SELECT * FROM course WHERE category > 0 ORDER BY fullname ASC")) {
+ if ($courses = get_courses()) {
foreach ($courses as $key => $course) {
if (!isteacher($course->id) and !isstudent($course->id)) {
unset($courses[$key]);
@@ -193,7 +183,7 @@ function print_all_courses($category="all", $style="full", $maxcount=999, $width
}
} else {
- $courses = get_records("course", "category", $category, "fullname ASC");
+ $courses = get_courses($category);
}
if ($style == "minimal") {
@@ -297,7 +287,7 @@ function print_recent_activity($course) {
echo "";
}
- if (! $logs = get_records_sql("SELECT * FROM log WHERE time > '$USER->lastlogin' AND course = '$course->id' ORDER BY time ASC")) {
+ if (! $logs = get_records_select("log", "time > '$USER->lastlogin' AND course = '$course->id'", "time ASC")) {
return;
}
@@ -400,11 +390,7 @@ function get_array_of_activities($courseid) {
$mod = array();
- if (!$rawmods = get_records_sql("SELECT cm.*, m.name as modname
- FROM modules m, course_modules cm
- WHERE cm.course = '$courseid'
- AND cm.deleted = '0'
- AND cm.module = m.id ") ) {
+ if (!$rawmods = get_course_mods($courseid)) {
return NULL;
}
@@ -435,7 +421,7 @@ function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modname
$modnamesplural= NULL; // all course module names (plural form)
$modnamesused = NULL; // course module names used
- if ($allmods = get_records_sql("SELECT * FROM modules") ) {
+ if ($allmods = get_records("modules")) {
foreach ($allmods as $mod) {
$modnames[$mod->name] = get_string("modulename", "$mod->name");
$modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
@@ -445,11 +431,7 @@ function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modname
error("No modules are installed!");
}
- if ($rawmods = get_records_sql("SELECT cm.*, m.name as modname
- FROM modules m, course_modules cm
- WHERE cm.course = '$courseid'
- AND cm.deleted = '0'
- AND cm.module = m.id ") ) {
+ if ($rawmods = get_course_mods($courseid)) {
foreach($rawmods as $mod) { // Index the mods
$mods[$mod->id] = $mod;
$mods[$mod->id]->modfullname = $modnames[$mod->modname];
@@ -459,17 +441,13 @@ function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modname
}
}
+
function get_all_sections($courseid) {
- return get_records_sql("SELECT section, id, course, summary, sequence
- FROM course_sections
- WHERE course = '$courseid'
- ORDER BY section");
+ return get_records("course_sections", "course", "$courseid", "sections",
+ "section, id, course, summary, sequence");
}
-function get_all_categories() {
- return get_records_sql("SELECT * FROM course_categories ORDER by name");
-}
function print_section_block($heading, $course, $section, $mods, $modnames, $modnamesused,
$absolute=true, $width="100%", $isediting=false) {
@@ -684,7 +662,7 @@ function print_course_categories($categories, $selected="none", $width=180) {
$strrequireskey = get_string("requireskey");
if ($selected == "index") { // Print comprehensive index of categories with courses
- if ($courses = get_records_sql("SELECT * FROM course WHERE category > 0 ORDER BY shortname")) {
+ if ($courses = get_courses()) {
if (isset($USER->id) and !isadmin()) {
print_simple_box_start("CENTER", "100%", $THEME->cellheading);
print_heading("".get_string("mycourses")."", "LEFT");
@@ -773,9 +751,7 @@ function add_mod_to_section($mod) {
// Returns the course_sections ID where the mod is inserted
GLOBAL $db;
- if ($section = get_record_sql("SELECT * FROM course_sections
- WHERE course = '$mod->course' AND section = '$mod->section'") ) {
-
+ if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
if ($section->sequence) {
$newsequence = "$section->sequence,$mod->coursemodule";
} else {
@@ -857,9 +833,8 @@ function move_module($cm, $move) {
return true;
} else { // Push onto end of previous section
$prevsectionnumber = $thissection->section - 1;
- if (! $prevsection = get_record_sql("SELECT * FROM course_sections
- WHERE course='$thissection->course'
- AND section='$prevsectionnumber' ")) {
+ if (! $prevsection = get_record("course_sections", "course", "$thissection->course",
+ "section", "$prevsectionnumber")) {
error("Previous section ($prevsection->id) doesn't exist");
}
@@ -902,9 +877,8 @@ function move_module($cm, $move) {
if ($last) {
$nextsectionnumber = $thissection->section + 1;
- if ($nextsection = get_record_sql("SELECT * FROM course_sections
- WHERE course='$thissection->course'
- AND section='$nextsectionnumber' ")) {
+ if ($nextsection = get_record("course_sections", "course", "$thissection->course",
+ "section", "$nextsectionnumber")) {
if ($nextsection->sequence) {
$newsequence = "$cm->id,$nextsection->sequence";
diff --git a/course/loggraph.php b/course/loggraph.php
index 58f77cad2b8..481b18f6eb6 100644
--- a/course/loggraph.php
+++ b/course/loggraph.php
@@ -63,12 +63,7 @@
$timestart = $timefinish;
}
- if ($rawlogs = get_records_sql("SELECT floor((`time` - $coursestart)/86400) as day,
- count(*) as num FROM log
- WHERE user = '$user->id'
- AND course = '$course->id'
- AND `time` > '$coursestart'
- GROUP BY day ")) {
+ if ($rawlogs = get_logs_usercourse($user->id, $course->id, $coursestart)) {
foreach ($rawlogs as $rawlog) {
$logs[$rawlog->day] = $rawlog->num;
}
@@ -116,12 +111,7 @@
$hours[$i] = userdate($hour, "$hh %p");
}
- if ($rawlogs = get_records_sql("SELECT floor((`time` - $daystart)/3600) as hour,
- count(*) as num FROM log
- WHERE user = '$user->id'
- AND course = '$course->id'
- AND `time` > '$daystart'
- GROUP BY hour ")) {
+ if ($rawlogs = get_logs_userday($user->id, $course->id, $daystart)) {
foreach ($rawlogs as $rawlog) {
$logs[$rawlog->hour] = $rawlog->num;
}
diff --git a/course/teacher.php b/course/teacher.php
index 9718df5f58c..4764e965e56 100644
--- a/course/teacher.php
+++ b/course/teacher.php
@@ -32,7 +32,7 @@
print_header("$site->shortname: $strassignteachers", "$site->fullname",
"wwwroot/admin\">$stradministration -> $strassignteachers");
- if ($courses = get_records_sql("SELECT * from course WHERE category > 0 ORDER BY fullname")) {
+ if ($courses = get_courses()) {
print_heading(get_string("choosecourse"));
print_simple_box_start("CENTER");
@@ -132,14 +132,9 @@
/// Print list of potential teachers
if ($search) {
- $users = get_records_sql("SELECT * from user WHERE confirmed = 1 AND deleted = 0
- AND (firstname LIKE '%$search%' OR
- lastname LIKE '%$search%' OR
- email LIKE '%$search%')
- AND username <> 'guest' AND username <> 'changeme'");
+ $users = get_users_search($search);
} else {
- $users = get_records_sql("SELECT * from user WHERE confirmed = 1 AND deleted = 0
- AND username <> 'guest' AND username <> 'changeme'");
+ $users = get_users_confirmed();
}
diff --git a/course/teachers.php b/course/teachers.php
index d81f8634115..fcf6275ba8e 100644
--- a/course/teachers.php
+++ b/course/teachers.php
@@ -33,7 +33,7 @@
}
foreach ($rank as $num => $vals) {
- if (! $teacher = get_record_sql("SELECT * FROM user_teachers WHERE course='$course->id' and user='$num'")) {
+ if (! $teacher = get_record("user_teachers", "course", "$course->id", "user", "$num")) {
error("No such teacher in course $course->shortname with user id $num");
}
$teacher->role = $vals[r];
diff --git a/course/view.php b/course/view.php
index 9cb0610a5ff..7c3dac1ce80 100644
--- a/course/view.php
+++ b/course/view.php
@@ -2,8 +2,8 @@
// Display the course home page.
- require("../config.php");
- require("lib.php");
+ include_once("../config.php");
+ include_once("lib.php");
optional_variable($id);
optional_variable($name);
diff --git a/index.php b/index.php
index 8133898ca79..a453883737e 100644
--- a/index.php
+++ b/index.php
@@ -47,7 +47,7 @@
}
if ($site->newsitems > 0 ) {
- $categories = get_all_categories();
+ $categories = get_categories();
if (count($categories) > 1) {
print_course_categories($categories, "none", $side);
} else {
@@ -73,7 +73,7 @@
if ($site->newsitems == 0 ) {
print_heading_block(get_string("availablecourses"));
print_spacer(8,1);
- $categories = get_all_categories();
+ $categories = get_categories();
if (count($categories) > 1) {
print_course_categories($categories, "index");
} else {
diff --git a/lang/en/moodle.php b/lang/en/moodle.php
index 6ba911c13fa..82b9803312d 100644
--- a/lang/en/moodle.php
+++ b/lang/en/moodle.php
@@ -111,6 +111,7 @@ $string['deletednot'] = "Could not delete \$a !";
$string['deletingcourse'] = "Deleting \$a";
$string['department'] = "Department";
$string['description'] = "Description";
+$string['displayingrecords'] = "Displaying \$a records";
$string['displayingusers'] = "Displaying users \$a->start to \$a->end";
$string['documentation'] = "Moodle Documentation";
$string['downloadexcel'] = "Download in Excel format";
diff --git a/lib/database.php b/lib/datalib.php
similarity index 64%
rename from lib/database.php
rename to lib/datalib.php
index 4ec59405f0f..1fdea66b1d8 100644
--- a/lib/database.php
+++ b/lib/datalib.php
@@ -69,11 +69,11 @@ function record_exists($table, $field="", $value="", $field2="", $value2="", $fi
global $CFG;
- if ($field and $value) {
+ if ($field) {
$select = "WHERE $field = '$value'";
- if ($field2 and $value2) {
+ if ($field2) {
$select .= " AND $field2 = '$value2'";
- if ($field3 and $value3) {
+ if ($field3) {
$select .= " AND $field3 = '$value3'";
}
}
@@ -105,11 +105,11 @@ function count_records($table, $field="", $value="", $field2="", $value2="", $fi
global $CFG;
- if ($field and $value) {
+ if ($field) {
$select = "WHERE $field = '$value'";
- if ($field2 and $value2) {
+ if ($field2) {
$select .= " AND $field2 = '$value2'";
- if ($field3 and $value3) {
+ if ($field3) {
$select .= " AND $field3 = '$value3'";
}
}
@@ -118,6 +118,15 @@ function count_records($table, $field="", $value="", $field2="", $value2="", $fi
return count_records_sql("SELECT COUNT(*) FROM $CFG->prefix$table $select");
}
+function count_records_select($table, $select="") {
+/// Get all the records and count them
+
+ global $CFG;
+
+ return count_records_sql("SELECT COUNT(*) FROM $CFG->prefix$table $select");
+}
+
+
function count_records_sql($sql) {
/// Get all the records and count them
/// The sql statement is provided as a string.
@@ -137,9 +146,9 @@ function get_record($table, $field, $value, $field2="", $value2="", $field3="",
$select = "WHERE $field = '$value'";
- if ($field2 and $value2) {
+ if ($field2) {
$select .= " AND $field2 = '$value2'";
- if ($field3 and $value3) {
+ if ($field3) {
$select .= " AND $field3 = '$value3'";
}
}
@@ -171,7 +180,7 @@ function get_records($table, $field="", $value="", $sort="", $fields="*") {
global $CFG;
- if ($field and $value) {
+ if ($field) {
$select = "WHERE $field = '$value'";
}
if ($sort) {
@@ -181,17 +190,32 @@ function get_records($table, $field="", $value="", $sort="", $fields="*") {
return get_records_sql("SELECT $fields FROM $CFG->prefix$table $select $sortorder");
}
-
-function get_records_list($table, $field="", $values="", $sort="", $fields="*") {
-// 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"
+function get_records_select($table, $select="", $sort="", $fields="*") {
+/// 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"
global $CFG;
- if ($field and $value) {
+ if ($sort) {
+ $sortorder = "ORDER BY $sort";
+ }
+
+ return get_records_sql("SELECT $fields FROM $CFG->prefix$table $select $sortorder");
+}
+
+
+function get_records_list($table, $field="", $values="", $sort="", $fields="*") {
+/// 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"
+
+ global $CFG;
+
+ if ($field) {
$select = "WHERE $field in ($values)";
}
if ($sort) {
@@ -202,10 +226,11 @@ function get_records_list($table, $field="", $values="", $sort="", $fields="*")
}
+
function get_records_sql($sql) {
-// 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.
+/// 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.
global $db;
@@ -226,11 +251,45 @@ function get_records_sql($sql) {
}
}
+function get_records_menu($table, $field="", $value="", $sort="", $fields="*") {
+/// 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"
+
+ global $CFG;
+
+ if ($field) {
+ $select = "WHERE $field = '$value'";
+ }
+ if ($sort) {
+ $sortorder = "ORDER BY $sort";
+ }
+
+ return get_records_sql_menu("SELECT $fields FROM $CFG->prefix$table $select $sortorder");
+}
+
+function get_records_select_menu($table, $select="", $sort="", $fields="*") {
+/// 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
+
+ global $CFG;
+
+ if ($sort) {
+ $sortorder = "ORDER BY $sort";
+ }
+
+ return get_records_sql_menu("SELECT $fields FROM $CFG->prefix$table $select $sortorder");
+}
+
+
function get_records_sql_menu($sql) {
-// 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.
+/// 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.
global $db;
@@ -278,11 +337,11 @@ function delete_records($table, $field="", $value="", $field2="", $value2="", $f
global $db, $CFG;
- if ($field and $value) {
+ if ($field) {
$select = "WHERE $field = '$value'";
- if ($field2 and $value2) {
+ if ($field2) {
$select .= " AND $field2 = '$value2'";
- if ($field3 and $value3) {
+ if ($field3) {
$select .= " AND $field3 = '$value3'";
}
}
@@ -482,7 +541,9 @@ function adminlogin($username, $md5password) {
global $CFG;
- return record_exists_sql("SELECT u.id FROM {$CFG->prefix}user u, {$CFG->prefix}user_admins a
+ return record_exists_sql("SELECT u.id
+ FROM {$CFG->prefix}user u,
+ {$CFG->prefix}user_admins a
WHERE u.id = a.user
AND u.username = '$username'
AND u.password = '$md5password'");
@@ -499,6 +560,31 @@ function get_site () {
}
}
+
+function get_courses($category=0, $sort="fullname ASC") {
+/// Returns list of courses
+
+ if ($category > 0) { // Return all courses in one category
+ return get_records("course", "category", $category, $sort);
+
+ } else if ($category < 0) { // Return all courses, even the site
+ return get_records("course", "", "", $sort);
+
+ } else { // Return all courses, except site
+ return get_records_select("course", "category > 0", $sort);
+ }
+}
+
+function get_categories() {
+ return get_records("course_categories", "", "", "name");
+}
+
+
+function get_guest() {
+ return get_user_info_from_db("username", "guest");
+}
+
+
function get_admin () {
/// Returns $user object of the main admin user
@@ -581,9 +667,82 @@ function get_course_users($courseid, $sort="u.lastaccess DESC") {
}
+function get_users_search($search, $sort="u.firstname ASC") {
+ global $CFG;
+
+ return get_records_sql("SELECT * from {$CFG->prefix}user
+ WHERE confirmed = 1
+ AND deleted = 0
+ AND (firstname LIKE '%$search%' OR
+ lastname LIKE '%$search%' OR
+ email LIKE '%$search%')
+ AND username <> 'guest'
+ AND username <> 'changeme'");
+}
+
+
+function get_users_count() {
+ return count_record_select("user", "username <> 'guest' AND deleted <> 1");
+}
+
+function get_users_listing($sort, $dir="ASC", $page=1, $recordsperpage=20) {
+ global $CFG;
+ return get_records_sql("SELECT id, username, email, firstname, lastname, city, country, lastaccess
+ FROM {$CFG->prefix}user
+ WHERE username <> 'guest'
+ AND deleted <> '1'
+ ORDER BY $sort $dir
+ LIMIT $page,$recordsperpage");
+
+}
+
+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'");
+}
+
+
+function get_users_unconfirmed($cutofftime=999999999) {
+ global $CFG;
+ return get_records_sql("SELECT *
+ FROM {$CFG->prefix}user
+ WHERE confirmed = 0
+ AND firstaccess > 0
+ AND firstaccess < '$cutofftime'");
+}
+
+
+function get_users_longtimenosee($cutofftime) {
+ global $CFG;
+ return get_records_sql("SELECT u.*
+ FROM {$CFG->prefix}user u,
+ {$CFG->prefix}user_students s
+ WHERE lastaccess > '0'
+ AND lastaccess < '$cutofftime'
+ AND u.id = s.user
+ GROUP BY u.id");
+}
+
+
/// MODULE FUNCTIONS /////////////////////////////////////////////////
+function get_course_mods($courseid) {
+/// Just gets a raw list of all modules in a course
+ global $CFG;
+
+ return get_records_sql("SELECT cm.*, m.name as modname
+ FROM {$CFG->prefix}modules m, {$CFG->prefix}course_modules cm
+ WHERE cm.course = '$courseid'
+ AND cm.deleted = '0'
+ AND cm.module = m.id ");
+}
+
function get_coursemodule_from_instance($modulename, $instance, $courseid) {
/// Given an instance of a module, finds the coursemodule description
@@ -619,4 +778,75 @@ function get_all_instances_in_course($modulename, $courseid, $sort="cw.section")
}
+
+/// LOG FUNCTIONS /////////////////////////////////////////////////////
+
+
+function add_to_log($course, $module, $action, $url="", $info="") {
+/// 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.
+///
+/// course = the course id
+/// module = forum, journal, resource, course, user etc
+/// action = view, edit, post (often but not always the same as the file.php)
+/// url = the file and parameters used to see the results of the action
+/// info = additional description information
+
+ global $db, $USER, $REMOTE_ADDR;
+
+ if (isset($USER->realuser)) { // Don't log
+ return;
+ }
+
+ $timenow = time();
+ $info = addslashes($info);
+
+ $result = $db->Execute("INSERT INTO log
+ SET time = '$timenow',
+ user = '$USER->id',
+ course = '$course',
+ ip = '$REMOTE_ADDR',
+ module = '$module',
+ action = '$action',
+ url = '$url',
+ info = '$info'");
+ if (!$result) {
+ echo "
Error: Could not insert a new entry to the Moodle log
"; // Don't throw an error
+ }
+}
+
+
+function get_logs($select, $order) {
+ global $CFG;
+
+ return get_records_sql("SELECT l.*, u.firstname, u.lastname, u.picture
+ FROM {$CFG->prefix}log l,
+ {$CFG->prefix}user u
+ $select $order");
+}
+
+function get_logs_usercourse($userid, $courseid, $coursestart) {
+ global $CFG;
+
+ return get_records_sql("SELECT floor((`time` - $coursestart)/86400) as day, count(*) as num
+ FROM {$CFG->prefix}log
+ WHERE user = '$userid'
+ AND course = '$courseid'
+ AND `time` > '$coursestart'
+ GROUP BY day ");
+}
+
+function get_logs_userday($userid, $courseid, $daystart) {
+ global $CFG;
+
+ return get_records_sql("SELECT floor((`time` - $daystart)/3600) as hour, count(*) as num
+ FROM {$CFG->prefix}log
+ WHERE user = '$userid'
+ AND course = '$courseid'
+ AND `time` > '$daystart'
+ GROUP BY hour ");
+}
+
+
?>
diff --git a/lib/db/mysql.sql b/lib/db/mysql.sql
index 724495f6433..f2e134fd5d6 100644
--- a/lib/db/mysql.sql
+++ b/lib/db/mysql.sql
@@ -14,7 +14,7 @@
# Table structure for table `config`
#
-CREATE TABLE `config` (
+CREATE TABLE `prefix_config` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '',
`value` varchar(255) NOT NULL default '',
@@ -27,7 +27,7 @@ CREATE TABLE `config` (
# Table structure for table `course`
#
-CREATE TABLE `course` (
+CREATE TABLE `prefix_course` (
`id` int(10) unsigned NOT NULL auto_increment,
`category` int(10) unsigned NOT NULL default '0',
`password` varchar(50) NOT NULL default '',
@@ -56,7 +56,7 @@ CREATE TABLE `course` (
# Table structure for table `course_categories`
#
-CREATE TABLE `course_categories` (
+CREATE TABLE `prefix_course_categories` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`),
@@ -68,7 +68,7 @@ CREATE TABLE `course_categories` (
# Table structure for table `course_modules`
#
-CREATE TABLE `course_modules` (
+CREATE TABLE `prefix_course_modules` (
`id` int(10) unsigned NOT NULL auto_increment,
`course` int(10) unsigned NOT NULL default '0',
`module` int(10) unsigned NOT NULL default '0',
@@ -86,7 +86,7 @@ CREATE TABLE `course_modules` (
# Table structure for table `course_sections`
#
-CREATE TABLE `course_sections` (
+CREATE TABLE `prefix_course_sections` (
`id` int(10) unsigned NOT NULL auto_increment,
`course` int(10) unsigned NOT NULL default '0',
`section` int(10) unsigned NOT NULL default '0',
@@ -100,7 +100,7 @@ CREATE TABLE `course_sections` (
# Table structure for table `log`
#
-CREATE TABLE `log` (
+CREATE TABLE `prefix_log` (
`id` int(10) unsigned NOT NULL auto_increment,
`time` int(10) unsigned NOT NULL default '0',
`user` int(10) unsigned NOT NULL default '0',
@@ -118,7 +118,7 @@ CREATE TABLE `log` (
# Table structure for table `log_display`
#
-CREATE TABLE `log_display` (
+CREATE TABLE `prefix_log_display` (
`module` varchar(20) NOT NULL default '',
`action` varchar(20) NOT NULL default '',
`mtable` varchar(20) NOT NULL default '',
@@ -130,7 +130,7 @@ CREATE TABLE `log_display` (
# Table structure for table `modules`
#
-CREATE TABLE `modules` (
+CREATE TABLE `prefix_modules` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(20) NOT NULL default '',
`version` int(10) NOT NULL default '0',
@@ -146,7 +146,7 @@ CREATE TABLE `modules` (
# Table structure for table `user`
#
-CREATE TABLE `user` (
+CREATE TABLE `prefix_user` (
`id` int(10) unsigned NOT NULL auto_increment,
`confirmed` tinyint(1) NOT NULL default '0',
`deleted` tinyint(1) NOT NULL default '0',
@@ -189,7 +189,7 @@ CREATE TABLE `user` (
# Table structure for table `user_admins`
#
-CREATE TABLE `user_admins` (
+CREATE TABLE `prefix_user_admins` (
`id` int(10) unsigned NOT NULL auto_increment,
`user` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`id`),
@@ -201,7 +201,7 @@ CREATE TABLE `user_admins` (
# Table structure for table `user_students`
#
-CREATE TABLE `user_students` (
+CREATE TABLE `prefix_user_students` (
`id` int(10) unsigned NOT NULL auto_increment,
`user` int(10) unsigned NOT NULL default '0',
`course` int(10) unsigned NOT NULL default '0',
@@ -217,7 +217,7 @@ CREATE TABLE `user_students` (
# Table structure for table `user_teachers`
#
-CREATE TABLE `user_teachers` (
+CREATE TABLE `prefix_user_teachers` (
`id` int(10) unsigned NOT NULL auto_increment,
`user` int(10) unsigned NOT NULL default '0',
`course` int(10) unsigned NOT NULL default '0',
diff --git a/lib/moodlelib.php b/lib/moodlelib.php
index 1bf01f178fa..b72d0849fbe 100644
--- a/lib/moodlelib.php
+++ b/lib/moodlelib.php
@@ -1,493 +1,81 @@
dirroot/theme/$CFG->theme/styles.php")) {
- $styles = $CFG->stylesheet;
+function require_variable($var) {
+/// Variable must be present
+ if (! isset($var)) {
+ error("A required parameter was missing");
+ }
+}
+
+function optional_variable(&$var, $default=0) {
+/// Variable may be present, if not then set a default
+ if (! isset($var)) {
+ $var = $default;
+ }
+}
+
+
+function set_config($name, $value) {
+/// No need for get_config because they are usually always available in $CFG
+
+ if (get_field("config", "name", "name", $name)) {
+ return set_field("config", "value", $value, "name", $name);
} else {
- $styles = "$CFG->wwwroot/theme/standard/styles.php";
- }
-
- if ($navigation == "home") {
- $home = true;
- $navigation = "";
- }
-
- if ($button == "") {
- $button = " ";
- }
-
- if (!$menu and $navigation) {
- if (isset($USER->id)) {
- $menu = "wwwroot/login/logout.php\">".get_string("logout")."";
- } else {
- $menu = "wwwroot/login/index.php\">".get_string("login")."";
- }
- }
-
- // Specify character set ... default is iso-8859-1 but some languages might need something else
- // Could be optimised by carrying the charset variable around in $USER
- if (current_language() == "en") {
- $meta .= "\n";
- } else {
- $meta .= "\n";
- }
-
- if ($CFG->langdir == "RTL") {
- $direction = " DIR=\"RTL\"";
- } else {
- $direction = " DIR=\"LTR\"";
- }
-
- if (!$cache) { // Do everything we can to prevent clients and proxies caching
- @header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
- @header("Pragma: no-cache");
- $meta .= "\n";
- $meta .= "\n";
- }
-
- include ("$CFG->dirroot/theme/$CFG->theme/header.html");
-}
-
-function print_footer ($course=NULL) {
-// Can provide a course object to make the footer contain a link to
-// to the course home page, otherwise the link will go to the site home
- global $USER, $CFG, $THEME;
-
-
-/// Course links
- if ($course) {
- if ($course == "home") { // special case for site home page - please do not remove
- $homelink = "
\n";
- print_simple_box_end();
-
- return true;
-}
-
-function print_editing_switch($courseid) {
- global $CFG, $USER;
-
- if (isteacher($courseid)) {
- if ($USER->editing) {
- echo "wwwroot/course/view.php?id=$courseid&edit=off\">Turn editing off";
- } else {
- echo "wwwroot/course/view.php?id=$courseid&edit=on\">Turn editing on";
- }
- }
-}
-
-function format_float($num, $places=0) {
- return sprintf("%.$places"."f", $num);
-}
-
-function print_textarea($richedit, $rows, $cols, $width, $height, $name, $value="") {
- global $CFG, $THEME;
-
- if ($richedit) {
- echo "\n";
- echo "\n";
- } else {
- echo "\n";
- }
-}
-
-function print_richedit_javascript($form, $name, $source="no") {
- echo "";
-}
-
-
-function update_course_icon($courseid) {
-// Used to be an icon, but it's now a simple form button
- global $CFG, $USER;
-
- if (isteacher($courseid)) {
- if ($USER->editing) {
- $string = get_string("turneditingoff");
- $edit = "off";
- } else {
- $string = get_string("turneditingon");
- $edit = "on";
- }
- return "";
- }
-}
-
-function update_module_button($moduleid, $courseid, $string) {
-// Prints the editing button on a module "view" page
- global $CFG;
-
- if (isteacher($courseid)) {
- $string = get_string("updatethis", "", $string);
- return "";
+ $config->name = $name;
+ $config->value = $value;
+ return insert_record("config", $config);
}
}
-function navmenu($course, $cm=NULL) {
-// Given a course and a (current) coursemodule
-// This function returns a small popup menu with all the
-// course activity modules in it, as a navigation menu
-// The data is taken from the serialised array stored in
-// the course record
-
- global $CFG;
-
- if ($cm) {
- $cm = $cm->id;
- }
-
- if ($course->format == 'weeks') {
- $strsection = get_string("week");
- } else {
- $strsection = get_string("topic");
- }
-
- if (!$modinfo = unserialize($course->modinfo)) {
- return "";
- }
- $section = -1;
- $selected = "";
- foreach ($modinfo as $mod) {
- if ($mod->section > 0 and $section <> $mod->section) {
- $menu[] = "-------------- $strsection $mod->section --------------";
- }
- $section = $mod->section;
- $url = "$mod->mod/view.php?id=$mod->cm";
- if ($cm == $mod->cm) {
- $selected = $url;
- }
- $mod->name = urldecode($mod->name);
- if (strlen($mod->name) > 55) {
- $mod->name = substr($mod->name, 0, 50)."...";
- }
- $menu[$url] = $mod->name;
- }
-
- return popup_form("$CFG->wwwroot/mod/", $menu, "navmenu", $selected, get_string("jumpto"), "", "", true);
-}
-
-
-function print_date_selector($day, $month, $year, $currenttime=0) {
-// Currenttime is a default timestamp in GMT
-// Prints form items with the names $day, $month and $year
-
- if (!$currenttime) {
- $currenttime = time();
- }
- $currentdate = usergetdate($currenttime);
-
- for ($i=1; $i<=31; $i++) {
- $days[$i] = "$i";
- }
- for ($i=1; $i<=12; $i++) {
- $months[$i] = date("F", mktime(0,0,0,$i,1,2000));
- }
- for ($i=2000; $i<=2010; $i++) {
- $years[$i] = $i;
- }
- choose_from_menu($days, $day, $currentdate[mday], "");
- choose_from_menu($months, $month, $currentdate[mon], "");
- choose_from_menu($years, $year, $currentdate[year], "");
-}
-
-function print_time_selector($hour, $minute, $currenttime=0) {
-// Currenttime is a default timestamp in GMT
-// Prints form items with the names $hour and $minute
-
- if (!$currenttime) {
- $currenttime = time();
- }
- $currentdate = usergetdate($currenttime);
- for ($i=0; $i<=23; $i++) {
- $hours[$i] = sprintf("%02d",$i);
- }
- for ($i=0; $i<=59; $i++) {
- $minutes[$i] = sprintf("%02d",$i);
- }
- choose_from_menu($hours, $hour, $currentdate[hours], "");
- choose_from_menu($minutes, $minute, $currentdate[minutes], "");
-}
+/// FUNCTIONS FOR HANDLING TIME ////////////////////////////////////////////
function make_timestamp($year, $month=1, $day=1, $hour=0, $minute=0, $second=0) {
-// Given date parts in user time, produce a GMT timestamp
+/// Given date parts in user time, produce a GMT timestamp
- return mktime((int)$hour,(int)$minute,(int)$second,(int)$month,(int)$day,(int)$year);
+ return mktime((int)$hour,(int)$minute,(int)$second,(int)$month,(int)$day,(int)$year);
}
-
function format_time($totalsecs, $str=NULL) {
-// Given an amount of time in seconds, prints it
-// nicely as months, days, hours etc as needed
+/// Given an amount of time in seconds, returns string
+/// formatted nicely as months, days, hours etc as needed
$totalsecs = abs($totalsecs);
@@ -527,12 +115,12 @@ function format_time($totalsecs, $str=NULL) {
}
function userdate($date, $format="", $timezone=99) {
-// Returns a formatted string that represents a date in user time
-// WARNING: note that the format is for strftime(), not date().
-// Because of a bug in most Windows time libraries, we can't use
-// the nicer %e, so we have to use %d which has leading zeroes.
-// A lot of the fuss below is just getting rid of these leading
-// zeroes as efficiently as possible.
+/// Returns a formatted string that represents a date in user time
+/// WARNING: note that the format is for strftime(), not date().
+/// Because of a bug in most Windows time libraries, we can't use
+/// the nicer %e, so we have to use %d which has leading zeroes.
+/// A lot of the fuss below is just getting rid of these leading
+/// zeroes as efficiently as possible.
global $USER;
@@ -561,7 +149,7 @@ function userdate($date, $format="", $timezone=99) {
} else {
if ($fixday) {
$datestring = gmstrftime($formatnoday, $date + (int)($timezone * 3600));
- $daystring = str_replace(" 0", "", strftime(" %d", $date));
+ $daystring = str_replace(" 0", "", gmstrftime(" %d", $date));
$datestring = str_replace("DD", $daystring, $datestring);
} else {
$datestring = gmstrftime($format, $date + (int)($timezone * 3600));
@@ -572,8 +160,8 @@ function userdate($date, $format="", $timezone=99) {
}
function usergetdate($date, $timezone=99) {
-// Given a $date timestamp in GMT, returns an array
-// that represents the date in user time
+/// Given a $date timestamp in GMT, returns an array
+/// that represents the date in user time
global $USER;
@@ -599,8 +187,8 @@ function usergetdate($date, $timezone=99) {
}
function usertime($date, $timezone=99) {
-// Given a GMT timestamp (seconds since epoch), offsets it by
-// the timezone. eg 3pm in India is 3pm GMT - 7 * 3600 seconds
+/// Given a GMT timestamp (seconds since epoch), offsets it by
+/// the timezone. eg 3pm in India is 3pm GMT - 7 * 3600 seconds
global $USER;
if ($timezone == 99) {
@@ -613,8 +201,8 @@ function usertime($date, $timezone=99) {
}
function usergetmidnight($date, $timezone=99) {
-// Given a time, return the GMT timestamp of the most recent midnight
-// for the current user.
+/// Given a time, return the GMT timestamp of the most recent midnight
+/// for the current user.
global $USER;
if ($timezone == 99) {
@@ -633,7 +221,7 @@ function usergetmidnight($date, $timezone=99) {
}
function usertimezone($timezone=99) {
-// returns a string that prints the user's timezone
+/// Returns a string that prints the user's timezone
global $USER;
if ($timezone == 99) {
@@ -653,552 +241,12 @@ function usertimezone($timezone=99) {
}
-function error ($message, $link="") {
- global $CFG, $SESSION;
-
- print_header(get_string("error"));
- echo " ";
- print_simple_box($message, "center", "", "#FFBBBB");
-
- if (!$link) {
- if ( !empty($SESSION->fromurl) ) {
- $link = "$SESSION->fromurl";
- unset($SESSION->fromurl);
- save_session("SESSION");
- } else {
- $link = "$CFG->wwwroot";
- }
- }
- print_continue($link);
- print_footer();
- die;
-}
-
-function helpbutton ($page, $title="", $module="moodle", $image=true, $linktext=false, $text="") {
- // $page = the keyword that defines a help page
- // $title = the title of links, rollover tips, alt tags etc
- // $module = which module is the page defined in
- // $image = use a help image for the link? (true/false/"both")
- // $text = if defined then this text is used in the page, and
- // the $page variable is ignored.
- global $CFG;
-
- if ($module == "") {
- $module = "moodle";
- }
-
- if ($image) {
- if ($linktext) {
- $linkobject = "$titlewwwroot/pix/help.gif\">";
- } else {
- $linkobject = "wwwroot/pix/help.gif\">";
- }
- } else {
- $linkobject = $title;
- }
- if ($text) {
- $url = "/help.php?module=$module&text=".htmlentities(urlencode($text));
- } else {
- $url = "/help.php?module=$module&file=$page.html";
- }
- link_to_popup_window ($url, "popup", $linkobject, 400, 500, $title);
-}
-
-function notice ($message, $link="") {
- global $THEME, $HTTP_REFERER;
-
- if (!$link) {
- $link = $HTTP_REFERER;
- }
-
- echo " ";
- print_simple_box($message, "center", "", "$THEME->cellheading");
- print_heading("".get_string("continue")."");
- print_footer(get_site());
- die;
-}
-
-function notice_yesno ($message, $linkyes, $linkno) {
- global $THEME;
-
- print_simple_box_start("center", "", "$THEME->cellheading");
- echo "
";
- }
- return false;
- }
-}
-
-function modify_database($sqlfile) {
-// Assumes that the input text file 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)
-
-
- if (file_exists($sqlfile)) {
- $success = true;
- $lines = file($sqlfile);
- $command = "";
-
- while ( list($i, $line) = each($lines) ) {
- $line = chop($line);
- $length = strlen($line);
-
- if ($length && substr($line, 0, 1) <> "#") {
- if (substr($line, $length-1, 1) == ";") {
- $line = substr($line, 0, $length-1); // strip ;
- $command .= $line;
- if (! execute_sql($command)) {
- $success = false;
- }
- $command = "";
- } else {
- $command .= $line;
- }
- }
- }
-
- } else {
- $success = false;
- echo "
Tried to modify database, but \"$sqlfile\" doesn't exist!
";
- }
-
- return $success;
-}
-
-
-function record_exists($table, $field, $value) {
- global $db;
-
- $rs = $db->Execute("SELECT * FROM $table WHERE $field = '$value' LIMIT 1");
- if (!$rs) return false;
-
- if ( $rs->RecordCount() ) {
- return true;
- } else {
- return false;
- }
-}
-
-function record_exists_sql($sql) {
- global $db;
-
- $rs = $db->Execute($sql);
- if (!$rs) return false;
-
- if ( $rs->RecordCount() ) {
- return true;
- } else {
- return false;
- }
-}
-
-
-function count_records($table, $selector, $value) {
-// Get all the records and count them
- global $db;
-
- $rs = $db->Execute("SELECT COUNT(*) FROM $table WHERE $selector = '$value'");
- if (!$rs) return 0;
-
- return $rs->fields[0];
-}
-
-function count_records_sql($sql) {
-// Get all the records and count them
- global $db;
-
- $rs = $db->Execute("$sql");
- if (!$rs) return 0;
-
- return $rs->fields[0];
-}
-
-function get_record($table, $selector, $value) {
-// Get a single record as an object
- global $db;
-
- $rs = $db->Execute("SELECT * FROM $table WHERE $selector = '$value'");
- if (!$rs) return false;
-
- if ( $rs->RecordCount() == 1 ) {
- return (object)$rs->fields;
- } else {
- return false;
- }
-}
-
-function get_record_sql($sql) {
-// Get a single record as an object
-// The sql statement is provided as a string.
-
- global $db;
-
- $rs = $db->Execute("$sql");
- if (!$rs) return false;
-
- if ( $rs->RecordCount() == 1 ) {
- return (object)$rs->fields;
- } else {
- return false;
- }
-}
-
-function get_records($table, $selector, $value, $sort="", $fields="*") {
-// 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"
- global $db;
-
- if ($sort) {
- $sortorder = "ORDER BY $sort";
- }
- $sql = "SELECT $fields FROM $table WHERE $selector = '$value' $sortorder";
-
- return get_records_sql($sql);
-}
-
-
-function get_records_list($table, $selector, $values, $sort="", $fields="*") {
-// 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"
- global $db;
-
- if ($sort) {
- $sortorder = "ORDER BY $sort";
- }
- $sql = "SELECT $fields FROM $table WHERE $selector in ($values) $sortorder";
-
- return get_records_sql($sql);
-}
-
-
-function get_records_sql($sql) {
-// 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.
-
- global $db;
-
- $rs = $db->Execute("$sql");
- if (!$rs) return false;
-
- 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;
- }
-}
-
-function get_records_sql_menu($sql) {
-// 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.
-
- global $db;
-
- $rs = $db->Execute("$sql");
- if (!$rs) return false;
-
- if ( $rs->RecordCount() > 0 ) {
- while (!$rs->EOF) {
- $menu[$rs->fields[0]] = $rs->fields[1];
- $rs->MoveNext();
- }
- return $menu;
-
- } else {
- return false;
- }
-}
-
-function get_field($table, $field, $selector, $value) {
- global $db;
-
- $rs = $db->Execute("SELECT $field FROM $table WHERE $selector = '$value'");
- if (!$rs) return false;
-
- if ( $rs->RecordCount() == 1 ) {
- return $rs->fields["$field"];
- } else {
- return false;
- }
-}
-
-function set_field($table, $field, $newvalue, $selector, $value) {
- global $db;
-
- return $db->Execute("UPDATE $table SET $field = '$newvalue' WHERE $selector = '$value'");
-}
-
-function set_config($name, $value) {
-// No need for get_config because they are usually always available in $CFG
-
- if (get_field("config", "name", "name", $name)) {
- return set_field("config", "value", $value, "name", $name);
- } else {
- $config->name = $name;
- $config->value = $value;
- return insert_record("config", $config);
- }
-}
-
-function delete_records($table, $selector, $value) {
-// Delete one or more records from a table
- global $db;
-
- return $db->Execute("DELETE FROM $table WHERE $selector = '$value'");
-}
-
-function insert_record($table, $dataobject) {
-// Insert a record into a table and return the "id" field
-// $dataobject is an object containing needed data
-
- global $db;
-
- // Determine all the fields needed
- if (! $columns = $db->MetaColumns("$table")) {
- return false;
- }
-
- $data = (array)$dataobject;
-
- // Pull out data matching these fields
- foreach ($columns as $column) {
- if ($column->name <> "id" && isset($data[$column->name]) ) {
- $ddd[$column->name] = $data[$column->name];
- }
- }
-
- // Construct SQL queries
- if (! $numddd = count($ddd)) {
- return 0;
- }
-
- $count = 0;
- $insert = "";
- $select = "";
-
- foreach ($ddd as $key => $value) {
- $count++;
- $insert .= "$key = '$value'";
- $select .= "$key = '$value'";
- if ($count < $numddd) {
- $insert .= ", ";
- $select .= " AND ";
- }
- }
-
- if (! $rs = $db->Execute("INSERT INTO $table SET $insert")) {
- return false;
- }
-
- // Pull it out again to find the id. This is the most cross-platform method.
- if ($rs = $db->Execute("SELECT id FROM $table WHERE $select")) {
- return $rs->fields[0];
- } else {
- return false;
- }
-}
-
-
-function update_record($table, $dataobject) {
-// Update a record in a table
-// $dataobject is an object containing needed data
-
- global $db;
-
- if (! isset($dataobject->id) ) {
- return false;
- }
-
- // Determine all the fields in the table
- if (!$columns = $db->MetaColumns($table)) {
- return false;
- }
- $data = (array)$dataobject;
-
- // Pull out data matching these fields
- foreach ($columns as $column) {
- if ($column->name <> "id" && isset($data[$column->name]) ) {
- $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 $table SET $update WHERE id = '$dataobject->id'")) {
- return true;
- } else {
- return false;
- }
-}
-
-
-function print_object($object) {
-// Mostly just for debugging
-
- $array = (array)$object;
- foreach ($array as $key => $item) {
- echo "$key -> $item ";
- }
-}
-
-
-/// USER DATABASE ////////////////////////////////////////////////
-
-function get_user_info_from_db($field, $value) {
-
- global $db;
-
- if (!$field || !$value)
- return false;
-
- if (! $result = $db->Execute("SELECT * FROM user WHERE $field = '$value' AND deleted <> '1'")) {
- error("Could not find any active users!");
- }
-
- if ( $result->RecordCount() == 1 ) {
- $user = (object)$result->fields;
-
- $rs = $db->Execute("SELECT course FROM user_students WHERE user = '$user->id' ");
- while (!$rs->EOF) {
- $course = $rs->fields["course"];
- $user->student["$course"] = true;
- $rs->MoveNext();
- }
-
- $rs = $db->Execute("SELECT course FROM user_teachers WHERE user = '$user->id' ");
- while (!$rs->EOF) {
- $course = $rs->fields["course"];
- $user->teacher["$course"] = true;
- $rs->MoveNext();
- }
-
- $rs = $db->Execute("SELECT * FROM user_admins WHERE user = '$user->id' ");
- while (!$rs->EOF) {
- $user->admin = true;
- $rs->MoveNext();
- }
-
- if ($course = get_site()) {
- // Everyone is always a member of the top course
- $user->student["$course->id"] = true;
- }
-
- return $user;
-
- } else {
- return false;
- }
-}
-
-function update_user_in_db() {
-
- global $db, $USER, $REMOTE_ADDR;
-
- if (!isset($USER->id))
- return false;
-
- $timenow = time();
- if ($db->Execute("UPDATE user SET lastIP='$REMOTE_ADDR', lastaccess='$timenow' WHERE id = '$USER->id' ")) {
- return true;
- } else {
- return false;
- }
-}
+/// USER AUTHENTICATION AND LOGIN ////////////////////////////////////////
function require_login($courseid=0) {
-// This function checks that the current user is logged in, and optionally
-// whether they are "logged in" or allowed to be in a particular course.
-// If not, then it redirects them to the site login or course enrolment.
+/// This function checks that the current user is logged in, and optionally
+/// whether they are "logged in" or allowed to be in a particular course.
+/// If not, then it redirects them to the site login or course enrolment.
global $CFG, $SESSION, $USER, $FULLME, $HTTP_REFERER, $PHPSESSID;
@@ -1263,8 +311,9 @@ function require_login($courseid=0) {
}
-
function update_login_count() {
+/// Keeps track of login attempts
+
global $SESSION;
$max_logins = 10;
@@ -1283,74 +332,30 @@ function update_login_count() {
}
}
-function remove_admin($user) {
- global $db;
+function reset_login_count() {
+/// Resets login attempts
+ global $SESSION;
- return $db->Execute("DELETE FROM user_admins WHERE user = '$user'");
+ $SESSION->logincount = 0;
+ save_session("SESSION");
}
-function remove_teacher($user, $course=0) {
- global $db;
-
- if ($course) {
- /// First delete any crucial stuff that might still send mail
- if ($forums = get_records("forum", "course", $course)) {
- foreach ($forums as $forum) {
- $db->Execute("DELETE FROM forum_subscriptions WHERE forum = '$forum->id' AND user = '$user'");
- }
- }
- return $db->Execute("DELETE FROM user_teachers WHERE user = '$user' AND course = '$course'");
- } else {
- delete_records("forum_subscriptions", "user", $user);
- return delete_records("user_teachers", "user", $user);
- }
-}
-
-
-function enrol_student($user, $course) {
- global $db;
-
- $timenow = time();
-
- $rs = $db->Execute("INSERT INTO user_students (user, course, start, end, time)
- VALUES ($user, $course, 0, 0, $timenow)");
- if ($rs) {
- return true;
- } else {
- return false;
- }
-}
-
-function unenrol_student($user, $course=0) {
- global $db;
-
- if ($course) {
- /// First delete any crucial stuff that might still send mail
- if ($forums = get_records("forum", "course", $course)) {
- foreach ($forums as $forum) {
- $db->Execute("DELETE FROM forum_subscriptions WHERE forum = '$forum->id' AND user = '$user'");
- }
- }
- return $db->Execute("DELETE FROM user_students WHERE user = '$user' AND course = '$course'");
-
- } else {
- delete_records("forum_subscriptions", "user", $user);
- return delete_records("user_students", "user", $user);
- }
-}
function isadmin($userid=0) {
+/// Is the user an admin?
global $USER;
if (!$userid) {
- return record_exists_sql("SELECT * FROM user_admins WHERE user='{$USER->id}'");
+ return record_exists("user_admins", "user", $USER->id);
}
- return record_exists_sql("SELECT * FROM user_admins WHERE user='$userid'");
+ return record_exists("user_admins", "user", $userid);
}
+
function isteacher($courseid, $userid=0) {
+/// Is the user a teacher or admin?
global $USER;
if (isadmin($userid)) { // admins can do anything the teacher can
@@ -1361,11 +366,12 @@ function isteacher($courseid, $userid=0) {
return $USER->teacher[$courseid];
}
- return record_exists_sql("SELECT * FROM user_teachers WHERE user='$userid' AND course='$courseid'");
+ return record_exists("user_teachers", "user", $userid, "course", $courseid);
}
function isstudent($courseid, $userid=0) {
+/// Is the user a student in this course?
global $USER;
if (!$userid) {
@@ -1374,20 +380,23 @@ function isstudent($courseid, $userid=0) {
$timenow = time(); // todo: add time check below
- return record_exists_sql("SELECT * FROM user_students WHERE user='$userid' AND course='$courseid'");
+ return record_exists("user_students", "user", $userid, "course", $courseid);
}
function isguest($userid=0) {
+/// Is the user a guest?
global $USER;
if (!$userid) {
return ($USER->username == "guest");
}
- return record_exists_sql("SELECT * FROM user WHERE id='$userid' AND username = 'guest' ");
+ return record_exists("user", "id", $userid, "username", "guest");
}
+
function isediting($courseid, $user=NULL) {
+/// Is the current user in editing mode?
global $USER;
if (!$user){
$user = $USER;
@@ -1395,15 +404,9 @@ function isediting($courseid, $user=NULL) {
return ($user->editing and isteacher($courseid, $user->id));
}
-function reset_login_count() {
- global $SESSION;
-
- $SESSION->logincount = 0;
- save_session("SESSION");
-}
-
function set_moodle_cookie($thing) {
+/// Sets a moodle cookie with an encrypted string
$days = 60;
$seconds = 60*60*24*$days;
@@ -1414,21 +417,22 @@ function set_moodle_cookie($thing) {
function get_moodle_cookie() {
+/// Gets a moodle cookie with an encrypted string
global $MOODLEID;
return rc4decrypt($MOODLEID);
}
function save_session($VAR) {
-// Copies temporary session variable to permanent sesson variable
-// eg $_SESSION["USER"] = $USER;
+/// Copies temporary session variable to permanent session variable
+/// eg $_SESSION["USER"] = $USER;
global $$VAR;
$_SESSION[$VAR] = $$VAR;
}
function create_user_record($username, $password) {
-// Creates a bare-bones user record
+/// Creates a bare-bones user record
global $REMOTE_ADDR, $CFG;
if (function_exists(auth_get_userinfo)) {
@@ -1453,12 +457,12 @@ function create_user_record($username, $password) {
}
function authenticate_user_login($username, $password) {
-// Given a username and password, this function looks them
-// up using the currently selected authentication mechanism,
-// and if the authentication is successful, it returns a
-// valid $user object from the 'user' table.
-//
-// Uses auth_ functions from the currently active auth module
+/// Given a username and password, this function looks them
+/// up using the currently selected authentication mechanism,
+/// and if the authentication is successful, it returns a
+/// valid $user object from the 'user' table.
+///
+/// Uses auth_ functions from the currently active auth module
global $CFG;
@@ -1476,10 +480,7 @@ function authenticate_user_login($username, $password) {
// Doing this first (even though it's less efficient) because
// the chosen authentication method might hang and lock the
// admin out.
- if ($user = get_record_sql("SELECT u.id FROM user u, user_admins a
- WHERE u.id = a.user
- AND u.username = '$username'
- AND u.password = '$md5password'")) {
+ if (adminlogin($username, $md5password)) {
return get_user_info_from_db("username", $username);
}
@@ -1503,128 +504,76 @@ function authenticate_user_login($username, $password) {
}
-function get_site () {
-// Returns $course object of the top-level site.
- if ( $course = get_record("course", "category", 0)) {
- return $course;
- } else {
- return false;
- }
+function enrol_student($user, $course) {
+/// Enrols a student in a given course
+ global $db;
+
+ $record->user = $user;
+ $record->course = $course;
+ $record->start = 0;
+ $record->end = 0;
+ $record->time = time();
+
+ return insert_record("user", $record);
}
-function get_admin () {
-// Returns $user object of the main admin user
+function unenrol_student($user, $course=0) {
+/// Unenrols a student from a given course
+ global $db;
- if ( $admins = get_records_sql("SELECT u.* FROM user u, user_admins a WHERE a.user = u.id ORDER BY u.id ASC")) {
- foreach ($admins as $admin) {
- return $admin; // ie the first one
- }
- } else {
- return false;
- }
-}
-
-function get_admins() {
- return get_records_sql("SELECT u.* FROM user u, user_admins a
- WHERE a.user = u.id
- ORDER BY u.id ASC");
-}
-
-
-function get_teacher($courseid) {
-// Returns $user object of the main teacher for a course
- if ( $teachers = get_records_sql("SELECT u.* FROM user u, user_teachers t
- WHERE t.user = u.id AND t.course = '$courseid'
- ORDER BY t.authority ASC")) {
- foreach ($teachers as $teacher) {
- if ($teacher->authority) {
- return $teacher; // the highest authority teacher
+ if ($course) {
+ /// First delete any crucial stuff that might still send mail
+ if ($forums = get_records("forum", "course", $course)) {
+ foreach ($forums as $forum) {
+ delete_records("forum_subscriptions", "forum", $forum->id, "user", $user);
}
}
+ return delete_records("user_students", "user", $user, "course", $course);
+
} else {
- return false;
+ delete_records("forum_subscriptions", "user", $user);
+ return delete_records("user_students", "user", $user);
}
}
-function get_course_students($courseid, $sort="u.lastaccess DESC") {
- return get_records_sql("SELECT u.* FROM user u, user_students s
- WHERE s.course = '$courseid' AND s.user = u.id AND u.deleted = '0'
- ORDER BY $sort");
-}
+function remove_teacher($user, $course=0) {
+/// Removes a teacher from a given course (or ALL courses)
+/// Does not delete the user account
+ global $db;
-function get_course_teachers($courseid, $sort="t.authority ASC") {
- return get_records_sql("SELECT u.*,t.authority,t.role FROM user u, user_teachers t
- WHERE t.course = '$courseid' AND t.user = u.id AND u.deleted = '0'
- ORDER BY $sort");
-}
-
-function get_course_users($courseid, $sort="u.lastaccess DESC") {
-// Using this method because the direct SQL just would not always work!
-
- $teachers = get_course_teachers($courseid, $sort);
- $students = get_course_students($courseid, $sort);
-
- if ($teachers and $students) {
- return array_merge($teachers, $students);
- } else if ($teachers) {
- return $teachers;
+ if ($course) {
+ /// First delete any crucial stuff that might still send mail
+ if ($forums = get_records("forum", "course", $course)) {
+ foreach ($forums as $forum) {
+ delete_records("forum_subscriptions", "forum", $forum->id, "user", $user);
+ }
+ }
+ return delete_records("user_teachers", "user", $user, "course", $course);
} else {
- return $students;
+ delete_records("forum_subscriptions", "user", $user);
+ return delete_records("user_teachers", "user", $user);
}
-
-// return get_records_sql("SELECT u.* FROM user u, user_students s, user_teachers t
-// WHERE (s.course = '$courseid' AND s.user = u.id) OR
-// (t.course = '$courseid' AND t.user = u.id)
-// ORDER BY $sort");
}
+function remove_admin($user) {
+/// Removes an admin from a site
+ global $db;
-
-/// MODULE FUNCTIONS /////////////////////////////////////////////////
-
-function get_coursemodule_from_instance($modulename, $instance, $courseid) {
-// Given an instance of a module, finds the coursemodule description
-
- return get_record_sql("SELECT cm.*, m.name
- FROM course_modules cm, modules md, $modulename m
- 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'");
-
+ return delete_records("user_admins", "user", $user);
}
-function get_all_instances_in_course($modulename, $courseid, $sort="cw.section") {
-// Returns an array of all the active instances of a particular
-// module in a given course. Returns false on any errors.
-
- return get_records_sql("SELECT m.*,cw.section,cm.id as coursemodule
- FROM course_modules cm, course_sections cw, modules md, $modulename m
- WHERE cm.course = '$courseid' AND
- cm.instance = m.id AND
- cm.deleted = '0' AND
- cm.section = cw.id AND
- md.name = '$modulename' AND
- md.id = cm.module
- ORDER BY $sort");
-
-}
-
-
/// CORRESPONDENCE ////////////////////////////////////////////////
function email_to_user($user, $from, $subject, $messagetext, $messagehtml="", $attachment="", $attachname="") {
-// user - a user record as an object
-// from - a user record as an object
-// subject - plain text subject line of the email
-// messagetext - plain text version of the message
-// messagehtml - complete html version of the message (optional)
-// attachment - a file on the filesystem, relative to $CFG->dataroot
-// attachname - the name of the file (extension indicates MIME)
+/// user - a user record as an object
+/// from - a user record as an object
+/// subject - plain text subject line of the email
+/// messagetext - plain text version of the message
+/// messagehtml - complete html version of the message (optional)
+/// attachment - a file on the filesystem, relative to $CFG->dataroot
+/// attachname - the name of the file (extension indicates MIME)
global $CFG, $_SERVER;
@@ -1700,9 +649,9 @@ function email_to_user($user, $from, $subject, $messagetext, $messagehtml="", $a
/// FILE HANDLING /////////////////////////////////////////////
function make_upload_directory($directory) {
-// $directory = a string of directory names under $CFG->dataroot
-// eg stuff/assignment/1
-// Returns full directory if successful, false if not
+/// $directory = a string of directory names under $CFG->dataroot
+/// eg stuff/assignment/1
+/// Returns full directory if successful, false if not
global $CFG;
@@ -1730,6 +679,7 @@ function make_upload_directory($directory) {
}
function make_mod_upload_directory($courseid) {
+/// Makes an upload directory for a particular module
global $CFG;
if (! $moddata = make_upload_directory("$courseid/$CFG->moddata")) {
@@ -1748,7 +698,7 @@ function make_mod_upload_directory($courseid) {
function valid_uploaded_file($newfile) {
-// Returns current name of file on disk if true
+/// Returns current name of file on disk if true
if (is_uploaded_file($newfile['tmp_name']) and $newfile['size'] > 0) {
return $newfile['tmp_name'];
} else {
@@ -1757,6 +707,7 @@ function valid_uploaded_file($newfile) {
}
function get_max_upload_file_size() {
+/// Returns the maximum size for uploading files
if (! $filesize = ini_get("upload_max_filesize")) {
$filesize = "5M";
}
@@ -1764,9 +715,9 @@ function get_max_upload_file_size() {
}
function get_directory_list($rootdir, $excludefile="", $descend=true) {
-// Returns an array with all the filenames in
-// all subdirectories, relative to the given rootdir.
-// If excludefile is defined, then that file/directory is ignored
+/// Returns an array with all the filenames in
+/// all subdirectories, relative to the given rootdir.
+/// If excludefile is defined, then that file/directory is ignored
$dirs = array();
@@ -1798,7 +749,7 @@ function get_directory_list($rootdir, $excludefile="", $descend=true) {
}
function get_real_size($size=0) {
-// Converts numbers like 10M into bytes
+/// Converts numbers like 10M into bytes
if (!$size) {
return 0;
}
@@ -1819,7 +770,7 @@ function get_real_size($size=0) {
}
function display_size($size) {
-// Converts bytes into display form
+/// Converts bytes into display form
if ($size >= 1073741824) {
$size = round($size / 1073741824 * 10) / 10 . "Gb";
} else if ($size >= 1048576) {
@@ -1833,6 +784,7 @@ function display_size($size) {
}
function clean_filename($string) {
+/// Cleans a given filename by removing suspicious or troublesome characters
$string = stripslashes($string);
$string = eregi_replace("\.\.", "", $string);
$string = eregi_replace("[^([:alnum:]|\.)]", "_", $string);
@@ -1842,12 +794,8 @@ function clean_filename($string) {
/// STRING TRANSLATION ////////////////////////////////////////
-function print_string($identifier, $module="", $a=NULL) {
- echo get_string($identifier, $module, $a);
-}
-
function current_language() {
-// Returns the code for the current language
+/// Returns the code for the current language
global $CFG, $USER;
if (isset($USER->lang)) { // User language can override site language
@@ -1857,14 +805,19 @@ function current_language() {
}
}
+function print_string($identifier, $module="", $a=NULL) {
+/// Given a string to translate - prints it out.
+ echo get_string($identifier, $module, $a);
+}
+
function get_string($identifier, $module="", $a=NULL) {
-// Return the translated string specified by $identifier as
-// for $module. Uses the same format files as STphp.
-// $a is an object, string or number that can be used
-// within translation strings
-//
-// eg "hello \$a->firstname \$a->lastname"
-// or "hello \$a"
+/// Return the translated string specified by $identifier as
+/// for $module. Uses the same format files as STphp.
+/// $a is an object, string or number that can be used
+/// within translation strings
+///
+/// eg "hello \$a->firstname \$a->lastname"
+/// or "hello \$a"
global $CFG;
@@ -1910,7 +863,7 @@ function get_string($identifier, $module="", $a=NULL) {
function get_string_from_file($identifier, $langfile, $destination) {
-// This function is only used from get_string().
+/// This function is only used from get_string().
include ($langfile);
if (!isset ($string[$identifier])) {
@@ -1937,6 +890,27 @@ function get_list_of_languages() {
return $languages;
}
+function get_list_of_plugins($plugin="mod") {
+/// Lists plugin directories within some directory
+
+ global $CFG;
+
+ $basedir = opendir("$CFG->dirroot/$plugin");
+ while ($dir = readdir($basedir)) {
+ if ($dir == "." || $dir == ".." || $dir == "CVS") {
+ continue;
+ }
+ if (filetype("$CFG->dirroot/$plugin/$dir") != "dir") {
+ continue;
+ }
+ $plugins[] = $dir;
+ }
+ if ($plugins) {
+ asort($plugins);
+ }
+ return $plugins;
+}
+
/// ENCRYPTION ////////////////////////////////////////////////
@@ -1951,7 +925,7 @@ function rc4decrypt($data) {
}
function endecrypt ($pwd, $data, $case) {
-// Based on a class by Mukul Sabharwal [mukulsabharwal@yahoo.com]
+/// Based on a class by Mukul Sabharwal [mukulsabharwal@yahoo.com]
if ($case == 'de') {
$data = urldecode($data);
@@ -2008,79 +982,74 @@ function endecrypt ($pwd, $data, $case) {
}
-/// MISCELLANEOUS ////////////////////////////////////////////////////////////////////
+/// ENVIRONMENT CHECKING ////////////////////////////////////////////////////////////
-function count_words($string) {
- $string = strip_tags($string);
- return count(preg_split("/\w\b/", $string)) - 1;
+function check_php_version($version="4.1.0") {
+/// Returns true is the current version of PHP is greater that the specified one
+ $minversion = intval(str_replace(".", "", $version));
+ $curversion = intval(str_replace(".", "", phpversion()));
+ return ($curversion >= $minversion);
}
-function getweek ($startdate, $thedate) {
-// Given dates in seconds, how many weeks is the date from startdate
-// The first week is 1, the second 2 etc ...
-
- if ($thedate < $startdate) { // error
- return 0;
+function check_browser_version($brand="MSIE", $version=5.5) {
+/// Checks to see if is a browser matches the specified
+/// brand and is equal or better version.
+ global $HTTP_USER_AGENT;
+
+ if (!$HTTP_USER_AGENT) {
+ return false;
+ }
+ $string = explode(";", $HTTP_USER_AGENT);
+ if (!isset($string[1])) {
+ return false;
+ }
+ $string = explode(" ", trim($string[1]));
+ if (!isset($string[0]) and !isset($string[1])) {
+ return false;
+ }
+ if ($string[0] == $brand and (float)$string[1] >= $version ) {
+ return true;
+ }
+ return false;
+}
+
+function can_use_richtext_editor() {
+/// Is the richedit editor enabled?
+ global $USER, $CFG;
+ if ($USER->htmleditor and $CFG->htmleditor) {
+ return check_browser_version("MSIE", 5.5);
+ }
+ return false;
+}
+
+function check_gd_version() {
+/// Hack to find out the GD version by parsing phpinfo output
+ ob_start();
+ phpinfo(8);
+ $phpinfo = ob_get_contents();
+ ob_end_clean();
+
+ $phpinfo = explode("\n",$phpinfo);
+
+ $gdversion = 0;
+
+ foreach ($phpinfo as $text) {
+ $parts = explode('',$text);
+ foreach ($parts as $key => $val) {
+ $parts[$key] = strip_tags($val);
+ }
+ if ($parts[0]=="GD Version") {
+ $gdversion = intval($parts[1]);
+ }
}
- return floor(($thedate - $startdate) / 604800.0) + 1;
+ return $gdversion; // 1, 2 or 0
}
-function add_to_log($course, $module, $action, $url="", $info="") {
-// 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.
-//
-// course = the course id
-// module = forum, journal, resource, course, user etc
-// action = view, edit, post (often but not always the same as the file.php)
-// url = the file and parameters used to see the results of the action
-// info = additional description information
-
-
- global $db, $USER, $REMOTE_ADDR;
-
- if (isset($USER->realuser)) { // Don't log
- return;
- }
-
- $timenow = time();
- $info = addslashes($info);
-
- $result = $db->Execute("INSERT INTO log
- SET time = '$timenow',
- user = '$USER->id',
- course = '$course',
- ip = '$REMOTE_ADDR',
- module = '$module',
- action = '$action',
- url = '$url',
- info = '$info'");
- if (!$result) {
- echo "
Error: Could not insert a new entry to the Moodle log
"; // Don't throw an error
- }
-}
-
-function generate_password($maxlen=10) {
-// returns a randomly generated password of length $maxlen. inspired by
-// http://www.phpbuilder.com/columns/jesus19990502.php3
-
- global $CFG;
-
- $fillers = "1234567890!$-+";
- $wordlist = file($CFG->wordlist);
-
- srand((double) microtime() * 1000000);
- $word1 = trim($wordlist[rand(0, count($wordlist) - 1)]);
- $word2 = trim($wordlist[rand(0, count($wordlist) - 1)]);
- $filler1 = $fillers[rand(0, strlen($fillers) - 1)];
-
- return substr($word1 . $filler1 . $word2, 0, $maxlen);
-}
function moodle_needs_upgrading() {
-// Checks version numbers of Main code and all modules to see
-// if there are any mismatches ... returns true or false
+/// Checks version numbers of Main code and all modules to see
+/// if there are any mismatches ... returns true or false
global $CFG;
include_once("$CFG->dirroot/version.php"); # defines $version and upgrades
@@ -2107,87 +1076,45 @@ function moodle_needs_upgrading() {
}
-function get_list_of_plugins($plugin="mod") {
-// Lists plugin directories within some directory
+/// MISCELLANEOUS ////////////////////////////////////////////////////////////////////
+
+function count_words($string) {
+/// Words are defined as things between whitespace
+ $string = strip_tags($string);
+ return count(preg_split("/\w\b/", $string)) - 1;
+}
+
+function getweek ($startdate, $thedate) {
+/// Given dates in seconds, how many weeks is the date from startdate
+/// The first week is 1, the second 2 etc ...
+
+ if ($thedate < $startdate) { // error
+ return 0;
+ }
+
+ return floor(($thedate - $startdate) / 604800.0) + 1;
+}
+
+function generate_password($maxlen=10) {
+/// returns a randomly generated password of length $maxlen. inspired by
+/// http://www.phpbuilder.com/columns/jesus19990502.php3
global $CFG;
- $basedir = opendir("$CFG->dirroot/$plugin");
- while ($dir = readdir($basedir)) {
- if ($dir == "." || $dir == ".." || $dir == "CVS") {
- continue;
- }
- if (filetype("$CFG->dirroot/$plugin/$dir") != "dir") {
- continue;
- }
- $plugins[] = $dir;
- }
- if ($plugins) {
- asort($plugins);
- }
- return $plugins;
+ $fillers = "1234567890!$-+";
+ $wordlist = file($CFG->wordlist);
+
+ srand((double) microtime() * 1000000);
+ $word1 = trim($wordlist[rand(0, count($wordlist) - 1)]);
+ $word2 = trim($wordlist[rand(0, count($wordlist) - 1)]);
+ $filler1 = $fillers[rand(0, strlen($fillers) - 1)];
+
+ return substr($word1 . $filler1 . $word2, 0, $maxlen);
}
-
-function check_php_version($version="4.1.0") {
-// Returns true is the current version of PHP is greater that the specified one
- $minversion = intval(str_replace(".", "", $version));
- $curversion = intval(str_replace(".", "", phpversion()));
- return ($curversion >= $minversion);
-}
-
-function check_browser_version($brand="MSIE", $version=5.5) {
-// Checks to see if is a browser matches the specified
-// brand and is equal or better version.
- global $HTTP_USER_AGENT;
-
- if (!$HTTP_USER_AGENT) {
- return false;
- }
- $string = explode(";", $HTTP_USER_AGENT);
- if (!isset($string[1])) {
- return false;
- }
- $string = explode(" ", trim($string[1]));
- if (!isset($string[0]) and !isset($string[1])) {
- return false;
- }
- if ($string[0] == $brand and (float)$string[1] >= $version ) {
- return true;
- }
- return false;
-}
-
-function can_use_richtext_editor() {
- global $USER, $CFG;
- if ($USER->htmleditor and $CFG->htmleditor) {
- return check_browser_version("MSIE", 5.5);
- }
- return false;
-}
-
-
-function check_gd_version() {
- ob_start();
- phpinfo(8);
- $phpinfo = ob_get_contents();
- ob_end_clean();
-
- $phpinfo = explode("\n",$phpinfo);
-
- $gdversion = 0;
-
- foreach ($phpinfo as $text) {
- $parts = explode('',$text);
- foreach ($parts as $key => $val) {
- $parts[$key] = strip_tags($val);
- }
- if ($parts[0]=="GD Version") {
- $gdversion = intval($parts[1]);
- }
- }
-
- return $gdversion; // 1, 2 or 0
+function format_float($num, $places=0) {
+/// Given a float, prints it nicely
+ return sprintf("%.$places"."f", $num);
}
diff --git a/lib/setup.php b/lib/setup.php
index 224953a7896..5bf03ba1f09 100644
--- a/lib/setup.php
+++ b/lib/setup.php
@@ -26,8 +26,9 @@
/// Load up standard libraries
- require("$CFG->libdir/weblib.php"); // Standard web page functions
- require("$CFG->libdir/moodlelib.php"); // Various Moodle functions
+ require("$CFG->libdir/weblib.php"); // Functions for producing HTML
+ require("$CFG->libdir/datalib.php"); // Functions for accessing databases
+ require("$CFG->libdir/moodlelib.php"); // Other general-purpose functions
/// Set error reporting back to normal
@@ -36,7 +37,7 @@
/// Load up any configuration from the config table
- if ($configs = get_records_sql("SELECT * FROM config")) {
+ if ($configs = get_records("config")) {
$CFG = (array)$CFG;
foreach ($configs as $config) {
$CFG[$config->name] = $config->value;
@@ -112,8 +113,10 @@
if (! isset($_SESSION["USER"])) { $_SESSION["USER"] = new object; }
extract($_SESSION); // Makes $SESSION and $USER available for read-only access
- $FULLME = qualified_me();
- $ME = strip_querystring($FULLME);
+ if (!$FULLME) {
+ $FULLME = qualified_me();
+ }
+ $ME = strip_querystring($FULLME);
/// Set language/locale of printed times. If user has chosen a language that
diff --git a/lib/weblib.php b/lib/weblib.php
index 5c9b8c40811..aaf6df54af0 100644
--- a/lib/weblib.php
+++ b/lib/weblib.php
@@ -1,10 +1,33 @@
dirroot/theme/$CFG->theme/styles.php")) {
+ $styles = $CFG->stylesheet;
+ } else {
+ $styles = "$CFG->wwwroot/theme/standard/styles.php";
+ }
+
+ if ($navigation == "home") {
+ $home = true;
+ $navigation = "";
+ }
+
+ if ($button == "") {
+ $button = " ";
+ }
+
+ if (!$menu and $navigation) {
+ if (isset($USER->id)) {
+ $menu = "wwwroot/login/logout.php\">".get_string("logout")."";
+ } else {
+ $menu = "wwwroot/login/index.php\">".get_string("login")."";
+ }
+ }
+
+ // Specify character set ... default is iso-8859-1 but some languages might need something else
+ // Could be optimised by carrying the charset variable around in $USER
+ if (current_language() == "en") {
+ $meta = "\n$meta\n";
+ } else {
+ $meta = "\n$meta\n";
+ }
+
+ if ($CFG->langdir == "RTL") {
+ $direction = " DIR=\"RTL\"";
+ } else {
+ $direction = " DIR=\"LTR\"";
+ }
+
+ if (!$cache) { // Do everything we can to prevent clients and proxies caching
+ @header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
+ @header("Pragma: no-cache");
+ $meta .= "\n";
+ $meta .= "\n";
+ }
+
+ include ("$CFG->dirroot/theme/$CFG->theme/header.html");
+}
+
+function print_footer ($course=NULL) {
+// Can provide a course object to make the footer contain a link to
+// to the course home page, otherwise the link will go to the site home
+ global $USER, $CFG, $THEME;
+
+
+/// Course links
+ if ($course) {
+ if ($course == "home") { // special case for site home page - please do not remove
+ $homelink = "