MDL-14679 converted some count_records
authorskodak <skodak>
Mon, 2 Jun 2008 21:47:31 +0000 (21:47 +0000)
committerskodak <skodak>
Mon, 2 Jun 2008 21:47:31 +0000 (21:47 +0000)
14 files changed:
admin/register.php
blocks/admin/block_admin.php
blocks/course_list/block_course_list.php
blog/lib.php
course/category.php
course/editcategory.php
course/reset_form.php
index.php
lib/adminlib.php
lib/simpletest/testeventslib.php
mnet/peer.php
user/profile/definelib.php
user/profile/index.php
user/view.php

index f213c6322ed7e0c25ab9265fe95c36089bd25ec9..d6bc2a3ed1adee20c3103b863a2ada1fdbd515ac 100644 (file)
     echo '<div class="fitemtitle"><label>'.get_string("statistics")."<br />(".get_string("notpublic").')'.'</label></div>';
     echo '<div class="felement ftext">';
 
-    $count = count_records('course')-1;
+    $count = $DB->count_records('course')-1;
     echo get_string("courses").": ".$count;
     echo "<input type=\"hidden\" name=\"courses\" value=\"$count\" />\n";
     echo '<br />';
 
-    $count = count_records('user', 'deleted', 0);
+    $count = $DB->count_records('user', array('deleted'=>0));
     echo get_string("users").": ".$count;
     echo "<input type=\"hidden\" name=\"users\" value=\"$count\" />\n";
     echo '<br />';
 
     // total number of role assignments
-    $count = count_records('role_assignments');
+    $count = $DB->count_records('role_assignments');
     echo get_string('roleassignments', 'role').": ".$count;
     echo "<input type=\"hidden\" name=\"roleassignments\" value=\"$count\" />\n";
     echo '<br />';
     // 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 "<input type=\"hidden\" name=\"courseupdaters\" value=\"$count\" />\n";
     echo '<br />';
 
-    $count = count_records('forum_posts');
+    $count = $DB->count_records('forum_posts');
     echo get_string("posts", 'forum').": ".$count;
     echo "<input type=\"hidden\" name=\"posts\" value=\"$count\" />\n";
     echo '<br />';
 
-    $count = count_records('question');
+    $count = $DB->count_records('question');
     echo get_string("questions", 'quiz').": ".$count;
     echo "<input type=\"hidden\" name=\"questions\" value=\"$count\" />\n";
     echo '<br />';
 
-    $count = count_records('resource');
+    $count = $DB->count_records('resource');
     echo get_string("modulenameplural", "resource").": ".$count;
     echo "<input type=\"hidden\" name=\"resources\" value=\"$count\" />\n";
     echo '</div>';
index 28655379d9b924799d8cdd3c3a1fbb7c803e1398..ebbd28fd9d79d7e5d7c4c30d7ad920340361f897 100644 (file)
@@ -194,7 +194,7 @@ class block_admin extends block_list {
             require_once($CFG->dirroot.'/enrol/authorize/const.php');
             $paymenturl = '<a href="'.$CFG->wwwroot.'/enrol/authorize/index.php?course='.$course->id.'">'.get_string('payments').'</a> ';
             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 .= '<a href="'.$CFG->wwwroot.'/enrol/authorize/index.php?status='.AN_STATUS_AUTH.'&amp;course='.$course->id.'">'.get_string('paymentpending', 'moodle', $cnt).'</a>';
                 }
             }
