MDL-44221 editor_atto: convert plugins to use insert_html_at_focus_point
[moodle.git] / mod / chat / locallib.php
blob3961fbd8ed7404fa4b39fb05693938ab178f939a
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Library of functions for chat outside of the core api
22 require_once($CFG->dirroot . '/mod/chat/lib.php');
23 require_once($CFG->libdir . '/portfolio/caller.php');
25 /**
26 * @package mod_chat
27 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 class chat_portfolio_caller extends portfolio_module_caller_base {
31 /** @var object */
32 private $chat;
33 /** @var int Timestamp */
34 protected $start;
35 /** @var int Timestamp */
36 protected $end;
37 /**
38 * @return array
40 public static function expected_callbackargs() {
41 return array(
42 'id' => true,
43 'start' => false,
44 'end' => false,
47 /**
48 * @global object
50 public function load_data() {
51 global $DB;
53 if (!$this->cm = get_coursemodule_from_id('chat', $this->id)) {
54 throw new portfolio_caller_exception('invalidid', 'chat');
56 $this->chat = $DB->get_record('chat', array('id' => $this->cm->instance));
57 $select = 'chatid = ?';
58 $params = array($this->chat->id);
59 if ($this->start && $this->end) {
60 $select .= ' AND timestamp >= ? AND timestamp <= ?';
61 $params[] = $this->start;
62 $params[] = $this->end;
64 $this->messages = $DB->get_records_select(
65 'chat_messages',
66 $select,
67 $params,
68 'timestamp ASC'
70 $select .= ' AND userid = ?';
71 $params[] = $this->user->id;
72 $this->participated = $DB->record_exists_select(
73 'chat_messages',
74 $select,
75 $params
78 /**
79 * @return array
81 public static function base_supported_formats() {
82 return array(PORTFOLIO_FORMAT_PLAINHTML);
84 /**
87 public function expected_time() {
88 return portfolio_expected_time_db(count($this->messages));
90 /**
91 * @return string
93 public function get_sha1() {
94 $str = '';
95 ksort($this->messages);
96 foreach ($this->messages as $m) {
97 $str .= implode('', (array)$m);
99 return sha1($str);
103 * @return bool
105 public function check_permissions() {
106 $context = context_module::instance($this->cm->id);
107 return has_capability('mod/chat:exportsession', $context)
108 || ($this->participated
109 && has_capability('mod/chat:exportparticipatedsession', $context));
113 * @todo Document this function
115 public function prepare_package() {
116 $content = '';
117 $lasttime = 0;
118 $sessiongap = 5 * 60; // 5 minutes silence means a new session
119 foreach ($this->messages as $message) { // We are walking FORWARDS through messages
120 $m = clone $message; // grrrrrr - this causes the sha1 to change as chat_format_message changes what it's passed.
121 $formatmessage = chat_format_message($m, $this->cm->course, $this->user);
122 if (!isset($formatmessage->html)) {
123 continue;
125 if (empty($lasttime) || (($message->timestamp - $lasttime) > $sessiongap)) {
126 $content .= '<hr />';
127 $content .= userdate($message->timestamp);
129 $content .= $formatmessage->html;
130 $lasttime = $message->timestamp;
132 $content = preg_replace('/\<img[^>]*\>/', '', $content);
134 $this->exporter->write_new_file($content, clean_filename($this->cm->name . '-session.html'), false);
138 * @return string
140 public static function display_name() {
141 return get_string('modulename', 'chat');
145 * @global object
146 * @return string
148 public function get_return_url() {
149 global $CFG;
151 return $CFG->wwwroot . '/mod/chat/report.php?id='
152 . $this->cm->id . ((isset($this->start))
153 ? '&start=' . $this->start . '&end=' . $this->end
154 : '');
159 * A chat event such a user entering or leaving a chat activity
161 * @package mod_chat
162 * @copyright 2012 Andrew Davis
163 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
165 class event_message implements renderable {
167 /** @var string The URL of the profile of the user who caused the event */
168 public $senderprofile;
170 /** @var string The ready to display name of the user who caused the event */
171 public $sendername;
173 /** @var string Ready to display event time */
174 public $time;
176 /** @var string Event description */
177 public $event;
179 /** @var string The chat theme name */
180 public $theme;
183 * event_message constructor
185 * @param string $senderprofile The URL of the profile of the user who caused the event
186 * @param string $sendername The ready to display name of the user who caused the event
187 * @param string $time Ready to display event time
188 * @param string $theme The chat theme name
190 function __construct($senderprofile, $sendername, $time, $event, $theme) {
192 $this->senderprofile = $senderprofile;
193 $this->sendername = $sendername;
194 $this->time = $time;
195 $this->event = $event;
196 $this->theme = $theme;
201 * A chat message from a user
203 * @package mod_chat
204 * @copyright 2012 Andrew Davis
205 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
207 class user_message implements renderable {
209 /** @var string The URL of the profile of the user sending the message */
210 public $senderprofile;
212 /** @var string The ready to display name of the user sending the message */
213 public $sendername;
215 /** @var string HTML for the avatar of the user sending the message */
216 public $avatar;
218 /** @var string Empty or a html class definition to append to the html */
219 public $mymessageclass;
221 /** @var string Ready to display message time */
222 public $time;
224 /** @var string The message */
225 public $message;
227 /** @var string The name of the chat theme to use */
228 public $theme;
231 * user_message constructor
233 * @param string $senderprofile The URL of the profile of the user sending the message
234 * @param string $sendername The ready to display name of the user sending the message
235 * @param string $avatar HTML for the avatar of the user sending the message
236 * @param string $mymessageclass Empty or a html class definition to append to the html
237 * @param string $time Ready to display message time
238 * @param string $message The message
239 * @param string $theme The name of the chat theme to use
241 function __construct($senderprofile, $sendername, $avatar, $mymessageclass, $time, $message, $theme) {
243 $this->sendername = $sendername;
244 $this->avatar = $avatar;
245 $this->mymessageclass = $mymessageclass;
246 $this->time = $time;
247 $this->message = $message;
248 $this->theme = $theme;