MDL-70424 auth: Avoid random changes to $CFG->auth
[moodle.git] / competency / classes / user_competency.php
blob0f99de9af83a56f3dac4d365b245b113a1f68ea0
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 * Class for user_competency persistence.
20 * @package core_competency
21 * @copyright 2015 Serge Gauthier
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace core_competency;
25 defined('MOODLE_INTERNAL') || die();
27 use coding_exception;
28 use context_course;
29 use context_user;
30 use comment;
31 use lang_string;
33 /**
34 * Class for loading/storing user_competency from the DB.
36 * @copyright 2015 Serge Gauthier
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class user_competency extends persistent {
41 /** Table name for user_competency persistency */
42 const TABLE = 'competency_usercomp';
44 /** Idle status */
45 const STATUS_IDLE = 0;
47 /** Waiting for review status */
48 const STATUS_WAITING_FOR_REVIEW = 1;
50 /** In review status */
51 const STATUS_IN_REVIEW = 2;
53 /**
54 * Return the definition of the properties of this model.
56 * @return array
58 protected static function define_properties() {
59 return array(
60 'userid' => array(
61 'type' => PARAM_INT,
63 'competencyid' => array(
64 'type' => PARAM_INT,
66 'status' => array(
67 'choices' => array(
68 self::STATUS_IDLE,
69 self::STATUS_WAITING_FOR_REVIEW,
70 self::STATUS_IN_REVIEW,
72 'type' => PARAM_INT,
73 'default' => self::STATUS_IDLE,
75 'reviewerid' => array(
76 'type' => PARAM_INT,
77 'default' => null,
78 'null' => NULL_ALLOWED,
80 'proficiency' => array(
81 'type' => PARAM_BOOL,
82 'default' => null,
83 'null' => NULL_ALLOWED,
85 'grade' => array(
86 'type' => PARAM_INT,
87 'default' => null,
88 'null' => NULL_ALLOWED,
93 /**
94 * Whether the current user can comment on this user competency.
96 * @return bool
98 public function can_comment() {
99 return static::can_comment_user($this->get('userid'));
103 * Whether the current user can read this user competency.
105 * @return bool
107 public function can_read() {
108 return static::can_read_user($this->get('userid'));
112 * Whether the current user can read comments on this user competency.
114 * @return bool
116 public function can_read_comments() {
117 return static::can_read_comments_user($this->get('userid'));
121 * Can the current user send the user competency for review?
123 * @return bool
125 public function can_request_review() {
126 return static::can_request_review_user($this->get('userid'));
130 * Can the current user review the user competency?
132 * @return bool
134 public function can_review() {
135 return static::can_review_user($this->get('userid'));
139 * Human readable status name.
141 * @param int $status The status code.
142 * @return lang_string
144 public static function get_status_name($status) {
146 switch ($status) {
147 case self::STATUS_IDLE:
148 $strname = 'idle';
149 break;
150 case self::STATUS_WAITING_FOR_REVIEW:
151 $strname = 'waitingforreview';
152 break;
153 case self::STATUS_IN_REVIEW:
154 $strname = 'inreview';
155 break;
156 default:
157 throw new \moodle_exception('errorusercomptencystatus', 'core_competency', '', $status);
158 break;
161 return new lang_string('usercompetencystatus_' . $strname, 'core_competency');
165 * Get list of competency status.
167 * @return array
169 public static function get_status_list() {
171 static $list = null;
173 if ($list === null) {
174 $list = array(
175 self::STATUS_IDLE => self::get_status_name(self::STATUS_IDLE),
176 self::STATUS_WAITING_FOR_REVIEW => self::get_status_name(self::STATUS_WAITING_FOR_REVIEW),
177 self::STATUS_IN_REVIEW => self::get_status_name(self::STATUS_IN_REVIEW));
180 return $list;
184 * Get the comment object.
186 * @return comment
188 public function get_comment_object() {
189 global $CFG;
190 require_once($CFG->dirroot . '/comment/lib.php');
192 if (!$this->get('id')) {
193 throw new coding_exception('The user competency record must exist.');
196 $comment = new comment((object) array(
197 'context' => $this->get_context(),
198 'component' => 'competency', // This cannot be named 'core_competency'.
199 'itemid' => $this->get('id'),
200 'area' => 'user_competency',
201 'showcount' => true,
203 $comment->set_fullwidth(true);
204 return $comment;
208 * Return the competency Object.
210 * @return competency Competency Object
212 public function get_competency() {
213 return new competency($this->get('competencyid'));
217 * Get the context.
219 * @return context The context.
221 public function get_context() {
222 return context_user::instance($this->get('userid'));
226 * Find the plans for the user and this competency.
228 * Note that this:
229 * - does not perform any capability check.
230 * - may return completed plans.
231 * - may return an empty array.
233 * @return plans[]
235 public function get_plans() {
236 return plan::get_by_user_and_competency($this->get('userid'), $this->get('competencyid'));
240 * Validate the user ID.
242 * @param int $value The value.
243 * @return true|lang_string
245 protected function validate_userid($value) {
246 global $DB;
248 if (!$DB->record_exists('user', array('id' => $value))) {
249 return new lang_string('invaliduserid', 'error');
252 return true;
256 * Validate the competency ID.
258 * @param int $value The value.
259 * @return true|lang_string
261 protected function validate_competencyid($value) {
262 if (!competency::record_exists($value)) {
263 return new lang_string('errornocompetency', 'core_competency', $value);
266 return true;
270 * Validate the proficiency.
272 * @param int $value The value.
273 * @return true|lang_string
275 protected function validate_proficiency($value) {
276 $grade = $this->get('grade');
278 if ($grade !== null && $value === null) {
279 // We must set a proficiency when we set a grade.
280 return new lang_string('invaliddata', 'error');
282 } else if ($grade === null && $value !== null) {
283 // We must not set a proficiency when we don't set a grade.
284 return new lang_string('invaliddata', 'error');
287 return true;
291 * Validate the reviewer ID.
293 * @param int $value The value.
294 * @return true|lang_string
296 protected function validate_reviewerid($value) {
297 global $DB;
299 if ($value !== null && !$DB->record_exists('user', array('id' => $value))) {
300 return new lang_string('invaliduserid', 'error');
303 return true;
307 * Validate the grade.
309 * @param int $value The value.
310 * @return true|lang_string
312 protected function validate_grade($value) {
313 if ($value !== null) {
314 if ($value <= 0) {
315 return new lang_string('invalidgrade', 'core_competency');
318 // TODO MDL-52243 Use a core method to validate the grade_scale item.
319 // Check if grade exist in the scale item values.
320 $competency = $this->get_competency();
321 if (!array_key_exists($value - 1 , $competency->get_scale()->scale_items)) {
322 return new lang_string('invalidgrade', 'core_competency');
326 return true;
330 * Can the current user comment on a user's competency?
332 * @param int $userid The user ID the competency belongs to.
333 * @return bool
335 public static function can_comment_user($userid) {
336 global $USER;
338 $capabilities = array('moodle/competency:usercompetencycomment');
339 if ($USER->id == $userid) {
340 $capabilities[] = 'moodle/competency:usercompetencycommentown';
343 if (has_any_capability($capabilities, context_user::instance($userid))) {
344 return true;
347 return false;
351 * Can the current user grade a user's user competency?
353 * @param int $userid The user ID the competency belongs to.
354 * @return bool
356 public static function can_grade_user($userid) {
357 $ratecap = 'moodle/competency:competencygrade';
358 return has_capability($ratecap, context_user::instance($userid));
362 * Can the current user grade a user's user competency in a course?
364 * @param int $userid The user ID the competency belongs to.
365 * @param int $courseid The course ID.
366 * @return bool
368 public static function can_grade_user_in_course($userid, $courseid) {
369 $ratecap = 'moodle/competency:competencygrade';
370 return has_capability($ratecap, context_course::instance($courseid))
371 || static::can_grade_user($userid);
375 * Can the current user read the comments on a user's competency?
377 * @param int $userid The user ID the competency belongs to.
378 * @return bool
380 public static function can_read_comments_user($userid) {
381 // Everyone who can read the user competency can read the comments.
382 return static::can_read_user($userid);
386 * Can the current user read the user competencies of a user in a course?
388 * @param int $userid The user ID the competency belongs to.
389 * @param int $courseid The course ID.
390 * @return bool
392 public static function can_read_user_in_course($userid, $courseid) {
393 $capability = 'moodle/competency:usercompetencyview';
394 return has_capability($capability, context_course::instance($courseid))
395 || static::can_read_user($userid);
399 * Can the current user read a user's competency?
401 * @param int $userid The user ID the competency belongs to.
402 * @return bool
404 public static function can_read_user($userid) {
405 $capability = 'moodle/competency:usercompetencyview';
406 return has_capability($capability, context_user::instance($userid))
407 || plan::can_read_user($userid);
411 * Can the current user send a user's competency for review?
413 * Note that the status 'review' is not meant to be used for student to self-assess
414 * themselves, then to ask the teacher to review their assessment. It is more intended
415 * for a student to provide evidence of prior learning and request their review.
417 * @param int $userid The user ID the competency belongs to.
418 * @return bool
420 public static function can_request_review_user($userid) {
421 global $USER;
423 $capabilities = array('moodle/competency:usercompetencyrequestreview');
424 if ($USER->id == $userid) {
425 $capabilities[] = 'moodle/competency:usercompetencyrequestreviewown';
428 if (has_any_capability($capabilities, context_user::instance($userid))) {
429 return true;
432 return false;
436 * Can the current user review the user competency?
438 * @param int $userid The user ID the competency belongs to.
439 * @return bool
441 public static function can_review_user($userid) {
442 $capability = 'moodle/competency:usercompetencyreview';
443 return has_capability($capability, context_user::instance($userid));
447 * Create a new user_competency object.
449 * Note, this is intended to be used to create a blank relation, for instance when
450 * the record was not found in the database. This does not save the model.
452 * @param int $userid The user ID.
453 * @param int $competencyid The competency ID.
454 * @return \core_competency\user_competency
456 public static function create_relation($userid, $competencyid) {
457 $relation = new user_competency(0, (object) array('userid' => $userid, 'competencyid' => $competencyid));
458 return $relation;
462 * Fetch a competency by user competency ID.
464 * This is a convenience method to attempt to efficiently fetch a competency when
465 * the only information we have is the user_competency ID, in evidence for instance.
467 * @param int $id The user competency ID.
468 * @return competency
470 public static function get_competency_by_usercompetencyid($id) {
471 global $DB;
472 $sql = "SELECT c.*
473 FROM {" . self::TABLE . "} uc
474 JOIN {" . competency::TABLE . "} c
475 ON c.id = uc.competencyid
476 WHERE uc.id = ?";
477 $record = $DB->get_record_sql($sql, array($id), MUST_EXIST);
478 return new competency(0, $record);
482 * Get multiple user_competency for a user.
484 * @param int $userid
485 * @param array $competenciesorids Limit search to those competencies, or competency IDs.
486 * @return \core_competency\user_competency[]
488 public static function get_multiple($userid, array $competenciesorids = null) {
489 global $DB;
491 $params = array();
492 $params['userid'] = $userid;
493 $sql = '1 = 1';
495 if (!empty($competenciesorids)) {
496 $test = reset($competenciesorids);
497 if (is_number($test)) {
498 $ids = $competenciesorids;
499 } else {
500 $ids = array();
501 foreach ($competenciesorids as $comp) {
502 $ids[] = $comp->get('id');
506 list($insql, $inparams) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED);
507 $params += $inparams;
508 $sql = "competencyid $insql";
511 // Order by ID to prevent random ordering.
512 return self::get_records_select("userid = :userid AND $sql", $params, 'id ASC');
516 * Checks if a competency has user competency records.
518 * @param int $competencyid The competency ID
519 * @return boolean
521 public static function has_records_for_competency($competencyid) {
522 return self::record_exists_select('competencyid = ?', array($competencyid));
526 * Checks if any of the competencies of a framework has a user competency record.
528 * @param int $frameworkid The competency framework ID.
529 * @return boolean
531 public static function has_records_for_framework($frameworkid) {
532 global $DB;
534 $sql = "SELECT 'x'
535 FROM {" . self::TABLE . "} uc
536 JOIN {" . competency::TABLE . "} c
537 ON uc.competencyid = c.id
538 WHERE c.competencyframeworkid = ?";
539 $params = array($frameworkid);
541 return $DB->record_exists_sql($sql, $params);
545 * Check if user competency has records for competencies.
547 * @param array $competencyids The competencies ids.
548 * @return boolean Return true if the delete was successfull.
550 public static function has_records_for_competencies($competencyids) {
551 global $DB;
552 list($insql, $params) = $DB->get_in_or_equal($competencyids, SQL_PARAMS_NAMED);
553 return self::record_exists_select("competencyid $insql", $params);