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 * Attendance module renderable components are defined here
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
25 defined('MOODLE_INTERNAL') ||
die();
27 require_once(dirname(__FILE__
).'/locallib.php');
31 * Represents info about attendance tabs.
33 * Proxy class for security reasons (renderers must not have access to all attendance methods)
35 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class attendance_tabs
implements renderable
{
40 const TAB_SESSIONS
= 1;
44 const TAB_PREFERENCES
= 5;
48 /** @var attendance */
52 * Prepare info about sessions for attendance taking into account view parameters.
54 * @param attendance $att instance
55 * @param $currenttab - one of attendance_tabs constants
57 public function __construct(attendance
$att, $currenttab=null
) {
59 $this->currenttab
= $currenttab;
63 * Return array of rows where each row is an array of tab objects
64 * taking into account permissions of current user
66 public function get_tabs() {
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'));
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'));
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'));
86 if ($this->att
->perm
->can_export()) {
87 $toprow[] = new tabobject(self
::TAB_EXPORT
, $this->att
->url_export()->out(),
88 get_string('export', 'quiz'));
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'));
96 return array($toprow);
101 class attendance_filter_controls
implements renderable
{
102 /** @var int current view mode */
112 public $reportcontrol;
119 public function __construct(attendance
$att, $report = false
) {
122 $this->pageparams
= $att->pageparams
;
124 $this->cm
= $att->cm
;
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);
129 $this->curdate
= $att->pageparams
->curdate
;
131 $date = usergetdate($att->pageparams
->curdate
);
132 $mday = $date['mday'];
133 $wday = $date['wday'];
135 $year = $date['year'];
137 switch ($this->pageparams
->view
) {
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);
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);
151 case ATT_VIEW_MONTHS
:
153 $this->prevcur
= make_timestamp($year, $mon - 1);
154 $this->nextcur
= make_timestamp($year, $mon +
1);
155 $this->curdatetxt
= userdate($att->pageparams
->startdate
, $format);
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;
167 public function url($params=array()) {
168 $params = array_merge($this->urlparams
, $params);
170 return new moodle_url($this->urlpath
, $params);
173 public function url_path() {
174 return $this->urlpath
;
177 public function url_params($params=array()) {
178 $params = array_merge($this->urlparams
, $params);
183 public function get_group_mode() {
184 return $this->att
->get_group_mode();
187 public function get_sess_groups_list() {
188 return $this->att
->pageparams
->get_sess_groups_list();
191 public function get_current_sesstype() {
192 return $this->att
->pageparams
->get_current_sesstype();
197 * Represents info about attendance sessions taking into account view parameters.
199 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
200 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
202 class attendance_manage_data
implements renderable
{
203 /** @var array of sessions*/
206 /** @var int number of hidden sessions (sessions before $course->startdate)*/
207 public $hiddensessionscount;
209 /** @var attendance_permissions permission of current user for attendance instance*/
214 public $hiddensesscount;
216 /** @var attendance */
219 * Prepare info about attendance sessions taking into account view parameters.
221 * @param attendance $att instance
223 public function __construct(attendance
$att) {
224 $this->perm
= $att->perm
;
226 $this->sessions
= $att->get_filtered_sessions();
228 $this->groups
= groups_get_all_groups($att->course
->id
);
230 $this->hiddensessionscount
= $att->get_hidden_sessions_count();
235 public function url_take($sessionid, $grouptype) {
236 return url_helpers
::url_take($this->att
, $sessionid, $grouptype);
240 * Must be called without or with both parameters
242 public function url_sessions($sessionid=null
, $action=null
) {
243 return url_helpers
::url_sessions($this->att
, $sessionid, $action);
247 class attendance_take_data
implements renderable
{
262 public $sessions4copy;
270 public function __construct(attendance
$att) {
271 if ($att->pageparams
->grouptype
) {
272 $this->users
= $att->get_users($att->pageparams
->grouptype
, $att->pageparams
->page
);
274 $this->users
= $att->get_users($att->pageparams
->group
, $att->pageparams
->page
);
277 $this->pageparams
= $att->pageparams
;
278 $this->perm
= $att->perm
;
280 $this->groupmode
= $att->get_group_mode();
281 $this->cm
= $att->cm
;
283 $this->statuses
= $att->get_statuses();
285 $this->sessioninfo
= $att->get_session_info($att->pageparams
->sessionid
);
286 $this->updatemode
= $this->sessioninfo
->lasttaken
> 0;
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
);
293 $this->sessionlog
= array();
296 if (!$this->updatemode
) {
297 $this->sessions4copy
= $att->get_today_sessions_for_copy($this->sessioninfo
);
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;
308 public function url($params=array(), $excludeparams=array()) {
309 $params = array_merge($this->urlparams
, $params);
311 foreach ($excludeparams as $paramkey) {
312 unset($params[$paramkey]);
315 return new moodle_url($this->urlpath
, $params);
318 public function url_view($params=array()) {
319 return url_helpers
::url_view($this->att
, $params);
322 public function url_path() {
323 return $this->urlpath
;
327 class attendance_user_data
implements renderable
{
342 public $decimalpoints;
344 public $filtercontrols;
355 public function __construct(attendance
$att, $userid) {
358 $this->user
= $att->get_user($userid);
360 $this->pageparams
= $att->pageparams
;
362 if (!$this->decimalpoints
= grade_get_setting($att->course
->id
, 'decimalpoints')) {
363 $this->decimalpoints
= $CFG->grade_decimalpoints
;
366 if ($this->pageparams
->mode
== att_view_page_params
::MODE_THIS_COURSE
) {
367 $this->statuses
= $att->get_statuses();
369 $this->stat
= $att->get_user_stat($userid);
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);
377 $this->filtercontrols
= new attendance_filter_controls($att);
379 $this->sessionslog
= $att->get_user_filtered_sessions_log_extended($userid);
381 $this->groups
= groups_get_all_groups($att->course
->id
);
383 $this->coursesatts
= att_get_user_courses_attendances($userid);
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
);
395 $this->statuses
[$ca->attid
] = $statuses;
397 $this->stat
[$ca->attid
]['completed'] = $user_taken_sessions_count;
398 $this->stat
[$ca->attid
]['statuses'] = $user_statuses_stat;
400 $this->gradable
[$ca->attid
] = $ca->attgrade
> 0;
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);
411 // For more comfortable and universal work with arrays.
412 $this->grade
[$ca->attid
] = null
;
413 $this->maxgrade
[$ca->attid
] = null
;
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;
424 public function url() {
425 return new moodle_url($this->urlpath
, $this->urlparams
);
429 class attendance_report_data
implements renderable
{
440 // Includes disablrd/deleted statuses.
445 public $decimalpoints;
447 public $usersgroups = array();
449 public $sessionslog = array();
451 public $usersstats = array();
453 public $grades = array();
455 public $maxgrades = array();
459 public function __construct(attendance
$att) {
462 $this->perm
= $att->perm
;
464 $currenttime = time();
465 if ($att->pageparams
->view
== ATT_VIEW_NOTPRESENT
) {
466 $att->pageparams
->enddate
= $currenttime;
469 $this->pageparams
= $att->pageparams
;
471 $this->users
= $att->get_users($att->pageparams
->group
, $att->pageparams
->page
);
473 $this->groups
= groups_get_all_groups($att->course
->id
);
475 $this->sessions
= $att->get_filtered_sessions();
477 $this->statuses
= $att->get_statuses();
478 $this->allstatuses
= $att->get_statuses(false
);
480 $this->gradable
= $att->grade
> 0;
482 if (!$this->decimalpoints
= grade_get_setting($att->course
->id
, 'decimalpoints')) {
483 $this->decimalpoints
= $CFG->grade_decimalpoints
;
486 $maxgrade = att_get_user_max_grade(count($this->sessions
), $this->statuses
);
488 foreach ($this->users
as $key => $user) {
490 if ($this->gradable
) {
491 $grade = $att->get_user_grade($user->id
, array('enddate' => $currenttime));
492 $totalgrade = $att->get_user_grade($user->id
);
495 if ($att->pageparams
->view
!= ATT_VIEW_NOTPRESENT ||
$grade < $maxgrade) {
496 $this->usersgroups
[$user->id
] = groups_get_all_groups($att->course
->id
, $user->id
);
498 $this->sessionslog
[$user->id
] = $att->get_user_filtered_sessions_log($user->id
);
500 $this->usersstats
[$user->id
] = $att->get_user_statuses_stat($user->id
);
502 if ($this->gradable
) {
503 $this->grades
[$user->id
] = $totalgrade;
504 $this->maxgrades
[$user->id
] = $att->get_user_max_grade($user->id
);;
507 unset($this->users
[$key]);
514 public function url_take($sessionid, $grouptype) {
515 return url_helpers
::url_take($this->att
, $sessionid, $grouptype);
518 public function url_view($params=array()) {
519 return url_helpers
::url_view($this->att
, $params);
522 public function url($params=array()) {
523 $params = array_merge($params, $this->pageparams
->get_significant_params());
525 return $this->att
->url_report($params);
530 class attendance_preferences_data
implements renderable
{
535 public function __construct(attendance
$att) {
536 $this->statuses
= $att->get_statuses(false
);
538 foreach ($this->statuses
as $st) {
539 $st->haslogs
= att_has_logs_for_status ($st->id
);
545 public function url($params=array(), $significant_params=true
) {
546 if ($significant_params) {
547 $params = array_merge($this->att
->pageparams
->get_significant_params(), $params);
550 return $this->att
->url_preferences($params);
555 public static function url_take($att, $sessionid, $grouptype) {
556 $params = array('sessionid' => $sessionid);
557 if (isset($grouptype)) {
558 $params['grouptype'] = $grouptype;
561 return $att->url_take($params);
565 * Must be called without or with both parameters
567 public static function url_sessions($att, $sessionid=null
, $action=null
) {
568 if (isset($sessionid) && isset($action)) {
569 $params = array('sessionid' => $sessionid, 'action' => $action);
574 return $att->url_sessions($params);
577 public static function url_view($att, $params=array()) {
578 return $att->url_view($params);