Merge branch 'MDL-63012-master' of https://github.com/jobyh/moodle
[moodle.git] / admin / searchareas.php
blob911a86bf94b680ad26c7c8ef597d740a86c509bc
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
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);
31 $indexingenabled = \core_search\manager::is_indexing_enabled(); // This restricts many of the actions on this page.
33 // Get a search manager instance, which we'll need for display and to handle some actions.
34 try {
35 $searchmanager = \core_search\manager::instance();
36 } catch (core_search\engine_exception $searchmanagererror) {
37 // In action cases, we'll throw this exception below. In non-action cases, we produce a lang string error.
40 // Handle all the actions.
41 if ($action) {
42 // If dealing with an areaid, we need to check that the area exists.
43 if ($areaid) {
44 $area = \core_search\manager::get_search_area($areaid);
45 if ($area === false) {
46 throw new moodle_exception('invalidrequest');
50 // All the indexing actions.
51 if (in_array($action, ['delete', 'indexall', 'reindexall', 'deleteall'])) {
53 // All of these actions require that indexing is enabled.
54 if ($indexingenabled) {
56 // For all of these actions, we strictly need a manager instance.
57 if (isset($searchmanagererror)) {
58 throw $searchmanagererror;
61 // Show confirm prompt for all these actions as they may be inadvisable, or may cause
62 // an interruption in search functionality, on production systems.
63 if (!optional_param('confirm', 0, PARAM_INT)) {
64 // Display confirmation prompt.
65 $a = null;
66 if ($areaid) {
67 $a = html_writer::tag('strong', $area->get_visible_name());
70 $actionparams = ['sesskey' => sesskey(), 'action' => $action, 'confirm' => 1];
71 if ($areaid) {
72 $actionparams['areaid'] = $areaid;
74 $actionurl = new moodle_url('/admin/searchareas.php', $actionparams);
75 $cancelurl = new moodle_url('/admin/searchareas.php');
76 echo $OUTPUT->header();
77 echo $OUTPUT->confirm(get_string('confirm_' . $action, 'search', $a),
78 new single_button($actionurl, get_string('continue'), 'post', true),
79 new single_button($cancelurl, get_string('cancel'), 'get'));
80 echo $OUTPUT->footer();
81 exit;
82 } else {
83 // Confirmed, so run the required action.
84 require_sesskey();
86 switch ($action) {
87 case 'delete':
88 $searchmanager->delete_index($areaid);
89 \core\notification::success(get_string('searchindexdeleted', 'admin'));
90 break;
91 case 'indexall':
92 $searchmanager->index();
93 \core\notification::success(get_string('searchindexupdated', 'admin'));
94 break;
95 case 'reindexall':
96 $searchmanager->index(true);
97 \core\notification::success(get_string('searchreindexed', 'admin'));
98 break;
99 case 'deleteall':
100 $searchmanager->delete_index();
101 \core\notification::success(get_string('searchalldeleted', 'admin'));
102 break;
103 default:
104 break;
107 // Redirect back to the main page after taking action.
108 redirect(new moodle_url('/admin/searchareas.php'));
111 } else if (in_array($action, ['enable', 'disable'])) {
112 // Toggling search areas requires no confirmation.
113 require_sesskey();
115 switch ($action) {
116 case 'enable':
117 $area->set_enabled(true);
118 \core\notification::success(get_string('searchareaenabled', 'admin'));
119 break;
120 case 'disable':
121 $area->set_enabled(false);
122 core\notification::success(get_string('searchareadisabled', 'admin'));
123 break;
124 default:
125 break;
128 redirect(new moodle_url('/admin/searchareas.php'));
129 } else {
130 // Invalid action.
131 throw new moodle_exception('invalidaction');
136 // Display.
137 if (isset($searchmanager) && $indexingenabled) {
138 \core\notification::info(get_string('indexinginfo', 'admin'));
139 } else if (isset($searchmanager)) {
140 $params = (object) [
141 'url' => (new moodle_url("/admin/settings.php?section=manageglobalsearch#admin-searchindexwhendisabled"))->out(false)
143 \core\notification::error(get_string('indexwhendisabledfullnotice', 'search', $params));
144 } else {
145 // In non-action cases, init errors are translated and displayed to the user as error notifications.
146 $errorstr = get_string($searchmanagererror->errorcode, $searchmanagererror->module, $searchmanagererror->a);
147 \core\notification::error($errorstr);
150 echo $OUTPUT->header();
152 $table = new html_table();
153 $table->id = 'core-search-areas';
154 $table->head = [
155 get_string('searcharea', 'search'),
156 get_string('enable'),
157 get_string('newestdocindexed', 'admin'),
158 get_string('searchlastrun', 'admin'),
159 get_string('searchindexactions', 'admin')
162 $searchareas = \core_search\manager::get_search_areas_list();
163 $areasconfig = isset($searchmanager) ? $searchmanager->get_areas_config($searchareas) : false;
164 foreach ($searchareas as $area) {
165 $areaid = $area->get_area_id();
166 $columns = array(new html_table_cell($area->get_visible_name()));
168 if ($area->is_enabled()) {
169 $columns[] = $OUTPUT->action_icon(admin_searcharea_action_url('disable', $areaid),
170 new pix_icon('t/hide', get_string('disable'), 'moodle', array('title' => '', 'class' => 'iconsmall')),
171 null, array('title' => get_string('disable')));
173 if ($areasconfig && $indexingenabled) {
174 $columns[] = $areasconfig[$areaid]->lastindexrun;
176 if ($areasconfig[$areaid]->indexingstart) {
177 $timediff = $areasconfig[$areaid]->indexingend - $areasconfig[$areaid]->indexingstart;
178 $laststatus = $timediff . ' , ' .
179 $areasconfig[$areaid]->docsprocessed . ' , ' .
180 $areasconfig[$areaid]->recordsprocessed . ' , ' .
181 $areasconfig[$areaid]->docsignored;
182 if ($areasconfig[$areaid]->partial) {
183 $laststatus .= ' ' . get_string('searchpartial', 'admin');
185 } else {
186 $laststatus = '';
188 $columns[] = $laststatus;
189 $accesshide = html_writer::span($area->get_visible_name(), 'accesshide');
190 $actions = [];
191 $actions[] = $OUTPUT->pix_icon('t/delete', '') .
192 html_writer::link(admin_searcharea_action_url('delete', $areaid),
193 get_string('deleteindex', 'search', $accesshide));
194 if ($area->supports_get_document_recordset()) {
195 $actions[] = $OUTPUT->pix_icon('i/reload', '') . html_writer::link(
196 new moodle_url('searchreindex.php', ['areaid' => $areaid]),
197 get_string('gradualreindex', 'search', $accesshide));
199 $columns[] = html_writer::alist($actions, ['class' => 'unstyled list-unstyled']);
201 } else {
202 if (!$areasconfig) {
203 $blankrow = new html_table_cell(get_string('searchnotavailable', 'admin'));
204 } else {
205 $blankrow = new html_table_cell(get_string('indexwhendisabledshortnotice', 'search'));
207 $blankrow->colspan = 3;
208 $columns[] = $blankrow;
211 } else {
212 $columns[] = $OUTPUT->action_icon(admin_searcharea_action_url('enable', $areaid),
213 new pix_icon('t/show', get_string('enable'), 'moodle', array('title' => '', 'class' => 'iconsmall')),
214 null, array('title' => get_string('enable')));
216 $blankrow = new html_table_cell(get_string('searchareadisabled', 'admin'));
217 $blankrow->colspan = 3;
218 $columns[] = $blankrow;
220 $row = new html_table_row($columns);
221 $table->data[] = $row;
224 // Cross-search area tasks.
225 $options = (isset($searchmanager) && $indexingenabled) ? [] : ['disabled' => true];
226 echo $OUTPUT->box_start('search-areas-actions');
227 echo $OUTPUT->single_button(admin_searcharea_action_url('indexall'), get_string('searchupdateindex', 'admin'), 'get', $options);
228 echo $OUTPUT->single_button(admin_searcharea_action_url('reindexall'), get_string('searchreindexindex', 'admin'), 'get', $options);
229 echo $OUTPUT->single_button(admin_searcharea_action_url('deleteall'), get_string('searchdeleteindex', 'admin'), 'get', $options);
230 echo $OUTPUT->box_end();
232 echo html_writer::table($table);
234 if (isset($searchmanager)) {
235 // Show information about queued index requests for specific contexts.
236 $searchrenderer = $PAGE->get_renderer('core_search');
237 echo $searchrenderer->render_index_requests_info($searchmanager->get_index_requests_info());
240 echo $OUTPUT->footer();
243 * Helper for generating url for management actions.
245 * @param string $action
246 * @param string $areaid
247 * @return moodle_url
249 function admin_searcharea_action_url($action, $areaid = false) {
250 $params = array('action' => $action);
251 if ($areaid) {
252 $params['areaid'] = $areaid;
254 if ($action === 'disable' || $action === 'enable') {
255 $params['sesskey'] = sesskey();
257 return new moodle_url('/admin/searchareas.php', $params);