-
Notifications
You must be signed in to change notification settings - Fork 2
/
renderables.php
688 lines (518 loc) · 20.7 KB
/
renderables.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
<?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/>.
/**
* Attendance module renderable components are defined here
*
* @package mod_attendance
* @copyright 2011 Artem Andreev <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(dirname(__FILE__).'/locallib.php');
/**
* Represents info about attendance tabs.
*
* Proxy class for security reasons (renderers must not have access to all attendance methods)
*
* @copyright 2011 Artem Andreev <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/
class attendance_tabs implements renderable {
const TAB_SESSIONS = 1;
const TAB_ADD = 2;
const TAB_REPORT = 3;
const TAB_EXPORT = 4;
const TAB_PREFERENCES = 5;
const TAB_TRAIN = 6;
const TAB_APPROVE = 7;
public $currenttab;
/** @var attendance */
private $att;
/**
* Prepare info about sessions for attendance taking into account view parameters.
*
* @param attendance $att instance
* @param $currenttab - one of attendance_tabs constants
*/
public function __construct(attendance $att, $currenttab=null) {
$this->att = $att;
$this->currenttab = $currenttab;
}
/**
* Return array of rows where each row is an array of tab objects
* taking into account permissions of current user
*/
public function get_tabs() {
$toprow = array();
if ($this->att->perm->can_manage() or
$this->att->perm->can_take() or
$this->att->perm->can_change()) {
$toprow[] = new tabobject(self::TAB_SESSIONS, $this->att->url_manage()->out(),
get_string('sessions', 'attendance'));
}
if ($this->att->perm->can_manage()) {
$toprow[] = new tabobject(self::TAB_ADD,
$this->att->url_sessions()->out(true, array('action' => att_sessions_page_params::ACTION_ADD)),
get_string('add', 'attendance'));
}
if ($this->att->perm->can_view_reports()) {
$toprow[] = new tabobject(self::TAB_REPORT, $this->att->url_report()->out(),
get_string('report', 'attendance'));
}
if ($this->att->perm->can_export()) {
$toprow[] = new tabobject(self::TAB_EXPORT, $this->att->url_export()->out(),
get_string('export', 'attendance'));
}
if ($this->att->perm->can_change_preferences()) {
$toprow[] = new tabobject(self::TAB_PREFERENCES, $this->att->url_preferences()->out(),
get_string('settings', 'attendance'));
}
// Ver quais permissoes colocar
// Adicionar get_string('train', 'attendance')
if ($this->att->perm->can_manage()) {
$toprow[] = new tabobject(self::TAB_TRAIN, $this->att->url_train()->out(),
'Train');
}
// Ver quais permissoes colocar
// Adicionar get_string('approve', 'attendance')
if ($this->att->perm->can_manage()) {
$toprow[] = new tabobject(self::TAB_APPROVE, $this->att->url_approve()->out(),
'Approve');
}
return array($toprow);
}
}
class attendance_filter_controls implements renderable {
/** @var int current view mode */
public $pageparams;
public $cm;
public $curdate;
public $prevcur;
public $nextcur;
public $curdatetxt;
public $reportcontrol;
private $urlpath;
private $urlparams;
private $att;
public function __construct(attendance $att, $report = false) {
global $PAGE;
$this->pageparams = $att->pageparams;
$this->cm = $att->cm;
// This is a report control only if $reports is true and the attendance block can be graded.
$this->reportcontrol = $report && ($att->grade > 0);
$this->curdate = $att->pageparams->curdate;
$date = usergetdate($att->pageparams->curdate);
$mday = $date['mday'];
$wday = $date['wday'];
$mon = $date['mon'];
$year = $date['year'];
switch ($this->pageparams->view) {
case ATT_VIEW_DAYS:
$format = get_string('strftimedm', 'attendance');
$this->prevcur = make_timestamp($year, $mon, $mday - 1);
$this->nextcur = make_timestamp($year, $mon, $mday + 1);
$this->curdatetxt = userdate($att->pageparams->startdate, $format);
break;
case ATT_VIEW_WEEKS:
$format = get_string('strftimedm', 'attendance');
$this->prevcur = $att->pageparams->startdate - WEEKSECS;
$this->nextcur = $att->pageparams->startdate + WEEKSECS;
$this->curdatetxt = userdate($att->pageparams->startdate, $format).
" - ".userdate($att->pageparams->enddate, $format);
break;
case ATT_VIEW_MONTHS:
$format = '%B';
$this->prevcur = make_timestamp($year, $mon - 1);
$this->nextcur = make_timestamp($year, $mon + 1);
$this->curdatetxt = userdate($att->pageparams->startdate, $format);
break;
}
$this->urlpath = $PAGE->url->out_omit_querystring();
$params = $att->pageparams->get_significant_params();
$params['id'] = $att->cm->id;
$this->urlparams = $params;
$this->att = $att;
}
public function url($params=array()) {
$params = array_merge($this->urlparams, $params);
return new moodle_url($this->urlpath, $params);
}
public function url_path() {
return $this->urlpath;
}
public function url_params($params=array()) {
$params = array_merge($this->urlparams, $params);
return $params;
}
public function get_group_mode() {
return $this->att->get_group_mode();
}
public function get_sess_groups_list() {
return $this->att->pageparams->get_sess_groups_list();
}
public function get_current_sesstype() {
return $this->att->pageparams->get_current_sesstype();
}
}
/**
* Represents info about attendance sessions taking into account view parameters.
*
* @copyright 2011 Artem Andreev <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class attendance_manage_data implements renderable {
/** @var array of sessions*/
public $sessions;
/** @var int number of hidden sessions (sessions before $course->startdate)*/
public $hiddensessionscount;
/** @var attendance_permissions permission of current user for attendance instance*/
public $perm;
public $groups;
public $hiddensesscount;
/** @var attendance */
private $att;
/**
* Prepare info about attendance sessions taking into account view parameters.
*
* @param attendance $att instance
*/
public function __construct(attendance $att) {
$this->perm = $att->perm;
$this->sessions = $att->get_filtered_sessions();
$this->groups = groups_get_all_groups($att->course->id);
$this->hiddensessionscount = $att->get_hidden_sessions_count();
$this->att = $att;
}
public function url_take($sessionid, $grouptype) {
return url_helpers::url_take($this->att, $sessionid, $grouptype);
}
/**
* Must be called without or with both parameters
*/
public function url_sessions($sessionid=null, $action=null) {
return url_helpers::url_sessions($this->att, $sessionid, $action);
}
}
class attendance_train_data implements renderable {
public $pageparams;
public $perm;
private $urlpath;
private $urlparams;
private $att;
public function __construct(attendance $att) {
$this->perm = $att->perm;
$this->pageparams = $att->pageparams;
$this->urlpath = $att->url_train()->out_omit_querystring();
$params = $att->pageparams->get_significant_params();
$params['id'] = $att->cm->id;
$this->urlparams = $params;
$this->att = $att;
}
public function url($params=array(), $excludeparams=array()) {
$params = array_merge($this->urlparams, $params);
foreach ($excludeparams as $paramkey) {
unset($params[$paramkey]);
}
return new moodle_url($this->urlpath, $params);
}
public function url_view($params=array()) {
return url_helpers::url_view($this->att, $params);
}
public function url_path() {
return $this->urlpath;
}
}
class attendance_approve_data implements renderable {
public $pageparams;
public $perm;
private $urlpath;
private $urlparams;
private $att;
public function __construct(attendance $att) {
$this->perm = $att->perm;
$this->pageparams = $att->pageparams;
$this->urlpath = $att->url_approve()->out_omit_querystring();
$params = $att->pageparams->get_significant_params();
$params['id'] = $att->cm->id;
$this->urlparams = $params;
$this->att = $att;
}
public function url($params=array(), $excludeparams=array()) {
$params = array_merge($this->urlparams, $params);
foreach ($excludeparams as $paramkey) {
unset($params[$paramkey]);
}
return new moodle_url($this->urlpath, $params);
}
public function url_view($params=array()) {
return url_helpers::url_view($this->att, $params);
}
public function url_path() {
return $this->urlpath;
}
}
class attendance_take_data implements renderable {
public $users;
public $pageparams;
public $perm;
public $groupmode;
public $cm;
public $statuses;
public $sessioninfo;
public $sessionlog;
public $sessions4copy;
public $updatemode;
private $urlpath;
private $urlparams;
private $att;
public function __construct(attendance $att) {
if ($att->pageparams->grouptype) {
$this->users = $att->get_users($att->pageparams->grouptype, $att->pageparams->page);
} else {
$this->users = $att->get_users($att->pageparams->group, $att->pageparams->page);
}
$this->pageparams = $att->pageparams;
$this->perm = $att->perm;
$this->groupmode = $att->get_group_mode();
$this->cm = $att->cm;
$this->statuses = $att->get_statuses();
$this->sessioninfo = $att->get_session_info($att->pageparams->sessionid);
$this->updatemode = $this->sessioninfo->lasttaken > 0;
if (isset($att->pageparams->copyfrom)) {
$this->sessionlog = $att->get_session_log($att->pageparams->copyfrom);
} else if ($this->updatemode) {
$this->sessionlog = $att->get_session_log($att->pageparams->sessionid);
} else {
$this->sessionlog = array();
}
if (!$this->updatemode) {
$this->sessions4copy = $att->get_today_sessions_for_copy($this->sessioninfo);
}
$this->urlpath = $att->url_take()->out_omit_querystring();
$params = $att->pageparams->get_significant_params();
$params['id'] = $att->cm->id;
$this->urlparams = $params;
$this->att = $att;
}
public function url($params=array(), $excludeparams=array()) {
$params = array_merge($this->urlparams, $params);
foreach ($excludeparams as $paramkey) {
unset($params[$paramkey]);
}
return new moodle_url($this->urlpath, $params);
}
public function url_view($params=array()) {
return url_helpers::url_view($this->att, $params);
}
public function url_path() {
return $this->urlpath;
}
}
class attendance_user_data implements renderable {
public $user;
public $pageparams;
public $stat;
public $statuses;
public $gradable;
public $grade;
public $maxgrade;
public $decimalpoints;
public $filtercontrols;
public $sessionslog;
public $groups;
public $coursesatts;
public $courseid;
public $urlpath;
public $urlparams;
public function __construct(attendance $att, $userid) {
global $CFG, $USER, $DB;
$this->user = $att->get_user($userid);
$this->pageparams = $att->pageparams;
if (!$this->decimalpoints = grade_get_setting($att->course->id, 'decimalpoints')) {
$this->decimalpoints = $CFG->grade_decimalpoints;
}
if ($this->pageparams->mode == att_view_page_params::MODE_THIS_COURSE) {
$this->statuses = $att->get_statuses();
$this->stat = $att->get_user_stat($userid);
$this->gradable = $att->grade > 0;
if ($this->gradable) {
$this->grade = $att->get_user_grade($userid);
$this->maxgrade = $att->get_user_max_grade($userid);
}
$this->filtercontrols = new attendance_filter_controls($att);
$this->sessionslog = $att->get_user_filtered_sessions_log_extended($userid);
$this->groups = groups_get_all_groups($att->course->id);
} else {
$this->coursesatts = att_get_user_courses_attendances($userid);
$this->statuses = array();
$this->stat = array();
$this->gradable = array();
$this->grade = array();
$this->maxgrade = array();
foreach ($this->coursesatts as $atid => $ca) {
// Check to make sure the user can view this cm.
if (!get_fast_modinfo($ca->courseid)->instances['attendance'][$ca->attid]->uservisible) {
unset($this->courseatts[$atid]);
continue;
}
$statuses = att_get_statuses($ca->attid);
$user_taken_sessions_count = att_get_user_taken_sessions_count($ca->attid, $ca->coursestartdate, $userid, $att->cm);
$user_statuses_stat = att_get_user_statuses_stat($ca->attid, $ca->coursestartdate, $userid, $att->cm);
$this->statuses[$ca->attid] = $statuses;
$this->stat[$ca->attid]['completed'] = $user_taken_sessions_count;
$this->stat[$ca->attid]['statuses'] = $user_statuses_stat;
$this->gradable[$ca->attid] = $ca->attgrade > 0;
if ($this->gradable[$ca->attid]) {
$this->grade[$ca->attid] = att_get_user_grade($user_statuses_stat, $statuses);
// For getting sessions count implemented simplest method - taken sessions.
// It can have error if users don't have attendance info for some sessions.
// In the future we can implement another methods:
// * all sessions between user start enrolment date and now;
// * all sessions between user start and end enrolment date.
$this->maxgrade[$ca->attid] = att_get_user_max_grade($user_taken_sessions_count, $statuses);
} else {
// For more comfortable and universal work with arrays.
$this->grade[$ca->attid] = null;
$this->maxgrade[$ca->attid] = null;
}
}
}
$this->urlpath = $att->url_view()->out_omit_querystring();
$params = $att->pageparams->get_significant_params();
$params['id'] = $att->cm->id;
$course = $DB->get_record('course', array('id' => $att->cm->course), '*', MUST_EXIST);
$this->courseid = $course->id;
$this->urlparams = $params;
}
public function url() {
return new moodle_url($this->urlpath, $this->urlparams);
}
}
class attendance_report_data implements renderable {
public $perm;
public $pageparams;
public $users;
public $groups;
public $sessions;
public $statuses;
// Includes disablrd/deleted statuses.
public $allstatuses;
public $gradable;
public $decimalpoints;
public $usersgroups = array();
public $sessionslog = array();
public $usersstats = array();
public $grades = array();
public $maxgrades = array();
private $att;
public function __construct(attendance $att) {
global $CFG;
$this->perm = $att->perm;
$currenttime = time();
if ($att->pageparams->view == ATT_VIEW_NOTPRESENT) {
$att->pageparams->enddate = $currenttime;
}
$this->pageparams = $att->pageparams;
$this->users = $att->get_users($att->pageparams->group, $att->pageparams->page);
$this->groups = groups_get_all_groups($att->course->id);
$this->sessions = $att->get_filtered_sessions(false);
$this->statuses = $att->get_statuses();
$this->allstatuses = $att->get_statuses(false);
$this->gradable = $att->grade > 0;
if (!$this->decimalpoints = grade_get_setting($att->course->id, 'decimalpoints')) {
$this->decimalpoints = $CFG->grade_decimalpoints;
}
$maxgrade = att_get_user_max_grade(count($this->sessions), $this->statuses);
foreach ($this->users as $key => $user) {
$grade = 0;
if ($this->gradable) {
$grade = $att->get_user_grade($user->id, array('enddate' => $currenttime));
$totalgrade = $att->get_user_grade($user->id);
}
if ($att->pageparams->view != ATT_VIEW_NOTPRESENT || $grade < $maxgrade) {
$this->usersgroups[$user->id] = groups_get_all_groups($att->course->id, $user->id);
$this->sessionslog[$user->id] = $att->get_user_filtered_sessions_log($user->id);
$this->usersstats[$user->id] = $att->get_user_statuses_stat($user->id);
if ($this->gradable) {
$this->grades[$user->id] = $totalgrade;
$this->maxgrades[$user->id] = $att->get_user_max_grade($user->id);;
}
} else {
unset($this->users[$key]);
}
}
$this->att = $att;
}
public function url_take($sessionid, $grouptype) {
return url_helpers::url_take($this->att, $sessionid, $grouptype);
}
public function url_view($params=array()) {
return url_helpers::url_view($this->att, $params);
}
public function url($params=array()) {
$params = array_merge($params, $this->pageparams->get_significant_params());
return $this->att->url_report($params);
}
}
class attendance_preferences_data implements renderable {
public $statuses;
private $att;
public function __construct(attendance $att) {
$this->statuses = $att->get_statuses(false);
foreach ($this->statuses as $st) {
$st->haslogs = att_has_logs_for_status ($st->id);
}
$this->att = $att;
}
public function url($params=array(), $significant_params=true) {
if ($significant_params) {
$params = array_merge($this->att->pageparams->get_significant_params(), $params);
}
return $this->att->url_preferences($params);
}
}
class url_helpers {
public static function url_take($att, $sessionid, $grouptype) {
$params = array('sessionid' => $sessionid);
if (isset($grouptype)) {
$params['grouptype'] = $grouptype;
}
return $att->url_take($params);
}
/**
* Must be called without or with both parameters
*/
public static function url_sessions($att, $sessionid=null, $action=null) {
if (isset($sessionid) && isset($action)) {
$params = array('sessionid' => $sessionid, 'action' => $action);
} else {
$params = array();
}
return $att->url_sessions($params);
}
public static function url_view($att, $params=array()) {
return $att->url_view($params);
}
}
class attendance_image_tagging_data implements renderable {
public $imageurl;
public $actionurl;
public function __construct($imageurl, $actionurl) {
$this->imageurl = $imageurl;
$this->actionurl = $actionurl;
}
}