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