weekly release 2.4dev
[moodle.git] / mod / data / export.php
CommitLineData
6111b2b0 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 */
2096fcf9 25
26require_once('../../config.php');
27require_once('lib.php');
2096fcf9 28require_once('export_form.php');
29
2096fcf9 30// database ID
6111b2b0 31$d = required_param('d', PARAM_INT);
32
a6855934 33$PAGE->set_url('/mod/data/export.php', array('d'=>$d));
2096fcf9 34
a656d951 35if (! $data = $DB->get_record('data', array('id'=>$d))) {
2096fcf9 36 print_error('wrongdataid', 'data');
37}
38
39if (! $cm = get_coursemodule_from_instance('data', $data->id, $data->course)) {
40 print_error('invalidcoursemodule');
41}
42
a656d951 43if(! $course = $DB->get_record('course', array('id'=>$cm->course))) {
19a86468 44 print_error('invalidcourseid');
2096fcf9 45}
46
47// fill in missing properties needed for updating of instance
48$data->course = $cm->course;
49$data->cmidnumber = $cm->idnumber;
50$data->instance = $cm->instance;
51
0b3b8c30 52$context = context_module::instance($cm->id);
2096fcf9 53
cdbea7ee 54require_login($course, false, $cm);
86be7720 55require_capability(DATA_CAP_EXPORT, $context);
2096fcf9 56
57// get fields for this database
a656d951 58$fieldrecords = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
2096fcf9 59
60if(empty($fieldrecords)) {
2096fcf9 61 if (has_capability('mod/data:managetemplates', $context)) {
62 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id);
63 } else {
64 print_error('nofieldindatabase', 'data');
65 }
66}
67
68// populate objets for this databases fields
69$fields = array();
70foreach ($fieldrecords as $fieldrecord) {
71 $fields[]= data_get_field($fieldrecord, $data);
72}
73
29b64a22 74
adbf3cd0 75$mform = new mod_data_export_form('export.php?d='.$data->id, $fields, $cm);
2096fcf9 76
77if($mform->is_cancelled()) {
78 redirect('view.php?d='.$data->id);
79} elseif (!$formdata = (array) $mform->get_data()) {
80 // build header to match the rest of the UI
b0ff558c 81 $PAGE->set_title($data->name);
91bc072a 82 $PAGE->set_heading($course->fullname);
b0ff558c 83 echo $OUTPUT->header();
83468603
AG
84 $url = new moodle_url('/mod/data/export.php', array('d' => $d));
85 groups_print_activity_menu($cm, $url);
b2dc6880 86 echo $OUTPUT->heading(format_string($data->name));
2096fcf9 87
88 // these are for the tab display
89 $currentgroup = groups_get_activity_group($cm);
90 $groupmode = groups_get_activity_groupmode($cm);
91 $currenttab = 'export';
92 include('tabs.php');
2096fcf9 93 $mform->display();
7b30ba8b 94 echo $OUTPUT->footer();
2096fcf9 95 die;
96}
97
ecd06483 98$selectedfields = array();
99foreach ($formdata as $key => $value) {
b133c4bb
AD
100 //field form elements are field_1 field_2 etc. 0 if not selected. 1 if selected.
101 if (strpos($key, 'field_')===0 && !empty($value)) {
ecd06483 102 $selectedfields[] = substr($key, 6);
2096fcf9 103 }
2096fcf9 104}
b133c4bb 105
83468603
AG
106$currentgroup = groups_get_activity_group($cm);
107
108$exportdata = data_get_exportdata($data->id, $fields, $selectedfields, $currentgroup);
ecd06483 109$count = count($exportdata);
2096fcf9 110switch ($formdata['exporttype']) {
111 case 'csv':
ecd06483 112 data_export_csv($exportdata, $formdata['delimiter_name'], $data->name, $count);
2096fcf9 113 break;
114 case 'xls':
ecd06483 115 data_export_xls($exportdata, $data->name, $count);
2096fcf9 116 break;
117 case 'ods':
ecd06483 118 data_export_ods($exportdata, $data->name, $count);
2096fcf9 119 break;
120}
121
20988152 122die();