Skip to content

Commit

Permalink
MDL-58885 core_search: Group support in relevant module search areas
Browse files Browse the repository at this point in the history
  • Loading branch information
sammarshallou committed Feb 16, 2018
1 parent 4359ef1 commit 3261e92
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 4 deletions.
14 changes: 12 additions & 2 deletions mod/data/classes/search/entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ public function get_document_recordset($modifiedfrom = 0, \context $context = nu
* @return \core_search\document
*/
public function get_document($entry, $options = array()) {
global $DB;

try {
$cm = $this->get_cm('data', $entry->dataid, $entry->course);
$context = \context_module::instance($cm->id);
Expand All @@ -97,6 +95,9 @@ public function get_document($entry, $options = array()) {
$doc->set('contextid', $context->id);
$doc->set('courseid', $entry->course);
$doc->set('userid', $entry->userid);
if ($entry->groupid > 0) {
$doc->set('groupid', $entry->groupid);
}
$doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
$doc->set('modified', $entry->timemodified);

Expand Down Expand Up @@ -353,4 +354,13 @@ protected function get_field_class_name($fieldtype) {
require_once($CFG->dirroot . '/mod/data/field/' . $fieldtype . '/field.class.php');
return 'data_field_' . $fieldtype;
}

/**
* Confirms that data entries support group restrictions.
*
* @return bool True
*/
public function supports_group_restriction() {
return true;
}
}
71 changes: 71 additions & 0 deletions mod/data/tests/search_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,77 @@ public function test_data_entries_document() {

}

/**
* Group support for data entries.
*/
public function test_data_entries_group_support() {
global $DB;

// Get the search area and test generators.
$searcharea = \core_search\manager::get_search_area($this->databaseentryareaid);
$generator = $this->getDataGenerator();
$datagenerator = $generator->get_plugin_generator('mod_data');

// Create a course, a user, and two groups.
$course = $generator->create_course();
$user = $generator->create_user();
$generator->enrol_user($user->id, $course->id, 'teacher');
$group1 = $generator->create_group(['courseid' => $course->id]);
$group2 = $generator->create_group(['courseid' => $course->id]);

// Separate groups database.
$data = self::getDataGenerator()->create_module('data', ['course' => $course->id,
'groupmode' => SEPARATEGROUPS]);
$fieldtypes = ['text', 'textarea'];
$this->create_default_data_fields($fieldtypes, $data);
$fields = $DB->get_records('data_fields', array('dataid' => $data->id));
foreach ($fields as $field) {
switch ($field->type) {
case 'text' :
$textid = $field->id;
break;
case 'textarea' :
$textareaid = $field->id;
break;
}
}

// As admin, create entries with each group and all groups.
$this->setAdminUser();
$fieldvalues = [$textid => 'Title', $textareaid => 'Content'];
$e1 = $datagenerator->create_entry($data, $fieldvalues, $group1->id);
$e2 = $datagenerator->create_entry($data, $fieldvalues, $group2->id);
$e3 = $datagenerator->create_entry($data, $fieldvalues);

// Do the indexing of all 3 entries.
$rs = $searcharea->get_recordset_by_timestamp(0);
$results = [];
foreach ($rs as $rec) {
$results[$rec->id] = $rec;
}
$rs->close();
$this->assertCount(3, $results);

// Check each has the correct groupid.
$doc = $searcharea->get_document($results[$e1]);
$this->assertTrue($doc->is_set('groupid'));
$this->assertEquals($group1->id, $doc->get('groupid'));
$doc = $searcharea->get_document($results[$e2]);
$this->assertTrue($doc->is_set('groupid'));
$this->assertEquals($group2->id, $doc->get('groupid'));
$doc = $searcharea->get_document($results[$e3]);
$this->assertFalse($doc->is_set('groupid'));

// While we're here, also test that the search area requests restriction by group.
$modinfo = get_fast_modinfo($course);
$this->assertTrue($searcharea->restrict_cm_access_by_group($modinfo->get_cm($data->cmid)));

// In visible groups mode, it won't request restriction by group.
set_coursemodule_groupmode($data->cmid, VISIBLEGROUPS);
$modinfo = get_fast_modinfo($course);
$this->assertFalse($searcharea->restrict_cm_access_by_group($modinfo->get_cm($data->cmid)));
}

/**
* Document accesses.
*
Expand Down
16 changes: 15 additions & 1 deletion mod/forum/classes/search/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function get_document_recordset($modifiedfrom = 0, \context $context = nu
return null;
}

$sql = "SELECT fp.*, f.id AS forumid, f.course AS courseid
$sql = "SELECT fp.*, f.id AS forumid, f.course AS courseid, fd.groupid AS groupid
FROM {forum_posts} fp
JOIN {forum_discussions} fd ON fd.id = fp.discussion
JOIN {forum} f ON f.id = fd.forum
Expand Down Expand Up @@ -110,6 +110,11 @@ public function get_document($record, $options = array()) {
$doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
$doc->set('modified', $record->modified);

// Store group id if there is one. (0 and -1 both mean not restricted to group.)
if ($record->groupid > 0) {
$doc->set('groupid', $record->groupid);
}

// Check if this document should be considered new.
if (isset($options['lastindexedtime']) && ($options['lastindexedtime'] < $record->created)) {
// If the document was created after the last index time, it must be new.
Expand Down Expand Up @@ -305,4 +310,13 @@ protected function get_contexts_to_reindex_extra_sql() {
'MAX(fd.timemodified) DESC'
];
}

/**
* Confirms that data entries support group restrictions.
*
* @return bool True
*/
public function supports_group_restriction() {
return true;
}
}
69 changes: 69 additions & 0 deletions mod/forum/tests/search_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public function test_posts_document() {
$record->userid = $user->id;
$record->forum = $forum1->id;
$record->message = 'discussion';
$record->groupid = 0;
$discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);

// Create post1 in discussion1.
Expand All @@ -205,11 +206,13 @@ public function test_posts_document() {
$record->userid = $user->id;
$record->subject = 'subject1';
$record->message = 'post1';
$record->groupid = -1;
$discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);

$post1 = $DB->get_record('forum_posts', array('id' => $discussion1reply1->id));
$post1->forumid = $forum1->id;
$post1->courseid = $forum1->course;
$post1->groupid = -1;

$doc = $searcharea->get_document($post1);
$this->assertInstanceOf('\core_search\document', $doc);
Expand All @@ -221,6 +224,72 @@ public function test_posts_document() {
$this->assertEquals($discussion1reply1->message, $doc->get('content'));
}

/**
* Group support for forum posts.
*/
public function test_posts_group_support() {
// Get the search area and test generators.
$searcharea = \core_search\manager::get_search_area($this->forumpostareaid);
$generator = $this->getDataGenerator();
$forumgenerator = $generator->get_plugin_generator('mod_forum');

// Create a course, a user, and two groups.
$course = $generator->create_course();
$user = $generator->create_user();
$generator->enrol_user($user->id, $course->id, 'teacher');
$group1 = $generator->create_group(['courseid' => $course->id]);
$group2 = $generator->create_group(['courseid' => $course->id]);

// Separate groups forum.
$forum = self::getDataGenerator()->create_module('forum', ['course' => $course->id,
'groupmode' => SEPARATEGROUPS]);

// Create discussion with each group and one for all groups. One has a post in.
$discussion1 = $forumgenerator->create_discussion(['course' => $course->id,
'userid' => $user->id, 'forum' => $forum->id, 'message' => 'd1',
'groupid' => $group1->id]);
$forumgenerator->create_discussion(['course' => $course->id,
'userid' => $user->id, 'forum' => $forum->id, 'message' => 'd2',
'groupid' => $group2->id]);
$forumgenerator->create_discussion(['course' => $course->id,
'userid' => $user->id, 'forum' => $forum->id, 'message' => 'd3']);

// Create a reply in discussion1.
$forumgenerator->create_post(['discussion' => $discussion1->id, 'parent' => $discussion1->firstpost,
'userid' => $user->id, 'message' => 'p1']);

// Do the indexing of all 4 posts.
$rs = $searcharea->get_recordset_by_timestamp(0);
$results = [];
foreach ($rs as $rec) {
$results[$rec->message] = $rec;
}
$rs->close();
$this->assertCount(4, $results);

// Check each document has the correct groupid.
$doc = $searcharea->get_document($results['d1']);
$this->assertTrue($doc->is_set('groupid'));
$this->assertEquals($group1->id, $doc->get('groupid'));
$doc = $searcharea->get_document($results['d2']);
$this->assertTrue($doc->is_set('groupid'));
$this->assertEquals($group2->id, $doc->get('groupid'));
$doc = $searcharea->get_document($results['d3']);
$this->assertFalse($doc->is_set('groupid'));
$doc = $searcharea->get_document($results['p1']);
$this->assertTrue($doc->is_set('groupid'));
$this->assertEquals($group1->id, $doc->get('groupid'));

// While we're here, also test that the search area requests restriction by group.
$modinfo = get_fast_modinfo($course);
$this->assertTrue($searcharea->restrict_cm_access_by_group($modinfo->get_cm($forum->cmid)));

// In visible groups mode, it won't request restriction by group.
set_coursemodule_groupmode($forum->cmid, VISIBLEGROUPS);
$modinfo = get_fast_modinfo($course);
$this->assertFalse($searcharea->restrict_cm_access_by_group($modinfo->get_cm($forum->cmid)));
}

/**
* Document accesses.
*
Expand Down
14 changes: 13 additions & 1 deletion mod/wiki/classes/search/collaborative_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function get_document_recordset($modifiedfrom = 0, \context $context = nu
return null;
}

$sql = "SELECT p.*, w.id AS wikiid, w.course AS courseid
$sql = "SELECT p.*, w.id AS wikiid, w.course AS courseid, s.groupid AS groupid
FROM {wiki_pages} p
JOIN {wiki_subwikis} s ON s.id = p.subwikiid
JOIN {wiki} w ON w.id = s.wikiid
Expand Down Expand Up @@ -111,6 +111,9 @@ public function get_document($record, $options = array()) {
$doc->set('content', $content);
$doc->set('contextid', $context->id);
$doc->set('courseid', $record->courseid);
if ($record->groupid > 0) {
$doc->set('groupid', $record->groupid);
}
$doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
$doc->set('modified', $record->timemodified);

Expand Down Expand Up @@ -205,4 +208,13 @@ public function get_search_fileareas() {

return $fileareas;
}

/**
* Confirms that data entries support group restrictions.
*
* @return bool True
*/
public function supports_group_restriction() {
return true;
}
}
54 changes: 54 additions & 0 deletions mod/wiki/tests/search_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,60 @@ public function test_collaborative_page_indexing() {
$rs->close();
}

/**
* Group support for wiki entries.
*/
public function test_collaborative_page_group_support() {
// Get the search area and test generators.
$searcharea = \core_search\manager::get_search_area($this->wikicollabpageareaid);
$generator = $this->getDataGenerator();
$wikigenerator = $generator->get_plugin_generator('mod_wiki');

// Create a course, a user, and two groups.
$course = $generator->create_course();
$user = $generator->create_user();
$generator->enrol_user($user->id, $course->id, 'teacher');
$group1 = $generator->create_group(['courseid' => $course->id]);
$group2 = $generator->create_group(['courseid' => $course->id]);

// Separate groups wiki.
$wiki = self::getDataGenerator()->create_module('wiki', ['course' => $course->id,
'groupmode' => SEPARATEGROUPS]);

// Create page with each group and one for all groups.
$wikigenerator->create_page($wiki, ['title' => 'G1', 'group' => $group1->id]);
$wikigenerator->create_page($wiki, ['title' => 'G2', 'group' => $group2->id]);
$wikigenerator->create_page($wiki, ['title' => 'ALLGROUPS']);

// Do the indexing of all 3 pages.
$rs = $searcharea->get_recordset_by_timestamp(0);
$results = [];
foreach ($rs as $rec) {
$results[$rec->title] = $rec;
}
$rs->close();
$this->assertCount(3, $results);

// Check each document has the correct groupid.
$doc = $searcharea->get_document($results['G1']);
$this->assertTrue($doc->is_set('groupid'));
$this->assertEquals($group1->id, $doc->get('groupid'));
$doc = $searcharea->get_document($results['G2']);
$this->assertTrue($doc->is_set('groupid'));
$this->assertEquals($group2->id, $doc->get('groupid'));
$doc = $searcharea->get_document($results['ALLGROUPS']);
$this->assertFalse($doc->is_set('groupid'));

// While we're here, also test that the search area requests restriction by group.
$modinfo = get_fast_modinfo($course);
$this->assertTrue($searcharea->restrict_cm_access_by_group($modinfo->get_cm($wiki->cmid)));

// In visible groups mode, it won't request restriction by group.
set_coursemodule_groupmode($wiki->cmid, VISIBLEGROUPS);
$modinfo = get_fast_modinfo($course);
$this->assertFalse($searcharea->restrict_cm_access_by_group($modinfo->get_cm($wiki->cmid)));
}

/**
* Check collaborative_page check access.
*
Expand Down

0 comments on commit 3261e92

Please sign in to comment.