MDL-14493 First checkin of Feedback module into HEAD.
[moodle.git] / mod / feedback / export.php
CommitLineData
c70ad9f7 1<?php // $Id$
2/**
3* prints the form to export the items as xml-file
4*
5* @version $Id$
6* @author Andreas Grabs
7* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8* @package feedback
9*/
10
11 require_once("../../config.php");
12 require_once("lib.php");
13
14 // get parameters
15 $id = required_param('id', PARAM_INT);
16 $action = optional_param('action', false, PARAM_ALPHA);
17
18 if ($id) {
19 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
20 error("Course Module ID was incorrect");
21 }
22
23 if (! $course = get_record("course", "id", $cm->course)) {
24 error("Course is misconfigured");
25 }
26
27 if (! $feedback = get_record("feedback", "id", $cm->instance)) {
28 error("Course module is incorrect");
29 }
30 }
31 $capabilities = feedback_load_capabilities($cm->id);
32
33 require_login($course->id);
34
35 if(!$capabilities->edititems){
36 error('this action is not allowed');
37 }
38
39 if ($action == 'exportfile') {
40 if(!$exportdata = feedback_get_xml_data($feedback->id)) {
41 error('no data');
42 }
43 @feedback_send_xml_data($exportdata, 'feedback_'.$feedback->id.'.xml');
44 exit;
45 }
46
47 redirect('view.php?id='.$id);
48 exit;
49
50 function feedback_get_xml_data($feedbackid) {
51 $space = ' ';
52 //get all items of the feedback
53 if(!$items = get_records('feedback_item', 'feedback', $feedbackid, 'position')) {
54 return false;
55 }
56
57 //writing the header of the xml file including the charset of the currrent used language
58 $data = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
59 $data .= '<FEEDBACK VERSION="200701" COMMENT="XML-Importfile for mod/feedback">'."\n";
60 $data .= $space.'<ITEMS>'."\n";
61
62 //writing all the items
63 foreach($items as $item) {
64 //start of item
65 $data .= $space.$space.'<ITEM TYPE="'.$item->typ.'" REQUIRED="'.$item->required.'">'."\n";
66
67 //start of itemtext
68 $data .= $space.$space.$space.'<ITEMTEXT>'."\n";
69 //start of CDATA
70 $data .= $space.$space.$space.$space.'<![CDATA[';
71 $data .= $item->name;
72 //end of CDATA
73 $data .= ']]>'."\n";
74 //end of itemtext
75 $data .= $space.$space.$space.'</ITEMTEXT>'."\n";
76
77 //start of presentation
78 $data .= $space.$space.$space.'<PRESENTATION>'."\n";
79 //start of CDATA
80 $data .= $space.$space.$space.$space.'<![CDATA[';
81 $data .= $item->presentation;
82 //end of CDATA
83 $data .= ']]>'."\n";
84 //end of presentation
85 $data .= $space.$space.$space.'</PRESENTATION>'."\n";
86
87 //end of item
88 $data .= $space.$space.'</ITEM>'."\n";
89 }
90
91 //writing the footer of the xml file
92 $data .= $space.'</ITEMS>'."\n";
93 $data .= '</FEEDBACK>'."\n";
94
95 return $data;
96 }
97
98 function feedback_send_xml_data($data, $filename) {
99 $charset = get_string('thischarset');
100 @header('Content-Type: application/xml; charset=UTF-8');
101 @header('Content-Disposition: attachment; filename='.$filename);
102 print($data);
103 }
104?>