Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-20596 - fixes to backup_course_silently and import_backup_file_si…

…lently
  • Loading branch information...
commit 38e7721a31b131d6cadeaa94cbafa2e5aca5e8e8 1 parent f50ea04
Penelope Leach authored

Showing 2 changed files with 104 additions and 36 deletions. Show diff stats Hide diff stats

  1. +100 34 backup/lib.php
  2. +4 2 backup/restorelib.php
134 backup/lib.php
@@ -655,7 +655,6 @@ function backup_file2data ($file,&$data) {
655 655 */
656 656 function import_backup_file_silently($pathtofile,$destinationcourse,$emptyfirst=false,$userdata=false, $preferences=array()) {
657 657 global $CFG,$SESSION,$USER; // is there such a thing on cron? I guess so..
658   - global $restore; // ick
659 658
660 659 if (!defined('RESTORE_SILENTLY')) {
661 660 define('RESTORE_SILENTLY',true); // don't output all the stuff to us.
@@ -665,7 +664,8 @@ function import_backup_file_silently($pathtofile,$destinationcourse,$emptyfirst=
665 664 $cleanupafter = false;
666 665 $errorstr = ''; // passed by reference to restore_precheck to get errors from.
667 666
668   - if (!$course = get_record('course','id',$destinationcourse)) {
  667 + $course = null;
  668 + if ($destinationcourse && !$course = get_record('course','id',$destinationcourse)) {
669 669 mtrace($debuginfo.'Course with id $destinationcourse was not a valid course!');
670 670 return false;
671 671 }
@@ -720,29 +720,65 @@ function import_backup_file_silently($pathtofile,$destinationcourse,$emptyfirst=
720 720 return false;
721 721 }
722 722
723   - $SESSION->restore = new StdClass;
  723 + global $restore; // ick
  724 + $restore = new StdClass;
  725 + // copy back over the stuff that gets set in restore_precheck
  726 + $restore->course_header = $SESSION->course_header;
  727 + $restore->info = $SESSION->info;
  728 +
  729 + $xmlfile = "$CFG->dataroot/temp/backup/$backup_unique_code/moodle.xml";
  730 + $info = restore_read_xml_roles($xmlfile);
  731 + $restore->rolesmapping = array();
  732 + if (isset($info->roles) && is_array($info->roles)) {
  733 + foreach ($info->roles as $id => $info) {
  734 + if ($newroleid = get_field('role', 'id', 'shortname', $info->shortname)) {
  735 + $restore->rolesmapping[$id] = $newroleid;
  736 + }
  737 + }
  738 + }
724 739
725 740 // add on some extra stuff we need...
726   - $SESSION->restore->metacourse = $restore->metacourse = (isset($preferences['restore_metacourse']) ? $preferences['restore_metacourse'] : 0);
727   - $SESSION->restore->restoreto = $restore->restoreto = RESTORETO_CURRENT_ADDING;
728   - $SESSION->restore->users = $restore->users = $userdata;
729   - $SESSION->restore->groups = $restore->groups = (isset($preferences['restore_groups']) ? $preferences['restore_groups'] : RESTORE_GROUPS_NONE);
730   - $SESSION->restore->logs = $restore->logs = (isset($preferences['restore_logs']) ? $preferences['restore_logs'] : 0);
731   - $SESSION->restore->user_files = $restore->user_files = $userdata;
732   - $SESSION->restore->messages = $restore->messages = (isset($preferences['restore_messages']) ? $preferences['restore_messages'] : 0);
733   - $SESSION->restore->blogs = $restore->blogs = (isset($preferences['restore_blogs']) ? $preferences['restore_blogs'] : 0);
734   - $SESSION->restore->course_id = $restore->course_id = $destinationcourse;
735   - $SESSION->restore->deleting = $emptyfirst;
736   - $SESSION->restore->restore_course_files = $restore->course_files = (isset($preferences['restore_course_files']) ? $preferences['restore_course_files'] : 0);
737   - $SESSION->restore->restore_site_files = $restore->site_files = (isset($preferences['restore_site_files']) ? $preferences['restore_site_files'] : 0);
738   - $SESSION->restore->backup_version = $SESSION->info->backup_backup_version;
739   - $SESSION->restore->course_startdateoffset = $course->startdate - $SESSION->course_header->course_startdate;
740   -
741   - restore_setup_for_check($SESSION->restore,$backup_unique_code);
742   -
743   - // maybe we need users (defaults to 2 in restore_setup_for_check)
  741 + $restore->metacourse = (isset($preferences['restore_metacourse']) ? $preferences['restore_metacourse'] : 0);
  742 + $restore->course_id = $destinationcourse;
  743 + if ($destinationcourse) {
  744 + $restore->restoreto = RESTORETO_CURRENT_ADDING;
  745 + $restore->course_startdateoffset = $course->startdate - $restore->course_header->course_startdate;
  746 + } else {
  747 + $restore->restoreto = RESTORETO_NEW_COURSE;
  748 + $restore->restore_restorecatto = 0; // let this be handled by the headers
  749 + $restore->course_startdateoffset = 0;
  750 +
  751 + }
  752 +
  753 + $restore->users = $userdata;
  754 + $restore->user_files = $userdata;
  755 + $restore->deleting = $emptyfirst;
  756 +
  757 + $restore->groups = (isset($preferences['restore_groups']) ? $preferences['restore_groups'] : RESTORE_GROUPS_NONE);
  758 + $restore->logs = (isset($preferences['restore_logs']) ? $preferences['restore_logs'] : 0);
  759 + $restore->messages = (isset($preferences['restore_messages']) ? $preferences['restore_messages'] : 0);
  760 + $restore->blogs = (isset($preferences['restore_blogs']) ? $preferences['restore_blogs'] : 0);
  761 + $restore->course_files = (isset($preferences['restore_course_files']) ? $preferences['restore_course_files'] : 0);
  762 + $restore->site_files = (isset($preferences['restore_site_files']) ? $preferences['restore_site_files'] : 0);
  763 +
  764 + $restore->backup_version = $restore->info->backup_backup_version;
  765 + $restore->original_wwwroot = $restore->info->original_wwwroot;
  766 +
  767 + // now copy what we have over to the session
  768 + // this needs to happen before restore_setup_for_check
  769 + // which for some reason reads the session
  770 + $SESSION->restore =& $restore;
  771 + // rename the things that are called differently
  772 + $SESSION->restore->restore_course_files = $restore->course_files;
  773 + $SESSION->restore->restore_site_files = $restore->site_files;
  774 + $SESSION->restore->backup_version = $restore->info->backup_backup_version;
  775 +
  776 + restore_setup_for_check($restore, $backup_unique_code);
  777 +
  778 + // maybe we need users (defaults to 2 (none) in restore_setup_for_check)
  779 + // so set this again here
744 780 if (!empty($userdata)) {
745   - $SESSION->restore->users = 1;
  781 + $restore->users = 1;
746 782 }
747 783
748 784 // we also need modules...
@@ -750,22 +786,28 @@ function import_backup_file_silently($pathtofile,$destinationcourse,$emptyfirst=
750 786 foreach ($allmods as $mod) {
751 787 $modname = $mod->name;
752 788 //Now check that we have that module info in the backup file
753   - if (isset($SESSION->info->mods[$modname]) && $SESSION->info->mods[$modname]->backup == "true") {
754   - $SESSION->restore->mods[$modname]->restore = true;
755   - $SESSION->restore->mods[$modname]->userinfo = $userdata;
  789 + if (isset($restore->info->mods[$modname]) && $restore->info->mods[$modname]->backup == "true") {
  790 + $restore->mods[$modname]->restore = true;
  791 + $restore->mods[$modname]->userinfo = $userdata;
756 792 }
757 793 else {
758 794 // avoid warnings
759   - $SESSION->restore->mods[$modname]->restore = false;
760   - $SESSION->restore->mods[$modname]->userinfo = false;
  795 + $restore->mods[$modname]->restore = false;
  796 + $restore->mods[$modname]->userinfo = false;
761 797 }
762 798 }
763 799 }
764   - $restore = clone($SESSION->restore);
765   - if (!restore_execute($SESSION->restore,$SESSION->info,$SESSION->course_header,$errorstr)) {
  800 + if (!$status = restore_execute($restore,$restore->info,$restore->course_header,$errorstr)) {
766 801 mtrace($debuginfo.'Failed restore_execute (error was '.$errorstr.')');
767 802 return false;
768 803 }
  804 + // now get out the new courseid and return that
  805 + if ($restore->restoreto = RESTORETO_NEW_COURSE) {
  806 + if (!empty($SESSION->restore->course_id)) {
  807 + return $SESSION->restore->course_id;
  808 + }
  809 + return false;
  810 + }
769 811 return true;
770 812 }
771 813
@@ -785,6 +827,7 @@ function backup_course_silently($courseid, $prefs, &$errorstring) {
785 827 return false;
786 828 }
787 829 $preferences = backup_generate_preferences_artificially($course, $prefs);
  830 + $preferences->destination = array_key_exists('destination', $prefs) ? $prefs['destination'] : 0;
788 831 if (backup_execute($preferences, $errorstring)) {
789 832 return $CFG->dataroot . '/' . $course->id . '/backupdata/' . $preferences->backup_name;
790 833 }
@@ -806,6 +849,7 @@ function backup_course_silently($courseid, $prefs, &$errorstring) {
806 849 backup_course_files
807 850 backup_site_files
808 851 backup_messages
  852 + userdata
809 853 * and if not provided, they will not be included.
810 854 */
811 855
@@ -815,6 +859,7 @@ function backup_generate_preferences_artificially($course, $prefs) {
815 859 $preferences->backup_unique_code = time();
816 860 $preferences->backup_users = (isset($prefs['backup_users']) ? $prefs['backup_users'] : 0);
817 861 $preferences->backup_name = backup_get_zipfile_name($course, $preferences->backup_unique_code);
  862 + $preferences->mods = array();
818 863 $count = 0;
819 864
820 865 if ($allmods = get_records("modules") ) {
@@ -831,7 +876,6 @@ function backup_generate_preferences_artificially($course, $prefs) {
831 876 if (!function_exists($modbackup) || !function_exists($modcheckbackup)) {
832 877 continue;
833 878 }
834   - $modcheckbackup($course->id, $preferences->backup_users, $preferences->backup_unique_code);
835 879 $var = "exists_".$modname;
836 880 $preferences->$var = true;
837 881 $count++;
@@ -849,8 +893,8 @@ function backup_generate_preferences_artificially($course, $prefs) {
849 893 $preferences->$var = true;
850 894 $preferences->mods[$modname]->instances[$instance->id]->backup = true;
851 895 $var = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
852   - $preferences->$var = true;
853   - $preferences->mods[$modname]->instances[$instance->id]->userinfo = true;
  896 + $preferences->$var = (!array_key_exists('userdata', $prefs) || $prefs['userdata']);
  897 + $preferences->mods[$modname]->instances[$instance->id]->userinfo = $preferences->$var;
854 898 $var = 'backup_'.$modname.'_instances';
855 899 $preferences->$var = 1; // we need this later to determine what to display in modcheckbackup.
856 900 }
@@ -865,9 +909,31 @@ function backup_generate_preferences_artificially($course, $prefs) {
865 909
866 910 //Check include user info
867 911 $var = "backup_user_info_".$modname;
868   - $preferences->$var = true;
869   - $preferences->mods[$modname]->userinfo = true;
  912 + $preferences->$var = (!array_key_exists('userdata', $prefs) || $prefs['userdata']);
  913 + $preferences->mods[$modname]->userinfo = $preferences->$var;
870 914
  915 + //Call the check function to show more info
  916 + $modcheckbackup = $modname."_check_backup_mods";
  917 + $var = $modname.'_instances';
  918 + $instancestopass = array();
  919 + if (!empty($preferences->$var) && is_array($preferences->$var) && count($preferences->$var)) {
  920 + $table->data = array();
  921 + $countinstances = 0;
  922 + foreach ($preferences->$var as $instance) {
  923 + $var1 = 'backup_'.$modname.'_instance_'.$instance->id;
  924 + $var2 = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
  925 + if (!empty($preferences->$var1)) {
  926 + $obj = new StdClass;
  927 + $obj->name = $instance->name;
  928 + $obj->userdata = $preferences->$var2;
  929 + $obj->id = $instance->id;
  930 + $instancestopass[$instance->id]= $obj;
  931 + $countinstances++;
  932 +
  933 + }
  934 + }
  935 + }
  936 + $modcheckbackup($course->id,$preferences->$var,$preferences->backup_unique_code,$instancestopass);
871 937 }
872 938 }
873 939
6 backup/restorelib.php
@@ -687,7 +687,7 @@ function restore_print_course_header ($course_header) {
687 687 //When finished, course_header contains the id of the new course
688 688 function restore_create_new_course($restore,&$course_header) {
689 689
690   - global $CFG;
  690 + global $CFG, $SESSION;
691 691
692 692 $status = true;
693 693
@@ -854,6 +854,8 @@ function restore_create_new_course($restore,&$course_header) {
854 854 backup_putid ($restore->backup_unique_code,"course",$course_header->course_id,$newid);
855 855 //Replace old course_id in course_header
856 856 $course_header->course_id = $newid;
  857 + $SESSION->restore->course_id = $newid;
  858 + return $newid;
857 859 } else {
858 860 $status = false;
859 861 }
@@ -8453,7 +8455,7 @@ function restore_execute(&$restore,$info,$course_header,&$errorstr) {
8453 8455 }
8454 8456
8455 8457 //Now create log entries as needed
8456   - if ($status and ($restore->logs)) {
  8458 + if ($status and ($info->backup_logs == 'true' && $restore->logs)) {
8457 8459 if (!defined('RESTORE_SILENTLY')) {
8458 8460 echo "<li>".get_string("creatinglogentries");
8459 8461 }

0 comments on commit 38e7721

Please sign in to comment.
Something went wrong with that request. Please try again.