output MDL-20327 Made moodle_core_renderer::link set a target attribute if CFG->frame...
[moodle.git] / mod / data / export.php
CommitLineData
2096fcf9 1<?php // $Id$
2
3require_once('../../config.php');
4require_once('lib.php');
2096fcf9 5require_once('export_form.php');
6
7$d = required_param('d', PARAM_INT);
8// database ID
9
a656d951 10if (! $data = $DB->get_record('data', array('id'=>$d))) {
2096fcf9 11 print_error('wrongdataid', 'data');
12}
13
14if (! $cm = get_coursemodule_from_instance('data', $data->id, $data->course)) {
15 print_error('invalidcoursemodule');
16}
17
a656d951 18if(! $course = $DB->get_record('course', array('id'=>$cm->course))) {
2096fcf9 19 print_error('invalidcourseid', '', '', $cm->course);
20}
21
22// fill in missing properties needed for updating of instance
23$data->course = $cm->course;
24$data->cmidnumber = $cm->idnumber;
25$data->instance = $cm->instance;
26
27if (! $context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
28 print_error('invalidcontext', '');
29}
30
31require_login($course->id, false, $cm);
86be7720 32require_capability(DATA_CAP_EXPORT, $context);
2096fcf9 33
34// get fields for this database
a656d951 35$fieldrecords = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
2096fcf9 36
37if(empty($fieldrecords)) {
38 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
39 if (has_capability('mod/data:managetemplates', $context)) {
40 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id);
41 } else {
42 print_error('nofieldindatabase', 'data');
43 }
44}
45
46// populate objets for this databases fields
47$fields = array();
48foreach ($fieldrecords as $fieldrecord) {
49 $fields[]= data_get_field($fieldrecord, $data);
50}
51
29b64a22 52$PAGE->navbar->add(get_string('export','data'));
53
adbf3cd0 54$mform = new mod_data_export_form('export.php?d='.$data->id, $fields, $cm);
2096fcf9 55
56if($mform->is_cancelled()) {
57 redirect('view.php?d='.$data->id);
58} elseif (!$formdata = (array) $mform->get_data()) {
59 // build header to match the rest of the UI
b0ff558c 60 $PAGE->set_title($data->name);
61 $PAGE->set_button(update_module_button($cm->id, $course->id, get_string('modulename', 'data')));
62 echo $OUTPUT->header();
b2dc6880 63 echo $OUTPUT->heading(format_string($data->name));
2096fcf9 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();
7b30ba8b 71 echo $OUTPUT->footer();
2096fcf9 72 die;
73}
74
ecd06483 75if (array_key_exists('portfolio', $formdata) && !empty($formdata['portfolio'])) {
76 // fake portfolio callback stuff and redirect
77 $formdata['id'] = $cm->id;
1bf8c6b2 78 $formdata['exporttype'] = 'csv'; // force for now
ecd06483 79 $url = portfolio_fake_add_url($formdata['portfolio'], 'data_portfolio_caller', '/mod/data/lib.php', $formdata);
80 redirect($url);
2096fcf9 81}
82
ecd06483 83$selectedfields = array();
84foreach ($formdata as $key => $value) {
85 if (strpos($key, 'field_') === 0) {
86 $selectedfields[] = substr($key, 6);
2096fcf9 87 }
2096fcf9 88}
ecd06483 89$exportdata = data_get_exportdata($data->id, $fields, $selectedfields);
90$count = count($exportdata);
2096fcf9 91switch ($formdata['exporttype']) {
92 case 'csv':
ecd06483 93 data_export_csv($exportdata, $formdata['delimiter_name'], $data->name, $count);
2096fcf9 94 break;
95 case 'xls':
ecd06483 96 data_export_xls($exportdata, $data->name, $count);
2096fcf9 97 break;
98 case 'ods':
ecd06483 99 data_export_ods($exportdata, $data->name, $count);
2096fcf9 100 break;
101}
102
20988152 103die();
104?>