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
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 if (!$context = get_context_instance(CONTEXT_MODULE
, $cm->id
)) {
55 print_error('badcontext');
58 require_login($course->id
, true, $cm);
60 require_capability('mod/feedback:viewreports', $context);
62 //buffering any output
63 //this prevents some output before the excel-header will be send
65 $fstring = new stdClass();
66 $fstring->bold
= get_string('bold', 'feedback');
67 $fstring->page
= get_string('page', 'feedback');
68 $fstring->of
= get_string('of', 'feedback');
69 $fstring->modulenameplural
= get_string('modulenameplural', 'feedback');
70 $fstring->questions
= get_string('questions', 'feedback');
71 $fstring->itemlabel
= get_string('item_label', 'feedback');
72 $fstring->question
= get_string('question', 'feedback');
73 $fstring->responses
= get_string('responses', 'feedback');
74 $fstring->idnumber
= get_string('idnumber');
75 $fstring->username
= get_string('username');
76 $fstring->fullname
= get_string('fullnameuser');
77 $fstring->courseid
= get_string('courseid', 'feedback');
78 $fstring->course
= get_string('course');
79 $fstring->anonymous_user
= get_string('anonymous_user', 'feedback');
82 //get the questions (item-names)
83 $params = array('feedback' => $feedback->id
, 'hasvalue' => 1);
84 if (!$items = $DB->get_records('feedback_item', $params, 'position')) {
85 print_error('no_items_available_yet',
87 $CFG->wwwroot
.'/mod/feedback/view.php?id='.$id);
91 $filename = "feedback.xls";
93 $mygroupid = groups_get_activity_group($cm);
95 // Creating a workbook
96 $workbook = new MoodleExcelWorkbook('-');
97 $workbook->send($filename);
99 //creating the needed formats
100 $xls_formats = new stdClass();
101 $xls_formats->head1
= $workbook->add_format(array(
105 $xls_formats->head2
= $workbook->add_format(array(
110 $xls_formats->default = $workbook->add_format(array(
114 $xls_formats->value_bold
= $workbook->add_format(array(
119 $xls_formats->procent
= $workbook->add_format(array(
123 'num_format'=>'#,##0.00%'));
125 // Creating the worksheets
126 $sheetname = clean_param($feedback->name
, PARAM_ALPHANUM
);
128 $worksheet1 =& $workbook->add_worksheet(substr($sheetname, 0, 31));
129 $worksheet2 =& $workbook->add_worksheet('detailed');
130 error_reporting($CFG->debug
);
131 $worksheet1->hide_gridlines();
132 $worksheet1->set_column(0, 0, 10);
133 $worksheet1->set_column(1, 1, 30);
134 $worksheet1->set_column(2, 20, 15);
136 //writing the table header
138 $worksheet1->write_string($row_offset1, 0, userdate(time()), $xls_formats->head1
);
140 ////////////////////////////////////////////////////////////////////////
141 //print the analysed sheet
142 ////////////////////////////////////////////////////////////////////////
144 $completedscount = feedback_get_completeds_group_count($feedback, $mygroupid, $coursefilter);
145 if ($completedscount > 0) {
146 //write the count of completeds
148 $worksheet1->write_string($row_offset1,
150 $fstring->modulenameplural
.': '.strval($completedscount),
151 $xls_formats->head1
);
154 if (is_array($items)) {
156 $worksheet1->write_string($row_offset1,
158 $fstring->questions
.': '. strval(count($items)),
159 $xls_formats->head1
);
163 $worksheet1->write_string($row_offset1, 0, $fstring->itemlabel
, $xls_formats->head1
);
164 $worksheet1->write_string($row_offset1, 1, $fstring->question
, $xls_formats->head1
);
165 $worksheet1->write_string($row_offset1, 2, $fstring->responses
, $xls_formats->head1
);
171 foreach ($items as $item) {
172 //get the class of item-typ
173 $itemobj = feedback_get_item_class($item->typ
);
174 $row_offset1 = $itemobj->excelprint_item($worksheet1,
182 ////////////////////////////////////////////////////////////////////////
183 //print the detailed sheet
184 ////////////////////////////////////////////////////////////////////////
187 $completeds = feedback_get_completeds_group($feedback, $mygroupid, $coursefilter);
188 //important: for each completed you have to print each item, even if it is not filled out!!!
189 //therefor for each completed we have to iterate over all items of the feedback
190 //this is done by feedback_excelprint_detailed_items
193 //first we print the table-header
194 $row_offset2 = feedback_excelprint_detailed_head($worksheet2, $xls_formats, $items, $row_offset2);
197 if (is_array($completeds)) {
198 foreach ($completeds as $completed) {
199 $row_offset2 = feedback_excelprint_detailed_items($worksheet2,
210 ////////////////////////////////////////////////////////////////////////////////
211 ////////////////////////////////////////////////////////////////////////////////
213 ////////////////////////////////////////////////////////////////////////////////
216 function feedback_excelprint_detailed_head(&$worksheet, $xls_formats, $items, $row_offset) {
217 global $fstring, $feedback;
224 $worksheet->write_string($row_offset +
1, $col_offset, $fstring->idnumber
, $xls_formats->head2
);
227 $worksheet->write_string($row_offset +
1, $col_offset, $fstring->username
, $xls_formats->head2
);
230 $worksheet->write_string($row_offset +
1, $col_offset, $fstring->fullname
, $xls_formats->head2
);
233 foreach ($items as $item) {
234 $worksheet->write_string($row_offset, $col_offset, $item->name
, $xls_formats->head2
);
235 $worksheet->write_string($row_offset +
1, $col_offset, $item->label
, $xls_formats->head2
);
239 $worksheet->write_string($row_offset +
1, $col_offset, $fstring->courseid
, $xls_formats->head2
);
242 $worksheet->write_string($row_offset +
1, $col_offset, $fstring->course
, $xls_formats->head2
);
245 return $row_offset +
2;
248 function feedback_excelprint_detailed_items(&$worksheet, $xls_formats,
249 $completed, $items, $row_offset) {
250 global $DB, $fstring;
258 $feedback = $DB->get_record('feedback', array('id'=>$completed->feedback
));
260 //anonymous users are separated automatically because the userid in the completed is "0"
261 if ($user = $DB->get_record('user', array('id'=>$completed->userid
))) {
262 if ($completed->anonymous_response
== FEEDBACK_ANONYMOUS_NO
) {
263 $worksheet->write_string($row_offset, $col_offset, $user->idnumber
, $xls_formats->head2
);
265 $userfullname = fullname($user);
266 $worksheet->write_string($row_offset, $col_offset, $user->username
, $xls_formats->head2
);
269 $userfullname = $fstring->anonymous_user
;
270 $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2
);
272 $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2
);
276 $userfullname = $fstring->anonymous_user
;
277 $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2
);
279 $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2
);
283 $worksheet->write_string($row_offset, $col_offset, $userfullname, $xls_formats->head2
);
286 foreach ($items as $item) {
287 $params = array('item' => $item->id
, 'completed' => $completed->id
);
288 $value = $DB->get_record('feedback_value', $params);
290 $itemobj = feedback_get_item_class($item->typ
);
291 $printval = $itemobj->get_printval($item, $value);
292 $printval = trim($printval);
294 if (is_numeric($printval)) {
295 $worksheet->write_number($row_offset, $col_offset, $printval, $xls_formats->default);
296 } else if ($printval != '') {
297 $worksheet->write_string($row_offset, $col_offset, $printval, $xls_formats->default);
301 $courseid = isset($value->course_id
) ?
$value->course_id
: 0;
302 if ($courseid == 0) {
303 $courseid = $feedback->course
;
306 $worksheet->write_number($row_offset, $col_offset, $courseid, $xls_formats->default);
308 if (isset($courseid) AND $course = $DB->get_record('course', array('id' => $courseid))) {
309 $coursecontext = get_context_instance(CONTEXT_COURSE
, $courseid);
310 $shortname = format_string($course->shortname
, true, array('context' => $coursecontext));
311 $worksheet->write_string($row_offset, $col_offset, $shortname, $xls_formats->default);
313 return $row_offset +
1;