Skip to content

Commit

Permalink
MDL-60913 search: add search area categories
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Jan 22, 2019
1 parent c092f75 commit e9074ee
Show file tree
Hide file tree
Showing 23 changed files with 781 additions and 7 deletions.
9 changes: 9 additions & 0 deletions admin/searchareas.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
$table->id = 'core-search-areas';
$table->head = [
get_string('searcharea', 'search'),
get_string('searchareacategories', 'search'),
get_string('enable'),
get_string('newestdocindexed', 'admin'),
get_string('searchlastrun', 'admin'),
Expand All @@ -165,6 +166,14 @@
$areaid = $area->get_area_id();
$columns = array(new html_table_cell($area->get_visible_name()));

$areacategories = [];
foreach (\core_search\manager::get_search_area_categories() as $category) {
if (key_exists($areaid, $category->get_areas())) {
$areacategories[] = $category->get_visiblename();
}
}
$columns[] = new html_table_cell(implode(', ', $areacategories));

if ($area->is_enabled()) {
$columns[] = $OUTPUT->action_icon(admin_searcharea_action_url('disable', $areaid),
new pix_icon('t/hide', get_string('disable'), 'moodle', array('title' => '', 'class' => 'iconsmall')),
Expand Down
19 changes: 19 additions & 0 deletions admin/settings/plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,25 @@
new lang_string('searchallavailablecourses_desc', 'admin'),
0, $options));

// Search display options.
$temp->add(new admin_setting_heading('searchdisplay', new lang_string('searchdisplay', 'admin'), ''));
$temp->add(new admin_setting_configcheckbox('searchenablecategories',
new lang_string('searchenablecategories', 'admin'),
new lang_string('searchenablecategories_desc', 'admin'),
0));
$options = [];
foreach (\core_search\manager::get_search_area_categories() as $category) {
$options[$category->get_name()] = $category->get_visiblename();
}
$temp->add(new admin_setting_configselect('searchdefaultcategory',
new lang_string('searchdefaultcategory', 'admin'),
new lang_string('searchdefaultcategory_desc', 'admin'),
\core_search\manager::SEARCH_AREA_CATEGORY_ALL, $options));
$temp->add(new admin_setting_configcheckbox('searchhideallcategory',
new lang_string('searchhideallcategory', 'admin'),
new lang_string('searchhideallcategory_desc', 'admin'),
0));

$ADMIN->add('searchplugins', $temp);
$ADMIN->add('searchplugins', new admin_externalpage('searchareas', new lang_string('searchareas', 'admin'),
new moodle_url('/admin/searchareas.php')));
Expand Down
9 changes: 9 additions & 0 deletions course/classes/search/mycourse.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,13 @@ public function get_component_name() {
public function get_doc_icon(\core_search\document $doc) : \core_search\document_icon {
return new \core_search\document_icon('i/course');
}

/**
* Returns a list of category names associated with the area.
*
* @return array
*/
public function get_category_names() {
return [\core_search\manager::SEARCH_AREA_CATEGORY_COURSES];
}
}
9 changes: 9 additions & 0 deletions course/classes/search/section.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,13 @@ public function get_component_name() {
public function get_doc_icon(\core_search\document $doc) : \core_search\document_icon {
return new \core_search\document_icon('i/section');
}

/**
* Returns a list of category names associated with the area.
*
* @return array
*/
public function get_category_names() {
return [\core_search\manager::SEARCH_AREA_CATEGORY_COURSE_CONTENT];
}
}
11 changes: 11 additions & 0 deletions course/tests/search_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,15 @@ public function test_get_doc_icon_for_section_area() {
$this->assertEquals('i/section', $result->get_name());
$this->assertEquals('moodle', $result->get_component());
}

/**
* Test assigned search categories.
*/
public function test_get_category_names() {
$coursessearcharea = \core_search\manager::get_search_area($this->mycoursesareaid);
$sectionsearcharea = \core_search\manager::get_search_area($this->sectionareaid);

$this->assertEquals(['core-courses'], $coursessearcharea->get_category_names());
$this->assertEquals(['core-course-content'], $sectionsearcharea->get_category_names());
}
}
7 changes: 7 additions & 0 deletions lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,13 @@
$string['searchallavailablecourses_off'] = 'Search within enrolled courses only';
$string['searchallavailablecourses_on'] = 'Search within all courses the user can access';
$string['searchallavailablecourses_desc'] = 'In some situations the search engine may not work when searching across a large number of courses. Set to search only enrolled courses if you need to restrict the number of courses searched.';
$string['searchdisplay'] = 'Search results display options';
$string['searchenablecategories'] = 'Display results in separate categories';
$string['searchenablecategories_desc'] = 'If enabled, search results will be displayed in separate categories.';
$string['searchhideallcategory'] = 'Hide All results category';
$string['searchhideallcategory_desc'] = 'If checked, the category with all results will be hidden on the search result screen.';
$string['searchdefaultcategory'] = 'Default search category';
$string['searchdefaultcategory_desc'] = 'Results from the selected search area category will be displayed by default.';
$string['searchalldeleted'] = 'All indexed contents have been deleted';
$string['searchareaenabled'] = 'Search area enabled';
$string['searchareadisabled'] = 'Search area disabled';
Expand Down
6 changes: 6 additions & 0 deletions lang/en/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
$string['confirm_indexall'] = 'Are you sure you want to update indexed contents now? If a large amount of content needs indexing, this can take a long time. For live servers, you should normally leave indexing to the \'Global search indexing\' scheduled task.';
$string['confirm_reindexall'] = 'Are you sure you want to reindex all site contents now? If your site contains a large amount of content, this will take a long time, and users may not get full search results until it completes.';
$string['confirm_deleteall'] = 'Are you sure you want to delete all indexed contents now? Until the site is indexed again, users will not get search results.';
$string['core-all'] = 'All';
$string['core-course-content'] = 'Course content';
$string['core-courses'] = 'Courses';
$string['core-users'] = 'Users';
$string['core-other'] = 'Other';
$string['createanindex'] = 'create an index';
$string['createdon'] = 'Created on';
$string['database'] = 'Database';
Expand Down Expand Up @@ -111,6 +116,7 @@
$string['search:section'] = 'Course sections';
$string['search:user'] = 'Users';
$string['searcharea'] = 'Search area';
$string['searchareacategories'] = 'Seach area categories';
$string['searching'] = 'Searching in ...';
$string['searchnotpermitted'] = 'You are not allowed to do a search';
$string['searchsetupdescription'] = 'The following steps help you to set up Moodle global search.';
Expand Down
9 changes: 9 additions & 0 deletions message/classes/search/base_message.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,13 @@ protected function get_document_recordset_helper($modifiedfrom, \context $contex
public function get_doc_icon(\core_search\document $doc) : \core_search\document_icon {
return new \core_search\document_icon('t/message');
}

/**
* Returns a list of category names associated with the area.
*
* @return array
*/
public function get_category_names() {
return [\core_search\manager::SEARCH_AREA_CATEGORY_USERS];
}
}
10 changes: 10 additions & 0 deletions message/tests/search_received_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,14 @@ public function test_get_doc_icon() {
$this->assertEquals('t/message', $result->get_name());
$this->assertEquals('moodle', $result->get_component());
}

