mirrored from git://git.moodle.org/moodle.git
/
plan.php
708 lines (621 loc) · 21 KB
/
plan.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Class for plans persistence.
*
* @package tool_lp
* @copyright 2015 David Monllao
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_lp;
defined('MOODLE_INTERNAL') || die();
use comment;
use context_user;
use dml_missing_record_exception;
use lang_string;
/**
* Class for loading/storing plans from the DB.
*
* @copyright 2015 David Monllao
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class plan extends persistent {
const TABLE = 'tool_lp_plan';
/** Draft status. */
const STATUS_DRAFT = 0;
/** Active status. */
const STATUS_ACTIVE = 1;
/** Complete status. */
const STATUS_COMPLETE = 2;
/** Waiting for review. */
const STATUS_WAITING_FOR_REVIEW = 3;
/** In review. */
const STATUS_IN_REVIEW = 4;
/** 10 minutes threshold **/
const DUEDATE_THRESHOLD = 600;
/** @var plan object before update. */
protected $beforeupdate = null;
/**
* Return the definition of the properties of this model.
*
* @return array
*/
protected static function define_properties() {
return array(
'name' => array(
'type' => PARAM_TEXT,
),
'description' => array(
'type' => PARAM_RAW,
'default' => ''
),
'descriptionformat' => array(
'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
'type' => PARAM_INT,
'default' => FORMAT_HTML,
),
'userid' => array(
'type' => PARAM_INT,
),
'templateid' => array(
'type' => PARAM_INT,
'default' => null,
'null' => NULL_ALLOWED,
),
'origtemplateid' => array(
'type' => PARAM_INT,
'default' => null,
'null' => NULL_ALLOWED,
),
'status' => array(
'choices' => array(self::STATUS_DRAFT, self::STATUS_COMPLETE, self::STATUS_ACTIVE,
self::STATUS_WAITING_FOR_REVIEW, self::STATUS_IN_REVIEW),
'type' => PARAM_INT,
'default' => self::STATUS_DRAFT,
),
'duedate' => array(
'type' => PARAM_INT,
'default' => 0,
),
'reviewerid' => array(
'type' => PARAM_INT,
'default' => null,
'null' => NULL_ALLOWED,
)
);
}
/**
* Hook to execute before validate.
*
* @return void
*/
protected function before_validate() {
$this->beforeupdate = null;
// During update.
if ($this->get_id()) {
$this->beforeupdate = new self($this->get_id());
}
}
/**
* Whether the current user can comment on this plan.
*
* @return bool
*/
public function can_comment() {
return static::can_comment_user($this->get_userid());
}
/**
* Whether the current user can manage the plan.
*
* @return bool
*/
public function can_manage() {
if ($this->is_draft()) {
return self::can_manage_user_draft($this->get_userid());
}
return self::can_manage_user($this->get_userid());
}
/**
* Whether the current user can read the plan.
*
* @return bool
*/
public function can_read() {
if ($this->is_draft()) {
return self::can_read_user_draft($this->get_userid());
}
return self::can_read_user($this->get_userid());
}
/**
* Whether the current user can read comments on this plan.
*
* @return bool
*/
public function can_read_comments() {
return $this->can_read();
}
/**
* Whether the current user can request a review of the plan.
*
* @return bool
*/
public function can_request_review() {
return self::can_request_review_user($this->get_userid());
}
/**
* Whether the current user can review the plan.
*
* @return bool
*/
public function can_review() {
return self::can_review_user($this->get_userid());
}
/**
* Get the comment object.
*
* @return comment
*/
public function get_comment_object() {
global $CFG;
require_once($CFG->dirroot . '/comment/lib.php');
if (!$this->get_id()) {
throw new \coding_exception('The plan must exist.');
}
$comment = new comment((object) array(
'client_id' => 'plancommentarea' . $this->get_id(),
'context' => $this->get_context(),
'component' => 'tool_lp',
'itemid' => $this->get_id(),
'area' => 'plan',
'showcount' => true,
));
$comment->set_fullwidth(true);
return $comment;
}
/**
* Get the competencies in this plan.
*
* @return competency[]
*/
public function get_competencies() {
$competencies = array();
if ($this->get_status() == self::STATUS_COMPLETE) {
// Get the competencies from the archive of the plan.
$competencies = user_competency_plan::list_competencies($this->get_id(), $this->get_userid());
} else if ($this->is_based_on_template()) {
// Get the competencies from the template.
$competencies = template_competency::list_competencies($this->get_templateid());
} else {
// Get the competencies from the plan.
$competencies = plan_competency::list_competencies($this->get_id());
}
return $competencies;
}
/**
* Get a single competency from this plan.
*
* @param int $competencyid The competency ID.
* @return competency
*/
public function get_competency($competencyid) {
$competencies = array();
if ($this->get_templateid()) {
// Get the competency from the template.
$competency = template_competency::get_competency($this->get_templateid(), $competencyid);
} else {
// Get the competency from the plan.
$competency = plan_competency::get_competency($this->get_id(), $competencyid);
}
return $competency;
}
/**
* Get the context in which the plan is attached.
*
* @return context_user
*/
public function get_context() {
return context_user::instance($this->get_userid());
}
/**
* Human readable status name.
*
* @return string
*/
public function get_statusname() {
$status = $this->get_status();
switch ($status) {
case self::STATUS_DRAFT:
$strname = 'draft';
break;
case self::STATUS_IN_REVIEW:
$strname = 'inreview';
break;
case self::STATUS_WAITING_FOR_REVIEW:
$strname = 'waitingforreview';
break;
case self::STATUS_ACTIVE:
$strname = 'active';
break;
case self::STATUS_COMPLETE:
$strname = 'complete';
break;
default:
throw new \moodle_exception('errorplanstatus', 'tool_lp', '', $status);
break;
}
return get_string('planstatus' . $strname, 'tool_lp');
}
/**
* Get the plan template.
*
* @return template|null
*/
public function get_template() {
$templateid = $this->get_templateid();
if ($templateid === null) {
return null;
}
return new template($templateid);
}
/**
* Is the plan in draft mode?
*
* This method is convenient to know if the plan is a draft because whilst a draft
* is being reviewed its status is not "draft" any more, but it still is a draft nonetheless.
*
* @return boolean
*/
public function is_draft() {
return in_array($this->get_status(), static::get_draft_statuses());
}
/**
* Validate the template ID.
*
* @param mixed $value The value.
* @return true|lang_string
*/
protected function validate_templateid($value) {
// Checks that the template exists.
if (!empty($value) && !template::record_exists($value)) {
return new lang_string('invaliddata', 'error');
}
return true;
}
/**
* Validate the user ID.
*
* @param int $value
* @return true|lang_string
*/
protected function validate_userid($value) {
global $DB;
// During create.
if (!$this->get_id()) {
// Check that the user exists. We do not need to do that on update because
// the userid of a plan should never change.
if (!$DB->record_exists('user', array('id' => $value))) {
return new lang_string('invaliddata', 'error');
}
}
return true;
}
/**
* Can the current user comment on a user's plan?
*
* @param int $planuserid The user ID the plan belongs to.
* @return bool
*/
public static function can_comment_user($planuserid) {
global $USER;
$capabilities = array('moodle/competency:plancomment');
if ($USER->id == $planuserid) {
$capabilities[] = 'moodle/competency:plancommentown';
}
return has_any_capability($capabilities, context_user::instance($planuserid));
}
/**
* Can the current user manage a user's plan?
*
* @param int $planuserid The user to whom the plan would belong.
* @return bool
*/
public static function can_manage_user($planuserid) {
global $USER;
$context = context_user::instance($planuserid);
$capabilities = array('moodle/competency:planmanage');
if ($context->instanceid == $USER->id) {
$capabilities[] = 'moodle/competency:planmanageown';
}
return has_any_capability($capabilities, $context);
}
/**
* Can the current user manage a user's draft plan?
*
* @param int $planuserid The user to whom the plan would belong.
* @return bool
*/
public static function can_manage_user_draft($planuserid) {
global $USER;
$context = context_user::instance($planuserid);
$capabilities = array('moodle/competency:planmanagedraft');
if ($context->instanceid == $USER->id) {
$capabilities[] = 'moodle/competency:planmanageowndraft';
}
return has_any_capability($capabilities, $context);
}
/**
* Can the current user read the comments on a user's plan?
*
* @param int $planuserid The user ID the plan belongs to.
* @return bool
*/
public static function can_read_comments_user($planuserid) {
// Everyone who can read the plan can read the comments.
return static::can_read_user($planuserid);
}
/**
* Can the current user view a user's plan?
*
* @param int $planuserid The user to whom the plan would belong.
* @return bool
*/
public static function can_read_user($planuserid) {
global $USER;
$context = context_user::instance($planuserid);
$capabilities = array('moodle/competency:planview');
if ($context->instanceid == $USER->id) {
$capabilities[] = 'moodle/competency:planviewown';
}
return has_any_capability($capabilities, $context)
|| self::can_manage_user($planuserid);
}
/**
* Can the current user view a user's draft plan?
*
* @param int $planuserid The user to whom the plan would belong.
* @return bool
*/
public static function can_read_user_draft($planuserid) {
global $USER;
$context = context_user::instance($planuserid);
$capabilities = array('moodle/competency:planviewdraft');
if ($context->instanceid == $USER->id) {
$capabilities[] = 'moodle/competency:planviewowndraft';
}
return has_any_capability($capabilities, $context)
|| self::can_manage_user_draft($planuserid);
}
/**
* Can the current user request the draft to be reviewed.
*
* @param int $planuserid The user to whom the plan would belong.
* @return bool
*/
public static function can_request_review_user($planuserid) {
global $USER;
$capabilities = array('moodle/competency:planrequestreview');
if ($USER->id == $planuserid) {
$capabilities[] = 'moodle/competency:planrequestreviewown';
}
return has_any_capability($capabilities, context_user::instance($planuserid));
}
/**
* Can the current user review the plan.
*
* This means being able to send the plan from draft to active, and vice versa.
*
* @param int $planuserid The user to whom the plan would belong.
* @return bool
*/
public static function can_review_user($planuserid) {
return has_capability('moodle/competency:planreview', context_user::instance($planuserid))
|| self::can_manage_user($planuserid);
}
/**
* Get the plans of a user containing a specific competency.
*
* @param int $userid The user ID.
* @param int $competencyid The competency ID.
* @return plans[]
*/
public static function get_by_user_and_competency($userid, $competencyid) {
global $DB;
$sql = 'SELECT p.*
FROM {' . self::TABLE . '} p
LEFT JOIN {' . plan_competency::TABLE . '} pc
ON pc.planid = p.id
AND pc.competencyid = :competencyid1
LEFT JOIN {' . user_competency_plan::TABLE . '} ucp
ON ucp.planid = p.id
AND ucp.competencyid = :competencyid2
LEFT JOIN {' . template_competency::TABLE . '} tc
ON tc.templateid = p.templateid
AND tc.competencyid = :competencyid3
WHERE p.userid = :userid
AND (pc.id IS NOT NULL
OR ucp.id IS NOT NULL
OR tc.id IS NOT NULL)
ORDER BY p.id ASC';
$params = array(
'competencyid1' => $competencyid,
'competencyid2' => $competencyid,
'competencyid3' => $competencyid,
'userid' => $userid
);
$plans = array();
$records = $DB->get_records_sql($sql, $params);
foreach ($records as $record) {
$plans[$record->id] = new plan(0, $record);
}
return $plans;
}
/**
* Get the list of draft statuses.
*
* @return array Contains the status constants.
*/
public static function get_draft_statuses() {
return array(self::STATUS_DRAFT, self::STATUS_WAITING_FOR_REVIEW, self::STATUS_IN_REVIEW);
}
/**
* Get the recordset of the plans that are due, incomplete and not draft.
*
* @return \moodle_recordset
*/
public static function get_recordset_for_due_and_incomplete() {
global $DB;
$sql = "duedate > 0 AND duedate < :now AND status = :status";
$params = array('now' => time(), 'status' => self::STATUS_ACTIVE);
return $DB->get_recordset_select(self::TABLE, $sql, $params);
}
/**
* Return a list of status depending on capabilities.
*
* @param int $userid The user to whom the plan would belong.
* @return array
*/
public static function get_status_list($userid) {
$status = array();
if (self::can_manage_user_draft($userid)) {
$status[self::STATUS_DRAFT] = get_string('planstatusdraft', 'tool_lp');
}
if (self::can_manage_user($userid)) {
$status[self::STATUS_ACTIVE] = get_string('planstatusactive', 'tool_lp');
}
return $status;
}
/**
* Update from template.
*
* Bulk update a lot of plans from a template
*
* @param template $template
* @return bool
*/
public static function update_multiple_from_template(template $template) {
global $DB;
if (!$template->is_valid()) {
// As we will bypass this model's validation we rely on the template being validated.
throw new \coding_exception('The template must be validated before updating plans.');
}
$params = array(
'templateid' => $template->get_id(),
'status' => self::STATUS_COMPLETE,
'name' => $template->get_shortname(),
'description' => $template->get_description(),
'descriptionformat' => $template->get_descriptionformat(),
'duedate' => $template->get_duedate(),
);
$sql = "UPDATE {" . self::TABLE . "}
SET name = :name,
description = :description,
descriptionformat = :descriptionformat,
duedate = :duedate
WHERE templateid = :templateid
AND status != :status";
return $DB->execute($sql, $params);
}
/**
* Check if a template is associated to the plan.
*
* @return bool
*/
public function is_based_on_template() {
return $this->get_templateid() !== null;
}
/**
* Check if plan can be edited.
*
* @return bool
*/
public function can_be_edited() {
return !$this->is_based_on_template() && $this->get_status() != self::STATUS_COMPLETE && $this->can_manage();
}
/**
* Validate the due date.
* When setting a due date it must not exceed the DUEDATE_THRESHOLD.
*
* @param int $value The due date.
* @return bool|lang_string
*/
protected function validate_duedate($value) {
// We do not check duedate when plan is draft, complete, unset, or based on a template.
if ($this->is_based_on_template()
|| $this->is_draft()
|| $this->get_status() == self::STATUS_COMPLETE
|| empty($value)) {
return true;
}
// During update.
if ($this->get_id()) {
$before = $this->beforeupdate->get_duedate();
$beforestatus = $this->beforeupdate->get_status();
// The value has not changed, then it's always OK. Though if we're going
// from draft to active it has to has to be validated.
if ($before == $value && !in_array($beforestatus, self::get_draft_statuses())) {
return true;
}
}
if ($value <= time()) {
// We cannot set the date in the past.
return new lang_string('errorcannotsetduedateinthepast', 'tool_lp');
}
if ($value <= time() + self::DUEDATE_THRESHOLD) {
// We cannot set the date too soon, but we can leave it empty.
return new lang_string('errorcannotsetduedatetoosoon', 'tool_lp');
}
return true;
}
/**
* Checks if a template has user plan records.
*
* @param int $templateid The template ID
* @return boolean
*/
public static function has_records_for_template($templateid) {
return self::record_exists_select('templateid = ?', array($templateid));
}
/**
* Count the number of plans for a template, optionally filtering by status.
*
* @param int $templateid The template ID
* @param int $status The plan status. 0 means all statuses.
* @return int
*/
public static function count_records_for_template($templateid, $status) {
$filters = array('templateid' => $templateid);
if ($status > 0) {
$filters['status'] = $status;
}
return self::count_records($filters);
}
/**
* Get the plans for a template, optionally filtering by status.
*
* @param int $templateid The template ID
* @param int $status The plan status. 0 means all statuses.
* @param int $skip The number of plans to skip
* @param int $limit The max number of plans to return
* @return int
*/
public static function get_records_for_template($templateid, $status = 0, $skip = 0, $limit = 100) {
$filters = array('templateid' => $templateid);
if ($status > 0) {
$filters['status'] = $status;
}
return self::get_records($filters, $skip, $limit);
}
}