Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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 loading/storing competencies from the DB.
  19   *
  20   * @package    core_competency
  21   * @copyright  2015 Damyon Wiese
  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 stdClass;
  28  use lang_string;
  29  
  30  /**
  31   * Class for loading/storing course_module_competencies from the DB.
  32   *
  33   * @copyright  2015 Damyon Wiese
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class course_module_competency extends persistent {
  37  
  38      const TABLE = 'competency_modulecomp';
  39  
  40      /** Course competency ruleoutcome constant. */
  41      const OUTCOME_NONE = 0;
  42      /** Course competency ruleoutcome constant. */
  43      const OUTCOME_EVIDENCE = 1;
  44      /** Course competency ruleoutcome constant. */
  45      const OUTCOME_RECOMMEND = 2;
  46      /** Course competency ruleoutcome constant. */
  47      const OUTCOME_COMPLETE = 3;
  48  
  49      /**
  50       * Return the definition of the properties of this model.
  51       *
  52       * @return array
  53       */
  54      protected static function define_properties() {
  55          return array(
  56              'cmid' => array(
  57                  'type' => PARAM_INT
  58              ),
  59              'competencyid' => array(
  60                  'type' => PARAM_INT
  61              ),
  62              'sortorder' => array(
  63                  'type' => PARAM_INT
  64              ),
  65              'ruleoutcome' => array(
  66                  'choices' => array(self::OUTCOME_NONE,
  67                      self::OUTCOME_EVIDENCE,
  68                      self::OUTCOME_RECOMMEND,
  69                      self::OUTCOME_COMPLETE
  70                  ),
  71                  'default' => self::OUTCOME_EVIDENCE,
  72                  'type' => PARAM_INT,
  73              ),
  74          );
  75      }
  76  
  77      /**
  78       * Hook to execute before validate.
  79       *
  80       * @return void
  81       */
  82      protected function before_validate() {
  83          if (($this->get('id') && $this->get('sortorder') === null) || !$this->get('id')) {
  84              $this->set('sortorder', $this->count_records(array('cmid' => $this->get('cmid'))));
  85          }
  86      }
  87  
  88      /**
  89       * Return a list of rules.
  90       *
  91       * @return array Indexed by outcome value.
  92       */
  93      public static function get_ruleoutcome_list() {
  94          static $list = null;
  95  
  96          if ($list === null) {
  97              $list = array(
  98                  self::OUTCOME_NONE => self::get_ruleoutcome_name(self::OUTCOME_NONE),
  99                  self::OUTCOME_EVIDENCE => self::get_ruleoutcome_name(self::OUTCOME_EVIDENCE),
 100                  self::OUTCOME_RECOMMEND => self::get_ruleoutcome_name(self::OUTCOME_RECOMMEND),
 101                  self::OUTCOME_COMPLETE => self::get_ruleoutcome_name(self::OUTCOME_COMPLETE));
 102          }
 103  
 104          return $list;
 105      }
 106  
 107      /**
 108       * Human readable rule name.
 109       *
 110       * @param int $ruleoutcome The value of ruleoutcome.
 111       * @return lang_string
 112       */
 113      public static function get_ruleoutcome_name($ruleoutcome) {
 114  
 115          switch ($ruleoutcome) {
 116              case self::OUTCOME_NONE:
 117                  $strname = 'none';
 118                  break;
 119              case self::OUTCOME_EVIDENCE:
 120                  $strname = 'evidence';
 121                  break;
 122              case self::OUTCOME_RECOMMEND:
 123                  $strname = 'recommend';
 124                  break;
 125              case self::OUTCOME_COMPLETE:
 126                  $strname = 'complete';
 127                  break;
 128              default:
 129                  throw new \moodle_exception('errorcompetencyrule', 'core_competency', '', $ruleoutcome);
 130                  break;
 131          }
 132  
 133          return new lang_string('coursemodulecompetencyoutcome_' . $strname, 'core_competency');
 134      }
 135  
 136      /**
 137       * Validate cmid ID.
 138       *
 139       * @param int $data The CM ID.
 140       * @return true|lang_string
 141       */
 142      protected function validate_cmid($data) {
 143          global $DB;
 144          if (!$DB->record_exists('course_modules', array('id' => $data))) {
 145              return new lang_string('invalidmodule', 'error');
 146          }
 147          return true;
 148      }
 149  
 150      /**
 151       * Validate competency ID.
 152       *
 153       * @param int $data The competency ID.
 154       * @return true|lang_string
 155       */
 156      protected function validate_competencyid($data) {
 157          if (!competency::record_exists($data)) {
 158              return new lang_string('invaliddata', 'error');
 159          }
 160          return true;
 161      }
 162  
 163      /**
 164       * Return the module IDs and visible flags that include this competency in a single course.
 165       *
 166       * @param int $competencyid The competency id
 167       * @param int $courseid The course ID.
 168       * @return array of ints (cmids)
 169       */
 170      public static function list_course_modules($competencyid, $courseid) {
 171          global $DB;
 172  
 173          $results = $DB->get_records_sql('SELECT coursemodules.id as id
 174                                             FROM {' . self::TABLE . '} modcomp
 175                                             JOIN {course_modules} coursemodules
 176                                               ON modcomp.cmid = coursemodules.id
 177                                            WHERE modcomp.competencyid = ? AND coursemodules.course = ?',
 178                                            array($competencyid, $courseid));
 179  
 180          return array_keys($results);
 181      }
 182  
 183      /**
 184       * Count the competencies in this course module.
 185       *
 186       * @param int $cmid The course module id.
 187       * @return int
 188       */
 189      public static function count_competencies($cmid) {
 190          global $DB;
 191  
 192          $sql = 'SELECT COUNT(comp.id)
 193                    FROM {' . self::TABLE . '} coursemodulecomp
 194                    JOIN {' . competency::TABLE . '} comp
 195                      ON coursemodulecomp.competencyid = comp.id
 196                   WHERE coursemodulecomp.cmid = ? ';
 197          $params = array($cmid);
 198  
 199          $results = $DB->count_records_sql($sql, $params);
 200  
 201          return $results;
 202      }
 203  
 204      /**
 205       * List the competencies in this course module.
 206       *
 207       * @param int $cmid The course module id
 208       * @return competency[] Indexed by competency ID.
 209       */
 210      public static function list_competencies($cmid) {
 211          global $DB;
 212  
 213          $sql = 'SELECT comp.*
 214                    FROM {' . competency::TABLE . '} comp
 215                    JOIN {' . self::TABLE . '} coursemodulecomp
 216                      ON coursemodulecomp.competencyid = comp.id
 217                   WHERE coursemodulecomp.cmid = ?
 218                   ORDER BY coursemodulecomp.sortorder ASC';
 219          $params = array($cmid);
 220  
 221          $results = $DB->get_recordset_sql($sql, $params);
 222          $instances = array();
 223          foreach ($results as $result) {
 224              $comp = new competency(0, $result);
 225              $instances[$comp->get('id')] = $comp;
 226          }
 227          $results->close();
 228  
 229          return $instances;
 230      }
 231  
 232      /**
 233       * Get a single competency from the course module (only if it is really in the course module).
 234       *
 235       * @param int $cmid The course module id
 236       * @param int $competencyid The competency id
 237       * @return competency
 238       */
 239      public static function get_competency($cmid, $competencyid) {
 240          global $DB;
 241  
 242          $sql = 'SELECT comp.*
 243                    FROM {' . competency::TABLE . '} comp
 244                    JOIN {' . self::TABLE . '} crsmodcomp
 245                      ON crsmodcomp.competencyid = comp.id
 246                   WHERE crsmodcomp.cmid = ? AND crsmodcomp.competencyid = ?';
 247          $params = array($cmid, $competencyid);
 248  
 249          $result = $DB->get_record_sql($sql, $params);
 250          if (!$result) {
 251              throw new \coding_exception('The competency does not belong to this course module: ' . $competencyid . ', ' . $cmid);
 252          }
 253  
 254          return new competency(0, $result);
 255      }
 256  
 257      /**
 258       * Hook to execute after delete.
 259       *
 260       * @param bool $result Whether or not the delete was successful.
 261       * @return void
 262       */
 263      protected function after_delete($result) {
 264          global $DB;
 265          if (!$result) {
 266              return;
 267          }
 268  
 269          $table = '{' . self::TABLE . '}';
 270          $sql = "UPDATE $table SET sortorder = sortorder -1 WHERE cmid = ? AND sortorder > ?";
 271          $DB->execute($sql, array($this->get('cmid'), $this->get('sortorder')));
 272      }
 273  
 274      /**
 275       * List the course_module_competencies in this course module.
 276       *
 277       * @param int $cmid The course module id
 278       * @return course_module_competency[]
 279       */
 280      public static function list_course_module_competencies($cmid) {
 281          global $DB;
 282  
 283          $sql = 'SELECT coursemodcomp.*
 284                    FROM {' . self::TABLE . '} coursemodcomp
 285                    JOIN {' . competency::TABLE . '} comp
 286                      ON coursemodcomp.competencyid = comp.id
 287                   WHERE coursemodcomp.cmid = ?
 288                   ORDER BY coursemodcomp.sortorder ASC';
 289          $params = array($cmid);
 290  
 291          $results = $DB->get_recordset_sql($sql, $params);
 292          $instances = array();
 293          foreach ($results as $result) {
 294              array_push($instances, new course_module_competency(0, $result));
 295          }
 296          $results->close();
 297  
 298          return $instances;
 299      }
 300  
 301      /**
 302       * List the relationship objects for a competency in a course.
 303       *
 304       * @param int $competencyid The competency ID.
 305       * @param int $courseid The course ID.
 306       * @return course_module_competency[]
 307       */
 308      public static function get_records_by_competencyid_in_course($competencyid, $courseid) {
 309          global $DB;
 310  
 311          $sql = 'SELECT cmc.*
 312                    FROM {' . self::TABLE . '} cmc
 313                    JOIN {course_modules} cm
 314                      ON cm.course = ?
 315                     AND cmc.cmid = cm.id
 316                   WHERE cmc.competencyid = ?
 317                ORDER BY cmc.sortorder ASC';
 318          $params = array($courseid, $competencyid);
 319  
 320          $results = $DB->get_recordset_sql($sql, $params);
 321          $instances = array();
 322          foreach ($results as $result) {
 323              $instances[$result->id] = new course_module_competency(0, $result);
 324          }
 325          $results->close();
 326  
 327          return $instances;
 328      }
 329  
 330  }