mirrored from git://git.moodle.org/moodle.git
/
report.php
202 lines (167 loc) · 7.84 KB
/
report.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
<?php // $Id$
require_once("../../config.php");
require_once("lib.php");
$id = required_param('id', PARAM_INT); //moduleid
$format = optional_param('format', CHOICE_PUBLISH_NAMES, PARAM_INT);
$download = optional_param('download', '', PARAM_ALPHA);
$action = optional_param('action', '', PARAM_ALPHA);
if (! $cm = get_record("course_modules", "id", $id)) {
error("Course Module ID was incorrect");
}
if (! $course = get_record("course", "id", $cm->course)) {
error("Course module is misconfigured");
}
require_login($course->id, false, $cm);
if (!isteacher($course->id)) {
error("Only teachers can look at this page");
}
if (!$choice = choice_get_choice($cm->instance)) {
error("Course module is incorrect");
}
$strchoice = get_string("modulename", "choice");
$strchoices = get_string("modulenameplural", "choice");
$strresponses = get_string("responses", "choice");
add_to_log($course->id, "choice", "report", "report.php?id=$cm->id", "$choice->id",$cm->id);
if ($action == 'delete') { //some responses need to be deleted
$attemptids = isset($_POST['attemptid']) ? $_POST['attemptid'] : array(); //get array of repsonses to delete.
choice_delete_responses($attemptids); //delete responses.
redirect("report.php?id=$cm->id");
}
if ($download <> "xls" and $download <> "txt" ) {
print_header_simple(format_string($choice->name).": $strresponses", "",
"<a href=\"index.php?id=$course->id\">$strchoices</a> ->
<a href=\"view.php?id=$cm->id\">".format_string($choice->name,true)."</a> -> $strresponses", "", '', true,
update_module_button($cm->id, $course->id, $strchoice), navmenu($course, $cm));
}
$users = get_course_users($course->id, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber') + get_admins();
if (!$users) {
print_heading(get_string("nousersyet"));
}
if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) {
foreach ($allresponses as $aa) {
$answers[$aa->userid] = $aa;
}
} else {
$answers = array () ;
}
$timenow = time();
foreach ($choice->option as $optionid => $text) {
$useranswer[$optionid] = array();
}
foreach ($users as $user) {
if (!empty($user->id) and !empty($answers[$user->id])) {
$answer = $answers[$user->id];
$useranswer[(int)$answer->optionid][] = $user;
} else {
$useranswer[0][] = $user;
}
}
foreach ($choice->option as $optionid => $text) {
if (!$choice->option[$optionid]) {
unset($useranswer[$optionid]); // Throw away any data that doesn't apply
}
}
ksort($useranswer);
//print spreadsheet if one is asked for:
if ($download == "xls") {
require_once("$CFG->libdir/excellib.class.php");
/// Calculate file name
$filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.xls';
/// Creating a workbook
$workbook = new MoodleExcelWorkbook("-");
/// Send HTTP headers
$workbook->send($filename);
/// Creating the first worksheet
$myxls =& $workbook->add_worksheet($strresponses);
/// Print names of all the fields
$myxls->write_string(0,0,get_string("lastname"));
$myxls->write_string(0,1,get_string("firstname"));
$myxls->write_string(0,2,get_string("idnumber"));
$myxls->write_string(0,3,get_string("group"));
$myxls->write_string(0,4,get_string("choice","choice"));
/// generate the data for the body of the spreadsheet
$i=0;
$row=1;
if ($users) {
foreach ($users as $user) {
if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && isadmin($user->id)) &&
(!($answers[$user->id]->optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) ) ) &&
!($choice->showunanswered==0 && $answers[$user->id]->optionid==0) ) { //make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page.
$myxls->write_string($row,0,$user->lastname);
$myxls->write_string($row,1,$user->firstname);
$studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
$myxls->write_string($row,2,$studentid);
$ug2 = '';
if ($usergrps = user_group($course->id, $user->id)) {
foreach ($usergrps as $ug) {
$ug2 = $ug2. $ug->name;
}
}
$myxls->write_string($row,3,$ug2);
$useroption = choice_get_option_text($choice, $answers[$user->id]->optionid);
if (isset($useroption)) {
$myxls->write_string($row,4,format_string($useroption,true));
}
$row++;
}
$pos=4;
}
}
/// Close the workbook
$workbook->close();
exit;
}
// print text file
if ($download == "txt") {
$filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.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");
/// Print names of all the fields
echo get_string("firstname")."\t".get_string("lastname") . "\t". get_string("idnumber") . "\t";
echo get_string("group"). "\t";
echo get_string("choice","choice"). "\n";
/// generate the data for the body of the spreadsheet
$i=0;
$row=1;
if ($users) foreach ($users as $user) {
if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && isadmin($user->id)) &&
(!($answers[$user->id]->optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) ) ) &&
!($choice->showunanswered==0 && $answers[$user->id]->optionid==0) ) { //make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page.
echo $user->lastname;
echo "\t".$user->firstname;
$studentid = " ";
if (!empty($user->idnumber)) {
$studentid = $user->idnumber;
}
echo "\t". $studentid."\t";
$ug2 = '';
if ($usergrps = user_group($course->id, $user->id)) {
foreach ($usergrps as $ug) {
$ug2 = $ug2. $ug->name;
}
}
echo $ug2. "\t";
echo format_string(choice_get_option_text($choice, $answers[$user->id]->optionid),true). "\n";
}
$row++;
}
exit;
}
choice_show_results($choice, $course, $cm, $format); //show table with students responses.
//now give links for downloading spreadsheets.
echo "<br />\n";
echo "<table border=\"0\" align=\"center\"><tr>\n";
echo "<td>";
unset($options);
$options["id"] = "$cm->id";
$options["download"] = "xls";
print_single_button("report.php", $options, get_string("downloadexcel"));
echo "</td><td>";
$options["download"] = "txt";
print_single_button("report.php", $options, get_string("downloadtext"));
echo "</td></tr></table>";
print_footer($course);
?>