Example #1
0
    $history_url = new moodle_url("{$CFG->wwwroot}/blocks/helpmenow/history.php#recent");
    $history_url->param('session', $sessionid);
    $history_url->param('date', '-1 year');
    $action = new popup_action('click', $history_url->out(), $sessionid, array('height' => 400, 'width' => 500));
    $history_link = $OUTPUT->action_link($history_url->out(), $history_name, $action);
    $history_link = '<div>' . $history_link . '</div>';
} else {
    $history_link = '';
}
$textarea_message = get_string('textarea_message', 'block_helpmenow');
# default text in chat input box
$jplayer = helpmenow_jplayer();
# jquery plugin for bell sound
$version = HELPMENOW_CLIENT_VERSION;
if ($history = helpmenow_get_history($sessionid)) {
    $messages = helpmenow_format_messages($history);
    foreach ($history as $m) {
        $last_message = $m->id;
    }
} else {
    $messages = '';
    $last_message = 0;
}
$messages = $history_link . $messages;
echo <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
    <head>
        <title>{$title}</title>
        <link rel="stylesheet" type="text/css" href="{$CFG->wwwroot}/blocks/helpmenow/style/chat.css" />
Example #2
0
/**
 * 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);
    }
}