|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00017 define('AJAX_SCRIPT', true); 00018 00019 require_once(dirname(dirname(dirname(__FILE__))) . '/config.php'); 00020 require_once(dirname(__FILE__) . '/lib.php'); 00021 00022 $action = optional_param('action', '', PARAM_ALPHANUM); 00023 $beep_id = optional_param('beep', '', PARAM_RAW); 00024 $chat_sid = required_param('chat_sid', PARAM_ALPHANUM); 00025 $theme = required_param('theme', PARAM_ALPHANUM); 00026 $chat_message = optional_param('chat_message', '', PARAM_RAW); 00027 $chat_lasttime = optional_param('chat_lasttime', 0, PARAM_INT); 00028 $chat_lastrow = optional_param('chat_lastrow', 1, PARAM_INT); 00029 00030 if (!confirm_sesskey()) { 00031 throw new moodle_exception('invalidsesskey', 'error'); 00032 } 00033 00034 if (!$chatuser = $DB->get_record('chat_users', array('sid'=>$chat_sid))) { 00035 throw new moodle_exception('notlogged', 'chat'); 00036 } 00037 if (!$chat = $DB->get_record('chat', array('id'=>$chatuser->chatid))) { 00038 throw new moodle_exception('invaliduserid', 'error'); 00039 } 00040 if (!$course = $DB->get_record('course', array('id'=>$chat->course))) { 00041 throw new moodle_exception('invalidcourseid', 'error'); 00042 } 00043 if (!$cm = get_coursemodule_from_instance('chat', $chat->id, $course->id)) { 00044 throw new moodle_exception('invalidcoursemodule', 'error'); 00045 } 00046 00047 if (!isloggedin()) { 00048 throw new moodle_exception('notlogged', 'chat'); 00049 } 00050 00051 // setup $PAGE so that format_text will work properly 00052 $PAGE->set_cm($cm, $course, $chat); 00053 $PAGE->set_url('/mod/chat/chat_ajax.php', array('chat_sid'=>$chat_sid)); 00054 00055 ob_start(); 00056 header('Expires: Sun, 28 Dec 1997 09:32:45 GMT'); 00057 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); 00058 header('Cache-Control: no-cache, must-revalidate'); 00059 header('Pragma: no-cache'); 00060 header('Content-Type: text/html; charset=utf-8'); 00061 00062 switch ($action) { 00063 case 'init': 00064 $users = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid); 00065 $users = chat_format_userlist($users, $course); 00066 $response['users'] = $users; 00067 echo json_encode($response); 00068 break; 00069 00070 case 'chat': 00071 session_get_instance()->write_close(); 00072 chat_delete_old_users(); 00073 $chat_message = clean_text($chat_message, FORMAT_MOODLE); 00074 00075 if (!empty($beep_id)) { 00076 $chat_message = 'beep '.$beep_id; 00077 } 00078 00079 if (!empty($chat_message)) { 00080 $message = new stdClass(); 00081 $message->chatid = $chatuser->chatid; 00082 $message->userid = $chatuser->userid; 00083 $message->groupid = $chatuser->groupid; 00084 $message->message = $chat_message; 00085 $message->timestamp = time(); 00086 00087 $chatuser->lastmessageping = time() - 2; 00088 $DB->update_record('chat_users', $chatuser); 00089 00090 $DB->insert_record('chat_messages', $message); 00091 $DB->insert_record('chat_messages_current', $message); 00092 // response ok message 00093 echo json_encode(true); 00094 add_to_log($course->id, 'chat', 'talk', "view.php?id=$cm->id", $chat->id, $cm->id); 00095 00096 ob_end_flush(); 00097 } 00098 break; 00099 00100 case 'update': 00101 if ((time() - $chat_lasttime) > $CFG->chat_old_ping) { 00102 chat_delete_old_users(); 00103 } 00104 00105 if ($latest_message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) { 00106 $chat_newlasttime = $latest_message->timestamp; 00107 } else { 00108 $chat_newlasttime = 0; 00109 } 00110 00111 if ($chat_lasttime == 0) { 00112 $chat_lasttime = time() - $CFG->chat_old_ping; 00113 } 00114 00115 $params = array('groupid'=>$chatuser->groupid, 'chatid'=>$chatuser->chatid, 'lasttime'=>$chat_lasttime); 00116 00117 $groupselect = $chatuser->groupid ? " AND (groupid=".$chatuser->groupid." OR groupid=0) " : ""; 00118 00119 $messages = $DB->get_records_select('chat_messages_current', 00120 'chatid = :chatid AND timestamp > :lasttime '.$groupselect, $params, 00121 'timestamp ASC'); 00122 00123 if (!empty($messages)) { 00124 $num = count($messages); 00125 } else { 00126 $num = 0; 00127 } 00128 $chat_newrow = ($chat_lastrow + $num) % 2; 00129 $send_user_list = false; 00130 if ($messages && ($chat_lasttime != $chat_newlasttime)) { 00131 foreach ($messages as $n => &$message) { 00132 $tmp = new stdClass(); 00133 // when somebody enter room, user list will be updated 00134 if (!empty($message->system)){ 00135 $send_user_list = true; 00136 $users = chat_format_userlist(chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid), $course); 00137 } 00138 if ($html = chat_format_message_theme($message, $chatuser, $USER, $cm->groupingid, $theme)) { 00139 $message->mymessage = ($USER->id == $message->userid); 00140 $message->message = $html->html; 00141 if (!empty($html->type)) { 00142 $message->type = $html->type; 00143 } 00144 } else { 00145 unset($messages[$n]); 00146 } 00147 } 00148 } 00149 00150 if(!empty($users) && $send_user_list){ 00151 // return users when system message coming 00152 $response['users'] = $users; 00153 } 00154 00155 $DB->set_field('chat_users', 'lastping', time(), array('id'=>$chatuser->id)); 00156 00157 $response['lasttime'] = $chat_newlasttime; 00158 $response['lastrow'] = $chat_newrow; 00159 if($messages){ 00160 $response['msgs'] = $messages; 00161 } 00162 00163 echo json_encode($response); 00164 header('Content-Length: ' . ob_get_length() ); 00165 00166 ob_end_flush(); 00167 break; 00168 00169 default: 00170 break; 00171 }