Commit 6d5c8534 authored by Evan Goldenberg's avatar Evan Goldenberg Committed by Richard Mansfield
Browse files

add basic message threading



When replying to a message from another user, the full message history
is now shown, if it is available (recorded in the database).
Signed-off-by: default avatarEvan Goldenberg <evang@catalyst.net.nz>
parent 6ad736c6
......@@ -299,6 +299,7 @@ abstract class ActivityType {
protected $activityname;
protected $cron;
protected $overridemessagecontents;
protected $parent;
public function get_id() {
if (!isset($this->id)) {
......@@ -643,6 +644,7 @@ class ActivityTypeUsermessage extends ActivityType {
* - userfrom (int)
* - subject (string)
* - message (string)
* - parent (int)
*/
public function __construct($data, $cron=false) {
parent::__construct($data, $cron);
......@@ -658,7 +660,7 @@ class ActivityTypeUsermessage extends ActivityType {
}
protected function update_url($internalid) {
$this->url = get_config('wwwroot') . 'user/sendmessage.php?id=' . $this->userfrom . '&replyto=' . $internalid;
$this->url = get_config('wwwroot') . 'user/sendmessage.php?id=' . $this->userfrom . '&replyto=' . $internalid . '&returnto=inbox';
return true;
}
......
......@@ -1609,5 +1609,10 @@ function xmldb_core_upgrade($oldversion=0) {
insert_record('cron', $cron);
}
if ($oldversion < 2010042600) {
execute_sql('ALTER TABLE {notification_internal_activity} ADD COLUMN parent BIGINT');
execute_sql('ALTER TABLE {notification_internal_activity} ADD CONSTRAINT {notiinteacti_par_fk} FOREIGN KEY (parent) REFERENCES {notification_internal_activity}(id)');
}
return $status;
}
......@@ -1137,6 +1137,18 @@ function activate_user($userid) {
handle_event('activateuser', $userid);
}
/**
* Get the thread of message up to this point, given the id of
* the message being replied to.
*/
function get_message_thread($replyto) {
$message = get_record('notification_internal_activity', 'id', $replyto);
if (!isset($message->parent)) {
return array($message);
}
return array_merge(get_message_thread($message->parent), array($message));
}
/**
* Sends a message from one user to another
*
......@@ -1147,7 +1159,7 @@ function activate_user($userid) {
* @throws AccessDeniedException if the message is not allowed to be sent (as
* configured by the 'to' user's settings)
*/
function send_user_message($to, $message, $from=null) {
function send_user_message($to, $message, $parent, $from=null) {
// FIXME: permission checking!
if ($from === null) {
global $USER;
......@@ -1162,6 +1174,7 @@ function send_user_message($to, $message, $from=null) {
'userto' => $to->id,
'userfrom' => $from->id,
'message' => $message,
'parent' => $parent,
)
);
}
......
......@@ -28,7 +28,7 @@
defined('INTERNAL') || die();
$config = new StdClass;
$config->version = 2010041900;
$config->version = 2010042600;
$config->release = '1.3.0dev';
$config->minupgradefrom = 2008040200;
$config->minupgraderelease = '1.0.0 (release tag 1.0.0_RELEASE)';
......
......@@ -14,11 +14,13 @@
<FIELD NAME="message" TYPE="text" NOTNULL="false" />
<FIELD NAME="url" TYPE="text" NOTNULL="false" />
<FIELD NAME="read" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" />
<FIELD NAME="parent" TYPE="int" LENGTH="10" NOTNULL="false" />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
<KEY NAME="typefk" TYPE="foreign" FIELDS="type" REFTABLE="activity_type" REFFIELDS="id" />
<KEY NAME="userfk" TYPE="foreign" FIELDS="usr" REFTABLE="usr" REFFIELDS="id" />
<KEY NAME="parentfk" TYPE="foreign" FIELDS="parent" REFTABLE="notification_internal_activity" REFFIELDS="id" />
</KEYS>
</TABLE>
</TABLES>
......
......@@ -41,6 +41,7 @@ class PluginNotificationInternal extends PluginNotification {
}
$toinsert->message = $data->message;
$toinsert->subject = $data->subject;
$toinsert->parent = $data->parent;
$toinsert->ctime = db_format_timestamp(time());
if (!empty($data->url)) {
......
......@@ -195,6 +195,10 @@ td.narrow {
#friendslist td {
border-bottom: 2px solid #FFF;
}
/* table with preserved whitespace */
#messagethread td {
white-space: pre;
}
/* table without border */
#bloglist th {
border: 0;
......
{include file="header.tpl"}
{include file="user/simpleuser.tpl" user=$user}
{if $replyto}
<h4>{$replyto->subject|escape}:</h4>
<br>
{foreach from=$replyto->lines item=line}
{$line|escape}<br>
{/foreach}
{if $messages}
<table id="messagethread" class="fullwidth listing">
<tbody>
{foreach from=$messages item=message}
<tr class="{cycle values='r0,r1'}">
{if $message->usr == $user->id}
<th>{include file="user/simpleuser.tpl" user=$USER}</th>
{else}
<th>{include file="user/simpleuser.tpl" user=$user}</th>
{/if}
<td>{$message->message}</td>
</tr>
{/foreach}
</tbody>
</table>
{/if}
{$form}
......
......@@ -2,7 +2,7 @@
<div class="fl">
<img src="{$WWWROOT}thumb.php?type=profileicon&amp;maxwidth=50&amp;maxheight=50&amp;id={$user->id}" alt="">
</div>
<h3><a href="{$WWWROOT}user/view.php?id={$user->id}">{$user->id|display_name|escape}</a></h3>
<h3><a href="{$WWWROOT}user/view.php?id={$user->id}">{$user|display_name|escape}</a></h3>
{if $user->introduction}
<p>{$user->introduction}</p>
{else}
......
......@@ -26,7 +26,7 @@
*/
define('INTERNAL', 1);
define('MENUITEM', 'groups/findfriends');
define('MENUITEM', 'inbox');
require(dirname(dirname(__FILE__)) . '/init.php');
require_once('pieforms/pieform.php');
require_once('searchlib.php');
......@@ -34,22 +34,17 @@ safe_require('search', 'internal');
$id = param_integer('id');
$replytoid = param_integer('replyto', null);
$replyto = false;
$messages = null;
if (!is_null($replytoid)) {
$replyto = get_record_sql('
SELECT
a.subject, a.message, a.url
FROM {notification_internal_activity} a
JOIN {activity_type} t ON a.type = t.id
WHERE t.name = ? AND a.id = ? AND a.usr = ?',
array('usermessage', $replytoid, $USER->get('id')));
if (!$replyto) {
$messages = get_message_thread($replytoid);
if (!$messages) {
throw new AccessDeniedException(get_string('cantviewmessage', 'group'));
}
// Make sure the message was sent by the user being replied to
$bits = parse_url($replyto->url);
// Make sure the thread was start by either the user being replied to, or the current user
$bits = parse_url($messages[0]->url);
parse_str($bits['query'], $params);
if (empty($params['id']) || $params['id'] != $id) {
if (empty($params['id']) || ($params['id'] != $id && $params['id'] != $USER->get('id'))) {
throw new AccessDeniedException(get_string('cantviewmessage', 'group'));
}
}
......@@ -62,20 +57,7 @@ if (!$user || !can_send_message($USER->to_stdclass(), $id)) {
throw new AccessDeniedException(get_string('cantmessageuser', 'group'));
}
$user->introduction = get_field('artefact', 'title', 'artefacttype', 'introduction', 'owner', $id);
$quote = '';
if ($replyto) {
$replyto->lines = split("\n", $replyto->message);
foreach ($replyto->lines as $line) {
$quote .= "\n> " . wordwrap($line, 75, "\n> ");
}
define('TITLE', get_string('viewmessage', 'group'));
}
else {
define('TITLE', get_string('sendmessageto', 'group', display_name($id)));
}
define('TITLE', get_string('sendmessageto', 'group', display_name($id)));
$form = pieform(array(
'name' => 'sendmessage',
......@@ -83,14 +65,13 @@ $form = pieform(array(
'elements' => array(
'message' => array(
'type' => 'textarea',
'title' => $replyto ? get_string('Reply', 'group') : get_string('message'),
'title' => $messages ? get_string('Reply', 'group') : get_string('message'),
'cols' => 80,
'rows' => 10,
'defaultvalue' => $quote,
),
'submit' => array(
'type' => 'submitcancel',
'value' => array($replyto ? get_string('Reply', 'group') : get_string('sendmessage', 'group'), get_string('cancel')),
'value' => array($messages ? get_string('Reply', 'group') : get_string('sendmessage', 'group'), get_string('cancel')),
'goto' => get_config('wwwroot') . ($returnto == 'find' ? 'user/find.php' : ($returnto == 'view' ? 'user/view.php?id=' . $id : 'user/myfriends.php')),
)
)
......@@ -100,13 +81,13 @@ $smarty = smarty();
$smarty->assign('PAGEHEADING', hsc(TITLE));
$smarty->assign('form', $form);
$smarty->assign('user', $user);
$smarty->assign('replyto', $replyto);
$smarty->assign('messages', $messages);
$smarty->display('user/sendmessage.tpl');
function sendmessage_submit(Pieform $form, $values) {
global $USER, $SESSION, $id;
$user = get_record('usr', 'id', $id);
send_user_message($user, $values['message']);
send_user_message($user, $values['message'], param_integer('replyto', null));
$SESSION->add_ok_msg(get_string('messagesent', 'group'));
switch (param_alpha('returnto', 'myfriends')) {
case 'find':
......@@ -115,6 +96,9 @@ function sendmessage_submit(Pieform $form, $values) {
case 'view':
redirect('/user/view.php?id=' . $id);
break;
case 'inbox':
redirect('/account/activity');
break;
default:
redirect('/user/myfriends.php');
break;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment