Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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.
   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 for sent 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  namespace core_message\search;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Search area for sent messages.
  30   *
  31   * @package    core_message
  32   * @copyright  2016 Devang Gaur
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class message_sent extends base_message {
  36  
  37      /**
  38       * Returns a recordset with the messages for indexing.
  39       *
  40       * @param int $modifiedfrom
  41       * @param \context|null $context Optional context to restrict scope of returned results
  42       * @return moodle_recordset|null Recordset (or null if no results)
  43       */
  44      public function get_document_recordset($modifiedfrom = 0, \context $context = null) {
  45          return $this->get_document_recordset_helper($modifiedfrom, $context, 'useridfrom');
  46      }
  47  
  48      /**
  49       * Returns the document associated with this message record.
  50       *
  51       * @param stdClass $record
  52       * @param array    $options
  53       * @return \core_search\document
  54       */
  55      public function get_document($record, $options = array()) {
  56          return parent::get_document($record, array('user1id' => $record->useridfrom, 'user2id' => $record->useridto));
  57      }
  58  
  59      /**
  60       * Whether the user can access the document or not.
  61       *
  62       * @param int $id The message instance id.
  63       * @return int
  64       */
  65      public function check_access($id) {
  66          global $CFG, $DB, $USER;
  67  
  68          if (!$CFG->messaging) {
  69              return \core_search\manager::ACCESS_DENIED;
  70          }
  71  
  72          $sql = "SELECT m.*, mcm.userid as useridto
  73                    FROM {messages} m
  74              INNER JOIN {message_conversations} mc
  75                      ON m.conversationid = mc.id
  76              INNER JOIN {message_conversation_members} mcm
  77                      ON mcm.conversationid = mc.id
  78                   WHERE mcm.userid != m.useridfrom
  79                     AND m.id = :id";
  80          $message = $DB->get_record_sql($sql, array('id' => $id));
  81          if (!$message) {
  82              return \core_search\manager::ACCESS_DELETED;
  83          }
  84  
  85          $userfrom = \core_user::get_user($message->useridfrom, 'id, deleted');
  86          $userto = \core_user::get_user($message->useridto, 'id, deleted');
  87  
  88          if (!$userfrom || !$userto || $userfrom->deleted || $userto->deleted) {
  89              return \core_search\manager::ACCESS_DELETED;
  90          }
  91  
  92          if ($USER->id != $userfrom->id) {
  93              return \core_search\manager::ACCESS_DENIED;
  94          }
  95  
  96          $userfromdeleted = $DB->record_exists('message_user_actions', ['messageid' => $id, 'userid' => $message->useridfrom,
  97              'action' => \core_message\api::MESSAGE_ACTION_DELETED]);
  98          if ($userfromdeleted) {
  99              return \core_search\manager::ACCESS_DELETED;
 100          }
 101  
 102          return \core_search\manager::ACCESS_GRANTED;
 103      }
 104  
 105  }