MDL-11515:
[moodle-linuxchix.git] / mod / data / preset.php
blob334be156acd349d59a4f877fa176c530b047e690
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 // fill in missing properties needed for updating of instance
52 $data->course = $cm->course;
53 $data->cmidnumber = $cm->idnumber;
54 $data->instance = $cm->instance;
56 if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
57 error('Could not find context');
60 require_login($course->id, false, $cm);
62 require_capability('mod/data:managetemplates', $context);
64 if ($userid && ($userid != $USER->id) && !has_capability('mod/data:viewalluserpresets', $context)) {
65 error('You are not allowed to access presets from other users');
68 /* Need sesskey security check here for import instruction */
69 $sesskey = sesskey();
71 /********************************************************************/
72 /* Output */
73 data_print_header($course, $cm, $data, 'presets');
75 switch ($action) {
77 /***************** Deleting *****************/
78 case 'confirmdelete' :
79 if (!confirm_sesskey()) { // GET request ok here
80 error("Sesskey Invalid");
83 if ($userid > 0 and ($userid == $USER->id || has_capability('mod/data:manageuserpresets', $context))) {
84 //ok can delete
85 } else {
86 error("Invalid request");
89 $path = data_preset_path($course, $userid, $shortname);
91 $strwarning = get_string('deletewarning', 'data').'<br />'.
92 data_preset_name($shortname, $path);
94 $options = new object();
95 $options->fullname = $userid.'/'.$shortname;
96 $options->action = 'delete';
97 $options->d = $data->id;
98 $options->sesskey = sesskey();
100 $optionsno = new object();
101 $optionsno->d = $data->id;
102 notice_yesno($strwarning, 'preset.php', 'preset.php', $options, $optionsno, 'post', 'get');
103 print_footer($course);
104 exit;
105 break;
107 case 'delete' :
108 if (!data_submitted() and !confirm_sesskey()) {
109 error("Invalid request");
112 if ($userid > 0 and ($userid == $USER->id || has_capability('mod/data:manageuserpresets', $context))) {
113 //ok can delete
114 } else {
115 error("Invalid request");
118 $presetpath = data_preset_path($course, $userid, $shortname);
120 if (!clean_preset($presetpath)) {
121 error("Error deleting a preset!");
123 @rmdir($presetpath);
125 $strdeleted = get_string('deleted', 'data');
126 notify("$shortname $strdeleted", 'notifysuccess');
128 break;
131 /***************** Importing *****************/
132 case 'importpreset' :
133 if (!data_submitted() or !confirm_sesskey()) {
134 error("Invalid request");
137 $pimporter = new PresetImporter($course, $cm, $data, $userid, $shortname);
138 $pimporter->import_options();
140 print_footer($course);
141 exit;
142 break;
144 /* Imports a zip file. */
145 case 'importzip' :
146 if (!data_submitted() or !confirm_sesskey()) {
147 error("Invalid request");
150 if (!make_upload_directory('temp/data/'.$USER->id)) {
151 error("Can't Create Directory");
154 $presetfile = $CFG->dataroot.'/temp/data/'.$USER->id;
155 clean_preset($presetfile);
157 if (!unzip_file($CFG->dataroot."/$course->id/$file", $presetfile, false)) {
158 error("Can't unzip file");
161 $pimporter = new PresetImporter($course, $cm, $data, -$USER->id, $shortname);
162 $pimporter->import_options();
164 print_footer($course);
165 exit;
166 break;
168 case 'finishimport':
169 if (!data_submitted() or !confirm_sesskey()) {
170 error("Invalid request");
173 $pimporter = new PresetImporter($course, $cm, $data, $userid, $shortname);
174 $pimporter->import();
176 $strimportsuccess = get_string('importsuccess', 'data');
177 $straddentries = get_string('addentries', 'data');
178 $strtodatabase = get_string('todatabase', 'data');
179 if (!get_records('data_records', 'dataid', $data->id)) {
180 notify("$strimportsuccess <a href='edit.php?d=$data->id'>$straddentries</a> $strtodatabase", 'notifysuccess');
181 } else {
182 notify("$strimportsuccess", 'notifysuccess');
184 break;
186 /* Exports as a zip file ready for download. */
187 case 'export':
188 if (!data_submitted() or !confirm_sesskey()) {
189 error("Invalid request");
192 echo '<div style="text-align:center">';
193 $file = data_presets_export($course, $cm, $data);
194 echo get_string('exportedtozip', 'data')."<br />";
195 $perminantfile = $CFG->dataroot."/$course->id/moddata/data/$data->id/preset.zip";
196 @unlink($perminantfile);
197 /* is this created elsewhere? sometimes its not present... */
198 make_upload_directory("$course->id/moddata/data/$data->id");
200 /* now just move the zip into this folder to allow a nice download */
201 if (!rename($file, $perminantfile)) error("Can't move zip");
202 echo "<a href='$CFG->wwwroot/file.php/$course->id/moddata/data/$data->id/preset.zip'>".get_string('download', 'data')."</a>";
203 echo '</div>';
204 break;
208 /***************** Exporting *****************/
209 case 'save1':
210 if (!data_submitted() or !confirm_sesskey()) {
211 error("Invalid request");
214 $strcontinue = get_string('continue');
215 $strwarning = get_string('presetinfo', 'data');
216 $strname = get_string('shortname');
218 echo '<div style="text-align:center">';
219 echo '<p>'.$strwarning.'</p>';
220 echo '<form action="preset.php" method="post">';
221 echo '<fieldset class="invisiblefieldset">';
222 echo '<label for="shorname">'.$strname.'</label> <input type="text" id="shorname" name="name" value="'.$data->name.'" />';
223 echo '<input type="hidden" name="action" value="save2" />';
224 echo '<input type="hidden" name="d" value="'.$data->id.'" />';
225 echo '<input type="hidden" name="sesskey" value="'.$sesskey.'" />';
226 echo '<input type="submit" value="'.$strcontinue.'" /></fieldset></form></div>';
227 print_footer($course);
228 exit;
229 break;
231 case 'save2':
232 if (!data_submitted() or !confirm_sesskey()) {
233 error("Invalid request");
236 $strcontinue = get_string('continue');
237 $stroverwrite = get_string('overwrite', 'data');
238 $strname = get_string('shortname');
240 $name = optional_param('name', $data->name, PARAM_FILE);
242 if (is_directory_a_preset("$CFG->dataroot/data/preset/$USER->id/$name")) {
243 notify("Preset already exists: Pick another name or overwrite");
245 echo '<div style="text-align:center">';
246 echo '<form action="preset.php" method="post">';
247 echo '<fieldset class="invisiblefieldset">';
248 echo '<label for="shorname">'.$strname.'</label> <input type="textbox" name="name" value="'.$name.'" />';
249 echo '<input type="hidden" name="action" value="save2" />';
250 echo '<input type="hidden" name="d" value="'.$data->id.'" />';
251 echo '<input type="hidden" name="sesskey" value="'.$sesskey.'" />';
252 echo '<input type="submit" value="'.$strcontinue.'" /></fieldset></form>';
254 echo '<form action="preset.php" method="post">';
255 echo '<div>';
256 echo '<input type="hidden" name="name" value="'.$name.'" />';
257 echo '<input type="hidden" name="action" value="save3" />';
258 echo '<input type="hidden" name="d" value="'.$data->id.'" />';
259 echo '<input type="hidden" name="sesskey" value="'.$sesskey.'" />';
260 echo '<input type="submit" value="'.$stroverwrite.'" /></div></form>';
261 echo '</div>';
262 print_footer($course);
263 exit;
264 break;
267 case 'save3':
268 if (!data_submitted() or !confirm_sesskey()) {
269 error("Invalid request");
272 $name = optional_param('name', $data->name, PARAM_FILE);
273 $presetdirectory = "/data/preset/$USER->id/$name";
275 make_upload_directory($presetdirectory);
276 clean_preset($CFG->dataroot.$presetdirectory);
278 $file = data_presets_export($course, $cm, $data);
279 if (!unzip_file($file, $CFG->dataroot.$presetdirectory, false)) {
280 error("Can't unzip to the preset directory");
282 notify(get_string('savesuccess', 'data'), 'notifysuccess');
283 break;
286 $presets = data_get_available_presets($context);
288 $strimport = get_string('import');
289 $strfromfile = get_string('fromfile', 'data');
290 $strchooseorupload = get_string('chooseorupload', 'data');
291 $strusestandard = get_string('usestandard', 'data');
292 $strchoose = get_string('choose');
293 $strexport = get_string('export', 'data');
294 $strexportaszip = get_string('exportaszip', 'data');
295 $strsaveaspreset = get_string('saveaspreset', 'data');
296 $strsave = get_string('save', 'data');
297 $strdelete = get_string('delete');
299 echo '<div style="text-align:center">';
300 echo '<table class="presets" cellpadding="5">';
301 echo '<tr><td valign="top" colspan="2" align="center"><h3>'.$strexport.'</h3></td></tr>';
303 echo '<tr><td><label>'.$strexportaszip.'</label>';
304 helpbutton('exportzip', '', 'data');
305 echo '</td><td>';
306 $options = new object();
307 $options->action = 'export';
308 $options->d = $data->id;
309 $options->sesskey = sesskey();
310 print_single_button('preset.php', $options, $strexport, 'post');
311 echo '</td></tr>';
313 echo '<tr><td><label>'.$strsaveaspreset.'</label>';
314 helpbutton('savepreset', '', 'data');
315 echo '</td><td>';
316 $options = new object();
317 $options->action = 'save1';
318 $options->d = $data->id;
319 $options->sesskey = sesskey();
320 print_single_button('preset.php', $options, $strsave, 'post');
321 echo '</td></tr>';
324 echo '<tr><td valign="top" colspan="2" align="center"><h3>'.$strimport.'</h3></td></tr>';
326 echo '<tr><td><label for="fromfile">'.$strfromfile.'</label>';
327 helpbutton('importfromfile', '', 'data');
328 echo '</td><td>';
330 echo '<form id="uploadpreset" method="post" action="preset.php">';
331 echo '<fieldset class="invisiblefieldset">';
332 echo '<input type="hidden" name="d" value="'.$data->id.'" />';
333 echo '<input type="hidden" name="action" value="importzip" />';
334 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
335 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" />';
336 echo '<input type="submit" value="'.$strimport.'" />';
337 echo '</fieldset></form>';
338 echo '</td></tr>';
341 echo '<tr valign="top"><td><label>'.$strusestandard.'</label>';
342 helpbutton('usepreset', '', 'data');
343 echo '</td><td>';
345 echo '<form id="presets" method="post" action="preset.php" >';
346 echo '<fieldset class="invisiblefieldset">';
347 echo '<input type="hidden" name="d" value="'.$data->id.'" />';
348 echo '<input type="hidden" name="action" value="importpreset" />';
349 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
351 $i = 0;
352 foreach ($presets as $id => $preset) {
353 $screenshot = '';
354 if (!empty($preset->userid)) {
355 $user = get_record('user', 'id', $preset->userid);
356 $desc = $preset->name.' ('.fullname($user, true).')';
357 } else {
358 $desc = $preset->name;
361 if (!empty($preset->screenshot)) {
362 $screenshot = '<img width="150" class="presetscreenshot" src="'.$preset->screenshot.'" alt="'.get_string('screenshot').' '.$desc.'" />&nbsp;';
365 $fullname = $preset->userid.'/'.$preset->shortname;
367 $dellink = '';
368 if ($preset->userid > 0 and ($preset->userid == $USER->id || has_capability('mod/data:manageuserpresets', $context))) {
369 $dellink = '&nbsp;<a href="preset.php?d='.$data->id.'&amp;action=confirmdelete&amp;fullname='.$fullname.'&amp;sesskey='.sesskey().'">'.
370 '<img src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$strdelete.' '.$desc.'" /></a>';
373 echo '<input type="radio" name="fullname" id="usepreset'.$i.'" value="'.$fullname.'" /><label for="usepreset'.$i++.'">'.$desc.'</label>'.$dellink.'<br />';
375 echo '<br />';
376 echo '<input type="submit" value="'.$strchoose.'" />';
377 echo '</fieldset></form>';
378 echo '</td></tr>';
379 echo '</table>';
380 echo '</div>';
382 print_footer($course);