Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Competency rule all.
  19   *
  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
  23   */
  24  
  25  namespace core_competency;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use lang_string;
  29  
  30  /**
  31   * Competency rule all class.
  32   *
  33   * This rule is considered matched when all the children of a competency are completed.
  34   *
  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
  38   */
  39  class competency_rule_all extends competency_rule {
  40  
  41      /**
  42       * Whether or not the rule is matched.
  43       *
  44       * @param user_competency $usercompetency The user competency.
  45       * @return bool
  46       */
  47      public function matches(user_competency $usercompetency) {
  48          global $DB;
  49  
  50          // TODO Improve performance here, perhaps the caller could already provide records.
  51          $children = competency::get_records(array('parentid' => $this->competency->get('id')));
  52  
  53          if (empty($children)) {
  54              // Leaves are not compatible with this rule.
  55              return false;
  56          }
  57  
  58          $ids = array();
  59          foreach ($children as $child) {
  60              $ids[] = $child->get('id');
  61          }
  62  
  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;
  69  
  70          // Is the user is marked as proficient in all children?
  71          return user_competency::count_records_select($sql, $params) === count($ids);
  72      }
  73  
  74      /**
  75       * Validate the rule config.
  76       *
  77       * @param string $value The value to validate.
  78       * @return bool
  79       */
  80      public function validate_config($value) {
  81          return $value === null;
  82      }
  83  
  84      /**
  85       * The name of the rule.
  86       *
  87       * @return lang_string
  88       */
  89      public static function get_name() {
  90          return new lang_string('allchildrenarecomplete', 'core_competency');
  91      }
  92  }