]> git.mjollnir.org Git - moodle.git/commitdiff
First version of inter-activities mantained in backup/restore.
authorstronk7 <stronk7>
Sat, 24 Apr 2004 00:40:45 +0000 (00:40 +0000)
committerstronk7 <stronk7>
Sat, 24 Apr 2004 00:40:45 +0000 (00:40 +0000)
All the conversion to some pseudo-codes are done at backup time.
In the restore process, every link is decoded to new ids.
For now, this is supported:
- In backup:
  -all links to forums are encoded.
- In restore:
  - all links to forums are decoded.
      - from forums (post->message).
      - from resources (resource->alltext).
The process requires a backup made with the same version (because it
has to be properly encoded at backup time). Old backups will work
without conversions at all.
Only tested with some little courses.
It would be perfect to have some "big" course to test it.
Please, test and feedback, ciao :-)

backup/STATUS.txt
backup/backuplib.php
backup/restore_execute.html
backup/restorelib.php
backup/version.php
mod/forum/backuplib.php
mod/forum/restorelib.php

index d0110d9a1ff952c87fb27b9ba9c1704f4a102499..6af982b3ce0f8d4d7eb08f1da47fd73400ed9a9a 100644 (file)
@@ -126,7 +126,9 @@ Backup Details:
       - Modify the backup frontend..............................NOT EXISTS
       - Modify the backup of course_sections and course_modules.NOT EXISTS
       - Modify every module backuplib to work properly..........NOT EXISTS
-
+   - Encode every possible internal link........................IN PROGRESS
+      - Links to forums.........................................DONE
+      - Links to resources......................................NOT EXISTS
 Restore Details:
 ===============
    - Delete old entries at restore start........................DONE
@@ -243,6 +245,11 @@ Restore Details:
       - From database...........................................DONE
       - From filesystem.........................................DONE
    - Upload backup files........................................DONE
+   - Decode every possible internal link........................IN PROGRESS
+      - Links to forums.........................................IN PROGRESS
+         - From forums (post->message)..........................DONE
+         - From resources (resource->alltext)...................DONE
+      - Links to resources......................................NOT EXISTS
 
 ========== ========== ========== =========== =========== =========== ===
 
index eccc3513f215f13e2da5458fe2a1e3247bce9b06..6f42d620c03405c7ae36d21a83dff7b16eec5b87 100644 (file)
     //It does this conversions:
     //    - $CFG->wwwroot -----------------------------> $@WWWROOT@$
     //    - /file.php/$courseid -----------------------> /file.php/$@COURSEID@$
+    //    - Links to forums everywhere (DB) are encoded.
     //
     function backup_encode_absolute_links($content) {
 
         global $CFG,$preferences;
 
+        //Now we encode calls to file.php or wwwroot
         $search = array ($CFG->wwwroot,
                          "/file.php/".$preferences->backup_course);
 
 
         $result = str_replace($search,$replace,$content);
 
-        //if ($result != $content) {                                                   //Debug
-        //    echo "\n<hr>".$content." \nchanged to \n".$result."<hr>\n";              //Debug
-        //}                                                                            //Debug
+        foreach ($preferences->mods as $name => $info) {
+            //Check if the xxxx_encode_content_links exists
+            $function_name = $name."_encode_content_links";
+            if (function_exists($function_name)) {
+                $result = $function_name($result,$preferences);
+            }
+        }
+
+        if ($result != $content && $CFG->debug>7) {                                  //Debug
+            echo "<br><hr>".$content."<br>changed to<br>".$result."<hr><br>";        //Debug
+        }                                                                            //Debug
 
         return $result;
     }
index a0306e79e677e4dd704d883ae444f2df351088bc..eb25617aaf5f18a5d8f142ed5750ec1fc1ae4424 100644 (file)
         }
     }
 
+    //Now, if all is OK, adjust inter-activity links
+    if ($status) {
+        echo "<li>".get_string("decodinginternallinks");
+        if (!$status = restore_decode_content_links($restore)) {
+            notify("Could not refresh events for activities!");
+        }
+    }
+
     //Now if all is OK, update:
     //   - course modinfo field 
     //   - categories table
index 54f51e587ec1ecce3f666bd8bed67423667624c9..6893af36bdb11c2b8316c3771917219e23a15216 100644 (file)
         return $status;
     }
 
