Merge pull request #83 from barrysspace/expired_self_enrolments_MDL26
[moodle-mod_attendance.git] / renderables.php
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.
13 //
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/>.
16
17 /**
18 * Attendance module renderable components are defined here
19 *
20 * @package mod_attendance
21 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25 defined('MOODLE_INTERNAL') || die();
26
27 require_once(dirname(__FILE__).'/locallib.php');
28
29
30 /**
31 * Represents info about attendance tabs.
32 *
33 * Proxy class for security reasons (renderers must not have access to all attendance methods)
34 *
35 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 *
38 */
39 class attendance_tabs implements renderable {
40 const TAB_SESSIONS = 1;
41 const TAB_ADD = 2;
42 const TAB_REPORT = 3;
43 const TAB_EXPORT = 4;
44 const TAB_PREFERENCES = 5;
45
46 public $currenttab;
47
48 /** @var attendance */
49 private $att;
50
51 /**
52 * Prepare info about sessions for attendance taking into account view parameters.
53 *
54 * @param attendance $att instance
55 * @param $currenttab - one of attendance_tabs constants
56 */
57 public function __construct(attendance $att, $currenttab=null) {
58 $this->att = $att;
59 $this->currenttab = $currenttab;
60 }
61
62 /**
63 * Return array of rows where each row is an array of tab objects
64 * taking into account permissions of current user
65 */
66 public function get_tabs() {
67 $toprow = array();
68 if ($this->att->perm->can_manage() or
69 $this->att->perm->can_take() or
70 $this->att->perm->can_change()) {
71 $toprow[] = new tabobject(self::TAB_SESSIONS, $this->att->url_manage()->out(),
72 get_string('sessions', 'attendance'));
73 }
74
75 if ($this->att->perm->can_manage()) {
76 $toprow[] = new tabobject(self::TAB_ADD,
77 $this->att->url_sessions()->out(true, array('action' => att_sessions_page_params::ACTION_ADD)),
78 get_string('add', 'attendance'));
79 }
80
81 if ($this->att->perm->can_view_reports()) {
82 $toprow[] = new tabobject(self::TAB_REPORT, $this->att->url_report()->out(),
83 get_string('report', 'attendance'));
84 }
85
86 if ($this->att->perm->can_export()) {
87 $toprow[] = new tabobject(self::TAB_EXPORT, $this->att->url_export()->out(),
88 get_string('export', 'quiz'));
89 }
90
91 if ($this->att->perm->can_change_preferences()) {
92 $toprow[] = new tabobject(self::TAB_PREFERENCES, $this->att->url_preferences()->out(),
93 get_string('settings', 'attendance'));
94 }
95
96 return array($toprow);
97 }
98 }
99
100
101 class attendance_filter_controls implements renderable {
102 /** @var int current view mode */
103 public $pageparams;
104
105 public $cm;
106
107 public $curdate;
108
109 public $prevcur;
110 public $nextcur;
111 public $curdatetxt;
112 public $reportcontrol;
113
114 private $urlpath;
115 private $urlparams;
116
117 private $att;
118
119 public function __construct(attendance $att, $report = false) {
120 global $PAGE;
121
122 $this->pageparams = $att->pageparams;
123
124 $this->cm = $att->cm;
125
126 // This is a report control only if $reports is true and the attendance block can be graded.
127 $this->reportcontrol = $report && ($att->grade > 0);
128
129 $this->curdate = $att->pageparams->curdate;
130
131 $date = usergetdate($att->pageparams->curdate);
132 $mday = $date['mday'];
133 $wday = $date['wday'];
134 $mon = $date['mon'];
135 $year = $date['year'];
136
137 switch ($this->pageparams->view) {
138 case ATT_VIEW_DAYS:
139 $format = get_string('strftimedm', 'attendance');
140 $this->prevcur = make_timestamp($year, $mon, $mday - 1);
141 $this->nextcur = make_timestamp($year, $mon, $mday + 1);
142 $this->curdatetxt = userdate($att->pageparams->startdate, $format);
143 break;
144 case ATT_VIEW_WEEKS:
145 $format = get_string('strftimedm', 'attendance');
146 $this->prevcur = $att->pageparams->startdate - WEEKSECS;
147 $this->nextcur = $att->pageparams->startdate + WEEKSECS;
148 $this->curdatetxt = userdate($att->pageparams->startdate, $format).
149 " - ".userdate($att->pageparams->enddate, $format);
150 break;
151 case ATT_VIEW_MONTHS:
152 $format = '%B';
153 $this->prevcur = make_timestamp($year, $mon - 1);
154 $this->nextcur = make_timestamp($year, $mon + 1);
155 $this->curdatetxt = userdate($att->pageparams->startdate, $format);
156 break;
157 }
158
159 $this->urlpath = $PAGE->url->out_omit_querystring();
160 $params = $att->pageparams->get_significant_params();
161 $params['id'] = $att->cm->id;
162 $this->urlparams = $params;
163
164 $this->att = $att;
165 }
166
167 public function url($params=array()) {
168 $params = array_merge($this->urlparams, $params);
169
170 return new moodle_url($this->urlpath, $params);
171 }
172
173 public function url_path() {
174 return $this->urlpath;
175 }
176
177 public function url_params($params=array()) {
178 $params = array_merge($this->urlparams, $params);
179
180 return $params;
181 }
182
183 public function get_group_mode() {
184 return $this->att->get_group_mode();
185 }
186
187 public function get_sess_groups_list() {
188 return $this->att->pageparams->get_sess_groups_list();
189 }
190
191 public function get_current_sesstype() {
192 return $this->att->pageparams->get_current_sesstype();
193 }
194 }
195
196 /**
197 * Represents info about attendance sessions taking into account view parameters.
198 *
199 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
200 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
201 */
202 class attendance_manage_data implements renderable {
203 /** @var array of sessions*/
204 public $sessions;
205
206 /** @var int number of hidden sessions (sessions before $course->startdate)*/
207 public $hiddensessionscount;
208
209 /** @var attendance_permissions permission of current user for attendance instance*/
210 public $perm;
211
212 public $groups;
213
214 public $hiddensesscount;
215
216 /** @var attendance */
217 private $att;
218 /**
219 * Prepare info about attendance sessions taking into account view parameters.
220 *
221 * @param attendance $att instance
222 */
223 public function __construct(attendance $att) {
224 $this->perm = $att->perm;
225
226 $this->sessions = $att->get_filtered_sessions();
227
228 $this->groups = groups_get_all_groups($att->course->id);
229
230 $this->hiddensessionscount = $att->get_hidden_sessions_count();
231
232 $this->att = $att;
233 }
234
235 public function url_take($sessionid, $grouptype) {
236 return url_helpers::url_take($this->att, $sessionid, $grouptype);
237 }
238
239 /**
240 * Must be called without or with both parameters
241 */
242 public function url_sessions($sessionid=null, $action=null) {
243 return url_helpers::url_sessions($this->att, $sessionid, $action);
244 }
245 }
246
247 class attendance_take_data implements renderable {
248 public $users;
249
250 public $pageparams;
251 public $perm;
252
253 public $groupmode;
254 public $cm;
255
256 public $statuses;
257
258 public $sessioninfo;
259
260 public $sessionlog;
261
262 public $sessions4copy;
263
264 public $updatemode;
265
266 private $urlpath;
267 private $urlparams;
268 private $att;
269
270 public function __construct(attendance $att) {
271 if ($att->pageparams->grouptype) {
272 $this->users = $att->get_users($att->pageparams->grouptype, $att->pageparams->page);
273 } else {
274 $this->users = $att->get_users($att->pageparams->group, $att->pageparams->page);
275 }
276
277 $this->pageparams = $att->pageparams;
278 $this->perm = $att->perm;
279
280 $this->groupmode = $att->get_group_mode();
281 $this->cm = $att->cm;
282
283 $this->statuses = $att->get_statuses();
284
285 $this->sessioninfo = $att->get_session_info($att->pageparams->sessionid);
286 $this->updatemode = $this->sessioninfo->lasttaken > 0;
287
288 if (isset($att->pageparams->copyfrom)) {
289 $this->sessionlog = $att->get_session_log($att->pageparams->copyfrom);
290 } else if ($this->updatemode) {
291 $this->sessionlog = $att->get_session_log($att->pageparams->sessionid);
292 } else {
293 $this->sessionlog = array();
294 }
295
296 if (!$this->updatemode) {
297 $this->sessions4copy = $att->get_today_sessions_for_copy($this->sessioninfo);
298 }
299
300 $this->urlpath = $att->url_take()->out_omit_querystring();
301 $params = $att->pageparams->get_significant_params();
302 $params['id'] = $att->cm->id;
303 $this->urlparams = $params;
304
305 $this->att = $att;
306 }
307
308 public function url($params=array(), $excludeparams=array()) {
309 $params = array_merge($this->urlparams, $params);
310
311 foreach ($excludeparams as $paramkey) {
312 unset($params[$paramkey]);
313 }
314
315 return new moodle_url($this->urlpath, $params);
316 }
317
318 public function url_view($params=array()) {
319 return url_helpers::url_view($this->att, $params);
320 }
321
322 public function url_path() {
323 return $this->urlpath;
324 }
325 }
326
327 class attendance_user_data implements renderable {
328 public $user;
329
330 public $pageparams;
331
332 public $stat;
333
334 public $statuses;
335
336 public $gradable;
337
338 public $grade;
339
340 public $maxgrade;
341
342 public $decimalpoints;
343
344 public $filtercontrols;
345
346 public $sessionslog;
347
348 public $groups;
349
350 public $coursesatts;
351
352 private $urlpath;
353 private $urlparams;
354
355 public function __construct(attendance $att, $userid) {
356 global $CFG;
357
358 $this->user = $att->get_user($userid);
359
360 $this->pageparams = $att->pageparams;
361
362 if (!$this->decimalpoints = grade_get_setting($att->course->id, 'decimalpoints')) {
363 $this->decimalpoints = $CFG->grade_decimalpoints;
364 }
365
366 if ($this->pageparams->mode == att_view_page_params::MODE_THIS_COURSE) {
367 $this->statuses = $att->get_statuses();
368
369 $this->stat = $att->get_user_stat($userid);
370
371 $this->gradable = $att->grade > 0;
372 if ($this->gradable) {
373 $this->grade = $att->get_user_grade($userid);
374 $this->maxgrade = $att->get_user_max_grade($userid);
375 }
376
377 $this->filtercontrols = new attendance_filter_controls($att);
378
379 $this->sessionslog = $att->get_user_filtered_sessions_log_extended($userid);
380
381 $this->groups = groups_get_all_groups($att->course->id);
382 } else {
383 $this->coursesatts = att_get_user_courses_attendances($userid);
384
385 $this->statuses = array();
386 $this->stat = array();
387 $this->gradable = array();
388 $this->grade = array();
389 $this->maxgrade = array();
390 foreach ($this->coursesatts as $ca) {
391 $statuses = att_get_statuses($ca->attid);
392 $user_taken_sessions_count = att_get_user_taken_sessions_count($ca->attid, $ca->coursestartdate, $userid, $att->cm);
393 $user_statuses_stat = att_get_user_statuses_stat($ca->attid, $ca->coursestartdate, $userid, $att->cm);
394
395 $this->statuses[$ca->attid] = $statuses;
396
397 $this->stat[$ca->attid]['completed'] = $user_taken_sessions_count;
398 $this->stat[$ca->attid]['statuses'] = $user_statuses_stat;
399
400 $this->gradable[$ca->attid] = $ca->attgrade > 0;
401
402 if ($this->gradable[$ca->attid]) {
403 $this->grade[$ca->attid] = att_get_user_grade($user_statuses_stat, $statuses);
404 // For getting sessions count implemented simplest method - taken sessions.
405 // It can have error if users don't have attendance info for some sessions.
406 // In the future we can implement another methods:
407 // * all sessions between user start enrolment date and now;
408 // * all sessions between user start and end enrolment date.
409 $this->maxgrade[$ca->attid] = att_get_user_max_grade($user_taken_sessions_count, $statuses);
410 } else {
411 // For more comfortable and universal work with arrays.
412 $this->grade[$ca->attid] = null;
413 $this->maxgrade[$ca->attid] = null;
414 }
415 }
416 }
417
418 $this->urlpath = $att->url_view()->out_omit_querystring();
419 $params = $att->pageparams->get_significant_params();
420 $params['id'] = $att->cm->id;
421 $this->urlparams = $params;
422 }
423
424 public function url() {
425 return new moodle_url($this->urlpath, $this->urlparams);
426 }
427 }
428
429 class attendance_report_data implements renderable {
430 public $perm;
431 public $pageparams;
432
433 public $users;
434
435 public $groups;
436
437 public $sessions;
438
439 public $statuses;
440 // Includes disablrd/deleted statuses.
441 public $allstatuses;
442
443 public $gradable;
444
445 public $decimalpoints;
446
447 public $usersgroups = array();
448
449 public $sessionslog = array();
450
451 public $usersstats = array();
452
453 public $grades = array();
454
455 public $maxgrades = array();
456
457 private $att;
458
459 public function __construct(attendance $att) {
460 global $CFG;
461
462 $this->perm = $att->perm;
463
464 $currenttime = time();
465 if ($att->pageparams->view == ATT_VIEW_NOTPRESENT) {
466 $att->pageparams->enddate = $currenttime;
467 }
468
469 $this->pageparams = $att->pageparams;
470
471 $this->users = $att->get_users($att->pageparams->group, $att->pageparams->page);
472
473 $this->groups = groups_get_all_groups($att->course->id);
474
475 $this->sessions = $att->get_filtered_sessions();
476
477 $this->statuses = $att->get_statuses();
478 $this->allstatuses = $att->get_statuses(false);
479
480 $this->gradable = $att->grade > 0;
481
482 if (!$this->decimalpoints = grade_get_setting($att->course->id, 'decimalpoints')) {
483 $this->decimalpoints = $CFG->grade_decimalpoints;
484 }
485
486 $maxgrade = att_get_user_max_grade(count($this->sessions), $this->statuses);
487
488 foreach ($this->users as $key => $user) {
489 $grade = 0;
490 if ($this->gradable) {
491 $grade = $att->get_user_grade($user->id, array('enddate' => $currenttime));
492 $totalgrade = $att->get_user_grade($user->id);
493 }
494
495 if ($att->pageparams->view != ATT_VIEW_NOTPRESENT || $grade < $maxgrade) {
496 $this->usersgroups[$user->id] = groups_get_all_groups($att->course->id, $user->id);
497
498 $this->sessionslog[$user->id] = $att->get_user_filtered_sessions_log($user->id);
499
500 $this->usersstats[$user->id] = $att->get_user_statuses_stat($user->id);
501
502 if ($this->gradable) {
503 $this->grades[$user->id] = $totalgrade;
504 $this->maxgrades[$user->id] = $att->get_user_max_grade($user->id);;
505 }
506 } else {
507 unset($this->users[$key]);
508 }
509 }
510
511 $this->att = $att;
512 }
513
514 public function url_take($sessionid, $grouptype) {
515 return url_helpers::url_take($this->att, $sessionid, $grouptype);
516 }
517
518 public function url_view($params=array()) {
519 return url_helpers::url_view($this->att, $params);
520 }
521
522 public function url($params=array()) {
523 $params = array_merge($params, $this->pageparams->get_significant_params());
524
525 return $this->att->url_report($params);
526 }
527
528 }
529
530 class attendance_preferences_data implements renderable {
531 public $statuses;
532
533 private $att;
534
535 public function __construct(attendance $att) {
536 $this->statuses = $att->get_statuses(false);
537
538 foreach ($this->statuses as $st) {
539 $st->haslogs = att_has_logs_for_status ($st->id);
540 }
541
542 $this->att = $att;
543 }
544
545 public function url($params=array(), $significant_params=true) {
546 if ($significant_params) {
547 $params = array_merge($this->att->pageparams->get_significant_params(), $params);
548 }
549
550 return $this->att->url_preferences($params);
551 }
552 }
553
554 class url_helpers {
555 public static function url_take($att, $sessionid, $grouptype) {
556 $params = array('sessionid' => $sessionid);
557 if (isset($grouptype)) {
558 $params['grouptype'] = $grouptype;
559 }
560
561 return $att->url_take($params);
562 }
563
564 /**
565 * Must be called without or with both parameters
566 */
567 public static function url_sessions($att, $sessionid=null, $action=null) {
568 if (isset($sessionid) && isset($action)) {
569 $params = array('sessionid' => $sessionid, 'action' => $action);
570 } else {
571 $params = array();
572 }
573
574 return $att->url_sessions($params);
575 }
576
577 public static function url_view($att, $params=array()) {
578 return $att->url_view($params);
579 }
580 }