3 require_once('../../config.php');
4 require_once('lib.php');
5 require_once($CFG->libdir
. '/csvlib.class.php');
6 require_once('export_form.php');
8 $d = required_param('d', PARAM_INT
);
11 if (! $data = get_record('data', 'id', $d)) {
12 print_error('wrongdataid', 'data');
15 if (! $cm = get_coursemodule_from_instance('data', $data->id
, $data->course
)) {
16 print_error('invalidcoursemodule');
19 if(! $course = get_record('course', 'id', $cm->course
)) {
20 print_error('invalidcourseid', '', '', $cm->course
);
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
;
28 if (! $context = get_context_instance(CONTEXT_MODULE
, $cm->id
)) {
29 print_error('invalidcontext', '');
32 require_login($course->id
, false, $cm);
33 require_capability(DATA_CAP_EXPORT
, $context);
35 // get fields for this database
36 $fieldrecords = get_records('data_fields','dataid', $data->id
, 'id');
38 if(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
);
43 print_error('nofieldindatabase', 'data');
47 // populate objets for this databases fields
49 foreach ($fieldrecords as $fieldrecord) {
50 $fields[]= data_get_field($fieldrecord, $data);
53 $mform = new mod_data_export_form('export.php?d='.$data->id
, $fields);
55 if($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
));
65 // these are for the tab display
66 $currentgroup = groups_get_activity_group($cm);
67 $groupmode = groups_get_activity_groupmode($cm);
68 $currenttab = 'export';
75 $exportdata = array();
77 // populate the header in first row of export
78 foreach($fields as $key => $field) {
79 if(empty($formdata['field_'.$field->field
->id
])) {
80 // ignore values we aren't exporting
83 $exportdata[0][] = $field->field
->name
;
87 $datarecords = get_records('data_records', 'dataid', $data->id
);
90 foreach($datarecords as $record) {
91 // get content indexed by fieldid
92 if( $content = get_records('data_content', 'recordid', $record->id
, 'fieldid', 'fieldid, content, content1, content2, content3, content4') ) {
93 foreach($fields as $field) {
95 if(isset($content[$field->field
->id
])) {
96 $contents = $field->export_text_value($content[$field->field
->id
]);
98 $exportdata[$line][] = $contents;
105 switch ($formdata['exporttype']) {
107 data_export_csv($exportdata, $formdata['delimiter_name'], $data->name
, $line);
110 data_export_xls($exportdata, $data->name
, $line);
113 data_export_ods($exportdata, $data->name
, $line);
118 function data_export_csv($export, $delimiter_name, $dataname, $count) {
119 $delimiter = csv_import_reader
::get_delimiter($delimiter_name);
120 $filename = clean_filename("${dataname}-${count}_record");
124 $filename .= clean_filename('-' . gmdate("Ymd_Hi"));
125 $filename .= clean_filename("-${delimiter_name}_separated");
127 header("Content-Type: application/download\n");
128 header("Content-Disposition: attachment; filename=$filename");
129 header('Expires: 0');
130 header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
131 header('Pragma: public');
132 $encdelim = '&#' . ord($delimiter) . ';';
133 foreach($export as $row) {
134 foreach($row as $key => $column) {
135 $row[$key] = str_replace($delimiter, $encdelim, $column);
137 echo implode($delimiter, $row) . "\n";
143 function data_export_xls($export, $dataname, $count) {
145 require_once("$CFG->libdir/excellib.class.php");
146 $filename = clean_filename("${dataname}-${count}_record");
150 $filename .= clean_filename('-' . gmdate("Ymd_Hi"));
152 $workbook = new MoodleExcelWorkbook('-');
153 $workbook->send($filename);
154 $worksheet = array();
155 $worksheet[0] =& $workbook->add_worksheet('');
157 foreach ($export as $row) {
159 foreach($row as $col) {
160 $worksheet[0]->write($rowno, $colno, $col);
170 function data_export_ods($export, $dataname, $count) {
172 require_once("$CFG->libdir/odslib.class.php");
173 $filename = clean_filename("${dataname}-${count}_record");
177 $filename .= clean_filename('-' . gmdate("Ymd_Hi"));
179 $workbook = new MoodleODSWorkbook('-');
180 $workbook->send($filename);
181 $worksheet = array();
182 $worksheet[0] =& $workbook->add_worksheet('');
184 foreach ($export as $row) {
186 foreach($row as $col) {
187 $worksheet[0]->write($rowno, $colno, $col);