Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/data/preset.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 
00031 require_once('../../config.php');
00032 require_once($CFG->dirroot.'/mod/data/lib.php');
00033 require_once($CFG->dirroot.'/mod/data/preset_form.php');
00034 require_once($CFG->libdir.'/xmlize.php');
00035 
00036 $id     = optional_param('id', 0, PARAM_INT);           // course module id
00037 if ($id) {
00038     $cm = get_coursemodule_from_id('data', $id, null, null, MUST_EXIST);
00039     $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
00040     $data = $DB->get_record('data', array('id'=>$cm->instance), '*', MUST_EXIST);
00041 } else {
00042     $d = required_param('d', PARAM_INT);     // database activity id
00043     $data = $DB->get_record('data', array('id'=>$d), '*', MUST_EXIST);
00044     $course = $DB->get_record('course', array('id'=>$data->course), '*', MUST_EXIST);
00045     $cm = get_coursemodule_from_instance('data', $data->id, $course->id, null, MUST_EXIST);
00046 }
00047 $context = get_context_instance(CONTEXT_MODULE, $cm->id, MUST_EXIST);
00048 require_login($course->id, false, $cm);
00049 require_capability('mod/data:managetemplates', $context);
00050 $PAGE->set_url(new moodle_url('/mod/data/preset.php', array('d'=>$data->id)));
00051 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
00052 $PAGE->set_heading($course->fullname);
00053 
00054 // fill in missing properties needed for updating of instance
00055 $data->course     = $cm->course;
00056 $data->cmidnumber = $cm->idnumber;
00057 $data->instance   = $cm->instance;
00058 
00059 $presets = data_get_available_presets($context);
00060 $canmanage = has_capability('mod/data:manageuserpresets', $context);
00061 $strdelete = get_string('deleted', 'data');
00062 foreach ($presets as &$preset) {
00063     if (!empty($preset->userid)) {
00064         $presetuser = $DB->get_record('user', array('id'=>$preset->userid), 'id,firstname,lastname', MUST_EXIST);
00065         $preset->description = $preset->name.' ('.fullname($presetuser, true).')';
00066     } else {
00067         $preset->userid = 0;
00068         $preset->description = $preset->name;
00069     }
00070     if ($preset->userid > 0 and ($preset->userid == $USER->id || $canmanage)) {
00071         $delurl = new moodle_url('/mod/data/preset.php', array('d'=> $data->id, 'action'=>'confirmdelete', 'fullname'=>$preset->userid.'/'.$preset->shortname, 'sesskey'=>sesskey()));
00072         $delicon = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/delete'), 'class'=>'iconsmall', 'alt'=>$strdelete.' '.$preset->description));
00073         $preset->description .= html_writer::link($delurl, $delicon);
00074     }
00075 }
00076 
00077 $form_importexisting = new data_existing_preset_form(null, array('presets'=>$presets));
00078 $form_importexisting->set_data(array('d' => $data->id));
00079 
00080 $form_importzip = new data_import_preset_zip_form();
00081 $form_importzip->set_data(array('d' => $data->id));
00082 
00083 $form_export = new data_export_form();
00084 $form_export->set_data(array('d' => $data->id));
00085 
00086 $form_save = new data_save_preset_form();
00087 $form_save->set_data(array('d' => $data->id, 'name'=>$data->name));
00088 
00089 /* Output */
00090 if (!$form_export->is_submitted()) {
00091     echo $OUTPUT->header();
00092     echo $OUTPUT->heading(format_string($data->name));
00093 
00094     // Needed for tabs.php
00095     $currenttab = 'presets';
00096     $currentgroup = groups_get_activity_group($cm);
00097     $groupmode = groups_get_activity_groupmode($cm);
00098     include('tabs.php');
00099 }
00100 
00101 if (optional_param('sesskey', false, PARAM_BOOL) && confirm_sesskey()) {
00102 
00103     $renderer = $PAGE->get_renderer('mod_data');
00104 
00105     if ($formdata = $form_importexisting->get_data()) {
00106         $importer = new data_preset_existing_importer($course, $cm, $data, $formdata->fullname);
00107         echo $renderer->import_setting_mappings($data, $importer);
00108         echo $OUTPUT->footer();
00109         exit(0);
00110     } else if ($formdata = $form_importzip->get_data()) {
00111         $file = new stdClass;
00112         $file->name = $form_importzip->get_new_filename('importfile');
00113         $file->path = $form_importzip->save_temp_file('importfile');
00114         $importer = new data_preset_upload_importer($course, $cm, $data, $file->path);
00115         echo $renderer->import_setting_mappings($data, $importer);
00116         echo $OUTPUT->footer();
00117         exit(0);
00118     } else if ($formdata = $form_export->get_data()) {
00119 
00120         if (headers_sent()) {
00121             print_error('headersent');
00122         }
00123 
00124         $exportfile = data_presets_export($course, $cm, $data);
00125         $exportfilename = basename($exportfile);
00126         header("Content-Type: application/download\n");
00127         header("Content-Disposition: attachment; filename=\"$exportfilename\"");
00128         header('Expires: 0');
00129         header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
00130         header('Pragma: public');
00131         $exportfilehandler = fopen($exportfile, 'rb');
00132         print fread($exportfilehandler, filesize($exportfile));
00133         fclose($exportfilehandler);
00134         unlink($exportfile);
00135         exit(0);
00136 
00137     } else if ($formdata = $form_save->get_data()) {
00138 
00139         if (!empty($formdata->overwrite)) {
00140             data_delete_site_preset($formdata->name);
00141         }
00142 
00143         // If the preset exists now then we need to throw an error.
00144         $sitepresets = data_get_available_site_presets($context);
00145         foreach ($sitepresets as $key=>$preset) {
00146             if ($formdata->name == $preset->name) {
00147                 print_error('errorpresetexists', 'preset');
00148             }
00149         }
00150 
00151         // Save the preset now
00152         data_presets_save($course, $cm, $data, $formdata->name);
00153 
00154         echo $OUTPUT->notification(get_string('savesuccess', 'data'), 'notifysuccess');
00155         echo $OUTPUT->continue_button($PAGE->url);
00156         echo $OUTPUT->footer();
00157         exit(0);
00158     } else {
00159         $action = optional_param('action', null, PARAM_ALPHA);
00160         $fullname = optional_param('fullname', '', PARAM_PATH); // directory the preset is in
00161         //
00162         // find out preset owner userid and shortname
00163         $parts = explode('/', $fullname, 2);
00164         $userid = empty($parts[0]) ? 0 : (int)$parts[0];
00165         $shortname = empty($parts[1]) ? '' : $parts[1];
00166 
00167         if ($userid && ($userid != $USER->id) && !has_capability('mod/data:viewalluserpresets', $context)) {
00168             print_error('cannotaccesspresentsother', 'data');
00169         }
00170 
00171         if ($action == 'confirmdelete') {
00172             $path = data_preset_path($course, $userid, $shortname);
00173             $strwarning = get_string('deletewarning', 'data').'<br />'.$shortname;
00174             $optionsyes = array('fullname' => $userid.'/'.$shortname,
00175                              'action' => 'delete',
00176                              'd' => $data->id);
00177             $optionsno = array('d' => $data->id);
00178             echo $OUTPUT->confirm($strwarning, new moodle_url('preset.php', $optionsyes), new moodle_url('preset.php', $optionsno));
00179             echo $OUTPUT->footer();
00180             exit(0);
00181         } else if ($action == 'delete') {
00182             if (!$userid || ($userid != $USER->id && !$canmanage)) {
00183                print_error('invalidrequest');
00184             }
00185 
00186             $presetpath = data_preset_path($course, $userid, $shortname);
00187             fulldelete($presetpath);
00188 
00189             $strdeleted = get_string('deleted', 'data');
00190             echo $OUTPUT->notification("$shortname $strdeleted", 'notifysuccess');
00191         } else if ($action == 'finishimport') {
00192             $overwritesettings = optional_param('overwritesettings', false, PARAM_BOOL);
00193             if (!$fullname) {
00194                 $presetdir = $CFG->tempdir.'/forms/'.required_param('directory', PARAM_ALPHANUMEXT);
00195                 if (!file_exists($presetdir) || !is_dir($presetdir)) {
00196                     print_error('cannotimport');
00197                 }
00198                 $importer = new data_preset_upload_importer($course, $cm, $data, $presetdir);
00199             } else {
00200                 $importer = new data_preset_existing_importer($course, $cm, $data, $fullname);
00201             }
00202             $importer->import($overwritesettings);
00203             $strimportsuccess = get_string('importsuccess', 'data');
00204             $straddentries = get_string('addentries', 'data');
00205             $strtodatabase = get_string('todatabase', 'data');
00206             if (!$DB->get_records('data_records', array('dataid'=>$data->id))) {
00207                 echo $OUTPUT->notification("$strimportsuccess <a href='edit.php?d=$data->id'>$straddentries</a> $strtodatabase", 'notifysuccess');
00208             } else {
00209                 echo $OUTPUT->notification("$strimportsuccess", 'notifysuccess');
00210             }
00211         }
00212         echo $OUTPUT->continue_button($PAGE->url);
00213         echo $OUTPUT->footer();
00214         exit(0);
00215     }
00216 }
00217 
00218 // Export forms
00219 echo $OUTPUT->heading(get_string('export', 'data'));
00220 $form_export->display();
00221 $form_save->display();
00222 
00223 // Import forms
00224 echo $OUTPUT->heading(get_string('import'));
00225 $form_importzip->display();
00226 $form_importexisting->display();
00227 
00228 echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations