MDL-57977 search: GlobalSearch by alternate name fields
authorVictor Deniz <victor@moodle.com>
Tue, 19 Jun 2018 11:28:04 +0000 (12:28 +0100)
committerVictor Deniz <victor@moodle.com>
Mon, 23 Jul 2018 09:10:05 +0000 (10:10 +0100)
Enable support for searching all name fields in search

search/classes/base.php
search/classes/document.php
user/classes/search/user.php
user/tests/search_test.php

index 97fb8ab..4a619cd 100644 (file)
@@ -340,6 +340,19 @@ abstract class base {
      */
     abstract public function get_document($record, $options = array());
 
+    /**
+     * Returns the document title to display.
+     *
+     * Allow to customize the document title string to display.
+     *
+     * @param \core_search\document $doc
+     * @return string Document title to display in the search results page
+     */
+    public function get_document_display_title(\core_search\document $doc) {
+
+        return $doc->get('title');
+    }
+
     /**
      * Return the context info required to index files for
      * this search area.
index 0808066..5f76f94 100644 (file)
@@ -592,7 +592,8 @@ class document implements \renderable, \templatable {
     public function export_for_template(\renderer_base $output) {
         list($componentname, $areaname) = \core_search\manager::extract_areaid_parts($this->get('areaid'));
 
-        $title = $this->is_set('title') ? $this->format_text($this->get('title')) : '';
+        $searcharea = \core_search\manager::get_search_area($this->data['areaid']);
+        $title = $this->is_set('title') ? $this->format_text($searcharea->get_document_display_title($this)) : '';
         $data = [
             'componentname' => $componentname,
             'areaname' => $areaname,
index 5724b69..337fa28 100644 (file)
@@ -92,8 +92,14 @@ class user extends \core_search\base {
 
         // Prepare associative array with data from DB.
         $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
+        // Include all alternate names in title.
+        $array = [];
+        foreach (get_all_user_name_fields(false, null, null, null, true) as $field) {
+            $array[$field] = $record->$field;
+        }
+        $fullusername = join(' ', $array);
         // Assigning properties to our document.
-        $doc->set('title', content_to_text(fullname($record), false));
+        $doc->set('title', content_to_text($fullusername, false));
         $doc->set('contextid', $context->id);
         $doc->set('courseid', SITEID);
         $doc->set('itemid', $record->id);
@@ -110,6 +116,18 @@ class user extends \core_search\base {
         return $doc;
     }
 
+    /**
+     * Returns the user fullname to display as document title
+     *
+     * @param \core_search\document $doc
+     * @return string User fullname
+     */
+    public function get_document_display_title(\core_search\document $doc) {
+
+        $user = \core_user::get_user($doc->get('itemid'));
+        return fullname($user);
+    }
+
     /**
      * Checking whether I can access a document
      *
index 589665a..5e7f5dc 100644 (file)
@@ -119,7 +119,7 @@ class user_search_testcase extends advanced_testcase {
         $this->assertEquals(SITEID, $doc->get('courseid'));
         $this->assertFalse($doc->is_set('userid'));
         $this->assertEquals(\core_search\manager::NO_OWNER_ID, $doc->get('owneruserid'));
-        $this->assertEquals(content_to_text(fullname($user), false), $doc->get('title'));
+        $this->assertEquals(content_to_text(fullname($user), false), $searcharea->get_document_display_title($doc));
         $this->assertEquals(content_to_text($user->description, $user->descriptionformat), $doc->get('content'));
     }