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 $format = get_string('strftimeh', 'attendance');
322 $format = get_string('strftimehm', 'attendance');
325 $userdate = userdate($time, $format);
327 // Some Lang packs use %p to suffix with AM/PM but not all strftime support this.
328 // Check if %p is in use and make sure it's being respected.
329 if (stripos($format, '%p')) {
330 // Check if $userdate did something with %p by checking userdate against the same format without %p.
331 $formatwithoutp = str_ireplace('%p', '', $format);
332 if (userdate($time, $formatwithoutp) == $userdate) {
333 // The date is the same with and without %p - we have a problem.
334 if (userdate($time, '%H') > 11) {
340 // Some locales and O/S don't respect correct intended case of %p vs %P
341 // This can cause problems with behat which expects AM vs am.
342 if (strpos($format, '%p')) { // Should be upper case according to PHP spec.
343 $userdate = str_replace('am', 'AM', $userdate);
344 $userdate = str_replace('pm', 'PM', $userdate);
352 * Used to print simple time - 1am instead of 1:00am.
354 * @param int $datetime - unix timestamp.
355 * @param int $duration - number of seconds.
357 function construct_session_time($datetime, $duration) {
358 $starttime = attendance_strftimehm($datetime);
359 $endtime = attendance_strftimehm($datetime +
$duration);
361 return $starttime . ($duration > 0 ?
' - ' . $endtime : '');
365 * Used to print session time.
367 * @param int $datetime - unix timestamp.
368 * @param int $duration - number of seconds duration.
371 function construct_session_full_date_time($datetime, $duration) {
372 $sessinfo = userdate($datetime, get_string('strftimedmyw', 'attendance'));
373 $sessinfo .= ' '.construct_session_time($datetime, $duration);
379 * Used to construct user summary.
381 * @param stdclass $usersummary - data for summary.
382 * @param int $view - ATT_VIEW_ALL|ATT_VIEW_
385 function construct_user_data_stat($usersummary, $view) {
386 $stattable = new html_table();
387 $stattable->attributes
['class'] = 'attlist';
388 $row = new html_table_row();
389 $row->attributes
['class'] = 'normal';
390 $row->cells
[] = get_string('sessionscompleted', 'attendance') . ':';
391 $row->cells
[] = $usersummary->numtakensessions
;
392 $stattable->data
[] = $row;
394 $row = new html_table_row();
395 $row->attributes
['class'] = 'normal';
396 $row->cells
[] = get_string('pointssessionscompleted', 'attendance') . ':';
397 $row->cells
[] = format_float($usersummary->takensessionspoints
, 1, true
, true
) . ' / ' .
398 format_float($usersummary->takensessionsmaxpoints
, 1, true
, true
);
399 $stattable->data
[] = $row;
401 $row = new html_table_row();
402 $row->attributes
['class'] = 'normal';
403 $row->cells
[] = get_string('percentagesessionscompleted', 'attendance') . ':';
404 $row->cells
[] = format_float($usersummary->takensessionspercentage
* 100) . '%';
405 $stattable->data
[] = $row;
407 if ($view == ATT_VIEW_ALL
) {
408 $row = new html_table_row();
409 $row->attributes
['class'] = 'highlight';
410 $row->cells
[] = get_string('sessionstotal', 'attendance') . ':';
411 $row->cells
[] = $usersummary->numallsessions
;
412 $stattable->data
[] = $row;
414 $row = new html_table_row();
415 $row->attributes
['class'] = 'highlight';
416 $row->cells
[] = get_string('pointsallsessions', 'attendance') . ':';
417 $row->cells
[] = format_float($usersummary->takensessionspoints
, 1, true
, true
) . ' / ' .
418 format_float($usersummary->allsessionsmaxpoints
, 1, true
, true
);
419 $stattable->data
[] = $row;
421 $row = new html_table_row();
422 $row->attributes
['class'] = 'highlight';
423 $row->cells
[] = get_string('percentageallsessions', 'attendance') . ':';
424 $row->cells
[] = format_float($usersummary->allsessionspercentage
* 100) . '%';
425 $stattable->data
[] = $row;
427 $row = new html_table_row();
428 $row->attributes
['class'] = 'normal';
429 $row->cells
[] = get_string('maxpossiblepoints', 'attendance') . ':';
430 $row->cells
[] = format_float($usersummary->maxpossiblepoints
, 1, true
, true
) . ' / ' .
431 format_float($usersummary->allsessionsmaxpoints
, 1, true
, true
);
432 $stattable->data
[] = $row;
434 $row = new html_table_row();
435 $row->attributes
['class'] = 'normal';
436 $row->cells
[] = get_string('maxpossiblepercentage', 'attendance') . ':';
437 $row->cells
[] = format_float($usersummary->maxpossiblepercentage
* 100) . '%';
438 $stattable->data
[] = $row;
441 return html_writer
::table($stattable);
445 * Returns html user summary
447 * @param stdclass $attendance - attendance record.
448 * @param stdclass $user - user record
452 function construct_full_user_stat_html_table($attendance, $user) {
453 $summary = new mod_attendance_summary($attendance->id
, $user->id
);
454 return construct_user_data_stat($summary->get_all_sessions_summary_for($user->id
), ATT_VIEW_ALL
);