diff --git a/search/classes/base.php b/search/classes/base.php index 2c10388a32d..50b9be68c42 100644 --- a/search/classes/base.php +++ b/search/classes/base.php @@ -319,6 +319,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. diff --git a/search/classes/document.php b/search/classes/document.php index 77c89c210f6..45a4a3eaa2a 100644 --- a/search/classes/document.php +++ b/search/classes/document.php @@ -579,7 +579,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, diff --git a/user/classes/search/user.php b/user/classes/search/user.php index 5724b69532f..337fa284ce3 100644 --- a/user/classes/search/user.php +++ b/user/classes/search/user.php @@ -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 * diff --git a/user/tests/search_test.php b/user/tests/search_test.php index 589665a44f7..5e7f5dc4a26 100644 --- a/user/tests/search_test.php +++ b/user/tests/search_test.php @@ -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')); }