Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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   * User evidence competency persistent.
  19   *
  20   * This represent the many to many relationship between evidence of prior
  21   * learning and competencies.
  22   *
  23   * @package    core_competency
  24   * @copyright  2015 Frédéric Massart - FMCorz.net
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  namespace core_competency;
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  use stdClass;
  31  use lang_string;
  32  
  33  /**
  34   * User evidence competency persistent class.
  35   *
  36   * @package    core_competency
  37   * @copyright  2015 Frédéric Massart - FMCorz.net
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class user_evidence_competency extends persistent {
  41  
  42      const TABLE = 'competency_userevidencecomp';
  43  
  44      /**
  45       * Return the definition of the properties of this model.
  46       *
  47       * @return array
  48       */
  49      protected static function define_properties() {
  50          return array(
  51              'userevidenceid' => array(
  52                  'type' => PARAM_INT
  53              ),
  54              'competencyid' => array(
  55                  'type' => PARAM_INT,
  56              ),
  57          );
  58      }
  59  
  60      /**
  61       * Validate competency ID.
  62       *
  63       * @param  int $value ID.
  64       * @return true|lang_string
  65       */
  66      protected function validate_competencyid($value) {
  67          if (!competency::record_exists($value)) {
  68              return new lang_string('invaliddata', 'error');
  69          }
  70          return true;
  71      }
  72  
  73      /**
  74       * Validate user evidence ID.
  75       *
  76       * @param  int $value ID.
  77       * @return true|lang_string
  78       */
  79      protected function validate_userevidenceid($value) {
  80          if (!user_evidence::record_exists($value)) {
  81              return new lang_string('invaliddata', 'error');
  82          }
  83          return true;
  84      }
  85  
  86      /**
  87       * Get competencies by user evidence ID.
  88       *
  89       * @param  int $userevidenceid The user evidence ID.
  90       * @return competency[]
  91       */
  92      public static function get_competencies_by_userevidenceid($userevidenceid) {
  93          global $DB;
  94          $sql = "SELECT c.*
  95                    FROM {" . self::TABLE . "} uec
  96                    JOIN {" . competency::TABLE . "} c
  97                      ON uec.userevidenceid = ?
  98                     AND uec.competencyid = c.id
  99                ORDER BY c.shortname";
 100          $competencies = array();
 101          $records = $DB->get_recordset_sql($sql, array($userevidenceid));
 102          foreach ($records as $record) {
 103              $competencies[] = new competency(0, $record);
 104          }
 105          $records->close();
 106          return $competencies;
 107      }
 108  
 109      /**
 110       * Get user competencies by user evidence ID.
 111       *
 112       * @param  int $userevidenceid The user evidence ID.
 113       * @return user_competency[]
 114       */
 115      public static function get_user_competencies_by_userevidenceid($userevidenceid) {
 116          global $DB;
 117  
 118          $sql = "SELECT uc.*
 119                    FROM {" . user_competency::TABLE . "} uc
 120                    JOIN {" . self::TABLE . "} uec
 121                      ON uc.competencyid = uec.competencyid
 122                    JOIN {" . user_evidence::TABLE . "} ue
 123                      ON uec.userevidenceid = ue.id
 124                     AND uc.userid = ue.userid
 125                     AND ue.id = ?
 126                ORDER BY uc.id ASC";
 127  
 128          $usercompetencies = array();
 129          $records = $DB->get_recordset_sql($sql, array($userevidenceid));
 130          foreach ($records as $record) {
 131              $usercompetencies[] = new user_competency(0, $record);
 132          }
 133          $records->close();
 134          return $usercompetencies;
 135      }
 136  
 137      /**
 138       * Get a relation.
 139       *
 140       * This does not perform any validation on the data passed. If the relation exists in the database
 141       * then it is loaded in a the model, if not then it is up to the developer to save the model.
 142       *
 143       * @param int $userevidenceid
 144       * @param int $competencyid
 145       * @return template_cohort
 146       */
 147      public static function get_relation($userevidenceid, $competencyid) {
 148          global $DB;
 149  
 150          $params = array(
 151              'userevidenceid' => $userevidenceid,
 152              'competencyid' => $competencyid
 153          );
 154  
 155          $relation = new static(null, (object) $params);
 156          if ($record = $DB->get_record(static::TABLE, $params)) {
 157              $relation->from_record($record);
 158          }
 159  
 160          return $relation;
 161      }
 162  
 163      /**
 164       * Delete evidences using competencies.
 165       *
 166       * @param array $competencyids Array of competencies ids.
 167       * @return bool Return true if the delete was successful.
 168       */
 169      public static function delete_by_competencyids($competencyids) {
 170          global $DB;
 171          if (empty($competencyids)) {
 172              return true;
 173          }
 174          list($insql, $params) = $DB->get_in_or_equal($competencyids);
 175          return $DB->delete_records_select(self::TABLE, "competencyid $insql", $params);
 176      }
 177  
 178  }