From ff7255e14c650bd6b79531adb601550c90f93579 Mon Sep 17 00:00:00 2001 From: dongsheng Date: Wed, 19 Nov 2008 07:58:48 +0000 Subject: [PATCH] CHAT_MOD/MDL-14651 jump to mod front page when error occors. --- mod/chat/gui_ajax/common.php | 10 ++++++++++ mod/chat/gui_ajax/index.php | 2 +- mod/chat/gui_ajax/script.js | 8 +++++++- mod/chat/gui_ajax/update.php | 18 +++++++----------- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/mod/chat/gui_ajax/common.php b/mod/chat/gui_ajax/common.php index c9bbf666ee..8f72fb8b7a 100755 --- a/mod/chat/gui_ajax/common.php +++ b/mod/chat/gui_ajax/common.php @@ -17,6 +17,16 @@ function format_user_list($data, $course) { } return $users; } +function chat_print_error($level, $msg) { + header('Content-Length: ' . ob_get_length() ); + $error = new stdclass; + $error->level = $level; + $error->msg = $msg; + $response['error'] = $error; + echo json_encode($response); + ob_end_flush(); + exit; +} class file_cache{ private $dir = ''; diff --git a/mod/chat/gui_ajax/index.php b/mod/chat/gui_ajax/index.php index 6763450ab4..4d924c5d31 100644 --- a/mod/chat/gui_ajax/index.php +++ b/mod/chat/gui_ajax/index.php @@ -65,7 +65,7 @@ $str_userlist = get_string('userlist', 'chat'); $USER->id, 'sid'=>$chat_sid,'timer'=>5000, 'chat_lasttime'=>0,'chat_lastrow'=>null,'header_title'=>$str_chat,'chatroom_name'=>$str_title), 'chat_cfg'); +print_js_config(array('home'=>$CFG->httpswwwroot.'/mod/chat/view.php?id='.$cm->id, 'userid'=>$USER->id, 'sid'=>$chat_sid,'timer'=>5000, 'chat_lasttime'=>0,'chat_lastrow'=>null,'header_title'=>$str_chat,'chatroom_name'=>$str_title), 'chat_cfg'); print_js_config(array('send'=>$str_send, 'sending'=>$str_sending, 'inputarea'=>$str_inputarea, 'userlist'=>$str_userlist), 'chat_lang'); ?> diff --git a/mod/chat/gui_ajax/script.js b/mod/chat/gui_ajax/script.js index 15bc3adc6a..318d6c80a2 100644 --- a/mod/chat/gui_ajax/script.js +++ b/mod/chat/gui_ajax/script.js @@ -113,7 +113,7 @@ function update_info() { function append_msg(msg) { var list = document.getElementById('msg_list'); var el = document.createElement('li'); - console.info('New message:'+msg); + console.info('New message:'+msg.msg); el.innerHTML = msg.msg; if(msg.type && msg.type == 'beep'){ document.getElementById('notify').innerHTML = ''; @@ -148,6 +148,12 @@ success: function(o){ alert(o.responseText); return; } + if(data.error) { + if(data.error.level == 'ERROR'){ + clearInterval(interval); + window.location = chat_cfg.home; + } + } if(!data) return false; chat_cfg.chat_lasttime = data['lasttime']; diff --git a/mod/chat/gui_ajax/update.php b/mod/chat/gui_ajax/update.php index c60b44f2ab..f2780d738c 100755 --- a/mod/chat/gui_ajax/update.php +++ b/mod/chat/gui_ajax/update.php @@ -9,11 +9,14 @@ require_once('../../../config.php'); require_once('../lib.php'); require_once('common.php'); +ob_start(); header('Expires: Sun, 28 Dec 1997 09:32:45 GMT'); header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); header('Cache-Control: no-cache, must-revalidate'); header('Pragma: no-cache'); header('Content-Type: text/html; charset=utf-8'); +header('X-Powered-By: MOODLE-Chat-V2'); + $time_start = microtime_float(); @@ -25,22 +28,22 @@ $chat_lastrow = optional_param('chat_lastrow', 1, PARAM_INT); $response = array(); if (!$chatuser = $DB->get_record('chat_users', array('sid'=>$chat_sid))) { - $response['error'] = get_string('notlogged', 'chat'); + chat_print_error('ERROR', get_string('notlogged','chat')); } //Get the minimal course if (!$course = $DB->get_record('course', array('id'=>$chatuser->course), 'id,theme,lang')) { - $response['error'] = get_string('invalidcourseid', 'error'); + chat_print_error('ERROR', get_string('invalidcourseid', 'error')); } //Get the user theme and enough info to be used in chat_format_message() which passes it along to if (!$USER = $DB->get_record('user', array('id'=>$chatuser->userid))) { // no optimisation here, it would break again in future! - $response['error'] = get_string('invaliduserid', 'error'); + chat_print_error('ERROR', get_string('invaliduserid', 'error')); } if (!$cm = get_coursemodule_from_instance('chat', $chatuser->chatid, $course->id)) { - $response['error'] = get_string('invalidcoursemodule', 'error'); + chat_print_error('ERROR', get_string('invalidcoursemodule', 'error')); } if($CFG->chat_use_cache){ @@ -54,10 +57,6 @@ if($CFG->chat_use_cache){ $users = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid); } -if (empty($users)) { - $response['error'] = get_string('nousers', 'error'); -} - $users = format_user_list($users, $course); if(!empty($chat_init)) { @@ -98,8 +97,6 @@ if (!empty($messages)) { $chat_newrow = ($chat_lastrow + $num) % 2; -ob_start(); - $send_user_list = false; if ($messages && ($chat_lasttime != $chat_newlasttime)) { foreach ($messages as $n => &$message) { @@ -146,7 +143,6 @@ if(!empty($CFG->chat_ajax_debug)) { echo json_encode($response); -header('X-Powered-By: MOODLE-Chat-V2'); header('Content-Length: ' . ob_get_length() ); ob_end_flush(); -- 2.39.5