mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
MDL-55356 core_search: Change existing search areas to new API
This change considers all existing search areas in Moodle and makes necessary changes. Custom change to course search, supported by helper in base.php: * course/classes/search/mycourse.php Custom change to message search: * message/classes/search/message_received.php * message/classes/search/message_sent.php Custom change to user search: * user/classes/search/user.php Custom changes to module areas, supported by helper in base_mod.php: * mod/book/classes/search/chapter.php * mod/data/classes/search/entry.php * mod/forum/classes/search/post.php * mod/glossary/classes/search/entry.php * mod/survey/classes/search/activity.php * mod/wiki/classes/search/collaborative_page.php (Note: the unit tests do not exhaustively check every context type for these, given that's mainly handled by the helper function which was already tested in the base_activity test.) Handled by block base class (no change): * blocks/html/classes/search/content.php Handled by activity base class (no change): * mod/assign/classes/search/activity.php * mod/book/classes/search/activity.php * mod/chat/classes/search/activity.php * mod/choice/classes/search/activity.php * mod/data/classes/search/activity.php * mod/feedback/classes/search/activity.php * mod/folder/classes/search/activity.php * mod/forum/classes/search/activity.php * mod/glossary/classes/search/activity.php * mod/imscp/classes/search/activity.php * mod/label/classes/search/activity.php * mod/lesson/classes/search/activity.php * mod/lti/classes/search/activity.php * mod/page/classes/search/activity.php * mod/quiz/classes/search/activity.php * mod/resource/classes/search/activity.php * mod/scorm/classes/search/activity.php * mod/url/classes/search/activity.php * mod/wiki/classes/search/activity.php * mod/workshop/classes/search/activity.php
This commit is contained in:
parent
81a988833e
commit
66e3702680
21 changed files with 584 additions and 58 deletions
|
@ -45,11 +45,24 @@ class mycourse extends \core_search\base {
|
|||
* Returns recordset containing required data for indexing courses.
|
||||
*
|
||||
* @param int $modifiedfrom timestamp
|
||||
* @return \moodle_recordset
|
||||
* @param \context|null $context Restriction context
|
||||
* @return \moodle_recordset|null Recordset or null if no change possible
|
||||
*/
|
||||
public function get_recordset_by_timestamp($modifiedfrom = 0) {
|
||||
public function get_document_recordset($modifiedfrom = 0, \context $context = null) {
|
||||
global $DB;
|
||||
return $DB->get_recordset_select('course', 'timemodified >= ?', array($modifiedfrom), 'timemodified ASC');
|
||||
|
||||
list ($contextjoin, $contextparams) = $this->get_course_level_context_restriction_sql(
|
||||
$context, 'c');
|
||||
if ($contextjoin === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $DB->get_recordset_sql("
|
||||
SELECT c.*
|
||||
FROM {course} c
|
||||
$contextjoin
|
||||
WHERE c.timemodified >= ?
|
||||
ORDER BY c.timemodified ASC", array_merge($contextparams, [$modifiedfrom]));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -98,6 +98,85 @@ class course_search_testcase extends advanced_testcase {
|
|||
$recordset->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests course indexing support for contexts.
|
||||
*/
|
||||
public function test_mycourses_indexing_contexts() {
|
||||
global $DB, $USER, $SITE;
|
||||
|
||||
$searcharea = \core_search\manager::get_search_area($this->mycoursesareaid);
|
||||
|
||||
// Create some courses in categories, and a forum.
|
||||
$generator = $this->getDataGenerator();
|
||||
$cat1 = $generator->create_category();
|
||||
$course1 = $generator->create_course(['category' => $cat1->id]);
|
||||
$cat2 = $generator->create_category(['parent' => $cat1->id]);
|
||||
$course2 = $generator->create_course(['category' => $cat2->id]);
|
||||
$cat3 = $generator->create_category();
|
||||
$course3 = $generator->create_course(['category' => $cat3->id]);
|
||||
$forum = $generator->create_module('forum', ['course' => $course1->id]);
|
||||
$DB->set_field('course', 'timemodified', 0, ['id' => $SITE->id]);
|
||||
$DB->set_field('course', 'timemodified', 1, ['id' => $course1->id]);
|
||||
$DB->set_field('course', 'timemodified', 2, ['id' => $course2->id]);
|
||||
$DB->set_field('course', 'timemodified', 3, ['id' => $course3->id]);
|
||||
|
||||
// Find the first block to use for a block context.
|
||||
$blockid = array_values($DB->get_records('block_instances', null, 'id', 'id', 0, 1))[0]->id;
|
||||
$blockcontext = context_block::instance($blockid);
|
||||
|
||||
// Check with block context - should be null.
|
||||
$this->assertNull($searcharea->get_document_recordset(0, $blockcontext));
|
||||
|
||||
// Check with user context - should be null.
|
||||
$this->setAdminUser();
|
||||
$usercontext = context_user::instance($USER->id);
|
||||
$this->assertNull($searcharea->get_document_recordset(0, $usercontext));
|
||||
|
||||
// Check with module context - should be null.
|
||||
$modcontext = context_module::instance($forum->cmid);
|
||||
$this->assertNull($searcharea->get_document_recordset(0, $modcontext));
|
||||
|
||||
// Check with course context - should return specified course if timestamp allows.
|
||||
$coursecontext = context_course::instance($course3->id);
|
||||
$results = self::recordset_to_ids($searcharea->get_document_recordset(3, $coursecontext));
|
||||
$this->assertEquals([$course3->id], $results);
|
||||
$results = self::recordset_to_ids($searcharea->get_document_recordset(4, $coursecontext));
|
||||
$this->assertEquals([], $results);
|
||||
|
||||
// Check with category context - should return course in categories and subcategories.
|
||||
$catcontext = context_coursecat::instance($cat1->id);
|
||||
$results = self::recordset_to_ids($searcharea->get_document_recordset(0, $catcontext));
|
||||
$this->assertEquals([$course1->id, $course2->id], $results);
|
||||
$results = self::recordset_to_ids($searcharea->get_document_recordset(2, $catcontext));
|
||||
$this->assertEquals([$course2->id], $results);
|
||||
|
||||
// Check with system context and null - should return all these courses + site course.
|
||||
$systemcontext = context_system::instance();
|
||||
$results = self::recordset_to_ids($searcharea->get_document_recordset(0, $systemcontext));
|
||||
$this->assertEquals([$SITE->id, $course1->id, $course2->id, $course3->id], $results);
|
||||
$results = self::recordset_to_ids($searcharea->get_document_recordset(0, null));
|
||||
$this->assertEquals([$SITE->id, $course1->id, $course2->id, $course3->id], $results);
|
||||
$results = self::recordset_to_ids($searcharea->get_document_recordset(3, $systemcontext));
|
||||
$this->assertEquals([$course3->id], $results);
|
||||
$results = self::recordset_to_ids($searcharea->get_document_recordset(3, null));
|
||||
$this->assertEquals([$course3->id], $results);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility function to convert recordset to array of IDs for testing.
|
||||
*
|
||||
* @param moodle_recordset $rs Recordset to convert (and close)
|
||||
* @return array Array of IDs from records indexed by number (0, 1, 2, ...)
|
||||
*/
|
||||
protected static function recordset_to_ids(moodle_recordset $rs) {
|
||||
$results = [];
|
||||
foreach ($rs as $rec) {
|
||||
$results[] = $rec->id;
|
||||
}
|
||||
$rs->close();
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Document contents.
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue