Merge branch 'MDL-69688' of https://github.com/stronk7/moodle
[moodle.git] / competency / classes / user_evidence.php
blobd70084b7ec1069d8478f0ca49f7f174e8ecf3cf8
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.
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/>.
17 /**
18 * User evidence persistent.
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
25 namespace core_competency;
26 defined('MOODLE_INTERNAL') || die();
28 use context_user;
29 use lang_string;
31 /**
32 * User evidence persistent class.
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
38 class user_evidence extends persistent {
40 const TABLE = 'competency_userevidence';
42 /**
43 * Return the definition of the properties of this model.
45 * @return array
47 protected static function define_properties() {
48 return array(
49 'userid' => array(
50 'type' => PARAM_INT
52 'name' => array(
53 'type' => PARAM_TEXT
55 'description' => array(
56 'type' => PARAM_CLEANHTML,
57 'default' => '',
59 'descriptionformat' => array(
60 'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
61 'type' => PARAM_INT,
62 'default' => FORMAT_HTML,
64 'url' => array(
65 'type' => PARAM_URL,
66 'default' => '',
67 'message' => new lang_string('invalidurl', 'core_competency')
72 /**
73 * Can the current user manage this user evidence?
75 * @return bool
77 public function can_manage() {
78 return self::can_manage_user($this->get('userid'));
81 /**
82 * Can the current user view this user evidence?
84 * @return bool
86 public function can_read() {
87 return self::can_read_user($this->get('userid'));
90 /**
91 * Get the context of this user evidence.
93 * @return context
95 public function get_context() {
96 return context_user::instance($this->get('userid'));
99 /**
100 * Get link competencies.
102 public function get_competencies() {
103 return user_evidence_competency::get_competencies_by_userevidenceid($this->get('id'));
107 * Get link user competencies.
109 public function get_user_competencies() {
110 return user_evidence_competency::get_user_competencies_by_userevidenceid($this->get('id'));
114 * Return true if the user of the evidence has plan.
116 * @return bool
118 public function user_has_plan() {
119 return plan::record_exists_select('userid = ?', array($this->get('userid')));
123 * Return the files associated with this evidence.
125 * @return object[]
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;
135 * Validate the URL.
137 * @param int $value
138 * @return true|lang_string
140 protected function validate_url($value) {
141 if (empty($value) && !is_numeric($value)) {
142 return true;
144 if (!preg_match('@^https?://.+@', $value)) {
145 return new lang_string('invalidurl', 'core_competency');
147 return true;
151 * Validate the user ID.
153 * @param int $value
154 * @return true|lang_string
156 protected function validate_userid($value) {
157 global $DB;
159 // During create.
160 if (!$this->get('id')) {
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');
170 return true;
174 * Can the current user manage a user's evidence?
176 * @param int $evidenceuserid The user to whom the evidence would belong.
177 * @return bool
179 public static function can_manage_user($evidenceuserid) {
180 global $USER;
181 $context = context_user::instance($evidenceuserid);
183 $capabilities = array('moodle/competency:userevidencemanage');
184 if ($context->instanceid == $USER->id) {
185 $capabilities[] = 'moodle/competency:userevidencemanageown';
188 return has_any_capability($capabilities, $context);
192 * Can the current user view a user's evidence?
194 * @param int $evidenceuserid The user to whom the evidence would belong.
195 * @return bool
197 public static function can_read_user($evidenceuserid) {
198 $context = context_user::instance($evidenceuserid);
200 $capabilities = array('moodle/competency:userevidenceview');
202 return has_any_capability($capabilities, $context) || self::can_manage_user($evidenceuserid);