insert calls to require_capability()
[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
24if ($id) {
25 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
26 print_error('invalidcoursemodule');
c70ad9f7 27 }
c70ad9f7 28
84973212 29 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
30 print_error('coursemisconf');
c70ad9f7 31 }
84973212 32
33 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
34 print_error('invalidcoursemodule');
c70ad9f7 35 }
84973212 36}
c987dbad
AG
37
38if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
39 print_error('badcontext');
40}
84973212 41
42require_login($course->id, true, $cm);
43
53ba55ec 44require_capability('mod/feedback:edititems', $context);
c70ad9f7 45
84973212 46if ($action == 'exportfile') {
47 if(!$exportdata = feedback_get_xml_data($feedback->id)) {
48 print_error('nodata');
49 }
50 @feedback_send_xml_data($exportdata, 'feedback_'.$feedback->id.'.xml');
c70ad9f7 51 exit;
84973212 52}
53
54redirect('view.php?id='.$id);
55exit;
56
57function feedback_get_xml_data($feedbackid) {
58 global $DB;
59
60 $space = ' ';
61 //get all items of the feedback
62 if(!$items = $DB->get_records('feedback_item', array('feedback'=>$feedbackid), 'position')) {
63 return false;
c70ad9f7 64 }
84973212 65
66 //writing the header of the xml file including the charset of the currrent used language
67 $data = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
68 $data .= '<FEEDBACK VERSION="200701" COMMENT="XML-Importfile for mod/feedback">'."\n";
69 $data .= $space.'<ITEMS>'."\n";
70
71 //writing all the items
72 foreach($items as $item) {
73 //start of item
74 $data .= $space.$space.'<ITEM TYPE="'.$item->typ.'" REQUIRED="'.$item->required.'">'."\n";
75
76 //start of itemtext
77 $data .= $space.$space.$space.'<ITEMTEXT>'."\n";
78 //start of CDATA
79 $data .= $space.$space.$space.$space.'<![CDATA[';
80 $data .= $item->name;
81 //end of CDATA
82 $data .= ']]>'."\n";
83 //end of itemtext
84 $data .= $space.$space.$space.'</ITEMTEXT>'."\n";
85
86 //start of presentation
87 $data .= $space.$space.$space.'<PRESENTATION>'."\n";
88 //start of CDATA
89 $data .= $space.$space.$space.$space.'<![CDATA[';
90 $data .= $item->presentation;
91 //end of CDATA
92 $data .= ']]>'."\n";
93 //end of presentation
94 $data .= $space.$space.$space.'</PRESENTATION>'."\n";
95
96 //end of item
97 $data .= $space.$space.'</ITEM>'."\n";
c70ad9f7 98 }
84973212 99
100 //writing the footer of the xml file
101 $data .= $space.'</ITEMS>'."\n";
102 $data .= '</FEEDBACK>'."\n";
103
104 return $data;
105}
106
107function feedback_send_xml_data($data, $filename) {
108 $charset = get_string('thischarset');
109 @header('Content-Type: application/xml; charset=UTF-8');
110 @header('Content-Disposition: attachment; filename='.$filename);
111 print($data);
112}