mod-data MDL-19806 Added set_url calls, replaced deprecated functions, and added...
[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)) {
5d2edd1c 20 print_error('invalidcoursemodule');
c70ad9f7 21 }
22
0085fff8 23 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
5d2edd1c 24 print_error('coursemisconf');
c70ad9f7 25 }
26
0085fff8 27 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
5d2edd1c 28 print_error('invalidcoursemodule');
c70ad9f7 29 }
30 }
31 $capabilities = feedback_load_capabilities($cm->id);
32
6178c62a 33 require_login($course->id, true, $cm);
c70ad9f7 34
35 if(!$capabilities->edititems){
5d2edd1c 36 print_error('invalidaction');
c70ad9f7 37 }
38
39 if ($action == 'exportfile') {
40 if(!$exportdata = feedback_get_xml_data($feedback->id)) {
5d2edd1c 41 print_error('nodata');
c70ad9f7 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) {
0085fff8 51 global $DB;
52
c70ad9f7 53 $space = ' ';
54 //get all items of the feedback
0085fff8 55 if(!$items = $DB->get_records('feedback_item', array('feedback'=>$feedbackid), 'position')) {
c70ad9f7 56 return false;
57 }
58
59 //writing the header of the xml file including the charset of the currrent used language
60 $data = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
61 $data .= '<FEEDBACK VERSION="200701" COMMENT="XML-Importfile for mod/feedback">'."\n";
62 $data .= $space.'<ITEMS>'."\n";
63
64 //writing all the items
65 foreach($items as $item) {
66 //start of item
67 $data .= $space.$space.'<ITEM TYPE="'.$item->typ.'" REQUIRED="'.$item->required.'">'."\n";
68
69 //start of itemtext
70 $data .= $space.$space.$space.'<ITEMTEXT>'."\n";
71 //start of CDATA
72 $data .= $space.$space.$space.$space.'<![CDATA[';
73 $data .= $item->name;
74 //end of CDATA
75 $data .= ']]>'."\n";
76 //end of itemtext
77 $data .= $space.$space.$space.'</ITEMTEXT>'."\n";
78
79 //start of presentation
80 $data .= $space.$space.$space.'<PRESENTATION>'."\n";
81 //start of CDATA
82 $data .= $space.$space.$space.$space.'<![CDATA[';
83 $data .= $item->presentation;
84 //end of CDATA
85 $data .= ']]>'."\n";
86 //end of presentation
87 $data .= $space.$space.$space.'</PRESENTATION>'."\n";
88
89 //end of item
90 $data .= $space.$space.'</ITEM>'."\n";
91 }
92
93 //writing the footer of the xml file
94 $data .= $space.'</ITEMS>'."\n";
95 $data .= '</FEEDBACK>'."\n";
96
97 return $data;
98 }
99
100 function feedback_send_xml_data($data, $filename) {
101 $charset = get_string('thischarset');
102 @header('Content-Type: application/xml; charset=UTF-8');
103 @header('Content-Disposition: attachment; filename='.$filename);
104 print($data);
105 }
106?>