2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Manage global search areas.
20 * @package core_search
21 * @copyright 2016 Dan Poltawski <dan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 require_once(__DIR__
. '/../config.php');
25 require_once($CFG->libdir
. '/adminlib.php');
27 admin_externalpage_setup('searchareas');
29 $areaid = optional_param('areaid', null, PARAM_ALPHAEXT
);
30 $action = optional_param('action', null, PARAM_ALPHA
);
33 $searchmanager = \core_search\manager
::instance();
34 } catch (core_search\engine_exception
$searchmanagererror) {
35 // Continue, we return an error later depending on the requested action.
38 echo $OUTPUT->header();
44 // We need to check that the area exists.
45 $area = \core_search\manager
::get_search_area($areaid);
46 if ($area === false) {
47 throw new moodle_exception('invalidrequest');
51 // All actions but enable/disable need the search engine to be ready.
52 if ($action !== 'enable' && $action !== 'disable') {
53 if (!empty($searchmanagererror)) {
54 throw $searchmanagererror;
60 $area->set_enabled(true);
61 echo $OUTPUT->notification(get_string('searchareaenabled', 'admin'), \core\output\notification
::NOTIFY_SUCCESS
);
64 $area->set_enabled(false);
65 echo $OUTPUT->notification(get_string('searchareadisabled', 'admin'), \core\output\notification
::NOTIFY_SUCCESS
);
68 $search = \core_search\manager
::instance();
69 $search->delete_index($areaid);
70 echo $OUTPUT->notification(get_string('searchindexdeleted', 'admin'), \core\output\notification
::NOTIFY_SUCCESS
);
73 $searchmanager->index();
74 echo $OUTPUT->notification(get_string('searchindexupdated', 'admin'), \core\output\notification
::NOTIFY_SUCCESS
);
77 $searchmanager->index(true);
78 echo $OUTPUT->notification(get_string('searchreindexed', 'admin'), \core\output\notification
::NOTIFY_SUCCESS
);
81 $searchmanager->delete_index();
82 echo $OUTPUT->notification(get_string('searchalldeleted', 'admin'), \core\output\notification
::NOTIFY_SUCCESS
);
85 throw new moodle_exception('invalidaction');
90 $searchareas = \core_search\manager
::get_search_areas_list();
91 if (empty($searchmanagererror)) {
92 $areasconfig = $searchmanager->get_areas_config($searchareas);
97 if (!empty($searchmanagererror)) {
98 $errorstr = get_string($searchmanagererror->errorcode
, $searchmanagererror->module
, $searchmanagererror->a
);
99 echo $OUTPUT->notification($errorstr, \core\output\notification
::NOTIFY_ERROR
);
101 echo $OUTPUT->notification(get_string('indexinginfo', 'admin'), \core\output\notification
::NOTIFY_INFO
);
104 $table = new html_table();
105 $table->id
= 'core-search-areas';
107 $table->head
= array(get_string('searcharea', 'search'), get_string('enable'), get_string('newestdocindexed', 'admin'),
108 get_string('searchlastrun', 'admin'), get_string('searchindexactions', 'admin'));
110 foreach ($searchareas as $area) {
111 $areaid = $area->get_area_id();
112 $columns = array(new html_table_cell($area->get_visible_name()));
114 if ($area->is_enabled()) {
115 $columns[] = $OUTPUT->action_icon(admin_searcharea_action_url('disable', $areaid),
116 new pix_icon('t/hide', get_string('disable'), 'moodle', array('title' => '', 'class' => 'iconsmall')),
117 null, array('title' => get_string('disable')));
120 $columns[] = $areasconfig[$areaid]->lastindexrun
;
122 if ($areasconfig[$areaid]->indexingstart
) {
123 $timediff = $areasconfig[$areaid]->indexingend
- $areasconfig[$areaid]->indexingstart
;
124 $laststatus = $timediff . ' , ' .
125 $areasconfig[$areaid]->docsprocessed
. ' , ' .
126 $areasconfig[$areaid]->recordsprocessed
. ' , ' .
127 $areasconfig[$areaid]->docsignored
;
128 if ($areasconfig[$areaid]->partial
) {
129 $laststatus .= ' ' . get_string('searchpartial', 'admin');
134 $columns[] = $laststatus;
135 $columns[] = html_writer
::link(admin_searcharea_action_url('delete', $areaid), 'Delete index');
138 $blankrow = new html_table_cell(get_string('searchnotavailable', 'admin'));
139 $blankrow->colspan
= 3;
140 $columns[] = $blankrow;
144 $columns[] = $OUTPUT->action_icon(admin_searcharea_action_url('enable', $areaid),
145 new pix_icon('t/show', get_string('enable'), 'moodle', array('title' => '', 'class' => 'iconsmall')),
146 null, array('title' => get_string('enable')));
148 $blankrow = new html_table_cell(get_string('searchareadisabled', 'admin'));
149 $blankrow->colspan
= 3;
150 $columns[] = $blankrow;
152 $row = new html_table_row($columns);
153 $table->data
[] = $row;
156 // Cross-search area tasks.
158 if (!empty($searchmanagererror)) {
159 $options['disabled'] = true;
161 echo $OUTPUT->box_start('search-areas-actions');
162 echo $OUTPUT->single_button(admin_searcharea_action_url('indexall'), get_string('searchupdateindex', 'admin'), 'get', $options);
163 echo $OUTPUT->single_button(admin_searcharea_action_url('reindexall'), get_string('searchreindexindex', 'admin'), 'get', $options);
164 echo $OUTPUT->single_button(admin_searcharea_action_url('deleteall'), get_string('searchdeleteindex', 'admin'), 'get', $options);
165 echo $OUTPUT->box_end();
167 echo html_writer
::table($table);
168 echo $OUTPUT->footer();
171 * Helper for generating url for management actions.
173 * @param string $action
174 * @param string $areaid
177 function admin_searcharea_action_url($action, $areaid = false) {
178 $params = array('action' => $action, 'sesskey' => sesskey());
180 $params['areaid'] = $areaid;
182 return new moodle_url('/admin/searchareas.php', $params);