Merge branch 'MDL-55980-master' of https://github.com/sammarshallou/moodle
[moodle.git] / admin / searchareas.php
blobdcbc7cb0bfd2bb124a5e453dda00de22417ebf57
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 } else {
129 $laststatus = '';
131 $columns[] = $laststatus;
132 $columns[] = html_writer::link(admin_searcharea_action_url('delete', $areaid), 'Delete index');
134 } else {
135 $blankrow = new html_table_cell(get_string('searchnotavailable', 'admin'));
136 $blankrow->colspan = 3;
137 $columns[] = $blankrow;
140 } else {
141 $columns[] = $OUTPUT->action_icon(admin_searcharea_action_url('enable', $areaid),
142 new pix_icon('t/show', get_string('enable'), 'moodle', array('title' => '', 'class' => 'iconsmall')),
143 null, array('title' => get_string('enable')));
145 $blankrow = new html_table_cell(get_string('searchareadisabled', 'admin'));
146 $blankrow->colspan = 3;
147 $columns[] = $blankrow;
149 $row = new html_table_row($columns);
150 $table->data[] = $row;
153 // Cross-search area tasks.
154 $options = array();
155 if (!empty($searchmanagererror)) {
156 $options['disabled'] = true;
158 echo $OUTPUT->box_start('search-areas-actions');
159 echo $OUTPUT->single_button(admin_searcharea_action_url('indexall'), get_string('searchupdateindex', 'admin'), 'get', $options);
160 echo $OUTPUT->single_button(admin_searcharea_action_url('reindexall'), get_string('searchreindexindex', 'admin'), 'get', $options);
161 echo $OUTPUT->single_button(admin_searcharea_action_url('deleteall'), get_string('searchdeleteindex', 'admin'), 'get', $options);
162 echo $OUTPUT->box_end();
164 echo html_writer::table($table);
165 echo $OUTPUT->footer();
168 * Helper for generating url for management actions.
170 * @param string $action
171 * @param string $areaid
172 * @return moodle_url
174 function admin_searcharea_action_url($action, $areaid = false) {
175 $params = array('action' => $action, 'sesskey' => sesskey());
176 if ($areaid) {
177 $params['areaid'] = $areaid;
179 return new moodle_url('/admin/searchareas.php', $params);