Merge branch 'MDL-62236-master' of git://github.com/FMCorz/moodle
[moodle.git] / admin / searchareas.php
blobbf178b55b413b0e5576fd5cdb15bea4fab779ac4
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);
32 try {
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 if ($action) {
40 if ($areaid) {
41 // We need to check that the area exists.
42 $area = \core_search\manager::get_search_area($areaid);
43 if ($area === false) {
44 throw new moodle_exception('invalidrequest');
48 if ($action !== 'enable' && $action !== 'disable') {
49 // All actions but enable/disable need the search engine to be ready.
50 if (!empty($searchmanagererror)) {
51 throw $searchmanagererror;
54 // Show confirm prompt for all these actions as they may be inadvisable, or may cause
55 // an interruption in search functionality, on production systems.
56 if (!optional_param('confirm', 0, PARAM_INT)) {
57 // Display confirmation prompt.
58 $a = null;
59 if ($areaid) {
60 $a = html_writer::tag('strong', $area->get_visible_name());
63 $actionparams = ['sesskey' => sesskey(), 'action' => $action, 'confirm' => 1];
64 if ($areaid) {
65 $actionparams['areaid'] = $areaid;
67 $actionurl = new moodle_url('/admin/searchareas.php', $actionparams);
68 $cancelurl = new moodle_url('/admin/searchareas.php');
69 echo $OUTPUT->header();
70 echo $OUTPUT->confirm(get_string('confirm_' . $action, 'search', $a),
71 new single_button($actionurl, get_string('continue'), 'post', true),
72 new single_button($cancelurl, get_string('cancel'), 'get'));
73 echo $OUTPUT->footer();
74 exit;
78 // We are now taking an actual action, so require sesskey.
79 require_sesskey();
81 switch ($action) {
82 case 'enable':
83 $area->set_enabled(true);
84 \core\notification::add(get_string('searchareaenabled', 'admin'), \core\output\notification::NOTIFY_SUCCESS);
85 break;
86 case 'disable':
87 $area->set_enabled(false);
88 \core\notification::add(get_string('searchareadisabled', 'admin'), \core\output\notification::NOTIFY_SUCCESS);
89 break;
90 case 'delete':
91 $search = \core_search\manager::instance();
92 $search->delete_index($areaid);
93 \core\notification::add(get_string('searchindexdeleted', 'admin'), \core\output\notification::NOTIFY_SUCCESS);
94 break;
95 case 'indexall':
96 $searchmanager->index();
97 \core\notification::add(get_string('searchindexupdated', 'admin'), \core\output\notification::NOTIFY_SUCCESS);
98 break;
99 case 'reindexall':
100 $searchmanager->index(true);
101 \core\notification::add(get_string('searchreindexed', 'admin'), \core\output\notification::NOTIFY_SUCCESS);
102 break;
103 case 'deleteall':
104 $searchmanager->delete_index();
105 \core\notification::add(get_string('searchalldeleted', 'admin'), \core\output\notification::NOTIFY_SUCCESS);
106 break;
107 default:
108 throw new moodle_exception('invalidaction');
109 break;
112 // Redirect back to the main page after taking action.
113 redirect(new moodle_url('/admin/searchareas.php'));
116 echo $OUTPUT->header();
118 $searchareas = \core_search\manager::get_search_areas_list();
119 if (empty($searchmanagererror)) {
120 $areasconfig = $searchmanager->get_areas_config($searchareas);
121 } else {
122 $areasconfig = false;
125 if (!empty($searchmanagererror)) {
126 $errorstr = get_string($searchmanagererror->errorcode, $searchmanagererror->module, $searchmanagererror->a);
127 echo $OUTPUT->notification($errorstr, \core\output\notification::NOTIFY_ERROR);
128 } else {
129 echo $OUTPUT->notification(get_string('indexinginfo', 'admin'), \core\output\notification::NOTIFY_INFO);
132 $table = new html_table();
133 $table->id = 'core-search-areas';
135 $table->head = array(get_string('searcharea', 'search'), get_string('enable'), get_string('newestdocindexed', 'admin'),
136 get_string('searchlastrun', 'admin'), get_string('searchindexactions', 'admin'));
138 foreach ($searchareas as $area) {
139 $areaid = $area->get_area_id();
140 $columns = array(new html_table_cell($area->get_visible_name()));
142 if ($area->is_enabled()) {
143 $columns[] = $OUTPUT->action_icon(admin_searcharea_action_url('disable', $areaid),
144 new pix_icon('t/hide', get_string('disable'), 'moodle', array('title' => '', 'class' => 'iconsmall')),
145 null, array('title' => get_string('disable')));
147 if ($areasconfig) {
148 $columns[] = $areasconfig[$areaid]->lastindexrun;
150 if ($areasconfig[$areaid]->indexingstart) {
151 $timediff = $areasconfig[$areaid]->indexingend - $areasconfig[$areaid]->indexingstart;
152 $laststatus = $timediff . ' , ' .
153 $areasconfig[$areaid]->docsprocessed . ' , ' .
154 $areasconfig[$areaid]->recordsprocessed . ' , ' .
155 $areasconfig[$areaid]->docsignored;
156 if ($areasconfig[$areaid]->partial) {
157 $laststatus .= ' ' . get_string('searchpartial', 'admin');
159 } else {
160 $laststatus = '';
162 $columns[] = $laststatus;
163 $accesshide = html_writer::span($area->get_visible_name(), 'accesshide');
164 $actions = [];
165 $actions[] = $OUTPUT->pix_icon('t/delete', '') .
166 html_writer::link(admin_searcharea_action_url('delete', $areaid),
167 get_string('deleteindex', 'search', $accesshide));
168 if ($area->supports_get_document_recordset()) {
169 $actions[] = $OUTPUT->pix_icon('i/reload', '') . html_writer::link(
170 new moodle_url('searchreindex.php', ['areaid' => $areaid]),
171 get_string('gradualreindex', 'search', $accesshide));
173 $columns[] = html_writer::alist($actions, ['class' => 'unstyled list-unstyled']);
175 } else {
176 $blankrow = new html_table_cell(get_string('searchnotavailable', 'admin'));
177 $blankrow->colspan = 3;
178 $columns[] = $blankrow;
181 } else {
182 $columns[] = $OUTPUT->action_icon(admin_searcharea_action_url('enable', $areaid),
183 new pix_icon('t/show', get_string('enable'), 'moodle', array('title' => '', 'class' => 'iconsmall')),
184 null, array('title' => get_string('enable')));
186 $blankrow = new html_table_cell(get_string('searchareadisabled', 'admin'));
187 $blankrow->colspan = 3;
188 $columns[] = $blankrow;
190 $row = new html_table_row($columns);
191 $table->data[] = $row;
194 // Cross-search area tasks.
195 $options = array();
196 if (!empty($searchmanagererror)) {
197 $options['disabled'] = true;
199 echo $OUTPUT->box_start('search-areas-actions');
200 echo $OUTPUT->single_button(admin_searcharea_action_url('indexall'), get_string('searchupdateindex', 'admin'), 'get', $options);
201 echo $OUTPUT->single_button(admin_searcharea_action_url('reindexall'), get_string('searchreindexindex', 'admin'), 'get', $options);
202 echo $OUTPUT->single_button(admin_searcharea_action_url('deleteall'), get_string('searchdeleteindex', 'admin'), 'get', $options);
203 echo $OUTPUT->box_end();
205 echo html_writer::table($table);
207 if (empty($searchmanagererror)) {
208 // Show information about queued index requests for specific contexts.
209 $searchrenderer = $PAGE->get_renderer('core_search');
210 echo $searchrenderer->render_index_requests_info($searchmanager->get_index_requests_info());
213 echo $OUTPUT->footer();
216 * Helper for generating url for management actions.
218 * @param string $action
219 * @param string $areaid
220 * @return moodle_url
222 function admin_searcharea_action_url($action, $areaid = false) {
223 $params = array('action' => $action);
224 if ($areaid) {
225 $params['areaid'] = $areaid;
227 if ($action === 'disable' || $action === 'enable') {
228 $params['sesskey'] = sesskey();
230 return new moodle_url('/admin/searchareas.php', $params);