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 * 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();
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';
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;
54 * Return the definition of the properties of this model.
58 protected static function define_properties() {
63 'competencyid' => array(
69 self
::STATUS_WAITING_FOR_REVIEW
,
70 self
::STATUS_IN_REVIEW
,
73 'default' => self
::STATUS_IDLE
,
75 'reviewerid' => array(
78 'null' => NULL_ALLOWED
,
80 'proficiency' => array(
83 'null' => NULL_ALLOWED
,
88 'null' => NULL_ALLOWED
,
94 * Whether the current user can comment on this user competency.
98 public function can_comment() {
99 return static::can_comment_user($this->get('userid'));
103 * Whether the current user can read this user competency.
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.
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?
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?
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) {
147 case self
::STATUS_IDLE
:
150 case self
::STATUS_WAITING_FOR_REVIEW
:
151 $strname = 'waitingforreview';
153 case self
::STATUS_IN_REVIEW
:
154 $strname = 'inreview';
157 throw new \
moodle_exception('errorusercomptencystatus', 'core_competency', '', $status);
161 return new lang_string('usercompetencystatus_' . $strname, 'core_competency');
165 * Get list of competency status.
169 public static function get_status_list() {
173 if ($list === null) {
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
));
184 * Get the comment object.
188 public function get_comment_object() {
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',
203 $comment->set_fullwidth(true);
208 * Return the competency Object.
210 * @return competency Competency Object
212 public function get_competency() {
213 return new competency($this->get('competencyid'));
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.
229 * - does not perform any capability check.
230 * - may return completed plans.
231 * - may return an empty array.
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) {
248 if (!$DB->record_exists('user', array('id' => $value))) {
249 return new lang_string('invaliduserid', 'error');
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);
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');
291 * Validate the reviewer ID.
293 * @param int $value The value.
294 * @return true|lang_string
296 protected function validate_reviewerid($value) {
299 if ($value !== null && !$DB->record_exists('user', array('id' => $value))) {
300 return new lang_string('invaliduserid', 'error');
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) {
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');
330 * Can the current user comment on a user's competency?
332 * @param int $userid The user ID the competency belongs to.
335 public static function can_comment_user($userid) {
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))) {
351 * Can the current user grade a user's user competency?
353 * @param int $userid The user ID the competency belongs to.
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.
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.
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.
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.
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.
420 public static function can_request_review_user($userid) {
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))) {
436 * Can the current user review the user competency?
438 * @param int $userid The user ID the competency belongs to.
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));
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.
470 public static function get_competency_by_usercompetencyid($id) {
473 FROM {" . self
::TABLE
. "} uc
474 JOIN {" . competency
::TABLE
. "} c
475 ON c.id = uc.competencyid
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.
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) {
492 $params['userid'] = $userid;
495 if (!empty($competenciesorids)) {
496 $test = reset($competenciesorids);
497 if (is_number($test)) {
498 $ids = $competenciesorids;
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
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.
531 public static function has_records_for_framework($frameworkid) {
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) {
552 list($insql, $params) = $DB->get_in_or_equal($competencyids, SQL_PARAMS_NAMED
);
553 return self
::record_exists_select("competencyid $insql", $params);