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   * Class for course_competency_settings persistence.
  19   *
  20   * @package    core_competency
  21   * @copyright  2016 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core_competency;
  25  
  26  use lang_string;
  27  use context_course;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  /**
  32   * Class for course_competency_settings persistence.
  33   *
  34   * @copyright  2016 Damyon Wiese
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class course_competency_settings extends persistent {
  38  
  39      /** Table name for plan_competency persistency */
  40      const TABLE = 'competency_coursecompsetting';
  41  
  42      /**
  43       * Return the definition of the properties of this model.
  44       *
  45       * @return array
  46       */
  47      protected static function define_properties() {
  48          return array(
  49              'courseid' => array(
  50                  'type' => PARAM_INT,
  51              ),
  52              'pushratingstouserplans' => array(
  53                  'type' => PARAM_BOOL,
  54                  'default' => function() {
  55                      return get_config('core_competency', 'pushcourseratingstouserplans');
  56                  }
  57              ),
  58          );
  59      }
  60  
  61      /**
  62       * Get a the course settings for a single course.
  63       *
  64       * @param int $courseid The course id
  65       * @return course_competency_settings
  66       */
  67      public static function get_by_courseid($courseid) {
  68          global $DB;
  69  
  70          $params = array(
  71              'courseid' => $courseid
  72          );
  73  
  74          $settings = new static(null, (object) $params);
  75          if ($record = $DB->get_record(self::TABLE, $params)) {
  76              $settings->from_record($record);
  77          }
  78  
  79          return $settings;
  80      }
  81  
  82      /**
  83       * Can the current user view competency settings for this course.
  84       *
  85       * @param int $courseid The course ID.
  86       * @return bool
  87       */
  88      public static function can_read($courseid) {
  89          $context = context_course::instance($courseid);
  90  
  91          $capabilities = array('moodle/competency:coursecompetencyview', 'moodle/competency:coursecompetencymanage');
  92  
  93          return has_any_capability($capabilities, $context);
  94      }
  95  
  96      /**
  97       * Can the current user change competency settings for this course.
  98       *
  99       * @param int $courseid The course ID.
 100       * @return bool
 101       */
 102      public static function can_manage_course($courseid) {
 103          $context = context_course::instance($courseid);
 104  
 105          $capabilities = array('moodle/competency:coursecompetencyconfigure');
 106  
 107          return has_any_capability($capabilities, $context);
 108      }
 109  
 110      /**
 111       * Can the current user change competency settings for this course.
 112       *
 113       * @return bool
 114       */
 115      public function can_manage() {
 116          return static::can_manage_course($this->get('courseid'));
 117      }
 118  
 119      /**
 120       * Validate course ID.
 121       *
 122       * @param int $data The course ID.
 123       * @return true|lang_string
 124       */
 125      protected function validate_courseid($data) {
 126          global $DB;
 127          if (!$DB->record_exists('course', array('id' => $data))) {
 128              return new lang_string('invalidcourseid', 'error');
 129          }
 130          return true;
 131      }
 132  
 133      /**
 134       * Get the context.
 135       *
 136       * @return context The context
 137       */
 138      public function get_context() {
 139          return context_course::instance($this->get('courseid'));
 140      }
 141  }