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.
   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 persistent.
  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 context_user;
  29  use lang_string;
  30  
  31  /**
  32   * User evidence persistent class.
  33   *
  34   * @package    core_competency
  35   * @copyright  2015 Frédéric Massart - FMCorz.net
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class user_evidence extends persistent {
  39  
  40      const TABLE = 'competency_userevidence';
  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              'userid' => array(
  50                  'type' => PARAM_INT
  51              ),
  52              'name' => array(
  53                  'type' => PARAM_TEXT
  54              ),
  55              'description' => array(
  56                  'type' => PARAM_CLEANHTML,
  57                  'default' => '',
  58              ),
  59              'descriptionformat' => array(
  60                  'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
  61                  'type' => PARAM_INT,
  62                  'default' => FORMAT_HTML,
  63              ),
  64              'url' => array(
  65                  'type' => PARAM_URL,
  66                  'default' => '',
  67                  'message' => new lang_string('invalidurl', 'core_competency')
  68              )
  69          );
  70      }
  71  
  72      /**
  73       * Can the current user manage this user evidence?
  74       *
  75       * @return bool
  76       */
  77      public function can_manage() {
  78          return self::can_manage_user($this->get('userid'));
  79      }
  80  
  81      /**
  82       * Can the current user view this user evidence?
  83       *
  84       * @return bool
  85       */
  86      public function can_read() {
  87          return self::can_read_user($this->get('userid'));
  88      }
  89  
  90      /**
  91       * Get the context of this user evidence.
  92       *
  93       * @return context
  94       */
  95      public function get_context() {
  96          return context_user::instance($this->get('userid'));
  97      }
  98  
  99      /**
 100       * Get link competencies.
 101       */
 102      public function get_competencies() {
 103          return user_evidence_competency::get_competencies_by_userevidenceid($this->get('id'));
 104      }
 105  
 106      /**
 107       * Get link user competencies.
 108       */
 109      public function get_user_competencies() {
 110          return user_evidence_competency::get_user_competencies_by_userevidenceid($this->get('id'));
 111      }
 112  
 113      /**
 114       * Return true if the user of the evidence has plan.
 115       *
 116       * @return bool
 117       */
 118      public function user_has_plan() {
 119          return plan::record_exists_select('userid = ?', array($this->get('userid')));
 120      }
 121  
 122      /**
 123       * Return the files associated with this evidence.
 124       *
 125       * @return object[]
 126       */
 127      public function get_files() {
 128          $fs = get_file_storage();
 129          $files = $fs->get_area_files($this->get_context()->id, 'core_competency', 'userevidence', $this->get('id'),
 130              'filename', false);
 131          return $files;
 132      }
 133  
 134      /**
 135       * Validate the URL.
 136       *
 137       * @param  int $value
 138       * @return true|lang_string
 139       */
 140      protected function validate_url($value) {
 141          if (empty($value) && !is_numeric($value)) {
 142              return true;
 143          }
 144          if (!preg_match('@^https?://.+@', $value)) {
 145              return new lang_string('invalidurl', 'core_competency');
 146          }
 147          return true;
 148      }
 149  
 150      /**
 151       * Validate the user ID.
 152       *
 153       * @param  int $value
 154       * @return true|lang_string
 155       */
 156      protected function validate_userid($value) {
 157          global $DB;
 158  
 159          // During create.
 160          if (!$this->get('id')) {
 161  
 162              // Check that the user exists. We do not need to do that on update because
 163              // the userid of an evidence should never change.
 164              if (!$DB->record_exists('user', array('id' => $value))) {
 165                  return new lang_string('invaliddata', 'error');
 166              }
 167  
 168          }
 169  
 170          return true;
 171      }
 172  
 173      /**
 174       * Can the current user manage a user's evidence?
 175       *
 176       * @param  int $evidenceuserid The user to whom the evidence would belong.
 177       * @return bool
 178       */
 179      public static function can_manage_user($evidenceuserid) {
 180          global $USER;
 181          $context = context_user::instance($evidenceuserid);
 182  
 183          $capabilities = array('moodle/competency:userevidencemanage');
 184          if ($context->instanceid == $USER->id) {
 185              $capabilities[] = 'moodle/competency:userevidencemanageown';
 186          }
 187  
 188          return has_any_capability($capabilities, $context);
 189      }
 190  
 191      /**
 192       * Can the current user view a user's evidence?
 193       *
 194       * @param  int $evidenceuserid The user to whom the evidence would belong.
 195       * @return bool
 196       */
 197      public static function can_read_user($evidenceuserid) {
 198          $context = context_user::instance($evidenceuserid);
 199  
 200          $capabilities = array('moodle/competency:userevidenceview');
 201  
 202          return has_any_capability($capabilities, $context) || self::can_manage_user($evidenceuserid);
 203      }
 204  
 205  }