-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattendances.php
245 lines (212 loc) · 10.6 KB
/
attendances.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
<?PHP // $Id: attendances.php,v 1.2.2.5 2009/02/23 19:22:40 dlnsk Exp $
// Lists all the sessions for a course
require_once('../../config.php');
require_once($CFG->libdir.'/blocklib.php');
require_once('locallib.php');
require_once('lib.php');
require_once('../../enrol/locallib.php'); // to get a list of enrolled students
if (!function_exists('grade_update')) { //workaround for buggy PHP versions
require_once($CFG->libdir.'/gradelib.php');
}
$id = required_param('id', PARAM_INT);
$sessionid = required_param('sessionid', PARAM_INT);
$group = optional_param('group', -1, PARAM_INT); // Group to show
$sort = optional_param('sort', 'lastname', PARAM_ALPHA);
$url = new moodle_url('/mod/attforblock/attendances.php');
if (! $cm = $DB->get_record('course_modules', array('id' => $id))) {
print_error('Course Module ID was incorrect');
}
if (! $course = $DB->get_record('course', array('id' => $cm->course))) {
print_error('Course is misconfigured');
}
$url->param($id);
$url->param($sessionid);
$url->param($group);
$url->param($sort);
$PAGE->set_url($url);
require_login($course->id);
if (! $attforblock = $DB->get_record('attforblock', array('id' => $cm->instance))) {
print_error("Course module is incorrect");
}
if (! $user = $DB->get_record('user', array('id' => $USER->id)) ) {
print_error("No such user in this course");
}
if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
print_error('badcontext');
}
$statlist = implode(',', array_keys( (array)get_statuses($course->id) ));
if ($form = data_submitted()) {
$students = array(); // stores students ids
$formarr = (array)$form;
$i = 0;
$now = time();
foreach($formarr as $key => $value) {
if(substr($key,0,7) == 'student' && $value !== '') {
$students[$i] = new stdClass();
$sid = substr($key,7); // gets studeent id from radiobutton name
$students[$i]->studentid = $sid;
$students[$i]->statusid = intval($value);
$students[$i]->statusset = $statlist;
$students[$i]->remarks = array_key_exists('remarks'.$sid, $formarr) ? $formarr['remarks'.$sid] : '';
$students[$i]->sessionid = $sessionid;
$students[$i]->timetaken = $now;
$students[$i]->takenby = $USER->id;
$i++;
}
}
$attforblockrecord = $DB->get_record('attforblock', array('course' => $course->id));
foreach($students as $student) {
if ($log = $DB->get_record('attendance_log', array('sessionid' => $sessionid, 'studentid' => $student->studentid))) {
$student->id = $log->id; // this is id of log
$DB->update_record('attendance_log', $student);
} else {
$DB->insert_record('attendance_log', $student);
}
}
$DB->set_field('attendance_sessions', 'lasttaken', $now, array('id' => $sessionid));
$DB->set_field('attendance_sessions', 'lasttakenby', $USER->id, array('id' => $sessionid));
attforblock_update_grades($attforblockrecord);
add_to_log($course->id, 'attendance', 'updated', 'mod/attforblock/report.php?id='.$id, $user->lastname.' '.$user->firstname);
redirect('manage.php?id='.$id, get_string('attendancesuccess','attforblock'), 3);
exit();
}
/// Print headers
$navlinks[] = array('name' => $attforblock->name, 'link' => "view.php?id=$id", 'type' => 'activity');
$navlinks[] = array('name' => get_string('update', 'attforblock'), 'link' => null, 'type' => 'activityinstance');
print_header("$course->shortname: ".$attforblock->name.' - ' .get_string('update','attforblock'), $course->fullname,
$navlinks, "", "", true, " ", navmenu($course));
//check for hack
if (!$sessdata = $DB->get_record('attendance_sessions', array('id' => $sessionid))) {
print_error("Required Information is missing", "manage.php?id=".$id);
}
$help = $OUTPUT->help_icon('updateattendance', 'attforblock', '');
$update = $DB->count_records('attendance_log', array('sessionid' => $sessionid));
if ($update) {
require_capability('mod/attforblock:changeattendances', $context);
echo $OUTPUT->heading(get_string('update','attforblock').' ' .get_string('attendanceforthecourse','attforblock').' :: ' .$course->fullname.$help);
} else {
require_capability('mod/attforblock:takeattendances', $context);
echo $OUTPUT->heading(get_string('attendanceforthecourse','attforblock').' :: ' .$course->fullname.$help);
}
/// find out current groups mode
$groupmode = groups_get_activity_groupmode($cm);
$currentgroup = groups_get_activity_group($cm, true);
$manager = new course_enrolment_manager($course);
if ($currentgroup) {
/*
$sql = "SELECT u.*
FROM {role_assignments} ra, {user} u, {course} c, {context} cxt
WHERE ra.userid = u.id
AND ra.contextid = cxt.id
AND cxt.contextlevel = 50
AND cxt.instanceid = c.id
AND c.id = ?
AND roleid =5
AND u.id IN (SELECT userid FROM {groups_members} gm WHERE gm.groupid = ?)
ORDER BY u.$sort ASC";
$params = array($cm->course, $currentgroup);
$students = $DB->get_records_sql($sql, $params);
*/
$students = $manager->get_users($sort); // FIXME add $currentgroup somehow
} else {
$students = $manager->get_users($sort);
}
$sort = $sort == 'firstname' ? 'firstname' : 'lastname';
/// Now we need a menu for separategroups as well!
if ($groupmode == VISIBLEGROUPS ||
($groupmode && has_capability('moodle/site:accessallgroups', $context))) {
groups_print_activity_menu($cm, $CFG->wwwroot."/mod/attforblock/attendances.php?id=$id&sessionid=$sessionid&sort=$sort");
}
$statuses = get_statuses($course->id);
if (count($statuses) == 0) {
// maybe we don't have them set yet? no sense in doing everything else
echo '<p>';
echo get_string('settingsareempty', 'attforblock');
echo ': <a href="attsettings.php?id='.$cm->id.'">'.get_string('settings', 'attforblock').'</a>';
echo '</p>';
echo $OUTPUT->footer($course);
exit;
}
$table = new html_table();
$table->data[][] = '<b>'.get_string('sessiondate','attforblock').': '.userdate($sessdata->sessdate, get_string('strftimedate').', '.get_string('str_ftimehm', 'attforblock')).
', "'.($sessdata->description ? $sessdata->description : get_string('nodescription', 'attforblock')).'"</b>';
echo html_writer::table($table);
echo '<script type="text/javascript">'; // substitute for select_all_in() function which does not seem to work with Moodle 2.0
echo 'function att_select_column(cl) {';
echo ' var inputs = document.getElementsByTagName("input");';
echo ' for(i=0,j=0; i<inputs.length; i++) {';
echo ' var test = " " + inputs[i].className + " ";';
echo ' if (test.indexOf(cl) != -1) {';
echo ' inputs[i].checked="checked";';
echo ' }';
echo ' }';
echo '}';
echo '</script>';
$i = 3;
foreach($statuses as $st) {
$tabhead[] = "<a href=\"javascript:att_select_column('attcb{$i}');\" style=\"text-decoration: underline;\">".$st->acronym."</a>";
$i++;
}
$tabhead[] = get_string('remarks','attforblock');
$firstname = "<a href=\"attendances.php?id=$id&sessionid=$sessionid&sort=firstname\">".get_string('firstname').'</a>';
$lastname = "<a href=\"attendances.php?id=$id&sessionid=$sessionid&sort=lastname\">".get_string('lastname').'</a>';
if ($CFG->fullnamedisplay == 'lastname firstname') { // for better view (dlnsk)
$fullnamehead = "$lastname / $firstname";
} else {
$fullnamehead = "$firstname / $lastname";
}
if ($students) {
unset($table);
$table = new html_table();
$table->head[] = '#';
$table->align[] = 'center';
$table->size[] = '20px';
$table->head[] = '';
$table->align[] = '';
$table->size[] = '1px';
$table->head[] = $fullnamehead;
$table->align[] = 'left';
$table->size[] = '';
$table->wrap[2] = 'nowrap';
foreach ($tabhead as $hd) {
$table->head[] = $hd;
$table->align[] = 'center';
$table->size[] = '20px';
}
$i = 0;
foreach($students as $student) {
$i++;
$att = $DB->get_record('attendance_log', array('sessionid' => $sessionid, 'studentid' => $student->id));
if (empty($att)) {
$att = new stdClass();
$att->statusid = 0;
$att->remarks = '';
}
$table->data[$student->id][] = (!$att && $update) ? "<b style=\"color:red;\">$i</b>" : $i;
$table->data[$student->id][] = $OUTPUT->user_picture($student, array($course->id));
$table->data[$student->id][] = "<a href=\"view.php?id=$id&student={$student->id}\">".((!$att && $update) ? '<b style="color:red;">' : '').fullname($student).((!$att && $update) ? '</b>' : '').'</a>';
$x = 3;
foreach($statuses as $st) {
$table->data[$student->id][] = '<input name="student'.$student->id.'" class="attcb'.$x.'" type="radio" value="'.$st->id.'" '.($st->id == $att->statusid ? 'checked="checked"' : '').'/>';
$x++;
}
$table->data[$student->id][] = '<input type="text" name="remarks'.$student->id.'" size="" value="'.($att ? $att->remarks : '').'"/>';
}
echo '<form name="takeattendance" method="post" action="attendances.php">';
echo html_writer::table($table);
echo '<div style="text-align: center;">';
echo '<input type="hidden" name="id" value="'.$id.'"/>';
echo '<input type="hidden" name="sessionid" value="'.$sessionid.'" />';
echo '<input type="hidden" name="formfrom" value="editsessvals" />';
echo '<input type="submit" name="esv" value="'.get_string('ok').'" />';
echo '</div>';
echo '</form>';
} else {
echo $OUTPUT->heading(get_string('nothingtodisplay'));
}
echo get_string('status','attforblock').':<br />';
foreach($statuses as $st) {
echo $st->acronym.' - '.$st->description.'<br />';
}
echo $OUTPUT->footer($course);
?>