This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
renderhelpers.php
252 lines (210 loc) · 8.74 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
<?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
* @subpackage attforblock
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(dirname(__FILE__).'/renderables.php');
/**
* Template method for generating user's session's cells
*/
class user_sessions_cells_generator {
protected $cells = array();
protected $reportdata;
protected $user;
public function __construct(attforblock_report_data $reportdata, $user) {
$this->reportdata = $reportdata;
$this->user = $user;
}
public function get_cells() {
$this->init_cells();
foreach ($this->reportdata->sessions as $sess) {
if (array_key_exists($sess->id, $this->reportdata->sessionslog[$this->user->id])) {
$statusid = $this->reportdata->sessionslog[$this->user->id][$sess->id]->statusid;
if (array_key_exists($statusid, $this->reportdata->statuses)) {
$this->construct_existing_status_cell($this->reportdata->statuses[$statusid]->acronym);
} else {
$this->construct_hidden_status_cell($this->reportdata->allstatuses[$statusid]->acronym);
}
} else {
if ($this->user->enrolmentstart > $sess->sessdate) {
$starttext = get_string('enrolmentstart', 'attforblock', userdate($this->user->enrolmentstart, '%d.%m.%Y'));
$this->construct_enrolments_info_cell($starttext);
}
elseif ($this->user->enrolmentend and $this->user->enrolmentend < $sess->sessdate) {
$endtext = get_string('enrolmentend', 'attforblock', userdate($this->user->enrolmentend, '%d.%m.%Y'));
$this->construct_enrolments_info_cell($endtext);
}
// no enrolmentend and ENROL_USER_SUSPENDED
elseif (!$this->user->enrolmentend and $this->user->enrolmentstatus == ENROL_USER_SUSPENDED) {
$suspendext = get_string('enrolmentsuspended', 'attforblock', userdate($this->user->enrolmentend, '%d.%m.%Y'));
$this->construct_enrolments_info_cell($suspendext);
}
else {
if ($sess->groupid == 0 or 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('');
}
}
}
$this->finalize_cells();
return $this->cells;
}
protected function init_cells() {
}
protected function construct_existing_status_cell($text) {
$this->cells[] = $text;
}
protected function construct_hidden_status_cell($text) {
$this->cells[] = $text;
}
protected function construct_enrolments_info_cell($text) {
$this->cells[] = $text;
}
protected function construct_not_taken_cell($text) {
$this->cells[] = $text;
}
protected function construct_not_existing_for_user_session_cell($text) {
$this->cells[] = $text;
}
protected function finalize_cells() {
}
}
class user_sessions_cells_html_generator extends user_sessions_cells_generator {
private $cell;
protected function construct_existing_status_cell($text) {
$this->close_open_cell_if_needed();
$this->cells[] = $text;
}
protected function construct_hidden_status_cell($text) {
$this->cells[] = html_writer::tag('s', $text);
}
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++;
}
}
private function close_open_cell_if_needed(){
if ($this->cell) {
$this->cells[] = $this->cell;
$this->cell = null;
}
}
protected function construct_not_taken_cell($text) {
$this->close_open_cell_if_needed();
$this->cells[] = $text;
}
protected function construct_not_existing_for_user_session_cell($text) {
$this->close_open_cell_if_needed();
$this->cells[] = $text;
}
protected function finalize_cells() {
if ($this->cell)
$this->cells[] = $this->cell;
}
}
class user_sessions_cells_text_generator extends user_sessions_cells_generator {
private $enrolments_info_cell_text;
protected function construct_hidden_status_cell($text) {
$this->cells[] = '-'.$text;
}
protected function construct_enrolments_info_cell($text) {
if ($this->enrolments_info_cell_text != $text) {
$this->enrolments_info_cell_text = $text;
$this->cells[] = $text;
}
else
$this->cells[] = '←';
}
}
function construct_session_time($datetime, $duration) {
$starttime = userdate($datetime, get_string('strftimehm', 'attforblock'));
$endtime = userdate($datetime + $duration, get_string('strftimehm', 'attforblock'));
return $starttime . ($duration > 0 ? ' - ' . $endtime : '');
}
function construct_session_full_date_time($datetime, $duration) {
$sessinfo = userdate($datetime, get_string('strftimedmyw', 'attforblock'));
$sessinfo .= ' '.construct_session_time($datetime, $duration);
return $sessinfo;
}
function construct_user_data_stat($stat, $statuses, $gradable, $grade, $maxgrade, $decimalpoints) {
global $OUTPUT;
$stattable = new html_table();
$stattable->attributes['class'] = 'attlist';
$row = new html_table_row();
$row->cells[] = get_string('sessionscompleted','attforblock').':';
$row->cells[] = $stat['completed'];
$stattable->data[] = $row;
foreach ($statuses as $st) {
$row = new html_table_row();
$row->cells[] = $st->description . ':';
$row->cells[] = array_key_exists($st->id, $stat['statuses']) ? $stat['statuses'][$st->id]->stcnt : 0;
$stattable->data[] = $row;
}
if ($gradable) {
$row = new html_table_row();
$row->cells[] = get_string('attendancegrade','attforblock') . $OUTPUT->help_icon('gradebookexplanation', 'attforblock') . ':';
$row->cells[] = $grade . ' / ' . $maxgrade;
$stattable->data[] = $row;
$row = new html_table_row();
$row->cells[] = get_string('attendancepercent','attforblock') . ':';
if ($maxgrade == 0) {
$percent = 0;
} else {
$percent = $grade / $maxgrade * 100;
}
$row->cells[] = sprintf("%0.{$decimalpoints}f", $percent);
$stattable->data[] = $row;
}
return html_writer::table($stattable);
}
function construct_full_user_stat_html_table($attforblock, $course, $user) {
global $CFG;
$gradeable = $attforblock->grade > 0;
$statuses = att_get_statuses($attforblock->id);
$userstatusesstat = att_get_user_statuses_stat($attforblock->id, $course->startdate, $user->id);
$stat['completed'] = att_get_user_taken_sessions_count($attforblock->id, $course->startdate, $user->id);
$stat['statuses'] = $userstatusesstat;
if ($gradeable) {
$grade = att_get_user_grade($userstatusesstat, $statuses);
$maxgrade = att_get_user_max_grade(att_get_user_taken_sessions_count($attforblock->id, $course->startdate, $user->id), $statuses);
if (!$decimalpoints = grade_get_setting($course->id, 'decimalpoints')) {
$decimalpoints = $CFG->grade_decimalpoints;
}
}
else {
$grade = 0;
$maxgrade = 0;
$decimalpoints = 0;
}
return construct_user_data_stat($stat, $statuses,
$gradeable, $grade, $maxgrade, $decimalpoints);
}