Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/data/export.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00026 require_once('../../config.php');
00027 require_once('lib.php');
00028 require_once('export_form.php');
00029 
00030 // database ID
00031 $d = required_param('d', PARAM_INT);
00032 
00033 $PAGE->set_url('/mod/data/export.php', array('d'=>$d));
00034 
00035 if (! $data = $DB->get_record('data', array('id'=>$d))) {
00036     print_error('wrongdataid', 'data');
00037 }
00038 
00039 if (! $cm = get_coursemodule_from_instance('data', $data->id, $data->course)) {
00040     print_error('invalidcoursemodule');
00041 }
00042 
00043 if(! $course = $DB->get_record('course', array('id'=>$cm->course))) {
00044     print_error('invalidcourseid', '', '', $cm->course);
00045 }
00046 
00047 // fill in missing properties needed for updating of instance
00048 $data->course     = $cm->course;
00049 $data->cmidnumber = $cm->idnumber;
00050 $data->instance   = $cm->instance;
00051 
00052 if (! $context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
00053     print_error('invalidcontext', '');
00054 }
00055 
00056 require_login($course->id, false, $cm);
00057 require_capability(DATA_CAP_EXPORT, $context);
00058 
00059 // get fields for this database
00060 $fieldrecords = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
00061 
00062 if(empty($fieldrecords)) {
00063     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00064     if (has_capability('mod/data:managetemplates', $context)) {
00065         redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id);
00066     } else {
00067         print_error('nofieldindatabase', 'data');
00068     }
00069 }
00070 
00071 // populate objets for this databases fields
00072 $fields = array();
00073 foreach ($fieldrecords as $fieldrecord) {
00074     $fields[]= data_get_field($fieldrecord, $data);
00075 }
00076 
00077 
00078 $mform = new mod_data_export_form('export.php?d='.$data->id, $fields, $cm);
00079 
00080 if($mform->is_cancelled()) {
00081     redirect('view.php?d='.$data->id);
00082 } elseif (!$formdata = (array) $mform->get_data()) {
00083     // build header to match the rest of the UI
00084     $PAGE->set_title($data->name);
00085     $PAGE->set_heading($course->fullname);
00086     echo $OUTPUT->header();
00087     $url = new moodle_url('/mod/data/export.php', array('d' => $d));
00088     groups_print_activity_menu($cm, $url);
00089     echo $OUTPUT->heading(format_string($data->name));
00090 
00091     // these are for the tab display
00092     $currentgroup = groups_get_activity_group($cm);
00093     $groupmode = groups_get_activity_groupmode($cm);
00094     $currenttab = 'export';
00095     include('tabs.php');
00096     $mform->display();
00097     echo $OUTPUT->footer();
00098     die;
00099 }
00100 
00101 $selectedfields = array();
00102 foreach ($formdata as $key => $value) {
00103     //field form elements are field_1 field_2 etc. 0 if not selected. 1 if selected.
00104     if (strpos($key, 'field_')===0 && !empty($value)) {
00105         $selectedfields[] = substr($key, 6);
00106     }
00107 }
00108 
00109 $currentgroup = groups_get_activity_group($cm);
00110 
00111 $exportdata = data_get_exportdata($data->id, $fields, $selectedfields, $currentgroup);
00112 $count = count($exportdata);
00113 switch ($formdata['exporttype']) {
00114     case 'csv':
00115         data_export_csv($exportdata, $formdata['delimiter_name'], $data->name, $count);
00116         break;
00117     case 'xls':
00118         data_export_xls($exportdata, $data->name, $count);
00119         break;
00120     case 'ods':
00121         data_export_ods($exportdata, $data->name, $count);
00122         break;
00123 }
00124 
00125 die();
 All Data Structures Namespaces Files Functions Variables Enumerations