2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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();
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';
43 * Return the definition of the properties of this model.
47 protected static function define_properties() {
55 'description' => array(
56 'type' => PARAM_CLEANHTML
,
59 'descriptionformat' => array(
60 'choices' => array(FORMAT_HTML
, FORMAT_MOODLE
, FORMAT_PLAIN
, FORMAT_MARKDOWN
),
62 'default' => FORMAT_HTML
,
67 'message' => new lang_string('invalidurl', 'core_competency')
73 * Can the current user manage this user evidence?
77 public function can_manage() {
78 return self
::can_manage_user($this->get('userid'));
82 * Can the current user view this user evidence?
86 public function can_read() {
87 return self
::can_read_user($this->get('userid'));
91 * Get the context of this user evidence.
95 public function get_context() {
96 return context_user
::instance($this->get('userid'));
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.
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.
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'),
138 * @return true|lang_string
140 protected function validate_url($value) {
141 if (empty($value) && !is_numeric($value)) {
144 if (!preg_match('@^https?://.+@', $value)) {
145 return new lang_string('invalidurl', 'core_competency');
151 * Validate the user ID.
154 * @return true|lang_string
156 protected function validate_userid($value) {
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');
174 * Can the current user manage a user's evidence?
176 * @param int $evidenceuserid The user to whom the evidence would belong.
179 public static function can_manage_user($evidenceuserid) {
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.
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);