Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   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.
   8  //
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * prints the form to export the items as xml-file
  19   *
  20   * @author Andreas Grabs
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  22   * @package mod_feedback
  23   */
  24  
  25  require_once("../../config.php");
  26  require_once ("lib.php");
  27  
  28  // get parameters
  29  $id = required_param('id', PARAM_INT);
  30  $action = optional_param('action', false, PARAM_ALPHA);
  31  
  32  $url = new moodle_url('/mod/feedback/export.php', array('id'=>$id));
  33  if ($action !== false) {
  34      $url->param('action', $action);
  35  }
  36  $PAGE->set_url($url);
  37  
  38  if (! $cm = get_coursemodule_from_id('feedback', $id)) {
  39      throw new \moodle_exception('invalidcoursemodule');
  40  }
  41  
  42  if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
  43      throw new \moodle_exception('coursemisconf');
  44  }
  45  
  46  if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
  47      throw new \moodle_exception('invalidcoursemodule');
  48  }
  49  
  50  $context = context_module::instance($cm->id);
  51  
  52  require_login($course, true, $cm);
  53  
  54  require_capability('mod/feedback:edititems', $context);
  55  
  56  if ($action == 'exportfile') {
  57      if (!$exportdata = feedback_get_xml_data($feedback->id)) {
  58          throw new \moodle_exception('nodata');
  59      }
  60      @feedback_send_xml_data($exportdata, 'feedback_'.$feedback->id.'.xml');
  61      exit;
  62  }
  63  
  64  redirect('view.php?id='.$id);
  65  exit;
  66  
  67  function feedback_get_xml_data($feedbackid) {
  68      global $DB;
  69  
  70      $space = '     ';
  71      //get all items of the feedback
  72      if (!$items = $DB->get_records('feedback_item', array('feedback'=>$feedbackid), 'position')) {
  73          return false;
  74      }
  75  
  76      //writing the header of the xml file including the charset of the currrent used language
  77      $data = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
  78      $data .= '<FEEDBACK VERSION="200701" COMMENT="XML-Importfile for mod/feedback">'."\n";
  79      $data .= $space.'<ITEMS>'."\n";
  80  
  81      //writing all the items
  82      foreach ($items as $item) {
  83          //start of item
  84          $data .= $space.$space.'<ITEM TYPE="'.$item->typ.'" REQUIRED="'.$item->required.'">'."\n";
  85  
  86          //start of itemid
  87          $data .= $space.$space.$space.'<ITEMID>'."\n";
  88          //start of CDATA
  89          $data .= $space.$space.$space.$space.'<![CDATA[';
  90          $data .= $item->id;
  91          //end of CDATA
  92          $data .= ']]>'."\n";
  93          //end of itemid
  94          $data .= $space.$space.$space.'</ITEMID>'."\n";
  95  
  96          //start of itemtext
  97          $data .= $space.$space.$space.'<ITEMTEXT>'."\n";
  98          //start of CDATA
  99          $data .= $space.$space.$space.$space.'<![CDATA[';
 100          $data .= $item->name;
 101          //end of CDATA
 102          $data .= ']]>'."\n";
 103          //end of itemtext
 104          $data .= $space.$space.$space.'</ITEMTEXT>'."\n";
 105  
 106          //start of itemtext
 107          $data .= $space.$space.$space.'<ITEMLABEL>'."\n";
 108          //start of CDATA
 109          $data .= $space.$space.$space.$space.'<![CDATA[';
 110          $data .= $item->label;
 111          //end of CDATA
 112          $data .= ']]>'."\n";
 113          //end of itemtext
 114          $data .= $space.$space.$space.'</ITEMLABEL>'."\n";
 115  
 116          //start of presentation
 117          $data .= $space.$space.$space.'<PRESENTATION>'."\n";
 118          //start of CDATA
 119          $data .= $space.$space.$space.$space.'<![CDATA[';
 120          $data .= $item->presentation;
 121          //end of CDATA
 122          $data .= ']]>'."\n";
 123          //end of presentation
 124          $data .= $space.$space.$space.'</PRESENTATION>'."\n";
 125  
 126          //start of options
 127          $data .= $space.$space.$space.'<OPTIONS>'."\n";
 128          //start of CDATA
 129          $data .= $space.$space.$space.$space.'<![CDATA[';
 130          $data .= $item->options;
 131          //end of CDATA
 132          $data .= ']]>'."\n";
 133          //end of options
 134          $data .= $space.$space.$space.'</OPTIONS>'."\n";
 135  
 136          //start of dependitem
 137          $data .= $space.$space.$space.'<DEPENDITEM>'."\n";
 138          //start of CDATA
 139          $data .= $space.$space.$space.$space.'<![CDATA[';
 140          $data .= $item->dependitem;
 141          //end of CDATA
 142          $data .= ']]>'."\n";
 143          //end of dependitem
 144          $data .= $space.$space.$space.'</DEPENDITEM>'."\n";
 145  
 146          //start of dependvalue
 147          $data .= $space.$space.$space.'<DEPENDVALUE>'."\n";
 148          //start of CDATA
 149          $data .= $space.$space.$space.$space.'<![CDATA[';
 150          $data .= $item->dependvalue;
 151          //end of CDATA
 152          $data .= ']]>'."\n";
 153          //end of dependvalue
 154          $data .= $space.$space.$space.'</DEPENDVALUE>'."\n";
 155  
 156          //end of item
 157          $data .= $space.$space.'</ITEM>'."\n";
 158      }
 159  
 160      //writing the footer of the xml file
 161      $data .= $space.'</ITEMS>'."\n";
 162      $data .= '</FEEDBACK>'."\n";
 163  
 164      return $data;
 165  }
 166  
 167  function feedback_send_xml_data($data, $filename) {
 168      @header('Content-Type: application/xml; charset=UTF-8');
 169      @header('Content-Disposition: attachment; filename="'.$filename.'"');
 170      print($data);
 171  }