Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * This file is part of the Database module for Moodle
  20   *
  21   * @copyright 2005 Martin Dougiamas  http://dougiamas.com
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   * @package mod_data
  24   */
  25  
  26  require_once('../../config.php');
  27  require_once ('lib.php');
  28  require_once ('export_form.php');
  29  
  30  // database ID
  31  $d = required_param('d', PARAM_INT);
  32  $exportuser = optional_param('exportuser', false, PARAM_BOOL); // Flag for exporting user details
  33  $exporttime = optional_param('exporttime', false, PARAM_BOOL); // Flag for exporting date/time information
  34  $exportapproval = optional_param('exportapproval', false, PARAM_BOOL); // Flag for exporting user details
  35  $tags = optional_param('exporttags', false, PARAM_BOOL); // Flag for exporting user details.
  36  $redirectbackto = optional_param('backto', '', PARAM_LOCALURL); // The location to redirect back to.
  37  
  38  $url = new moodle_url('/mod/data/export.php', array('d' => $d));
  39  $PAGE->set_url($url);
  40  
  41  if (! $data = $DB->get_record('data', array('id'=>$d))) {
  42      throw new \moodle_exception('wrongdataid', 'data');
  43  }
  44  
  45  if (! $cm = get_coursemodule_from_instance('data', $data->id, $data->course)) {
  46      throw new \moodle_exception('invalidcoursemodule');
  47  }
  48  
  49  if(! $course = $DB->get_record('course', array('id'=>$cm->course))) {
  50      throw new \moodle_exception('invalidcourseid');
  51  }
  52  
  53  // fill in missing properties needed for updating of instance
  54  $data->course     = $cm->course;
  55  $data->cmidnumber = $cm->idnumber;
  56  $data->instance   = $cm->instance;
  57  
  58  $context = context_module::instance($cm->id);
  59  
  60  require_login($course, false, $cm);
  61  require_capability(DATA_CAP_EXPORT, $context);
  62  
  63  // get fields for this database
  64  $fieldrecords = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
  65  
  66  if(empty($fieldrecords)) {
  67      if (has_capability('mod/data:managetemplates', $context)) {
  68          redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id);
  69      } else {
  70          throw new \moodle_exception('nofieldindatabase', 'data');
  71      }
  72  }
  73  
  74  // populate objets for this databases fields
  75  $fields = array();
  76  foreach ($fieldrecords as $fieldrecord) {
  77      $fields[]= data_get_field($fieldrecord, $data);
  78  }
  79  
  80  $mform = new mod_data_export_form(new moodle_url('/mod/data/export.php', ['d' => $data->id,
  81      'backto' => $redirectbackto]), $fields, $cm, $data);
  82  
  83  if ($mform->is_cancelled()) {
  84      $redirectbackto = !empty($redirectbackto) ? $redirectbackto :
  85          new \moodle_url('/mod/data/view.php', ['d' => $data->id]);
  86      redirect($redirectbackto);
  87  } else if ($formdata = (array) $mform->get_data()) {
  88      $selectedfields = array();
  89      foreach ($formdata as $key => $value) {
  90          //field form elements are field_1 field_2 etc. 0 if not selected. 1 if selected.
  91          if (strpos($key, 'field_')===0 && !empty($value)) {
  92              $selectedfields[] = substr($key, 6);
  93          }
  94      }
  95  
  96      $currentgroup = groups_get_activity_group($cm);
  97  
  98      $exportdata = data_get_exportdata($data->id, $fields, $selectedfields, $currentgroup, $context,
  99          $exportuser, $exporttime, $exportapproval, $tags);
 100      $count = count($exportdata);
 101      switch ($formdata['exporttype']) {
 102          case 'csv':
 103              data_export_csv($exportdata, $formdata['delimiter_name'], $data->name, $count);
 104              break;
 105          case 'xls':
 106              data_export_xls($exportdata, $data->name, $count);
 107              break;
 108          case 'ods':
 109              data_export_ods($exportdata, $data->name, $count);
 110              break;
 111      }
 112  }
 113  
 114  // Build header to match the rest of the UI.
 115  $PAGE->add_body_class('mediumwidth');
 116  $pagename = get_string('exportentries', 'data');
 117  $titleparts = [
 118      $pagename,
 119      format_string($data->name),
 120      format_string($course->fullname),
 121  ];
 122  $PAGE->set_title(implode(moodle_page::TITLE_SEPARATOR, $titleparts));
 123  $PAGE->set_heading($course->fullname);
 124  $PAGE->force_settings_menu(true);
 125  $PAGE->set_secondary_active_tab('modulepage');
 126  $PAGE->activityheader->disable();
 127  echo $OUTPUT->header();
 128  echo $OUTPUT->heading($pagename);
 129  
 130  groups_print_activity_menu($cm, $url);
 131  
 132  $mform->display();
 133  
 134  echo $OUTPUT->footer();
 135  
 136  die();