# iterate through the requests and create responses $responses = array(); foreach ($requests->requests as $request) { try { # all responses need id of the request and client instance $response = (object) array('id' => $request->id); if (isset($request->instanceId)) { $response->instanceId = $request->instanceId; } # verify session where applicable switch ($request->function) { case 'message': case 'sysmessage': case 'refresh': case 'last_read': if (!($session2user = helpmenow_get_s2u($request->session))) { throw new Exception('Could not get session2user record'); } #echo "verrified session2user: "******"\n"; # DEBUGGING break; } # generate response switch ($request->function) { case 'message': case 'sysmessage': case 'refresh': case 'block': case 'motd': case 'plugin': case 'last_read': $function = 'helpmenow_serverfunc_' . $request->function;
/** * chat refresh * * @param object $request request from client * @param object $response response */ function helpmenow_serverfunc_refresh($request, &$response) { global $USER, $CFG, $DB; $session2user = helpmenow_get_s2u($request->session); # unless something has gone wrong, we should already have a response ready: if ($request->last_message == $session2user->optimistic_last_message) { $response_cache = json_decode($session2user->cache); $response = (object) array_merge((array) $response, (array) $response_cache); } else { # unread messages $messages = helpmenow_get_unread($request->session, $USER->id); # todo: move this to the client if ($messages) { if (!isset($response->html)) { $response->html = ''; } $response->html .= helpmenow_format_messages($messages); # determine if we need to beep foreach ($messages as $m) { if ($m->notify) { $response->title_flash = format_string($m->message); $response->beep = true; } $response->last_message = $m->id; } } else { $response->last_message = $request->last_message; } } if (!isset($response->last_message)) { $response->last_message = null; } /** * if there aren't any new messages check to see if we should add a "sent: _time_" message * * yes, it sucks, it's one of the many things todo: move to client * -dzaharee */ if ($response->last_message == $request->last_message) { $sql = "\n SELECT *\n FROM {block_helpmenow_message}\n WHERE id = (\n SELECT max(id)\n FROM {block_helpmenow_message}\n WHERE sessionid = {$request->session}\n )"; if ($last_message = $DB->get_record_sql($sql)) { if (!is_null($last_message->userid) and $last_message->time < time() - 30) { $message = get_string('sent', 'block_helpmenow') . ': ' . userdate($last_message->time, '%r'); helpmenow_message($request->session, null, $message, 0); } } } # update session2user $session2user->last_message = $request->last_message; $session2user->optimistic_last_message = $response->last_message; $session2user->last_refresh = time(); $session2user->cache = json_encode((object) array('html' => '', 'beep' => false, 'last_message' => $request->last_message)); if (!$DB->update_record('block_helpmenow_session2user', $session2user)) { $response->html = ''; $response->beep = false; $response->last_message = $request->last_message; throw new Exception('Could not update session2user record'); } # call subplugin on_chat_refresh methods foreach (helpmenow_plugin::get_plugins() as $pluginname) { $class = "helpmenow_plugin_{$pluginname}"; $class::on_chat_refresh($request, $response); } }