diff --git a/admin/register.php b/admin/register.php
index f213c6322ed7e..d6bc2a3ed1ade 100644
--- a/admin/register.php
+++ b/admin/register.php
@@ -110,18 +110,18 @@
echo '
';
echo '';
- $count = count_records('course')-1;
+ $count = $DB->count_records('course')-1;
echo get_string("courses").": ".$count;
echo "\n";
echo '
';
- $count = count_records('user', 'deleted', 0);
+ $count = $DB->count_records('user', array('deleted'=>0));
echo get_string("users").": ".$count;
echo "\n";
echo '
';
// total number of role assignments
- $count = count_records('role_assignments');
+ $count = $DB->count_records('role_assignments');
echo get_string('roleassignments', 'role').": ".$count;
echo "\n";
echo '
';
@@ -129,29 +129,29 @@
// first find all distinct roles with mod/course:update
// please change the name and strings to something appropriate to reflect the new data collected
$sql = "SELECT COUNT(DISTINCT u.id)
- FROM {$CFG->prefix}role_capabilities rc,
- {$CFG->prefix}role_assignments ra,
- {$CFG->prefix}user u
- WHERE (rc.capability = 'moodle/course:update' or rc.capability='moodle/site:doanything')
+ FROM {role_capabilities} rc,
+ {role_assignments} ra,
+ {user} u
+ WHERE (rc.capability = 'moodle/course:update' or rc.capability='moodle/site:doanything')
AND rc.roleid = ra.roleid
AND u.id = ra.userid";
- $count = count_records_sql($sql);
+ $count = $DB->count_records_sql($sql);
echo get_string("teachers").": ".$count;
echo "\n";
echo '
';
- $count = count_records('forum_posts');
+ $count = $DB->count_records('forum_posts');
echo get_string("posts", 'forum').": ".$count;
echo "\n";
echo '
';
- $count = count_records('question');
+ $count = $DB->count_records('question');
echo get_string("questions", 'quiz').": ".$count;
echo "\n";
echo '
';
- $count = count_records('resource');
+ $count = $DB->count_records('resource');
echo get_string("modulenameplural", "resource").": ".$count;
echo "\n";
echo '
';
diff --git a/blocks/admin/block_admin.php b/blocks/admin/block_admin.php
index 28655379d9b92..ebbd28fd9d79d 100644
--- a/blocks/admin/block_admin.php
+++ b/blocks/admin/block_admin.php
@@ -194,7 +194,7 @@ function get_content() {
require_once($CFG->dirroot.'/enrol/authorize/const.php');
$paymenturl = ''.get_string('payments').' ';
if (has_capability('enrol/authorize:managepayments', $context)) {
- if ($cnt = count_records('enrol_authorize', 'status', AN_STATUS_AUTH, 'courseid', $course->id)) {
+ if ($cnt = $DB->count_records('enrol_authorize', array('status'=>AN_STATUS_AUTH, 'courseid'=>$course->id))) {
$paymenturl .= ''.get_string('paymentpending', 'moodle', $cnt).'';
}
}
diff --git a/blocks/course_list/block_course_list.php b/blocks/course_list/block_course_list.php
index b2475ac4e1293..53f7dcc04a17e 100644
--- a/blocks/course_list/block_course_list.php
+++ b/blocks/course_list/block_course_list.php
@@ -13,7 +13,7 @@ function has_config() {
}
function get_content() {
- global $THEME, $CFG, $USER;
+ global $THEME, $CFG, $USER, $DB;
if($this->content !== NULL) {
return $this->content;
@@ -62,7 +62,7 @@ function get_content() {
$categories = get_categories("0"); // Parent = 0 ie top-level categories only
if ($categories) { //Check we have categories
- if (count($categories) > 1 || (count($categories) == 1 && count_records('course') > 200)) { // Just print top level category links
+ if (count($categories) > 1 || (count($categories) == 1 && $DB->count_records('course') > 200)) { // Just print top level category links
foreach ($categories as $category) {
$linkcss = $category->visible ? "" : " class=\"dimmed\" ";
$this->content->items[]="wwwroot/course/category.php?id=$category->id\">" . format_string($category->name) . "";
diff --git a/blog/lib.php b/blog/lib.php
index 026a3a04168ff..cfa55e26a9c0e 100755
--- a/blog/lib.php
+++ b/blog/lib.php
@@ -631,7 +631,7 @@ function blog_fetch_entries($postid='', $fetchlimit=10, $fetchstart='', $filtert
* get the count of viewable entries, easiest way is to count blog_fetch_entries
* this is used for print_paging_bar
* this is not ideal, but because of the UNION in the sql in blog_fetch_entries,
- * it is hard to use count_records_sql
+ * it is hard to use $DB->count_records_sql
*/
function get_viewable_entry_count($postid='', $fetchlimit=10,
$fetchstart='', $filtertype='', $filterselect='', $tagid='',
diff --git a/course/category.php b/course/category.php
index 1b1c7e4df0aa0..05d7d942b3ac2 100644
--- a/course/category.php
+++ b/course/category.php
@@ -104,7 +104,7 @@
/// Print headings
- $numcategories = count_records("course_categories");
+ $numcategories = $DB->count_records("course_categories");
$stradministration = get_string("administration");
$strcategories = get_string("categories");
diff --git a/course/editcategory.php b/course/editcategory.php
index 57a6538bbd8ea..dfe7d2bb1ce9a 100644
--- a/course/editcategory.php
+++ b/course/editcategory.php
@@ -142,7 +142,7 @@
}
// Print headings
- $numcategories = count_records("course_categories");
+ $numcategories = $DB->count_records("course_categories");
$stradministration = get_string("administration");
$strcategories = get_string("categories");
diff --git a/course/reset_form.php b/course/reset_form.php
index 1053e700a9da8..42e0e411ff4c5 100644
--- a/course/reset_form.php
+++ b/course/reset_form.php
@@ -52,7 +52,7 @@ function definition (){
if ($allmods = $DB->get_records('modules') ) {
foreach ($allmods as $mod) {
$modname = $mod->name;
- if (!count_records($modname, 'course', $COURSE->id)) {
+ if (!$DB->count_records($modname, array('course'=>$COURSE->id))) {
continue; // skip mods with no instances
}
$modfile = $CFG->dirroot."/mod/$modname/lib.php";
diff --git a/index.php b/index.php
index 93644e2e5653e..3bde1548e0957 100644
--- a/index.php
+++ b/index.php
@@ -219,7 +219,7 @@
if (isloggedin() and !has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)) and !isguest() and empty($CFG->disablemycourses)) {
print_heading_block(get_string('mycourses'));
print_my_moodle();
- } else if ((!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)) and !isguest()) or (count_records('course') <= FRONTPAGECOURSELIMIT)) {
+ } else if ((!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)) and !isguest()) or ($DB->count_records('course') <= FRONTPAGECOURSELIMIT)) {
// admin should not see list of courses when there are too many of them
print_heading_block(get_string('availablecourses'));
print_courses(0);
diff --git a/lib/adminlib.php b/lib/adminlib.php
index 390998e6f344a..3a9db94b8dd3e 100644
--- a/lib/adminlib.php
+++ b/lib/adminlib.php
@@ -2481,6 +2481,7 @@ function admin_setting_courselist_frontpage($loggedin) {
}
function load_choices() {
+ global $DB;
if (is_array($this->choices)) {
return true;
}
@@ -2489,7 +2490,7 @@ function load_choices() {
FRONTPAGECATEGORYNAMES => get_string('frontpagecategorynames'),
FRONTPAGECATEGORYCOMBO => get_string('frontpagecategorycombo'),
'none' => get_string('none'));
- if ($this->name == 'frontpage' and count_records('course') > FRONTPAGECOURSELIMIT) {
+ if ($this->name == 'frontpage' and $DB->count_records('course') > FRONTPAGECOURSELIMIT) {
unset($this->choices[FRONTPAGECOURSELIST]);
}
return true;
diff --git a/lib/simpletest/testeventslib.php b/lib/simpletest/testeventslib.php
index a5b8ba8012e08..1c704ae4aafbd 100755
--- a/lib/simpletest/testeventslib.php
+++ b/lib/simpletest/testeventslib.php
@@ -100,9 +100,9 @@ function tearDown() {
* Tests the installation of event handlers from file
*/
function test__events_update_definition__install() {
- global $CFG;
+ global $CFG, $DB;
- $dbcount = count_records('events_handlers', 'handlermodule', 'unittest');
+ $dbcount = $DB->count_records('events_handlers', array('handlermodule'=>'unittest'));
$handlers = array();
require($CFG->libdir.'/simpletest/fixtures/events.php');
$filecount = count($handlers);
@@ -113,8 +113,10 @@ function test__events_update_definition__install() {
* Tests the uninstallation of event handlers from file
*/
function test__events_update_definition__uninstall() {
+ global $DB;
+
events_uninstall('unittest');
- $this->assertEqual(0, count_records('events_handlers', 'handlermodule', 'unittest'), 'All handlers should be uninstalled: %s');
+ $this->assertEqual(0, count_records('events_handlers', array('handlermodule'=>'unittest')), 'All handlers should be uninstalled: %s');
}
/**
diff --git a/mnet/peer.php b/mnet/peer.php
index 3512951bb8f4b..74ad313c4d26a 100644
--- a/mnet/peer.php
+++ b/mnet/peer.php
@@ -88,19 +88,21 @@ function bootstrap($wwwroot, $pubkey = null, $application) {
}
function delete() {
+ global $DB;
+
if ($this->deleted) return true;
- $users = count_records('user','mnethostid', $this->id);
+ $users = $DB->count_records('user', array('mnethostid'=>$this->id));
if ($users > 0) {
$this->deleted = 1;
}
- $actions = count_records('mnet_log','hostid', $this->id);
+ $actions = $DB->count_records('mnet_log', array('hostid'=>$this->id));
if ($actions > 0) {
$this->deleted = 1;
}
- $obj = delete_records('mnet_rpc2host', 'host_id', $this->id);
+ $obj = $DB->delete_records('mnet_rpc2host', array('host_id'=>$this->id));
$this->delete_all_sessions();
@@ -108,27 +110,29 @@ function delete() {
// provides a foreign key, then we can delete the record. Otherwise, we
// just mark it as deleted.
if (0 == $this->deleted) {
- delete_records('mnet_host', "id", $this->id);
+ $DB->delete_records('mnet_host', array("id"=>$this->id));
} else {
$this->commit();
}
}
function count_live_sessions() {
+ global $DB;
$obj = $this->delete_expired_sessions();
- return count_records('mnet_session','mnethostid', $this->id);
+ return $DB->count_records('mnet_session', array('mnethostid'=>$this->id));
}
function delete_expired_sessions() {
+ global $DB;
$now = time();
- return delete_records_select('mnet_session', " mnethostid = '{$this->id}' AND expires < '$now' ");
+ return $DB->delete_records_select('mnet_session', " mnethostid = ? AND expires < ? ", array($this->id, $now));
}
function delete_all_sessions() {
- global $CFG;
+ global $CFG, $DB;
// TODO: Expires each PHP session individually
// $sessions = get_records('mnet_session', 'mnethostid', $this->id);
- $sessions = get_records('mnet_session', 'mnethostid', $this->id);
+ $sessions = $DB->get_records('mnet_session', array('mnethostid'=>$this->id));
if (count($sessions) > 0 && file_exists($CFG->dirroot.'/auth/mnet/auth.php')) {
require_once($CFG->dirroot.'/auth/mnet/auth.php');
@@ -136,7 +140,7 @@ function delete_all_sessions() {
$auth->end_local_sessions($sessions);
}
- $deletereturn = delete_records_select('mnet_session', " mnethostid = '{$this->id}'");
+ $deletereturn = $DB->delete_records('mnet_session', array('mnethostid'=>$this->id));
return true;
}
@@ -171,6 +175,7 @@ function check_credentials($key) {
}
function commit() {
+ global $DB;
$obj = new stdClass();
$obj->wwwroot = $this->wwwroot;
@@ -187,9 +192,9 @@ function commit() {
if (isset($this->id) && $this->id > 0) {
$obj->id = $this->id;
- return update_record('mnet_host', $obj);
+ return $DB->update_record('mnet_host', $obj);
} else {
- $this->id = insert_record('mnet_host', $obj);
+ $this->id = $DB->insert_record('mnet_host', $obj);
return $this->id > 0;
}
}
@@ -216,9 +221,9 @@ function set_applicationid($applicationid) {
}
function set_wwwroot($wwwroot) {
- global $CFG;
+ global $CFG, $DB;
- $hostinfo = get_record('mnet_host', 'wwwroot', $wwwroot);
+ $hostinfo = $DB->get_record('mnet_host', array('wwwroot'=>$wwwroot));
if ($hostinfo != false) {
$this->populate($hostinfo);
@@ -228,7 +233,7 @@ function set_wwwroot($wwwroot) {
}
function set_id($id) {
- global $CFG;
+ global $CFG, $DB;
if (clean_param($id, PARAM_INT) != $id) {
$this->errno[] = 1;
@@ -240,11 +245,11 @@ function set_id($id) {
SELECT
h.*
FROM
- {$CFG->prefix}mnet_host h
+ {mnet_host} h
WHERE
- h.id = '". $id ."'";
+ h.id = ?";
- if ($hostinfo = get_record_sql($sql)) {
+ if ($hostinfo = $DB->get_record_sql($sql, array($id))) {
$this->populate($hostinfo);
return true;
}
@@ -259,6 +264,7 @@ function set_id($id) {
* @return void
*/
function populate($hostinfo) {
+ global $DB;
$this->id = $hostinfo->id;
$this->wwwroot = $hostinfo->wwwroot;
$this->ip_address = $hostinfo->ip_address;
@@ -271,7 +277,7 @@ function populate($hostinfo) {
$this->force_theme = $hostinfo->force_theme;
$this->theme = $hostinfo->theme;
$this->applicationid = $hostinfo->applicationid;
- $this->application = get_record('mnet_application', 'id', $this->applicationid);
+ $this->application = $DB->get_record('mnet_application', array('id'=>$this->applicationid));
}
function get_public_key() {
diff --git a/user/profile/definelib.php b/user/profile/definelib.php
index 4a1191ac7e5d9..e929401468f35 100644
--- a/user/profile/definelib.php
+++ b/user/profile/definelib.php
@@ -243,7 +243,7 @@ function profile_delete_category($id) {
}
/// Does the category contain any fields
- if (count_records('user_info_field', 'categoryid', $category->id)) {
+ if ($DB->count_records('user_info_field', array('categoryid'=>$category->id))) {
if (array_key_exists($category->sortorder-1, $categories)) {
$newcategory = $categories[$category->sortorder-1];
} else if (array_key_exists($category->sortorder+1, $categories)) {
@@ -423,7 +423,7 @@ function profile_edit_category($id, $redirect) {
if ($data = $categoryform->get_data(false)) {
if (empty($data->id)) {
unset($data->id);
- $data->sortorder = count_records('user_info_category') + 1;
+ $data->sortorder = $DB->count_records('user_info_category') + 1;
if (!$DB->insert_record('user_info_category', $data, false)) {
print_error('There was a problem adding the record to the database');
}
diff --git a/user/profile/index.php b/user/profile/index.php
index 93f940092f060..d71fa051dd668 100644
--- a/user/profile/index.php
+++ b/user/profile/index.php
@@ -49,7 +49,7 @@
}
//ask for confirmation
- $fieldcount = count_records('user_info_field', 'categoryid', $id);
+ $fieldcount = $DB->count_records('user_info_field', array('categoryid'=>$id));
$optionsyes = array ('id'=>$id, 'confirm'=>1, 'action'=>'deletecategory', 'sesskey'=>sesskey());
admin_externalpage_print_header();
print_heading('profiledeletecategory', 'admin');
@@ -67,7 +67,7 @@
}
//ask for confirmation
- $datacount = count_records('user_info_data', 'fieldid', $id);
+ $datacount = $DB->count_records('user_info_data', array('fieldid'=>$id));
$optionsyes = array ('id'=>$id, 'confirm'=>1, 'action'=>'deletefield', 'sesskey'=>sesskey());
admin_externalpage_print_header();
print_heading('profiledeletefield', 'admin');
diff --git a/user/view.php b/user/view.php
index b7bbbbd94a2cd..79340dc43e9d8 100644
--- a/user/view.php
+++ b/user/view.php
@@ -478,7 +478,7 @@
if (!empty($CFG->messaging) and !isguest() and has_capability('moodle/site:sendmessage', get_context_instance(CONTEXT_SYSTEM))) {
if (!empty($USER->id) and ($USER->id == $user->id)) {
- if ($countmessages = count_records('message', 'useridto', $user->id)) {
+ if ($countmessages = $DB->count_records('message', array('useridto'=>$user->id))) {
$messagebuttonname = get_string("messages", "message")."($countmessages)";
} else {
$messagebuttonname = get_string("messages", "message");