index b2475ac4e12931faf4902eeb2f3ff14a452f8fb7..53f7dcc04a17eb171f2ad865fc8e9c362f37d837 100644 (file)
@@ -13,7 +13,7 @@ class block_course_list extends block_list {
     }
 
     function get_content() {
-        global $THEME, $CFG, $USER;
+        global $THEME, $CFG, $USER, $DB;
 
         if($this->content !== NULL) {
             return $this->content;
@@ -62,7 +62,7 @@ class block_course_list extends block_list {
 
         $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[]="<a $linkcss href=\"$CFG->wwwroot/course/category.php?id=$category->id\">" . format_string($category->name) . "</a>";
index 026a3a04168ff68f839b4eef7c45f5b0295a3d60..cfa55e26a9c0eb5b3e1314c42b598438b70e039a 100755 (executable)
      * 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='',
index 1b1c7e4df0aa05dd3232267237baae00edbdfda2..05d7d942b3ac22dd58fcdf51618e9e810dd317d2 100644 (file)
 
 /// Print headings
 
-    $numcategories = count_records("course_categories");
+    $numcategories = $DB->count_records("course_categories");
 
     $stradministration = get_string("administration");
     $strcategories = get_string("categories");
index 57a6538bbd8eaff874ae2bc6b1d6d9796e1a2b30..dfe7d2bb1ce9ad9f15697a1bfa66a4f68c468d6a 100644 (file)
@@ -142,7 +142,7 @@ if ($id && !$categoryadd && !$categoryupdate && false) {
     }
     
     // Print headings 
-    $numcategories = count_records("course_categories");
+    $numcategories = $DB->count_records("course_categories");
 
     $stradministration = get_string("administration");
     $strcategories = get_string("categories");
index 1053e700a9da87d12ba4b92f1cb1a4b861f9ca67..42e0e411ff4c50083e6f4a7aa468b54ca9d1fc55 100644 (file)
@@ -52,7 +52,7 @@ class course_reset_form extends moodleform {
         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";
index 93644e2e5653e1e5d6c7acf86fb2cbbc993b0e71..3bde1548e0957347226def9504163d3e8a017b92 100644 (file)
--- a/index.php
+++ b/index.php
                 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);
index 390998e6f344aa4affef423af066ce1c038e8998..3a9db94b8dd3e5e568bb7b3fd457bdeb9c1e578d 100644 (file)
@@ -2481,6 +2481,7 @@ class admin_setting_courselist_frontpage extends admin_setting {
     }
 
     function load_choices() {
+        global $DB;
         if (is_array($this->choices)) {
             return true;
         }
@@ -2489,7 +2490,7 @@ class admin_setting_courselist_frontpage extends admin_setting {
                                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;
index a5b8ba8012e081764151b013fd5a94062557dc56..1c704ae4aafbdab84a6589d7f672e4bcccad431a 100755 (executable)
@@ -100,9 +100,9 @@ class eventslib_test extends UnitTestCase {
      * 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 @@ class eventslib_test extends UnitTestCase {
      * 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');
     }
 
     /**
index 3512951bb8f4b291799463f13b0b6128c9aea88c..74ad313c4d26abf7b5042733ade404734626aafe 100644 (file)
@@ -88,19 +88,21 @@ class mnet_peer {
     }
 
     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 @@ class mnet_peer {
         // 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 @@ class mnet_peer {
             $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 @@ class mnet_peer {
     }
 
     function commit() {
+        global $DB;
         $obj = new stdClass();
 
         $obj->wwwroot               = $this->wwwroot;
@@ -187,9 +192,9 @@ class mnet_peer {
 
         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 @@ class mnet_peer {
     }
 
     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 @@ class mnet_peer {
     }
 
     function set_id($id) {
-        global $CFG;
+        global $CFG, $DB;
 
         if (clean_param($id, PARAM_INT) != $id) {
             $this->errno[]  = 1;
@@ -240,11 +245,11 @@ class mnet_peer {
                 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 @@ class mnet_peer {
      * @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 @@ class mnet_peer {
         $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() {
index 4a1191ac7e5d939b607fd389eb3458be7503eadb..e929401468f3516ba44de7b9f67833890296baa1 100644 (file)
@@ -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');
                 }
index 93f940092f0605bd35f5022f4434d82ebadb128c..d71fa051dd6682b6cbf8119f235048ee0931f5ef 100644 (file)
@@ -49,7 +49,7 @@ switch ($action) {
         }
 
         //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 @@ switch ($action) {
         }
 
         //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');
index b7bbbbd94a2cd37894edcf3caf34ee3cbafcc275..79340dc43e9d8848aa162349bc609c3d72b7b41e 100644 (file)
 
     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");