Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 403]

   1  <?php
   2  
   3  if (!defined('MOODLE_INTERNAL')) {
   4      die('Direct access to this script is forbidden!');
   5  }
   6  require_once($CFG->libdir . '/formslib.php');
   7  require_once($CFG->libdir . '/csvlib.class.php');
   8  
   9  class mod_data_export_form extends moodleform {
  10      var $_datafields = array();
  11      var $_cm;
  12      var $_data;
  13  
  14       // @param string $url: the url to post to
  15       // @param array $datafields: objects in this database
  16      public function __construct($url, $datafields, $cm, $data) {
  17          $this->_datafields = $datafields;
  18          $this->_cm = $cm;
  19          $this->_data = $data;
  20          parent::__construct($url);
  21      }
  22  
  23      /**
  24       * Old syntax of class constructor. Deprecated in PHP7.
  25       *
  26       * @deprecated since Moodle 3.1
  27       */
  28      public function mod_data_export_form($url, $datafields, $cm, $data) {
  29          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  30          self::__construct($url, $datafields, $cm, $data);
  31      }
  32  
  33      function definition() {
  34          $mform =& $this->_form;
  35          $mform->addElement('header', 'exportformat', get_string('chooseexportformat', 'data'));
  36  
  37          $optionattrs = ['class' => 'mt-1 mb-1'];
  38  
  39          // Export format type radio group.
  40          $typesarray = array();
  41          $typesarray[] = $mform->createElement('radio', 'exporttype', null, get_string('csvwithselecteddelimiter', 'data'), 'csv',
  42              $optionattrs);
  43          // Temporarily commenting out Excel export option. See MDL-19864.
  44          //$typesarray[] = $mform->createElement('radio', 'exporttype', null, get_string('excel', 'data'), 'xls');
  45          $typesarray[] = $mform->createElement('radio', 'exporttype', null, get_string('ods', 'data'), 'ods', $optionattrs);
  46          $mform->addGroup($typesarray, 'exportar', get_string('exportformat', 'data'), null, false);
  47          $mform->addRule('exportar', null, 'required');
  48          $mform->setDefault('exporttype', 'csv');
  49  
  50          // CSV delimiter list.
  51          $choices = csv_import_reader::get_delimiter_list();
  52          $key = array_search(';', $choices);
  53          if ($key !== false) {
  54              // Array $choices contains the semicolon -> drop it (because its encrypted form also contains a semicolon):
  55              unset($choices[$key]);
  56          }
  57          $mform->addElement('select', 'delimiter_name', get_string('fielddelimiter', 'data'), $choices);
  58          $mform->hideIf('delimiter_name', 'exporttype', 'neq', 'csv');
  59          if (array_key_exists('cfg', $choices)) {
  60              $mform->setDefault('delimiter_name', 'cfg');
  61          } else if (get_string('listsep', 'langconfig') == ';') {
  62              $mform->setDefault('delimiter_name', 'semicolon');
  63          } else {
  64              $mform->setDefault('delimiter_name', 'comma');
  65          }
  66  
  67          // Fields to be exported.
  68          $mform->addElement('header', 'exportfieldsheader', get_string('chooseexportfields', 'data'));
  69          $mform->setExpanded('exportfieldsheader');
  70          $numfieldsthatcanbeselected = 0;
  71          $exportfields = [];
  72          $unsupportedfields = [];
  73          foreach ($this->_datafields as $field) {
  74              $label = get_string('fieldnametype', 'data', (object)['name' => $field->field->name, 'type' => $field->name()]);
  75              if ($field->text_export_supported()) {
  76                  $numfieldsthatcanbeselected++;
  77                  $exportfields[] = $mform->createElement('advcheckbox', 'field_' . $field->field->id, '', $label,
  78                      array_merge(['group' => 1], $optionattrs));
  79                  $mform->setDefault('field_' . $field->field->id, 1);
  80              } else {
  81                  $unsupportedfields[] = $label;
  82              }
  83          }
  84          $mform->addGroup($exportfields, 'exportfields', get_string('selectfields', 'data'), ['<br>'], false);
  85  
  86          if ($numfieldsthatcanbeselected > 1) {
  87              $this->add_checkbox_controller(1, null, null, 1);
  88          }
  89  
  90          // List fields that cannot be exported.
  91          if (!empty($unsupportedfields)) {
  92              $unsupportedfieldslist = html_writer::tag('p', get_string('unsupportedfieldslist', 'data'), ['class' => 'mt-1']);
  93              $unsupportedfieldslist .= html_writer::alist($unsupportedfields);
  94              $mform->addElement('static', 'unsupportedfields', get_string('unsupportedfields', 'data'), $unsupportedfieldslist);
  95          }
  96  
  97          // Export options.
  98          $mform->addElement('header', 'exportoptionsheader', get_string('exportoptions', 'data'));
  99          $mform->setExpanded('exportoptionsheader');
 100          $exportoptions = [];
 101          if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
 102              $exportoptions[] = $mform->createElement('checkbox', 'exporttags', get_string('includetags', 'data'), '', $optionattrs);
 103              $mform->setDefault('exporttags', 1);
 104          }
 105          $context = context_module::instance($this->_cm->id);
 106          if (has_capability('mod/data:exportuserinfo', $context)) {
 107              $exportoptions[] = $mform->createElement('checkbox', 'exportuser', get_string('includeuserdetails', 'data'), '',
 108                  $optionattrs);
 109          }
 110          $exportoptions[] = $mform->createElement('checkbox', 'exporttime', get_string('includetime', 'data'), '', $optionattrs);
 111          if ($this->_data->approval) {
 112              $exportoptions[] = $mform->createElement('checkbox', 'exportapproval', get_string('includeapproval', 'data'), '',
 113                  $optionattrs);
 114          }
 115          $mform->addGroup($exportoptions, 'exportoptions', get_string('selectexportoptions', 'data'), ['<br>'], false);
 116  
 117          $this->add_action_buttons(true, get_string('exportentries', 'data'));
 118      }
 119  
 120  }
 121  
 122