Browse Source

Fixing PHP warnings.

Julio Montoya 9 years ago
parent
commit
a17d52df4a

+ 20 - 11
main/coursecopy/classes/CourseRecycler.class.php

@@ -46,9 +46,9 @@ class CourseRecycler
 
         $this->type = $type;
 
-        $table_tool_intro 		= Database::get_course_table(TABLE_TOOL_INTRO);
+        $table_tool_intro = Database::get_course_table(TABLE_TOOL_INTRO);
         $table_linked_resources = Database::get_course_table(TABLE_LINKED_RESOURCES);
-        $table_item_properties 	= Database::get_course_table(TABLE_ITEM_PROPERTY);
+        $table_item_properties = Database::get_course_table(TABLE_ITEM_PROPERTY);
 
         $this->recycle_links();
         $this->recycle_link_categories();
@@ -117,7 +117,8 @@ class CourseRecycler
                 }
 
                 $ids = implode(',', (array_keys($this->course->resources[RESOURCE_DOCUMENT])));
-                $sql = "DELETE FROM $table WHERE c_id = ".$this->course_id." AND id IN(".$ids.")";
+                $sql = "DELETE FROM $table
+                        WHERE c_id = ".$this->course_id." AND id IN(".$ids.")";
                 Database::query($sql);
             }
         }
@@ -129,8 +130,8 @@ class CourseRecycler
     public function recycle_wiki()
     {
         if ($this->course->has_resources(RESOURCE_WIKI)) {
-            $table_wiki 		= Database::get_course_table(TABLE_WIKI);
-            $table_wiki_conf 	= Database::get_course_table(TABLE_WIKI_CONF);
+            $table_wiki = Database::get_course_table(TABLE_WIKI);
+            $table_wiki_conf = Database::get_course_table(TABLE_WIKI_CONF);
             $pages = array();
             foreach ($this->course->resources[RESOURCE_WIKI] as $resource) {
                 $pages[] = $resource->page_id;
@@ -138,9 +139,11 @@ class CourseRecycler
             $wiki_ids = implode(',', (array_keys($this->course->resources[RESOURCE_WIKI])));
             $page_ids = implode(',', $pages);
 
-            $sql = "DELETE FROM ".$table_wiki." WHERE c_id = ".$this->course_id." AND id IN(".$wiki_ids.")";
+            $sql = "DELETE FROM ".$table_wiki."
+                    WHERE c_id = ".$this->course_id." AND id IN(".$wiki_ids.")";
             Database::query($sql);
-            $sql = "DELETE FROM ".$table_wiki_conf." WHERE c_id = ".$this->course_id." AND page_id IN(".$page_ids.")";
+            $sql = "DELETE FROM ".$table_wiki_conf."
+                    WHERE c_id = ".$this->course_id." AND page_id IN(".$page_ids.")";
             Database::query($sql);
         }
     }
@@ -279,7 +282,6 @@ class CourseRecycler
                     WHERE c_id = ".$this->course_id." AND forum_id IN(".$forum_ids.")";
             Database::query($sql);
         }
-
     }
 
     /**
@@ -374,10 +376,11 @@ class CourseRecycler
     public function recycle_quizzes()
     {
         if ($this->course->has_resources(RESOURCE_QUIZ)) {
+
             $table_qui_que = Database :: get_course_table(TABLE_QUIZ_QUESTION);
             $table_qui_ans = Database :: get_course_table(TABLE_QUIZ_ANSWER);
-            $table_qui 	   = Database :: get_course_table(TABLE_QUIZ_TEST);
-            $table_rel 	   = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION);
+            $table_qui = Database :: get_course_table(TABLE_QUIZ_TEST);
+            $table_rel = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION);
             $table_qui_que_opt = Database :: get_course_table(TABLE_QUIZ_QUESTION_OPTION);
             $table_qui_que_cat = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
             $table_qui_que_rel_cat = Database :: get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
@@ -617,7 +620,13 @@ class CourseRecycler
                     }
                     $cond = array('id = ? AND c_id = ?'=>array($last_id, $this->course_id));
                     Database::delete($table_attendance, $cond);
-                    api_item_property_update($this->course_info, TOOL_ATTENDANCE, $last_id,'AttendanceDeleted', api_get_user_id());
+                    api_item_property_update(
+                        $this->course_info,
+                        TOOL_ATTENDANCE,
+                        $last_id,
+                        'AttendanceDeleted',
+                        api_get_user_id()
+                    );
                 }
             }
         }

+ 103 - 52
main/coursecopy/classes/CourseRestorer.class.php

@@ -78,6 +78,7 @@ class CourseRestorer
      *
      **/
     public $add_text_in_items = false;
