MDL-61363 tag: add change_instances_context function
[moodle.git] / admin / searchareas.php
blob9cac43969da90ebad0d63c207209598822af7eef
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 echo $OUTPUT->header();
40 if ($action) {
41 require_sesskey();
43 if ($areaid) {
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;
58 switch ($action) {
59 case 'enable':
60 $area->set_enabled(true);
61 echo $OUTPUT->notification(get_string('searchareaenabled', 'admin'), \core\output\notification::NOTIFY_SUCCESS);
62 break;
63 case 'disable':
64 $area->set_enabled(false);
65 echo $OUTPUT->notification(get_string('searchareadisabled', 'admin'), \core\output\notification::NOTIFY_SUCCESS);
66 break;
67 case 'delete':
68 $search = \core_search\manager::instance();
69 $search->delete_index($areaid);
70 echo $OUTPUT->notification(get_string('searchindexdeleted', 'admin'), \core\output\notification::NOTIFY_SUCCESS);
71 break;
72 case 'indexall':
73 $searchmanager->index();
74 echo $OUTPUT->notification(get_string('searchindexupdated', 'admin'), \core\output\notification::NOTIFY_SUCCESS);
75 break;
76 case 'reindexall':
77 $searchmanager->index(true);
78 echo $OUTPUT->notification(get_string('searchreindexed', 'admin'), \core\output\notification::NOTIFY_SUCCESS);
79 break;
80 case 'deleteall':
81 $searchmanager->delete_index();
82 echo $OUTPUT->notification(get_string('searchalldeleted', 'admin'), \core\output\notification::NOTIFY_SUCCESS);
83 break;
84 default:
85 throw new moodle_exception('invalidaction');
86 break;
90 $searchareas = \core_search\manager::get_search_areas_list();
91 if (empty($searchmanagererror)) {
92 $areasconfig = $searchmanager->get_areas_config($searchareas);
93 } else {
94 $areasconfig = false;
97 if (!empty($searchmanagererror)) {
98 $errorstr = get_string($searchmanagererror->errorcode, $searchmanagererror->module, $searchmanagererror->a);
99 echo $OUTPUT->notification($errorstr, \core\output\notification::NOTIFY_ERROR);
100 } else {
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')));
119 if ($areasconfig) {
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');
131 } else {
132 $laststatus = '';
134 $columns[] = $laststatus;
135 $accesshide = html_writer::span($area->get_visible_name(), 'accesshide');
136 $actions = [];
137 $actions[] = $OUTPUT->pix_icon('t/delete', '') .
138 html_writer::link(admin_searcharea_action_url('delete', $areaid),
139 get_string('deleteindex', 'search', $accesshide));
140 if ($area->supports_get_document_recordset()) {
141 $actions[] = $OUTPUT->pix_icon('i/reload', '') . html_writer::link(
142 new moodle_url('searchreindex.php', ['areaid' => $areaid]),
143 get_string('gradualreindex', 'search', $accesshide));
145 $columns[] = html_writer::alist($actions, ['class' => 'unstyled list-unstyled']);
147 } else {
148 $blankrow = new html_table_cell(get_string('searchnotavailable', 'admin'));
149 $blankrow->colspan = 3;
150 $columns[] = $blankrow;
153 } else {
154 $columns[] = $OUTPUT->action_icon(admin_searcharea_action_url('enable', $areaid),
155 new pix_icon('t/show', get_string('enable'), 'moodle', array('title' => '', 'class' => 'iconsmall')),
156 null, array('title' => get_string('enable')));
158 $blankrow = new html_table_cell(get_string('searchareadisabled', 'admin'));
159 $blankrow->colspan = 3;
160 $columns[] = $blankrow;
162 $row = new html_table_row($columns);
163 $table->data[] = $row;
166 // Cross-search area tasks.
167 $options = array();
168 if (!empty($searchmanagererror)) {
169 $options['disabled'] = true;
171 echo $OUTPUT->box_start('search-areas-actions');
172 echo $OUTPUT->single_button(admin_searcharea_action_url('indexall'), get_string('searchupdateindex', 'admin'), 'get', $options);
173 echo $OUTPUT->single_button(admin_searcharea_action_url('reindexall'), get_string('searchreindexindex', 'admin'), 'get', $options);
174 echo $OUTPUT->single_button(admin_searcharea_action_url('deleteall'), get_string('searchdeleteindex', 'admin'), 'get', $options);
175 echo $OUTPUT->box_end();
177 echo html_writer::table($table);
179 if (empty($searchmanagererror)) {
180 // Show information about queued index requests for specific contexts.
181 $searchrenderer = $PAGE->get_renderer('core_search');
182 echo $searchrenderer->render_index_requests_info($searchmanager->get_index_requests_info());
185 echo $OUTPUT->footer();
188 * Helper for generating url for management actions.
190 * @param string $action
191 * @param string $areaid
192 * @return moodle_url
194 function admin_searcharea_action_url($action, $areaid = false) {
195 $params = array('action' => $action, 'sesskey' => sesskey());
196 if ($areaid) {
197 $params['areaid'] = $areaid;
199 return new moodle_url('/admin/searchareas.php', $params);