From 953d72db50a5454c03ca18e1958e3c7ea80aa4af Mon Sep 17 00:00:00 2001 From: skodak Date: Fri, 30 May 2008 21:16:16 +0000 Subject: [PATCH] MDL-14679 towards /admin conversion --- admin/auth_config.php | 3 +- admin/blocks.php | 28 ++++------ admin/cron.php | 118 ++++++++++++++++++++-------------------- admin/dbperformance.php | 2 + 4 files changed, 73 insertions(+), 78 deletions(-) diff --git a/admin/auth_config.php b/admin/auth_config.php index 9d78b2e26f..9e150a80b7 100644 --- a/admin/auth_config.php +++ b/admin/auth_config.php @@ -18,8 +18,7 @@ $err = array(); $returnurl = "$CFG->wwwroot/$CFG->admin/settings.php?section=manageauths"; // save configuration changes -if ($frm = data_submitted() and confirm_sesskey()) { - $frm = stripslashes_recursive($frm); +if ($frm = data_submitted(false) and confirm_sesskey()) { $authplugin->validate_form($frm, $err); diff --git a/admin/blocks.php b/admin/blocks.php index dcbda49466..162cbcfcc0 100644 --- a/admin/blocks.php +++ b/admin/blocks.php @@ -32,18 +32,18 @@ /// If data submitted, then process and store. if (!empty($hide) && confirm_sesskey()) { - if (!$block = get_record('block', 'id', $hide)) { + if (!$block = $DB->get_record('block', array('id'=>$hide))) { print_error('blockdoesnotexist', 'error'); } - set_field('block', 'visible', '0', 'id', $block->id); // Hide block + $DB->set_field('block', 'visible', '0', array('id'=>$block->id)); // Hide block admin_get_root(true, false); // settings not required - only pages } if (!empty($show) && confirm_sesskey() ) { - if (!$block = get_record('block', 'id', $show)) { + if (!$block = $DB->get_record('block', array('id'=>$show))) { print_error('blockdoesnotexist', 'error'); } - set_field('block', 'visible', '1', 'id', $block->id); // Show block + $DB->set_field('block', 'visible', '1', array('id'=>$block->id)); // Show block admin_get_root(true, false); // settings not required - only pages } @@ -52,7 +52,7 @@ print_error('blockdoesnotexist', 'error'); } $block->multiple = !$block->multiple; - update_record('block', $block); + $DB->update_record('block', $block); admin_get_root(true, false); // settings not required - only pages } @@ -87,7 +87,7 @@ } // First delete instances and then block - $instances = get_records('block_instance', 'blockid', $block->id); + $instances = $DB->get_records('block_instance', array('blockid'=>$block->id)); if(!empty($instances)) { foreach($instances as $instance) { blocks_delete_instance($instance); @@ -96,7 +96,7 @@ } // Delete block - if (!delete_records('block', 'id', $block->id)) { + if (!$DB->delete_records('block', array('id'=>$block->id))) { notify("Error occurred while deleting the $strblockname record from blocks table"); } @@ -122,19 +122,19 @@ /// Get and sort the existing blocks - if (false === ($blocks = get_records('block'))) { + if (!$blocks = $DB->get_records('block')) { print_error('noblocks', 'error'); // Should never happen } $incompatible = array(); foreach ($blocks as $block) { - if(!block_is_compatible($block->name)) { + if (!block_is_compatible($block->name)) { notify('Block '. $block->name .' is not compatible with the current version of Moodle and needs to be updated by a programmer.'); $incompatible[] = $block; continue; } - if(($blockobject = block_instance($block->name)) === false) { + if (($blockobject = block_instance($block->name)) === false) { // Failed to load continue; } @@ -178,12 +178,8 @@ // MDL-11167, blocks can be placed on mymoodle, or the blogs page // and it should not show up on course search page - $totalcount = count_records('block_instance', 'blockid', $blockid); - - $count = count_records_sql('SELECT COUNT(*) - FROM '.$CFG->prefix.'block_instance - WHERE blockid = '.$blockid.' AND - pagetype = \'course-view\''); + $totalcount = $DB->count_records('block_instance', array('blockid'=>$blockid)); + $count = $DB->count_records('block_instance', array('blockid'=>$blockid, 'pagetype'=>'course-view')); if ($count>0) { $blocklist = "wwwroot}/course/search.php?blocklist=$blockid&sesskey={$USER->sesskey}\" "; diff --git a/admin/cron.php b/admin/cron.php index 9c3449cce3..2e59060127 100644 --- a/admin/cron.php +++ b/admin/cron.php @@ -101,7 +101,7 @@ mtrace("Starting activity modules"); get_mailer('buffer'); - if ($mods = get_records_select("modules", "cron > 0 AND (($timenow - lastcron) > cron) AND visible = 1 ")) { + if ($mods = $DB->get_records_select("modules", "cron > 0 AND ((? - lastcron) > cron) AND visible = 1", array($timenow))) { foreach ($mods as $mod) { $libfile = "$CFG->dirroot/mod/$mod->name/lib.php"; if (file_exists($libfile)) { @@ -115,7 +115,7 @@ $pre_time = microtime(1); } if ($cron_function()) { - if (! set_field("modules", "lastcron", $timenow, "id", $mod->id)) { + if (!$DB->set_field("modules", "lastcron", $timenow, array("id"=>$mod->id))) { mtrace("Error: could not update timestamp for $mod->fullname"); } } @@ -134,7 +134,7 @@ mtrace("Finished activity modules"); mtrace("Starting blocks"); - if ($blocks = get_records_select("block", "cron > 0 AND (($timenow - lastcron) > cron) AND visible = 1")) { + if ($blocks = $DB->get_records_select("block", "cron > 0 AND ((? - lastcron) > cron) AND visible = 1", array($timenow))) { // we will need the base class. require_once($CFG->dirroot.'/blocks/moodleblock.class.php'); foreach ($blocks as $block) { @@ -146,7 +146,7 @@ if (method_exists($blockobj,'cron')) { mtrace("Processing cron function for ".$block->name.'....',''); if ($blockobj->cron()) { - if (!set_field('block','lastcron',$timenow,'id',$block->id)) { + if (!$DB->set_field('block', 'lastcron', $timenow, array('id'=>$block->id))) { mtrace('Error: could not update timestamp for '.$block->name); } } @@ -197,22 +197,22 @@ $somefound = false; // The preferred way saves memory, dmllib.php // find courses where limited enrolment is enabled - global $CFG; - $rs_enrol = get_recordset_sql("SELECT ra.roleid, ra.userid, ra.contextid - FROM {$CFG->prefix}course c - INNER JOIN {$CFG->prefix}context cx ON cx.instanceid = c.id - INNER JOIN {$CFG->prefix}role_assignments ra ON ra.contextid = cx.id - WHERE cx.contextlevel = '".CONTEXT_COURSE."' - AND ra.timeend > 0 - AND ra.timeend < '$timenow' - AND c.enrolperiod > 0 - "); - while ($oldenrolment = rs_fetch_next_record($rs_enrol)) { - role_unassign($oldenrolment->roleid, $oldenrolment->userid, 0, $oldenrolment->contextid); - $somefound = true; + $sql = "SELECT ra.roleid, ra.userid, ra.contextid + FROM {course} c + JOIN {context} cx ON cx.instanceid = c.id + JOIN {role_assignments} ra ON ra.contextid = cx.id + WHERE cx.contextlevel = '".CONTEXT_COURSE."' + AND ra.timeend > 0 + AND ra.timeend < ? + AND c.enrolperiod > 0"; + if ($rs = $DB->get_recordset_sql($sql, array($timenow))) { + foreach ($rs as $oldenrolment) { + role_unassign($oldenrolment->roleid, $oldenrolment->userid, 0, $oldenrolment->contextid); + $somefound = true; + } + $rs->close(); } - rs_close($rs_enrol); - if($somefound) { + if ($somefound) { mtrace('Done'); } else { mtrace('none found'); @@ -239,33 +239,33 @@ if ($CFG->longtimenosee) { // value in days $cuttime = $timenow - ($CFG->longtimenosee * 3600 * 24); - $rs = get_recordset_sql ("SELECT id, userid, courseid - FROM {$CFG->prefix}user_lastaccess - WHERE courseid != ".SITEID." - AND timeaccess < $cuttime "); - while ($assign = rs_fetch_next_record($rs)) { + $rs = $DB->get_recordset_sql ("SELECT id, userid, courseid + FROM {user_lastaccess} + WHERE courseid != ".SITEID." + AND timeaccess < ?", array($cuttime)); + foreach ($rs as $assign) { if ($context = get_context_instance(CONTEXT_COURSE, $assign->courseid)) { if (role_unassign(0, $assign->userid, 0, $context->id)) { mtrace("Deleted assignment for user $assign->userid from course $assign->courseid"); } } } - rs_close($rs); + $rs->close(); /// Execute the same query again, looking for remaining records and deleting them /// if the user hasn't moodle/course:view in the CONTEXT_COURSE context (orphan records) - $rs = get_recordset_sql ("SELECT id, userid, courseid - FROM {$CFG->prefix}user_lastaccess - WHERE courseid != ".SITEID." - AND timeaccess < $cuttime "); - while ($assign = rs_fetch_next_record($rs)) { + $rs = $DB->get_recordset_sql ("SELECT id, userid, courseid + FROM {user_lastaccess} + WHERE courseid != ".SITEID." + AND timeaccess < ?", array($cuttime)); + foreach ($rs as $assign) { if ($context = get_context_instance(CONTEXT_COURSE, $assign->courseid)) { if (!has_capability('moodle/course:view', $context, $assign->userid)) { - delete_records('user_lastaccess', 'userid', $assign->userid, 'courseid', $assign->courseid); + $DB->delete_records('user_lastaccess', array('userid'=>$assign->userid, 'courseid'=>$assign->courseid)); mtrace("Deleted orphan user_lastaccess for user $assign->userid from course $assign->courseid"); } } } - rs_close($rs); + $rs->close(); } flush(); @@ -274,17 +274,16 @@ if (!empty($CFG->deleteunconfirmed)) { $cuttime = $timenow - ($CFG->deleteunconfirmed * 3600); - $rs = get_recordset_sql ("SELECT id, firstname, lastname - FROM {$CFG->prefix}user - WHERE confirmed = 0 - AND firstaccess > 0 - AND firstaccess < $cuttime"); - while ($user = rs_fetch_next_record($rs)) { - if (delete_records('user', 'id', $user->id)) { + $rs = $DB->get_recordset_sql ("SELECT id, firstname, lastname + FROM {user} + WHERE confirmed = 0 AND firstaccess > 0 + AND firstaccess < ?", array($cuttime)); + foreach ($rs as $user) { + if ($DB->delete_records('user', array('id'=>$user->id))) { mtrace("Deleted unconfirmed user for ".fullname($user, true)." ($user->id)"); } } - rs_close($rs); + $rs->close(); } flush(); @@ -293,19 +292,18 @@ if (!empty($CFG->deleteunconfirmed)) { $cuttime = $timenow - ($CFG->deleteunconfirmed * 3600); - $rs = get_recordset_sql ("SELECT id, username - FROM {$CFG->prefix}user - WHERE confirmed = 1 - AND lastaccess > 0 - AND lastaccess < $cuttime - AND deleted = 0 - AND (lastname = '' OR firstname = '' OR email = '')"); - while ($user = rs_fetch_next_record($rs)) { - if (delete_records('user', 'id', $user->id)) { + $rs = $DB->get_recordset_sql ("SELECT id, username + FROM {user} + WHERE confirmed = 1 AND lastaccess > 0 + AND lastaccess < ? AND deleted = 0 + AND (lastname = '' OR firstname = '' OR email = '')", + array($cuttime)); + foreach ($rs as $user) { + if ($DB->delete_records('user', array('id'=>$user->id))) { mtrace("Deleted not fully setup user $user->username ($user->id)"); } } - rs_close($rs); + $rs->close(); } flush(); @@ -314,7 +312,7 @@ if (!empty($CFG->loglifetime)) { // value in days $loglifetime = $timenow - ($CFG->loglifetime * 3600 * 24); - if (delete_records_select("log", "time < '$loglifetime'")) { + if ($DB->delete_records_select("log", "time < ?", array($loglifetime))) { mtrace("Deleted old log records"); } } @@ -325,7 +323,7 @@ if (!empty($CFG->cachetext)) { // Defined in config.php $cachelifetime = time() - $CFG->cachetext - 60; // Add an extra minute to allow for really heavy sites - if (delete_records_select('cache_text', "timemodified < '$cachelifetime'")) { + if ($DB->delete_records_select('cache_text', "timemodified < ?", array($cachelifetime))) { mtrace("Deleted old cache_text records"); } } @@ -344,21 +342,21 @@ // generate new password emails for users // mtrace('checking for create_password'); - if (count_records('user_preferences', 'name', 'create_password', 'value', '1')) { + if ($DB->count_records('user_preferences', array('name'=>'create_password', 'value'=>'1'))) { mtrace('creating passwords for new users'); - $newusers = get_records_sql("SELECT u.id as id, u.email, u.firstname, - u.lastname, u.username, - p.id as prefid - FROM {$CFG->prefix}user u - JOIN {$CFG->prefix}user_preferences p ON u.id=p.userid - WHERE p.name='create_password' AND p.value=1 AND u.email !='' "); + $newusers = $DB->get_records_sql("SELECT u.id as id, u.email, u.firstname, + u.lastname, u.username, + p.id as prefid + FROM {user} u + JOIN {user_preferences} p ON u.id=p.userid + WHERE p.name='create_password' AND p.value=1 AND u.email !='' "); foreach ($newusers as $newuserid => $newuser) { $newuser->emailstop = 0; // send email regardless // email user if (setnew_password_and_mail($newuser)) { // remove user pref - delete_records('user_preferences', 'id', $newuser->prefid); + $DB->delete_records('user_preferences', array('id'=>$newuser->prefid)); } else { trigger_error("Could not create and mail new user password!"); } diff --git a/admin/dbperformance.php b/admin/dbperformance.php index 648a2ec8c9..3bfd4566af 100644 --- a/admin/dbperformance.php +++ b/admin/dbperformance.php @@ -3,6 +3,8 @@ require_once('../config.php'); +error('TODO: rewrite db perf code'); // TODO: rewrite + // disable moodle specific debug messages that would be breaking the frames disable_debugging(); -- 2.39.5