MDL-10234
[moodle-linuxchix.git] / mod / data / preset.php
blob551aea831956bcfa5d9c85244218d52c49b8dba5
1 <?php // $Id$
2 /* Preset Menu
4 * This is the page that is the menu item in the config database
5 * pages.
6 */
8 require_once('../../config.php');
9 require_once('lib.php');
10 require_once($CFG->libdir.'/uploadlib.php');
11 require_once($CFG->libdir.'/xmlize.php');
14 $id = optional_param('id', 0, PARAM_INT); // course module id
15 $d = optional_param('d', 0, PARAM_INT); // database activity id
16 $action = optional_param('action', 'base', PARAM_ALPHANUM); // current action
17 $fullname = optional_param('fullname', '', PARAM_PATH); // directory the preset is in
18 $file = optional_param('file', '', PARAM_FILE); // uploaded file
20 // find out preset owner userid and shortname
21 $parts = explode('/', $fullname);
22 $userid = empty($parts[0]) ? 0 : (int)$parts[0];
23 $shortname = empty($parts[1]) ? '' : $parts[1];
24 unset($parts);
25 unset($fullname);
27 if ($id) {
28 if (! $cm = get_coursemodule_from_id('data', $id)) {
29 error('Course Module ID was incorrect');
31 if (! $course = get_record('course', 'id', $cm->course)) {
32 error('Course is misconfigured');
34 if (! $data = get_record('data', 'id', $cm->instance)) {
35 error('Module Incorrect');
37 } else if ($d) {
38 if (! $data = get_record('data', 'id', $d)) {
39 error('Database ID Incorrect');
41 if (! $course = get_record('course', 'id', $data->course)) {
42 error('Course is misconfigured');
44 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
45 error('Course Module ID was incorrect');
47 } else {
48 error('Parameter missing');
51 if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
52 error('Could not find context');
55 require_login($course->id, false, $cm);
57 require_capability('mod/data:managetemplates', $context);
59 if ($userid && ($userid != $USER->id) && !has_capability('mod/data:viewalluserpresets', $context)) {
60 error('You are not allowed to access presets from other users');
63 /* Need sesskey security check here for import instruction */
64 $sesskey = sesskey();
66 /********************************************************************/
67 /* Output */
68 data_print_header($course, $cm, $data, 'presets');
70 switch ($action) {
72 /***************** Deleting *****************/
73 case 'confirmdelete' :
74 if (!confirm_sesskey()) { // GET request ok here
75 error("Sesskey Invalid");
78 if ($userid > 0 and ($userid == $USER->id || has_capability('mod/data:manageuserpresets', $context))) {
79 //ok can delete
80 } else {
81 error("Invalid request");
84 $path = data_preset_path($course, $userid, $shortname);
86 $strwarning = get_string('deletewarning', 'data').'<br />'.
87 data_preset_name($shortname, $path);
89 $options = new object();
90 $options->fullname = $userid.'/'.$shortname;
91 $options->action = 'delete';
92 $options->d = $data->id;
93 $options->sesskey = sesskey();
95 $optionsno = new object();
96 $optionsno->d = $data->id;
97 notice_yesno($strwarning, 'preset.php', 'preset.php', $options, $optionsno, 'post', 'get');
98 print_footer($course);
99 exit;
100 break;
102 case 'delete' :
103 if (!data_submitted() and !confirm_sesskey()) {
104 error("Invalid request");
107 if ($userid > 0 and ($userid == $USER->id || has_capability('mod/data:manageuserpresets', $context))) {
108 //ok can delete
109 } else {
110 error("Invalid request");
113 $presetpath = data_preset_path($course, $userid, $shortname);
115 if (!clean_preset($presetpath)) {
116 error("Error deleting a preset!");
118 @rmdir($presetpath);
120 $strdeleted = get_string('deleted', 'data');
121 notify("$shortname $strdeleted", 'notifysuccess');
123 break;
126 /***************** Importing *****************/
127 case 'importpreset' :
128 if (!data_submitted() or !confirm_sesskey()) {
129 error("Invalid request");
132 $pimporter = new PresetImporter($course, $cm, $data, $userid, $shortname);
133 $pimporter->import_options();
135 print_footer($course);
136 exit;
137 break;
139 /* Imports a zip file. */
140 case 'importzip' :
141 if (!data_submitted() or !confirm_sesskey()) {
142 error("Invalid request");
145 if (!make_upload_directory('temp/data/'.$USER->id)) {
146 error("Can't Create Directory");
149 $presetfile = $CFG->dataroot.'/temp/data/'.$USER->id;
150 clean_preset($presetfile);
152 if (!unzip_file($CFG->dataroot."/$course->id/$file", $presetfile, false)) {
153 error("Can't unzip file");
156 $pimporter = new PresetImporter($course, $cm, $data, -$USER->id, $shortname);
157 $pimporter->import_options();
159 print_footer($course);
160 exit;
161 break;
163 case 'finishimport':
164 if (!data_submitted() or !confirm_sesskey()) {
165 error("Invalid request");
168 $pimporter = new PresetImporter($course, $cm, $data, $userid, $shortname);
169 $pimporter->import();
171 $strimportsuccess = get_string('importsuccess', 'data');
172 $straddentries = get_string('addentries', 'data');
173 $strtodatabase = get_string('todatabase', 'data');
174 if (!get_records('data_records', 'dataid', $data->id)) {
175 notify("$strimportsuccess <a href='edit.php?d=$data->id'>$straddentries</a> $strtodatabase", 'notifysuccess');
176 } else {
177 notify("$strimportsuccess", 'notifysuccess');
179 break;
181 /* Exports as a zip file ready for download. */
182 case 'export':
183 if (!data_submitted() or !confirm_sesskey()) {
184 error("Invalid request");
187 echo '<div style="text-align:center">';
188 $file = data_presets_export($course, $cm, $data);
189 echo get_string('exportedtozip', 'data')."<br />";
190 $perminantfile = $CFG->dataroot."/$course->id/moddata/data/$data->id/preset.zip";
191 @unlink($perminantfile);
192 /* is this created elsewhere? sometimes its not present... */
193 make_upload_directory("$course->id/moddata/data/$data->id");
195 /* now just move the zip into this folder to allow a nice download */
196 if (!rename($file, $perminantfile)) error("Can't move zip");
197 echo "<a href='$CFG->wwwroot/file.php/$course->id/moddata/data/$data->id/preset.zip'>".get_string('download', 'data')."</a>";
198 echo '</div>';
199 break;
203 /***************** Exporting *****************/
204 case 'save1':
205 if (!data_submitted() or !confirm_sesskey()) {
206 error("Invalid request");
209 $strcontinue = get_string('continue');
210 $strwarning = get_string('presetinfo', 'data');
211 $strname = get_string('shortname');
213 echo '<div style="text-align:center">';
214 echo '<p>'.$strwarning.'</p>';
215 echo '<form action="preset.php" method="post">';
216 echo '<fieldset class="invisiblefieldset">';
217 echo '<label for="shorname">'.$strname.'</label> <input type="text" id="shorname" name="name" value="'.$data->name.'" />';
218 echo '<input type="hidden" name="action" value="save2" />';
219 echo '<input type="hidden" name="d" value="'.$data->id.'" />';
220 echo '<input type="hidden" name="sesskey" value="'.$sesskey.'" />';
221 echo '<input type="submit" value="'.$strcontinue.'" /></fieldset></form></div>';
222 print_footer($course);
223 exit;
224 break;
226 case 'save2':
227 if (!data_submitted() or !confirm_sesskey()) {
228 error("Invalid request");
231 $strcontinue = get_string('continue');
232 $stroverwrite = get_string('overwrite', 'data');
233 $strname = get_string('shortname');
235 $name = optional_param('name', $data->name, PARAM_FILE);
237 if (is_directory_a_preset("$CFG->dataroot/data/preset/$USER->id/$name")) {
238 notify("Preset already exists: Pick another name or overwrite");
240 echo '<div style="text-align:center">';
241 echo '<form action="preset.php" method="post">';
242 echo '<fieldset class="invisiblefieldset">';
243 echo '<label for="shorname">'.$strname.'</label> <input type="textbox" name="name" value="'.$name.'" />';
244 echo '<input type="hidden" name="action" value="save2" />';
245 echo '<input type="hidden" name="d" value="'.$data->id.'" />';
246 echo '<input type="hidden" name="sesskey" value="'.$sesskey.'" />';
247 echo '<input type="submit" value="'.$strcontinue.'" /></fieldset></form>';
249 echo '<form action="preset.php" method="post">';
250 echo '<div>';
251 echo '<input type="hidden" name="name" value="'.$name.'" />';
252 echo '<input type="hidden" name="action" value="save3" />';
253 echo '<input type="hidden" name="d" value="'.$data->id.'" />';
254 echo '<input type="hidden" name="sesskey" value="'.$sesskey.'" />';
255 echo '<input type="submit" value="'.$stroverwrite.'" /></div></form>';
256 echo '</div>';
257 print_footer($course);
258 exit;
259 break;
262 case 'save3':
263 if (!data_submitted() or !confirm_sesskey()) {
264 error("Invalid request");
267 $name = optional_param('name', $data->name, PARAM_FILE);
268 $presetdirectory = "/data/preset/$USER->id/$name";
270 make_upload_directory($presetdirectory);
271 clean_preset($CFG->dataroot.$presetdirectory);
273 $file = data_presets_export($course, $cm, $data);
274 if (!unzip_file($file, $CFG->dataroot.$presetdirectory, false)) {
275 error("Can't unzip to the preset directory");
277 notify(get_string('savesuccess', 'data'), 'notifysuccess');
278 break;
281 $presets = data_get_available_presets($context);
283 $strimport = get_string('import');
284 $strfromfile = get_string('fromfile', 'data');
285 $strchooseorupload = get_string('chooseorupload', 'data');
286 $strusestandard = get_string('usestandard', 'data');
287 $strchoose = get_string('choose');
288 $strexport = get_string('export', 'data');
289 $strexportaszip = get_string('exportaszip', 'data');
290 $strsaveaspreset = get_string('saveaspreset', 'data');
291 $strsave = get_string('save', 'data');
292 $strdelete = get_string('delete');
294 echo '<div style="text-align:center">';
295 echo '<table class="presets" cellpadding="5">';
296 echo '<tr><td valign="top" colspan="2" align="center"><h3>'.$strexport.'</h3></td></tr>';
298 echo '<tr><td><label>'.$strexportaszip.'</label>';
299 helpbutton('exportzip', '', 'data');
300 echo '</td><td>';
301 $options = new object();
302 $options->action = 'export';
303 $options->d = $data->id;
304 $options->sesskey = sesskey();
305 print_single_button('preset.php', $options, $strexport, 'post');
306 echo '</td></tr>';
308 echo '<tr><td><label>'.$strsaveaspreset.'</label>';
309 helpbutton('savepreset', '', 'data');
310 echo '</td><td>';
311 $options = new object();
312 $options->action = 'save1';
313 $options->d = $data->id;
314 $options->sesskey = sesskey();
315 print_single_button('preset.php', $options, $strsave, 'post');
316 echo '</td></tr>';
319 echo '<tr><td valign="top" colspan="2" align="center"><h3>'.$strimport.'</h3></td></tr>';
321 echo '<tr><td><label for="fromfile">'.$strfromfile.'</label>';
322 helpbutton('importfromfile', '', 'data');
323 echo '</td><td>';
325 echo '<form id="uploadpreset" method="post" action="preset.php">';
326 echo '<fieldset class="invisiblefieldset">';
327 echo '<input type="hidden" name="d" value="'.$data->id.'" />';
328 echo '<input type="hidden" name="action" value="importzip" />';
329 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
330 echo '<input name="file" size="20" value="" id="fromfile" type="text" /><input name="coursefiles" value="'.$strchooseorupload.'" onclick="return openpopup('."'/files/index.php?id=2&amp;choose=uploadpreset.file', 'coursefiles', 'menubar=0,location=0,scrollbars,resizable,width=750,height=500', 0".');" type="button" />';
331 echo '<input type="submit" value="'.$strimport.'" />';
332 echo '</fieldset></form>';
333 echo '</td></tr>';
336 echo '<tr valign="top"><td><label>'.$strusestandard.'</label>';
337 helpbutton('usepreset', '', 'data');
338 echo '</td><td>';
340 echo '<form id="presets" method="post" action="preset.php" >';
341 echo '<fieldset class="invisiblefieldset">';
342 echo '<input type="hidden" name="d" value="'.$data->id.'" />';
343 echo '<input type="hidden" name="action" value="importpreset" />';
344 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
346 $i = 0;
347 foreach ($presets as $id => $preset) {
348 $screenshot = '';
349 if (!empty($preset->userid)) {
350 $user = get_record('user', 'id', $preset->userid);
351 $desc = $preset->name.' ('.fullname($user, true).')';
352 } else {
353 $desc = $preset->name;
356 if (!empty($preset->screenshot)) {
357 $screenshot = '<img width="150" class="presetscreenshot" src="'.$preset->screenshot.'" alt="'.get_string('screenshot').' '.$desc.'" />&nbsp;';
360 $fullname = $preset->userid.'/'.$preset->shortname;
362 $dellink = '';
363 if ($preset->userid > 0 and ($preset->userid == $USER->id || has_capability('mod/data:manageuserpresets', $context))) {
364 $dellink = '&nbsp;<a href="preset.php?d='.$data->id.'&amp;action=confirmdelete&amp;fullname='.$fullname.'&amp;sesskey='.sesskey().'">'.
365 '<img src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$strdelete.' '.$desc.'" /></a>';
368 echo '<input type="radio" name="fullname" id="usepreset'.$i.'" value="'.$fullname.'" /><label for="usepreset'.$i++.'">'.$desc.'</label>'.$dellink.'<br />';
370 echo '<br />';
371 echo '<input type="submit" value="'.$strchoose.'" />';
372 echo '</fieldset></form>';
373 echo '</td></tr>';
374 echo '</table>';
375 echo '</div>';
377 print_footer($course);