[MDL-8407] Add two new files that contain the core CSV/XLS/ODS export code.
[moodle.git] / mod / data / export.php
CommitLineData
2096fcf9 1<?php // $Id$
2
3require_once('../../config.php');
4require_once('lib.php');
5require_once($CFG->libdir . '/csvlib.class.php');
6require_once('export_form.php');
7
8$d = required_param('d', PARAM_INT);
9// database ID
10
11if (! $data = get_record('data', 'id', $d)) {
12 print_error('wrongdataid', 'data');
13}
14
15if (! $cm = get_coursemodule_from_instance('data', $data->id, $data->course)) {
16 print_error('invalidcoursemodule');
17}
18
19if(! $course = get_record('course', 'id', $cm->course)) {
20 print_error('invalidcourseid', '', '', $cm->course);
21}
22
23// fill in missing properties needed for updating of instance
24$data->course = $cm->course;
25$data->cmidnumber = $cm->idnumber;
26$data->instance = $cm->instance;
27
28if (! $context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
29 print_error('invalidcontext', '');
30}
31
32require_login($course->id, false, $cm);
33require_capability('mod/data:managetemplates', $context);
34
35// get fields for this database
36$fieldrecords = get_records('data_fields','dataid', $data->id, 'id');
37
38if(empty($fieldrecords)) {
39 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
40 if (has_capability('mod/data:managetemplates', $context)) {
41 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id);
42 } else {
43 print_error('nofieldindatabase', 'data');
44 }
45}
46
47// populate objets for this databases fields
48$fields = array();
49foreach ($fieldrecords as $fieldrecord) {
50 $fields[]= data_get_field($fieldrecord, $data);
51}
52
53$mform = new mod_data_export_form('export.php?d='.$data->id, $fields);
54
55if($mform->is_cancelled()) {
56 redirect('view.php?d='.$data->id);
57} elseif (!$formdata = (array) $mform->get_data()) {
58 // build header to match the rest of the UI
59 $nav = build_navigation('', $cm);
60 print_header_simple($data->name, '', $nav,
61 '', '', true, update_module_button($cm->id, $course->id, get_string('modulename', 'data')),
62 navmenu($course, $cm), '', '');
63 print_heading(format_string($data->name));
64
65 // these are for the tab display
66 $currentgroup = groups_get_activity_group($cm);
67 $groupmode = groups_get_activity_groupmode($cm);
68 $currenttab = 'export';
69 include('tabs.php');
70
71 $mform->display();
72 print_footer();
73 die;
74}
75
76$exportdata = array();
77
78// populate the header in first row of export
79foreach($fields as $key => $field) {
80 if(empty($formdata['field_'.$field->field->id])) {
81 // ignore values we aren't exporting
82 unset($fields[$key]);
83 } else{
84 $exportdata[0][] = $field->field->name;
85 }
86}
87
88$datarecords = get_records('data_records', 'dataid', $data->id);
89ksort($datarecords);
90$line = 1;
91foreach($datarecords as $record) {
92 // get content indexed by fieldid
93 if( $content = get_records('data_content', 'recordid', $record->id, 'fieldid', 'fieldid, content, content1, content2, content3, content4') ) {
94 foreach($fields as $field) {
95 $contents = '';
96 if(isset($content[$field->field->id])) {
97 $contents = $field->export_text_value($content[$field->field->id]);
98 }
99 $exportdata[$line][] = $contents;
100 }
101 }
102 $line++;
103}
104$line--;
105
106switch ($formdata['exporttype']) {
107 case 'csv':
108 data_export_csv($exportdata, $formdata['delimiter_name'], $data->name, $line);
109 break;
110 case 'xls':
111 data_export_xls($exportdata, $data->name, $line);
112 break;
113 case 'ods':
114 data_export_ods($exportdata, $data->name, $line);
115 break;
116}
117
118
119function data_export_csv($export, $delimiter_name, $dataname, $count) {
120 $delimiter = csv_import_reader::get_delimiter($delimiter_name);
121 $filename = clean_filename("${dataname}-${count}_record");
122 if ($count > 1) {
123 $filename .= 's';
124 }
125 $filename .= clean_filename('-' . gmdate("Ymd_Hi"));
126 $filename .= clean_filename("-${delimiter_name}_separated");
127 $filename .= '.csv';
128 header("Content-Type: application/download\n");
129 header("Content-Disposition: attachment; filename=$filename");
130 header('Expires: 0');
131 header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
132 header('Pragma: public');
133 $encdelim = '&#' . ord($delimiter) . ';';
134 foreach($export as $row) {
135 foreach($row as $key => $column) {
136 $row[$key] = str_replace($delimiter, $encdelim, $column);
137 }
138 echo implode($delimiter, $row) . "\n";
139 }
140 die;
141}
142
143
144function data_export_xls($export, $dataname, $count) {
145 global $CFG;
146 require_once("$CFG->libdir/excellib.class.php");
147 $filename = clean_filename("${dataname}-${count}_record");
148 if ($count > 1) {
149 $filename .= 's';
150 }
151 $filename .= clean_filename('-' . gmdate("Ymd_Hi"));
152 $filename .= '.xls';
153 $workbook = new MoodleExcelWorkbook('-');
154 $workbook->send($filename);
155 $worksheet = array();
156 $worksheet[0] =& $workbook->add_worksheet('');
157 $rowno = 0;
158 foreach ($export as $row) {
159 $colno = 0;
160 foreach($row as $col) {
161 $worksheet[0]->write($rowno, $colno, $col);
162 $colno++;
163 }
164 $rowno++;
165 }
166 $workbook->close();
167 die;
168}
169
170
171function data_export_ods($export, $dataname, $count) {
172 global $CFG;
173 require_once("$CFG->libdir/odslib.class.php");
174 $filename = clean_filename("${dataname}-${count}_record");
175 if ($count > 1) {
176 $filename .= 's';
177 }
178 $filename .= clean_filename('-' . gmdate("Ymd_Hi"));
179 $filename .= '.ods';
180 $workbook = new MoodleODSWorkbook('-');
181 $workbook->send($filename);
182 $worksheet = array();
183 $worksheet[0] =& $workbook->add_worksheet('');
184 $rowno = 0;
185 foreach ($export as $row) {
186 $colno = 0;
187 foreach($row as $col) {
188 $worksheet[0]->write($rowno, $colno, $col);
189 $colno++;
190 }
191 $rowno++;
192 }
193 $workbook->close();
194 die;
195}