Skip to content

Commit

Permalink
MDL-14679 converted some count_records
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jun 2, 2008
1 parent a5d424d commit c7da435
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 46 deletions.
22 changes: 11 additions & 11 deletions admin/register.php
Expand Up @@ -110,48 +110,48 @@
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>';
Expand Down
2 changes: 1 addition & 1 deletion blocks/admin/block_admin.php
Expand Up @@ -194,7 +194,7 @@ function get_content() {
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>';
}
}
Expand Down
4 changes: 2 additions & 2 deletions blocks/course_list/block_course_list.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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[]="<a $linkcss href=\"$CFG->wwwroot/course/category.php?id=$category->id\">" . format_string($category->name) . "</a>";
Expand Down
2 changes: 1 addition & 1 deletion blog/lib.php
Expand Up @@ -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='',
Expand Down
2 changes: 1 addition & 1 deletion course/category.php
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion course/editcategory.php
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion course/reset_form.php
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion index.php
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion lib/adminlib.php
Expand Up @@ -2481,6 +2481,7 @@ function admin_setting_courselist_frontpage($loggedin) {
}

function load_choices() {
global $DB;
if (is_array($this->choices)) {
return true;
}
Expand All @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions lib/simpletest/testeventslib.php
Expand Up @@ -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);
Expand All @@ -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');
}

/**
Expand Down
42 changes: 24 additions & 18 deletions mnet/peer.php
Expand Up @@ -88,55 +88,59 @@ 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();

// If we don't have any activity records for which the mnet_host table
// 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');
$auth = new auth_plugin_mnet();
$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;
}

Expand Down Expand Up @@ -171,6 +175,7 @@ function check_credentials($key) {
}

function commit() {
global $DB;
$obj = new stdClass();

$obj->wwwroot = $this->wwwroot;
Expand All @@ -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;
}
}
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions user/profile/definelib.php
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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');
}
Expand Down
4 changes: 2 additions & 2 deletions user/profile/index.php
Expand Up @@ -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');
Expand All @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion user/view.php
Expand Up @@ -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");
Expand Down

0 comments on commit c7da435

Please sign in to comment.