+    public $destination_course_id;
 
 	/**
 	 * Create a new CourseRestorer
@@ -149,7 +150,9 @@ class CourseRestorer
         $this->destination_course_id = $course_info['real_id'];
 
         //Getting first teacher (for the forums)
-        $teacher_list = CourseManager::get_teacher_list_from_course_code($course_info['code']);
+        $teacher_list = CourseManager::get_teacher_list_from_course_code(
+            $course_info['code']
+        );
         $this->first_teacher_id = api_get_user_id();
 
         if (!empty($teacher_list)) {
@@ -194,48 +197,70 @@ class CourseRestorer
         }
 
         // Restore the item properties
-        $table = Database :: get_course_table(TABLE_ITEM_PROPERTY);
+        $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
 
         foreach ($this->course->resources as $type => $resources) {
-			if (is_array($resources)) {
-				foreach ($resources as $id => $resource) {
-					foreach ($resource->item_properties as $property) {
-						// First check if there isn't already a record for this resource
-						$sql = "SELECT * FROM $table
-						        WHERE
-						            c_id = ".$this->destination_course_id." AND
-						            tool = '".$property['tool']."' AND
-						            ref = '".$resource->destination_id."'";
-
-                        $params = [];
-                        if (!empty($session_id)) {
-                            $params['session_id'] = intval($session_id);
-                        }
+            if (is_array($resources)) {
+                foreach ($resources as $id => $resource) {
+                    if (isset($resource->item_properties)) {
+                        foreach ($resource->item_properties as $property) {
+                            // First check if there isn't already a record for this resource
+                            $sql = "SELECT * FROM $table
+                                    WHERE
+                                        c_id = ".$this->destination_course_id." AND
+                                        tool = '".$property['tool']."' AND
+                                        ref = '".$resource->destination_id."'";
 
-                        $res = Database::query($sql);
-                        if (Database::num_rows($res) == 0) {
-                            /* The to_group_id and to_user_id are set to default
-                            values as users/groups possibly not exist in
-                            the target course*/
-
-                            $params['c_id'] = $this->destination_course_id;
-                            $params['tool'] = self::DBUTF8($property['tool']);
-                            $params['insert_user_id'] = self::DBUTF8($property['insert_user_id']);
-                            $params['insert_date'] = self::DBUTF8($property['insert_date']);
-                            $params['lastedit_date'] = self::DBUTF8($property['lastedit_date']);
-                            $params['ref'] = $resource->destination_id;
-                            $params['lastedit_type'] = self::DBUTF8($property['lastedit_type']);
-                            $params['lastedit_user_id'] = self::DBUTF8($property['lastedit_user_id']);
-                            $params['visibility'] = self::DBUTF8($property['visibility']);
-                            $params['start_visible'] = self::DBUTF8($property['start_visible']);
-                            $params['end_visible'] = self::DBUTF8($property['end_visible']);
-                            $params['to_user_id'] = self::DBUTF8($property['to_user_id']);
-                            //$params['to_group_id'] = 'NULL';
-
-                            $id = Database::insert($table, $params);
-                            if ($id) {
-                                $sql = "UPDATE $table SET id = iid WHERE iid = $id";
-                                Database::query($sql);
+                            $params = [];
+                            if (!empty($session_id)) {
+                                $params['session_id'] = intval($session_id);
+                            }
+
+                            $res = Database::query($sql);
+                            if (Database::num_rows($res) == 0) {
+                                /* The to_group_id and to_user_id are set to default
+                                values as users/groups possibly not exist in
+                                the target course*/
+
+                                $params['c_id'] = $this->destination_course_id;
+                                $params['tool'] = self::DBUTF8(
+                                    $property['tool']
+                                );
+                                $params['insert_user_id'] = self::DBUTF8(
+                                    $property['insert_user_id']
+                                );
+                                $params['insert_date'] = self::DBUTF8(
+                                    $property['insert_date']
+                                );
+                                $params['lastedit_date'] = self::DBUTF8(
+                                    $property['lastedit_date']
+                                );
+                                $params['ref'] = $resource->destination_id;
+                                $params['lastedit_type'] = self::DBUTF8(
+                                    $property['lastedit_type']
+                                );
+                                $params['lastedit_user_id'] = self::DBUTF8(
+                                    $property['lastedit_user_id']
+                                );
+                                $params['visibility'] = self::DBUTF8(
+                                    $property['visibility']
+                                );
+                                $params['start_visible'] = self::DBUTF8(
+                                    $property['start_visible']
+                                );
+                                $params['end_visible'] = self::DBUTF8(
+                                    $property['end_visible']
+                                );
+                                $params['to_user_id'] = self::DBUTF8(
+                                    $property['to_user_id']
+                                );
+                                //$params['to_group_id'] = 'NULL';
+
+                                $id = Database::insert($table, $params);
+                                if ($id) {
+                                    $sql = "UPDATE $table SET id = iid WHERE iid = $id";
+                                    Database::query($sql);
+                                }
                             }
                         }
                     }
