Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 39 and 400]

   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 Users for whom I have authority to view profile.
  19   *
  20   * @package    core_user
  21   * @copyright  2016 Devang Gaur {@link http://www.devanggaur.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_user\search;
  26  
  27  require_once($CFG->dirroot . '/user/lib.php');
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  /**
  32   * Search area for Users for whom I have access to view profile.
  33   *
  34   * @package    core_user
  35   * @copyright  2016 Devang Gaur {@link http://www.devanggaur.com}
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class user extends \core_search\base {
  39  
  40      /**
  41       * Returns recordset containing required data attributes for indexing.
  42       *
  43       * @param number $modifiedfrom
  44       * @param \context|null $context Optional context to restrict scope of returned results
  45       * @return \moodle_recordset|null Recordset (or null if no results)
  46       */
  47      public function get_document_recordset($modifiedfrom = 0, \context $context = null) {
  48          global $DB;
  49  
  50          // Prepare query conditions.
  51          $where = 'timemodified >= ? AND deleted = ? AND confirmed = ?';
  52          $params = [$modifiedfrom, 0, 1];
  53  
  54          // Handle context types.
  55          if (!$context) {
  56              $context = \context_system::instance();
  57          }
  58          switch ($context->contextlevel) {
  59              case CONTEXT_MODULE:
  60              case CONTEXT_BLOCK:
  61              case CONTEXT_COURSE:
  62              case CONTEXT_COURSECAT:
  63                  // These contexts cannot contain any users.
  64                  return null;
  65  
  66              case CONTEXT_USER:
  67                  // Restrict to specific user.
  68                  $where .= ' AND id = ?';
  69                  $params[] = $context->instanceid;
  70                  break;
  71  
  72              case CONTEXT_SYSTEM:
  73                  break;
  74  
  75              default:
  76                  throw new \coding_exception('Unexpected contextlevel: ' . $context->contextlevel);
  77          }
  78  
  79          return $DB->get_recordset_select('user', $where, $params);
  80      }
  81  
  82      /**
  83       * Returns document instances for each record in the recordset.
  84       *
  85       * @param \stdClass $record
  86       * @param array $options
  87       * @return \core_search\document
  88       */
  89      public function get_document($record, $options = array()) {
  90  
  91          $context = \context_system::instance();
  92  
  93          // Prepare associative array with data from DB.
  94          $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
  95          // Include all alternate names in title.
  96          $array = [];
  97          foreach (\core_user\fields::get_name_fields(true) as $field) {
  98              $array[$field] = $record->$field;
  99          }
 100          $fullusername = join(' ', $array);
 101          // Assigning properties to our document.
 102          $doc->set('title', content_to_text($fullusername, false));
 103          $doc->set('contextid', $context->id);
 104          $doc->set('courseid', SITEID);
 105          $doc->set('itemid', $record->id);
 106          $doc->set('modified', $record->timemodified);
 107          $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
 108          $doc->set('content', content_to_text($record->description, $record->descriptionformat));
 109  
 110          // Check if this document should be considered new.
 111          if (isset($options['lastindexedtime']) && $options['lastindexedtime'] < $record->timecreated) {
 112              // If the document was created after the last index time, it must be new.
 113              $doc->set_is_new(true);
 114          }
 115  
 116          return $doc;
 117      }
 118  
 119      /**
 120       * Returns the user fullname to display as document title
 121       *
 122       * @param \core_search\document $doc
 123       * @return string User fullname
 124       */
 125      public function get_document_display_title(\core_search\document $doc) {
 126  
 127          $user = \core_user::get_user($doc->get('itemid'));
 128          return fullname($user);
 129      }
 130  
 131      /**
 132       * Checking whether I can access a document
 133       *
 134       * @param int $id user id
 135       * @return int
 136       */
 137      public function check_access($id) {
 138          global $DB, $USER;
 139  
 140          $user = $DB->get_record('user', array('id' => $id));
 141          if (!$user || $user->deleted) {
 142              return \core_search\manager::ACCESS_DELETED;
 143          }
 144  
 145          if (user_can_view_profile($user)) {
 146              return \core_search\manager::ACCESS_GRANTED;
 147          }
 148  
 149          return \core_search\manager::ACCESS_DENIED;
 150      }
 151  
 152      /**
 153       * Returns a url to the profile page of user.
 154       *
 155       * @param \core_search\document $doc
 156       * @return \moodle_url
 157       */
 158      public function get_doc_url(\core_search\document $doc) {
 159          return $this->get_context_url($doc);
 160      }
 161  
 162      /**
 163       * Returns a url to the document context.
 164       *
 165       * @param \core_search\document $doc
 166       * @return \moodle_url
 167       */
 168      public function get_context_url(\core_search\document $doc) {
 169          return new \moodle_url('/user/profile.php', array('id' => $doc->get('itemid')));
 170      }
 171  
 172      /**
 173       * Returns true if this area uses file indexing.
 174       *
 175       * @return bool
 176       */
 177      public function uses_file_indexing() {
 178          return true;
 179      }
 180  
 181      /**
 182       * Return the context info required to index files for
 183       * this search area.
 184       *
 185       * Should be onerridden by each search area.
 186       *
 187       * @return array
 188       */
 189      public function get_search_fileareas() {
 190          $fileareas = array(
 191                  'profile' // Fileareas.
 192          );
 193  
 194          return $fileareas;
 195      }
 196  
 197      /**
 198       * Returns the moodle component name.
 199       *
 200       * It might be the plugin name (whole frankenstyle name) or the core subsystem name.
 201       *
 202       * @return string
 203       */
 204      public function get_component_name() {
 205          return 'user';
 206      }
 207  
 208      /**
 209       * Returns an icon instance for the document.
 210       *
 211       * @param \core_search\document $doc
 212       *
 213       * @return \core_search\document_icon
 214       */
 215      public function get_doc_icon(\core_search\document $doc) : \core_search\document_icon {
 216          return new \core_search\document_icon('i/user');
 217      }
 218  
 219      /**
 220       * Returns a list of category names associated with the area.
 221       *
 222       * @return array
 223       */
 224      public function get_category_names() {
 225          return [\core_search\manager::SEARCH_AREA_CATEGORY_USERS];
 226      }
 227  
 228  }