forked from danmarsden/moodle-mod_attendance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderhelpers.php
executable file
·389 lines (350 loc) · 12.8 KB
/
renderhelpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Attendance module renderering helpers
*
* @package mod_attendance
* @copyright 2011 Artem Andreev <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* class Template method for generating user's session's cells
*
* @copyright 2011 Artem Andreev <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_sessions_cells_generator {
/** @var array $cells - list of table cells. */
protected $cells = array();
/** @var stdClass $reportdata - data for report. */
protected $reportdata;
/** @var stdClass $user - user record. */
protected $user;
/**
* Set up params.
* @param mod_attendance\output\report_data $reportdata - reportdata.
* @param stdClass $user - user record.
*/
public function __construct(mod_attendance\output\report_data $reportdata, $user) {
$this->reportdata = $reportdata;
$this->user = $user;
}
/**
* Get cells for the table.
*
* @param boolean $remarks - include remarks cell.
*/
public function get_cells($remarks = false) {
foreach ($this->reportdata->sessions as $sess) {
if (array_key_exists($sess->id, $this->reportdata->sessionslog[$this->user->id]) &&
!empty($this->reportdata->sessionslog[$this->user->id][$sess->id]->statusid)) {
$statusid = $this->reportdata->sessionslog[$this->user->id][$sess->id]->statusid;
if (array_key_exists($statusid, $this->reportdata->statuses)) {
$points = format_float($this->reportdata->statuses[$statusid]->grade, 1, true, true);
$maxpoints = format_float($sess->maxpoints, 1, true, true);
$this->construct_existing_status_cell($this->reportdata->statuses[$statusid]->acronym .
" ({$points}/{$maxpoints})");
} else {
if (!empty($this->reportdata->allstatuses[$statusid] &&
isset($this->reportdata->allstatuses[$statusid]->acronym))) {
$statusac = $this->reportdata->allstatuses[$statusid]->acronym;
} else {
$statusac = get_string('unknownstatus', 'mod_attendance', $statusid);
}
$this->construct_hidden_status_cell($statusac);
}
if ($remarks) {
$this->construct_remarks_cell($this->reportdata->sessionslog[$this->user->id][$sess->id]->remarks);
}
} else {
if ($this->user->enrolmentstart > ($sess->sessdate + $sess->duration)) {
$starttext = get_string('enrolmentstart', 'attendance', userdate($this->user->enrolmentstart, '%d.%m.%Y'));
$this->construct_enrolments_info_cell($starttext);
} else if ($this->user->enrolmentend && $this->user->enrolmentend < $sess->sessdate) {
$endtext = get_string('enrolmentend', 'attendance', userdate($this->user->enrolmentend, '%d.%m.%Y'));
$this->construct_enrolments_info_cell($endtext);
} else if (!$this->user->enrolmentend && $this->user->enrolmentstatus == ENROL_USER_SUSPENDED) {
// No enrolmentend and ENROL_USER_SUSPENDED.
$suspendext = get_string('enrolmentsuspended', 'attendance', userdate($this->user->enrolmentend, '%d.%m.%Y'));
$this->construct_enrolments_info_cell($suspendext);
} else {
if ($sess->groupid == 0 || array_key_exists($sess->groupid, $this->reportdata->usersgroups[$this->user->id])) {
$this->construct_not_taken_cell('?');
} else {
$this->construct_not_existing_for_user_session_cell('');
}
}
if ($remarks) {
$this->construct_remarks_cell('');
}
}
}
$this->finalize_cells();
return $this->cells;
}
/**
* Construct status cell.
*
* @param string $text - text for the cell.
*/
protected function construct_existing_status_cell($text) {
$this->cells[] = $text;
}
/**
* Construct hidden status cell.
*
* @param string $text - text for the cell.
*/
protected function construct_hidden_status_cell($text) {
$this->cells[] = $text;
}
/**
* Construct enrolments info cell.
*
* @param string $text - text for the cell.
*/
protected function construct_enrolments_info_cell($text) {
$this->cells[] = $text;
}
/**
* Construct not taken cell.
*
* @param string $text - text for the cell.
*/
protected function construct_not_taken_cell($text) {
$this->cells[] = $text;
}
/**
* Construct remarks cell.
*
* @param string $text - text for the cell.
*/
protected function construct_remarks_cell($text) {
$this->cells[] = $text;
}
/**
* Construct not existing user session cell.
*
* @param string $text - text for the cell.
*/
protected function construct_not_existing_for_user_session_cell($text) {
$this->cells[] = $text;
}
/**
* Dummy stub method, called at the end. - override if you need/
*/
protected function finalize_cells() {
}
}
/**
* class Template method for generating user's session's cells in html
*
* @copyright 2011 Artem Andreev <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_sessions_cells_html_generator extends user_sessions_cells_generator {
/** @var html_table_cell $cell */
private $cell;
/**
* Construct status cell.
*
* @param string $text - text for the cell.
*/
protected function construct_existing_status_cell($text) {
$this->close_open_cell_if_needed();
$this->cells[] = html_writer::span($text, 'attendancestatus-'.$text);
}
/**
* Construct hidden status cell.
*
* @param string $text - text for the cell.
*/
protected function construct_hidden_status_cell($text) {
$this->cells[] = html_writer::tag('s', $text);
}
/**
* Construct enrolments info cell.
*
* @param string $text - text for the cell.
*/
protected function construct_enrolments_info_cell($text) {
if (is_null($this->cell)) {
$this->cell = new html_table_cell($text);
$this->cell->colspan = 1;
} else {
if ($this->cell->text != $text) {
$this->cells[] = $this->cell;
$this->cell = new html_table_cell($text);
$this->cell->colspan = 1;
} else {
$this->cell->colspan++;
}
}
}
/**
* Close cell if needed.
*/
private function close_open_cell_if_needed() {
if ($this->cell) {
$this->cells[] = $this->cell;
$this->cell = null;
}
}
/**
* Construct not taken cell.
*
* @param string $text - text for the cell.
*/
protected function construct_not_taken_cell($text) {
$this->close_open_cell_if_needed();
$this->cells[] = $text;
}
/**
* Construct remarks cell.
*
* @param string $text - text for the cell.
*/
protected function construct_remarks_cell($text) {
global $OUTPUT;
if (!trim($text)) {
return;
}
// Format the remark.
$icon = $OUTPUT->pix_icon('i/info', '');
$remark = html_writer::span($text, 'remarkcontent');
$remark = html_writer::span($icon.$remark, 'remarkholder');
// Add it into the previous cell.
$markcell = array_pop($this->cells);
$markcell .= ' '.$remark;
$this->cells[] = $markcell;
}
/**
* Construct not existing for user session cell.
*
* @param string $text - text for the cell.
*/
protected function construct_not_existing_for_user_session_cell($text) {
$this->close_open_cell_if_needed();
$this->cells[] = $text;
}
/**
* Finalize cells.
*
*/
protected function finalize_cells() {
if ($this->cell) {
$this->cells[] = $this->cell;
}
}
}
/**
* class Template method for generating user's session's cells in text
*
* @copyright 2011 Artem Andreev <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_sessions_cells_text_generator extends user_sessions_cells_generator {
/** @var string $enrolmentsinfocelltext. */
private $enrolmentsinfocelltext;
/**
* Construct hidden status cell.
*
* @param string $text - text for the cell.
*/
protected function construct_hidden_status_cell($text) {
$this->cells[] = '-'.$text;
}
/**
* Construct enrolments info cell.
*
* @param string $text - text for the cell.
*/
protected function construct_enrolments_info_cell($text) {
if ($this->enrolmentsinfocelltext != $text) {
$this->enrolmentsinfocelltext = $text;
$this->cells[] = $text;
} else {
$this->cells[] = '←';
}
}
}
/**
* Used to construct user summary.
*
* @param stdclass $usersummary - data for summary.
* @param int $view - ATT_VIEW_ALL|ATT_VIEW_
* @return string.
*/
function construct_user_data_stat($usersummary, $view) {
$stattable = new html_table();
$stattable->attributes['class'] = 'attlist';
$row = new html_table_row();
$row->attributes['class'] = 'normal';
$row->cells[] = get_string('sessionscompleted', 'attendance') . ':';
$row->cells[] = $usersummary->numtakensessions;
$stattable->data[] = $row;
$row = new html_table_row();
$row->attributes['class'] = 'normal';
$row->cells[] = get_string('pointssessionscompleted', 'attendance') . ':';
$row->cells[] = $usersummary->pointssessionscompleted;
$stattable->data[] = $row;
$row = new html_table_row();
$row->attributes['class'] = 'normal';
$row->cells[] = get_string('percentagesessionscompleted', 'attendance') . ':';
$row->cells[] = $usersummary->percentagesessionscompleted;
$stattable->data[] = $row;
if ($view == ATT_VIEW_ALL) {
$row = new html_table_row();
$row->attributes['class'] = 'highlight';
$row->cells[] = get_string('sessionstotal', 'attendance') . ':';
$row->cells[] = $usersummary->numallsessions;
$stattable->data[] = $row;
$row = new html_table_row();
$row->attributes['class'] = 'highlight';
$row->cells[] = get_string('pointsallsessions', 'attendance') . ':';
$row->cells[] = $usersummary->pointsallsessions;
$stattable->data[] = $row;
$row = new html_table_row();
$row->attributes['class'] = 'highlight';
$row->cells[] = get_string('percentageallsessions', 'attendance') . ':';
$row->cells[] = $usersummary->allsessionspercentage;
$stattable->data[] = $row;
$row = new html_table_row();
$row->attributes['class'] = 'normal';
$row->cells[] = get_string('maxpossiblepoints', 'attendance') . ':';
$row->cells[] = $usersummary->maxpossiblepoints;
$stattable->data[] = $row;
$row = new html_table_row();
$row->attributes['class'] = 'normal';
$row->cells[] = get_string('maxpossiblepercentage', 'attendance') . ':';
$row->cells[] = $usersummary->maxpossiblepercentage;
$stattable->data[] = $row;
}
return html_writer::table($stattable);
}
/**
* Returns html user summary
*
* @param stdclass $attendance - attendance record.
* @param stdclass $user - user record
* @return string.
*
*/
function construct_full_user_stat_html_table($attendance, $user) {
$summary = new mod_attendance_summary($attendance->id, $user->id);
return construct_user_data_stat($summary->get_all_sessions_summary_for($user->id), ATT_VIEW_ALL);
}