Skip to content

Commit

Permalink
MDL-57977 search: GlobalSearch by alternate name fields
Browse files Browse the repository at this point in the history
Enable support for searching all name fields in search
  • Loading branch information
vmdef committed Jul 23, 2018
1 parent 2d6cb43 commit 215dd70
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
13 changes: 13 additions & 0 deletions search/classes/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,19 @@ public function get_document_recordset($modifiedfrom = 0, \context $context = nu
*/
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.
Expand Down
3 changes: 2 additions & 1 deletion search/classes/document.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,8 @@ protected function apply_defaults() {
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,
Expand Down
20 changes: 19 additions & 1 deletion user/classes/search/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,14 @@ public function get_document($record, $options = array()) {

// 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);
Expand All @@ -110,6 +116,18 @@ public function get_document($record, $options = array()) {
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
*
Expand Down
2 changes: 1 addition & 1 deletion user/tests/search_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function test_users_document() {
$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'));
}

Expand Down

0 comments on commit 215dd70

Please sign in to comment.