Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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   * Class for plan_competency persistence.
  19   *
  20   * @package    core_competency
  21   * @copyright  2015 Issam Taboubi <issam.taboubi@umontreal.ca>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core_competency;
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use lang_string;
  28  
  29  /**
  30   * Class for managing competencies in the plan (add/remove competencies for given plan).
  31   *
  32   * @copyright  2015 Issam Taboubi <issam.taboubi@umontreal.ca>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class plan_competency extends persistent {
  36  
  37      /** Table name for plan_competency persistency */
  38      const TABLE = 'competency_plancomp';
  39  
  40      /**
  41       * Return the definition of the properties of this model.
  42       *
  43       * @return array
  44       */
  45      protected static function define_properties() {
  46          return array(
  47              'planid' => array(
  48                  'type' => PARAM_INT,
  49              ),
  50              'competencyid' => array(
  51                  'type' => PARAM_INT,
  52              ),
  53              'sortorder' => array(
  54                  'type' => PARAM_INT,
  55                  'default' => null,
  56              ),
  57          );
  58      }
  59  
  60      /**
  61       * List the competencies in this plan.
  62       *
  63       * @param int $planid The plan id
  64       * @return array[competency]
  65       */
  66      public static function list_competencies($planid) {
  67          global $DB;
  68  
  69          $sql = 'SELECT comp.*
  70                    FROM {' . competency::TABLE . '} comp
  71                    JOIN {' . self::TABLE . '} plancomp
  72                      ON plancomp.competencyid = comp.id
  73                   WHERE plancomp.planid = ?
  74                ORDER BY plancomp.sortorder ASC,
  75                         plancomp.id ASC';
  76          $params = array($planid);
  77  
  78          // TODO MDL-52229 Handle hidden competencies.
  79          $results = $DB->get_records_sql($sql, $params);
  80  
  81          $instances = array();
  82          foreach ($results as $result) {
  83              array_push($instances, new competency(0, $result));
  84          }
  85  
  86          return $instances;
  87      }
  88  
  89      /**
  90       * Get a single competency from the plan (only if it is really in the plan).
  91       *
  92       * @param int $planid The plan id
  93       * @param int $competencyid The competency id
  94       * @return competency
  95       */
  96      public static function get_competency($planid, $competencyid) {
  97          global $DB;
  98  
  99          $sql = 'SELECT comp.*
 100                    FROM {' . competency::TABLE . '} comp
 101                    JOIN {' . self::TABLE . '} plncomp
 102                      ON plncomp.competencyid = comp.id
 103                   WHERE plncomp.planid = ? AND plncomp.competencyid = ?';
 104          $params = array($planid, $competencyid);
 105  
 106          $result = $DB->get_record_sql($sql, $params);
 107          if (!$result) {
 108              throw new \coding_exception('The competency does not belong to this plan: ' . $competencyid . ', ' . $planid);
 109          }
 110  
 111          return new competency(0, $result);
 112      }
 113  
 114      /**
 115       * Hook to execute before validate.
 116       *
 117       * @return void
 118       */
 119      protected function before_validate() {
 120          if (($this->get('id') && $this->get('sortorder') === null) || !$this->get('id')) {
 121              $this->set('sortorder', $this->count_records(array('planid' => $this->get('planid'))));
 122          }
 123      }
 124  
 125      /**
 126       * Validate competencyid.
 127       *
 128       * @param  int $value ID.
 129       * @return true|lang_string
 130       */
 131      protected function validate_competencyid($value) {
 132          if (!competency::record_exists($value)) {
 133              return new lang_string('invaliddata', 'error');
 134          }
 135          return true;
 136      }
 137  
 138      /**
 139       * Validate planid.
 140       *
 141       * @param  int $value ID.
 142       * @return true|lang_string
 143       */
 144      protected function validate_planid($value) {
 145          if (!plan::record_exists($value)) {
 146              return new lang_string('invaliddata', 'error');
 147          }
 148          return true;
 149      }
 150  
 151      /**
 152       * Hook to execute after delete.
 153       *
 154       * @param bool $result Whether or not the delete was successful.
 155       * @return void
 156       */
 157      protected function after_delete($result) {
 158          global $DB;
 159          if (!$result) {
 160              return;
 161          }
 162  
 163          $table = '{' . self::TABLE . '}';
 164          $sql = "UPDATE $table SET sortorder = sortorder -1  WHERE planid = ? AND sortorder > ?";
 165          $DB->execute($sql, array($this->get('planid'), $this->get('sortorder')));
 166      }
 167  
 168      /**
 169       * Check if plan competency has records for competencies.
 170       *
 171       * @param array $competencyids The competences IDs
 172       * @return boolean
 173       */
 174      public static function has_records_for_competencies($competencyids) {
 175          global $DB;
 176          list($insql, $params) = $DB->get_in_or_equal($competencyids, SQL_PARAMS_NAMED);
 177          return self::record_exists_select("competencyid $insql", $params);
 178      }
 179  
 180  }