public function test_rule_points_matching() { $this->resetAfterTest(true); $this->setAdminUser(); $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); $u1 = $this->getDataGenerator()->create_user(); // Set up the framework and competencies. $framework = $lpg->create_framework(); $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $c1->set_ruletype('core_competency\\competency_rule_points'); $comprule = new competency_rule_points($c1); $c11 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(), 'parentid' => $c1->get_id())); $c12 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(), 'parentid' => $c1->get_id())); $c13 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(), 'parentid' => $c1->get_id())); $c14 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(), 'parentid' => $c1->get_id())); // Create some user competency records. $uc1 = $lpg->create_user_competency(array('competencyid' => $c1->get_id(), 'userid' => $u1->id)); $uc11 = $lpg->create_user_competency(array('competencyid' => $c11->get_id(), 'userid' => $u1->id, 'grade' => 1, 'proficiency' => 1)); $uc12 = $lpg->create_user_competency(array('competencyid' => $c12->get_id(), 'userid' => $u1->id, 'grade' => 1, 'proficiency' => 1)); $uc13 = $lpg->create_user_competency(array('competencyid' => $c13->get_id(), 'userid' => $u1->id)); // Enough points. $rule = array('base' => array('points' => 8), 'competencies' => array(array('id' => $c11->get_id(), 'points' => 4, 'required' => 0), array('id' => $c12->get_id(), 'points' => 4, 'required' => 0))); $c1->set_ruleconfig(json_encode($rule)); $c1->update(); $this->assertTrue($comprule->matches($uc1)); // Not enough points. $rule = array('base' => array('points' => 8), 'competencies' => array(array('id' => $c11->get_id(), 'points' => 4, 'required' => 0), array('id' => $c13->get_id(), 'points' => 4, 'required' => 0))); $c1->set_ruleconfig(json_encode($rule)); $c1->update(); $this->assertFalse($comprule->matches($uc1)); // One required that is not met but points were OK. $rule = array('base' => array('points' => 8), 'competencies' => array(array('id' => $c11->get_id(), 'points' => 4, 'required' => 0), array('id' => $c12->get_id(), 'points' => 4, 'required' => 0), array('id' => $c13->get_id(), 'points' => 4, 'required' => 1))); $c1->set_ruleconfig(json_encode($rule)); $c1->update(); $this->assertFalse($comprule->matches($uc1)); // One required, one not, should match. $rule = array('base' => array('points' => 8), 'competencies' => array(array('id' => $c11->get_id(), 'points' => 4, 'required' => 0), array('id' => $c12->get_id(), 'points' => 4, 'required' => 1))); $c1->set_ruleconfig(json_encode($rule)); $c1->update(); $this->assertTrue($comprule->matches($uc1)); // All required and should match. $rule = array('base' => array('points' => 8), 'competencies' => array(array('id' => $c11->get_id(), 'points' => 4, 'required' => 1), array('id' => $c12->get_id(), 'points' => 4, 'required' => 1))); $c1->set_ruleconfig(json_encode($rule)); $c1->update(); $this->assertTrue($comprule->matches($uc1)); // All required, but one doesn't have a user record. $rule = array('base' => array('points' => 4), 'competencies' => array(array('id' => $c12->get_id(), 'points' => 4, 'required' => 1), array('id' => $c14->get_id(), 'points' => 4, 'required' => 1))); $c1->set_ruleconfig(json_encode($rule)); $c1->update(); $this->assertFalse($comprule->matches($uc1)); }
/** * Get the available rules. * * @return array Keys are the class names, values are the name of the rule. */ public static function get_available_rules() { // Fully qualified class names without leading slashes because get_class() does not add them either. $rules = array('core_competency\\competency_rule_all' => competency_rule_all::get_name(), 'core_competency\\competency_rule_points' => competency_rule_points::get_name()); return $rules; }