Index: lang/en_utf8/data.php =================================================================== RCS file: /cvsroot/moodle/moodle/lang/en_utf8/data.php,v retrieving revision 1.53.4.13 diff -u -r1.53.4.13 data.php --- lang/en_utf8/data.php 20 Apr 2008 10:08:04 -0000 1.53.4.13 +++ lang/en_utf8/data.php 18 May 2008 15:54:34 -0000 @@ -29,6 +29,8 @@ $string['bynameondate'] = 'by $a->name - $a->date'; $string['cancel'] = 'Cancel'; $string['checkbox'] = 'Checkbox'; +$string['chooseexportfields'] = 'Choose the fields you wish to export:'; +$string['chooseexportformat'] = 'Choose the format you wish to export to:'; $string['chooseorupload'] = 'Choose file'; $string['columns'] = 'columns'; $string['commentdeleted'] = 'Comment deleted'; @@ -42,6 +44,7 @@ $string['confirmdeleterecord'] = 'Are you sure you want to delete this entry?'; $string['csstemplate'] = 'CSS template'; $string['csvfile'] = 'CSV file'; +$string['csvformat'] = 'CSV format'; $string['csvimport'] = 'CSV file import'; $string['data:approve'] = 'Approve unapproved entries'; $string['data:comment'] = 'Write comments'; @@ -83,8 +86,10 @@ $string['entrysaved'] = 'Your entry has been saved'; $string['errormustbeteacher'] = 'You need to be a teacher to use this page!'; $string['example'] = 'Database module example'; +$string['excelformat'] = 'Excel format'; $string['export'] = 'Export'; $string['exportaszip'] = 'Export as zip'; +$string['exportdatabase'] = 'Export Database records'; $string['exportedtozip'] = 'Exported to temporary zip...'; $string['failedpresetdelete'] = 'Error deleting a preset!'; $string['fieldadded'] = 'Field added'; @@ -176,6 +181,7 @@ $string['numberrssarticles'] = 'RSS articles'; $string['numnotapproved'] = 'Pending'; $string['numrecords'] = '$a entries'; +$string['odsformat'] = 'ODS Format'; $string['optionaldescription'] = 'Short description (optional)'; $string['optionalfilename'] = 'Filename (optional)'; $string['other'] = 'Other'; @@ -236,6 +242,7 @@ $string['todatabase'] = 'to this database.'; $string['type'] = 'Field type'; $string['undefinedprocessactionmethod'] = 'No action method defined in Data_Preset to handle action \"$a\".'; +$string['unsupportedexport'] = '($a->fieldtype) cannot be exported.'; $string['updatefield'] = 'Update an existing field'; $string['uploadfile'] = 'Upload file'; $string['uploadrecords'] = 'Upload entries from a file'; Index: lang/en_utf8/error.php =================================================================== RCS file: /cvsroot/moodle/moodle/lang/en_utf8/error.php,v retrieving revision 1.26.2.10 diff -u -r1.26.2.10 error.php --- lang/en_utf8/error.php 14 May 2008 02:47:35 -0000 1.26.2.10 +++ lang/en_utf8/error.php 18 May 2008 15:25:38 -0000 @@ -48,7 +48,10 @@ $string['groupunknown'] = 'Group $a not associated to specified course'; $string['guestnoeditprofile'] = 'The guest user cannot edit their profile'; $string['guestnoeditprofileother'] = 'The guest user profile cannot be edited'; +$string['invalidcontext'] = 'Invalid context'; $string['invalidcourse'] = 'Invalid course'; +$string['invalidcourseid'] = 'You are tring to use an invalid course ID ($a)'; +$string['invalidcoursemodule'] = 'Invalid course module ID'; $string['invalidfieldname'] = '\"$a\" is not a valid field name'; $string['invalidfiletype'] = '\"$a\" is not a valid file type'; $string['invalidipformat'] = 'Invalid IP address format'; Index: mod/data/field/file/field.class.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/data/field/file/field.class.php,v retrieving revision 1.17.2.3 diff -u -r1.17.2.3 field.class.php --- mod/data/field/file/field.class.php 17 Apr 2008 09:41:56 -0000 1.17.2.3 +++ mod/data/field/file/field.class.php 18 May 2008 14:39:50 -0000 @@ -181,6 +181,9 @@ return false; } + function text_export_supported() { + return false; + } } ?> Index: mod/data/field/latlong/field.class.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/data/field/latlong/field.class.php,v retrieving revision 1.8 diff -u -r1.8 field.class.php --- mod/data/field/latlong/field.class.php 26 Feb 2007 06:56:07 -0000 1.8 +++ mod/data/field/latlong/field.class.php 18 May 2008 14:44:38 -0000 @@ -203,6 +203,10 @@ return $fieldname; } } + + function export_text_value($record) { + return sprintf('%01.4f', $record->content) . " " . sprintf('%01.4f', $record->content1); + } } ?> Index: mod/data/field/picture/field.class.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/data/field/picture/field.class.php,v retrieving revision 1.24 diff -u -r1.24 field.class.php --- mod/data/field/picture/field.class.php 5 Aug 2007 16:42:31 -0000 1.24 +++ mod/data/field/picture/field.class.php 18 May 2008 14:39:52 -0000 @@ -283,6 +283,10 @@ @unlink($thumbnaillocation); } } + + function text_export_supported() { + return false; + } } ?> Index: mod/data/field/url/field.class.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/data/field/url/field.class.php,v retrieving revision 1.15 diff -u -r1.15 field.class.php --- mod/data/field/url/field.class.php 26 Feb 2007 06:56:12 -0000 1.15 +++ mod/data/field/url/field.class.php 18 May 2008 14:52:02 -0000 @@ -131,6 +131,9 @@ return false; } + function export_text_value($record) { + return $record->content . " " . $record->content1; + } } ?> Index: mod/data/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/data/lib.php,v retrieving revision 1.137.2.31 diff -u -r1.137.2.31 lib.php --- mod/data/lib.php 24 Apr 2008 17:10:03 -0000 1.137.2.31 +++ mod/data/lib.php 18 May 2008 14:32:14 -0000 @@ -313,6 +313,18 @@ return $str; } +/// Overide and return false if field does not support text export + function text_export_supported() { + return true; + } + +/// Return the field text value from an content record +/// which is passed in + function export_text_value($record){ + if ($this->text_export_supported()) { + return $record->content; + } + } } //end of major class data_field_base Index: mod/data/tabs.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/data/tabs.php,v retrieving revision 1.28.2.1 diff -u -r1.28.2.1 tabs.php --- mod/data/tabs.php 16 Nov 2007 05:55:00 -0000 1.28.2.1 +++ mod/data/tabs.php 20 Apr 2008 13:56:46 -0000 @@ -69,6 +69,8 @@ get_string('fields','data')); $row[] = new tabobject('presets', $CFG->wwwroot.'/mod/data/preset.php?d='.$data->id, get_string('presets', 'data')); + $row[] = new tabobject('export', $CFG->wwwroot.'/mod/data/export.php?d='.$data->id, + get_string('export', 'data')); } } =================================================================== --- /dev/null +++ mod/data/export.php 2008-05-18 10:26:42.000000000 -0500 @@ -0,0 +1,204 @@ +id, $data->course)) { + print_error('invalidcoursemodule'); +} + +if(! $course = get_record('course', 'id', $cm->course)) { + print_error('invalidcourseid', '', '', $cm->course); +} + +// fill in missing properties needed for updating of instance +$data->course = $cm->course; +$data->cmidnumber = $cm->idnumber; +$data->instance = $cm->instance; + +if (! $context = get_context_instance(CONTEXT_MODULE, $cm->id)) { + print_error('invalidcontext', ''); +} + +require_login($course->id, false, $cm); +require_capability('mod/data:managetemplates', $context); + + +// get fields for this database +$fieldrecords = get_records('data_fields','dataid', $data->id, 'id'); + +if(empty($fieldrecords)) { + $context = get_context_instance(CONTEXT_MODULE, $cm->id); + + if (has_capability('mod/data:managetemplates', $context)) { + redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); + } else { + print_error('nofieldindatabase', 'data'); + } +} + +// populate objets for this databases fields +$fields = array(); +foreach ($fieldrecords as $fieldrecord) { + $fields[]= data_get_field($fieldrecord, $data); +} + + +$mform = new mod_data_export_form('export.php?d='.$data->id, $fields); + +if($mform->is_cancelled()) { + redirect('view.php?d='.$data->id); +} elseif (!$formdata = (array) $mform->get_data()) { + // build header to match the rest of the ui + $nav = build_navigation('', $cm); + + print_header_simple($data->name, '', $nav, + '', '', true, update_module_button($cm->id, $course->id, get_string('modulename', 'data')), + navmenu($course, $cm), '', ''); + + print_heading(format_string($data->name)); + + // these are for the tab display + $currentgroup = groups_get_activity_group($cm); + $groupmode = groups_get_activity_groupmode($cm); + $currenttab = 'export'; + include('tabs.php'); + + $mform->display(); + print_footer(); + die; +} + + +$exportdata = array(); + +// populate the header in first row of export +foreach($fields as $key => $field){ + if(empty($formdata['field_'.$field->field->id])) { + // ignore values we aren't exporting + unset($fields[$key]); + } else{ + $exportdata[0][] = $field->field->name; + } +} + +$datarecords = get_records('data_records', 'dataid', $data->id); + +$line = 1; +foreach($datarecords as $record) { + // get content indexed by fieldid + if( $content = get_records('data_content', 'recordid', $record->id, 'fieldid', 'fieldid, content, content1, content2, content3, content4') ){ + + foreach($fields as $field) { + $contents = ''; + if(isset($content[$field->field->id])) { + $contents = $field->export_text_value($content[$field->field->id]); + } + $exportdata[$line][] = $contents; + } + } + $line++; +} + +switch ($formdata['exporttype']) { + case 'csv': + data_export_csv($exportdata); + break; + case 'xls': + data_export_xls($exportdata); + break; + case 'ods': + data_export_ods($exportdata); + break; +} + + +function data_export_csv($export) { + $filename = clean_filename('database_export.csv'); + + header("Content-Type: application/download\n"); + header("Content-Disposition: attachment; filename=$filename"); + header("Expires: 0"); + header("Cache-Control: must-revalidate,post-check=0,pre-check=0"); + header("Pragma: public"); + +# $delimiter = get_string('listsep'); + $delimiter = "|"; + $encdelim = '&#' . ord($delimiter) . ';'; + + foreach($export as $row) { + foreach($row as $key => $column) { + $row[$key] = str_replace($delimiter, $encdelim, $column); + } + echo implode($delimiter, $row)."\n"; + } + + die; +} + + +function data_export_xls($export) { + global $CFG; + require_once("$CFG->libdir/excellib.class.php"); + + $filename = clean_filename('database_export.xls'); + + $workbook = new MoodleExcelWorkbook('-'); + $workbook->send($filename); + + $worksheet = array(); + $worksheet[0] =& $workbook->add_worksheet(''); + $rowno = 0; + + foreach ($export as $row) { + $colno = 0; + foreach($row as $col){ + $worksheet[0]->write($rowno, $colno, $col); + $colno++; + } + $rowno++; + } + + $workbook->close(); + die; +} + +function data_export_ods($export) { + global $CFG; + require_once("$CFG->libdir/odslib.class.php"); + + $filename = clean_filename('database_export.ods'); + + $workbook = new MoodleODSWorkbook('-'); + $workbook->send($filename); + + $worksheet = array(); + $worksheet[0] =& $workbook->add_worksheet(''); + $rowno = 0; + + foreach ($export as $row) { + $colno = 0; + foreach($row as $col){ + $worksheet[0]->write($rowno, $colno, $col); + $colno++; + } + $rowno++; + } + + $workbook->close(); + die; +} =================================================================== --- /dev/null +++ mod/data/export_form.php 2008-05-18 10:54:34.000000000 -0500 @@ -0,0 +1,56 @@ +libdir.'/formslib.php'); + +class mod_data_export_form extends moodleform { + + var $_datafields = array(); + + /** + * Create form for exporting database + * + * @param string $url the url to post to + * @param array field objects in this database + */ + function mod_data_export_form($url, $datafields){ + $this->_datafields = $datafields; + parent::moodleform($url); + } + + function definition(){ + $mform =& $this->_form; + + $mform->addElement('header', 'notice', get_string('chooseexportformat', 'data')); + + $typesarray = array(); + $typesarray[] = &MoodleQuickForm::createElement('radio', 'exporttype', '', get_string('csvformat', 'data'), 'csv'); + $typesarray[] = &MoodleQuickForm::createElement('radio', 'exporttype', '', get_string('excelformat', 'data'), 'xls'); + $typesarray[] = &MoodleQuickForm::createElement('radio', 'exporttype', '', get_string('odsformat', 'data'), 'ods'); + + $mform->addGroup($typesarray, 'exportar', '', array(' '), false); + $mform->setDefault('exporttype', 'csv'); + $mform->addRule('exportar', null, 'required'); + + $mform->addElement('header', 'notice', get_string('chooseexportfields', 'data')); + + foreach($this->_datafields as $field) { + if($field->text_export_supported()) { + $mform->addElement('advcheckbox', 'field_'.$field->field->id, $field->field->name, null, array('group'=>1)); + $mform->setDefault('field_'.$field->field->id, 1); + } else { + $a = new object; + $a->fieldtype = $field->name(); + $mform->addElement('static', 'unsupported'.$field->field->id, $field->field->name, get_string('unsupportedexport', 'data', $a)); + } + } + $this->add_checkbox_controller(1, null, null, 1); + $this->add_action_buttons(true, get_string('exportdatabase', 'data')); + } + +} + +?>