Source for file module_chat.php
Documentation is available at module_chat.php
// This file is part of the Sloodle project (www.sloodle.org)
* This file defines a chat module for Sloodle.
* @copyright Copyright (c) 2008 Sloodle (various contributors)
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
* @contributor Peter R. Bloomfield
/** The Sloodle module base. */
require_once(SLOODLE_LIBROOT.
'/modules/module_base.php');
/** General Sloodle functions. */
require_once(SLOODLE_LIBROOT.
'/general.php');
* The Sloodle chat module class.
* Internal for Moodle only - course module instance.
* Corresponds to one record from the Moodle 'course_modules' table.
* Internal only - Moodle chat module instance database object.
* Corresponds to one record from the Moodle 'chat' table.
parent::$constructor($_session);
* Loads data from the database.
* Note: even if the function fails, it may still have overwritten some or all existing data in the object.
* @param mixed $id The site-wide unique identifier for all modules. Type depends on VLE. On Moodle, it is an integer course module identifier ('id' field of 'course_modules' table)
* @return bool True if successful, or false otherwise
// Make sure the ID is valid
if ($id <=
0) return false;
// Fetch the course module data
if (!($this->cm =
get_coursemodule_from_id('chat', $id))) {
sloodle_debug("Failed to load course module instance #$id.<br/>");
// Make sure the module is visible
if ($this->cm->visible ==
0) {
sloodle_debug("Error: course module instance #$id not visible.<br/>");
// Load from the primary table: chat instance
sloodle_debug("Failed to load chatroom with instance ID #{$cm->instance}.<br/>
");
* Gets a recent history of messages from the chatroom.
* @param int $time How far back to search the database (in seconds) (default: 1 minute)
* @return array A numeric array of {@link SloodleChatMessage} object, in order of oldest to newest
// Calculate the earliest acceptable timestamp
$earliest =
time() -
$time;
// Get all message records for this chatroom
$recs =
get_records_select('chat_messages', "chatid = {$this->moodle_chat_instance->id} AND timestamp >=
$earliest", 'timestamp ASC');
if (!$recs) return array();
// We'll need to lookup all the user data.
// Cache the user records so we don't need to duplicate searches.
// This will be an associative array of user ID's to SloodleUser objects.
// Prepare an array of chat message objects
// Go through each result
// Do we already have the current user cached?
if (!isset
($usercache[$r->userid])) {
// No - query the database
if ($usercache[$r->userid]->load_user($r->userid)) {
// Attempt to load any linked avatar data too
$usercache[$r->userid]->load_linked_avatar();
// Construct and add a message object
$chatmessages[] =
new SloodleChatMessage($r->id, $r->message, $usercache[$r->userid], $r->timestamp);
* Adds a new chat message.
* <b>Note:</b> if the $author parameter is omitted or invalid, then the function will attempt to use the {@link SloodleUser} member
* of the current {@link SloodleSession} object;
* If that is unavailable, then it will try to use the user currently 'logged-in' to the VLE (i.e. the $USER variable in Moodle).
* If all else fails, it will attempt to attribute the message to the guest user.
* @param string $message The text of the message.
* @param mixed $user The user who wrote the message -- either a VLE user ID or (preferably) a {@link SloodleUser} object. If null, then the user in the current SloodleSession object will be used. At that fails, then the guest user is used if possible.
* @param int $timestamp Timestamp of the message. If omitted or <= 0 then the current timestamp is used
* @return bool True if successful, or false otherwise
function add_message($message, $user =
null, $timestamp =
null)
if (empty($message)) return false;
// Make sure the message is safe
// We need to get the user ID for the message
// Has a user object been provided?
// Yes - grab the user ID
$userid =
$user->get_user_id();
} else if ($user !=
null) {
// Did we end up with a valid user ID?
// No - do we have a user in the session parameter?
$userid =
$this->_session->user->get_user_id();
// Are we still lacking a valid user?
// Yes - user the guest user
if ($guest) $userid =
$guest->id;
// Prepare the timestamp variable if necessary
// Create a chat message record object
$rec->message =
$message;
$rec->timestamp =
$timestamp;
// Attempt to insert the chat message
$result =
insert_record('chat_messages', $rec);
if (!$result) return false;
// We successfully added a chat message
// If possible, add an appropriate side effect code to our response
$this->_session->response->add_side_effect(10101);
* Gets the name of this module instance.
* @return string The name of this controller
* Gets the intro description of this module instance, if available.
* @return string The intro description of this controller
* Gets the identifier of the course this controller belongs to.
* @return mixed Course identifier. Type depends on VLE. (In Moodle, it will be an integer).
* Gets the time at which this instance was created, or 0 if unknown.
* Gets the time at which this instance was last modified, or 0 if unknown.
* Gets the short type name of this instance.
* Gets the full type name of this instance, according to the current language pack, if available.
* Note: should be overridden by sub-classes.
* @return string Full type name if possible, or the short name otherwise.
return get_string('modulename', 'chat');
* Represents a single chat message
* Constructor - initialises members.
* @param mixed $id The ID of this message - type depends on VLE, but is typically an integer
* @param string $message The chat message
* @param SloodleUser $user The user who wrote the message
* @param int $timestamp The timestamp of the message
* Accessor - set all members in a single call.
* @param mixed $id The ID of this message - type depends on VLE, but is typically an integer
* @param string $message The chat message
* @param SloodleUser $user The user who wrote the message
* @param int $timestamp The timestamp of the message
function set($id, $message, $user, $timestamp)
* The type depends on the VLE, but typically is an integer.
* The text of the message.
* The user who wrote this message.
* Timestamp of the message.
Documentation generated on Mon, 16 Jun 2008 15:56:44 +0100 by phpDocumentor 1.4.0