Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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   * 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   */
  24  
  25  namespace core_message\search;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->dirroot . '/message/lib.php');
  30  
  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 {
  39  
  40      /**
  41       * The context levels the search area is working on.
  42       * @var array
  43       */
  44      protected static $levels = [CONTEXT_USER];
  45  
  46      /**
  47       * Returns the document associated with this message record.
  48       *
  49       * @param stdClass $record
  50       * @param array    $options
  51       * @return \core_search\document
  52       */
  53      public function get_document($record, $options = array()) {
  54  
  55          // Check if user still exists, before proceeding.
  56          $user = \core_user::get_user($options['user1id'], 'deleted');
  57          if ($user->deleted == 1) {
  58              return false;
  59          }
  60  
  61          // Get user context.
  62          try {
  63              $usercontext = \context_user::instance($options['user1id']);
  64          } catch (\moodle_exception $ex) {
  65              // Notify it as we run here as admin, we should see everything.
  66              debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' .
  67                      $ex->getMessage(), DEBUG_DEVELOPER);
  68              return false;
  69          }
  70          // Prepare associative array with data from DB.
  71          $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
  72          $doc->set('title', content_to_text($record->subject, false));
  73          $doc->set('itemid', $record->id);
  74          $doc->set('content', content_to_text($record->smallmessage, false));
  75          $doc->set('contextid', $usercontext->id);
  76          $doc->set('courseid', SITEID);
  77          $doc->set('owneruserid', $options['user1id']);
  78          $doc->set('userid', $options['user2id']);
  79          $doc->set('modified', $record->timecreated);
  80  
  81          // Check if this document should be considered new.
  82          if (isset($options['lastindexedtime']) && $options['lastindexedtime'] < $record->timecreated) {
  83              // If the document was created after the last index time, it must be new.
  84              $doc->set_is_new(true);
  85          }
  86  
  87          return $doc;
  88      }
  89  
  90      /**
  91       * Link to the message.
  92       *
  93       * @param \core_search\document $doc
  94       * @return \moodle_url
  95       */
  96      public function get_doc_url(\core_search\document $doc) {
  97          $users = $this->get_current_other_users($doc);
  98          $position = 'm'.$doc->get('itemid');
  99          return new \moodle_url('/message/index.php', array('history' => MESSAGE_HISTORY_ALL,
 100                  'user1' => $users['currentuserid'], 'user2' => $users['otheruserid']), $position);
 101      }
 102  
 103      /**
 104       * Link to the conversation.
 105       *
 106       * @param \core_search\document $doc
 107       * @return \moodle_url
 108       */
 109      public function get_context_url(\core_search\document $doc) {
 110          $users = $this->get_current_other_users($doc);
 111          return new \moodle_url('/message/index.php', array('user1' => $users['currentuserid'], 'user2' => $users['otheruserid']));
 112      }
 113  
 114      /**
 115       * Sorting the current(user1) and other(user2) user in the conversation.
 116       *
 117       * @param \core_search\document $doc
 118       * @return array()
 119       */
 120      protected function get_current_other_users($doc) {
 121          global $USER;
 122  
 123          $users = array();
 124          if (($USER->id == $doc->get('owneruserid')) || (get_class($this) === 'message_sent')) {
 125              $users['currentuserid'] = $doc->get('owneruserid');
 126              $users['otheruserid'] = $doc->get('userid');
 127          } else {
 128              $users['currentuserid'] = $doc->get('userid');
 129              $users['otheruserid'] = $doc->get('owneruserid');
 130          }
 131  
 132          return $users;
 133      }
 134  
 135      /**
 136       * Helper function to implement get_document_recordset for subclasses.
 137       *
 138       * @param int $modifiedfrom Modified from date
 139       * @param \context|null $context Context or null
 140       * @param string $userfield Name of user field (from or to) being considered
 141       * @return \moodle_recordset|null Recordset or null if no results possible
 142       * @throws \coding_exception If context invalid
 143       */
 144      protected function get_document_recordset_helper($modifiedfrom, \context $context = null,
 145              $userfield) {
 146          global $DB;
 147  
 148          if ($userfield == 'useridto') {
 149              $userfield = 'mcm.userid';
 150          } else {
 151              $userfield = 'm.useridfrom';
 152          }
 153  
 154          // Set up basic query.
 155          $where = $userfield . ' != :noreplyuser AND ' . $userfield .
 156                  ' != :supportuser AND m.timecreated >= :modifiedfrom';
 157          $params = [
 158              'noreplyuser' => \core_user::NOREPLY_USER,
 159              'supportuser' => \core_user::SUPPORT_USER,
 160              'modifiedfrom' => $modifiedfrom
 161          ];
 162  
 163          // Check context to see whether to add other restrictions.
 164          if ($context === null) {
 165              $context = \context_system::instance();
 166          }
 167          switch ($context->contextlevel) {
 168              case CONTEXT_COURSECAT:
 169              case CONTEXT_COURSE:
 170              case CONTEXT_MODULE:
 171              case CONTEXT_BLOCK:
 172                  // There are no messages in any of these contexts so nothing can be found.
 173                  return null;
 174  
 175              case CONTEXT_USER:
 176                  // Add extra restriction to specific user context.
 177                  $where .= ' AND ' . $userfield . ' = :userid';
 178                  $params['userid'] = $context->instanceid;
 179                  break;
 180  
 181              case CONTEXT_SYSTEM:
 182                  break;
 183  
 184              default:
 185                  throw new \coding_exception('Unexpected contextlevel: ' . $context->contextlevel);
 186          }
 187  
 188          $sql = "SELECT m.*, mcm.userid as useridto
 189                    FROM {messages} m
 190              INNER JOIN {message_conversations} mc
 191                      ON m.conversationid = mc.id
 192              INNER JOIN {message_conversation_members} mcm
 193                      ON mcm.conversationid = mc.id
 194                   WHERE mcm.userid != m.useridfrom
 195                     AND $where
 196                ORDER BY m.timecreated ASC";
 197          return $DB->get_recordset_sql($sql, $params);
 198      }
 199  
 200      /**
 201       * Returns an icon instance for the document.
 202       *
 203       * @param \core_search\document $doc
 204       *
 205       * @return \core_search\document_icon
 206       */
 207      public function get_doc_icon(\core_search\document $doc) : \core_search\document_icon {
 208          return new \core_search\document_icon('t/message');
 209      }
 210  
 211      /**
 212       * Returns a list of category names associated with the area.
 213       *
 214       * @return array
 215       */
 216      public function get_category_names() {
 217          return [\core_search\manager::SEARCH_AREA_CATEGORY_USERS];
 218      }
 219  }