+    //This function makes all the necessary calls to xxxx_decode_content_links_caller()
+    //function in each module, passing them the desired contents to be decoded
+    //from backup format to destination site/course in order to mantain inter-activities
+    //working in the backup/restore process
+    function restore_decode_content_links($restore) {
+
+        global $CFG;
+
+        $status = true;
+
+        echo "<ul>";
+        foreach ($restore->mods as $name => $info) {
+            //If the module is being restored
+            if ($info->restore == 1) {
+                //Check if the xxxx_decode_content_links_caller exists
+                $function_name = $name."_decode_content_links_caller";
+                if (function_exists($function_name)) {
+                    echo "<li>".get_string("modulenameplural",$name);
+                    $status = $function_name($restore);
+                }
+            }
+        }
+        echo "</ul>";
+
+        return $status;
+    }
+
     //This function read the xml file and store it data from the info zone in an object
     function restore_read_xml_info ($xml_file) {
 
     //It does this conversions:
     //    - $@WWWROOT@$ -------------------------------> $CFG->wwwroot
     //    - $@COURSEID@$ ------------------------------> $courseid
-    //                              
+    //
+    //Note: Inter-activities linking is being implemented as a final
+    //step in the restore execution, because we need to have it 
+    //finished to know all the oldid, newid equivaleces
     function restore_decode_absolute_links($content) {
                                      
         global $CFG,$restore;    
-        
+
+        //Now decode wwwroot and file.php calls
         $search = array ("$@WWWROOT@$",
                          "$@COURSEID@$");
         
     
         $result = str_replace($search,$replace,$content);
 
-        //if ($result != $content) {                                                   //Debug
-        //    echo "\n<hr>".$content." \nchanged to \n".$result."<hr>\n";              //Debug
-        //}                                                                            //Debug
+        if ($result != $content && $CFG->debug>7) {                                  //Debug
+            echo "<br><hr>".$content."<br>changed to<br>".$result."<hr><br>";        //Debug
+        }                                                                            //Debug
 
         return $result;
     }
index 9a207b5a820f407dfcdca4d92ecbce7436b5f778..10ff8b23e2962b3afe0851507fb84584e5efae1e 100644 (file)
@@ -5,6 +5,6 @@
 // database (backup_version) to determine whether upgrades should
 // be performed (see db/backup_*.php)
 
-$backup_version = 2004042100;   // The current version is a date (YYYYMMDDXX)
+$backup_version = 2004042400;   // The current version is a date (YYYYMMDDXX)
 
 $backup_release = "1.3 development";  // User-friendly version number
index 31864258763c43609af2425276479debe00c9cdc..44a79ec757516a4cafc7c962214f46a706375db9 100644 (file)
         return $info;
     }
 
+    //Return a content encoded to support interactivities linking. Every module
+    //should have its own. They are called automatically from the backup procedure.
+    function forum_encode_content_links ($content,$preferences) {
 
+        $base = '\$@WWWROOT@\$';
+        //Link to the list of forums
+        $buscar="/(".$base."\/mod\/forum\/index.php\?id\=)([0-9]+)/";
+        $result= preg_replace($buscar,'$@FORUMINDEX*$2@$',$content);
 
+        //Link to forum view by moduleid
+        $buscar="/(".$base."\/mod\/forum\/view.php\?id\=)([0-9]+)/";
+        $result= preg_replace($buscar,'$@FORUMVIEWBYID*$2@$',$result);
 
+        //Link to forum view by forumid
+        $buscar="/(".$base."\/mod\/forum\/view.php\?f\=)([0-9]+)/";
+        $result= preg_replace($buscar,'$@FORUMVIEWBYF*$2@$',$result);
 
+        //Link to forum discussion with parent syntax
+        $buscar="/(".$base."\/mod\/forum\/discuss.php\?d\=)([0-9]+)\&parent\=([0-9]+)/";
+        $result= preg_replace($buscar,'$@FORUMDISCUSSIONVIEWPARENT*$2*$3@$',$result);
+
+        //Link to forum discussion with relative syntax
+        $buscar="/(".$base."\/mod\/forum\/discuss.php\?d\=)([0-9]+)\#([0-9]+)/";
+        $result= preg_replace($buscar,'$@FORUMDISCUSSIONVIEWINSIDE*$2*$3@$',$result);
+
+        //Link to forum discussion by discussionid
+        $buscar="/(".$base."\/mod\/forum\/discuss.php\?d\=)([0-9]+)/";
+        $result= preg_replace($buscar,'$@FORUMDISCUSSIONVIEW*$2@$',$result);
+
+        return $result;
+    }
 
     // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
 
index e1c9086c1bff4c5bdbbd2844be9b7ff8649621d4..e530408f5d3a36c5c426b5a1ea2fe63f50758526 100644 (file)
         }
         return $status;
     }
