Merge branch 'MDL-81457-main' of https://github.com/andrewnicols/moodle
[moodle.git] / competency / classes / competency_rule_all.php
blob2874afc90dc98dded18fb5c1200cf805cf3604dd
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Competency rule all.
20 * @package core_competency
21 * @copyright 2015 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_competency;
26 defined('MOODLE_INTERNAL') || die();
28 use lang_string;
30 /**
31 * Competency rule all class.
33 * This rule is considered matched when all the children of a competency are completed.
35 * @package core_competency
36 * @copyright 2015 Frédéric Massart - FMCorz.net
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class competency_rule_all extends competency_rule {
41 /**
42 * Whether or not the rule is matched.
44 * @param user_competency $usercompetency The user competency.
45 * @return bool
47 public function matches(user_competency $usercompetency) {
48 global $DB;
50 // TODO Improve performance here, perhaps the caller could already provide records.
51 $children = competency::get_records(array('parentid' => $this->competency->get('id')));
53 if (empty($children)) {
54 // Leaves are not compatible with this rule.
55 return false;
58 $ids = array();
59 foreach ($children as $child) {
60 $ids[] = $child->get('id');
63 list($insql, $params) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED);
64 $sql = "userid = :userid
65 AND proficiency = :proficiency
66 AND competencyid $insql";
67 $params['userid'] = $usercompetency->get('userid');
68 $params['proficiency'] = 1;
70 // Is the user is marked as proficient in all children?
71 return user_competency::count_records_select($sql, $params) === count($ids);
74 /**
75 * Validate the rule config.
77 * @param string $value The value to validate.
78 * @return bool
80 public function validate_config($value) {
81 return $value === null;
84 /**
85 * The name of the rule.
87 * @return lang_string
89 public static function get_name() {
90 return new lang_string('allchildrenarecomplete', 'core_competency');