Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sammarshallou committed May 2, 2024
1 parent 792698a commit ce6643a
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 0 deletions.
6 changes: 6 additions & 0 deletions admin/settings/plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,12 @@
$ADMIN->add('searchplugins', new admin_externalpage('searchareas', new lang_string('searchareas', 'admin'),
new moodle_url('/admin/searchareas.php')));

// Only add reindex page if search indexing is enabled.
if (\core_search\manager::is_indexing_enabled()) {
$ADMIN->add('searchplugins', new admin_externalpage('searchreindex', new lang_string('reindexcourse', 'search'),
new moodle_url('/search/reindex.php')));
}

core_collator::asort_objects_by_property($pages, 'visiblename');
foreach ($pages as $page) {
$ADMIN->add('searchplugins', $page);
Expand Down
2 changes: 2 additions & 0 deletions lang/en/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
$string['progress'] = 'Progress';
$string['queryerror'] = 'The query you provided could not be parsed by the search engine: {$a}';
$string['queueheading'] = 'Additional indexing queue ({$a} items)';
$string['reindexcourse'] = 'Reindex course or activity';
$string['reindexcourse_info'] = 'If a course or activity gives incomplete search results, you can use this form to reindex it. This is not necessary as part of normal operation.';
$string['resultsreturnedfor'] = 'results returned for';
$string['runindexer'] = 'Run indexer (real)';
$string['runindexertest'] = 'Run indexer test';
Expand Down
6 changes: 6 additions & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,12 @@
'type' => 'read',
'ajax' => true
),
'core_search_get_course_activities' => [
'classname' => '\core_search\external\get_course_activities',
'description' => 'Gets activities on a particular course',
'type' => 'read',
'ajax' => true
],
'core_search_get_results' => [
'classname' => '\core_search\external\get_results',
'description' => 'Get search results.',
Expand Down
10 changes: 10 additions & 0 deletions search/amd/build/activityselector.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions search/amd/build/activityselector.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions search/amd/src/activityselector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Activity selector for the reindex form.
*
* @module core_search/activityselector
* @copyright 2024 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

import Ajax from 'core/ajax';

/**
* Obtains list of activities for Ajax autocomplete element.
*
* @param {String} selector Selector of the element
* @param {String} query The query string
* @param {Function} success Callback function to be called with an array of results
* @param {Function} failure Callback to be called in case of failure, with error message
*/
export async function transport(selector, query, success, failure) {
// Get course id.
const element = document.querySelector(selector);
const courseSelection = element.closest('form').querySelector(
'select[name=courseid] ~ .form-autocomplete-selection');
const courseId = parseInt(courseSelection.dataset.activeValue);

// Do AJAX request to list activities on course matching query.
try {
const response = await Ajax.call([{methodname: 'core_search_get_course_activities', args: {
courseid: courseId,
query: query
}}]);
success(response);
} catch (e) {
failure(e);
}
}

/**
* Processes results for Ajax autocomplete element.
*
* @param {String} selector Selector of the element
* @param {Array} results Array of results
* @return {Object[]} Array of results with 'value' and 'label' fields
*/
export function processResults(selector, results) {
const output = [];
for (let result of results) {
output.push({value: result.cmid, label: result.name});
}
return output;
}
104 changes: 104 additions & 0 deletions search/classes/external/get_course_activities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core_search\external;

use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_multiple_structure;
use core_external\external_value;

/**
* External function for listing activities on a course.
*
* @package core_search
* @copyright 2024 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_course_activities extends external_api {

/**
* Webservice parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters(
[
'courseid' => new external_value(PARAM_INT, 'Course id'),
'query' => new external_value(PARAM_NOTAGS, 'Optional query (blank = all activities)'),
]
);
}

/**
* Webservice returns.
*
* @return external_multiple_structure
*/
public static function execute_returns(): external_multiple_structure {
return new external_multiple_structure(new external_single_structure([
'cmid' => new external_value(PARAM_INT, 'Course-module id'),
'name' => new external_value(PARAM_INT, 'Activity name e.g. "General discussion forum"'),
'modname' => new external_value(PARAM_ALPHANUMEXT, 'Module name e.g. "forum"'),
]));
}

/**
* Gets activities on a course.
*
* @param int $courseid Course id
* @param string $query Optional search query ('' = all)
* @return array List of activities
*/
public static function execute(int $courseid, string $query): array {
global $PAGE;

['courseid' => $courseid, 'query' => $query] = self::validate_parameters(
self::execute_parameters(),
[
'courseid' => $courseid,
'query' => $query,
]
);

// Get the context. This should ensure that user is allowed to access the course.
$context = \context_course::instance($courseid);
external_api::validate_context($context);

// Get details for all activities in course.
$modinfo = get_fast_modinfo($courseid);
$results = [];
$lowerquery = \core_text::strtolower($query);
foreach ($modinfo->get_cms() as $cm) {
// When there is a query, skip activities that don't match it.
if ($lowerquery !== '') {
$lowername = \core_text::strtolower($cm->name);
if (strpos($lowername, $lowerquery) === false) {
continue;
}
}
$results[] = (object)[
'cmid' => $cm->id,
'name' => $cm->name,
'modname' => $cm->modname,
];
}

return $results;
}
}
56 changes: 56 additions & 0 deletions search/classes/form/reindex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Form to reindex a course or activity.
*
* @package core_search
* @copyright 2024 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_search\form;

defined('MOODLE_INTERNAL') || die();

require_once($CFG->libdir . '/formslib.php');

/**
* Form to reindex a course or activity.
*
* @package core_search
*/
class reindex extends \moodleform {
/**
* Defines form contents.
*/
public function definition() {
$mform = $this->_form;

$mform->addElement('static', 'info', '', get_string('reindexcourse_info', 'search'));

$mform->addElement('course', 'courseid', get_string('course'));
$mform->addRule('courseid', null, 'required', null, 'client');

// $options = [
// 'ajax' => 'core_search/activityselector'
// ];
// $mform->addElement('autocomplete', 'cmid', get_string('activity'), [], $options);
// $mform->disabledIf('cmid', 'courseid', 'eq', 0);

$mform->addElement('submit', 'reindex', get_string('reindexcourse', 'search'));
}
}
46 changes: 46 additions & 0 deletions search/reindex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Manually request a reindex of a particular context for cases when the index is incorrect.
*
* @package core_search
* @copyright 2024 The Open University
* @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('searchreindex');

$PAGE->set_primary_active_tab('siteadminnode');

$mform = new core_search\form\reindex();

if ($mform->get_data()) {

}

echo $OUTPUT->header();

// Throw an error if search indexing is off - we don't show the page link in that case.
if (!\core_search\manager::is_indexing_enabled()) {
throw new \moodle_exception('notavailable');
}

echo $mform->render();

echo $OUTPUT->footer();

0 comments on commit ce6643a

Please sign in to comment.