forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
792698a
commit ce6643a
Showing
9 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |