MDL-54687 core_message: created page for new messaging interface
[moodle.git] / message / classes / api.php
CommitLineData
879e2bef
MN
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Contains class used to return information to display for the message area.
19 *
20 * @package core_message
21 * @copyright 2016 Mark Nelson <markn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_message;
26
27require_once($CFG->dirroot . '/lib/messagelib.php');
28
29defined('MOODLE_INTERNAL') || die();
30
31/**
32 * Class used to return information to display for the message area.
33 *
34 * @copyright 2016 Mark Nelson <markn@moodle.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class api {
38
39 /**
40 * Returns the contacts and their conversation to display in the contacts area.
41 *
42 * @param int $userid The user id
43 * @param int $otheruserid The id of the user we have selected, 0 if none have been selected
44 * @param int $limitfrom
45 * @param int $limitnum
46 * @return \core_message\output\contacts
47 */
48 public static function get_conversations($userid, $otheruserid = 0, $limitfrom = 0, $limitnum = 0) {
49 $arrcontacts = array();
50 if ($conversations = message_get_recent_conversations($userid, $limitfrom, $limitnum)) {
51 foreach ($conversations as $conversation) {
52 $arrcontacts[] = \core_message\helper::create_contact($conversation);
53 }
54 }
55
56 return new \core_message\output\contacts($userid, $otheruserid, $arrcontacts);
57 }
58
59 /**
60 * Returns the contacts to display in the contacts area.
61 *
62 * @param int $userid The user id
63 * @param int $limitfrom
64 * @param int $limitnum
65 * @return \core_message\output\contacts
66 */
67 public static function get_contacts($userid, $limitfrom = 0, $limitnum = 0) {
68 global $DB;
69
70 $arrcontacts = array();
71 $sql = "SELECT u.*
72 FROM {message_contacts} mc
73 JOIN {user} u
74 ON mc.contactid = u.id
75 WHERE mc.userid = :userid
76 AND u.deleted = 0
77 ORDER BY " . $DB->sql_fullname();
78 if ($contacts = $DB->get_records_sql($sql, array('userid' => $userid), $limitfrom, $limitnum)) {
79 foreach ($contacts as $contact) {
80 $arrcontacts[] = \core_message\helper::create_contact($contact);
81 }
82 }
83
84 return new \core_message\output\contacts($userid, 0, $arrcontacts, false);
85 }
86
87 /**
88 * Returns the messages to display in the message area.
89 *
90 * @param int $userid the current user
91 * @param int $otheruserid the other user
92 * @param int $limitfrom
93 * @param int $limitnum
94 * @return \core_message\output\messages
95 */
96 public static function get_messages($userid, $otheruserid, $limitfrom = 0, $limitnum = 0) {
97 $arrmessages = array();
98 if ($messages = \core_message\helper::get_messages($userid, $otheruserid, $limitfrom, $limitnum)) {
99 $arrmessages = \core_message\helper::create_messages($userid, $messages);
100 }
101
102 return new \core_message\output\messages($userid, $otheruserid, $arrmessages);
103 }
104}