[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/backup/ -> restore_form.html (source)

   1  <?php //$Id: restore_form.html,v 1.60.2.14 2008/07/07 22:34:21 skodak Exp $
   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.
   6  
   7      //Get objects from session
   8      if (!($info = $SESSION->info)) {
   9        error( 'info object missing from session' );
  10      }
  11      if (!($course_header = $SESSION->course_header)) {
  12        error( 'course_header object missing from session' );
  13      }
  14  
  15      $restore_gradebook_history =  optional_param('restore_gradebook_history', 0, PARAM_INT);
  16  
  17      //Check that we have all we need
  18      //backup_unique_code
  19      $backup_unique_code = required_param( 'backup_unique_code' );
  20      //file
  21      $file = required_param( 'file' );
  22  
  23      //Check login
  24      require_login();
  25  
  26      //Check admin
  27      if (!empty($id)) {
  28          if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))) {
  29              error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
  30          }
  31      } else {
  32          if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM))) {
  33              error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
  34          }
  35      }
  36  
  37      //Check site
  38      if (!$site = get_site()) {
  39          error("Site not found!");
  40      }
  41  
  42      //Checks for the required files/functions to restore every mod
  43      $count = 0;
  44      if ($allmods = get_records("modules") ) {
  45          foreach ($allmods as $mod) {
  46              $modname = $mod->name;
  47              $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
  48              $modrestore = $modname."_restore_mods";
  49              if (file_exists($modfile)) {
  50                 include_once($modfile);
  51                 if (function_exists($modrestore)) {
  52                     $var = "exists_".$modname;
  53                     $$var = true;
  54                     $count++;
  55                 }
  56              }
  57              //Check data
  58              //Check module info
  59              $var = "restore_".$modname;
  60              if (!isset($$var)) {
  61                  $$var = 1;
  62              }
  63              //Check include user info
  64              $var = "restore_user_info_".$modname;
  65              if (!isset($$var)) {
  66                  $$var = 1;
  67              }
  68          }
  69      }
  70  
  71      //Check other parameters
  72      if (!isset($restore_metacourse)) {
  73          $restore_metacourse = 1;
  74      }
  75  
  76      if (!isset($restore_users)) {
  77          $restore_users = 1;
  78      }
  79  
  80      if (!isset($restore_groups)) {
  81          if (empty($CFG->enablegroupings)) {
  82              $restore_groups = RESTORE_GROUPS_ONLY;
  83          } else {
  84              $restore_groups = RESTORE_GROUPS_GROUPINGS;
  85          }
  86      }
  87  
  88      if (!isset($restore_logs)) {
  89          $restore_logs = 1;
  90      }
  91  
  92      if (!isset($restore_user_files)) {
  93          $restore_user_files = 1;
  94      }
  95  
  96      if (!isset($restore_course_files)) {
  97          $restore_course_files = 1;
  98      }
  99  
 100      if (!isset($restore_site_files)) {
 101          $restore_site_files = 1;
 102      }
 103  
 104      if (!isset($restore_messages)) {
 105          $restore_messages = 1;
 106      }
 107  
 108      if (!isset($restore_blogs)) {
 109          $restore_blogs = 1;
 110      }
 111  
 112      if (!isset($restore_restoreto)) {
 113          if (!user_can_create_courses()) {
 114              $restore_restoreto = 1;
 115          } else {
 116              $restore_restoreto = 2;
 117          }
 118      }
 119  
 120      if (!isset($course_header->category->id)) {
 121          $course_header->category->id = 0;
 122      }
 123  
 124      if(!isset($form1->startdate)) {
 125          $form1->startdate = $course_header->course_startdate; //$course_header->course_startdate;
 126      }
 127  
 128      if (empty($form1->shortname)) {
 129         $form1->shortname = $course_header->course_shortname; //'_shortname'; //$course_header->course_shortname;
 130      }
 131  
 132      if (empty($form1->fullname)) {
 133        $form1->fullname = $course_header->course_fullname; // '_fullname';   //$course_header->course_fullname;
 134      }
 135  
 136      if ($count == 0) {
 137          notice("No restorable modules are installed!");
 138      }
 139  
 140  ?>
 141  
 142  <script type="text/javascript">
 143  <!--
 144  function selectItemInMenuByName(formId, menuName, selectIndex ) {
 145      myForm = document.getElementById(formId)
 146      for (i=0,n=myForm.elements.length;i<n;i++) {
 147          myLen = menuName.length;
 148          myName = myForm.elements[i].name;
 149          myType = myForm.elements[i].type;
 150          if (myName.substring(0,myLen) == menuName && myType == "select-one") {
 151              myForm.elements[i].options[selectIndex].selected = true;
 152          }
 153      }
 154  }
 155  
 156  function selectItemInRadioByName(formId, radioName, selectIndex ) {
 157      myForm = document.getElementById(formId)
 158      for (i=0,n=myForm.elements.length;i<n;i++) {
 159          myLen = radioName.length;
 160          myName = myForm.elements[i].name;
 161          myType = myForm.elements[i].type;
 162          if (myName.substring(0,myLen) == radioName && myType == "radio") {
 163              myRadioGroup = myForm.elements[myName];
 164              myRadioGroup[selectIndex].checked = true;
 165          }
 166      }
 167  }
 168  
 169  function selectItemInCheckboxByName(formId, checkName, checked ) {
 170      myForm = document.getElementById(formId)
 171      for (i=0,n=myForm.elements.length;i<n;i++) {
 172          myLen = checkName.length;
 173          myName = myForm.elements[i].name;
 174          myType = myForm.elements[i].type;
 175          if (myName.substring(0,myLen) == checkName && myType == "checkbox") {
 176              myForm.elements[i].checked = checked;
 177          }
 178      }
 179  }
 180  -->
 181  </script>
 182  
 183  <form id="form1" method="post" action="restore.php">
 184  <div>
 185  <table cellpadding="5" class="boxaligncenter">
 186  <?php
 187  
 188      //First, course destination
 189      //Print the full tr
 190      echo "<tr>";
 191      echo "<td align=\"right\"><b>";
 192      echo '<label for="menurestore_restoreto">'.get_string ('restoreto').'</label>';
 193      echo "</b>";
 194      echo "</td><td colspan=\"3\">";
 195  
 196  
 197      // permission should have been checked already
 198  
 199      /**
 200       * if user has manageactivities in any course and we are not restoring from within SITEID
 201       *      existingcoursedeleting
 202       *      existingcourseadding
 203       * else we show
 204       *      currentcoursedeleting
 205       *      currentcourse
 206       * if user has course:create in any category, we show
 207       *      newcourse
 208       */
 209       
 210      // Non-cached - get accessinfo
 211      if (isset($USER->access)) {
 212          $accessinfo = $USER->access;
 213      } else {
 214          $accessinfo = get_user_access_sitewide($USER->id);
 215      }
 216  
 217      $mycourses = get_user_courses_bycap($USER->id, 'moodle/site:restore', $accessinfo, true);
 218  
 219      // if the user can manage 2 or more courses and we are not restoring from within SITEID,
 220      // we show options for existing courses
 221  
 222      if (count($mycourses) > 1 && $id != SITEID) {
 223          $restore_restoreto_options[0] = get_string("existingcoursedeleting");
 224          $restore_restoreto_options[1] = get_string("existingcourseadding");
 225      // else if the user can write to current course
 226      } else if (has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))){
 227          $restore_restoreto_options[0] = get_string("currentcoursedeleting");
 228          $restore_restoreto_options[1] = get_string("currentcourseadding");
 229      }
 230  
 231      // if user can create any course at all, give the option
 232      if (user_can_create_courses()) {
 233          $restore_restoreto_options[2] = get_string("newcourse");
 234      }
 235  
 236  /// Acummulator for hidden options and proper XHTML output
 237      $hidden_options = '';
 238  
 239      choose_from_menu($restore_restoreto_options, "restore_restoreto", $restore_restoreto, "");
 240      echo "</td></tr>";
 241      if (user_can_create_courses()) {  //display these fields conditionally
 242  
 243          // find the list of cates user can edit
 244          echo "<tr valign=\"top\" >";
 245          echo "<td align=\"right\">";
 246          echo '<label for="menurestore_restorecatto">'.get_string ('category').'</label>';
 247          echo "</td>";
 248          echo "<td>";
 249          // Category selection isn't alowed if restoring from within SITEID course
 250          if ($id != SITEID) {
 251              make_categories_list($categories, $parents);
 252              choose_from_menu($categories, "restore_restorecatto", $course_header->category->id, "");
 253          } else {
 254              print_string('notavailable');
 255              echo '<input type="hidden" name="restore_restorecatto" id="menurestore_restorecatto" value="0" />';
 256          }
 257          echo "</td>";
 258          echo "</tr>";
 259  
 260          echo "<tr valign=\"top\" >";
 261          echo "<td align=\"right\">";
 262          echo '<label for="shortnamefield">'.get_string ('shortname').'</label>';
 263          echo "</td>";
 264          echo "<td><input type=\"text\" id=\"shortnamefield\" name=\"shortname\" maxlength=\"100\"  size=\"20\" value=\"".s($form1->shortname)."\" alt=\"".get_string("shortname")."\"  />" ;
 265          helpbutton("courseshortname", get_string("shortname")) ;
 266          if (isset($err["shortname"])) formerr($err["shortname"]);
 267          echo "</td>";
 268          echo "</tr>";
 269          echo "<tr valign=\"top\" >";
 270          echo "<td align=\"right\">";
 271          echo '<label for="fullnamefield">'.get_string ('fullname').'</label>';
 272          echo "</td>";
 273          echo "<td><input type=\"text\" id=\"fullnamefield\" name=\"fullname\" maxlength=\"254\" size=\"50\" value=\"".s($form1->fullname)."\" alt=\" ".get_string("fullname")."\"  />" ;
 274          helpbutton("coursefullname", get_string("fullname")) ;
 275          if (isset($err["fullname"])) formerr($err["fullname"]);
 276          echo"</td></tr>";
 277          echo "<tr valign=\"top\"> ";
 278          echo "<td align=\"right\"> ";
 279          print_string("startdate");
 280          echo "</td><td>";
 281      /// Show the roll dates option only if the backup course has a start date
 282      /// (some formats like main page, social..., haven't it and rolling dates
 283      /// from 0 produces crazy dates. MDL-10125
 284          if ($form1->startdate) {
 285              print_date_selector("startday", "startmonth", "startyear", $form1->startdate);
 286              helpbutton("coursestartdate", get_string("startdate"));
 287          } else {
 288              print_string('notavailable');
 289              echo '<input type="hidden" name="startyear" value="0" />';
 290              echo '<input type="hidden" name="startmonth" value="0" />';
 291              echo '<input type="hidden" name="startday" value="0" />';
 292          }
 293          echo "</td></tr>";
 294      }
 295      //Line
 296      echo "<tr><td colspan=\"4\"><hr /></td></tr>";
 297      //Now, check modules and info and show posibilities
 298      if ($allmods = get_records("modules") ) {
 299          //Print option to select/deselect everything with 1 click.
 300          echo "<tr>";
 301          echo "<td align=\"right\">";
 302          echo '<b>'.get_string("include")."</b>";
 303          echo "</td><td>";
 304          echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', true);\">".
 305               get_string("all")."</a>/";
 306          echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', false);\">".
 307               get_string("none")."</a>";
 308          echo "</td>";
 309          echo "<td align=\"right\">";
 310          echo '<b>&nbsp;</b>';
 311          echo "</td><td>";
 312          echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', true);\">".
 313               get_string("all")."</a>/";
 314          echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', false);\">".
 315               get_string("none")."</a>";
 316          echo "</td>";
 317          echo "</tr>";
 318          echo "<tr><td colspan=\"4\"><hr /></td></tr>";
 319          $currentrow = 0;
 320          $nonrestmod = '';
 321          foreach ($allmods as $mod) {
 322              $modname = $mod->name;
 323              $modrestore = $modname."_restore_mods";
 324              //If exists the lib & function
 325              $exist = "exists_".$modname;
 326              $restore_var = "restore_".$modname;
 327              $user_info_var = "restore_user_info_".$modname;
 328              if (isset($$exist)) {
 329                  if ($$exist) {
 330                      //Now check that we have that module info in the backup file
 331                      if (isset($info->mods[$modname]) && $info->mods[$modname]->backup == "true") {
 332                          //Print the full tr
 333                          echo "<tr class=\"r".$currentrow."\">";
 334                          echo "<td align=\"right\">&nbsp;";
 335                          echo "</td><td>";
 336                          $restore_options[1] = get_string("yes");
 337                          $restore_options[0] = get_string("no");
 338                          //choose_from_menu($restore_options, $restore_var, $$restore_var, "");
 339                          //choose_from_radio($restore_options, $restore_var, $$restore_var);
 340                          //Print the checkbox
 341                          print_checkbox($restore_var, $$restore_var, $$restore_var, get_string("modulenameplural",$modname),'','selectItemInCheckboxByName(\'form1\',\'restore_'.$modname.'\',this.checked)');
 342                          //If backup contains user data, then show menu, else fix it to
 343                          //without user data
 344                          echo "</td><td align=\"right\">&nbsp;";
 345                          echo "</td><td>";
 346                          if ($info->mods[$modname]->userinfo == "true") {
 347                              $restore_user_options[1] = get_string("yes");
 348                              $restore_user_options[0] = get_string("no");
 349                              //choose_from_menu($restore_user_options, $user_info_var, $$user_info_var, "");
 350                              //choose_from_radio($restore_user_options, $user_info_var, $$user_info_var);
 351                              print_checkbox($user_info_var, $$user_info_var, $$user_info_var, get_string("userdata"),'','selectItemInCheckboxByName(\'form1\',\'restore_user_info_'.$modname.'\',this.checked)');
 352                          } else {
 353                              //Module haven't userdata
 354                              echo get_string("withoutuserdata");
 355                              echo "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
 356                          }
 357                          echo "</td></tr>";
 358                          if (isset($info->mods[$modname]->instances)) {
 359                              $instances = $info->mods[$modname]->instances;
 360                          }
 361                          if (!empty($instances) && is_array($instances)) {
 362                              echo '<tr><td></td><td colspan="3"><table class="restore-form-instances">';
 363                              foreach ($instances as $instance) {
 364                                  echo '<tr><td>';
 365                                  $var = 'restore_'.$modname.'_instance_'.$instance->id;
 366                                  $$var = optional_param($var,1);
 367                                  print_checkbox($var,$$var,$$var,$instance->name,$instance->name,'this.form.elements[\'restore_'.$modname.'\'].checked=1;');
 368                                  echo '</td><td align="right">&nbsp;';
 369                                  $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
 370                                  $$var = optional_param($var,1);
 371                                  if (!empty($info->mods[$modname]->instances) && ($info->mods[$modname]->instances[$instance->id]->userinfo == 'true')) {
 372                                      print_checkbox($var,$$var,$$var,get_string('userdata'),'','this.form.elements[\'restore_user_info_'.$modname.'\'].checked=1;');
 373                                  } else {
 374                                      echo '<input type="hidden" name="'.$var.'" value="0" />';
 375                                  }
 376                                  echo '</td></tr>';
 377                              }
 378                              echo '</table></td></tr>';
 379                          }
 380                      } else {
 381                          //Module isn't restorable
 382                          $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
 383                          $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
 384                      }
 385                  } else {
 386                      //Module isn't restorable
 387                      $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
 388                      $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
 389                  }
 390              } else {
 391                  //Module isn't restorable
 392                  $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
 393                  $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
 394              }
 395              $currentrow = ($currentrow + 1) % 2;
 396          }
 397          //Line
 398          echo "<tr><td colspan=\"4\">$nonrestmod<hr /></td></tr>";
 399  
 400          //Now print the Metacourse tr
 401          echo "<tr>";
 402          echo "<td align=\"right\" colspan=\"2\"><b>";
 403          echo '<label for="menurestore_metacourse">'.get_string ("metacourse").'</label>';
 404          echo "</b></td><td colspan=\"2\">";
 405          //If metacourse are in the backup file, show menu, else fixed to no
 406          if ($info->backup_metacourse == "true") {
 407              $metacourse_options = array();
 408              $metacourse_options[0] = get_string("no");
 409              $metacourse_options[1] = get_string("yes");
 410              choose_from_menu($metacourse_options, "restore_metacourse", $restore_metacourse, "");
 411          } else {
 412              echo get_string("no");
 413              echo "<input type=\"hidden\" id=\"menurestore_metacourse\" name=\"restore_metacourse\" value=\"0\" />";
 414          }
 415          echo "</td></tr>";
 416          //Now print the Users tr
 417          echo "<tr>";
 418          echo "<td align=\"right\" colspan=\"2\"><b>";
 419          echo '<label for="menurestore_users">'.get_string ("users").'</label>';
 420          echo "</b></td><td colspan=\"2\">";
 421          //If some user is present in the backup file
 422          if ($info->backup_users == "all" or $info->backup_users == "course") {
 423              $user_options = array();
 424              //If all users are in the backup file
 425              if ($info->backup_users == "all") {
 426                  $user_options[0] = get_string("all");
 427              }
 428              $user_options[1] = get_string("course");
 429              $user_options[2] = get_string("none");
 430              choose_from_menu($user_options, "restore_users", $restore_users, "");
 431          } else {
 432              echo get_string("none");
 433              echo "<input type=\"hidden\" id=\"menurestore_users\" name=\"restore_users\" value=\"2\" />";
 434  
 435          }
 436          echo "</td></tr>";
 437  
 438          //Now print the Groups tr (assume there is no $info->backup_groups)
 439          echo "<tr>";
 440          echo "<td align=\"right\" colspan=\"2\"><b>";
 441          $helplink = helpbutton('grouprestore', get_string('groups'), '', true, false, '', true);
 442          if (empty($CFG->enablegroupings)) {
 443              echo '<label for="menurestore_groups">'.get_string ("groups").'</label>'.$helplink;
 444              echo "</b></td><td colspan=\"2\">";
 445              $group_options[RESTORE_GROUPS_NONE] = get_string('no');
 446              $group_options[RESTORE_GROUPS_ONLY] = get_string('yes');
 447          } else {
 448              echo '<label for="menurestore_groups">'.get_string ('groupsgroupings', 'group').'</label>'.$helplink;
 449              echo "</b></td><td colspan=\"2\">";
 450              $group_options[RESTORE_GROUPS_NONE] = get_string('none');
 451              $group_options[RESTORE_GROUPS_ONLY] = get_string('groupsonly', 'group');
 452              $group_options[RESTORE_GROUPINGS_ONLY] = get_string('groupingsonly', 'group');
 453              $group_options[RESTORE_GROUPS_GROUPINGS] = get_string('groupsgroupings', 'group'); //all.
 454  
 455          } /*else {
 456              echo get_string('none');
 457              echo "<input type=\"hidden\" name=\"restore_groups\" value=\"2\" />";
 458          }*/
 459          choose_from_menu($group_options, 'restore_groups', $restore_groups, '');
 460          echo "</td></tr>";
 461  
 462          //Now print the Logs tr
 463          echo "<tr>";
 464          echo "<td align=\"right\" colspan=\"2\"><b>";
 465          echo '<label for="menurestore_logs">'.get_string ("logs").'</label>';
 466          echo "</b></td><td colspan=\"2\">";
 467          //If logs are in the backup file, show menu, else fixed to no
 468          if ($info->backup_logs == "true") {
 469              $log_options = array();
 470              $log_options[0] = get_string("no");
 471              $log_options[1] = get_string("yes");
 472              choose_from_menu($log_options, "restore_logs", $restore_logs, "");
 473          } else {
 474              echo get_string("no");
 475              echo "<input type=\"hidden\" id=\"menurestore_logs\" name=\"restore_logs\" value=\"0\" />";
 476          }
 477          echo "</td></tr>";
 478  
 479          //Now print the User Files tr
 480          echo "<tr>";
 481          echo "<td align=\"right\" colspan=\"2\"><b>";
 482          echo '<label for="menurestore_user_files">'.get_string ("userfiles").'</label>';
 483          echo "</b></td><td colspan=\"2\">";
 484          //If user files are in the backup file, show menu, else fixed to no
 485          if ($info->backup_user_files == "true") {
 486              $user_file_options = array();
 487              $user_file_options[0] = get_string("no");
 488              $user_file_options[1] = get_string("yes");
 489              choose_from_menu($user_file_options, "restore_user_files", $restore_user_files, "");
 490          } else {
 491              echo get_string("no");
 492              echo "<input type=\"hidden\" id=\"menurestore_user_files\" name=\"restore_user_files\" value=\"0\" />";
 493          }
 494          echo "</td></tr>";
 495  
 496          //Now print the Course Files tr
 497          echo "<tr>";
 498          echo "<td align=\"right\" colspan=\"2\"><b>";
 499          echo '<label for="menurestore_course_files">'.get_string ("coursefiles").'</label>';
 500          echo "</b></td><td colspan=\"2\">";
 501          echo "<input type=\"hidden\" name=\"backup_unique_code\" value=\"$backup_unique_code\" />";
 502          echo "<input type=\"hidden\" name=\"file\" value=\"$file\" />";
 503          //If course files are in the backup file, show menu, else fixed to no
 504          if ($info->backup_course_files == "true") {
 505              $course_file_options = array();
 506              $course_file_options[0] = get_string("no");
 507              $course_file_options[1] = get_string("yes");
 508              choose_from_menu($course_file_options, "restore_course_files", $restore_course_files, "");
 509          } else {
 510              echo get_string("no");
 511              echo "<input type=\"hidden\" id=\"menurestore_course_files\" name=\"restore_course_files\" value=\"0\" />";
 512          }
 513          echo "</td></tr>";
 514  
 515  
 516          //Now print the Site Files tr
 517          echo "<tr>";
 518          echo "<td align=\"right\" colspan=\"2\"><b>";
 519          echo '<label for="menurestore_site_files">'.get_string ("sitefiles").'</label>';
 520          echo "</b></td><td colspan=\"2\">";
 521          //If site files are in the backup file, show menu, else fixed to no
 522          if (isset($info->backup_site_files) && $info->backup_site_files == "true") {
 523              $site_file_options = array();
 524              $site_file_options[0] = get_string("no");
 525              $site_file_options[1] = get_string("yes");
 526              choose_from_menu($site_file_options, "restore_site_files", $restore_site_files, "");
 527          } else {
 528              echo get_string("no");
 529              echo "<input type=\"hidden\" id=\"menurestore_site_files\" name=\"restore_site_files\" value=\"0\" />";
 530          }
 531          echo "</td></tr>";
 532          
 533          // do you want grade histories to be restored?
 534          if (empty($CFG->disablegradehistory)) {
 535              echo "<tr>";
 536              echo "<td align=\"right\" colspan=\"2\"><b>";
 537              echo '<label for="menurestore_gradebook_history">'.get_string ('gradebookhistories', 'grades').'</label>';
 538              echo "</b></td><td colspan=\"2\">";
 539              if (isset($info->gradebook_histories) && $info->gradebook_histories == "true") {
 540                  $gradebook_history_options = array();
 541                  $gradebook_history_options[0] = get_string("no");
 542                  $gradebook_history_options[1] = get_string("yes");
 543                  choose_from_menu($gradebook_history_options, "restore_gradebook_history", $restore_gradebook_history, "");
 544              } else {
 545                  echo get_string("no");
 546                  echo "<input type=\"hidden\" id=\"menurestore_gradebook_history\" name=\"restore_gradebook_history\" value=\"0\" />";
 547              }
 548              echo "</td></tr>";
 549          } else {
 550              $hidden_options .= '<input type="hidden" name="restore_gradebook_history" value="0" />';
 551          }
 552  
 553          //This tr is slighty different. Everything becomes hidden if
 554          //we haven't messages is the backup, to avoid confusions to users.
 555          //If messages are in the backup file, show menu, else fixed to no and show nothing
 556          //Also, messaging must be enabled in the destination site
 557          if (isset($info->backup_messages) && $info->backup_messages == "true" && !empty($CFG->messaging)) {
 558              echo "<tr>";
 559              echo "<td align=\"right\" colspan=\"2\"><b>";
 560              echo '<label for="menurestore_messages">'.get_string ('messages', 'message').'</label>';
 561              echo "</b></td><td colspan=\"2\">";
 562              $message_options = array();
 563              $message_options[0] = get_string("no");
 564              $message_options[1] = get_string("yes");
 565              choose_from_menu($message_options, "restore_messages", $restore_messages, "");
 566              echo "</td></tr>";
 567          } else {
 568              $hidden_options .= '<input type="hidden" name="restore_messages" value="0" />';
 569          }
 570  
 571          //This tr is slighty different. Everything becomes hidden if
 572          //we haven't blogs is the backup, to avoid confusions to users.
 573          //If blogs are in the backup file, show menu, else fixed to no and show nothing
 574          //Also, blogs must be enabled in the destination site
 575          if (isset($info->backup_blogs) && $info->backup_blogs == "true" && !empty($CFG->bloglevel)) {
 576              echo "<tr>";
 577              echo "<td align=\"right\" colspan=\"2\"><b>";
 578              echo '<label for="menurestore_blogs">'.get_string ('blogs', 'blog').'</label>';
 579              echo "</b></td><td colspan=\"2\">";
 580              $blog_options = array();
 581              $blog_options[0] = get_string("no");
 582              $blog_options[1] = get_string("yes");
 583              choose_from_menu($blog_options, "restore_blogs", $restore_blogs, "");
 584              echo "</td></tr>";
 585          } else {
 586              $hidden_options .= '<input type="hidden" name="restore_blogs" value="0" />';
 587          }
 588  
 589      }
 590  ?>
 591  </table>
 592  
 593  <hr />
 594  <?php
 595  
 596  print_heading(get_string('rolemappings'));
 597  $xml_file  = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
 598  
 599  $info = restore_read_xml_info($xml_file);
 600  
 601  // fix for MDL-9068, front page course is just a normal course
 602  $siterolesarray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "shortname", ROLENAME_ORIGINAL);
 603  $siterolesnamearray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "name", ROLENAME_ORIGINAL);
 604  $allroles = get_records('role');
 605  
 606  echo ('<table width="100%" class="restore-form-instances">');
 607  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>');
 608  
 609  if ($info->backup_moodle_version < 2006092801) {
 610      // 1.6 and below backup
 611  
 612      /// Editting teacher
 613      echo ('<tr><td align="right">');
 614      echo '<label for="menudefaultteacheredit">'.get_string ('defaultcourseteacher').'</label>';
 615      echo ('</td><td algin="left">');
 616  
 617      // get the first teacheredit legacy
 618      $roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM));
 619  
 620      $editteacher = reset($roles);
 621      choose_from_menu ($siterolesarray, "defaultteacheredit", $editteacher->id, 'new role', '', '0');
 622      echo ('</td></tr>');
 623  
 624      /// Non-editting teacher
 625      echo ('<tr><td align="right">');
 626      echo '<label for="menudefaultteacher">'.get_string ('noneditingteacher').'</label>';
 627      print_string('noneditingteacher');
 628      echo ('</td><td algin="left">');
 629  
 630      // get the first teacheredit legacy
 631      $roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM));
 632      $teacher = reset($roles);
 633  
 634      choose_from_menu ($siterolesarray, "defaultteacher", $teacher->id, 'new role', '', '0');
 635      echo ('</td></tr>');
 636  
 637  
 638      /// Student
 639      echo ('<tr><td align="right">');
 640      echo '<label for="menudefaultstudent">'.get_string ('defaultcoursestudent').'</label>';
 641      echo ('</td><td algin="left">');
 642  
 643      // get the first teacheredit legacy
 644      $roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM));
 645      $studentrole = array_shift($roles);
 646  
 647      choose_from_menu ($siterolesarray, "defaultstudent", $studentrole->id, 'new role', '', '0');
 648      echo ('</td></tr>');
 649  
 650  } else {
 651      // 1.7 and above backup
 652      $roles = restore_read_xml_roles($xml_file);
 653  
 654      if (!empty($roles->roles)) { // possible to have course with no roles
 655          foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
 656              $siteroleschoicearray[$siteroleid] = $siterolesnamearray[$siteroleid]." (". $siterolesarray[$siteroleid].")";
 657          }
 658  
 659          foreach ($roles->roles as $roleid=>$role) {
 660  
 661              $mappableroles = $siteroleschoicearray;
 662  
 663              echo ('<tr><td align="right">');
 664              echo '<label for="menuroles_'.$roleid.'">'.$role->name." (".($role->shortname).")".'</label>';
 665              echo ('</td><td align="left">');
 666  
 667              /// first, we see if any exact role definition is found
 668              /// if found, that is the only option of restoring to
 669  
 670              if ($samerole = restore_samerole($roleid, $role)) {
 671                  $matchrole = $samerole->id;
 672                  // if an exact role is found, it does not matter whether this user can assign this role or not,
 673                  // this will be presented as a valid option regardless
 674                  $mappableroles[$samerole->id] = $allroles[$samerole->id]->name." (". $allroles[$samerole->id]->shortname.")";
 675              } else {
 676                  // no exact role found, let's try to match shortname
 677                  // this is useful in situations where basic roles differ slightly in definition
 678                  $matchrole = 0;
 679                  foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
 680                      if ($siteroleshortname == $role->shortname) {
 681                          $matchrole = $siteroleid;
 682                          break;
 683                      }
 684                  }
 685              }
 686              choose_from_menu ($mappableroles, "roles_".$roleid, $matchrole, 'new role', '', '0');
 687              echo ('</td></tr>');
 688          }
 689      }
 690  
 691  } // end else
 692  
 693  echo ('</table>'); // end of role mappings table
 694  
 695  ?>
 696  <br />
 697  <div style="text-align:center">
 698  <?php
 699  /// Print captured hidden options, now that we have closed the table
 700      echo $hidden_options;
 701  ?>
 702  <input type="hidden" name="id"     value="<?php  p($id) ?>" />
 703  <input type="hidden" name="launch" value="check" />
 704  <input type="hidden" name="fromform" value="1" />
 705  <input type="submit" value="<?php  print_string("continue") ?>" />
 706  <input type="submit" name="cancel" value="<?php  print_string("cancel") ?>" />
 707  </div>
 708  </div>
 709  </form>
 710  
 711  <?php
 712  
 713  /**
 714   * Look for a role in the database with exactly the same definition as the one in the backup file.
 715   * 
 716   * @param integer $roleid the id that the role in the backup files had on the old server.
 717   * @param object $role the rest of the definition of the role from the backup file. 
 718   */
 719  function restore_samerole($roleid, $rolefromxml) {
 720      global $CFG;
 721  
 722      // First we try some intelligent guesses, then, if none of those work, we do a more extensive
 723      // search.
 724  
 725      // First guess, try let's use the id
 726      if (restore_is_samerole($roleid, $rolefromxml)) {
 727          return get_record('role', 'id', $roleid); 
 728      }
 729  
 730      // Second guess, try the shortname
 731      $testroleid = get_field('role', 'id', 'shortname', $rolefromxml->shortname);
 732      if ($testroleid && restore_is_samerole($testroleid, $rolefromxml)) {
 733          return get_record('role', 'id', $testroleid); 
 734      }
 735  
 736      // Finally, search all other roles. In orter to speed things up, we exclude the ones we have
 737      // already tested, and we only search roles with the same number of capabilities set in their
 738      // definition.
 739      $extracondition = '';
 740      if ($testroleid) {
 741          $extracondition = "AND roleid <> $testroleid";
 742      }
 743      $candidateroleids = get_records_sql("SELECT roleid
 744             FROM {$CFG->prefix}role_capabilities
 745             WHERE roleid <> $roleid $extracondition
 746             GROUP BY roleid
 747             HAVING COUNT(capability) = ".count($rolefromxml->capabilities));
 748      if (!empty($candidateroleids)) {
 749          foreach ($candidateroleids as $testroleid => $notused) {
 750              if (restore_is_samerole($testroleid, $rolefromxml)) {
 751                  return get_record('role', 'id', $testroleid);
 752              }
 753          }
 754      }
 755  
 756      return false;
 757  }
 758  
 759  /**
 760   * Compare a role in the database with one loaded from the backup file, and determine whether
 761   * they have identical permissions for each capability.
 762   * @param integer $testroleid the id of the role from the database to test against.
 763   * @param object $rolefromxml the role definition loaded from the backup file.
 764   * @return boolean true if the two roles are identical.
 765   */
 766  function restore_is_samerole($testroleid, $rolefromxml) {
 767      // Load the role definition from the databse.
 768      $rolefromdb = get_records('role_capabilities', 'roleid', $testroleid, '', 'capability,permission'); 
 769      if (!$rolefromdb) {
 770          return false;
 771      }
 772  
 773      // Quick check, do they have the permissions on the same number of capabilities?
 774      if (count($rolefromdb) != count($rolefromxml->capabilities)) {
 775          return false;
 776      }
 777  
 778      // If they do, check each one.
 779      foreach ($rolefromdb as $capability => $permissions) {
 780          if (!isset($rolefromxml->capabilities[$capability]) ||
 781                  $permissions->permission != $rolefromxml->capabilities[$capability]->permission) {
 782              return false;
 783          }
 784      }
 785      return true;
 786  }
 787  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7