Integration 3 - groups in core, modules, calendar... (Part of MDL-7380, new groups)
[moodle.git] / mod / choice / report.php
CommitLineData
1c61c8d6 1<?php // $Id$
f9903ed0 2
b0e3a925 3 require_once("../../config.php");
4 require_once("lib.php");
f9903ed0 5
69e3c416 6 $id = required_param('id', PARAM_INT); //moduleid
09972dd3 7 $format = optional_param('format', CHOICE_PUBLISH_NAMES, PARAM_INT);
8 $download = optional_param('download', '', PARAM_ALPHA);
69e3c416 9 $action = optional_param('action', '', PARAM_ALPHA);
bfe97ca6 10
f9d5371b 11 if (! $cm = get_coursemodule_from_id('choice', $id)) {
f9903ed0 12 error("Course Module ID was incorrect");
13 }
14
15 if (! $course = get_record("course", "id", $cm->course)) {
16 error("Course module is misconfigured");
17 }
18
ec81373f 19 require_login($course->id, false, $cm);
bbbf2d40 20
dabfd0ed 21 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
bbbf2d40 22
0468976c 23 require_capability('mod/choice:readresponses', $context);
bbbf2d40 24
cd3fccff 25 if (!$choice = choice_get_choice($cm->instance)) {
f9903ed0 26 error("Course module is incorrect");
27 }
28
c4016bc1 29 $strchoice = get_string("modulename", "choice");
30 $strchoices = get_string("modulenameplural", "choice");
31 $strresponses = get_string("responses", "choice");
32
408f7c41 33 add_to_log($course->id, "choice", "report", "report.php?id=$cm->id", "$choice->id",$cm->id);
348630d8 34
0468976c 35 if ($action == 'delete' && has_capability('mod/choice:deleteresponses',$context)) {
348630d8 36 $attemptids = isset($_POST['attemptid']) ? $_POST['attemptid'] : array(); //get array of repsonses to delete.
90ca2ce8 37 choice_delete_responses($attemptids); //delete responses.
dabfd0ed 38 redirect("report.php?id=$cm->id");
348630d8 39 }
40
d921818d 41 if ($download <> "xls" and $download <> "txt" ) {
42 print_header_simple(format_string($choice->name).": $strresponses", "",
43 "<a href=\"index.php?id=$course->id\">$strchoices</a> ->
44 <a href=\"view.php?id=$cm->id\">".format_string($choice->name,true)."</a> -> $strresponses", "", '', true,
45 update_module_button($cm->id, $course->id, $strchoice), navmenu($course, $cm));
b6ee2d82 46 }
f9903ed0 47
33a893aa 48 $users = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC');
348630d8 49
b6ee2d82 50 if (!$users) {
39ee02a3 51 print_heading(get_string("nousersyet"));
f9903ed0 52 }
53
bfe97ca6 54 if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) {
6fd87e3b 55 foreach ($allresponses as $aa) {
ebc3bd2b 56 $answers[$aa->userid] = $aa;
f9903ed0 57 }
f9903ed0 58 } else {
59 $answers = array () ;
60 }
f9903ed0 61
62 $timenow = time();
63
bfe97ca6 64 foreach ($choice->option as $optionid => $text) {
65 $useranswer[$optionid] = array();
66062dd3 66 }
da664183 67 foreach ($users as $user) {
bfe97ca6 68 if (!empty($user->id) and !empty($answers[$user->id])) {
dcde9f02 69 $answer = $answers[$user->id];
bfe97ca6 70 $useranswer[(int)$answer->optionid][] = $user;
dcde9f02 71 } else {
bfe97ca6 72 $useranswer[0][] = $user;
dcde9f02 73 }
da664183 74 }
bfe97ca6 75 foreach ($choice->option as $optionid => $text) {
76 if (!$choice->option[$optionid]) {
77 unset($useranswer[$optionid]); // Throw away any data that doesn't apply
cd3fccff 78 }
79 }
da664183 80 ksort($useranswer);
64739659 81
82 //print spreadsheet if one is asked for:
0468976c 83 if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
b0898566 84 require_once("$CFG->libdir/excellib.class.php");
64739659 85
31344133 86 /// Calculate file name
b0898566 87 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.xls';
88 /// Creating a workbook
89 $workbook = new MoodleExcelWorkbook("-");
90 /// Send HTTP headers
91 $workbook->send($filename);
92 /// Creating the first worksheet
31344133 93 $myxls =& $workbook->add_worksheet($strresponses);
64739659 94
b0898566 95 /// Print names of all the fields
64739659 96 $myxls->write_string(0,0,get_string("lastname"));
97 $myxls->write_string(0,1,get_string("firstname"));
98 $myxls->write_string(0,2,get_string("idnumber"));
348630d8 99 $myxls->write_string(0,3,get_string("group"));
100 $myxls->write_string(0,4,get_string("choice","choice"));
101
64739659 102
64739659 103 /// generate the data for the body of the spreadsheet
b0898566 104 $i=0;
105 $row=1;
106 if ($users) {
f9a59efa 107 foreach ($users as $user) {
108 // this needs fixing
39b3e7de 109
110 if (!($optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id))) {
111
112 if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id) && $choice->showunanswered==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.
113
114 $myxls->write_string($row,0,$user->lastname);
115 $myxls->write_string($row,1,$user->firstname);
116 $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
117 $myxls->write_string($row,2,$studentid);
118 $ug2 = '';
119 if ($usergrps = user_group($course->id, $user->id)) {
120 foreach ($usergrps as $ug) {
121 $ug2 = $ug2. $ug->name;
122 }
dabfd0ed 123 }
39b3e7de 124 $myxls->write_string($row,3,$ug2);
348630d8 125
39b3e7de 126 $useroption = choice_get_option_text($choice, $answers[$user->id]->optionid);
127 if (isset($useroption)) {
128 $myxls->write_string($row,4,format_string($useroption,true));
129 }
130 $row++;
131 }
132 $pos=4;
b0898566 133 }
b0898566 134 }
b0898566 135
136 /// Close the workbook
39b3e7de 137 $workbook->close();
b0898566 138
39b3e7de 139 exit;
140 }
141 }
bbbf2d40 142 // print text file
0468976c 143 if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
09972dd3 144 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.txt';
0468976c 145
146 header("Content-Type: application/download\n");
147 header("Content-Disposition: attachment; filename=\"$filename\"");
148 header("Expires: 0");
149 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
150 header("Pragma: public");
151
152 /// Print names of all the fields
64739659 153
154 echo get_string("firstname")."\t".get_string("lastname") . "\t". get_string("idnumber") . "\t";
348630d8 155 echo get_string("group"). "\t";
64739659 156 echo get_string("choice","choice"). "\n";
0468976c 157
158 /// generate the data for the body of the spreadsheet
159 $i=0;
160 $row=1;
161 if ($users) foreach ($users as $user) {
39b3e7de 162 if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id) && $choice->showunanswered==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.
0468976c 163
164 echo $user->lastname;
165 echo "\t".$user->firstname;
166 $studentid = " ";
167 if (!empty($user->idnumber)) {
168 $studentid = $user->idnumber;
169 }
170 echo "\t". $studentid."\t";
171 $ug2 = '';
172 if ($usergrps = user_group($course->id, $user->id)) {
173 foreach ($usergrps as $ug) {
174 $ug2 = $ug2. $ug->name;
175 }
176 }
177 echo $ug2. "\t";
178 echo format_string(choice_get_option_text($choice, $answers[$user->id]->optionid),true). "\n";
179 }
180 $row++;
181 }
182 exit;
183 }
184
348630d8 185 choice_show_results($choice, $course, $cm, $format); //show table with students responses.
186
187 //now give links for downloading spreadsheets.
64739659 188 echo "<br />\n";
189 echo "<table border=\"0\" align=\"center\"><tr>\n";
190 echo "<td>";
191 unset($options);
192 $options["id"] = "$cm->id";
193 $options["download"] = "xls";
194 print_single_button("report.php", $options, get_string("downloadexcel"));
195 echo "</td><td>";
196 $options["download"] = "txt";
197 print_single_button("report.php", $options, get_string("downloadtext"));
198
199 echo "</td></tr></table>";
39b3e7de 200 print_footer($course);
ec81373f 201
f9903ed0 202?>