update README
[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[] = $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 $this->close_open_cell_if_needed();
166 $this->cells[] = $text;
167 }
168
169 protected function construct_not_existing_for_user_session_cell($text) {
170 $this->close_open_cell_if_needed();
171 $this->cells[] = $text;
172 }
173
174 protected function finalize_cells() {
175 if ($this->cell) {
176 $this->cells[] = $this->cell;
177 }
178 }
179 }
180
181 /**
182 * class Template method for generating user's session's cells in text
183 *
184 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
185 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
186 */
187 class user_sessions_cells_text_generator extends user_sessions_cells_generator {
188 private $enrolments_info_cell_text;
189
190 protected function construct_hidden_status_cell($text) {
191 $this->cells[] = '-'.$text;
192 }
193
194 protected function construct_enrolments_info_cell($text) {
195 if ($this->enrolments_info_cell_text != $text) {
196 $this->enrolments_info_cell_text = $text;
197 $this->cells[] = $text;
198 } else {
199 $this->cells[] = '←';
200 }
201 }
202 }
203
204 function construct_session_time($datetime, $duration) {
205 $starttime = userdate($datetime, get_string('strftimehm', 'attendance'));
206 $endtime = userdate($datetime + $duration, get_string('strftimehm', 'attendance'));
207
208 return $starttime . ($duration > 0 ? ' - ' . $endtime : '');
209 }
210
211 function construct_session_full_date_time($datetime, $duration) {
212 $sessinfo = userdate($datetime, get_string('strftimedmyw', 'attendance'));
213 $sessinfo .= ' '.construct_session_time($datetime, $duration);
214
215 return $sessinfo;
216 }
217
218 function construct_user_data_stat($stat, $statuses, $gradable, $grade, $maxgrade, $decimalpoints) {
219 global $OUTPUT;
220
221 $stattable = new html_table();
222 $stattable->attributes['class'] = 'attlist';
223 $row = new html_table_row();
224 $row->cells[] = get_string('sessionscompleted', 'attendance').':';
225 $row->cells[] = $stat['completed'];
226 $stattable->data[] = $row;
227
228 foreach ($statuses as $st) {
229 $row = new html_table_row();
230 $row->cells[] = $st->description . ':';
231 $row->cells[] = array_key_exists($st->id, $stat['statuses']) ? $stat['statuses'][$st->id]->stcnt : 0;
232
233 $stattable->data[] = $row;
234 }
235
236 if ($gradable) {
237 $row = new html_table_row();
238 $row->cells[] = get_string('attendancegrade', 'attendance') .
239 $OUTPUT->help_icon('gradebookexplanation', 'attendance') . ':';
240 $row->cells[] = $grade . ' / ' . $maxgrade;
241 $stattable->data[] = $row;
242
243 $row = new html_table_row();
244 $row->cells[] = get_string('attendancepercent', 'attendance') . ':';
245 if ($maxgrade == 0) {
246 $percent = 0;
247 } else {
248 $percent = $grade / $maxgrade * 100;
249 }
250 $row->cells[] = sprintf("%0.{$decimalpoints}f", $percent);
251 $stattable->data[] = $row;
252 }
253
254 return html_writer::table($stattable);
255 }
256
257 function construct_full_user_stat_html_table($attendance, $course, $user, $coursemodule) {
258 global $CFG;
259 $gradeable = $attendance->grade > 0;
260 $statuses = att_get_statuses($attendance->id);
261 $userstatusesstat = att_get_user_statuses_stat($attendance->id, $course->startdate, $user->id, $coursemodule);
262 $stat['completed'] = att_get_user_taken_sessions_count($attendance->id, $course->startdate, $user->id, $coursemodule);
263 $stat['statuses'] = $userstatusesstat;
264 if ($gradeable) {
265 $grade = att_get_user_grade($userstatusesstat, $statuses);
266 $maxgrade = att_get_user_max_grade(att_get_user_taken_sessions_count($attendance->id, $course->startdate,
267 $user->id, $coursemodule), $statuses);
268 if (!$decimalpoints = grade_get_setting($course->id, 'decimalpoints')) {
269 $decimalpoints = $CFG->grade_decimalpoints;
270 }
271 } else {
272 $grade = 0;
273 $maxgrade = 0;
274 $decimalpoints = 0;
275 }
276
277 return construct_user_data_stat($stat, $statuses,
278 $gradeable, $grade, $maxgrade, $decimalpoints);
279 }