bump version.
[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 protected $cells = array();
37
38 protected $reportdata;
39 protected $user;
40
41 public function __construct(attendance_report_data $reportdata, $user) {
42 $this->reportdata = $reportdata;
43 $this->user = $user;
44 }
45
46 public function get_cells($remarks = false) {
47 $this->init_cells();
48 foreach ($this->reportdata->sessions as $sess) {
49 if (array_key_exists($sess->id, $this->reportdata->sessionslog[$this->user->id])) {
50 $statusid = $this->reportdata->sessionslog[$this->user->id][$sess->id]->statusid;
51 if (array_key_exists($statusid, $this->reportdata->statuses)) {
52 $points = format_float($this->reportdata->statuses[$statusid]->grade, 1, true, true);
53 $maxpoints = format_float($sess->maxpoints, 1, true, true);
54 $this->construct_existing_status_cell($this->reportdata->statuses[$statusid]->acronym .
55 " ({$points}/{$maxpoints})");
56 } else {
57 $this->construct_hidden_status_cell($this->reportdata->allstatuses[$statusid]->acronym);
58 }
59 if ($remarks) {
60 $this->construct_remarks_cell($this->reportdata->sessionslog[$this->user->id][$sess->id]->remarks);
61 }
62 } else {
63 if ($this->user->enrolmentstart > $sess->sessdate) {
64 $starttext = get_string('enrolmentstart', 'attendance', userdate($this->user->enrolmentstart, '%d.%m.%Y'));
65 $this->construct_enrolments_info_cell($starttext);
66 } else if ($this->user->enrolmentend and $this->user->enrolmentend < $sess->sessdate) {
67 $endtext = get_string('enrolmentend', 'attendance', userdate($this->user->enrolmentend, '%d.%m.%Y'));
68 $this->construct_enrolments_info_cell($endtext);
69 } else if (!$this->user->enrolmentend and $this->user->enrolmentstatus == ENROL_USER_SUSPENDED) {
70 // No enrolmentend and ENROL_USER_SUSPENDED.
71 $suspendext = get_string('enrolmentsuspended', 'attendance', userdate($this->user->enrolmentend, '%d.%m.%Y'));
72 $this->construct_enrolments_info_cell($suspendext);
73 } else {
74 if ($sess->groupid == 0 or array_key_exists($sess->groupid, $this->reportdata->usersgroups[$this->user->id])) {
75 $this->construct_not_taken_cell('?');
76 } else {
77 $this->construct_not_existing_for_user_session_cell('');
78 }
79 }
80 if ($remarks) {
81 $this->construct_remarks_cell('');
82 }
83 }
84 }
85 $this->finalize_cells();
86
87 return $this->cells;
88 }
89
90 protected function init_cells() {
91
92 }
93
94 protected function construct_existing_status_cell($text) {
95 $this->cells[] = $text;
96 }
97
98 protected function construct_hidden_status_cell($text) {
99 $this->cells[] = $text;
100 }
101
102 protected function construct_enrolments_info_cell($text) {
103 $this->cells[] = $text;
104 }
105
106 protected function construct_not_taken_cell($text) {
107 $this->cells[] = $text;
108 }
109
110 protected function construct_remarks_cell($text) {
111 $this->cells[] = $text;
112 }
113
114 protected function construct_not_existing_for_user_session_cell($text) {
115 $this->cells[] = $text;
116 }
117
118 protected function finalize_cells() {
119 }
120 }
121
122 /**
123 * class Template method for generating user's session's cells in html
124 *
125 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
126 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
127 */
128 class user_sessions_cells_html_generator extends user_sessions_cells_generator {
129 private $cell;
130
131 protected function construct_existing_status_cell($text) {
132 $this->close_open_cell_if_needed();
133 $this->cells[] = html_writer::span($text, 'attendancestatus-'.$text);
134 }
135
136 protected function construct_hidden_status_cell($text) {
137 $this->cells[] = html_writer::tag('s', $text);
138 }
139
140 protected function construct_enrolments_info_cell($text) {
141 if (is_null($this->cell)) {
142 $this->cell = new html_table_cell($text);
143 $this->cell->colspan = 1;
144 } else {
145 if ($this->cell->text != $text) {
146 $this->cells[] = $this->cell;
147 $this->cell = new html_table_cell($text);
148 $this->cell->colspan = 1;
149 } else {
150 $this->cell->colspan++;
151 }
152 }
153 }
154
155 private function close_open_cell_if_needed() {
156 if ($this->cell) {
157 $this->cells[] = $this->cell;
158 $this->cell = null;
159 }
160 }
161
162 protected function construct_not_taken_cell($text) {
163 $this->close_open_cell_if_needed();
164 $this->cells[] = $text;
165 }
166
167 protected function construct_remarks_cell($text) {
168 global $OUTPUT;
169
170 if (!trim($text)) {
171 return;
172 }
173
174 // Format the remark.
175 $icon = $OUTPUT->pix_icon('i/info', '');
176 $remark = html_writer::span($text, 'remarkcontent');
177 $remark = html_writer::span($icon.$remark, 'remarkholder');
178
179 // Add it into the previous cell.
180 $markcell = array_pop($this->cells);
181 $markcell .= ' '.$remark;
182 $this->cells[] = $markcell;
183 }
184
185 protected function construct_not_existing_for_user_session_cell($text) {
186 $this->close_open_cell_if_needed();
187 $this->cells[] = $text;
188 }
189
190 protected function finalize_cells() {
191 if ($this->cell) {
192 $this->cells[] = $this->cell;
193 }
194 }
195 }
196
197 /**
198 * class Template method for generating user's session's cells in text
199 *
200 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
201 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
202 */
203 class user_sessions_cells_text_generator extends user_sessions_cells_generator {
204 private $enrolmentsinfocelltext;
205
206 protected function construct_hidden_status_cell($text) {
207 $this->cells[] = '-'.$text;
208 }
209
210 protected function construct_enrolments_info_cell($text) {
211 if ($this->enrolmentsinfocelltext != $text) {
212 $this->enrolmentsinfocelltext = $text;
213 $this->cells[] = $text;
214 } else {
215 $this->cells[] = '←';
216 }
217 }
218 }
219
220 function construct_session_time($datetime, $duration) {
221 $starttime = userdate($datetime, get_string('strftimehm', 'attendance'));
222 $endtime = userdate($datetime + $duration, get_string('strftimehm', 'attendance'));
223
224 return $starttime . ($duration > 0 ? ' - ' . $endtime : '');
225 }
226
227 function construct_session_full_date_time($datetime, $duration) {
228 $sessinfo = userdate($datetime, get_string('strftimedmyw', 'attendance'));
229 $sessinfo .= ' '.construct_session_time($datetime, $duration);
230
231 return $sessinfo;
232 }
233
234 function construct_user_data_stat($usersummary, $view) {
235 $stattable = new html_table();
236 $stattable->attributes['class'] = 'attlist';
237 $row = new html_table_row();
238 $row->attributes['class'] = 'normal';
239 $row->cells[] = get_string('sessionscompleted', 'attendance') . ':';
240 $row->cells[] = $usersummary->numtakensessions;
241 $stattable->data[] = $row;
242
243 $row = new html_table_row();
244 $row->attributes['class'] = 'normal';
245 $row->cells[] = get_string('pointssessionscompleted', 'attendance') . ':';
246 $row->cells[] = format_float($usersummary->takensessionspoints, 1, true, true) . ' / ' .
247 format_float($usersummary->takensessionsmaxpoints, 1, true, true);
248 $stattable->data[] = $row;
249
250 $row = new html_table_row();
251 $row->attributes['class'] = 'normal';
252 $row->cells[] = get_string('percentagesessionscompleted', 'attendance') . ':';
253 $row->cells[] = format_float($usersummary->takensessionspercentage * 100) . '%';
254 $stattable->data[] = $row;
255
256 if ($view == ATT_VIEW_ALL) {
257 $row = new html_table_row();
258 $row->attributes['class'] = 'highlight';
259 $row->cells[] = get_string('sessionstotal', 'attendance') . ':';
260 $row->cells[] = $usersummary->numallsessions;
261 $stattable->data[] = $row;
262
263 $row = new html_table_row();
264 $row->attributes['class'] = 'highlight';
265 $row->cells[] = get_string('pointsallsessions', 'attendance') . ':';
266 $row->cells[] = format_float($usersummary->takensessionspoints, 1, true, true) . ' / ' .
267 format_float($usersummary->allsessionsmaxpoints, 1, true, true);
268 $stattable->data[] = $row;
269
270 $row = new html_table_row();
271 $row->attributes['class'] = 'highlight';
272 $row->cells[] = get_string('percentageallsessions', 'attendance') . ':';
273 $row->cells[] = format_float($usersummary->allsessionspercentage * 100) . '%';
274 $stattable->data[] = $row;
275
276 $row = new html_table_row();
277 $row->attributes['class'] = 'normal';
278 $row->cells[] = get_string('maxpossiblepoints', 'attendance') . ':';
279 $row->cells[] = format_float($usersummary->maxpossiblepoints, 1, true, true) . ' / ' .
280 format_float($usersummary->allsessionsmaxpoints, 1, true, true);
281 $stattable->data[] = $row;
282
283 $row = new html_table_row();
284 $row->attributes['class'] = 'normal';
285 $row->cells[] = get_string('maxpossiblepercentage', 'attendance') . ':';
286 $row->cells[] = format_float($usersummary->maxpossiblepercentage * 100) . '%';
287 $stattable->data[] = $row;
288 }
289
290 return html_writer::table($stattable);
291 }
292
293 function construct_full_user_stat_html_table($attendance, $user) {
294 $summary = new mod_attendance_summary($attendance->id, $user->id);
295 return construct_user_data_stat($summary->get_all_sessions_summary_for($user->id), ATT_VIEW_ALL);
296 }