@@ -868,7 +893,7 @@ class CourseRestorer
 
                             $params = [
                                 'c_id' => $this->destination_course_id,
-                                'path' => "/".self::DBUTF8escapestring(substr($new_file_name, 9)),
+                                'path' => "/".self::DBUTF8("/".substr($document->path, 9)),
                                 'comment'=> self::DBUTF8($document->comment),
                                 'title' => self::DBUTF8($document->title),
                                 'filetype' => self::DBUTF8($document->file_type),
@@ -1087,7 +1112,7 @@ class CourseRestorer
                     }
                 }
                 if ($forum_cat && !$forum_cat->is_restored()) {
-                    $title = $forum_cat->obj->cat_title;
+                    /*$title = $forum_cat->obj->cat_title;
                     if (!empty($title)) {
                         if (!preg_match('/.*\((.+)\)$/', $title, $matches)) {
                             // This is for avoiding repetitive adding of training code after several backup/restore cycles.
@@ -1095,7 +1120,7 @@ class CourseRestorer
                                 $title = $title.' ('.$this->course->code.')';
                             }
                         }
-                    }
+                    }*/
                     $params = (array) $forum_cat->obj;
                     $params['c_id'] = $this->destination_course_id;
                     $params['cat_comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
@@ -1237,9 +1262,14 @@ class CourseRestorer
 		if ($this->course->has_resources(RESOURCE_LINK)) {
 			$link_table = Database :: get_course_table(TABLE_LINK);
 			$resources = $this->course->resources;
+
 			foreach ($resources[RESOURCE_LINK] as $id => $link) {
-				$cat_id = $this->restore_link_category($link->category_id, $session_id);
-				$sql = "SELECT MAX(display_order) FROM  $link_table
+                $cat_id = $this->restore_link_category(
+                    $link->category_id,
+                    $session_id
+                );
+				$sql = "SELECT MAX(display_order)
+				        FROM $link_table
 				        WHERE
 				            c_id = ".$this->destination_course_id." AND
 				            category_id='" . intval($cat_id). "'";
@@ -1264,9 +1294,21 @@ class CourseRestorer
                 if ($id) {
                     $sql = "UPDATE $link_table SET id = iid WHERE iid = $id";
                     Database::query($sql);
-                }
 
-                $this->course->resources[RESOURCE_LINK][$id]->destination_id = $id;
+                    api_item_property_update(
+                        $this->destination_course_info,
+                        TOOL_LINK,
+                        $id,
+                        'LinkAdded',
+                        api_get_user_id()
+                    );
+
+                    if (!isset($this->course->resources[RESOURCE_LINK][$id])) {
+                        $this->course->resources[RESOURCE_LINK][$id] = new stdClass();
+                    }
+                    $this->course->resources[RESOURCE_LINK][$id]->destination_id = $id;
+
+                }
 			}
 		}
 	}
@@ -1304,6 +1346,7 @@ class CourseRestorer
             if ($new_id) {
                 $sql = "UPDATE $link_cat_table SET id = iid WHERE iid = $new_id";
                 Database::query($sql);
+                api_set_default_visibility($new_id, TOOL_LINK_CATEGORY);
             }
 
             $this->course->resources[RESOURCE_LINKCATEGORY][$id]->destination_id = $new_id;
@@ -1347,9 +1390,13 @@ class CourseRestorer
                 if ($id) {
                     $sql = "UPDATE $tool_intro_table SET id = iid WHERE iid = $id";
                     Database::query($sql);
-                }
 
-				$this->course->resources[RESOURCE_TOOL_INTRO][$id]->destination_id = $id;
+                    if (!isset($this->course->resources[RESOURCE_TOOL_INTRO][$id])) {
+                        $this->course->resources[RESOURCE_TOOL_INTRO][$id] = new stdClass();
+                    }
+
+                    $this->course->resources[RESOURCE_TOOL_INTRO][$id]->destination_id = $id;
+                }
 			}
 		}
 	}
@@ -1387,11 +1434,15 @@ class CourseRestorer
                 if ($new_event_id) {
                     $sql = "UPDATE $table SET id = iid WHERE iid = $new_event_id";
                     Database::query($sql);
-                }
 
-				$this->course->resources[RESOURCE_EVENT][$id]->destination_id = $new_event_id;
+                    if (!isset($this->course->resources[RESOURCE_EVENT][$id])) {
+                        $this->course->resources[RESOURCE_EVENT][$id] = new stdClass();
+                    }
+
+                    $this->course->resources[RESOURCE_EVENT][$id]->destination_id = $new_event_id;
+                }
 
-				//Copy event attachment
+				// Copy event attachment
 
 				$origin_path = $this->course->backup_path.'/upload/calendar/';
 				$destination_path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/upload/calendar/';