-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.php
204 lines (175 loc) · 7.59 KB
/
export.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
<?PHP // $Id: export.php,v 1.1.2.2 2009/02/23 19:22:40 dlnsk Exp $
// Lists all the sessions for a course
require_once('../../config.php');
require_once('locallib.php');
require_once('export_form.php');
$id = required_param('id', PARAM_INT);
$url = new moodle_url('/mod/attforblock/export.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");
}
if (! $attforblock = $DB->get_record('attforblock', array('id' => $cm->instance))) {
print_error("Course module is incorrect");
}
$url->param($id);
$PAGE->set_url($url);
require_login($course->id);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_capability('mod/attforblock:export', $context);
$mform_export = new mod_attforblock_export_form('export.php', array('course'=>$course, 'cm'=>$cm, 'modcontext'=>$context));
if ($fromform = $mform_export->get_data()) {
$group = groups_get_group($fromform->group);
if ($group) {
$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.lastname ASC";
$params = array($cm->course, $group->id);
$students = $DB->get_records_sql($sql, $params);
//$students = get_users_by_capability($context, 'moodle/legacy:student', '', 'u.lastname ASC', '', '', $group->id, '', false);
} else {
$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
ORDER BY u.lastname ASC";
$params = array($cm->course);
$students = $DB->get_records_sql($sql, $params);
//$students = get_users_by_capability($context, 'moodle/legacy:student', '', 'u.lastname ASC', '', '', '', '', false);
}
if ($students) {
$filename = clean_filename($course->shortname.'_Attendances_'.userdate(time(), '%Y%m%d-%H%M'));
$data->tabhead = array();
$data->course = $course->fullname;
$data->group = $group ? $group->name : get_string('allparticipants');
if (isset($fromform->ident['id'])) {
$data->tabhead[] = get_string('studentid','attforblock');
}
if (isset($fromform->ident['uname'])) {
$data->tabhead[] = get_string('username');
}
$data->tabhead[] = get_string('lastname');
$data->tabhead[] = get_string('firstname');
$select = "courseid = {$course->id} AND sessdate >= {$course->startdate}";
if (isset($fromform->includenottaken)) {
$select .= " AND sessdate <= {$fromform->sessionenddate}";
} else {
$select .= " AND lasttaken != 0";
}
if ($sessions = $DB->get_records_select('attendance_sessions', $select, null, 'sessdate ASC')) {
foreach($sessions as $sess) {
$data->tabhead[] = userdate($sess->sessdate, get_string('str_ftimedmyhm', 'attforblock'));
}
} else {
print_error('Sessions not found!', 'report.php?id='.$id);
}
$data->tabhead[] = '%';
$i = 0;
$data->table = array();
$statuses = get_statuses($course->id);
foreach($students as $student) {
if (isset($fromform->ident['id'])) {
$data->table[$i][] = $student->id;
}
if (isset($fromform->ident['uname'])) {
$data->table[$i][] = $student->username;
}
$data->table[$i][] = $student->lastname;
$data->table[$i][] = $student->firstname;
foreach ($sessions as $sess) {
if ($rec = $DB->get_record('attendance_log', array('sessionid' => $sess->id, 'studentid' => $student->id))) {
$data->table[$i][] = $statuses[$rec->statusid]->acronym;
} else {
$data->table[$i][] = '-';
}
}
$data->table[$i][] = get_percent($student->id, $course).'%';
$i++;
}
if ($fromform->format === 'text') {
ExportToCSV($data, $filename);
} else {
ExportToTableEd($data, $filename, $fromform->format);
}
exit;
} else {
print_error('Students not found!', 'report.php?id='.$id);
}
} else {
/// Print headers
$navlinks[] = array('name' => $attforblock->name, 'link' => "view.php?id=$id", 'type' => 'activity');
$navlinks[] = array('name' => get_string('export', 'quiz'), 'link' => null, 'type' => 'activityinstance');
$navigation = build_navigation($navlinks);
print_header("$course->shortname: ".$attforblock->name.' - ' .get_string('export', 'quiz'), $course->fullname,
$navigation, "", "", true, " ", navmenu($course));
show_tabs($cm, $context, 'export');
$mform_export->display();
}
echo $OUTPUT->footer($course);
/////////////////////////////////////////////////////////////////////////////////
function ExportToTableEd($data, $filename, $format) {
global $CFG;
if ($format === 'excel') {
require_once("$CFG->libdir/excellib.class.php");
$filename .= ".xls";
$workbook = new MoodleExcelWorkbook("-");
} else {
require_once("$CFG->libdir/odslib.class.php");
$filename .= ".ods";
$workbook = new MoodleODSWorkbook("-");
}
/// Sending HTTP headers
$workbook->send($filename);
/// Creating the first worksheet
$myxls =& $workbook->add_worksheet('Attendances');
/// format types
$formatbc =& $workbook->add_format();
$formatbc->set_bold(1);
$myxls->write(0, 0, get_string('course'), $formatbc);
$myxls->write(0, 1, $data->course);
$myxls->write(1, 0, get_string('group'), $formatbc);
$myxls->write(1, 1, $data->group);
$i = 3;
$j = 0;
foreach ($data->tabhead as $cell) {
$myxls->write($i, $j++, $cell, $formatbc);
}
$i++;
$j = 0;
foreach ($data->table as $row) {
foreach ($row as $cell) {
$myxls->write($i, $j++, $cell);
}
$i++;
$j = 0;
}
$workbook->close();
}
function ExportToCSV($data, $filename) {
$filename .= ".txt";
header("Content-Type: application/download\n");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Expires: 0");
header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
header("Pragma: public");
echo get_string('course')."\t".$data->course."\n";
echo get_string('group')."\t".$data->group."\n\n";
echo implode("\t", $data->tabhead)."\n";
foreach ($data->table as $row) {
echo implode("\t", $row)."\n";
}
}
?>