]> git.mjollnir.org Git - moodle.git/commitdiff
Added data_decode_content_links_caller() and data_decode_content_links()
authorstronk7 <stronk7>
Mon, 17 Sep 2007 21:58:55 +0000 (21:58 +0000)
committerstronk7 <stronk7>
Mon, 17 Sep 2007 21:58:55 +0000 (21:58 +0000)
to provide interlinking capabilities to restore. MDL-9576

mod/data/restorelib.php

index fcbfe38161d5ddbcefd3b5b227384e8f3d994f05..3ac837bda754cca942988ae67798782013fe2bda 100644 (file)
@@ -1,4 +1,4 @@
-<?php
+<?php // $Id
 //This php script contains all the stuff to backup/restore data mod
 
     //This is the "graphical" structure of the data mod:
@@ -426,4 +426,284 @@ function data_comments_restore_mods ($oldid, $newid, $info, $rec_info) {
     return $status;
 
 }
+
+/**
+ * Returns a content decoded to support interactivities linking. Every module
+ * should have its own. They are called automatically from
+ * xxxx_decode_content_links_caller() function in each module
+ * in the restore process.
+ *
+ * @param string $content the content to be decoded
+ * @param object $restore the preferences used in restore
+ * @return string the decoded string
+ */
+function data_decode_content_links ($content,$restore) {
+
+    global $CFG;
+
+    $result = $content;
+
+/// Link to the list of datas
+
+    $searchstring='/\$@(DATAINDEX)\*([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 id)
+            $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
+        /// Personalize the searchstring
+            $searchstring='/\$@(DATAINDEX)\*('.$old_id.')@\$/';
+        /// If it is a link to this course, update the link to its new location
+            if($rec->new_id) {
+            /// Now replace it
+                $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/data/index.php?id='.$rec->new_id,$result);
+            } else {
+            /// It's a foreign link so leave it as original
+                $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/data/index.php?id='.$old_id,$result);
+            }
+        }
+    }
+
+/// Link to data view by moduleid
+
+    $searchstring='/\$@(DATAVIEWBYID)\*([0-9]+)@\$/';
+/// We look for it
+    preg_match_all($searchstring,$result,$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);
+        /// Personalize the searchstring
+            $searchstring='/\$@(DATAVIEWBYID)\*('.$old_id.')@\$/';
+        /// If it is a link to this course, update the link to its new location
+            if($rec->new_id) {
+            /// Now replace it
+                $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/data/view.php?id='.$rec->new_id,$result);
+            } else {
+            /// It's a foreign link so leave it as original
+                $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/data/view.php?id='.$old_id,$result);
+            }
+        }
+    }
+
+/// Link to data view by dataid
+
+    $searchstring='/\$@(DATAVIEWBYD)\*([0-9]+)@\$/';
+/// We look for it
+    preg_match_all($searchstring,$result,$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 (data id)
+            $rec = backup_getid($restore->backup_unique_code,"data",$old_id);
+        /// Personalize the searchstring
+            $searchstring='/\$@(DATAVIEWBYD)\*('.$old_id.')@\$/';
+        /// If it is a link to this course, update the link to its new location
+            if($rec->new_id) {
+            /// Now replace it
+                $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/data/view.php?d='.$rec->new_id,$result);
+            } else {
+            /// It's a foreign link so leave it as original
+                $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/data/view.php?d='.$old_id,$result);
+            }
+        }
+    }
+
+/// Link to data record (element)
+
+    $searchstring='/\$@(DATAVIEWRECORD)\*([0-9]+)\*([0-9]+)@\$/';
+/// We look for it
+    preg_match_all($searchstring,$result,$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 (data id and record id)
+            $rec = backup_getid($restore->backup_unique_code,"data",$old_id);
+            $rec2 = backup_getid($restore->backup_unique_code,"data_records",$old_id2);
+        /// Personalize the searchstring
+            $searchstring='/\$@(DATAVIEWRECORD)\*('.$old_id.')\*('.$old_id2.')@\$/';
+        /// If it is a link to this course, update the link to its new location
+            if($rec->new_id && $rec2->new_id) {
+            /// Now replace it
+                $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/data/view.php?d='.$rec->new_id.'&amp;rid='.$rec2->new_id,$result);
+            } else {
+            /// It's a foreign link so leave it as original
+                $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/data/view.php?d='.$old_id.'&amp;rid='.$old_id2,$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
+ *
+ * @param object $restore the preferences used in restore
+ * @return boolean status of the execution
+ */
+function data_decode_content_links_caller($restore) {
+
+    global $CFG;
+    $status = true;
+
+/// Process every DATA (intro) in the course
+    if ($datas = get_records_sql ("SELECT d.id, d.intro
+                                  FROM {$CFG->prefix}data d
+                                  WHERE d.course = $restore->course_id")) {
+    /// Iterate over each data->intro
+        $i = 0;   //Counter to send some output to the browser to avoid timeouts
+        foreach ($datas as $data) {
+        /// Increment counter
+            $i++;
+            $content = $data->intro;
+            $result = restore_decode_content_links_worker($content,$restore);
+            if ($result != $content) {
+            /// Update record
+                $data->intro = addslashes($result);
+                $status = update_record("data",$data);
+                if (debugging()) {
+                    if (!defined('RESTORE_SILENTLY')) {
+                        echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
+                    }
+                }
+            }
+        /// Do some output
+            if (($i+1) % 5 == 0) {
+                if (!defined('RESTORE_SILENTLY')) {
+                    echo ".";
+                    if (($i+1) % 100 == 0) {
+                        echo "<br />";
+                    }
+                }
+                backup_flush(300);
+            }
+        }
+    }
+
+/// Process every COMMENT (content) in the course
+    if ($comments = get_records_sql ("SELECT dc.id, dc.content
+                                      FROM {$CFG->prefix}data d,
+                                           {$CFG->prefix}data_records dr,
+                                           {$CFG->prefix}data_comments dc
+                                      WHERE d.course = $restore->course_id
+                                        AND dr.dataid = d.id
+                                        AND dc.recordid = dr.id")) {
+    /// Iterate over each data_comments->content
+        $i = 0;   //Counter to send some output to the browser to avoid timeouts
+        foreach ($comments as $comment) {
+        /// Increment counter
+            $i++;
+            $content = $comment->content;
+            $result = restore_decode_content_links_worker($content,$restore);
+            if ($result != $content) {
+            /// Update record
+                $comment->content = addslashes($result);
+                $status = update_record("data_comments",$comment);
+                if (debugging()) {
+                    if (!defined('RESTORE_SILENTLY')) {
+                        echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
+                    }
+                }
+            }
+        /// Do some output
+            if (($i+1) % 5 == 0) {
+                if (!defined('RESTORE_SILENTLY')) {
+                    echo ".";
+                    if (($i+1) % 100 == 0) {
+                        echo "<br />";
+                    }
+                }
+                backup_flush(300);
+            }
+        }
+    }
+
+/// Process every CONTENT (content, content1, content2, content3, content4) in the course
+    if ($contents = get_records_sql ("SELECT dc.id, dc.content, dc.content1, dc.content2, dc.content3, dc.content4
+                                      FROM {$CFG->prefix}data d,
+                                           {$CFG->prefix}data_records dr,
+                                           {$CFG->prefix}data_content dc
+                                      WHERE d.course = $restore->course_id
+                                        AND dr.dataid = d.id
+                                        AND dc.recordid = dr.id")) {
+    /// Iterate over each data_content->content, content1, content2, content3 and content4
+        $i = 0;   //Counter to send some output to the browser to avoid timeouts
+        foreach ($contents as $cnt) {
+        /// Increment counter
+            $i++;
+            $content = $cnt->content;
+            $content1 = $cnt->content1;
+            $content2 = $cnt->content2;
+            $content3 = $cnt->content3;
+            $content4 = $cnt->content4;
+            $result = restore_decode_content_links_worker($content,$restore);
+            $result1 = restore_decode_content_links_worker($content1,$restore);
+            $result2 = restore_decode_content_links_worker($content2,$restore);
+            $result3 = restore_decode_content_links_worker($content3,$restore);
+            $result4 = restore_decode_content_links_worker($content4,$restore);
+            if ($result != $content || $result1 != $content1 || $result2 != $content2 ||
+                $result3 != $content3 || $result4 != $content4) {
+            /// Unset fields to update only the necessary ones
+                unset($cnt->content);
+                unset($cnt->content1);
+                unset($cnt->content2);
+                unset($cnt->content3);
+                unset($cnt->content4);
+            /// Conditionally set the fields
+                if ($result != $content) {
+                    $cnt->content = addslashes($result);
+                }
+                if ($result1 != $content1) {
+                    $cnt->content1 = addslashes($result1);
+                }
+                if ($result2 != $content2) {
+                    $cnt->content2 = addslashes($result2);
+                }
+                if ($result3 != $content3) {
+                    $cnt->content3 = addslashes($result3);
+                }
+                if ($result4 != $content4) {
+                    $cnt->content4 = addslashes($result4);
+                }
+            /// Update record with the changed fields
+                $status = update_record("data_content",$cnt);
+                if (debugging()) {
+                    if (!defined('RESTORE_SILENTLY')) {
+                        echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
+                    }
+                }
+            }
+        /// Do some output
+            if (($i+1) % 5 == 0) {
+                if (!defined('RESTORE_SILENTLY')) {
+                    echo ".";
+                    if (($i+1) % 100 == 0) {
+                        echo "<br />";
+                    }
+                }
+                backup_flush(300);
+            }
+        }
+    }
+
+    return $status;
+}
+
 ?>