3 require_once("../../config.php");
4 require_once("lib.php");
6 $id = required_param('id', PARAM_INT
); //moduleid
7 $format = optional_param('format', CHOICE_PUBLISH_NAMES
, PARAM_INT
);
8 $download = optional_param('download', '', PARAM_ALPHA
);
9 $action = optional_param('action', '', PARAM_ALPHA
);
11 if (! $cm = get_coursemodule_from_id('choice', $id)) {
12 error("Course Module ID was incorrect");
15 if (! $course = get_record("course", "id", $cm->course
)) {
16 error("Course module is misconfigured");
19 require_login($course->id
, false, $cm);
21 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
23 require_capability('mod/choice:readresponses', $context);
25 if (!$choice = choice_get_choice($cm->instance
)) {
26 error("Course module is incorrect");
29 $strchoice = get_string("modulename", "choice");
30 $strchoices = get_string("modulenameplural", "choice");
31 $strresponses = get_string("responses", "choice");
33 add_to_log($course->id
, "choice", "report", "report.php?id=$cm->id", "$choice->id",$cm->id
);
35 if ($action == 'delete' && has_capability('mod/choice:deleteresponses',$context)) {
36 $attemptids = isset($_POST['attemptid']) ?
$_POST['attemptid'] : array(); //get array of repsonses to delete.
37 choice_delete_responses($attemptids); //delete responses.
38 redirect("report.php?id=$cm->id");
44 $navlinks[] = array('name' => $strchoices, 'link' => "index.php?id=$course->id", 'type' => 'activity');
45 $navlinks[] = array('name' => format_string($choice->name
), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
46 $navlinks[] = array('name' => $strresponses, 'link' => '', 'type' => 'title');
48 $navigation = build_navigation($navlinks);
50 print_header_simple(format_string($choice->name
).": $strresponses", "", $navigation, "", '', true,
51 update_module_button($cm->id
, $course->id
, $strchoice), navmenu($course, $cm));
52 /// Check to see if groups are being used in this choice
53 $groupmode = groupmode($course, $cm);
54 setup_and_print_groups($course, $groupmode, 'report.php?id='.$id);
56 $groupmode = groupmode($course, $cm);
57 get_and_set_current_group($course, $groupmode);
60 $users = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC');
63 print_heading(get_string("nousersyet"));
66 if ($allresponses = get_records("choice_answers", "choiceid", $choice->id
)) {
67 foreach ($allresponses as $aa) {
68 $answers[$aa->userid
] = $aa;
76 foreach ($choice->option
as $optionid => $text) {
77 $useranswer[$optionid] = array();
79 foreach ($users as $user) {
80 if (!empty($user->id
) and !empty($answers[$user->id
])) {
81 $answer = $answers[$user->id
];
82 $useranswer[(int)$answer->optionid
][] = $user;
84 $useranswer[0][] = $user;
87 foreach ($choice->option
as $optionid => $text) {
88 if (!$choice->option
[$optionid]) {
89 unset($useranswer[$optionid]); // Throw away any data that doesn't apply
95 if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
96 require_once("$CFG->libdir/odslib.class.php");
98 /// Calculate file name
99 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name
,true))).'.ods';
100 /// Creating a workbook
101 $workbook = new MoodleODSWorkbook("-");
102 /// Send HTTP headers
103 $workbook->send($filename);
104 /// Creating the first worksheet
105 $myxls =& $workbook->add_worksheet($strresponses);
107 /// Print names of all the fields
108 $myxls->write_string(0,0,get_string("lastname"));
109 $myxls->write_string(0,1,get_string("firstname"));
110 $myxls->write_string(0,2,get_string("idnumber"));
111 $myxls->write_string(0,3,get_string("group"));
112 $myxls->write_string(0,4,get_string("choice","choice"));
115 /// generate the data for the body of the spreadsheet
119 foreach ($users as $user) {
122 if (!($optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id
))) {
124 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.
126 $myxls->write_string($row,0,$user->lastname
);
127 $myxls->write_string($row,1,$user->firstname
);
128 $studentid=(!empty($user->idnumber
) ?
$user->idnumber
: " ");
129 $myxls->write_string($row,2,$studentid);
131 if ($usergrps = user_group($course->id
, $user->id
)) {
132 foreach ($usergrps as $ug) {
133 $ug2 = $ug2. $ug->name
;
136 $myxls->write_string($row,3,$ug2);
138 $useroption = choice_get_option_text($choice, $answers[$user->id
]->optionid
);
139 if (isset($useroption)) {
140 $myxls->write_string($row,4,format_string($useroption,true));
148 /// Close the workbook
156 //print spreadsheet if one is asked for:
157 if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
158 require_once("$CFG->libdir/excellib.class.php");
160 /// Calculate file name
161 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name
,true))).'.xls';
162 /// Creating a workbook
163 $workbook = new MoodleExcelWorkbook("-");
164 /// Send HTTP headers
165 $workbook->send($filename);
166 /// Creating the first worksheet
167 $myxls =& $workbook->add_worksheet($strresponses);
169 /// Print names of all the fields
170 $myxls->write_string(0,0,get_string("lastname"));
171 $myxls->write_string(0,1,get_string("firstname"));
172 $myxls->write_string(0,2,get_string("idnumber"));
173 $myxls->write_string(0,3,get_string("group"));
174 $myxls->write_string(0,4,get_string("choice","choice"));
177 /// generate the data for the body of the spreadsheet
181 foreach ($users as $user) {
184 if (!($optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id
))) {
186 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.
188 $myxls->write_string($row,0,$user->lastname
);
189 $myxls->write_string($row,1,$user->firstname
);
190 $studentid=(!empty($user->idnumber
) ?
$user->idnumber
: " ");
191 $myxls->write_string($row,2,$studentid);
193 if ($usergrps = user_group($course->id
, $user->id
)) {
194 foreach ($usergrps as $ug) {
195 $ug2 = $ug2. $ug->name
;
198 $myxls->write_string($row,3,$ug2);
200 $useroption = choice_get_option_text($choice, $answers[$user->id
]->optionid
);
201 if (isset($useroption)) {
202 $myxls->write_string($row,4,format_string($useroption,true));
210 /// Close the workbook
217 if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
218 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name
,true))).'.txt';
220 header("Content-Type: application/download\n");
221 header("Content-Disposition: attachment; filename=\"$filename\"");
222 header("Expires: 0");
223 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
224 header("Pragma: public");
226 /// Print names of all the fields
228 echo get_string("firstname")."\t".get_string("lastname") . "\t". get_string("idnumber") . "\t";
229 echo get_string("group"). "\t";
230 echo get_string("choice","choice"). "\n";
232 /// generate the data for the body of the spreadsheet
235 if ($users) foreach ($users as $user) {
236 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.
238 echo $user->lastname
;
239 echo "\t".$user->firstname
;
241 if (!empty($user->idnumber
)) {
242 $studentid = $user->idnumber
;
244 echo "\t". $studentid."\t";
246 if ($usergrps = user_group($course->id
, $user->id
)) {
247 foreach ($usergrps as $ug) {
248 $ug2 = $ug2. $ug->name
;
252 echo format_string(choice_get_option_text($choice, $answers[$user->id
]->optionid
),true). "\n";
259 choice_show_results($choice, $course, $cm, $format); //show table with students responses.
261 //now give links for downloading spreadsheets.
263 echo "<table class=\"downloadreport\"><tr>\n";
266 $options["id"] = "$cm->id";
267 $options["download"] = "ods";
268 print_single_button("report.php", $options, get_string("downloadods"));
270 $options["download"] = "xls";
271 print_single_button("report.php", $options, get_string("downloadexcel"));
273 $options["download"] = "txt";
274 print_single_button("report.php", $options, get_string("downloadtext"));
276 echo "</td></tr></table>";
277 print_footer($course);