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 renderering helpers
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__
).'/renderables.php');
30 * class Template method for generating user's session's cells
32 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class user_sessions_cells_generator
{
36 protected $cells = array();
38 protected $reportdata;
41 public function __construct(attendance_report_data
$reportdata, $user) {
42 $this->reportdata
= $reportdata;
46 public function get_cells($remarks = false
) {
48 foreach ($this->reportdata
->sessions
as $sess) {
49 if (array_key_exists($sess->id
, $this->reportdata
->sessionslog
[$this->user
->id
])) {
50 $statusid = $this->reportdata
->sessionslog
[$this->user
->id
][$sess->id
]->statusid
;
51 if (array_key_exists($statusid, $this->reportdata
->statuses
)) {
52 $this->construct_existing_status_cell($this->reportdata
->statuses
[$statusid]->acronym
);
54 $this->construct_hidden_status_cell($this->reportdata
->allstatuses
[$statusid]->acronym
);
57 $this->construct_remarks_cell($this->reportdata
->sessionslog
[$this->user
->id
][$sess->id
]->remarks
);
60 if ($this->user
->enrolmentstart
> $sess->sessdate
) {
61 $starttext = get_string('enrolmentstart', 'attendance', userdate($this->user
->enrolmentstart
, '%d.%m.%Y'));
62 $this->construct_enrolments_info_cell($starttext);
63 } else if ($this->user
->enrolmentend
and $this->user
->enrolmentend
< $sess->sessdate
) {
64 $endtext = get_string('enrolmentend', 'attendance', userdate($this->user
->enrolmentend
, '%d.%m.%Y'));
65 $this->construct_enrolments_info_cell($endtext);
66 } else if (!$this->user
->enrolmentend
and $this->user
->enrolmentstatus
== ENROL_USER_SUSPENDED
) {
67 // No enrolmentend and ENROL_USER_SUSPENDED.
68 $suspendext = get_string('enrolmentsuspended', 'attendance', userdate($this->user
->enrolmentend
, '%d.%m.%Y'));
69 $this->construct_enrolments_info_cell($suspendext);
71 if ($sess->groupid
== 0 or array_key_exists($sess->groupid
, $this->reportdata
->usersgroups
[$this->user
->id
])) {
72 $this->construct_not_taken_cell('?');
74 $this->construct_not_existing_for_user_session_cell('');
78 $this->construct_remarks_cell('');
82 $this->finalize_cells();
87 protected function init_cells() {
91 protected function construct_existing_status_cell($text) {
92 $this->cells
[] = $text;
95 protected function construct_hidden_status_cell($text) {
96 $this->cells
[] = $text;
99 protected function construct_enrolments_info_cell($text) {
100 $this->cells
[] = $text;
103 protected function construct_not_taken_cell($text) {
104 $this->cells
[] = $text;
107 protected function construct_remarks_cell($text) {
108 $this->cells
[] = $text;
111 protected function construct_not_existing_for_user_session_cell($text) {
112 $this->cells
[] = $text;
115 protected function finalize_cells() {
120 * class Template method for generating user's session's cells in html
122 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
123 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
125 class user_sessions_cells_html_generator
extends user_sessions_cells_generator
{
128 protected function construct_existing_status_cell($text) {
129 $this->close_open_cell_if_needed();
130 $this->cells
[] = $text;
133 protected function construct_hidden_status_cell($text) {
134 $this->cells
[] = html_writer
::tag('s', $text);
137 protected function construct_enrolments_info_cell($text) {
138 if (is_null($this->cell
)) {
139 $this->cell
= new html_table_cell($text);
140 $this->cell
->colspan
= 1;
142 if ($this->cell
->text
!= $text) {
143 $this->cells
[] = $this->cell
;
144 $this->cell
= new html_table_cell($text);
145 $this->cell
->colspan
= 1;
147 $this->cell
->colspan++
;
152 private function close_open_cell_if_needed() {
154 $this->cells
[] = $this->cell
;
159 protected function construct_not_taken_cell($text) {
160 $this->close_open_cell_if_needed();
161 $this->cells
[] = $text;
164 protected function construct_remarks_cell($text) {
165 $this->close_open_cell_if_needed();
166 $this->cells
[] = $text;
169 protected function construct_not_existing_for_user_session_cell($text) {
170 $this->close_open_cell_if_needed();
171 $this->cells
[] = $text;
174 protected function finalize_cells() {
176 $this->cells
[] = $this->cell
;
182 * class Template method for generating user's session's cells in text
184 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
185 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
187 class user_sessions_cells_text_generator
extends user_sessions_cells_generator
{
188 private $enrolments_info_cell_text;
190 protected function construct_hidden_status_cell($text) {
191 $this->cells
[] = '-'.$text;
194 protected function construct_enrolments_info_cell($text) {
195 if ($this->enrolments_info_cell_text
!= $text) {
196 $this->enrolments_info_cell_text
= $text;
197 $this->cells
[] = $text;
199 $this->cells
[] = '←';
204 function construct_session_time($datetime, $duration) {
205 $starttime = userdate($datetime, get_string('strftimehm', 'attendance'));
206 $endtime = userdate($datetime +
$duration, get_string('strftimehm', 'attendance'));
208 return $starttime . ($duration > 0 ?
' - ' . $endtime : '');
211 function construct_session_full_date_time($datetime, $duration) {
212 $sessinfo = userdate($datetime, get_string('strftimedmyw', 'attendance'));
213 $sessinfo .= ' '.construct_session_time($datetime, $duration);
218 function construct_user_data_stat($stat, $statuses, $gradable, $grade, $maxgrade, $decimalpoints) {
221 $stattable = new html_table();
222 $stattable->attributes
['class'] = 'attlist';
223 $row = new html_table_row();
224 $row->cells
[] = get_string('sessionscompleted', 'attendance').':';
225 $row->cells
[] = $stat['completed'];
226 $stattable->data
[] = $row;
228 foreach ($statuses as $st) {
229 $row = new html_table_row();
230 $row->cells
[] = $st->description
. ':';
231 $row->cells
[] = array_key_exists($st->id
, $stat['statuses']) ?
$stat['statuses'][$st->id
]->stcnt
: 0;
233 $stattable->data
[] = $row;
237 $row = new html_table_row();
238 $row->cells
[] = get_string('attendancegrade', 'attendance') .
239 $OUTPUT->help_icon('gradebookexplanation', 'attendance') . ':';
240 $row->cells
[] = $grade . ' / ' . $maxgrade;
241 $stattable->data
[] = $row;
243 $row = new html_table_row();
244 $row->cells
[] = get_string('attendancepercent', 'attendance') . ':';
245 if ($maxgrade == 0) {
248 $percent = $grade / $maxgrade * 100;
250 $row->cells
[] = sprintf("%0.{$decimalpoints}f", $percent);
251 $stattable->data
[] = $row;
254 return html_writer
::table($stattable);
257 function construct_full_user_stat_html_table($attendance, $course, $user, $coursemodule) {
259 $gradeable = $attendance->grade
> 0;
260 $statuses = att_get_statuses($attendance->id
);
261 $userstatusesstat = att_get_user_statuses_stat($attendance->id
, $course->startdate
, $user->id
, $coursemodule);
262 $stat['completed'] = att_get_user_taken_sessions_count($attendance->id
, $course->startdate
, $user->id
, $coursemodule);
263 $stat['statuses'] = $userstatusesstat;
265 $grade = att_get_user_grade($userstatusesstat, $statuses);
266 $maxgrade = att_get_user_max_grade(att_get_user_taken_sessions_count($attendance->id
, $course->startdate
,
267 $user->id
, $coursemodule), $statuses);
268 if (!$decimalpoints = grade_get_setting($course->id
, 'decimalpoints')) {
269 $decimalpoints = $CFG->grade_decimalpoints
;
277 return construct_user_data_stat($stat, $statuses,
278 $gradeable, $grade, $maxgrade, $decimalpoints);