Skip to content

Commit

Permalink
MDL-54687 core_message: created page for new messaging interface
Browse files Browse the repository at this point in the history
This commit introduces the templates, renderer and initial API
that will be used for further development.
  • Loading branch information
mdjnelson committed Oct 7, 2016
1 parent 0dec2f9 commit 879e2be
Show file tree
Hide file tree
Showing 18 changed files with 991 additions and 135 deletions.
3 changes: 3 additions & 0 deletions lang/en/message.php
Expand Up @@ -39,6 +39,7 @@
$string['contactlistempty'] = 'Contact list empty';
$string['contacts'] = 'Contacts';
$string['context'] = 'context';
$string['conversations'] = 'Conversations';
$string['defaultmessageoutputs'] = 'Default message outputs';
$string['defaults'] = 'Defaults';
$string['deletemessage'] = 'Delete message';
Expand Down Expand Up @@ -129,6 +130,7 @@
$string['searchforperson'] = 'Search for a person';
$string['searchmessages'] = 'Search messages';
$string['searchcombined'] = 'Search people and messages';
$string['send'] = 'Send';
$string['sendingvia'] = 'Sending "{$a->provider}" via "{$a->processor}"';
$string['sendingviawhen'] = 'Sending "{$a->provider}" via "{$a->processor}" when {$a->state}';
$string['sendingmessage'] = 'Sending message';
Expand All @@ -151,4 +153,5 @@
$string['userisblockingyou'] = 'This user has blocked you from sending messages to them';
$string['userisblockingyounoncontact'] = '{$a} only accepts messages from their contacts.';
$string['userssearchresults'] = 'Users found: {$a}';
$string['viewinganotherusersmessagearea'] = 'You are viewing another user\'s message area.';
$string['viewconversation'] = 'View conversation';
104 changes: 104 additions & 0 deletions message/classes/api.php
@@ -0,0 +1,104 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Contains class used to return information to display for the message area.
*
* @package core_message
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_message;

require_once($CFG->dirroot . '/lib/messagelib.php');

defined('MOODLE_INTERNAL') || die();

/**
* Class used to return information to display for the message area.
*
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class api {

/**
* Returns the contacts and their conversation to display in the contacts area.
*
* @param int $userid The user id
* @param int $otheruserid The id of the user we have selected, 0 if none have been selected
* @param int $limitfrom
* @param int $limitnum
* @return \core_message\output\contacts
*/
public static function get_conversations($userid, $otheruserid = 0, $limitfrom = 0, $limitnum = 0) {
$arrcontacts = array();
if ($conversations = message_get_recent_conversations($userid, $limitfrom, $limitnum)) {
foreach ($conversations as $conversation) {
$arrcontacts[] = \core_message\helper::create_contact($conversation);
}
}

return new \core_message\output\contacts($userid, $otheruserid, $arrcontacts);
}

/**
* Returns the contacts to display in the contacts area.
*
* @param int $userid The user id
* @param int $limitfrom
* @param int $limitnum
* @return \core_message\output\contacts
*/
public static function get_contacts($userid, $limitfrom = 0, $limitnum = 0) {
global $DB;

$arrcontacts = array();
$sql = "SELECT u.*
FROM {message_contacts} mc
JOIN {user} u
ON mc.contactid = u.id
WHERE mc.userid = :userid
AND u.deleted = 0
ORDER BY " . $DB->sql_fullname();
if ($contacts = $DB->get_records_sql($sql, array('userid' => $userid), $limitfrom, $limitnum)) {
foreach ($contacts as $contact) {
$arrcontacts[] = \core_message\helper::create_contact($contact);
}
}

return new \core_message\output\contacts($userid, 0, $arrcontacts, false);
}

/**
* Returns the messages to display in the message area.
*
* @param int $userid the current user
* @param int $otheruserid the other user
* @param int $limitfrom
* @param int $limitnum
* @return \core_message\output\messages
*/
public static function get_messages($userid, $otheruserid, $limitfrom = 0, $limitnum = 0) {
$arrmessages = array();
if ($messages = \core_message\helper::get_messages($userid, $otheruserid, $limitfrom, $limitnum)) {
$arrmessages = \core_message\helper::create_messages($userid, $messages);
}

return new \core_message\output\messages($userid, $otheruserid, $arrmessages);
}
}
147 changes: 147 additions & 0 deletions message/classes/helper.php
@@ -0,0 +1,147 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Contains helper class for the message area.
*
* @package core_message
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_message;

defined('MOODLE_INTERNAL') || die();

