From d3bf6f92f6b5d4042b5fba3b212cd5d621bc4848 Mon Sep 17 00:00:00 2001 From: skodak Date: Sun, 8 Jun 2008 15:49:36 +0000 Subject: [PATCH] MDL-15104 chat dml conversion --- mod/chat/backuplib.php | 45 +++---- mod/chat/chatd.php | 37 +++--- mod/chat/gui_basic/index.php | 15 ++- mod/chat/gui_header_js/chatinput.php | 4 +- mod/chat/gui_header_js/index.php | 4 +- mod/chat/gui_header_js/insert.php | 12 +- mod/chat/gui_header_js/jsupdate.php | 8 +- mod/chat/gui_header_js/jsupdated.php | 12 +- mod/chat/gui_header_js/users.php | 14 +- mod/chat/gui_sockets/chatinput.php | 4 +- mod/chat/gui_sockets/index.php | 4 +- mod/chat/index.php | 2 +- mod/chat/lib.php | 184 ++++++++++++++------------- mod/chat/report.php | 26 ++-- mod/chat/view.php | 4 +- 15 files changed, 192 insertions(+), 183 deletions(-) diff --git a/mod/chat/backuplib.php b/mod/chat/backuplib.php index c39b391682..6465af9762 100644 --- a/mod/chat/backuplib.php +++ b/mod/chat/backuplib.php @@ -23,13 +23,12 @@ //This function executes all the backup procedure about this mod function chat_backup_mods($bf,$preferences) { - - global $CFG; + global $CFG, $DB; $status = true; //Iterate over chat table - $chats = get_records ("chat","course",$preferences->backup_course,"id"); + $chats = $DB->get_records ("chat", array("course"=>$preferences->backup_course), "id"); if ($chats) { foreach ($chats as $chat) { if (backup_mod_selected($preferences,'chat',$chat->id)) { @@ -41,11 +40,10 @@ } function chat_backup_one_mod($bf,$preferences,$chat) { - - global $CFG; + global $CFG, $DB; if (is_numeric($chat)) { - $chat = get_record('chat','id',$chat); + $chat = $DB->get_record('chat', array('id'=>$chat)); } $status = true; @@ -74,12 +72,11 @@ //Backup chat_messages contents (executed from chat_backup_mods) function backup_chat_messages ($bf,$preferences,$chat) { - - global $CFG; + global $CFG, $DB; $status = true; - $chat_messages = get_records("chat_messages","chatid",$chat,"id"); + $chat_messages = $DB->get_records("chat_messages", array("chatid"=>$chat), "id"); //If there is messages if ($chat_messages) { //Write start tag @@ -175,33 +172,29 @@ //Returns an array of chats id function chat_ids ($course) { + global $DB; - global $CFG; - - return get_records_sql ("SELECT c.id, c.course - FROM {$CFG->prefix}chat c - WHERE c.course = '$course'"); + return $DB->get_records_sql("SELECT c.id, c.course + FROM {chat} c + WHERE c.course = ?", array($course)); } //Returns an array of assignment_submissions id function chat_message_ids_by_course ($course) { + global $DB; - global $CFG; - - return get_records_sql ("SELECT m.id , m.chatid - FROM {$CFG->prefix}chat_messages m, - {$CFG->prefix}chat c - WHERE c.course = '$course' AND - m.chatid = c.id"); + return $DB->get_records_sql("SELECT m.id , m.chatid + FROM {chat_messages} m, {chat} c + WHERE c.course = ? AND + m.chatid = c.id", array($course)); } //Returns an array of chat id function chat_message_ids_by_instance ($instanceid) { + global $DB; - global $CFG; - - return get_records_sql ("SELECT m.id , m.chatid - FROM {$CFG->prefix}chat_messages m - WHERE m.chatid = $instanceid"); + return $DB->get_records_sql("SELECT m.id , m.chatid + FROM {chat_messages} m + WHERE m.chatid = ?", array($instanceid)); } ?> diff --git a/mod/chat/chatd.php b/mod/chat/chatd.php index d8d34fa483..972063ff35 100755 --- a/mod/chat/chatd.php +++ b/mod/chat/chatd.php @@ -204,6 +204,8 @@ class ChatDaemon { } function user_lazy_update($sessionid) { + global $DB; + // TODO: this can and should be written as a single UPDATE query if(empty($this->sets_info[$sessionid])) { $this->trace('user_lazy_update() called for an invalid SID: '.$sessionid, E_USER_WARNING); @@ -217,7 +219,7 @@ class ChatDaemon { if($now - $this->sets_info[$sessionid]['lastinfocommit'] > $this->_freq_update_records) { // commit to permanent storage $this->sets_info[$sessionid]['lastinfocommit'] = $now; - update_record('chat_users', $this->sets_info[$sessionid]['chatuser']); + $DB->update_record('chat_users', $this->sets_info[$sessionid]['chatuser']); } return true; } @@ -320,7 +322,7 @@ class ChatDaemon { } function dispatch_sidekick($handle, $type, $sessionid, $customdata) { - global $CFG; + global $CFG, $DB; switch($type) { case CHAT_SIDEKICK_BEEP: @@ -334,7 +336,7 @@ class ChatDaemon { $msg->timestamp = time(); // Commit to DB - insert_record('chat_messages', $msg, false); + $DB->insert_record('chat_messages', $msg, false); // OK, now push it out to all users $this->message_broadcast($msg, $this->sets_info[$sessionid]['user']); @@ -425,10 +427,10 @@ class ChatDaemon { // A slight hack to prevent malformed SQL inserts $origmsg = $msg->message; - $msg->message = addslashes($msg->message); + $msg->message = $msg->message; // Commit to DB - insert_record('chat_messages', $msg, false); + $DB->insert_record('chat_messages', $msg, false); // Undo the hack $msg->message = $origmsg; @@ -467,31 +469,32 @@ class ChatDaemon { } function promote_final($sessionid, $customdata) { + global $DB; + if(isset($this->conn_sets[$sessionid])) { $this->trace('Set cannot be finalized: Session '.$sessionid.' is already active'); return false; } - $chatuser = get_record('chat_users', 'sid', $sessionid); + $chatuser = $DB->get_record('chat_users', array('sid'=>$sessionid)); if($chatuser === false) { $this->dismiss_half($sessionid); return false; } - $chat = get_record('chat', 'id', $chatuser->chatid); + $chat = $DB->get_record('chat', array('id'=>$chatuser->chatid)); if($chat === false) { $this->dismiss_half($sessionid); return false; } - $user = get_record('user', 'id', $chatuser->userid); + $user = $DB->get_record('user', array('id'=>$chatuser->userid)); if($user === false) { $this->dismiss_half($sessionid); return false; } - $course = get_record('course', 'id', $chat->course); { - if($course === false) { + $course = $DB->get_record('course', array('id'=>$chat->course)); + if($course === false) { $this->dismiss_half($sessionid); return false; - } } global $CHAT_HTMLHEAD_JS, $CFG; @@ -531,7 +534,7 @@ class ChatDaemon { // Finally, broadcast the "entered the chat" message - $msg = &New stdClass; + $msg = new stdClass; $msg->chatid = $chatuser->chatid; $msg->userid = $chatuser->userid; $msg->groupid = $chatuser->groupid; @@ -539,7 +542,7 @@ class ChatDaemon { $msg->message = 'enter'; $msg->timestamp = time(); - insert_record('chat_messages', $msg, false); + $DB->insert_record('chat_messages', $msg, false); $this->message_broadcast($msg, $this->sets_info[$sessionid]['user']); return true; @@ -732,9 +735,11 @@ class ChatDaemon { } function disconnect_session($sessionid) { + global $DB; + $info = $this->sets_info[$sessionid]; - delete_records('chat_users', 'sid', $sessionid); + $DB->delete_records('chat_users', array('sid'=>$sessionid)); $msg = &New stdClass; $msg->chatid = $info['chatid']; $msg->userid = $info['userid']; @@ -744,7 +749,7 @@ class ChatDaemon { $msg->timestamp = time(); $this->trace('User has disconnected, destroying uid '.$info['userid'].' with SID '.$sessionid, E_USER_WARNING); - insert_record('chat_messages', $msg, false); + $DB->insert_record('chat_messages', $msg, false); // *************************** IMPORTANT // @@ -957,7 +962,7 @@ else { $DAEMON->trace('Started Moodle chatd on port '.$CFG->chat_serverport.', listening socket '.$DAEMON->listen_socket, E_USER_WARNING); /// Clear the decks of old stuff -delete_records('chat_users', 'version', 'sockets'); +$DB->delete_records('chat_users', array('version'=>'sockets')); while(true) { $active = array(); diff --git a/mod/chat/gui_basic/index.php b/mod/chat/gui_basic/index.php index 51562b9c9d..4a64cbb31a 100644 --- a/mod/chat/gui_basic/index.php +++ b/mod/chat/gui_basic/index.php @@ -52,7 +52,7 @@ print_error('errornousers', 'chat'); } - set_field('chat_users', 'lastping', time(), 'sid', $chat_sid); + $DB->set_field('chat_users', 'lastping', time(), array('sid'=>$chat_sid)); if (!isset($SESSION->chatprefs)) { $SESSION->chatprefs = array(); @@ -80,11 +80,11 @@ $newmessage->systrem = 0; $newmessage->message = $message; $newmessage->timestamp = time(); - if (!insert_record('chat_messages', $newmessage)) { + if (!$DB->insert_record('chat_messages', $newmessage)) { print_error('cantinsert', 'chat'); } - set_field('chat_users', 'lastmessageping', time(), 'sid', $chat_sid); + $DB->set_field('chat_users', 'lastmessageping', time(), array('sid'=>$chat_sid)); add_to_log($course->id, 'chat', 'talk', "view.php?id=$cm->id", $chat->id, $cm->id); } @@ -141,15 +141,18 @@ $options->para = false; $options->newlines = true; + $params = array('last'=>$last, 'groupid'=>$groupid, 'chatid'=>$chat->id, 'chatentered'=>$chatentered); + if ($newonly) { - $lastsql = "AND timestamp > $last"; + $lastsql = "AND timestamp > :last"; } else { $lastsql = ""; } - $groupselect = $groupid ? "AND (groupid='$groupid' OR groupid='0')" : ""; + $groupselect = $groupid ? "AND (groupid=:groupid OR groupid=0)" : ""; + $messages = $DB->get_records_select("chat_messages", - "chatid = '$chat->id' AND timestamp > $chatentered $lastsql $groupselect", null, + "chatid = :chatid AND timestamp > :chatentered $lastsql $groupselect", $params, "timestamp DESC"); if ($messages) { diff --git a/mod/chat/gui_header_js/chatinput.php b/mod/chat/gui_header_js/chatinput.php index 7d37c5cda9..ea61d3f042 100644 --- a/mod/chat/gui_header_js/chatinput.php +++ b/mod/chat/gui_header_js/chatinput.php @@ -7,12 +7,12 @@ $chat_sid = required_param('chat_sid', PARAM_ALPHANUM); - if (!$chatuser = get_record('chat_users', 'sid', $chat_sid)) { + if (!$chatuser = $DB->get_record('chat_users', array('sid'=>$chat_sid))) { print_error('Not logged in!'); } //Get the user theme - $USER = get_record('user', 'id', $chatuser->userid); + $USER = $DB->get_record('user', array('id'=>$chatuser->userid)); //Setup course, lang and theme course_setup($chatuser->course); diff --git a/mod/chat/gui_header_js/index.php b/mod/chat/gui_header_js/index.php index da3ef18521..41fddcc60c 100644 --- a/mod/chat/gui_header_js/index.php +++ b/mod/chat/gui_header_js/index.php @@ -6,11 +6,11 @@ $id = required_param('id', PARAM_INT); $groupid = optional_param('groupid', 0, PARAM_INT); //only for teachers - if (!$chat = get_record('chat', 'id', $id)) { + if (!$chat = $DB->get_record('chat', array('id'=>$id))) { print_error('invalidid', 'chat'); } - if (!$course = get_record('course', 'id', $chat->course)) { + if (!$course = $DB->get_record('course', array('id'=>$chat->course))) { print_error('invalidcourseid'); } diff --git a/mod/chat/gui_header_js/insert.php b/mod/chat/gui_header_js/insert.php index a7f84873ed..29f89f1a20 100644 --- a/mod/chat/gui_header_js/insert.php +++ b/mod/chat/gui_header_js/insert.php @@ -6,15 +6,15 @@ $chat_sid = required_param('chat_sid', PARAM_ALPHANUM); $chat_message = required_param('chat_message', PARAM_RAW); - if (!$chatuser = get_record('chat_users', 'sid', $chat_sid)) { + if (!$chatuser = $DB->get_record('chat_users', array('sid'=>$chat_sid))) { print_error('Not logged in!'); } - if (!$chat = get_record('chat', 'id', $chatuser->chatid)) { + if (!$chat = $DB->get_record('chat', array('id'=>$chatuser->chatid))) { print_error('No chat found'); } - if (!$course = get_record('course', 'id', $chat->course)) { + if (!$course = $DB->get_record('course', array('id'=>$chat->course))) { print_error('Could not find the course this belongs to!'); } @@ -36,7 +36,7 @@ /// Clean up the message - $chat_message = addslashes(clean_text(stripslashes($chat_message), FORMAT_MOODLE)); // Strip bad tags + $chat_message = clean_text(stripslashes($chat_message), FORMAT_MOODLE); // Strip bad tags /// Add the message to the database @@ -49,12 +49,12 @@ $message->message = $chat_message; $message->timestamp = time(); - if (!insert_record('chat_messages', $message)) { + if (!$DB->insert_record('chat_messages', $message)) { print_error('Could not insert a chat message!'); } $chatuser->lastmessageping = time() - 2; - update_record('chat_users', $chatuser); + $DB->update_record('chat_users', $chatuser); if ($cm = get_coursemodule_from_instance('chat', $chat->id, $course->id)) { add_to_log($course->id, 'chat', 'talk', "view.php?id=$cm->id", $chat->id, $cm->id); diff --git a/mod/chat/gui_header_js/jsupdate.php b/mod/chat/gui_header_js/jsupdate.php index b8203c360c..b9616fcfaa 100644 --- a/mod/chat/gui_header_js/jsupdate.php +++ b/mod/chat/gui_header_js/jsupdate.php @@ -45,10 +45,12 @@ $timenow = time(); - $groupselect = $chatuser->groupid ? " AND (groupid='".$chatuser->groupid."' OR groupid='0') " : ""; + $params = array('groupid'=>$chatuser->groupid, 'chatid'=>$chatuser->chatid, 'lasttime'=>$chat_lasttime); + + $groupselect = $chatuser->groupid ? " AND (groupid=:groupid OR groupid=0) " : ""; $messages = $DB->get_records_select("chat_messages", - "chatid = '$chatuser->chatid' AND timestamp > '$chat_lasttime' $groupselect", null, + "chatid = :chatid AND timestamp > :lasttime $groupselect", $params, "timestamp ASC"); if ($messages) { @@ -119,7 +121,7 @@ } $chatuser->lastping = time(); - set_field('chat_users', 'lastping', $chatuser->lastping, 'id', $chatuser->id ); + $DB->set_field('chat_users', 'lastping', $chatuser->lastping, array('id'=>$chatuser->id)); if ($refreshusers) { echo "if (parent.users.document.anchors[0] != null) {" . diff --git a/mod/chat/gui_header_js/jsupdated.php b/mod/chat/gui_header_js/jsupdated.php index c17ce09d99..54d2758691 100644 --- a/mod/chat/gui_header_js/jsupdated.php +++ b/mod/chat/gui_header_js/jsupdated.php @@ -103,7 +103,7 @@ // ping first so we can later shortcut as needed. $chatuser->lastping = time(); - set_field('chat_users', 'lastping', $chatuser->lastping, 'id', $chatuser->id ); + $DB->set_field('chat_users', 'lastping', $chatuser->lastping, array('id'=>$chatuser->id)); if ($message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) { $chat_newlasttime = $message->timestamp; @@ -120,21 +120,21 @@ $timenow = time(); - - $groupselect = $chatuser->groupid ? " AND (groupid='".$chatuser->groupid."' OR groupid='0') " : ""; + $params = array('groupid'=>$chatuser->groupid, 'lastid'=>$chat_lastid, 'lasttime'=>$chat_lasttime, 'chatid'=>$chatuser->chatid); + $groupselect = $chatuser->groupid ? " AND (groupid=:groupid OR groupid=0) " : ""; $newcriteria = ''; if ($chat_lastid > 0) { - $newcriteria = "id > $chat_lastid"; + $newcriteria = "id > :lastid"; } else { if ($chat_lasttime == 0) { //display some previous messages $chat_lasttime = $timenow - $CFG->chat_old_ping; //TO DO - any better value?? } - $newcriteria = "timestamp > $chat_lasttime"; + $newcriteria = "timestamp > :lasttime"; } $messages = $DB->get_records_select("chat_messages", - "chatid = '$chatuser->chatid' AND $newcriteria $groupselect", null, + "chatid = :chatid AND $newcriteria $groupselect", $params, "timestamp ASC"); if ($messages) { diff --git a/mod/chat/gui_header_js/users.php b/mod/chat/gui_header_js/users.php index f21b90d639..df65d5d2dd 100644 --- a/mod/chat/gui_header_js/users.php +++ b/mod/chat/gui_header_js/users.php @@ -2,23 +2,23 @@ $nomoodlecookie = true; // Session not needed! - include('../../../config.php'); - include('../lib.php'); + require('../../../config.php'); + require('../lib.php'); $chat_sid = required_param('chat_sid', PARAM_ALPHANUM); $beep = optional_param('beep', 0, PARAM_INT); // beep target - if (!$chatuser = get_record('chat_users', 'sid', $chat_sid)) { + if (!$chatuser = $DB->get_record('chat_users', array('sid'=>$chat_sid))) { print_error('notlogged', 'chat'); } //Get the minimal course - if (!$course = get_record('course','id',$chatuser->course,'','','','','id,theme,lang')) { + if (!$course = $DB->get_record('course', array('id'=>$chatuser->course), 'id,theme,lang')) { print_error('invalidcourseid'); } //Get the user theme and enough info to be used in chat_format_message() which passes it along to - if (!$USER = get_record('user','id',$chatuser->userid)) { // no optimisation here, it would break again in future! + if (!$USER = $DB->get_record('user', array('id'=>$chatuser->userid))) { // no optimisation here, it would break again in future! print_error('invaliduser'); } $USER->description = ''; @@ -40,7 +40,7 @@ $message->system = 0; $message->timestamp = time(); - if (!insert_record('chat_messages', $message)) { + if (!$DB->insert_record('chat_messages', $message)) { print_error('cantinsert', 'chat'); } @@ -48,7 +48,7 @@ } $chatuser->lastping = time(); - set_field('chat_users', 'lastping', $chatuser->lastping, 'id', $chatuser->id ); + $DB->set_field('chat_users', 'lastping', $chatuser->lastping, array('id'=>$chatuser->id)); $refreshurl = "users.php?chat_sid=$chat_sid"; diff --git a/mod/chat/gui_sockets/chatinput.php b/mod/chat/gui_sockets/chatinput.php index 34f4da1685..f66b03d249 100644 --- a/mod/chat/gui_sockets/chatinput.php +++ b/mod/chat/gui_sockets/chatinput.php @@ -7,12 +7,12 @@ $chat_sid = required_param('chat_sid', PARAM_ALPHANUM); - if (!$chatuser = get_record('chat_users', 'sid', $chat_sid)) { + if (!$chatuser = $DB->get_record('chat_users', array('sid'=>$chat_sid))) { print_error('Not logged in!'); } //Get the user theme - $USER = get_record('user', 'id', $chatuser->userid); + $USER = $DB->get_record('user', array('id'=>$chatuser->userid)); //Setup course, lang and theme course_setup($chatuser->course); diff --git a/mod/chat/gui_sockets/index.php b/mod/chat/gui_sockets/index.php index 30bfd4430e..cad32ada67 100644 --- a/mod/chat/gui_sockets/index.php +++ b/mod/chat/gui_sockets/index.php @@ -6,11 +6,11 @@ $id = required_param('id', PARAM_INT); $groupid = optional_param('groupid', 0, PARAM_INT); //only for teachers - if (!$chat = get_record('chat', 'id', $id)) { + if (!$chat = $DB->get_record('chat', array('id'=>$id))) { print_error('Could not find that chat room!'); } - if (!$course = get_record('course', 'id', $chat->course)) { + if (!$course = $DB->get_record('course', array('id'=>$chat->course))) { print_error('Could not find the course this belongs to!'); } diff --git a/mod/chat/index.php b/mod/chat/index.php index 05c9ca77a4..945ee8c42e 100644 --- a/mod/chat/index.php +++ b/mod/chat/index.php @@ -5,7 +5,7 @@ $id = required_param('id', PARAM_INT); // course - if (! $course = get_record('course', 'id', $id)) { + if (! $course = $DB->get_record('course', array('id'=>$id))) { print_error('invalidcourseid'); } diff --git a/mod/chat/lib.php b/mod/chat/lib.php index 77d84240ed..81417fda26 100644 --- a/mod/chat/lib.php +++ b/mod/chat/lib.php @@ -124,35 +124,31 @@ function chat_user_outline($course, $user, $mod, $chat) { /// Used for user activity reports. /// $return->time = the time they did it /// $return->info = a short text description - - $return = NULL; - return $return; + return NULL; } function chat_user_complete($course, $user, $mod, $chat) { /// Print a detailed representation of what a user has done with /// a given particular instance of this module, for user activity reports. - return true; } function chat_print_recent_activity($course, $viewfullnames, $timestart) { /// Given a course and a date, prints a summary of all chat rooms past and present /// This function is called from course/lib.php: print_recent_activity() - - global $CFG, $USER; + global $CFG, $USER, $DB; // this is approximate only, but it is really fast ;-) $timeout = $CFG->chat_old_ping * 10; - if (!$mcms = get_records_sql("SELECT cm.id, MAX(chm.timestamp) AS lasttime - FROM {$CFG->prefix}course_modules cm - JOIN {$CFG->prefix}modules md ON md.id = cm.module - JOIN {$CFG->prefix}chat ch ON ch.id = cm.instance - JOIN {$CFG->prefix}chat_messages chm ON chm.chatid = ch.id - WHERE chm.timestamp > $timestart AND ch.course = {$course->id} AND md.name = 'chat' - GROUP BY cm.id - ORDER BY lasttime ASC")) { + if (!$mcms = $DB->get_records_sql("SELECT cm.id, MAX(chm.timestamp) AS lasttime + FROM {course_modules} cm + JOIN {modules} md ON md.id = cm.module + JOIN {chat} ch ON ch.id = cm.instance + JOIN {chat_messages} chm ON chm.chatid = ch.id + WHERE chm.timestamp > ? AND ch.course = ? AND md.name = 'chat' + GROUP BY cm.id + ORDER BY lasttime ASC", array($timestart, $course->id))) { return false; } @@ -195,13 +191,13 @@ function chat_print_recent_activity($course, $viewfullnames, $timestart) { $mygroupids = implode(',', $mygroupids); $cm->mygroupids = $mygroupids; - if (!$mcm = get_record_sql("SELECT cm.id, MAX(chm.timestamp) AS lasttime - FROM {$CFG->prefix}course_modules cm - JOIN {$CFG->prefix}chat ch ON ch.id = cm.instance - JOIN {$CFG->prefix}chat_messages chm ON chm.chatid = ch.id - WHERE chm.timestamp > $timestart AND cm.id = {$cm->id} AND - (chm.groupid IN ($mygroupids) OR chm.groupid = 0) - GROUP BY cm.id")) { + if (!$mcm = $DB->get_record_sql("SELECT cm.id, MAX(chm.timestamp) AS lasttime + FROM {course_modules} cm + JOIN {chat} ch ON ch.id = cm.instance + JOIN {chat_messages} chm ON chm.chatid = ch.id + WHERE chm.timestamp > ? AND cm.id = ? AND + (chm.groupid IN ($mygroupids) OR chm.groupid = 0) + GROUP BY cm.id", array($timestart, $cm->id))) { continue; } @@ -240,7 +236,9 @@ function chat_print_recent_activity($course, $viewfullnames, $timestart) { $timeoldext = time() - ($CFG->chat_old_ping*10); // JSless gui_basic needs much longer timeouts $timeoldext = floor($timeoldext/10)*10; // better db caching - $timeout = "AND (chu.version<>'basic' AND chu.lastping>$timeold) OR (chu.version='basic' AND chu.lastping>$timeoldext)"; + $params = array('timeold'=>$timeold, 'timeoldext'=>$timeoldext, 'cmid'=>$cm->id); + + $timeout = "AND (chu.version<>'basic' AND chu.lastping>:timeold) OR (chu.version='basic' AND chu.lastping>:timeoldext)"; foreach ($current as $cm) { //count users first @@ -250,13 +248,13 @@ function chat_print_recent_activity($course, $viewfullnames, $timestart) { $groupselect = ""; } - if (!$users = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email, u.picture - FROM {$CFG->prefix}course_modules cm - JOIN {$CFG->prefix}chat ch ON ch.id = cm.instance - JOIN {$CFG->prefix}chat_users chu ON chu.chatid = ch.id - JOIN {$CFG->prefix}user u ON u.id = chu.userid - WHERE cm.id = {$cm->id} $timeout $groupselect - GROUP BY u.id, u.firstname, u.lastname, u.email, u.picture")) { + if (!$users = $DB->get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email, u.picture + FROM {course_modules} cm + JOIN {chat} ch ON ch.id = cm.instance + JOIN {chat_users} chu ON chu.chatid = ch.id + JOIN {user} u ON u.id = chu.userid + WHERE cm.id = :cmid $timeout $groupselect + GROUP BY u.id, u.firstname, u.lastname, u.email, u.picture", $params)) { } $link = $CFG->wwwroot.'/mod/chat/view.php?id='.$cm->id; @@ -284,8 +282,7 @@ function chat_cron () { /// Function to be run periodically according to the moodle cron /// This function searches for things that need to be done, such /// as sending out mail, toggling flags etc ... - - global $CFG; + global $DB; chat_update_chat_times(); @@ -294,14 +291,14 @@ function chat_cron () { /// Delete old messages with a /// single SQL query. $subselect = "SELECT c.keepdays - FROM {$CFG->prefix}chat c - WHERE c.id = {$CFG->prefix}chat_messages.chatid"; + FROM {chat} c + WHERE c.id = {chat_messages}.chatid"; $sql = "DELETE - FROM {$CFG->prefix}chat_messages + FROM {chat_messages} WHERE ($subselect) > 0 AND timestamp < ( ".time()." -($subselect) * 24 * 3600)"; - execute_sql($sql, false); + $DB->execute($sql); return true; } @@ -309,21 +306,21 @@ function chat_cron () { function chat_get_participants($chatid, $groupid=0) { //Returns the users with data in one chat //(users with records in chat_messages, students) + global $DB; - global $CFG; + $params = array('groupid'=>$groupid, 'chatid'=>$chatid); if ($groupid) { - $groupselect = " AND (c.groupid='$groupid' OR c.groupid='0')"; + $groupselect = " AND (c.groupid=:groupid OR c.groupid='0')"; } else { $groupselect = ""; } //Get students - $students = get_records_sql("SELECT DISTINCT u.id, u.id - FROM {$CFG->prefix}user u, - {$CFG->prefix}chat_messages c - WHERE c.chatid = '$chatid' $groupselect - AND u.id = c.userid"); + $students = $DB->get_records_sql("SELECT DISTINCT u.id, u.id + FROM {user} u, {chat_messages} c + WHERE c.chatid = :chatid $groupselect + AND u.id = c.userid", $params); //Return students array (it contains an array of unique users) return ($students); @@ -335,25 +332,26 @@ function chat_refresh_events($courseid = 0) { // If courseid = 0, then every chat event in the site is checked, else // only chat events belonging to the course specified are checked. // This function is used, in its new format, by restore_refresh_events() + global $DB; if ($courseid) { - if (! $chats = get_records("chat", "course", $courseid)) { + if (! $chats = $DB->get_records("chat", array("course"=>$courseid))) { return true; } } else { - if (! $chats = get_records("chat")) { + if (! $chats = $DB->get_records("chat")) { return true; } } - $moduleid = get_field('modules', 'id', 'name', 'chat'); + $moduleid = $DB->get_field('modules', 'id', array('name'=>'chat')); foreach ($chats as $chat) { $event = NULL; - $event->name = addslashes($chat->name); - $event->description = addslashes($chat->intro); + $event->name = $chat->name; + $event->description = $chat->intro; $event->timestart = $chat->chattime; - if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) { + if ($event->id = $DB->get_field('event', 'id', array('modulename'=>'chat', 'instance'=>$chat->id))) { update_event($event); } else { @@ -364,7 +362,7 @@ function chat_refresh_events($courseid = 0) { $event->instance = $chat->id; $event->eventtype = $chat->schedule; $event->timeduration = 0; - $event->visible = get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $chat->id); + $event->visible = $DB->get_field('course_modules', 'visible', array('module'=>$moduleid, 'instance'=>$chat->id)); add_event($event); } @@ -377,50 +375,49 @@ function chat_refresh_events($courseid = 0) { /// Functions that require some SQL function chat_get_users($chatid, $groupid=0, $groupingid=0) { + global $DB; - global $CFG; + $params = array('chatid'=>$chatid, 'groupid'=>$groupid, 'groupingid'=>$groupingid); if ($groupid) { - $groupselect = " AND (c.groupid='$groupid' OR c.groupid='0')"; + $groupselect = " AND (c.groupid=:groupid OR c.groupid='0')"; } else { $groupselect = ""; } if (!empty($CFG->enablegroupings) && !(empty($groupingid))) { - $groupingjoin = "INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid - INNER JOIN {$CFG->prefix}groupings_groups gg ON gm.groupid = gg.groupid AND gg.groupingid = $groupingid "; + $groupingjoin = "JOIN {groups_members} gm ON u.id = gm.userid + JOIN {groupings_groups} gg ON gm.groupid = gg.groupid AND gg.groupingid = :groupingid "; } else { $groupingjoin = ''; } - return get_records_sql("SELECT DISTINCT u.id, u.firstname, u.lastname, u.picture, c.lastmessageping, c.firstping, u.imagealt - FROM {$CFG->prefix}chat_users c - INNER JOIN {$CFG->prefix}user u ON u.id = c.userid - $groupingjoin - WHERE c.chatid = '$chatid' - $groupselect - ORDER BY c.firstping ASC"); + return $DB->get_records_sql("SELECT DISTINCT u.id, u.firstname, u.lastname, u.picture, c.lastmessageping, c.firstping, u.imagealt + FROM {chat_users} c + JOIN {user} u ON u.id = c.userid + $groupingjoin + WHERE c.chatid = :chatid + $groupselect + ORDER BY c.firstping ASC", $params); } function chat_get_latest_message($chatid, $groupid=0) { global $DB; - $params = array(); + $params = array('chatid'=>$chatid, 'groupid'=>$groupid); if ($groupid) { - $groupselect = " AND (groupid=? OR groupid=0)"; - $params[] = $groupid; + $groupselect = "AND (groupid=:groupid OR groupid=0)"; } else { $groupselect = ""; } $sql = "SELECT * FROM {chat_messages} - WHERE chatid = ? + WHERE chatid = :chatid $groupselect ORDER BY timestamp DESC"; - $params[] = $chatid; return $DB->get_record_sql($sql, $params, true); } @@ -430,8 +427,9 @@ function chat_get_latest_message($chatid, $groupid=0) { // login if not already logged in function chat_login_user($chatid, $version, $groupid, $course) { - global $USER; - if (($version != 'sockets') and $chatuser = get_record_select('chat_users', "chatid='$chatid' AND userid='$USER->id' AND groupid='$groupid'")) { + global $USER, $DB; + + if (($version != 'sockets') and $chatuser = $DB->get_record('chat_users', array('chatid'=>$chatid, 'userid'=>$USER->id, 'groupid'=>$groupid))) { $chatuser->version = $version; $chatuser->ip = $USER->lastip; $chatuser->lastping = time(); @@ -451,7 +449,7 @@ function chat_login_user($chatid, $version, $groupid, $course) { or ($chatuser->userid != $USER->id)) { return false; } - if (!update_record('chat_users', $chatuser)) { + if (!$DB->update_record('chat_users', $chatuser)) { return false; } } else { @@ -477,7 +475,7 @@ function chat_login_user($chatid, $version, $groupid, $course) { } - if (!insert_record('chat_users', $chatuser)) { + if (!$DB->insert_record('chat_users', $chatuser)) { return false; } @@ -492,7 +490,7 @@ function chat_login_user($chatid, $version, $groupid, $course) { $message->system = 1; $message->timestamp = time(); - if (!insert_record('chat_messages', $message)) { + if (!$DB->insert_record('chat_messages', $message)) { print_error('cantinsert', 'chat'); } } @@ -503,16 +501,16 @@ function chat_login_user($chatid, $version, $groupid, $course) { function chat_delete_old_users() { // Delete the old and in the way - - global $CFG; + global $CFG, $DB; $timeold = time() - $CFG->chat_old_ping; $timeoldext = time() - ($CFG->chat_old_ping*10); // JSless gui_basic needs much longer timeouts - $query = "(version<>'basic' AND lastping<'$timeold') OR (version='basic' AND lastping<'$timeoldext')"; + $query = "(version<>'basic' AND lastpingget_records_select('chat_users', $query, $params) ) { + $DB->delete_records_select('chat_users', $query, $params); foreach ($oldusers as $olduser) { $message = new object(); $message->chatid = $olduser->chatid; @@ -522,7 +520,7 @@ function chat_delete_old_users() { $message->system = 1; $message->timestamp = time(); - if (!insert_record('chat_messages', $message)) { + if (!$DB->insert_record('chat_messages', $message)) { print_error('cantinsert', 'chat'); } } @@ -532,14 +530,18 @@ function chat_delete_old_users() { function chat_update_chat_times($chatid=0) { /// Updates chat records so that the next chat time is correct + global $DB; $timenow = time(); + + $params = array('timenow'=>$timenow, 'chatid'=>$chatid); + if ($chatid) { - if (!$chats[] = get_record_select("chat", "id = '$chatid' AND chattime <= '$timenow' AND schedule > '0'")) { + if (!$chats[] = $DB->get_record_select("chat", "id = :chatid AND chattime <= :timenow AND schedule > 0", $params)) { return; } } else { - if (!$chats = get_records_select("chat", "chattime <= '$timenow' AND schedule > '0'")) { + if (!$chats = $DB->get_records_select("chat", "chattime <= :timenow AND schedule > 0", $params)) { return; } } @@ -563,12 +565,14 @@ function chat_update_chat_times($chatid=0) { } break; } - update_record("chat", $chat); + $DB->update_record("chat", $chat); + + $event = new object(); // Update calendar too - $event = NULL; // Update calendar too - $cond = "modulename='chat' AND instance = {$chat->id} - AND timestart != {$chat->chattime}"; - if ($event->id = get_field_select('event', 'id', $cond)) { + $cond = "modulename='chat' AND instance = :chatid AND timestart <> :chattime"; + $params = array('chattime'=>$chat->chattime, 'chatid'=>$chatid); + + if ($event->id = $DB->get_field_select('event', 'id', $cond, $params)) { $event->timestart = $chat->chattime; update_event($event); } @@ -688,12 +692,13 @@ function chat_format_message($message, $courseid, $currentuser, $chat_lastrow=NU /// Given a message object full of information, this function /// formats it appropriately into text and html, then /// returns the formatted data. + global $DB; static $users; // Cache user lookups if (isset($users[$message->userid])) { $user = $users[$message->userid]; - } else if ($user = get_record('user', 'id', $message->userid, '','','','','id,picture,firstname,lastname')) { + } else if ($user = $DB->get_record('user', array('id'=>$message->userid), 'id,picture,firstname,lastname')) { $users[$message->userid] = $user; } else { return NULL; @@ -765,18 +770,19 @@ function chat_reset_course_form_defaults($course) { * @return array status array */ function chat_reset_userdata($data) { - global $CFG; + global $CFG, $DB; $componentstr = get_string('modulenameplural', 'chat'); $status = array(); if (!empty($data->reset_chat)) { $chatessql = "SELECT ch.id - FROM {$CFG->prefix}chat ch - WHERE ch.course={$data->courseid}"; + FROM {chat} ch + WHERE ch.course=?"; + $params = array($data->courseid); - delete_records_select('chat_messages', "chatid IN ($chatessql)"); - delete_records_select('chat_users', "chatid IN ($chatessql)"); + $DB->delete_records_select('chat_messages', "chatid IN ($chatessql)", $params); + $DB->delete_records_select('chat_users', "chatid IN ($chatessql)", $params); $status[] = array('component'=>$componentstr, 'item'=>get_string('removemessages', 'chat'), 'error'=>false); } diff --git a/mod/chat/report.php b/mod/chat/report.php index 25e71f1d98..93bd0b69f5 100644 --- a/mod/chat/report.php +++ b/mod/chat/report.php @@ -14,10 +14,10 @@ if (! $cm = get_coursemodule_from_id('chat', $id)) { print_error('invalidcoursemodule'); } - if (! $chat = get_record('chat', 'id', $cm->instance)) { + if (! $chat = $DB->get_record('chat', array('id'=>$cm->instance))) { print_error('invalidcoursemodule'); } - if (! $course = get_record('course', 'id', $chat->course)) { + if (! $course = $DB->get_record('course', array('id'=>$chat->course))) { print_error('coursemisconf'); } @@ -48,9 +48,10 @@ $currentgroup = groups_get_activity_group($cm, true); groups_print_activity_menu($cm, "report.php?id=$cm->id"); + $params = array('currentgroup'=>$currentgroup, 'chatid'=>$chat->id, 'start'=>$start, 'end'=>$end); if ($currentgroup) { - $groupselect = " AND groupid = '$currentgroup'"; + $groupselect = " AND groupid = :currentgroup"; } else { $groupselect = ""; } @@ -61,9 +62,9 @@ "report.php?id=$cm->id"); } - if (!$messages = get_records_select('chat_messages', "chatid = $chat->id AND - timestamp >= '$start' AND - timestamp <= '$end' $groupselect", "timestamp ASC")) { + if (!$messages = $DB->get_records_select('chat_messages', "chatid = :chatid AND + timestamp >= :start AND + timestamp <= :end $groupselect", "timestamp ASC", $params)) { print_heading(get_string('nomessages', 'chat')); } else { @@ -104,8 +105,10 @@ $currentgroup = false; } + $params = array('currentgroup'=>$currentgroup, 'chatid'=>$chat->id, 'start'=>$start, 'end'=>$end); + if (!empty($currentgroup)) { - $groupselect = " AND groupid = '$currentgroup'"; + $groupselect = " AND groupid = :currentgroup"; } else { $groupselect = ""; } @@ -113,9 +116,8 @@ /// Delete a session if one has been specified if ($deletesession and has_capability('mod/chat:deletelog', $context) and $confirmdelete and $start and $end and confirm_sesskey()) { - delete_records_select('chat_messages', "chatid = $chat->id AND - timestamp >= '$start' AND - timestamp <= '$end' $groupselect"); + $DB->delete_records_select('chat_messages', "chatid = :chatid AND timestamp >= :start AND + timestamp <= :end $groupselect", $params); $strdeleted = get_string('deleted'); notify("$strdeleted: ".userdate($start).' --> '. userdate($end)); unset($deletesession); @@ -125,7 +127,7 @@ /// Get the messages if (empty($messages)) { /// May have already got them above - if (!$messages = get_records_select('chat_messages', "chatid = '$chat->id' $groupselect", "timestamp DESC")) { + if (!$messages = $DB->get_records_select('chat_messages', "chatid = :chatid $groupselect", $params, "timestamp DESC")) { print_heading(get_string('nomessages', 'chat')); print_footer($course); exit; @@ -171,7 +173,7 @@ arsort($sessionusers); foreach ($sessionusers as $sessionuser => $usermessagecount) { - if ($user = get_record('user', 'id', $sessionuser)) { + if ($user = $DB->get_record('user', array('id'=>$sessionuser))) { print_user_picture($user, $course->id, $user->picture); echo ' '.fullname($user, true); // XXX TODO use capability instead of true echo " ($usermessagecount)
"; diff --git a/mod/chat/view.php b/mod/chat/view.php index bef45b07f8..1def50e531 100644 --- a/mod/chat/view.php +++ b/mod/chat/view.php @@ -32,7 +32,7 @@ if (! $chat = $DB->get_record('chat', array('id'=>$c))) { print_error('coursemisconf'); } - if (! $course = get_record('course', array('id'=>$chat->course))) { + if (! $course = $DB->get_record('course', array('id'=>$chat->course))) { print_error('coursemisconf'); } if (! $cm = get_coursemodule_from_instance('chat', $chat->id, $course->id)) { @@ -113,10 +113,8 @@ groups_print_activity_menu($cm, "view.php?id=$cm->id"); if ($currentgroup) { - $groupselect = " AND groupid = '$currentgroup'"; $groupparam = "&groupid=$currentgroup"; } else { - $groupselect = ""; $groupparam = ""; } -- 2.39.5