adding current groupid to grade_export class - soon to be used in plugins
[moodle-pu.git] / backup / restore_form.html
blobda78ade98391ff4716b33cba020577c3ab5abe9e
1 <?php //$Id$
2 //This page prints the restore form to select everything yo want
3 //to restore. Form is dinamically buid, depending of "info" object
4 //that contains the backup contents and depending of every mod
5 //capabilities.
7 //Get objects from session
8 if (!($info = $SESSION->info)) {
9 error( 'info object missing from session' );
11 if (!($course_header = $SESSION->course_header)) {
12 error( 'course_header object missing from session' );
15 //Check that we have all we need
16 //backup_unique_code
17 $backup_unique_code = required_param( 'backup_unique_code' );
18 //file
19 $file = required_param( 'file' );
21 //Check login
22 require_login();
24 //Check admin
25 if (!empty($id)) {
26 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))) {
27 error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
29 } else {
30 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
31 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
35 //Check site
36 if (!$site = get_site()) {
37 error("Site not found!");
40 //Checks for the required files/functions to restore every mod
41 $count = 0;
42 if ($allmods = get_records("modules") ) {
43 foreach ($allmods as $mod) {
44 $modname = $mod->name;
45 $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
46 $modrestore = $modname."_restore_mods";
47 if (file_exists($modfile)) {
48 include_once($modfile);
49 if (function_exists($modrestore)) {
50 $var = "exists_".$modname;
51 $$var = true;
52 $count++;
55 //Check data
56 //Check module info
57 $var = "restore_".$modname;
58 if (!isset($$var)) {
59 $$var = 1;
61 //Check include user info
62 $var = "restore_user_info_".$modname;
63 if (!isset($$var)) {
64 $$var = 1;
69 //Check other parameters
70 if (!isset($restore_metacourse)) {
71 $restore_metacourse = 1;
74 if (!isset($restore_users)) {
75 $restore_users = 1;
78 if (!isset($restore_logs)) {
79 $restore_logs = 1;
82 if (!isset($restore_user_files)) {
83 $restore_user_files = 1;
86 if (!isset($restore_course_files)) {
87 $restore_course_files = 1;
90 if (!isset($restore_site_files)) {
91 $restore_site_files = 1;
94 if (!isset($restore_messages)) {
95 $restore_messages = 1;
98 if (!isset($restore_restoreto)) {
99 if (!user_can_create_courses()) {
100 $restore_restoreto = 1;
101 } else {
102 $restore_restoreto = 2;
106 if(!isset($form1->startdate)) {
107 $form1->startdate = $course_header->course_startdate; //$course_header->course_startdate;
110 if (empty($form1->shortname)) {
111 $form1->shortname = $course_header->course_shortname; //'_shortname'; //$course_header->course_shortname;
114 if (empty($form1->fullname)) {
115 $form1->fullname = $course_header->course_fullname; // '_fullname'; //$course_header->course_fullname;
118 if ($count == 0) {
119 notice("No restorable modules are installed!");
124 <script type="text/javascript">
125 <!--
126 function selectItemInMenuByName(formId, menuName, selectIndex ) {
127 myForm = document.getElementById(formId)
128 for (i=0,n=myForm.elements.length;i<n;i++) {
129 myLen = menuName.length;
130 myName = myForm.elements[i].name;
131 myType = myForm.elements[i].type;
132 if (myName.substring(0,myLen) == menuName && myType == "select-one") {
133 myForm.elements[i].options[selectIndex].selected = true;
138 function selectItemInRadioByName(formId, radioName, selectIndex ) {
139 myForm = document.getElementById(formId)
140 for (i=0,n=myForm.elements.length;i<n;i++) {
141 myLen = radioName.length;
142 myName = myForm.elements[i].name;
143 myType = myForm.elements[i].type;
144 if (myName.substring(0,myLen) == radioName && myType == "radio") {
145 myRadioGroup = myForm.elements[myName];
146 myRadioGroup[selectIndex].checked = true;
151 function selectItemInCheckboxByName(formId, checkName, checked ) {
152 myForm = document.getElementById(formId)
153 for (i=0,n=myForm.elements.length;i<n;i++) {
154 myLen = checkName.length;
155 myName = myForm.elements[i].name;
156 myType = myForm.elements[i].type;
157 if (myName.substring(0,myLen) == checkName && myType == "checkbox") {
158 myForm.elements[i].checked = checked;
163 </script>
165 <form id="form1" method="post" action="restore.php">
166 <div>
167 <table cellpadding="5" class="boxaligncenter">
168 <?php
170 //First, course destination
171 //Print the full tr
172 echo "<tr>";
173 echo "<td align=\"right\"><b>";
174 echo get_string("restoreto").":</b>";
175 echo "</td><td colspan=\"3\">";
178 // permission should have been checked already
181 * if user has manageactivities in any course, we show
182 * existingcoursedeleting
183 * existingcourseadding
184 * else we show
185 * currentcoursedeleting
186 * currentcourse
187 * if user has course:create in any category, we show
188 * newcourse
191 $mycourses = get_capability_courses('moodle/site:restore');
193 // if the user can manage 2 or more courses,
194 // or if the only course the user can manage is not the current course
195 // we show options for existing courses
196 if (count($mycourses) > 1 || !in_array($id, $mycourses)) {
197 // if user can manage more than 1 course, or if user can restore to a single different course
198 $restore_restoreto_options[0] = get_string("existingcoursedeleting");
199 $restore_restoreto_options[1] = get_string("existingcourseadding");
200 // else if the user can write to current course
201 } else if (has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))){
202 $restore_restoreto_options[0] = get_string("currentcoursedeleting");
203 $restore_restoreto_options[1] = get_string("currentcourseadding");
206 // if user can create any course at all, give the option
207 if (user_can_create_courses()) {
208 $restore_restoreto_options[2] = get_string("newcourse");
211 choose_from_menu($restore_restoreto_options, "restore_restoreto", $restore_restoreto, "");
212 echo "</td></tr>";
213 if (user_can_create_courses()) { //display these fields conditionally
215 // find the list of cates user can edit
216 echo "<tr valign=\"top\" >";
217 echo "<td align=\"right\">";
218 print_string('category');
219 echo " :</td>";
220 echo "<td>";
221 choose_from_menu(get_creatable_categories(), "restore_restorecatto", $course_header->category->id, "");
222 echo "</td>";
223 echo "</tr>";
225 echo "<tr valign=\"top\" >";
226 echo "<td align=\"right\">";
227 print_string("shortname");
228 echo " :</td>";
229 echo "<td><input type=\"text\" name=\"shortname\" maxlength=\"100\" size=\"20\" value=\"$form1->shortname\" alt=\"".get_string("shortname")."\" />" ;
230 helpbutton("courseshortname", get_string("shortname")) ;
231 if (isset($err["shortname"])) formerr($err["shortname"]);
232 echo "</td>";
233 echo "</tr>";
234 echo "<tr valign=\"top\" >";
235 echo "<td align=\"right\">";
236 print_string("fullname");
237 echo " :</td>";
238 echo "<td><input type=\"text\" name=\"fullname\" maxlength=\"254\" size=\"50\" value=\"$form1->fullname\" alt=\" ".get_string("fullname")."\" />" ;
239 helpbutton("coursefullname", get_string("fullname")) ;
240 if (isset($err["fullname"])) formerr($err["fullname"]);
241 echo"</td></tr>";
242 echo "<tr valign=\"top\"> ";
243 echo "<td align=\"right\"> ";
244 print_string("startdate");
245 echo ":</td><td>";
246 print_date_selector("startday", "startmonth", "startyear", $form1->startdate);
247 helpbutton("coursestartdate", get_string("startdate"));
248 echo "</td></tr>";
250 //Line
251 echo "<tr><td colspan=\"4\"><hr /></td></tr>";
252 //Now, check modules and info and show posibilities
253 if ($allmods = get_records("modules") ) {
254 //Print option to select/deselect everything with 1 click.
255 echo "<tr>";
256 echo "<td align=\"right\">";
257 echo '<b>'.get_string("include").":</b>";
258 echo "</td><td>";
259 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', true);\">".
260 get_string("all")."</a>/";
261 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', false);\">".
262 get_string("none")."</a>";
263 echo "</td>";
264 echo "<td align=\"right\">";
265 echo '<b>&nbsp;</b>';
266 echo "</td><td>";
267 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', true);\">".
268 get_string("all")."</a>/";
269 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', false);\">".
270 get_string("none")."</a>";
271 echo "</td>";
272 echo "</tr>";
273 echo "<tr><td colspan=\"4\"><hr /></td></tr>";
274 $currentrow = 0;
275 $nonrestmod = '';
276 foreach ($allmods as $mod) {
277 $modname = $mod->name;
278 $modrestore = $modname."_restore_mods";
279 //If exists the lib & function
280 $exist = "exists_".$modname;
281 $restore_var = "restore_".$modname;
282 $user_info_var = "restore_user_info_".$modname;
283 if (isset($$exist)) {
284 if ($$exist) {
285 //Now check that we have that module info in the backup file
286 if (isset($info->mods[$modname]) && $info->mods[$modname]->backup == "true") {
287 //Print the full tr
288 echo "<tr class=\"r".$currentrow."\">";
289 echo "<td align=\"right\">&nbsp;";
290 echo "</td><td>";
291 $restore_options[1] = get_string("yes");
292 $restore_options[0] = get_string("no");
293 //choose_from_menu($restore_options, $restore_var, $$restore_var, "");
294 //choose_from_radio($restore_options, $restore_var, $$restore_var);
295 //Print the checkbox
296 print_checkbox($restore_var, $$restore_var, $$restore_var, get_string("modulenameplural",$modname),'','selectItemInCheckboxByName(\'form1\',\'restore_'.$modname.'\',this.checked)');
297 //If backup contains user data, then show menu, else fix it to
298 //without user data
299 echo "</td><td align=\"right\">&nbsp;";
300 echo "</td><td>";
301 if ($info->mods[$modname]->userinfo == "true") {
302 $restore_user_options[1] = get_string("yes");
303 $restore_user_options[0] = get_string("no");
304 //choose_from_menu($restore_user_options, $user_info_var, $$user_info_var, "");
305 //choose_from_radio($restore_user_options, $user_info_var, $$user_info_var);
306 print_checkbox($user_info_var, $$user_info_var, $$user_info_var, get_string("userdata"),'','selectItemInCheckboxByName(\'form1\',\'restore_user_info_'.$modname.'\',this.checked)');
307 } else {
308 //Module haven't userdata
309 echo get_string("withoutuserdata");
310 echo "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
312 echo "</td></tr>";
313 if (isset($info->mods[$modname]->instances)) {
314 $instances = $info->mods[$modname]->instances;
316 if (!empty($instances) && is_array($instances)) {
317 echo '<tr><td></td><td colspan="3"><table class="restore-form-instances">';
318 foreach ($instances as $instance) {
319 echo '<tr><td>';
320 $var = 'restore_'.$modname.'_instance_'.$instance->id;
321 $$var = optional_param($var,1);
322 print_checkbox($var,$$var,$$var,$instance->name,$instance->name,'this.form.elements[\'restore_'.$modname.'\'].checked=1;');
323 echo '</td><td align="right">&nbsp;';
324 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
325 $$var = optional_param($var,1);
326 if ($info->mods[$modname]->instances[$instance->id]->userinfo == 'true') {
327 print_checkbox($var,$$var,$$var,get_string('userdata'),'','this.form.elements[\'restore_user_info_'.$modname.'\'].checked=1;');
328 } else {
329 echo '<input type="hidden" name="'.$var.'" value="0" />';
331 echo '</td></tr>';
333 echo '</table></td></tr>';
335 } else {
336 //Module isn't restorable
337 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
338 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
340 } else {
341 //Module isn't restorable
342 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
343 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
345 } else {
346 //Module isn't restorable
347 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
348 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
350 $currentrow = ($currentrow + 1) % 2;
352 //Line
353 echo "<tr><td colspan=\"4\">$nonrestmod<hr /></td></tr>";
355 //Now print the Metacourse tr
356 echo "<tr>";
357 echo "<td align=\"right\" colspan=\"2\"><b>";
358 echo get_string("metacourse").":";
359 echo "</b></td><td colspan=\"2\">";
360 //If metacourse are in the backup file, show menu, else fixed to no
361 if ($info->backup_metacourse == "true") {
362 $metacourse_options[0] = get_string("no");
363 $metacourse_options[1] = get_string("yes");
364 choose_from_menu($metacourse_options, "restore_metacourse", $restore_metacourse, "");
365 } else {
366 echo get_string("no");
367 echo "<input type=\"hidden\" name=\"restore_metacourse\" value=\"0\" />";
369 echo "</td></tr>";
370 //Now print the Users tr
371 echo "<tr>";
372 echo "<td align=\"right\" colspan=\"2\"><b>";
373 echo get_string("users").":";
374 echo "</b></td><td colspan=\"2\">";
375 //If some user is present in the backup file
376 if ($info->backup_users == "all" or $info->backup_users == "course") {
377 //If all users are in the backup file
378 if ($info->backup_users == "all") {
379 $user_options[0] = get_string("all");
381 $user_options[1] = get_string("course");
382 $user_options[2] = get_string("none");
383 choose_from_menu($user_options, "restore_users", $restore_users, "");
384 } else {
385 echo get_string("none");
386 echo "<input type=\"hidden\" name=\"restore_users\" value=\"2\" />";
389 echo "</td></tr>";
391 //Now print the Logs tr
392 echo "<tr>";
393 echo "<td align=\"right\" colspan=\"2\"><b>";
394 echo get_string("logs").":";
395 echo "</b></td><td colspan=\"2\">";
396 //If logs are in the backup file, show menu, else fixed to no
397 if ($info->backup_logs == "true") {
398 $log_options[0] = get_string("no");
399 $log_options[1] = get_string("yes");
400 choose_from_menu($log_options, "restore_logs", $restore_logs, "");
401 } else {
402 echo get_string("no");
403 echo "<input type=\"hidden\" name=\"restore_logs\" value=\"0\" />";
405 echo "</td></tr>";
407 //Now print the User Files tr
408 echo "<tr>";
409 echo "<td align=\"right\" colspan=\"2\"><b>";
410 echo get_string ("userfiles").":";
411 echo "</b></td><td colspan=\"2\">";
412 //If user files are in the backup file, show menu, else fixed to no
413 if ($info->backup_user_files == "true") {
414 $user_file_options[0] = get_string("no");
415 $user_file_options[1] = get_string("yes");
416 choose_from_menu($user_file_options, "restore_user_files", $restore_user_files, "");
417 } else {
418 echo get_string("no");
419 echo "<input type=\"hidden\" name=\"restore_user_files\" value=\"0\" />";
421 echo "</td></tr>";
423 //Now print the Course Files tr
424 echo "<tr>";
425 echo "<td align=\"right\" colspan=\"2\"><b>";
426 echo get_string ("coursefiles").":";
427 echo "</b></td><td colspan=\"2\">";
428 echo "<input type=\"hidden\" name=\"backup_unique_code\" value=\"$backup_unique_code\" />";
429 echo "<input type=\"hidden\" name=\"file\" value=\"$file\" />";
430 //If course files are in the backup file, show menu, else fixed to no
431 if ($info->backup_course_files == "true") {
432 $course_file_options[0] = get_string("no");
433 $course_file_options[1] = get_string("yes");
434 choose_from_menu($course_file_options, "restore_course_files", $restore_course_files, "");
435 } else {
436 echo get_string("no");
437 echo "<input type=\"hidden\" name=\"restore_course_files\" value=\"0\" />";
439 echo "</td></tr>";
442 //Now print the Site Files tr
443 echo "<tr>";
444 echo "<td align=\"right\" colspan=\"2\"><b>";
445 echo get_string ("sitefiles").":";
446 echo "</b></td><td colspan=\"2\">";
447 //If site files are in the backup file, show menu, else fixed to no
448 if (isset($info->backup_site_files) && $info->backup_site_files == "true") {
449 $site_file_options[0] = get_string("no");
450 $site_file_options[1] = get_string("yes");
451 choose_from_menu($site_file_options, "restore_site_files", $restore_site_files, "");
452 } else {
453 echo get_string("no");
454 echo "<input type=\"hidden\" name=\"restore_site_files\" value=\"0\" />";
456 echo "</td></tr>";
458 //Now print the Messages tr
459 echo "<tr>";
460 echo "<td align=\"right\" colspan=\"2\"><b>";
462 //This tr is slighty different. Everything becomes hidden if
463 //we haven't messages is the backup, to avoid confusions to users.
464 //If messages are in the backup file, show menu, else fixed to no and show nothing
465 if ($info->backup_messages == "true") {
466 echo get_string ('messages','message').":";
467 echo "</b></td><td colspan=\"2\">";
468 $message_options[0] = get_string("no");
469 $message_options[1] = get_string("yes");
470 choose_from_menu($message_options, "restore_messages", $restore_messages, "");
471 } else {
472 echo "&nbsp;</b></td><td colspan=\"2\">";
473 echo "<input type=\"hidden\" name=\"restore_messages\" value=\"0\" />";
475 echo "</td></tr>";
478 </table>
480 <hr/>
481 <?php
483 print_heading(get_string('rolemappings'));
484 $xml_file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
486 $info = restore_read_xml_info($xml_file);
488 // fix for MDL-9068, front page course is just a normal course
489 $siterolesarray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "shortname");
490 $siterolesnamearray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "name");
492 echo ('<table width="100%" class="restore-form-instances">');
493 echo ('<tr><td align="right" style="width:50%"><b>'.get_string('sourcerole').'</b></td><td align="left" style="width:50%"><b>'.get_string('targetrole').'</b></td></tr>');
495 if ($info->backup_moodle_version < 2006092801) {
496 // 1.6 and below backup
498 /// Editting teacher
499 echo ('<tr><td align="right">');
500 print_string('defaultcourseteacher');
501 echo ('</td><td algin="left">');
503 // get the first teacheredit legacy
504 $roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM, SITEID));
506 $editteacher = array_shift($roles);
507 choose_from_menu ($siterolesarray, "defaultteacheredit", $editteacher->id, 'new role', '', '0');
508 echo ('</td></tr>');
510 /// Non-editting teacher
511 echo ('<tr><td align="right">');
512 print_string('noneditingteacher');
513 echo ('</td><td algin="left">');
515 // get the first teacheredit legacy
516 $roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM, SITEID));
517 $teacher = array_shift($roles);
519 choose_from_menu ($siterolesarray, "defaultteacher", $teacher->id, 'new role', '', '0');
520 echo ('</td></tr>');
523 /// Student
524 echo ('<tr><td align="right">');
525 print_string('defaultcoursestudent');
526 echo ('</td><td algin="left">');
528 // get the first teacheredit legacy
529 $roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM, SITEID));
530 $studentrole = array_shift($roles);
532 choose_from_menu ($siterolesarray, "defaultstudent", $studentrole->id, 'new role', '', '0');
533 echo ('</td></tr>');
535 } else {
536 // 1.7 and above backup
537 $roles = restore_read_xml_roles($xml_file);
539 if (!empty($roles->roles)) { // possible to have course with no roles
540 foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
541 $siteroleschoicearray[$siteroleid] = $siterolesnamearray[$siteroleid]." (". $siterolesarray[$siteroleid].")";
544 foreach ($roles->roles as $roleid=>$role) {
545 echo ('<tr><td align="right">');
546 echo $role->name." (".($role->shortname).")";
547 echo ('</td><td align="left">');
549 // see if any short name match
550 $matchrole = 0;
551 foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
552 if ($siteroleshortname == $role->shortname) {
553 $matchrole = $siteroleid;
554 break;
558 choose_from_menu ($siteroleschoicearray, "roles_".$roleid, $matchrole, 'new role', '', '0');
559 echo ('</td></tr>');
563 } // end else
565 echo ('</table>'); // end of role mappings table
568 <br />
569 <div style="text-align:center">
570 <input type="hidden" name="id" value="<?php p($id) ?>" />
571 <input type="hidden" name="launch" value="check" />
572 <input type="hidden" name="fromform" value="1" />
573 <input type="submit" value="<?php print_string("continue") ?>" />
574 <input type="submit" name="cancel" value="<?php print_string("cancel") ?>" />
575 </div>
576 </div>
577 </form>