dokeos-backup
[ class tree: dokeos-backup ] [ index: dokeos-backup ] [ all elements ]

Source for file CourseRestorer.class.php

Documentation is available at CourseRestorer.class.php

  1. <?php // $Id: CourseRestorer.class.php 15301 2008-05-16 03:21:42Z yannoo $
  2. /*
  3. ==============================================================================
  4.     Dokeos - elearning and course management software
  5.  
  6.     Copyright (c) 2008 Dokeos SPRL
  7.     Copyright (c) 2003 Ghent University (UGent)
  8.     Copyright (c) 2001 Universite catholique de Louvain (UCL)
  9.     Copyright (c) Bart Mollet (bart.mollet@hogent.be)
  10.  
  11.     For a full list of contributors, see "credits.txt".
  12.     The full license can be read in "license.txt".
  13.  
  14.     This program is free software; you can redistribute it and/or
  15.     modify it under the terms of the GNU General Public License
  16.     as published by the Free Software Foundation; either version 2
  17.     of the License, or (at your option) any later version.
  18.  
  19.     See the GNU General Public License for more details.
  20.  
  21.     Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  22.     Mail: info@dokeos.com
  23. ==============================================================================
  24. */
  25. require_once ('Course.class.php');
  26. require_once ('Event.class.php');
  27. require_once ('Link.class.php');
  28. require_once ('ToolIntro.class.php');
  29. require_once ('LinkCategory.class.php');
  30. require_once ('ForumCategory.class.php');
  31. require_once ('Forum.class.php');
  32. require_once ('ForumTopic.class.php');
  33. require_once ('ForumPost.class.php');
  34. require_once ('CourseDescription.class.php');
  35. require_once ('Learnpath.class.php');
  36. require_once ('Survey.class.php');
  37. require_once ('SurveyQuestion.class.php');
  38. require_once ('mkdirr.php');
  39. require_once ('rmdirr.php');
  40. define('FILE_SKIP'1);
  41. define('FILE_RENAME'2);
  42. define('FILE_OVERWRITE'3);
  43. /**
  44.  * Class to restore items from a course object to a Dokeos-course
  45.  * @author Bart Mollet <bart.mollet@hogent.be>
  46.  * @package dokeos.backup
  47.  */
  48. {
  49.     /**
  50.      * The course-object
  51.      */
  52.     var $course;
  53.     /**
  54.      * What to do with files with same name (FILE_SKIP, FILE_RENAME or
  55.      * FILE_OVERWRITE)
  56.      */
  57.     var $file_option;
  58.     /**
  59.      * Create a new CourseRestorer
  60.      */
  61.     function CourseRestorer($course)
  62.     {
  63.         $this->course = $course;
  64.         $this->file_option = FILE_RENAME;
  65.     }
  66.     /**
  67.      * Set the file-option
  68.      * @param constant $options What to do with files with same name (FILE_SKIP,
  69.      *  FILE_RENAME or FILE_OVERWRITE)
  70.      */
  71.     function set_file_option($option)
  72.     {
  73.         $this->file_option = $option;
  74.     }
  75.     /**
  76.      * Restore a course.
  77.      * @param string $destination_course_code The code of the Dokeos-course in
  78.      *  which the resources should be stored. Default: Current Dokeos-course.
  79.      */
  80.     function restore($destination_course_code '')
  81.     {
  82.         if ($destination_course_code == '')
  83.         {
  84.             $course_info api_get_course_info();
  85.             $this->course->destination_db $course_info['dbName'];
  86.             $this->course->destination_path $course_info['path'];
  87.         }
  88.         else
  89.         {
  90.             $course_info Database :: get_course_info($destination_course_code);
  91.             $this->course->destination_db $course_info['database'];
  92.             $this->course->destination_path $course_info['directory'];
  93.         }
  94.         $this->restore_links();
  95.         $this->restore_tool_intro();
  96.         $this->restore_events();
  97.         $this->restore_announcements();
  98.         $this->restore_documents();
  99.         $this->restore_scorm_documents();
  100.         $this->restore_course_descriptions();
  101.         //$this->restore_forums();
  102.         $this->restore_quizzes()// after restore_documents! (for correct import of sound/video)
  103.         $this->restore_learnpaths();
  104.         $this->restore_surveys();
  105.         // Restore the item properties
  106.         $table Database :: get_course_table(TABLE_ITEM_PROPERTY$this->course->destination_db);
  107.         foreach ($this->course->resources as $type => $resources)
  108.         {
  109.             foreach ($resources as $id => $resource)
  110.             {
  111.                 foreach ($resource->item_properties as $property)
  112.                 {
  113.                     // First check if there isn't allready a record for this resource
  114.                     $sql "SELECT * FROM $table WHERE tool = '".$property['tool']."' AND ref = '".$resource->destination_id."'";
  115.                     $res api_sql_query($sql,__FILE__,__LINE__);
  116.                     ifDatabase::num_rows($res== 0)
  117.                     {
  118.                         // The to_group_id and to_user_id are set to default values as users/groups possibly not exist in the target course
  119.                         $sql "INSERT INTO $table SET
  120.                                                 tool = '".$property['tool']."',
  121.                                                 insert_user_id = '".$property['insert_user_id']."',
  122.                                                 insert_date = '".$property['insert_date']."',
  123.                                                 lastedit_date = '".$property['lastedit_date']."',
  124.                                                 ref = '".$resource->destination_id."',
  125.                                                 lastedit_type = '".$property['lastedit_type']."',
  126.                                                 lastedit_user_id = '".$property['lastedit_user_id']."',
  127.                                                 visibility = '".$property['visibility']."',
  128.                                                 start_visible = '".$property['start_visible']."',
  129.                                                 end_visible = '".$property['end_visible']."',
  130.                                                 to_user_id  = '".$property['to_user_id']."',
  131.                                                 to_group_id = '0'";
  132.                                                 ;
  133.                         api_sql_query($sql__FILE____LINE__);
  134.                     }
  135.                 }
  136.             }
  137.         }
  138.         // Restore the linked-resources
  139.         $table Database :: get_course_table(TABLE_LINKED_RESOURCES$this->course->destination_db);
  140.         foreach ($this->course->resources as $type => $resources)
  141.         {
  142.             foreach ($resources as $id => $resource)
  143.             {
  144.                 $linked_resources $resource->get_linked_resources();
  145.                 foreach ($linked_resources as $to_type => $to_ids)
  146.                 {
  147.                     foreach ($to_ids as $index => $to_id)
  148.                     {
  149.                         $to_resource $this->course->resources[$to_type][$to_id];
  150.                         $sql "INSERT INTO ".$table." SET source_type = '".$type."', source_id = '".$resource->destination_id."', resource_type='".$to_type."', resource_id='".$to_resource->destination_id."' ";
  151.                         api_sql_query($sql__FILE____LINE__);
  152.                     }
  153.                 }
  154.             }
  155.         }
  156.     }
  157.     /**
  158.      * Restore documents
  159.      */
  160.     function restore_documents()
  161.     {
  162.         if ($this->course->has_resources(RESOURCE_DOCUMENT))
  163.         {
  164.             $table Database :: get_course_table(TABLE_DOCUMENT$this->course->destination_db);
  165.             $resources $this->course->resources;
  166.             foreach ($resources[RESOURCE_DOCUMENTas $id => $document)
  167.             {
  168.                 $path api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/';
  169.                 $perm api_get_setting('permissions_for_new_directories');
  170.                     $perm octdec(!empty($perm)?$perm:'0770');
  171.                 mkdirr(dirname($path.$document->path),$perm);
  172.                 if ($document->file_type == DOCUMENT)
  173.                 {
  174.                     if (file_exists($path.$document->path))
  175.                     {
  176.                         switch ($this->file_option)
  177.                         {
  178.                             case FILE_OVERWRITE :
  179.                                 copy($this->course->backup_path.'/'.$document->path$path.$document->path);
  180.                                 $sql "SELECT id FROM ".$table." WHERE path='/".substr($document->path9)."'";
  181.                                 $res api_sql_query($sql__FILE____LINE__);
  182.                                 $obj Database::fetch_object($res);
  183.                                 $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id $obj->id;
  184.                                 $sql "UPDATE ".$table." SET comment = '".Database::escape_string($document->comment)."', title='".Database::escape_string($document->title)."', size='".$document->size."' WHERE id = '".$obj->id."'";
  185.                                 api_sql_query($sql__FILE____LINE__);
  186.                                 break;
  187.                             case FILE_SKIP :
  188.                                 $sql "SELECT id FROM ".$table." WHERE path='/".Database::escape_string(substr($document->path9))."'";
  189.                                 $res api_sql_query($sql__FILE____LINE__);
  190.                                 $obj Database::fetch_object($res);
  191.                                 $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id $obj->id;
  192.                                 break;
  193.                             case FILE_RENAME :
  194.                                 $i 1;
  195.                                 $ext explode('.'basename($document->path));
  196.                                 if (count($ext1)
  197.                                 {
  198.                                     $ext array_pop($ext);
  199.                                     $file_name_no_ext substr($document->path0(strlen($ext1));
  200.                                     $ext '.'.$ext;
  201.                                 }
  202.                                 else
  203.                                 {
  204.                                     $ext '';
  205.                                     $file_name_no_ext $document->path;
  206.                                 }
  207.                                 $new_file_name $file_name_no_ext.'_'.$i.$ext;
  208.                                 $file_exists file_exists($path.$new_file_name);
  209.                                 while ($file_exists)
  210.                                 {
  211.                                     $i ++;
  212.                                     $new_file_name $file_name_no_ext.'_'.$i.$ext;
  213.                                     $file_exists file_exists($path.$new_file_name);
  214.                                 }
  215.                                 copy($this->course->backup_path.'/'.$document->path$path.$new_file_name);
  216.                                 $sql "INSERT INTO ".$table." SET path = '/".Database::escape_string(substr($new_file_name9))."', comment = '".Database::escape_string($document->comment)."', title = '".Database::escape_string($document->title)."' ,filetype='".$document->file_type."', size= '".$document->size."'";
  217.                                 api_sql_query($sql__FILE____LINE__);
  218.                                 $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id Database::get_last_insert_id();
  219.                                 //also insert into item_property
  220.                                 /*
  221.                                 api_item_property_update(
  222.                                         array('dbName'=>$this->course->destination_db,
  223.                                         TOOL_DOCUMENT,
  224.                                         $this->course->resource[RESOURCE_DOCUMENT][$id]->destination_id,
  225.                                         'DocumentAdded',
  226.                                         );
  227.                                 */
  228.                                 break;
  229.                         // end switch
  230.                     // end if file exists
  231.                     else
  232.                     {
  233.                         //make sure the source file actually exists
  234.                         if(is_file($this->course->backup_path.'/'.$document->path&& is_readable($this->course->backup_path.'/'.$document->path&& is_dir(dirname($path.$document->path)) && is_writeable(dirname($path.$document->path)))
  235.                         {
  236.                             copy($this->course->backup_path.'/'.$document->path$path.$document->path);
  237.                             $sql "INSERT INTO ".$table." SET path = '/".substr($document->path9)."', comment = '".Database::escape_string($document->comment)."', title = '".Database::escape_string($document->title)."' ,filetype='".$document->file_type."', size= '".$document->size."'";
  238.                             api_sql_query($sql__FILE____LINE__);
  239.                             $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id Database::get_last_insert_id();
  240.                         }
  241.                         else
  242.                         {
  243.                             if(is_file($this->course->backup_path.'/'.$document->path&& is_readable($this->course->backup_path.'/'.$document->path))
  244.                             {
  245.                                 error_log('Course copy generated an ignoreable error while trying to copy '.$this->course->backup_path.'/'.$document->path.': file not found');
  246.                             }
  247.                             if(!is_dir(dirname($path.$document->path)))
  248.                             {
  249.                                 error_log('Course copy generated an ignoreable error while trying to copy to '.dirname($path.$document->path).': directory not found');
  250.                             }
  251.                             if(!is_writeable(dirname($path.$document->path)))
  252.                             {
  253.                                 error_log('Course copy generated an ignoreable error while trying to copy to '.dirname($path.$document->path).': directory not writeable');
  254.                             }
  255.                         }
  256.                     // end file doesn't exist
  257.                 }
  258.                 else
  259.                 {
  260.                     $sql "SELECT id FROM ".$table." WHERE path = '/".Database::escape_string(substr($document->path9))."'";
  261.                     $res api_sql_query($sql,__FILE__,__LINE__);
  262.                     ifDatabase::num_rows($res)0)
  263.                     {
  264.                         $obj Database::fetch_object($res);
  265.                         $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id $obj->id;    
  266.                     }
  267.                     else
  268.                     {
  269.                         $sql "INSERT INTO ".$table." SET path = '/".Database::escape_string(substr($document->path9))."', comment = '".Database::escape_string($document->comment)."', title = '".Database::escape_string($document->title)."' ,filetype='".$document->file_type."', size= '".$document->size."'";
  270.                         api_sql_query($sql__FILE____LINE__);
  271.                         $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id Database::get_last_insert_id();
  272.                     }
  273.                 // end folder
  274.             // end for each
  275.         }
  276.     }
  277.  
  278.     /**
  279.      * Restore scorm documents
  280.      * TODO @TODO check that the restore function with renaming doesn't break the scorm structure!
  281.      */
  282.     function restore_scorm_documents()
  283.     {
  284.         if ($this->course->has_resources(RESOURCE_SCORM))
  285.         {
  286.             $resources $this->course->resources;
  287.  
  288.             foreach ($resources[RESOURCE_SCORMas $id => $document)
  289.             {
  290.                 $path api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/';
  291.  
  292.                 $perm api_get_setting('permissions_for_new_directories');
  293.                     $perm octdec(!empty($perm)?$perm:'0770');
  294.                 mkdirr(dirname($path.$document->path),$perm);
  295.  
  296.                 if (file_exists($path.$document->path))
  297.                 {
  298.                     switch ($this->file_option)
  299.                     {
  300.                         case FILE_OVERWRITE :
  301.                             rmdirr($path.$document->path);
  302.  
  303.                             copyDirTo($this->course->backup_path.'/'.$document->path$path.dirname($document->path)false);
  304.  
  305.                             break;
  306.                         case FILE_SKIP :
  307.                             break;
  308.                         case FILE_RENAME :
  309.                             $i 1;
  310.  
  311.                             $ext explode('.'basename($document->path));
  312.  
  313.                             if (count($ext1)
  314.                             {
  315.                                 $ext array_pop($ext);
  316.                                 $file_name_no_ext substr($document->path0(strlen($ext1));
  317.                                 $ext '.'.$ext;
  318.                             }
  319.                             else
  320.                             {
  321.                                 $ext '';
  322.                                 $file_name_no_ext $document->path;
  323.                             }
  324.  
  325.                             $new_file_name $file_name_no_ext.'_'.$i.$ext;
  326.                             $file_exists file_exists($path.$new_file_name);
  327.  
  328.                             while ($file_exists)
  329.                             {
  330.                                 $i ++;
  331.                                 $new_file_name $file_name_no_ext.'_'.$i.$ext;
  332.                                 $file_exists file_exists($path.$new_file_name);
  333.                             }
  334.  
  335.                             rename($this->course->backup_path.'/'.$document->path,$this->course->backup_path.'/'.$new_file_name);
  336.  
  337.                             copyDirTo($this->course->backup_path.'/'.$new_file_name$path.dirname($new_file_name)false);
  338.  
  339.                             rename($this->course->backup_path.'/'.$new_file_name,$this->course->backup_path.'/'.$document->path);
  340.  
  341.                             break;
  342.                     // end switch
  343.                 // end if file exists
  344.                 else
  345.                 {
  346.                     copyDirTo($this->course->backup_path.'/'.$document->path$path.dirname($document->path)false);
  347.                 }
  348.             // end for each
  349.         }
  350.     }
  351.  
  352.     /**
  353.      * Restore forums
  354.      */
  355.     function restore_forums()
  356.     {
  357.         if ($this->course->has_resources(RESOURCE_FORUM))
  358.         {
  359.             $table_forum Database :: get_course_table(TABLE_FORUM$this->course->destination_db);
  360.             $table_topic Database :: get_course_table(TABLE_FORUM_POST$this->course->destination_db);
  361.             $table_post Database :: get_course_table(TABLE_FORUM_POST$this->course->destination_db);
  362.             $resources $this->course->resources;
  363.             foreach ($resources[RESOURCE_FORUMas $id => $forum)
  364.             {
  365.                 $cat_id $this->restore_forum_category($forum->category_id);
  366.                 $sql "INSERT INTO ".$table_forum." SET forum_name = '".Database::escape_string($forum->title)."', forum_desc = '".Database::escape_string($forum->description)."', cat_id = '".$cat_id."', forum_access='2'";
  367.                 api_sql_query($sql__FILE____LINE__);
  368.                 $new_id Database::get_last_insert_id();
  369.                 $this->course->resources[RESOURCE_FORUM][$id]->destination_id $new_id;
  370.                 $forum_topics 0;
  371.                 if (is_array($this->course->resources[RESOURCE_FORUMTOPIC]))
  372.                 {
  373.                     foreach ($this->course->resources[RESOURCE_FORUMTOPICas $topic_id => $topic)
  374.                     {
  375.                         if ($topic->forum_id == $id)
  376.                         {
  377.                             $this->restore_topic($topic_id$new_id);
  378.                             $forum_topics ++;
  379.                         }
  380.                     }
  381.                 }
  382.                 if ($forum_topics 0)
  383.                 {
  384.                     $last_post $this->course->resources[RESOURCE_FORUMPOST][$forum->last_post];
  385.                     $sql "UPDATE ".$table_forum." SET forum_topics = ".$forum_topics.", forum_last_post_id = ".$last_post->destination_id." WHERE forum_id = '".$new_id."' ";
  386.                     api_sql_query($sql__FILE____LINE__);
  387.                 }
  388.             }
  389.         }
  390.     }
  391.     /**
  392.      * Restore forum-categories
  393.      */
  394.     function restore_forum_category($id)
  395.     {
  396.         $forum_cat_table Database :: get_course_table(TABLE_FORUM_CATEGORY$this->course->destination_db);
  397.         $resources $this->course->resources;
  398.         $forum_cat $resources[RESOURCE_FORUMCATEGORY][$id];
  399.         if (!$forum_cat->is_restored())
  400.         {
  401.             $sql "INSERT INTO ".$forum_cat_table." SET cat_title = '".Database::escape_string($forum_cat->title.' ('.$this->course->code.')')."'";
  402.             api_sql_query($sql__FILE____LINE__);
  403.             $new_id Database::get_last_insert_id();
  404.             $this->course->resources[RESOURCE_FORUMCATEGORY][$id]->destination_id $new_id;
  405.             return $new_id;
  406.         }
  407.         return $this->course->resources[RESOURCE_FORUMCATEGORY][$id]->destination_id;
  408.     }
  409.     /**
  410.      * Restore a forum-topic
  411.      */
  412.     function restore_topic($id$forum_id)
  413.     {
  414.         $table Database :: get_course_table(TABLE_FORUM_POST$this->course->destination_db);
  415.         $resources $this->course->resources;
  416.         $topic $resources[RESOURCE_FORUMTOPIC][$id];
  417.         $sql "INSERT INTO ".$table." SET topic_title = '".Database::escape_string($topic->title)."', topic_time = '".$topic->time."', nom = '".Database::escape_string($topic->lastname)."', prenom = '".Database::escape_string($topic->firstname)."', topic_notify = '".$topic->topic_notify."', forum_id = '".$forum_id."'";
  418.         api_sql_query($sql__FILE____LINE__);
  419.         $new_id Database::get_last_insert_id();
  420.         $this->course->resources[RESOURCE_FORUMTOPIC][$id]->destination_id $new_id;
  421.         $topic_replies = -1;
  422.         foreach ($this->course->resources[RESOURCE_FORUMPOSTas $post_id => $post)
  423.         {
  424.             if ($post->topic_id == $id)
  425.             {
  426.                 $topic_replies ++;
  427.                 $this->restore_post($post_id$new_id$forum_id);
  428.             }
  429.         }
  430.         if ($topic_replies >= 0)
  431.         {
  432.             $last_post $this->course->resources[RESOURCE_FORUMPOST][$topic->last_post];
  433.             $sql "UPDATE ".$table." SET topic_replies = '".$topic_replies."', topic_last_post_id = ".$last_post->destination_id;
  434.             api_sql_query($sql__FILE____LINE__);
  435.         }
  436.         return $new_id;
  437.     }
  438.     /**
  439.      * restore a forum-post
  440.      * @todo restore tree-structure of posts.
  441.      */
  442.     function restore_post($id$topic_id$forum_id)
  443.     {
  444.         $table_post Database :: get_course_table(TABLE_FORUM_POST$this->course->destination_db);
  445.         $table_posttext Database :: get_course_table(TOOL_FORUM_POST_TEXT_TABLE$this->course->destination_db);
  446.         $resources $this->course->resources;
  447.         $post $resources[RESOURCE_FORUMPOST][$id];
  448.         $sql "INSERT INTO ".$table_post." SET topic_id = '".$topic_id."', post_time = '".$post->post_time."', forum_id = '".$forum_id."', nom = '".Database::escape_string($post->lastname)."', prenom = '".Database::escape_string($post->firstname)."', topic_notify = '".$post->topic_notify."', poster_ip = '".$post->poster_ip."'";
  449.         api_sql_query($sql__FILE____LINE__);
  450.         $new_id Database::get_last_insert_id();
  451.         $this->course->resources[RESOURCE_FORUMPOST][$id]->destination_id $new_id;
  452.         $sql "INSERT INTO ".$table_posttext." SET post_text = '".Database::escape_string($post->text)."', post_title = '".Database::escape_string($post->title)."', post_id = '".$new_id."'";
  453.         api_sql_query($sql__FILE____LINE__);
  454.         return $new_id;
  455.     }
  456.     /**
  457.      * Restore links
  458.      */
  459.     function restore_links()
  460.     {
  461.         if ($this->course->has_resources(RESOURCE_LINK))
  462.         {
  463.             $link_table Database :: get_course_table(TABLE_LINK$this->course->destination_db);
  464.             $resources $this->course->resources;
  465.             foreach ($resources[RESOURCE_LINKas $id => $link)
  466.             {
  467.                 $cat_id $this->restore_link_category($link->category_id);
  468.                 $sql "SELECT MAX(display_order) FROM  $link_table WHERE category_id='Database::escape_string($cat_id)"'";
  469.                 $result api_sql_query($sql__FILE____LINE__);
  470.                 list($max_orderDatabase::fetch_array($result);
  471.                 $sql "INSERT INTO ".$link_table." SET url = '".Database::escape_string($link->url)."', title = '".Database::escape_string($link->title)."', description = '".Database::escape_string($link->description)."', category_id='".$cat_id."', on_homepage = '".$link->on_homepage."', display_order='".($max_order+1)."'";
  472.                 api_sql_query($sql__FILE____LINE__);
  473.                 $this->course->resources[RESOURCE_LINK][$id]->destination_id Database::get_last_insert_id();
  474.             }
  475.         }
  476.     }
  477.     /**
  478.      * Restore tool intro
  479.      */
  480.     function restore_tool_intro()
  481.     {
  482.         if ($this->course->has_resources(RESOURCE_TOOL_INTRO))
  483.         {
  484.             $tool_intro_table Database :: get_course_table(TABLE_TOOL_INTRO$this->course->destination_db);
  485.             $resources $this->course->resources;
  486.             foreach ($resources[RESOURCE_TOOL_INTROas $id => $tool_intro)
  487.             {
  488.                 $sql "DELETE FROM ".$tool_intro_table." WHERE id='".Database::escape_string($tool_intro->id)."'";
  489.                 api_sql_query($sql__FILE____LINE__);
  490.  
  491.                 $sql "INSERT INTO ".$tool_intro_table." SET id='".Database::escape_string($tool_intro->id)."', intro_text = '".Database::escape_string($tool_intro->intro_text)."'";
  492.                 api_sql_query($sql__FILE____LINE__);
  493.  
  494.                 $this->course->resources[RESOURCE_TOOL_INTRO][$id]->destination_id Database::get_last_insert_id();
  495.             }
  496.         }
  497.     }
  498.     /**
  499.      * Restore a link-category
  500.      */
  501.     function restore_link_category($id)
  502.     {
  503.         if ($id == 0)
  504.             return 0;
  505.         $link_cat_table Database :: get_course_table(TABLE_LINK_CATEGORY$this->course->destination_db);
  506.         $resources $this->course->resources;
  507.         $link_cat $resources[RESOURCE_LINKCATEGORY][$id];
  508.         if (is_object($link_cat&& !$link_cat->is_restored())
  509.         {
  510.             $sql "SELECT MAX(display_order) FROM  $link_cat_table";
  511.             $result=api_sql_query($sql,__FILE__,__LINE__);
  512.             list($orderMax)=Database::fetch_array($result,'NUM');
  513.             $display_order=$orderMax+1;
  514.             $sql "INSERT INTO ".$link_cat_table." SET category_title = '".Database::escape_string($link_cat->title)."', description='".Database::escape_string($link_cat->description)."', display_order='".$display_order."' ";
  515.             api_sql_query($sql__FILE____LINE__);
  516.             $new_id Database::get_last_insert_id();
  517.             $this->course->resources[RESOURCE_LINKCATEGORY][$id]->destination_id $new_id;
  518.             return $new_id;
  519.         }
  520.         return $this->course->resources[RESOURCE_LINKCATEGORY][$id]->destination_id;
  521.     }
  522.     /**
  523.      * Restore events
  524.      */
  525.     function restore_events()
  526.     {
  527.         if ($this->course->has_resources(RESOURCE_EVENT))
  528.         {
  529.             $table Database :: get_course_table(TABLE_AGENDA$this->course->destination_db);
  530.             $resources $this->course->resources;
  531.             foreach ($resources[RESOURCE_EVENTas $id => $event)
  532.             {
  533.                 $sql "INSERT INTO ".$table." SET title = '".Database::escape_string($event->title)."', content = '".Database::escape_string($event->content)."', start_date = '".$event->start_date."', end_date = '".$event->end_date."'";
  534.                 api_sql_query($sql__FILE____LINE__);
  535.                 $this->course->resources[RESOURCE_EVENT][$id]->destination_id Database::get_last_insert_id();
  536.             }
  537.         }
  538.     }
  539.     /**
  540.      * Restore course-description
  541.      */
  542.     function restore_course_descriptions()
  543.     {
  544.         if ($this->course->has_resources(RESOURCE_COURSEDESCRIPTION))
  545.         {
  546.             $table Database :: get_course_table(TABLE_COURSE_DESCRIPTION$this->course->destination_db);
  547.             $resources $this->course->resources;
  548.             foreach ($resources[RESOURCE_COURSEDESCRIPTIONas $id => $cd)
  549.             {
  550.                 $sql "INSERT INTO ".$table." SET title = '".Database::escape_string($cd->title)."', content = '".Database::escape_string($cd->content)."'";
  551.                 api_sql_query($sql__FILE____LINE__);
  552.                 $this->course->resources[RESOURCE_COURSEDESCRIPTION][$id]->destination_id Database::get_last_insert_id();
  553.             }
  554.         }
  555.     }
  556.     /**
  557.      * Restore announcements
  558.      */
  559.     function restore_announcements()
  560.     {
  561.         if ($this->course->has_resources(RESOURCE_ANNOUNCEMENT))
  562.         {
  563.             $table Database :: get_course_table(TABLE_ANNOUNCEMENT$this->course->destination_db);
  564.             $resources $this->course->resources;
  565.             foreach ($resources[RESOURCE_ANNOUNCEMENTas $id => $announcement)
  566.             {
  567.                 $sql "INSERT INTO ".$table." " .
  568.                         "SET title = '".Database::escape_string($announcement->title)."'," .
  569.                             "content = '".Database::escape_string($announcement->content)."', " .
  570.                             "end_date = '".$announcement->date."', " .
  571.                             "display_order = '".$announcement->display_order."', " .
  572.                             "email_sent = '".$announcement->email_sent."'";
  573.                 api_sql_query($sql__FILE____LINE__);
  574.                 $this->course->resources[RESOURCE_ANNOUNCEMENT][$id]->destination_id Database::get_last_insert_id();
  575.             }
  576.         }
  577.     }
  578.     /**
  579.      * Restore Quiz
  580.      */
  581.     function restore_quizzes()
  582.     {
  583.         if ($this->course->has_resources(RESOURCE_QUIZ))
  584.         {
  585.             $table_qui Database :: get_course_table(TABLE_QUIZ_TEST$this->course->destination_db);
  586.             $table_rel Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION$this->course->destination_db);
  587.             $table_doc Database :: get_course_table(TABLE_DOCUMENT$this->course->destination_db);
  588.             $resources $this->course->resources;
  589.             foreach ($resources[RESOURCE_QUIZas $id => $quiz)
  590.             {
  591.                 $doc '';
  592.                 if (strlen($quiz->media0)
  593.                 {
  594.                     if ($this->course->resources[RESOURCE_DOCUMENT][$quiz->media]->is_restored())
  595.                     {
  596.                         $sql "SELECT path FROM ".$table_doc." WHERE id = ".$resources[RESOURCE_DOCUMENT][$quiz->media]->destination_id;
  597.                         $doc api_sql_query($sql__FILE____LINE__);
  598.                         $doc Database::fetch_object($doc);
  599.                         $doc str_replace('/audio/'''$doc->path);
  600.                     }
  601.                 }
  602.                 $sql "INSERT INTO ".$table_qui." SET title = '".Database::escape_string($quiz->title)."', description = '".Database::escape_string($quiz->description)."', type = '".$quiz->quiz_type."', random = '".$quiz->random."', active = '".$quiz->active."', sound = '".Database::escape_string($doc)."' ";
  603.                 api_sql_query($sql__FILE____LINE__);
  604.                 $new_id Database::get_last_insert_id();
  605.                 $this->course->resources[RESOURCE_QUIZ][$id]->destination_id $new_id;
  606.                 foreach ($quiz->question_ids as $index => $question_id)
  607.                 {
  608.                     $qid $this->restore_quiz_question($question_id);
  609.                     $sql "INSERT IGNORE INTO ".$table_rel." SET question_id = ".$qid.", exercice_id = ".$new_id."";
  610.                     api_sql_query($sql__FILE____LINE__);
  611.                 }
  612.             }
  613.         }
  614.     }
  615.     /**
  616.      * Restore quiz-questions
  617.      */
  618.     function restore_quiz_question($id)
  619.     {
  620.         $resources $this->course->resources;
  621.         $question $resources[RESOURCE_QUIZQUESTION][$id];
  622.  
  623.         $new_id=0;
  624.  
  625.         if(is_object($question))
  626.         {
  627.             if ($question->is_restored())
  628.             {
  629.                 return $question->destination_id;
  630.             }
  631.             $table_que Database :: get_course_table(TABLE_QUIZ_QUESTION$this->course->destination_db);
  632.             $table_ans Database :: get_course_table(TABLE_QUIZ_ANSWER$this->course->destination_db);
  633.             $sql "INSERT INTO ".$table_que." SET question = '".addslashes($question->question)."', description = '".addslashes($question->description)."', ponderation = '".addslashes($question->ponderation)."', position = '".addslashes($question->position)."', type='".addslashes($question->quiz_type)."', picture='".addslashes($question->picture)."'";
  634.             api_sql_query($sql__FILE____LINE__);
  635.             $new_id Database::get_last_insert_id();
  636.             foreach ($question->answers as $index => $answer)
  637.             {
  638.                 $sql "INSERT INTO ".$table_ans." SET id= '"($index +1)."',question_id = '".$new_id."', answer = '".Database::escape_string($answer['answer'])."', correct = '".$answer['correct']."', comment = '".Database::escape_string($answer['comment'])."', ponderation='".$answer['ponderation']."', position = '".$answer['position']."', hotspot_coordinates = '".$answer['hotspot_coordinates']."', hotspot_type = '".$answer['hotspot_type']."'";
  639.                 api_sql_query($sql__FILE____LINE__);
  640.             }
  641.             $this->course->resources[RESOURCE_QUIZQUESTION][$id]->destination_id $new_id;
  642.         }
  643.  
  644.         return $new_id;
  645.     }
  646.     /**
  647.      * Restore Quiz
  648.      */
  649.     function restore_surveys()
  650.     {
  651.         if ($this->course->has_resources(RESOURCE_SURVEY))
  652.         {
  653.             $table_sur Database :: get_course_table(TABLE_SURVEY$this->course->destination_db);
  654.             $table_que Database :: get_course_table(TABLE_SURVEY_QUESTION$this->course->destination_db);
  655.             $table_ans Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION$this->course->destination_db);
  656.             $resources $this->course->resources;
  657.             foreach ($resources[RESOURCE_SURVEYas $id => $survey)
  658.             {
  659.                 $doc '';
  660.                 $sql "INSERT INTO ".$table_sur." " .
  661.                         "SET code = '".Database::escape_string($survey->code)."', " .
  662.                         "title = '".Database::escape_string($survey->title)."', " .
  663.                         "subtitle = '".Database::escape_string($survey->subtitle)."', " .
  664.                         "author = '".Database::escape_string($survey->author)."', " .
  665.                         "lang = '".Database::escape_string($survey->lang)."', " .
  666.                         "avail_from = '".Database::escape_string($survey->avail_from)."', " .
  667.                         "avail_till = '".Database::escape_string($survey->avail_till)."', " .
  668.                         "is_shared = '".Database::escape_string($survey->is_shared)."', " .
  669.                         "template = '".Database::escape_string($survey->template)."', " .
  670.                         "intro = '".Database::escape_string($survey->intro)."', " .
  671.                         "surveythanks = '".Database::escape_string($survey->surveythanks)."', " .
  672.                         "creation_date = '".Database::escape_string($survey->creation_date)."', " .
  673.                         "invited = '0', " .
  674.                         "answered = '0', " .
  675.                         "invite_mail = '".Database::escape_string($survey->invite_mail)."', " .
  676.                         "reminder_mail = '".Database::escape_string($survey->reminder_mail)."'";
  677.                 api_sql_query($sql__FILE____LINE__);
  678.                 $new_id Database::get_last_insert_id();
  679.                 $this->course->resources[RESOURCE_SURVEY][$id]->destination_id $new_id;
  680.                 foreach ($survey->question_ids as $index => $question_id)
  681.                 {
  682.                     $qid $this->restore_survey_question($question_id);
  683.                     $sql "UPDATE ".$table_que." " .
  684.                             "SET survey_id = ".$new_id." WHERE " .
  685.                             "question_id = ".$qid."";
  686.                     api_sql_query($sql__FILE____LINE__);
  687.                     $sql "UPDATE ".$table_ans." ".
  688.                             "SET survey_id = ".$new_id." WHERE " .
  689.                             "question_id = ".$qid."";
  690.                     api_sql_query($sql__FILE____LINE__);
  691.                 }
  692.             }
  693.         }
  694.     }
  695.     /**
  696.      * Restore survey-questions
  697.      */
  698.     function restore_survey_question($id)
  699.     {
  700.         $resources $this->course->resources;
  701.         $question $resources[RESOURCE_SURVEYQUESTION][$id];
  702.  
  703.         $new_id=0;
  704.  
  705.         if(is_object($question))
  706.         {
  707.             if ($question->is_restored())
  708.             {
  709.                 return $question->destination_id;
  710.             }
  711.             $table_que Database :: get_course_table(TABLE_SURVEY_QUESTION$this->course->destination_db);
  712.             $table_ans Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION$this->course->destination_db);
  713.             $sql "INSERT INTO ".$table_que." " .
  714.                     "SET survey_id = '".addslashes($question->survey_id)."', " .
  715.                     "survey_question = '".addslashes($question->survey_question)."', " .
  716.                     "survey_question_comment = '".addslashes($question->survey_question_comment)."', " .
  717.                     "type = '".addslashes($question->survey_question_type)."', " .
  718.                     "display = '".addslashes($question->display)."', " .
  719.                     "sort = '".addslashes($question->sort)."', " .
  720.                     "shared_question_id = '".addslashes($question->shared_question_id)."', " .
  721.                     "max_value = '".addslashes($question->max_value)."' ";
  722.             api_sql_query($sql__FILE____LINE__);
  723.             $new_id Database::get_last_insert_id();
  724.             foreach ($question->answers as $index => $answer)
  725.             {
  726.                 $sql "INSERT INTO ".$table_ans." " .
  727.                         "SET " .
  728.                         "question_id = '".addslashes($new_id)."', " .
  729.                         "option_text = '".addslashes($answer['option_text'])."', " .
  730.                         "sort = '".addslashes($answer['sort'])."', " .
  731.                         "survey_id = '".addslashes($question->survey_id)."'";
  732.  
  733.                 api_sql_query($sql__FILE____LINE__);
  734.             }
  735.             $this->course->resources[RESOURCE_SURVEYQUESTION][$id]->destination_id $new_id;
  736.         }
  737.  
  738.         return $new_id;
  739.     }
  740.     /**
  741.      * Restore learnpaths
  742.      */
  743.     function restore_learnpaths()
  744.     {
  745.         if ($this->course->has_resources(RESOURCE_LEARNPATH))
  746.         {
  747.             $table_main     Database :: get_course_table(TABLE_LP_MAIN$this->course->destination_db);
  748.             $table_item     Database :: get_course_table(TABLE_LP_ITEM$this->course->destination_db);
  749.             $table_tool     Database::get_course_table(TABLE_TOOL_LIST$this->course->destination_db);
  750.  
  751.             $resources $this->course->resources;
  752.             $prereq_old array ();
  753.             $item_old_id array ();
  754.  
  755.             foreach ($resources[RESOURCE_LEARNPATHas $id => $lp)
  756.             {
  757.                 $sql "INSERT INTO ".$table_main." " .
  758.                         "SET lp_type = '".$lp->lp_type."', " .
  759.                                 "name = '".Database::escape_string($lp->name)."', " .
  760.                                 "path = '".Database::escape_string($lp->path)."', " .
  761.                                 "ref = '".$lp->ref."', " .
  762.                                 "description = '".Database::escape_string($lp->description)."', " .
  763.                                 "content_local = '".Database::escape_string($lp->content_local)."', " .
  764.                                 "default_encoding = '".Database::escape_string($lp->default_encoding)."', " .
  765.                                 "default_view_mod = '".Database::escape_string($lp->default_view_mod)."', " .
  766.                                 "prevent_reinit = '".Database::escape_string($lp->prevent_reinit)."', " .
  767.                                 "force_commit = '".Database::escape_string($lp->force_commit)."', " .
  768.                                 "content_maker = '".Database::escape_string($lp->content_maker)."', " .
  769.                                 "display_order = '".Database::escape_string($lp->display_order)."', " .
  770.                                 "js_lib= '".Database::escape_string($lp->js_lib)."', " .
  771.                                 "content_license= '".Database::escape_string($lp->content_license)."', " .
  772.                                 "debug= '".Database::escape_string($lp->debug)."' ";
  773.                 api_sql_query($sql__FILE____LINE__);
  774.  
  775.                 $new_lp_id Database::get_last_insert_id();
  776.  
  777.                 if($lp->visibility)
  778.                 {
  779.                     $sql "INSERT INTO $table_tool SET name='".Database::escape_string($lp->name)."', link='newscorm/lp_controller.php?action=view&lp_id=$new_lp_id', image='scormbuilder.gif', visibility='1', admin='0', address='squaregrey.gif'";
  780.                     api_sql_query($sql__FILE____LINE__);
  781.                 }
  782.  
  783.                 $new_item_ids array();
  784.                 $parent_item_ids array();
  785.                 $previous_item_ids array();
  786.                 $next_item_ids array();
  787.                 foreach ($lp->get_items(as $index => $item)
  788.                 {
  789.                     /*
  790.                     if ($item['id'] != 0)
  791.                     {
  792.                          // Links in learnpath have types 'Link _self' or 'Link _blank'. We only need 'Link' here.
  793.                          $type_parts = explode(' ',$item['type']);
  794.                          $item['id'] = $this->course->resources[$type_parts[0]][$item['id']]->destination_id;
  795.                     }
  796.                     */
  797.                     //Get the new ref ID for all items that are not sco (dokeos quizzes, documents, etc)
  798.                     $ref '';
  799.                     if(!empty($item['ref']&& $lp->lp_type!='2'){
  800.                         $ref $this->get_new_id($item['item_type'],$item['ref']);
  801.                     }else{
  802.                         $ref $item['ref'];
  803.                     }
  804.                     //Dealing with path the same way as ref as some data has been put into path when it's a
  805.                     //local resource
  806.                     $path Database::escape_string($item['path']);
  807.                     if(strval(intval($path)) === $path)
  808.                     {
  809.                         $path $this->get_new_id($item['item_type'],$path);
  810.                     }
  811.                     $sql "INSERT INTO ".$table_item." SET " .
  812.                             "lp_id = '".$new_lp_id."', " .
  813.                             "item_type='".$item['item_type']."', " .
  814.                             "ref = '".$ref."', " .
  815.                             "title = '".Database::escape_string($item['title'])."', " .
  816.                             "description ='".Database::escape_string($item['description'])."', " .
  817.                             "path = '".$path."', " .
  818.                             "min_score = '".$item['min_score']."', " .
  819.                             "max_score = '".$item['max_score']."', " .
  820.                             "mastery_score = '".$item['mastery_score']."', " .
  821.                             "parent_item_id = '".$item['parent_item_id']."', " .
  822.                             "previous_item_id = '".$item['previous_item_id']."', " .
  823.                             "next_item_id = '".$item['next_item_id']."', " .
  824.                             "display_order = '".$item['display_order']."', " .
  825.                             "prerequisite = '".Database::escape_string($item['prerequisite'])."', " .
  826.                             "parameters='".Database::escape_string($item['parameters'])."', " .
  827.                             "launch_data = '".Database::escape_string($item['launch_dataprereq_type'])."'";
  828.                     api_sql_query($sql__FILE____LINE__);
  829.                     $new_item_id Database::get_last_insert_id();
  830.                     //save a link between old and new item IDs
  831.                     $new_item_ids[$item['id']] $new_item_id;
  832.                     //save a reference of items that need a parent_item_id refresh
  833.                     $parent_item_ids[$new_item_id$item['parent_item_id'];
  834.                     //save a reference of items that need a previous_item_id refresh
  835.                     $previous_item_ids[$new_item_id$item['previous_item_id'];
  836.                     //save a reference of items that need a next_item_id refresh
  837.                     $next_item_ids[$new_item_id$item['next_item_id'];
  838.                 }
  839.                 foreach ($parent_item_ids as $new_item_id => $parent_item_old_id)
  840.                 {
  841.                     $parent_new_id 0;
  842.                     if($parent_item_old_id != 0){
  843.                         $parent_new_id $new_item_ids[$parent_item_old_id];
  844.                     }
  845.                     $sql "UPDATE ".$table_item." SET parent_item_id = '".$parent_new_id."' WHERE id = '".$new_item_id."'";
  846.                     api_sql_query($sql__FILE____LINE__);
  847.                 }
  848.                 foreach ($previous_item_ids as $new_item_id => $previous_item_old_id)
  849.                 {
  850.                     $previous_new_id 0;
  851.                     if($previous_item_old_id != 0){
  852.                         $previous_new_id $new_item_ids[$previous_item_old_id];
  853.                     }
  854.                     $sql "UPDATE ".$table_item." SET previous_item_id = '".$previous_new_id."' WHERE id = '".$new_item_id."'";
  855.                     api_sql_query($sql__FILE____LINE__);
  856.                 }
  857.                 foreach ($next_item_ids as $new_item_id => $next_item_old_id)
  858.                 {
  859.                     $next_new_id 0;
  860.                     if($next_item_old_id != 0){
  861.                         $next_new_id $new_item_ids[$next_item_old_id];
  862.                     }
  863.                     $sql "UPDATE ".$table_item." SET next_item_id = '".$next_new_id."' WHERE id = '".$new_item_id."'";
  864.                     api_sql_query($sql__FILE____LINE__);
  865.                 }
  866.                 $this->course->resources[RESOURCE_LEARNPATH][$id]->destination_id $new_lp_id;
  867.             }
  868.         }
  869.     }
  870.     /**
  871.      * Gets the new ID of one specific tool item from the tool name and the old ID
  872.      * @param    string    Tool name
  873.      * @param    integer    Old ID
  874.      * @return    integer    New ID
  875.      */
  876.     function get_new_id($tool,$ref)
  877.     {
  878.         //transform $tool into one backup/restore constant
  879.         if($tool == 'hotpotatoes'){$tool 'document';}
  880.         if(!empty($this->course->resources[$tool][$ref]->destination_id)){
  881.             return $this->course->resources[$tool][$ref]->destination_id;
  882.         }
  883.         return '';
  884.     }
  885. }
  886. ?>

Documentation generated on Thu, 12 Jun 2008 13:12:57 -0500 by phpDocumentor 1.4.1