Implement data privacy provider.
[moodle-mod_attendance.git] / renderhelpers.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 renderering helpers
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__).'/renderables.php');
28
29 /**
30 * class Template method for generating user's session's cells
31 *
32 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35 class user_sessions_cells_generator {
36 /** @var array $cells - list of table cells. */
37 protected $cells = array();
38
39 /** @var stdClass $reportdata - data for report. */
40 protected $reportdata;
41
42 /** @var stdClass $user - user record. */
43 protected $user;
44
45 /**
46 * Set up params.
47 * @param attendance_report_data $reportdata - reportdata.
48 * @param stdClass $user - user record.
49 */
50 public function __construct(attendance_report_data $reportdata, $user) {
51 $this->reportdata = $reportdata;
52 $this->user = $user;
53 }
54
55 /**
56 * Get cells for the table.
57 *
58 * @param boolean $remarks - include remarks cell.
59 */
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})");
70 } else {
71 $this->construct_hidden_status_cell($this->reportdata->allstatuses[$statusid]->acronym);
72 }
73 if ($remarks) {
74 $this->construct_remarks_cell($this->reportdata->sessionslog[$this->user->id][$sess->id]->remarks);
75 }
76 } else {
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);
87 } else {
88 if ($sess->groupid == 0 or array_key_exists($sess->groupid, $this->reportdata->usersgroups[$this->user->id])) {
89 $this->construct_not_taken_cell('?');
90 } else {
91 $this->construct_not_existing_for_user_session_cell('');
92 }
93 }
94 if ($remarks) {
95 $this->construct_remarks_cell('');
96 }
97 }
98 }
99 $this->finalize_cells();
100
101 return $this->cells;
102 }
103
104 /**
105 * Construct status cell.
106 *
107 * @param string $text - text for the cell.
108 */
109 protected function construct_existing_status_cell($text) {
110 $this->cells[] = $text;
111 }
112
113 /**
114 * Construct hidden status cell.
115 *
116 * @param string $text - text for the cell.
117 */
118 protected function construct_hidden_status_cell($text) {
119 $this->cells[] = $text;
120 }
121
122 /**
123 * Construct enrolments info cell.
124 *
125 * @param string $text - text for the cell.
126 */
127 protected function construct_enrolments_info_cell($text) {
128 $this->cells[] = $text;
129 }
130
131 /**
132 * Construct not taken cell.
133 *
134 * @param string $text - text for the cell.
135 */
136 protected function construct_not_taken_cell($text) {
137 $this->cells[] = $text;
138 }
139
140 /**
141 * Construct remarks cell.
142 *
143 * @param string $text - text for the cell.
144 */
145 protected function construct_remarks_cell($text) {
146 $this->cells[] = $text;
147 }
148
149 /**
150 * Construct not existing user session cell.
151 *
152 * @param string $text - text for the cell.
153 */
154 protected function construct_not_existing_for_user_session_cell($text) {
155 $this->cells[] = $text;
156 }
157
158 /**
159 * Dummy stub method, called at the end. - override if you need/
160 */
161 protected function finalize_cells() {
162 }
163 }
164
165 /**
166 * class Template method for generating user's session's cells in html
167 *
168 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
169 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
170 */
171 class user_sessions_cells_html_generator extends user_sessions_cells_generator {
172 /** @var html_table_cell $cell */
173 private $cell;
174
175 /**
176 * Construct status cell.
177 *
178 * @param string $text - text for the cell.
179 */
180 protected function construct_existing_status_cell($text) {
181 $this->close_open_cell_if_needed();
182 $this->cells[] = html_writer::span($text, 'attendancestatus-'.$text);
183 }
184
185 /**
186 * Construct hidden status cell.
187 *
188 * @param string $text - text for the cell.
189 */
190 protected function construct_hidden_status_cell($text) {
191 $this->cells[] = html_writer::tag('s', $text);
192 }
193
194 /**
195 * Construct enrolments info cell.
196 *
197 * @param string $text - text for the cell.
198 */
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;
203 } else {
204 if ($this->cell->text != $text) {
205 $this->cells[] = $this->cell;
206 $this->cell = new html_table_cell($text);
207 $this->cell->colspan = 1;
208 } else {
209 $this->cell->colspan++;
210 }
211 }
212 }
213
214 /**
215 * Close cell if needed.
216 */
217 private function close_open_cell_if_needed() {
218 if ($this->cell) {
219 $this->cells[] = $this->cell;
220 $this->cell = null;
221 }
222 }
223
224 /**
225 * Construct not taken cell.
226 *
227 * @param string $text - text for the cell.
228 */
229 protected function construct_not_taken_cell($text) {
230 $this->close_open_cell_if_needed();
231 $this->cells[] = $text;
232 }
233
234 /**
235 * Construct remarks cell.
236 *
237 * @param string $text - text for the cell.
238 */
239 protected function construct_remarks_cell($text) {
240 global $OUTPUT;
241
242 if (!trim($text)) {
243 return;
244 }
245
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');
250
251 // Add it into the previous cell.
252 $markcell = array_pop($this->cells);
253 $markcell .= ' '.$remark;
254 $this->cells[] = $markcell;
255 }
256
257 /**
258 * Construct not existing for user session cell.
259 *
260 * @param string $text - text for the cell.
261 */
262 protected function construct_not_existing_for_user_session_cell($text) {
263 $this->close_open_cell_if_needed();
264 $this->cells[] = $text;
265 }
266
267 /**
268 * Finalize cells.
269 *
270 */
271 protected function finalize_cells() {
272 if ($this->cell) {
273 $this->cells[] = $this->cell;
274 }
275 }
276 }
277
278 /**
279 * class Template method for generating user's session's cells in text
280 *
281 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
282 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
283 */
284 class user_sessions_cells_text_generator extends user_sessions_cells_generator {
285 /** @var string $enrolmentsinfocelltext. */
286 private $enrolmentsinfocelltext;
287
288 /**
289 * Construct hidden status cell.
290 *
291 * @param string $text - text for the cell.
292 */
293 protected function construct_hidden_status_cell($text) {
294 $this->cells[] = '-'.$text;
295 }
296
297 /**
298 * Construct enrolments info cell.
299 *
300 * @param string $text - text for the cell.
301 */
302 protected function construct_enrolments_info_cell($text) {
303 if ($this->enrolmentsinfocelltext != $text) {
304 $this->enrolmentsinfocelltext = $text;
305 $this->cells[] = $text;
306 } else {
307 $this->cells[] = '←';
308 }
309 }
310 }
311
312 /**
313 * Used to print simple time - 1am instead of 1:00am.
314 *
315 * @param int $time - unix timestamp.
316 */
317 function attendance_strftimehm($time) {
318 $mins = userdate($time, '%M');
319 if ($mins == '00') {
320 $format = get_string('strftimeh', 'attendance');
321 } else {
322 $format = get_string('strftimehm', 'attendance');
323 }
324
325 $userdate = userdate($time, $format);
326
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) {
335 $userdate .= 'pm';
336 } else {
337 $userdate .= 'am';
338 }
339 }
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);
345 }
346 }
347
348 return $userdate;
349 }
350
351 /**
352 * Used to print simple time - 1am instead of 1:00am.
353 *
354 * @param int $datetime - unix timestamp.
355 * @param int $duration - number of seconds.
356 */
357 function construct_session_time($datetime, $duration) {
358 $starttime = attendance_strftimehm($datetime);
359 $endtime = attendance_strftimehm($datetime + $duration);
360
361 return $starttime . ($duration > 0 ? ' - ' . $endtime : '');
362 }
363
364 /**
365 * Used to print session time.
366 *
367 * @param int $datetime - unix timestamp.
368 * @param int $duration - number of seconds duration.
369 * @return string.
370 */
371 function construct_session_full_date_time($datetime, $duration) {
372 $sessinfo = userdate($datetime, get_string('strftimedmyw', 'attendance'));
373 $sessinfo .= ' '.construct_session_time($datetime, $duration);
374
375 return $sessinfo;
376 }
377
378 /**
379 * Used to construct user summary.
380 *
381 * @param stdclass $usersummary - data for summary.
382 * @param int $view - ATT_VIEW_ALL|ATT_VIEW_
383 * @return string.
384 */
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;
393
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;
400
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;
406
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;
413
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;
420
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;
426
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;
433
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;
439 }
440
441 return html_writer::table($stattable);
442 }
443
444 /**
445 * Returns html user summary
446 *
447 * @param stdclass $attendance - attendance record.
448 * @param stdclass $user - user record
449 * @return string.
450 *
451 */
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);
455 }