Index: lang/en_utf8/data.php =================================================================== RCS file: /cvsroot/moodle/moodle/lang/en_utf8/data.php,v retrieving revision 1.53.4.10 diff -u -r1.53.4.10 data.php --- lang/en_utf8/data.php 17 Apr 2008 07:38:26 -0000 1.53.4.10 +++ lang/en_utf8/data.php 18 Apr 2008 21:38:16 -0000 @@ -1,5 +1,5 @@ name - $a->date'; $string['cancel'] = 'Cancel'; $string['checkbox'] = 'Checkbox'; +$string['chooseexportformat'] = 'Choose the format you wish to export in:'; +$string['chooseexportfields'] = 'Choose the fields you wish to export:'; $string['chooseorupload'] = 'Choose file'; $string['columns'] = 'columns'; $string['commentdeleted'] = 'Comment deleted'; @@ -41,7 +43,10 @@ $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['coursemodulenotfound'] = 'Course Module not found'; +$string['coursenotfound'] = 'Course not found'; $string['data:approve'] = 'Approve unapproved entries'; $string['data:comment'] = 'Write comments'; $string['data:managecomments'] = 'Manage comments'; @@ -82,10 +87,12 @@ $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['exportedtozip'] = 'Exported to temporary zip...'; $string['failedpresetdelete'] = 'Error deleting a preset!'; +$string['exportdatabase'] = 'Export Database'; $string['fieldadded'] = 'Field added'; $string['fieldallowautolink'] = 'Allow autolink'; $string['fielddeleted'] = 'Field deleted'; @@ -173,6 +180,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'; @@ -231,6 +239,7 @@ $string['timemodified'] = 'Time modified'; $string['todatabase'] = 'to this database.'; $string['type'] = 'Field type'; +$string['unsupportedexport'] = '$a->fieldname [$a->fieldtype] can not be exported.'; $string['undefinedprocessactionmethod'] = 'No action method defined in Data_Preset to handle action \"$a\".'; $string['updatefield'] = 'Update an existing field'; $string['uploadfile'] = 'Upload file'; Index: mod/data/field/checkbox/field.class.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/data/field/checkbox/field.class.php,v retrieving revision 1.14 diff -u -r1.14 field.class.php --- mod/data/field/checkbox/field.class.php 26 Feb 2007 06:56:07 -0000 1.14 +++ mod/data/field/checkbox/field.class.php 18 Apr 2008 22:12:24 -0000 @@ -129,5 +129,9 @@ $str = clean_param($str, PARAM_NOTAGS); return $str; } + + function text_export_supported(){ + return true; + } } ?> Index: mod/data/field/date/field.class.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/data/field/date/field.class.php,v retrieving revision 1.6.2.1 diff -u -r1.6.2.1 field.class.php --- mod/data/field/date/field.class.php 16 Apr 2008 15:48:04 -0000 1.6.2.1 +++ mod/data/field/date/field.class.php 18 Apr 2008 22:12:24 -0000 @@ -127,6 +127,13 @@ } } + function text_export_supported(){ + return true; + } + + function export_text_value($record){ + return userdate($record->content, get_string('strftimedate'), 0); + } } 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 Apr 2008 22:12:24 -0000 @@ -203,6 +203,30 @@ return $fieldname; } } + + function text_export_supported(){ + return true; + } + + function export_text_value($record){ + + $lat = $record->content; + $long = $record->content1; + + $str = ''; + if($lat < 0) { + $str.= "" . sprintf('%01.4f', 0 - $lat) . '°S '; + } else { + $str.= "" . sprintf('%01.4f', $lat) . "°N "; + } + if($long < 0) { + $str.= "" . sprintf('%01.4f', 0 - $long) . '°W'; + } else { + $str.= "" . sprintf('%01.4f', $long) . "°E"; + } + + return $str; + } } ?> Index: mod/data/field/menu/field.class.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/data/field/menu/field.class.php,v retrieving revision 1.12 diff -u -r1.12 field.class.php --- mod/data/field/menu/field.class.php 26 Feb 2007 06:56:08 -0000 1.12 +++ mod/data/field/menu/field.class.php 18 Apr 2008 22:12:24 -0000 @@ -78,6 +78,10 @@ return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = '$value') "; } + function text_export_supported(){ + return true; + } + } ?> Index: mod/data/field/multimenu/field.class.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/data/field/multimenu/field.class.php,v retrieving revision 1.11.2.2 diff -u -r1.11.2.2 field.class.php --- mod/data/field/multimenu/field.class.php 16 Apr 2008 14:50:58 -0000 1.11.2.2 +++ mod/data/field/multimenu/field.class.php 18 Apr 2008 22:12:24 -0000 @@ -181,5 +181,9 @@ } return false; } + + function text_export_supported(){ + return true; + } } ?> Index: mod/data/field/number/field.class.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/data/field/number/field.class.php,v retrieving revision 1.6 diff -u -r1.6 field.class.php --- mod/data/field/number/field.class.php 26 Feb 2007 06:56:08 -0000 1.6 +++ mod/data/field/number/field.class.php 18 Apr 2008 22:12:24 -0000 @@ -70,6 +70,10 @@ } } + function text_export_supported(){ + return true; + } + } ?> Index: mod/data/field/radiobutton/field.class.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/data/field/radiobutton/field.class.php,v retrieving revision 1.9 diff -u -r1.9 field.class.php --- mod/data/field/radiobutton/field.class.php 26 Feb 2007 06:56:09 -0000 1.9 +++ mod/data/field/radiobutton/field.class.php 18 Apr 2008 22:12:24 -0000 @@ -88,5 +88,9 @@ return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = '$value') "; } + function text_export_supported(){ + return true; + } + } ?> Index: mod/data/field/text/field.class.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/data/field/text/field.class.php,v retrieving revision 1.8 diff -u -r1.8 field.class.php --- mod/data/field/text/field.class.php 26 Feb 2007 06:56:09 -0000 1.8 +++ mod/data/field/text/field.class.php 18 Apr 2008 22:12:24 -0000 @@ -41,7 +41,11 @@ function generate_sql($tablealias, $value) { return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content LIKE '%{$value}%') "; } - + + function text_export_supported(){ + return true; + } + } ?> Index: mod/data/field/textarea/field.class.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/data/field/textarea/field.class.php,v retrieving revision 1.19 diff -u -r1.19 field.class.php --- mod/data/field/textarea/field.class.php 26 Feb 2007 06:56:09 -0000 1.19 +++ mod/data/field/textarea/field.class.php 18 Apr 2008 22:12:24 -0000 @@ -114,5 +114,9 @@ return insert_record('data_content', $content); } } + + function text_export_supported(){ + return true; + } } ?> 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 Apr 2008 22:12:24 -0000 @@ -131,6 +131,10 @@ return false; } + function text_export_supported(){ + return true; + } + } ?> Index: mod/data/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/data/lib.php,v retrieving revision 1.137.2.22 diff -u -r1.137.2.22 lib.php --- mod/data/lib.php 18 Apr 2008 07:43:18 -0000 1.137.2.22 +++ mod/data/lib.php 18 Apr 2008 22:12:24 -0000 @@ -310,6 +310,19 @@ return $str; } +/// Overide and return true if field supports text export + function text_export_supported(){ + return false; + } + +/// 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 18 Apr 2008 22:12:24 -0000 @@ -72,6 +72,10 @@ } } + $row[] = new tabobject('export', $CFG->wwwroot.'/mod/data/export.php?d='.$data->id, + get_string('export', 'data')); + + $tabs[] = $row; if ($currenttab == 'templates' and isset($mode)) { =================================================================== --- /dev/null +++ mod/data/export.php @@ -0,0 +1,200 @@ +id, $data->course)) { + print_error('coursemodulenotfound', 'data'); +} + +if(! $course = get_record('course', 'id', $cm->course)){ + print_error('coursenotfound', 'data'); +} + + +require_login($course->id, false, $cm); + + +// 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'); + $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 @@ -0,0 +1,58 @@ +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(); + $a->fieldname = $field->field->name; + + $mform->addElement('static', 'unsupported'.$field->field->id, '', get_string('unsupportedexport', 'data', $a)); + } + } + $this->add_checkbox_controller(1, null, null, 1); + $this->add_action_buttons(true, get_string('exportdatabase', 'data')); + } + +} + +?>