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