From 17675612b5b651b0056dc8173ff275789d59ef41 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Fri, 19 Feb 2016 14:36:16 +0800 Subject: [PATCH] MDL-31989 report_search: Adding the report --- report/search/classes/output/form.php | 75 ++++++++++++++++++ report/search/classes/output/renderer.php | 79 +++++++++++++++++++ report/search/index.php | 94 +++++++++++++++++++++++ report/search/lang/en/report_search.php | 35 +++++++++ report/search/settings.php | 35 +++++++++ report/search/version.php | 29 +++++++ 6 files changed, 347 insertions(+) create mode 100644 report/search/classes/output/form.php create mode 100644 report/search/classes/output/renderer.php create mode 100644 report/search/index.php create mode 100644 report/search/lang/en/report_search.php create mode 100644 report/search/settings.php create mode 100644 report/search/version.php diff --git a/report/search/classes/output/form.php b/report/search/classes/output/form.php new file mode 100644 index 00000000000..0d592cc84f5 --- /dev/null +++ b/report/search/classes/output/form.php @@ -0,0 +1,75 @@ +. + +/** + * Global Search admin form definition + * + * @package report_search + * @copyright Prateek Sachan {@link http://prateeksachan.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace report_search\output; + +defined('MOODLE_INTERNAL') || die(); + +require_once("$CFG->libdir/formslib.php"); + +/** + * Search report form. + * + * @package report_search + * @copyright Prateek Sachan {@link http://prateeksachan.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class form extends \moodleform { + + /** + * Form definition. + * + * @return void + */ + public function definition() { + + $mform = $this->_form; + + $checkboxarray = array(); + $checkboxarray[] =& $mform->createElement('checkbox', 'reindex', '', get_string('indexsite', 'report_search')); + $mform->addGroup($checkboxarray, 'reindexcheckbox', '', array(''), false); + $mform->closeHeaderBefore('reindexcheckbox'); + + $checkboxarray = array(); + $checkboxarray[] =& $mform->createElement('checkbox', 'delete', '', get_string('delete', 'report_search')); + $mform->addGroup($checkboxarray, 'deletecheckbox', '', array(''), false); + $mform->closeHeaderBefore('deletecheckbox'); + + // Only available if delete checked. + $areacheckboxarray = array(); + $areacheckboxarray[] =& $mform->createElement('advcheckbox', 'all', '', get_string('entireindex', 'report_search'), + array('group' => 1)); + $mform->setDefault('all', true); + + foreach ($this->_customdata['searchareas'] as $key => $searcharea) { + $areacheckboxarray[] =& $mform->createElement('advcheckbox', $key, '', + $searcharea->get_visible_name(), array('group' => 2)); + } + $mform->addGroup($areacheckboxarray, 'areasadvcheckbox', '', array(' '), false); + $mform->closeHeaderBefore('areasadvcheckbox'); + $mform->disabledIf('areasadvcheckbox', 'delete', 'notchecked'); + + $this->add_action_buttons(false, get_string('execute', 'report_search')); + } +} diff --git a/report/search/classes/output/renderer.php b/report/search/classes/output/renderer.php new file mode 100644 index 00000000000..119292fadc4 --- /dev/null +++ b/report/search/classes/output/renderer.php @@ -0,0 +1,79 @@ +. + +/** + * Search report renderer. + * + * @package report_search + * @copyright 2015 David Monllao {@link http://www.davidmonllao.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace report_search\output; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Renderer for search report. + * + * @package report_search + * @copyright 2015 David Monllao {@link http://www.davidmonllao.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class renderer extends \plugin_renderer_base { + + /** + * Renders the global search admin interface. + * + * @param \report_search\output\form\admin $form + * @param \core_search\area\base[] $searchareas + * @param \stdClass[] $areasconfig + * @return string HTML + */ + public function render_report($form, $searchareas, $areasconfig) { + + $table = new \html_table(); + $table->head = array(get_string('searcharea', 'search'), get_string('newestdocindexed', 'report_search'), + get_string('lastrun', 'report_search')); + + foreach ($searchareas as $areaid => $searcharea) { + $cname = new \html_table_cell($searcharea->get_visible_name()); + $clastrun = new \html_table_cell($areasconfig[$areaid]->lastindexrun); + if ($areasconfig[$areaid]->indexingstart) { + $timediff = $areasconfig[$areaid]->indexingend - $areasconfig[$areaid]->indexingstart; + $ctimetaken = new \html_table_cell($timediff . ' , ' . + $areasconfig[$areaid]->docsprocessed . ' , ' . + $areasconfig[$areaid]->recordsprocessed . ' , ' . + $areasconfig[$areaid]->docsignored); + } else { + $ctimetaken = ''; + } + $row = new \html_table_row(array($cname, $clastrun, $ctimetaken)); + $table->data[] = $row; + } + + // Display the table. + $content = \html_writer::table($table); + + // Display the form. + $formcontents = $this->output->heading(get_string('indexform', 'report_search'), 3) . + $this->output->notification(get_string('indexinginfo', 'report_search'), 'notifymessage') . $form->render(); + $content .= \html_writer::tag('div', $formcontents, array('id' => 'searchindexform')); + + return $content; + } + +} diff --git a/report/search/index.php b/report/search/index.php new file mode 100644 index 00000000000..5da3d20314d --- /dev/null +++ b/report/search/index.php @@ -0,0 +1,94 @@ +. + +/** + * Global search report + * + * @package report_search + * @copyright Prateek Sachan {@link http://prateeksachan.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(__DIR__ . '/../../config.php'); +require_once($CFG->libdir . '/adminlib.php'); + +admin_externalpage_setup('reportsearch'); + +$pagetitle = get_string('pluginname', 'report_search'); +$PAGE->set_title($pagetitle); +$PAGE->set_heading($pagetitle); + +echo $OUTPUT->header(); +echo $OUTPUT->heading($pagetitle); + +if (\core_search\manager::is_global_search_enabled() === false) { + $renderer = $PAGE->get_renderer('core_search'); + echo $renderer->render_search_disabled(); + echo $OUTPUT->footer(); + exit; +} + +$renderer = $PAGE->get_renderer('report_search'); +$search = \core_search\manager::instance(); + +// All enabled components. +$searchareas = $search->get_search_areas_list(true); + +$mform = new \report_search\output\form(null, array('searchareas' => $searchareas)); +if ($data = $mform->get_data()) { + + if (!empty($data->delete)) { + if (!empty($data->all)) { + $search->delete_index(); + } else { + $anydelete = false; + // We check that the component exist and is enabled. + foreach ($searchareas as $areaid => $searcharea) { + if (!empty($data->{$areaid})) { + $anydelete = true; + $search->delete_index($areaid); + } + } + } + + if (!empty($data->all) || $anydelete) { + echo $OUTPUT->notification(get_string('deleted', 'report_search'), 'notifysuccess'); + + // Purge the cache. + $cache = \cache::make('core', 'search_results'); + $cache->purge(); + } + } + + if (!empty($data->reindex)) { + // Force full reindex. Quite heavy operation. + $search->index(true); + $search->optimize_index(); + echo $OUTPUT->notification(get_string('indexed', 'report_search'), 'notifysuccess'); + } +} + +// After processing the form as config might change depending on the action. +$areasconfig = $search->get_areas_config($searchareas); + +// Ensure that all search areas that we are going to display have config. +$missingareas = array_diff_key($searchareas, $areasconfig); +foreach ($missingareas as $searcharea) { + $search->reset_config($searcharea->get_area_id()); +} + +echo $renderer->render_report($mform, $searchareas, $areasconfig); +echo $OUTPUT->footer(); diff --git a/report/search/lang/en/report_search.php b/report/search/lang/en/report_search.php new file mode 100644 index 00000000000..5cfd6eb87b9 --- /dev/null +++ b/report/search/lang/en/report_search.php @@ -0,0 +1,35 @@ +. + +/** + * Strings for component 'report_search' + * + * @package report_search + * @copyright 2015 David Monllao {@link http://www.davidmonllao.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +$string['delete'] = 'Delete'; +$string['deleted'] = 'Selected indexes deleted'; +$string['entireindex'] = 'Entire index'; +$string['execute'] = 'Execute'; +$string['indexed'] = 'Indexing finished'; +$string['indexform'] = 'Indexing'; +$string['indexinginfo'] = 'The recommended way to index your site\'s contents is using "Global search indexing" scheduled task which runs automatically by Cron.'; +$string['indexsite'] = 'Index all site contents'; +$string['lastrun'] = 'Last run (time, # docs, # records, # ignores)'; +$string['newestdocindexed'] = 'Newest document indexed'; +$string['pluginname'] = 'Global search info'; diff --git a/report/search/settings.php b/report/search/settings.php new file mode 100644 index 00000000000..7f5f887db85 --- /dev/null +++ b/report/search/settings.php @@ -0,0 +1,35 @@ +. + +/** + * Adds the search report link to the admin tree. + * + * @package report_search + * @copyright 2015 David Monllao {@link http://www.davidmonllao.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +if ($hassiteconfig) { + + $searchurl = $CFG->wwwroot . '/report/search/index.php'; + $ADMIN->add('reports', new admin_externalpage('reportsearch', new lang_string('pluginname', 'report_search'), + $searchurl)); + + // No report settings. + $settings = null; +} diff --git a/report/search/version.php b/report/search/version.php new file mode 100644 index 00000000000..756e0c9123a --- /dev/null +++ b/report/search/version.php @@ -0,0 +1,29 @@ +. + +/** + * Version details. + * + * @package report_search + * @copyright 2015 David Monllao {@link http://www.davidmonllao.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$plugin->version = 2016012001; // The current plugin version (Date: YYYYMMDDXX). +$plugin->requires = 2015111000; // Requires this Moodle version. +$plugin->component = 'report_search'; // Full name of the plugin (used for diagnostics). -- 2.43.0