MDL-14591 many small fixes to portfolio
[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
a656d951 11if (! $data = $DB->get_record('data', array('id'=>$d))) {
2096fcf9 12 print_error('wrongdataid', 'data');
13}
14
15if (! $cm = get_coursemodule_from_instance('data', $data->id, $data->course)) {
16 print_error('invalidcoursemodule');
17}
18
a656d951 19if(! $course = $DB->get_record('course', array('id'=>$cm->course))) {
2096fcf9 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);
86be7720 33require_capability(DATA_CAP_EXPORT, $context);
2096fcf9 34
35// get fields for this database
a656d951 36$fieldrecords = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
2096fcf9 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');
2096fcf9 70 $mform->display();
71 print_footer();
72 die;
73}
74
75$exportdata = array();
76
77// populate the header in first row of export
78foreach($fields as $key => $field) {
79 if(empty($formdata['field_'.$field->field->id])) {
80 // ignore values we aren't exporting
81 unset($fields[$key]);
86be7720 82 } else {
2096fcf9 83 $exportdata[0][] = $field->field->name;
84 }
85}
86
a656d951 87$datarecords = $DB->get_records('data_records', array('dataid'=>$data->id));
2096fcf9 88ksort($datarecords);
89$line = 1;
90foreach($datarecords as $record) {
91 // get content indexed by fieldid
a656d951 92 if( $content = $DB->get_records('data_content', array('recordid'=>$record->id), 'fieldid', 'fieldid, content, content1, content2, content3, content4') ) {
2096fcf9 93 foreach($fields as $field) {
94 $contents = '';
95 if(isset($content[$field->field->id])) {
96 $contents = $field->export_text_value($content[$field->field->id]);
97 }
98 $exportdata[$line][] = $contents;
99 }
100 }
101 $line++;
102}
103$line--;
104
105switch ($formdata['exporttype']) {
106 case 'csv':
107 data_export_csv($exportdata, $formdata['delimiter_name'], $data->name, $line);
108 break;
109 case 'xls':
110 data_export_xls($exportdata, $data->name, $line);
111 break;
112 case 'ods':
113 data_export_ods($exportdata, $data->name, $line);
114 break;
115}
116
20988152 117die();
118?>