/**
* Helper class for the message area.
*
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class helper {

/**
* Helper function to retrieve the messages between two users
*
* @param int $userid the current user
* @param int $otheruserid the other user
* @param int $limitfrom
* @param int $limitnum
* @param string $sort
* @return array of messages
*/
public static function get_messages($userid, $otheruserid, $limitfrom = 0, $limitnum = 0, $sort = 'timecreated ASC') {
global $DB;

$sql = "SELECT id, useridfrom, useridto, subject, fullmessage, fullmessagehtml, fullmessageformat,
smallmessage, notification, timecreated, 0 as timeread
FROM {message} m
WHERE ((useridto = ? AND useridfrom = ? AND timeusertodeleted = 0)
OR (useridto = ? AND useridfrom = ? AND timeuserfromdeleted = 0))
AND notification = 0
UNION ALL
SELECT id, useridfrom, useridto, subject, fullmessage, fullmessagehtml, fullmessageformat,
smallmessage, notification, timecreated, timeread
FROM {message_read} mr
WHERE ((useridto = ? AND useridfrom = ? AND timeusertodeleted = 0)
OR (useridto = ? AND useridfrom = ? AND timeuserfromdeleted = 0))
AND notification = 0
ORDER BY $sort";
$params = array($userid, $otheruserid, $otheruserid, $userid,
$userid, $otheruserid, $otheruserid, $userid);

return $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
}

/**
* Helper function to return an array of messages renderables to display in the message area.
*
* @param int $userid
* @param array $messages
* @return \core_message\output\message[]
*/
public static function create_messages($userid, $messages) {
// Store the messages.
$arrmessages = array();

// Keeps track of the last day, month and year combo we were viewing.
$day = '';
$month = '';
$year = '';
foreach ($messages as $message) {
// Check if we are now viewing a different block period.
$blocktime = null;
$date = usergetdate($message->timecreated);
if ($day != $date['mday'] || $month != $date['month'] || $year != $date['year']) {
$day = $date['mday'];
$month = $date['month'];
$year = $date['year'];
$blocktime = userdate($message->timecreated, get_string('strftimedaydate'));
}
// Store the message to pass to the renderable.
$msg = new \stdClass();
$msg->text = message_format_message_text($message);
$msg->currentuserid = $userid;
$msg->useridfrom = $message->useridfrom;
$msg->useridto = $message->useridto;
$msg->blocktime = $blocktime;
$msg->timecreated = $message->timecreated;
$arrmessages[] = new \core_message\output\message($msg);
}

return $arrmessages;
}

/**
* Helper function for creating a contact renderable.
*
* @param \stdClass $contact
* @return \core_message\output\contact
*/
public static function create_contact($contact) {
global $CFG, $PAGE;

// Variables to check if we consider this user online or not.
$timetoshowusers = 300; // Seconds default.
if (isset($CFG->block_online_users_timetosee)) {
$timetoshowusers = $CFG->block_online_users_timetosee * 60;
}
$time = time() - $timetoshowusers;

// Create the data we are going to pass to the renderable.
$userfields = \user_picture::unalias($contact, array('lastaccess'));
$data = new \stdClass();
$data->userid = $userfields->id;
$data->fullname = fullname($userfields);
// Get the user picture data.
$userpicture = new \user_picture($userfields);
$userpicture->size = 1; // Size f1.
$data->profileimageurl = $userpicture->get_url($PAGE)->out(false);
$userpicture->size = 0; // Size f2.
$data->profileimageurlsmall = $userpicture->get_url($PAGE)->out(false);
// Store the message if we have it.
if (isset($contact->smallmessage)) {
$data->lastmessage = $contact->smallmessage;
} else {
$data->lastmessage = null;
}
// Check if the user is online.
$data->isonline = $userfields->lastaccess >= $time;

return new \core_message\output\contact($data);
}
}
69 changes: 69 additions & 0 deletions message/classes/output/contact.php
@@ -0,0 +1,69 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Contains class used to prepare a contact for display.
*
* @package core_message
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_message\output;

use renderable;
use templatable;

/**
* Class to prepare a contact for display.
*
* @package core_message
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class contact implements templatable, renderable {

/**
* Maximum length of message to show in left panel.
*/
const MAX_MSG_LENGTH = 60;

/**
* The contact.
*/
protected $contact;

/**
* Constructor.
*
* @param \stdClass $contact
*/
public function __construct($contact) {
$this->contact = $contact;
}

public function export_for_template(\renderer_base $output) {
$contact = new \stdClass();
$contact->userid = $this->contact->userid;
$contact->fullname = $this->contact->fullname;
$contact->profileimageurl = $this->contact->profileimageurl;
$contact->profileimageurlsmall = $this->contact->profileimageurlsmall;
$contact->lastmessage = shorten_text($this->contact->lastmessage, self::MAX_MSG_LENGTH);
$contact->isonline = $this->contact->isonline;

return $contact;
}
}

0 comments on commit 879e2be

Please sign in to comment.