Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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  
  37  $PAGE->set_url('/mod/data/export.php', array('d'=>$d));
  38  
  39  if (! $data = $DB->get_record('data', array('id'=>$d))) {
  40      print_error('wrongdataid', 'data');
  41  }
  42  
  43  if (! $cm = get_coursemodule_from_instance('data', $data->id, $data->course)) {
  44      print_error('invalidcoursemodule');
  45  }
  46  
  47  if(! $course = $DB->get_record('course', array('id'=>$cm->course))) {
  48      print_error('invalidcourseid');
  49  }
  50  
  51  // fill in missing properties needed for updating of instance
  52  $data->course     = $cm->course;
  53  $data->cmidnumber = $cm->idnumber;
  54  $data->instance   = $cm->instance;
  55  
  56  $context = context_module::instance($cm->id);
  57  
  58  require_login($course, false, $cm);
  59  require_capability(DATA_CAP_EXPORT, $context);
  60  
  61  // get fields for this database
  62  $fieldrecords = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
  63  
  64  if(empty($fieldrecords)) {
  65      if (has_capability('mod/data:managetemplates', $context)) {
  66          redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id);
  67      } else {
  68          print_error('nofieldindatabase', 'data');
  69      }
  70  }
  71  
  72  // populate objets for this databases fields
  73  $fields = array();
  74  foreach ($fieldrecords as $fieldrecord) {
  75      $fields[]= data_get_field($fieldrecord, $data);
  76  }
  77  
  78  
  79  $mform = new mod_data_export_form('export.php?d='.$data->id, $fields, $cm, $data);
  80  
  81  if($mform->is_cancelled()) {
  82      redirect('view.php?d='.$data->id);
  83  } elseif (!$formdata = (array) $mform->get_data()) {
  84      // build header to match the rest of the UI
  85      $PAGE->set_title($data->name);
  86      $PAGE->set_heading($course->fullname);
  87      $PAGE->force_settings_menu(true);
  88      echo $OUTPUT->header();
  89      echo $OUTPUT->heading(format_string($data->name), 2);
  90  
  91      // Render the activity information.
  92      $cminfo = cm_info::create($cm);
  93      $completiondetails = \core_completion\cm_completion_details::get_instance($cminfo, $USER->id);
  94      $activitydates = \core\activity_dates::get_dates_for_module($cminfo, $USER->id);
  95      echo $OUTPUT->activity_information($cminfo, $completiondetails, $activitydates);
  96  
  97      echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
  98  
  99      $url = new moodle_url('/mod/data/export.php', array('d' => $d));
 100      groups_print_activity_menu($cm, $url);
 101  
 102      // these are for the tab display
 103      $currentgroup = groups_get_activity_group($cm);
 104      $groupmode = groups_get_activity_groupmode($cm);
 105      $currenttab = 'export';
 106      include ('tabs.php');
 107      $mform->display();
 108      echo $OUTPUT->footer();
 109      die;
 110  }
 111  
 112  $selectedfields = array();
 113  foreach ($formdata as $key => $value) {
 114      //field form elements are field_1 field_2 etc. 0 if not selected. 1 if selected.
 115      if (strpos($key, 'field_')===0 && !empty($value)) {
 116          $selectedfields[] = substr($key, 6);
 117      }
 118  }
 119  
 120  $currentgroup = groups_get_activity_group($cm);
 121  
 122  $exportdata = data_get_exportdata($data->id, $fields, $selectedfields, $currentgroup, $context,
 123                                    $exportuser, $exporttime, $exportapproval, $tags);
 124  $count = count($exportdata);
 125  switch ($formdata['exporttype']) {
 126      case 'csv':
 127          data_export_csv($exportdata, $formdata['delimiter_name'], $data->name, $count);
 128          break;
 129      case 'xls':
 130          data_export_xls($exportdata, $data->name, $count);
 131          break;
 132      case 'ods':
 133          data_export_ods($exportdata, $data->name, $count);
 134          break;
 135  }
 136  
 137  die();