+
+    //Return a content decoded to support interactivities linking. Every module
+    //should have its own. They are called automatically from 
+    //forum_decode_content_links_caller() function in each module
+    //in the restore process
+    function forum_decode_content_links ($content,$restore) {
+
+        global $CFG;
+
+        $result = $content;
+
+        //Link to the list of forums
+
+        $searchstring='/\$@(FORUMINDEX)\*([0-9]+)@\$/';
+        //We look for it
+        preg_match_all($searchstring,$content,$foundset);
+        //If found, then we are going to look for its new id (in backup tables)
+        if ($foundset[0]) {
+            //print_object($foundset);                                     //Debug
+            //We get the needed variables here
+            $courseid = $restore->course_id;
+            //Now replace it
+            $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/index.php?id='.$courseid,$result);
+        }
+
+        //Link to forum view by moduleid
+
+        $searchstring='/\$@(FORUMVIEWBYID)\*([0-9]+)@\$/';
+        //We look for it
+        preg_match_all($searchstring,$content,$foundset);
+        //If found, then we are going to look for its new id (in backup tables)
+        if ($foundset[0]) {
+            //print_object($foundset);                                     //Debug
+            //Iterate over foundset[2]. They are the old_ids
+            foreach($foundset[2] as $old_id) {
+                //We get the needed variables here (course_modules id)
+                $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
+                if($rec->new_id) {
+                    //Personalize the searchstring
+                    $searchstring='/\$@(FORUMVIEWBYID)\*('.$old_id.')@\$/';
+                    //Now replace it
+                    $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/view.php?id='.$rec->new_id,$result);
+                }
+            }
+        }
+
+        //Link to forum view by forumid
+
+        $searchstring='/\$@(FORUMVIEWBYF)\*([0-9]+)@\$/';
+        //We look for it
+        preg_match_all($searchstring,$content,$foundset);
+        //If found, then we are going to look for its new id (in backup tables)
+        if ($foundset[0]) {
+            //print_object($foundset);                                     //Debug
+            //Iterate over foundset[2]. They are the old_ids
+            foreach($foundset[2] as $old_id) {
+                //We get the needed variables here (forum id)
+                $rec = backup_getid($restore->backup_unique_code,"forum",$old_id);
+                if($rec->new_id) {
+                    //Personalize the searchstring
+                    $searchstring='/\$@(FORUMVIEWBYF)\*('.$old_id.')@\$/';
+                    //Now replace it
+                    $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/view.php?f='.$rec->new_id,$result);
+                }
+            }
+        }
+
+        //Link to forum discussion by discussionid
+
+        $searchstring='/\$@(FORUMDISCUSSIONVIEW)\*([0-9]+)@\$/';
+        //We look for it
+        preg_match_all($searchstring,$content,$foundset);
+        //If found, then we are going to look for its new id (in backup tables)
+        if ($foundset[0]) {
+            //print_object($foundset);                                     //Debug
+            //Iterate over foundset[2]. They are the old_ids
+            foreach($foundset[2] as $old_id) {
+                //We get the needed variables here (discussion id)
+                $rec = backup_getid($restore->backup_unique_code,"forum_discussions",$old_id);
+                if($rec->new_id) {
+                    //Personalize the searchstring
+                    $searchstring='/\$@(FORUMDISCUSSIONVIEW)\*('.$old_id.')@\$/';
+                    //Now replace it
+                    $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/discuss.php?d='.$rec->new_id,$result);
+                }
+            }
+        }
+
+        //Link to forum discussion with parent syntax
+
+        $searchstring='/\$@(FORUMDISCUSSIONVIEWPARENT)\*([0-9]+)\*([0-9]+)@\$/';
+        //We look for it
+        preg_match_all($searchstring,$content,$foundset);
+        //If found, then we are going to look for its new id (in backup tables)
+        if ($foundset[0]) {
+            //print_object($foundset);                                     //Debug
+            //Iterate over foundset[2] and foundset[3]. They are the old_ids
+            foreach($foundset[2] as $key => $old_id) {
+                $old_id2 = $foundset[3][$key];
+                //We get the needed variables here (discussion id and post id)
+                $rec = backup_getid($restore->backup_unique_code,"forum_discussions",$old_id);
+                $rec2 = backup_getid($restore->backup_unique_code,"forum_posts",$old_id2);
+                if($rec->new_id && $rec2->new_id) {
+                    //Personalize the searchstring
+                    $searchstring='/\$@(FORUMDISCUSSIONVIEWPARENT)\*('.$old_id.')\*('.$old_id2.')@\$/';
+                    //Now replace it
+                    $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/discuss.php?d='.$rec->new_id.'&parent='.$rec2->new_id,$result);
+                }
+            }
+        }
+
+        //Link to forum discussion with relative syntax
+
+        $searchstring='/\$@(FORUMDISCUSSIONVIEWINSIDE)\*([0-9]+)\*([0-9]+)@\$/';
+        //We look for it
+        preg_match_all($searchstring,$content,$foundset);
+        //If found, then we are going to look for its new id (in backup tables)
+        if ($foundset[0]) {
+            //print_object($foundset);                                     //Debug
+            //Iterate over foundset[2] and foundset[3]. They are the old_ids
+            foreach($foundset[2] as $key => $old_id) {
+                $old_id2 = $foundset[3][$key];
+                //We get the needed variables here (discussion id and post id)
+                $rec = backup_getid($restore->backup_unique_code,"forum_discussions",$old_id);
+                $rec2 = backup_getid($restore->backup_unique_code,"forum_posts",$old_id2);
+                if($rec->new_id && $rec2->new_id) {
+                    //Personalize the searchstring
+                    $searchstring='/\$@(FORUMDISCUSSIONVIEWINSIDE)\*('.$old_id.')\*('.$old_id2.')@\$/';
+                    //Now replace it
+                    $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/discuss.php?d='.$rec->new_id.'#'.$rec2->new_id,$result);
+                }
+            }
+        }
+
+        return $result;
+    }
+
+    //This function makes all the necessary calls to xxxx_decode_content_links()
+    //function in each module, passing them the desired contents to be decoded
+    //from backup format to destination site/course in order to mantain inter-activities
+    //working in the backup/restore process. It's called from restore_decode_content_links()
+    //function in restore process
+    function forum_decode_content_links_caller($restore) {
+
+        global $CFG;
+
+        $status = true;
+
+        echo "<ul>";
+  
+        //FORUM: Decode every POST (message) in the coure
+        
+        //Check we are restoring forums
+        if ($restore->mods['forum']->restore == 1) {
+            echo "<li>".get_string("from")." ".get_string("modulenameplural","forum");
+            //Get all course posts
+            if ($posts = get_records_sql ("SELECT p.id, p.message
+                                       FROM {$CFG->prefix}forum_posts p,
+                                            {$CFG->prefix}forum_discussions d
+                                       WHERE d.course = $restore->course_id AND
+                                             p.discussion = d.id")) {
+                //Iterate over each post->message
+                $i = 0;   //Counter to send some output to the browser to avoid timeouts
+                foreach ($posts as $post) {
+                    //Increment counter
+                    $i++; 
+                    $content = $post->message;
+                    $result = forum_decode_content_links($content,$restore);
+                    if ($result != $content) {
+                        //Update record
+                        $post->message = $result;
+                        $status = update_record("forum_posts",$post);
+                        if ($CFG->debug>7) {
+                            echo "<br><hr>".$content."<br>changed to</br>".$result."<hr><br>";
+                        }
+                    }
+                    //Do some output
+                    if (($i+1) % 5 == 0) {
+                        echo ".";
+                        if (($i+1) % 100 == 0) {
+                            echo "<br>";
+                        }
+                        backup_flush(300);
+                    }
+                }
+            }
+        }
+
+        //RESOURCE: Decode every RESOURCE (alltext) in the coure
+
+        //Check we are restoring resources
+        if ($restore->mods['resource']->restore == 1) {
+            echo "<li>".get_string("from")." ".get_string("modulenameplural","resource");
+            //Get all course resources
+            if ($resources = get_records_sql ("SELECT r.id, r.alltext
+                                       FROM {$CFG->prefix}resource r
+                                       WHERE r.course = $restore->course_id")) {
+                //Iterate over each resource->alltext
+                $i = 0;   //Counter to send some output to the browser to avoid timeouts
+                foreach ($resources as $resource) {
+                    //Increment counter
+                    $i++;
+                    $content = $resource->alltext;
+                    $result = forum_decode_content_links($content,$restore);
+                    if ($result != $content) {
+                        //Update record
+                        $resource->alltext = $result;
+                        $status = update_record("resource",$resource);
+                        if ($CFG->debug>7) {
+                            echo "<br><hr>".$content."<br>changed to</br>".$result."<hr><br>";
+                        }
+                    }
+                    //Do some output
+                    if (($i+1) % 5 == 0) {
+                        echo ".";
+                        if (($i+1) % 100 == 0) {
+                            echo "<br>";
+                        }
+                        backup_flush(300);
+                    }
+                }
+            }
+        }
+
+        echo "</ul>";
+        return $status;
+    }
 ?>