MDL-21227 - convert Feedback to new files api
[moodle.git] / mod / feedback / export.php
CommitLineData
84973212 1<?php
2
c70ad9f7 3/**
84973212 4 * prints the form to export the items as xml-file
5 *
6 * @author Andreas Grabs
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package feedback
9 */
10
11require_once("../../config.php");
12require_once("lib.php");
13
14// get parameters
1adbd2c3 15$id = required_param('id', PARAM_INT);
84973212 16$action = optional_param('action', false, PARAM_ALPHA);
17
a6855934 18$url = new moodle_url('/mod/feedback/export.php', array('id'=>$id));
84973212 19if ($action !== false) {
20 $url->param('action', $action);
21}
22$PAGE->set_url($url);
23
b6f26001
AG
24if (! $cm = get_coursemodule_from_id('feedback', $id)) {
25 print_error('invalidcoursemodule');
26}
27
28if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
29 print_error('coursemisconf');
30}
31
32if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
33 print_error('invalidcoursemodule');
84973212 34}
c987dbad
AG
35
36if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
37 print_error('badcontext');
38}
84973212 39
40require_login($course->id, true, $cm);
41
53ba55ec 42require_capability('mod/feedback:edititems', $context);
c70ad9f7 43
84973212 44if ($action == 'exportfile') {
45 if(!$exportdata = feedback_get_xml_data($feedback->id)) {
46 print_error('nodata');
47 }
48 @feedback_send_xml_data($exportdata, 'feedback_'.$feedback->id.'.xml');
c70ad9f7 49 exit;
84973212 50}
51
52redirect('view.php?id='.$id);
53exit;
54
55function feedback_get_xml_data($feedbackid) {
56 global $DB;
57
58 $space = ' ';
59 //get all items of the feedback
60 if(!$items = $DB->get_records('feedback_item', array('feedback'=>$feedbackid), 'position')) {
61 return false;
c70ad9f7 62 }
84973212 63
64 //writing the header of the xml file including the charset of the currrent used language
65 $data = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
66 $data .= '<FEEDBACK VERSION="200701" COMMENT="XML-Importfile for mod/feedback">'."\n";
67 $data .= $space.'<ITEMS>'."\n";
68
69 //writing all the items
70 foreach($items as $item) {
71 //start of item
72 $data .= $space.$space.'<ITEM TYPE="'.$item->typ.'" REQUIRED="'.$item->required.'">'."\n";
73
74 //start of itemtext
75 $data .= $space.$space.$space.'<ITEMTEXT>'."\n";
76 //start of CDATA
77 $data .= $space.$space.$space.$space.'<![CDATA[';
78 $data .= $item->name;
79 //end of CDATA
80 $data .= ']]>'."\n";
81 //end of itemtext
82 $data .= $space.$space.$space.'</ITEMTEXT>'."\n";
83
30ec6bc9
AG
84 //start of itemtext
85 $data .= $space.$space.$space.'<ITEMLABEL>'."\n";
86 //start of CDATA
87 $data .= $space.$space.$space.$space.'<![CDATA[';
88 $data .= $item->label;
89 //end of CDATA
90 $data .= ']]>'."\n";
91 //end of itemtext
92 $data .= $space.$space.$space.'</ITEMLABEL>'."\n";
93
84973212 94 //start of presentation
95 $data .= $space.$space.$space.'<PRESENTATION>'."\n";
96 //start of CDATA
97 $data .= $space.$space.$space.$space.'<![CDATA[';
98 $data .= $item->presentation;
99 //end of CDATA
100 $data .= ']]>'."\n";
101 //end of presentation
102 $data .= $space.$space.$space.'</PRESENTATION>'."\n";
103
104 //end of item
105 $data .= $space.$space.'</ITEM>'."\n";
c70ad9f7 106 }
84973212 107
108 //writing the footer of the xml file
109 $data .= $space.'</ITEMS>'."\n";
110 $data .= '</FEEDBACK>'."\n";
111
112 return $data;
113}
114
115function feedback_send_xml_data($data, $filename) {
84973212 116 @header('Content-Type: application/xml; charset=UTF-8');
117 @header('Content-Disposition: attachment; filename='.$filename);
118 print($data);
119}