2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * prints an analysed excel-spreadsheet of the feedback
20 * @author Andreas Grabs
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22 * @package mod_feedback
25 require_once("../../config.php");
26 require_once("lib.php");
27 require_once("$CFG->libdir/excellib.class.php");
29 feedback_load_feedback_items();
31 $id = required_param('id', PARAM_INT
); //the POST dominated the GET
32 $coursefilter = optional_param('coursefilter', '0', PARAM_INT
);
34 $url = new moodle_url('/mod/feedback/analysis_to_excel.php', array('id'=>$id));
35 if ($coursefilter !== '0') {
36 $url->param('coursefilter', $coursefilter);
40 $formdata = data_submitted();
42 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
43 print_error('invalidcoursemodule');
46 if (! $course = $DB->get_record("course", array("id"=>$cm->course
))) {
47 print_error('coursemisconf');
50 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance
))) {
51 print_error('invalidcoursemodule');
54 $context = context_module
::instance($cm->id
);
56 require_login($course, true, $cm);
58 require_capability('mod/feedback:viewreports', $context);
60 //buffering any output
61 //this prevents some output before the excel-header will be send
63 $fstring = new stdClass();
64 $fstring->bold
= get_string('bold', 'feedback');
65 $fstring->page
= get_string('page', 'feedback');
66 $fstring->of
= get_string('of', 'feedback');
67 $fstring->modulenameplural
= get_string('modulenameplural', 'feedback');
68 $fstring->questions
= get_string('questions', 'feedback');
69 $fstring->itemlabel
= get_string('item_label', 'feedback');
70 $fstring->question
= get_string('question', 'feedback');
71 $fstring->responses
= get_string('responses', 'feedback');
72 $fstring->idnumber
= get_string('idnumber');
73 $fstring->username
= get_string('username');
74 $fstring->fullname
= get_string('fullnameuser');
75 $fstring->courseid
= get_string('courseid', 'feedback');
76 $fstring->course
= get_string('course');
77 $fstring->anonymous_user
= get_string('anonymous_user', 'feedback');
80 //get the questions (item-names)
81 $params = array('feedback' => $feedback->id
, 'hasvalue' => 1);
82 if (!$items = $DB->get_records('feedback_item', $params, 'position')) {
83 print_error('no_items_available_yet',
85 $CFG->wwwroot
.'/mod/feedback/view.php?id='.$id);
89 $filename = "feedback.xls";
91 $mygroupid = groups_get_activity_group($cm);
93 // Creating a workbook
94 $workbook = new MoodleExcelWorkbook('-');
95 $workbook->send($filename);
97 //creating the needed formats
98 $xls_formats = new stdClass();
99 $xls_formats->head1
= $workbook->add_format(array(
103 $xls_formats->head2
= $workbook->add_format(array(
108 $xls_formats->default = $workbook->add_format(array(
112 $xls_formats->value_bold
= $workbook->add_format(array(
117 $xls_formats->procent
= $workbook->add_format(array(
121 'num_format'=>'#,##0.00%'));
123 // Creating the worksheets
124 $sheetname = clean_param($feedback->name
, PARAM_ALPHANUM
);
126 $worksheet1 = $workbook->add_worksheet(substr($sheetname, 0, 31));
127 $worksheet2 = $workbook->add_worksheet('detailed');
128 error_reporting($CFG->debug
);
129 $worksheet1->hide_gridlines();
130 $worksheet1->set_column(0, 0, 10);
131 $worksheet1->set_column(1, 1, 30);
132 $worksheet1->set_column(2, 20, 15);
134 //writing the table header
136 $worksheet1->write_string($row_offset1, 0, userdate(time()), $xls_formats->head1
);
138 ////////////////////////////////////////////////////////////////////////
139 //print the analysed sheet
140 ////////////////////////////////////////////////////////////////////////
142 $completedscount = feedback_get_completeds_group_count($feedback, $mygroupid, $coursefilter);
143 if ($completedscount > 0) {
144 //write the count of completeds
146 $worksheet1->write_string($row_offset1,
148 $fstring->modulenameplural
.': '.strval($completedscount),
149 $xls_formats->head1
);
152 if (is_array($items)) {
154 $worksheet1->write_string($row_offset1,
156 $fstring->questions
.': '. strval(count($items)),
157 $xls_formats->head1
);
161 $worksheet1->write_string($row_offset1, 0, $fstring->itemlabel
, $xls_formats->head1
);
162 $worksheet1->write_string($row_offset1, 1, $fstring->question
, $xls_formats->head1
);
163 $worksheet1->write_string($row_offset1, 2, $fstring->responses
, $xls_formats->head1
);
169 foreach ($items as $item) {
170 //get the class of item-typ
171 $itemobj = feedback_get_item_class($item->typ
);
172 $row_offset1 = $itemobj->excelprint_item($worksheet1,
180 ////////////////////////////////////////////////////////////////////////
181 //print the detailed sheet
182 ////////////////////////////////////////////////////////////////////////
185 $completeds = feedback_get_completeds_group($feedback, $mygroupid, $coursefilter);
186 //important: for each completed you have to print each item, even if it is not filled out!!!
187 //therefor for each completed we have to iterate over all items of the feedback
188 //this is done by feedback_excelprint_detailed_items
191 //first we print the table-header
192 $row_offset2 = feedback_excelprint_detailed_head($worksheet2, $xls_formats, $items, $row_offset2);
195 if (is_array($completeds)) {
196 foreach ($completeds as $completed) {
197 $row_offset2 = feedback_excelprint_detailed_items($worksheet2,
208 ////////////////////////////////////////////////////////////////////////////////
209 ////////////////////////////////////////////////////////////////////////////////
211 ////////////////////////////////////////////////////////////////////////////////
214 function feedback_excelprint_detailed_head(&$worksheet, $xls_formats, $items, $row_offset) {
215 global $fstring, $feedback;
222 $worksheet->write_string($row_offset +
1, $col_offset, $fstring->idnumber
, $xls_formats->head2
);
225 $worksheet->write_string($row_offset +
1, $col_offset, $fstring->username
, $xls_formats->head2
);
228 $worksheet->write_string($row_offset +
1, $col_offset, $fstring->fullname
, $xls_formats->head2
);
231 foreach ($items as $item) {
232 $worksheet->write_string($row_offset, $col_offset, $item->name
, $xls_formats->head2
);
233 $worksheet->write_string($row_offset +
1, $col_offset, $item->label
, $xls_formats->head2
);
237 $worksheet->write_string($row_offset +
1, $col_offset, $fstring->courseid
, $xls_formats->head2
);
240 $worksheet->write_string($row_offset +
1, $col_offset, $fstring->course
, $xls_formats->head2
);
243 return $row_offset +
2;
246 function feedback_excelprint_detailed_items(&$worksheet, $xls_formats,
247 $completed, $items, $row_offset) {
248 global $DB, $fstring;
256 $feedback = $DB->get_record('feedback', array('id'=>$completed->feedback
));
258 //anonymous users are separated automatically because the userid in the completed is "0"
259 if ($user = $DB->get_record('user', array('id'=>$completed->userid
))) {
260 if ($completed->anonymous_response
== FEEDBACK_ANONYMOUS_NO
) {
261 $worksheet->write_string($row_offset, $col_offset, $user->idnumber
, $xls_formats->head2
);
263 $userfullname = fullname($user);
264 $worksheet->write_string($row_offset, $col_offset, $user->username
, $xls_formats->head2
);
267 $userfullname = $fstring->anonymous_user
;
268 $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2
);
270 $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2
);
274 $userfullname = $fstring->anonymous_user
;
275 $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2
);
277 $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2
);
281 $worksheet->write_string($row_offset, $col_offset, $userfullname, $xls_formats->head2
);
284 foreach ($items as $item) {
285 $params = array('item' => $item->id
, 'completed' => $completed->id
);
286 $value = $DB->get_record('feedback_value', $params);
288 $itemobj = feedback_get_item_class($item->typ
);
289 $printval = $itemobj->get_printval($item, $value);
290 $printval = trim($printval);
292 if (is_numeric($printval)) {
293 $worksheet->write_number($row_offset, $col_offset, $printval, $xls_formats->default);
294 } else if ($printval != '') {
295 $worksheet->write_string($row_offset, $col_offset, $printval, $xls_formats->default);
299 $courseid = isset($value->course_id
) ?
$value->course_id
: 0;
300 if ($courseid == 0) {
301 $courseid = $feedback->course
;
304 $worksheet->write_number($row_offset, $col_offset, $courseid, $xls_formats->default);
306 if (isset($courseid) AND $course = $DB->get_record('course', array('id' => $courseid))) {
307 $coursecontext = context_course
::instance($courseid);
308 $shortname = format_string($course->shortname
, true, array('context' => $coursecontext));
309 $worksheet->write_string($row_offset, $col_offset, $shortname, $xls_formats->default);
311 return $row_offset +
1;