/**
* Test assigned search categories.
*/
public function test_get_category_names() {
$searcharea = \core_search\manager::get_search_area($this->messagereceivedareaid);

$expected = ['core-users'];
$this->assertEquals($expected, $searcharea->get_category_names());
}
}
10 changes: 10 additions & 0 deletions message/tests/search_sent_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,14 @@ public function test_get_doc_icon() {
$this->assertEquals('t/message', $result->get_name());
$this->assertEquals('moodle', $result->get_component());
}

/**
* Test assigned search categories.
*/
public function test_get_category_names() {
$searcharea = \core_search\manager::get_search_area($this->messagesentareaid);

$expected = ['core-users'];
$this->assertEquals($expected, $searcharea->get_category_names());
}
}
126 changes: 126 additions & 0 deletions search/classes/area_category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Search area category.
*
* @package core_search
* @copyright Dmitrii Metelkin <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_search;

defined('MOODLE_INTERNAL') || die();

/**
* Search area category.
*
* @package core_search
* @copyright Dmitrii Metelkin <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class area_category {

/**
* Category name.
* @var string
*/
protected $name;

/**
* Category visible name.
* @var string
*/
protected $visiblename;

/**
* Category order.
* @var int
*/
protected $order = 0;

/**
* Category areas.
* @var \core_search\base[]
*/
protected $areas = [];

/**
* Constructor.
*
* @param string $name Unique name of the category.
* @param string $visiblename Visible name of the category.
* @param int $order Category position in the list (smaller numbers will be displayed first).
* @param \core_search\base[] $areas A list of search areas associated with this category.
*/
public function __construct(string $name, string $visiblename, int $order = 0, array $areas = []) {
$this->name = $name;
$this->visiblename = $visiblename;
$this->order = $order;
$this->set_areas($areas);
}

/**
* Get name.
*
* @return string
*/
public function get_name() {
return $this->name;
}

/**
* Get visible name.
*
* @return string
*/
public function get_visiblename() {
return $this->visiblename;
}

/**
* Get order to display.
*
* @return int
*/
public function get_order() {
return $this->order;
}

/**
* Return a keyed by area id list of areas for this category.
*
* @return \core_search\base[]
*/
public function get_areas() {
return $this->areas;
}

/**
* Set list of search areas for this category,
*
* @param \core_search\base[] $areas
*/
public function set_areas(array $areas) {
foreach ($areas as $area) {
if ($area instanceof base && !key_exists($area->get_area_id(), $this->areas)) {
$this->areas[$area->get_area_id()] = $area;
}
}
}

}
9 changes: 9 additions & 0 deletions search/classes/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,4 +532,13 @@ public function get_contexts_to_reindex() {
public function get_doc_icon(document $doc) : document_icon {
return new document_icon('i/empty');
}

/**
* Returns a list of category names associated with the area.
*
* @return array
*/
public function get_category_names() {
return [manager::SEARCH_AREA_CATEGORY_OTHER];
}
}
9 changes: 9 additions & 0 deletions search/classes/base_block.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,13 @@ public function get_contexts_to_reindex() {
public function get_doc_icon(document $doc) : document_icon {
return new document_icon('e/anchor');
}

/**
* Returns a list of category names associated with the area.
*
* @return array
*/
public function get_category_names() {
return [manager::SEARCH_AREA_CATEGORY_COURSE_CONTENT];
}
}
9 changes: 9 additions & 0 deletions search/classes/base_mod.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,13 @@ public function restrict_cm_access_by_group(\cm_info $cm) {
public function get_doc_icon(document $doc) : document_icon {
return new document_icon('icon', $this->get_module_name());
}

/**
* Returns a list of category names associated with the area.
*
* @return array
*/
public function get_category_names() {
return [manager::SEARCH_AREA_CATEGORY_COURSE_CONTENT];
}
}
Loading

0 comments on commit e9074ee

Please sign in to comment.