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 /** @var array $cells - list of table cells. */
37 protected $cells = array();
39 /** @var stdClass $reportdata - data for report. */
40 protected $reportdata;
42 /** @var stdClass $user - user record. */
47 * @param attendance_report_data $reportdata - reportdata.
48 * @param stdClass $user - user record.
50 public function __construct(attendance_report_data
$reportdata, $user) {
51 $this->reportdata
= $reportdata;
56 * Get cells for the table.
58 * @param boolean $remarks - include remarks cell.
60 public function get_cells($remarks = false
) {
61 foreach ($this->reportdata
->sessions
as $sess) {
62 if (array_key_exists($sess->id
, $this->reportdata
->sessionslog
[$this->user
->id
]) &&
63 !empty($this->reportdata
->sessionslog
[$this->user
->id
][$sess->id
]->statusid
)) {
64 $statusid = $this->reportdata
->sessionslog
[$this->user
->id
][$sess->id
]->statusid
;
65 if (array_key_exists($statusid, $this->reportdata
->statuses
)) {
66 $points = format_float($this->reportdata
->statuses
[$statusid]->grade
, 1, true
, true
);
67 $maxpoints = format_float($sess->maxpoints
, 1, true
, true
);
68 $this->construct_existing_status_cell($this->reportdata
->statuses
[$statusid]->acronym
.
69 " ({$points}/{$maxpoints})");
71 $this->construct_hidden_status_cell($this->reportdata
->allstatuses
[$statusid]->acronym
);
74 $this->construct_remarks_cell($this->reportdata
->sessionslog
[$this->user
->id
][$sess->id
]->remarks
);
77 if ($this->user
->enrolmentstart
> $sess->sessdate
) {
78 $starttext = get_string('enrolmentstart', 'attendance', userdate($this->user
->enrolmentstart
, '%d.%m.%Y'));
79 $this->construct_enrolments_info_cell($starttext);
80 } else if ($this->user
->enrolmentend
and $this->user
->enrolmentend
< $sess->sessdate
) {
81 $endtext = get_string('enrolmentend', 'attendance', userdate($this->user
->enrolmentend
, '%d.%m.%Y'));
82 $this->construct_enrolments_info_cell($endtext);
83 } else if (!$this->user
->enrolmentend
and $this->user
->enrolmentstatus
== ENROL_USER_SUSPENDED
) {
84 // No enrolmentend and ENROL_USER_SUSPENDED.
85 $suspendext = get_string('enrolmentsuspended', 'attendance', userdate($this->user
->enrolmentend
, '%d.%m.%Y'));
86 $this->construct_enrolments_info_cell($suspendext);
88 if ($sess->groupid
== 0 or array_key_exists($sess->groupid
, $this->reportdata
->usersgroups
[$this->user
->id
])) {
89 $this->construct_not_taken_cell('?');
91 $this->construct_not_existing_for_user_session_cell('');
95 $this->construct_remarks_cell('');
99 $this->finalize_cells();
105 * Construct status cell.
107 * @param string $text - text for the cell.
109 protected function construct_existing_status_cell($text) {
110 $this->cells
[] = $text;
114 * Construct hidden status cell.
116 * @param string $text - text for the cell.
118 protected function construct_hidden_status_cell($text) {
119 $this->cells
[] = $text;
123 * Construct enrolments info cell.
125 * @param string $text - text for the cell.
127 protected function construct_enrolments_info_cell($text) {
128 $this->cells
[] = $text;
132 * Construct not taken cell.
134 * @param string $text - text for the cell.
136 protected function construct_not_taken_cell($text) {
137 $this->cells
[] = $text;
141 * Construct remarks cell.
143 * @param string $text - text for the cell.
145 protected function construct_remarks_cell($text) {
146 $this->cells
[] = $text;
150 * Construct not existing user session cell.
152 * @param string $text - text for the cell.
154 protected function construct_not_existing_for_user_session_cell($text) {
155 $this->cells
[] = $text;
159 * Dummy stub method, called at the end. - override if you need/
161 protected function finalize_cells() {
166 * class Template method for generating user's session's cells in html
168 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
169 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
171 class user_sessions_cells_html_generator
extends user_sessions_cells_generator
{
172 /** @var html_table_cell $cell */
176 * Construct status cell.
178 * @param string $text - text for the cell.
180 protected function construct_existing_status_cell($text) {
181 $this->close_open_cell_if_needed();
182 $this->cells
[] = html_writer
::span($text, 'attendancestatus-'.$text);
186 * Construct hidden status cell.
188 * @param string $text - text for the cell.
190 protected function construct_hidden_status_cell($text) {
191 $this->cells
[] = html_writer
::tag('s', $text);
195 * Construct enrolments info cell.
197 * @param string $text - text for the cell.
199 protected function construct_enrolments_info_cell($text) {
200 if (is_null($this->cell
)) {
201 $this->cell
= new html_table_cell($text);
202 $this->cell
->colspan
= 1;
204 if ($this->cell
->text
!= $text) {
205 $this->cells
[] = $this->cell
;
206 $this->cell
= new html_table_cell($text);
207 $this->cell
->colspan
= 1;
209 $this->cell
->colspan++
;
215 * Close cell if needed.
217 private function close_open_cell_if_needed() {
219 $this->cells
[] = $this->cell
;
225 * Construct not taken cell.
227 * @param string $text - text for the cell.
229 protected function construct_not_taken_cell($text) {
230 $this->close_open_cell_if_needed();
231 $this->cells
[] = $text;
235 * Construct remarks cell.
237 * @param string $text - text for the cell.
239 protected function construct_remarks_cell($text) {
246 // Format the remark.
247 $icon = $OUTPUT->pix_icon('i/info', '');
248 $remark = html_writer
::span($text, 'remarkcontent');
249 $remark = html_writer
::span($icon.$remark, 'remarkholder');
251 // Add it into the previous cell.
252 $markcell = array_pop($this->cells
);
253 $markcell .= ' '.$remark;
254 $this->cells
[] = $markcell;
258 * Construct not existing for user session cell.
260 * @param string $text - text for the cell.
262 protected function construct_not_existing_for_user_session_cell($text) {
263 $this->close_open_cell_if_needed();
264 $this->cells
[] = $text;
271 protected function finalize_cells() {
273 $this->cells
[] = $this->cell
;
279 * class Template method for generating user's session's cells in text
281 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
282 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
284 class user_sessions_cells_text_generator
extends user_sessions_cells_generator
{
285 /** @var string $enrolmentsinfocelltext. */
286 private $enrolmentsinfocelltext;
289 * Construct hidden status cell.
291 * @param string $text - text for the cell.
293 protected function construct_hidden_status_cell($text) {
294 $this->cells
[] = '-'.$text;
298 * Construct enrolments info cell.
300 * @param string $text - text for the cell.
302 protected function construct_enrolments_info_cell($text) {
303 if ($this->enrolmentsinfocelltext
!= $text) {
304 $this->enrolmentsinfocelltext
= $text;
305 $this->cells
[] = $text;
307 $this->cells
[] = '←';
313 * Used to print simple time - 1am instead of 1:00am.
315 * @param int $time - unix timestamp.
317 function attendance_strftimehm($time) {
318 $mins = userdate($time, '%M');
320 return userdate($time, get_string('strftimeh', 'attendance'));
322 return userdate($time, get_string('strftimehm', 'attendance'));
327 * Used to print simple time - 1am instead of 1:00am.
329 * @param int $datetime - unix timestamp.
330 * @param int $duration - number of seconds.
332 function construct_session_time($datetime, $duration) {
333 $starttime = attendance_strftimehm($datetime);
334 $endtime = attendance_strftimehm($datetime +
$duration);
336 return $starttime . ($duration > 0 ?
' - ' . $endtime : '');
340 * Used to print session time.
342 * @param int $datetime - unix timestamp.
343 * @param int $duration - number of seconds duration.
346 function construct_session_full_date_time($datetime, $duration) {
347 $sessinfo = userdate($datetime, get_string('strftimedmyw', 'attendance'));
348 $sessinfo .= ' '.construct_session_time($datetime, $duration);
354 * Used to construct user summary.
356 * @param stdclass $usersummary - data for summary.
357 * @param int $view - ATT_VIEW_ALL|ATT_VIEW_
360 function construct_user_data_stat($usersummary, $view) {
361 $stattable = new html_table();
362 $stattable->attributes
['class'] = 'attlist';
363 $row = new html_table_row();
364 $row->attributes
['class'] = 'normal';
365 $row->cells
[] = get_string('sessionscompleted', 'attendance') . ':';
366 $row->cells
[] = $usersummary->numtakensessions
;
367 $stattable->data
[] = $row;
369 $row = new html_table_row();
370 $row->attributes
['class'] = 'normal';
371 $row->cells
[] = get_string('pointssessionscompleted', 'attendance') . ':';
372 $row->cells
[] = format_float($usersummary->takensessionspoints
, 1, true
, true
) . ' / ' .
373 format_float($usersummary->takensessionsmaxpoints
, 1, true
, true
);
374 $stattable->data
[] = $row;
376 $row = new html_table_row();
377 $row->attributes
['class'] = 'normal';
378 $row->cells
[] = get_string('percentagesessionscompleted', 'attendance') . ':';
379 $row->cells
[] = format_float($usersummary->takensessionspercentage
* 100) . '%';
380 $stattable->data
[] = $row;
382 if ($view == ATT_VIEW_ALL
) {
383 $row = new html_table_row();
384 $row->attributes
['class'] = 'highlight';
385 $row->cells
[] = get_string('sessionstotal', 'attendance') . ':';
386 $row->cells
[] = $usersummary->numallsessions
;
387 $stattable->data
[] = $row;
389 $row = new html_table_row();
390 $row->attributes
['class'] = 'highlight';
391 $row->cells
[] = get_string('pointsallsessions', 'attendance') . ':';
392 $row->cells
[] = format_float($usersummary->takensessionspoints
, 1, true
, true
) . ' / ' .
393 format_float($usersummary->allsessionsmaxpoints
, 1, true
, true
);
394 $stattable->data
[] = $row;
396 $row = new html_table_row();
397 $row->attributes
['class'] = 'highlight';
398 $row->cells
[] = get_string('percentageallsessions', 'attendance') . ':';
399 $row->cells
[] = format_float($usersummary->allsessionspercentage
* 100) . '%';
400 $stattable->data
[] = $row;
402 $row = new html_table_row();
403 $row->attributes
['class'] = 'normal';
404 $row->cells
[] = get_string('maxpossiblepoints', 'attendance') . ':';
405 $row->cells
[] = format_float($usersummary->maxpossiblepoints
, 1, true
, true
) . ' / ' .
406 format_float($usersummary->allsessionsmaxpoints
, 1, true
, true
);
407 $stattable->data
[] = $row;
409 $row = new html_table_row();
410 $row->attributes
['class'] = 'normal';
411 $row->cells
[] = get_string('maxpossiblepercentage', 'attendance') . ':';
412 $row->cells
[] = format_float($usersummary->maxpossiblepercentage
* 100) . '%';
413 $stattable->data
[] = $row;
416 return html_writer
::table($stattable);
420 * Returns html user summary
422 * @param stdclass $attendance - attendance record.
423 * @param stdclass $user - user record
427 function construct_full_user_stat_html_table($attendance, $user) {
428 $summary = new mod_attendance_summary($attendance->id
, $user->id
);
429 return construct_user_data_stat($summary->get_all_sessions_summary_for($user->id
), ATT_VIEW_ALL
);