/** * Test workshop::check_group_membership() functionality. */ public function test_check_group_membership() { global $DB, $CFG; $this->resetAfterTest(); $courseid = $this->course->id; $generator = $this->getDataGenerator(); // Make test groups. $group1 = $generator->create_group(array('courseid' => $courseid)); $group2 = $generator->create_group(array('courseid' => $courseid)); $group3 = $generator->create_group(array('courseid' => $courseid)); // Revoke the accessallgroups from non-editing teachers (tutors). $roleids = $DB->get_records_menu('role', null, '', 'shortname, id'); unassign_capability('moodle/site:accessallgroups', $roleids['teacher']); // Create test use accounts. $teacher1 = $generator->create_user(); $tutor1 = $generator->create_user(); $tutor2 = $generator->create_user(); $student1 = $generator->create_user(); $student2 = $generator->create_user(); $student3 = $generator->create_user(); // Enrol the teacher (has the access all groups permission). $generator->enrol_user($teacher1->id, $courseid, $roleids['editingteacher']); // Enrol tutors (can not access all groups). $generator->enrol_user($tutor1->id, $courseid, $roleids['teacher']); $generator->enrol_user($tutor2->id, $courseid, $roleids['teacher']); // Enrol students. $generator->enrol_user($student1->id, $courseid, $roleids['student']); $generator->enrol_user($student2->id, $courseid, $roleids['student']); $generator->enrol_user($student3->id, $courseid, $roleids['student']); // Add users in groups. groups_add_member($group1, $tutor1); groups_add_member($group2, $tutor2); groups_add_member($group1, $student1); groups_add_member($group2, $student2); groups_add_member($group3, $student3); // Workshop with no groups. $workshopitem1 = $this->getDataGenerator()->create_module('workshop', ['course' => $courseid, 'groupmode' => NOGROUPS]); $cm = get_coursemodule_from_instance('workshop', $workshopitem1->id, $courseid, false, MUST_EXIST); $workshop1 = new testable_workshop($workshopitem1, $cm, $this->course); $this->setUser($teacher1); $this->assertTrue($workshop1->check_group_membership($student1->id)); $this->assertTrue($workshop1->check_group_membership($student2->id)); $this->assertTrue($workshop1->check_group_membership($student3->id)); $this->setUser($tutor1); $this->assertTrue($workshop1->check_group_membership($student1->id)); $this->assertTrue($workshop1->check_group_membership($student2->id)); $this->assertTrue($workshop1->check_group_membership($student3->id)); // Workshop in visible groups mode. $workshopitem2 = $this->getDataGenerator()->create_module('workshop', ['course' => $courseid, 'groupmode' => VISIBLEGROUPS]); $cm = get_coursemodule_from_instance('workshop', $workshopitem2->id, $courseid, false, MUST_EXIST); $workshop2 = new testable_workshop($workshopitem2, $cm, $this->course); $this->setUser($teacher1); $this->assertTrue($workshop2->check_group_membership($student1->id)); $this->assertTrue($workshop2->check_group_membership($student2->id)); $this->assertTrue($workshop2->check_group_membership($student3->id)); $this->setUser($tutor1); $this->assertTrue($workshop2->check_group_membership($student1->id)); $this->assertTrue($workshop2->check_group_membership($student2->id)); $this->assertTrue($workshop2->check_group_membership($student3->id)); // Workshop in separate groups mode. $workshopitem3 = $this->getDataGenerator()->create_module('workshop', ['course' => $courseid, 'groupmode' => SEPARATEGROUPS]); $cm = get_coursemodule_from_instance('workshop', $workshopitem3->id, $courseid, false, MUST_EXIST); $workshop3 = new testable_workshop($workshopitem3, $cm, $this->course); $this->setUser($teacher1); $this->assertTrue($workshop3->check_group_membership($student1->id)); $this->assertTrue($workshop3->check_group_membership($student2->id)); $this->assertTrue($workshop3->check_group_membership($student3->id)); $this->setUser($tutor1); $this->assertTrue($workshop3->check_group_membership($student1->id)); $this->assertFalse($workshop3->check_group_membership($student2->id)); $this->assertFalse($workshop3->check_group_membership($student3->id)); $this->setUser($tutor2); $this->assertFalse($workshop3->check_group_membership($student1->id)); $this->assertTrue($workshop3->check_group_membership($student2->id)); $this->assertFalse($workshop3->check_group_membership($student3->id)); }
/** * Tests user restrictions, as they affect lists of users returned by * core API functions. * * This includes the groupingid option (when group mode is in use), and * standard activity restrictions using the availability API. */ public function test_user_restrictions() { global $DB, $CFG; $this->resetAfterTest(); // Use existing sample course from setUp. $courseid = $this->workshop->course->id; // Make a test grouping and two groups. $generator = $this->getDataGenerator(); $grouping = $generator->create_grouping(array('courseid' => $courseid)); $group1 = $generator->create_group(array('courseid' => $courseid)); groups_assign_grouping($grouping->id, $group1->id); $group2 = $generator->create_group(array('courseid' => $courseid)); groups_assign_grouping($grouping->id, $group2->id); // Group 3 is not in the grouping. $group3 = $generator->create_group(array('courseid' => $courseid)); // Enrol some students. $roleids = $DB->get_records_menu('role', null, '', 'shortname, id'); $student1 = $generator->create_user(); $student2 = $generator->create_user(); $student3 = $generator->create_user(); $generator->enrol_user($student1->id, $courseid, $roleids['student']); $generator->enrol_user($student2->id, $courseid, $roleids['student']); $generator->enrol_user($student3->id, $courseid, $roleids['student']); // Place students in groups (except student 3). groups_add_member($group1, $student1); groups_add_member($group2, $student2); groups_add_member($group3, $student3); // The existing workshop doesn't have any restrictions, so user lists // should include all three users. $allusers = get_enrolled_users(context_course::instance($courseid)); $result = $this->workshop->get_grouped($allusers); $this->assertCount(4, $result); $users = array_keys($result[0]); sort($users); $this->assertEquals(array($student1->id, $student2->id, $student3->id), $users); $this->assertEquals(array($student1->id), array_keys($result[$group1->id])); $this->assertEquals(array($student2->id), array_keys($result[$group2->id])); $this->assertEquals(array($student3->id), array_keys($result[$group3->id])); // Test get_users_with_capability_sql (via get_potential_authors). $users = $this->workshop->get_potential_authors(false); $this->assertCount(3, $users); $users = $this->workshop->get_potential_authors(false, $group2->id); $this->assertEquals(array($student2->id), array_keys($users)); // Create another test workshop with grouping set. $workshopitem = $this->getDataGenerator()->create_module('workshop', array('course' => $courseid, 'groupmode' => SEPARATEGROUPS, 'groupingid' => $grouping->id)); $cm = get_coursemodule_from_instance('workshop', $workshopitem->id, $courseid, false, MUST_EXIST); $workshopgrouping = new testable_workshop($workshopitem, $cm, $this->workshop->course); // This time the result should only include users and groups in the // selected grouping. $result = $workshopgrouping->get_grouped($allusers); $this->assertCount(3, $result); $users = array_keys($result[0]); sort($users); $this->assertEquals(array($student1->id, $student2->id), $users); $this->assertEquals(array($student1->id), array_keys($result[$group1->id])); $this->assertEquals(array($student2->id), array_keys($result[$group2->id])); // Test get_users_with_capability_sql (via get_potential_authors). $users = $workshopgrouping->get_potential_authors(false); $userids = array_keys($users); sort($userids); $this->assertEquals(array($student1->id, $student2->id), $userids); $users = $workshopgrouping->get_potential_authors(false, $group2->id); $this->assertEquals(array($student2->id), array_keys($users)); // Enable the availability system and create another test workshop with // availability restriction on grouping. $CFG->enableavailability = true; $workshopitem = $this->getDataGenerator()->create_module('workshop', array('course' => $courseid, 'availability' => json_encode(\core_availability\tree::get_root_json(array(\availability_grouping\condition::get_json($grouping->id)), \core_availability\tree::OP_AND, false)))); $cm = get_coursemodule_from_instance('workshop', $workshopitem->id, $courseid, false, MUST_EXIST); $workshoprestricted = new testable_workshop($workshopitem, $cm, $this->workshop->course); // The get_grouped function isn't intended to apply this restriction, // so it should be the same as the base workshop. (Note: in reality, // get_grouped is always run with the parameter being the result of // one of the get_potential_xxx functions, so it works.) $result = $workshoprestricted->get_grouped($allusers); $this->assertCount(4, $result); $this->assertCount(3, $result[0]); // The get_users_with_capability_sql-based functions should apply it. $users = $workshoprestricted->get_potential_authors(false); $userids = array_keys($users); sort($userids); $this->assertEquals(array($student1->id, $student2->id), $userids); $users = $workshoprestricted->get_potential_authors(false, $group2->id); $this->assertEquals(array($student2->id), array_keys($users)); }
public function test_assessment_reevaluated() { $this->resetAfterTest(); $this->setAdminUser(); $cm = get_coursemodule_from_instance('workshop', $this->workshop->id, $this->course->id, false, MUST_EXIST); $workshop = new testable_workshop($this->workshop, $cm, $this->course); $assessments = array(); $assessments[] = (object) array('reviewerid' => 2, 'gradinggrade' => null, 'gradinggradeover' => null, 'aggregationid' => 2, 'aggregatedgrade' => 12); // Trigger and capture the event. $sink = $this->redirectEvents(); $workshop->aggregate_grading_grades_process($assessments); $events = $sink->get_events(); $event = reset($events); $this->assertInstanceOf('\\mod_workshop\\event\\assessment_reevaluated', $event); $this->assertEquals('workshop_aggregations', $event->objecttable); $this->assertEquals(context_module::instance($cm->id), $event->get_context()); $expected = array($this->course->id, 'workshop', 'update aggregate grade', 'view.php?id=' . $event->get_context()->instanceid, $event->objectid, $event->get_context()->instanceid); $this->assertEventLegacyLogData($expected, $event); $this->assertEventContextNotUsed($event); $sink->close(); }