Esempio n. 1
0
 /**
  * Modifies XML files from a backup before doing the restore.
  * @param string $path Path to backup root
  * @param int $courseid Target course id
  * @param array $sectionids ids for each section in subpage (given new number)
  * @param int $spsectionid Section id for this subpage (set to 0)
  * @param int $spid cm id for this subpage (used to hide it)
  */
 private function update_backup($path, $courseid, $sectionids, $spsectionid, $spid = null)
 {
     // List all files in backup so we can search through it later.
     $allfiles = self::list_files_recursive($path);
     // Create array of original id and new number.
     $sections = array($spsectionid => 0);
     $minnumber = null;
     foreach ($sectionids as $sectionid) {
         $minnumber = \mod_subpage::add_course_section($courseid, $minnumber);
         $sections[$sectionid] = $minnumber;
     }
     // Update section number (title element???) with empty value on target course.
     foreach (self::get_matching_files($path, $allfiles, '(moodle_backup.xml)') as $file) {
         $dom = new \DOMDocument();
         $dom->load($file);
         $xpath = new \DOMXpath($dom);
         foreach ($xpath->query('/contents/sections/section/sectionid') as $node) {
             if (in_array($node->nodeValue, $sectionids)) {
                 $titlenode = $node->parentNode->getElemetsByTagName('title')[0];
                 $titlenode->nodeValue = $sections[$node->nodeValue];
             }
         }
         $dom->save($file);
     }
     // Update section number in each section xml.
     foreach ($sections as $origid => $newnum) {
         foreach (self::get_matching_files($path, $allfiles, "(.*/section_{$origid}/section\\.xml)") as $file) {
             $dom = new \DOMDocument();
             $dom->load($file);
             $xpath = new \DOMXpath($dom);
             foreach ($xpath->query('/section/number') as $node) {
                 $node->nodeValue = $newnum;
             }
             $dom->save($file);
         }
     }
     // Hide subpage if id sent.
     if ($spid) {
         foreach (self::get_matching_files($path, $allfiles, "(.*/subpage_{$spid}/module\\.xml)") as $file) {
             $dom = new \DOMDocument();
             $dom->load($file);
             $xpath = new \DOMXpath($dom);
             foreach ($xpath->query('/module/visible') as $node) {
                 $node->nodeValue = 0;
             }
             $dom->save($file);
         }
     }
     // Ensure we don't copy groups and groupings.
     file_put_contents($path . '/groups.xml', '<groups></groups>');
     // If there are no quizzes included then don't copy the question bank.
     if (!self::get_matching_files($path, $allfiles, "activities/quiz_[0-9]+/module\\.xml")) {
         file_put_contents($path . '/questions.xml', '<question_categories></question_categories>');
     }
 }