mirrored from git://git.moodle.org/moodle.git
/
base_message.php
127 lines (112 loc) · 4.41 KB
/
base_message.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?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/>.
/**
* Search area base class for messages.
*
* @package core_message
* @copyright 2016 Devang Gaur
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_message\search;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/message/lib.php');
/**
* Search area base class for messages.
*
* @package core_message
* @copyright 2016 Devang Gaur
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class base_message extends \core_search\base {
/**
* The context levels the search area is working on.
* @var array
*/
protected static $levels = [CONTEXT_USER];
/**
* Returns the document associated with this message record.
*
* @param stdClass $record
* @param array $options
* @return \core_search\document
*/
public function get_document($record, $options = array()) {
try {
$usercontext = \context_user::instance($options['user1id']);
} catch (\moodle_exception $ex) {
// Notify it as we run here as admin, we should see everything.
debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' .
$ex->getMessage(), DEBUG_DEVELOPER);
return false;
}
// Prepare associative array with data from DB.
$doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
$doc->set('title', content_to_text($record->subject, false));
$doc->set('itemid', $record->id);
$doc->set('content', content_to_text($record->smallmessage, false));
$doc->set('contextid', $usercontext->id);
$doc->set('courseid', SITEID);
$doc->set('owneruserid', $options['user1id']);
$doc->set('userid', $options['user2id']);
$doc->set('modified', $record->timecreated);
// Check if this document should be considered new.
if (isset($options['lastindexedtime']) && $options['lastindexedtime'] < $record->timecreated) {
// If the document was created after the last index time, it must be new.
$doc->set_is_new(true);
}
return $doc;
}
/**
* Link to the message.
*
* @param \core_search\document $doc
* @return \moodle_url
*/
public function get_doc_url(\core_search\document $doc) {
$users = $this->get_current_other_users($doc);
$position = 'm'.$doc->get('itemid');
return new \moodle_url('/message/index.php', array('history' => MESSAGE_HISTORY_ALL,
'user1' => $users['currentuserid'], 'user2' => $users['otheruserid']), $position);
}
/**
* Link to the conversation.
*
* @param \core_search\document $doc
* @return \moodle_url
*/
public function get_context_url(\core_search\document $doc) {
$users = $this->get_current_other_users($doc);
return new \moodle_url('/message/index.php', array('user1' => $users['currentuserid'], 'user2' => $users['otheruserid']));
}
/**
* Sorting the current(user1) and other(user2) user in the conversation.
*
* @param \core_search\document $doc
* @return array()
*/
protected function get_current_other_users($doc) {
global $USER;
$users = array();
if (($USER->id == $doc->get('owneruserid')) || (get_class($this) === 'message_sent')) {
$users['currentuserid'] = $doc->get('owneruserid');
$users['otheruserid'] = $doc->get('userid');
} else {
$users['currentuserid'] = $doc->get('userid');
$users['otheruserid'] = $doc->get('owneruserid');
}
return $users;
}
}