public function test_reset_data_with_groups()
 {
     global $DB;
     $c1 = $this->getDataGenerator()->create_course();
     $c2 = $this->getDataGenerator()->create_course();
     $u1 = $this->getDataGenerator()->create_user();
     $u2 = $this->getDataGenerator()->create_user();
     $g1 = $this->getDataGenerator()->create_group(array('courseid' => $c1->id));
     $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
     $this->getDataGenerator()->enrol_user($u2->id, $c1->id);
     $this->getDataGenerator()->enrol_user($u1->id, $c2->id);
     $this->getDataGenerator()->create_group_member(array('groupid' => $g1->id, 'userid' => $u1->id));
     $manager = block_xp_manager::get($c1->id);
     $manager->update_config(array('enabled' => true, 'timebetweensameactions' => 0));
     $e = \block_xp\event\something_happened::mock(array('crud' => 'c', 'userid' => $u1->id, 'courseid' => $c1->id));
     $manager->capture_event($e);
     $manager->capture_event($e);
     $e = \block_xp\event\something_happened::mock(array('crud' => 'c', 'userid' => $u2->id, 'courseid' => $c1->id));
     $manager->capture_event($e);
     $manager->capture_event($e);
     $manager = block_xp_manager::get($c2->id);
     $manager->update_config(array('enabled' => true, 'timebetweensameactions' => 0));
     $e = \block_xp\event\something_happened::mock(array('crud' => 'c', 'userid' => $u1->id, 'courseid' => $c2->id));
     $manager->capture_event($e);
     $this->assertEquals(1, $DB->count_records('block_xp', array('courseid' => $c1->id, 'userid' => $u1->id)));
     $this->assertEquals(1, $DB->count_records('block_xp', array('courseid' => $c1->id, 'userid' => $u2->id)));
     $this->assertEquals(2, $DB->count_records('block_xp_log', array('courseid' => $c1->id, 'userid' => $u1->id)));
     $this->assertEquals(2, $DB->count_records('block_xp_log', array('courseid' => $c1->id, 'userid' => $u2->id)));
     $this->assertEquals(1, $DB->count_records('block_xp', array('courseid' => $c2->id)));
     $this->assertEquals(1, $DB->count_records('block_xp_log', array('courseid' => $c2->id)));
     $manager = block_xp_manager::get($c1->id);
     $manager->reset_data($g1->id);
     $this->assertEquals(0, $DB->count_records('block_xp', array('courseid' => $c1->id, 'userid' => $u1->id)));
     $this->assertEquals(1, $DB->count_records('block_xp', array('courseid' => $c1->id, 'userid' => $u2->id)));
     $this->assertEquals(0, $DB->count_records('block_xp_log', array('courseid' => $c1->id, 'userid' => $u1->id)));
     $this->assertEquals(2, $DB->count_records('block_xp_log', array('courseid' => $c1->id, 'userid' => $u2->id)));
     $this->assertEquals(1, $DB->count_records('block_xp', array('courseid' => $c2->id)));
     $this->assertEquals(1, $DB->count_records('block_xp_log', array('courseid' => $c2->id)));
 }
Пример #2
0
 public function test_custom_filters()
 {
     $this->resetAfterTest(true);
     $course = $this->getDataGenerator()->create_course();
     $manager = block_xp_manager::get($course->id);
     $fm = $manager->get_filter_manager();
     // Define some custom rules, the sortorder and IDs are mixed here.
     $rule = new block_xp_rule_property(block_xp_rule_base::EQ, 'c', 'crud');
     $data = array('courseid' => $course->id, 'sortorder' => 1, 'points' => 100, 'rule' => $rule);
     block_xp_filter::load_from_data($data)->save();
     $rule = new block_xp_ruleset(array(new block_xp_rule_property(block_xp_rule_base::EQ, 2, 'objectid'), new block_xp_rule_property(block_xp_rule_base::EQ, 'u', 'crud')), block_xp_ruleset::ANY);
     $data = array('courseid' => $course->id, 'sortorder' => 2, 'points' => 120, 'rule' => $rule);
     block_xp_filter::load_from_data($data)->save();
     $rule = new block_xp_ruleset(array(new block_xp_rule_property(block_xp_rule_base::GTE, 100, 'objectid'), new block_xp_rule_property(block_xp_rule_base::EQ, 'r', 'crud')), block_xp_ruleset::ALL);
     $data = array('courseid' => $course->id, 'sortorder' => 0, 'points' => 130, 'rule' => $rule);
     block_xp_filter::load_from_data($data)->save();
     // We can override default filters.
     $e = \block_xp\event\something_happened::mock(array('crud' => 'c', 'objectid' => 2));
     $this->assertSame(100, $fm->get_points_for_event($e));
     // We can still fallback on default filters.
     $e = \block_xp\event\something_happened::mock(array('crud' => 'd'));
     $this->assertSame(0, $fm->get_points_for_event($e));
     // Sort order is respected.
     $e = \block_xp\event\something_happened::mock(array('crud' => 'u', 'objectid' => 2));
     $this->assertSame(120, $fm->get_points_for_event($e));
     $e = \block_xp\event\something_happened::mock(array('crud' => 'r'));
     $this->assertSame(9, $fm->get_points_for_event($e));
     $e = \block_xp\event\something_happened::mock(array('crud' => 'r', 'objectid' => 100));
     $this->assertSame(130, $fm->get_points_for_event($e));
     // This filter will catch everything before the default rules.
     $rule = new block_xp_rule_property(block_xp_rule_base::CT, 'something', 'eventname');
     $data = array('courseid' => $course->id, 'sortorder' => 3, 'points' => 110, 'rule' => $rule);
     block_xp_filter::load_from_data($data)->save();
     $e = \block_xp\event\something_happened::mock(array('crud' => 'd'));
     $this->assertSame(110, $fm->get_points_for_event($e));
     $e = \block_xp\event\something_happened::mock(array('crud' => 'r'));
     $this->assertSame(110, $fm->get_points_for_event($e));
     // This filter will catch everything.
     $rule = new block_xp_rule_property(block_xp_rule_base::CT, 'something', 'eventname');
     $data = array('courseid' => $course->id, 'sortorder' => -1, 'points' => -10, 'rule' => $rule);
     block_xp_filter::load_from_data($data)->save();
     $e = \block_xp\event\something_happened::mock(array('crud' => 'u', 'objectid' => 2));
     $this->assertSame(-10, $fm->get_points_for_event($e));
     $e = \block_xp\event\something_happened::mock(array('crud' => 'r', 'objectid' => 100));
     $this->assertSame(-10, $fm->get_points_for_event($e));
     $e = \block_xp\event\something_happened::mock(array('crud' => 'd'));
     $this->assertSame(-10, $fm->get_points_for_event($e));
     $e = \block_xp\event\something_happened::mock(array('crud' => 'r'));
     $this->assertSame(-10, $fm->get_points_for_event($e));
 }