Browse Source

Fix import course validating foreign keys - refs #8113

Angel Fernando Quiroz Campos 8 years ago
parent
commit
d0e66faeb7

+ 36 - 7
main/inc/lib/api.lib.php

@@ -3583,6 +3583,8 @@ function api_item_property_update(
         return false;
     }
 
+    $em = Database::getManager();
+
     // Definition of variables.
     $tool = Database::escape_string($tool);
     $item_id = intval($item_id);
@@ -3802,14 +3804,41 @@ function api_item_property_update(
 
     // Insert if no entries are found (can only happen in case of $last_edit_type switch is 'default').
     if ($result == false || Database::affected_rows($result) == 0) {
-        $sessionCondition = empty($session_id) ? "NULL" : "'$session_id'";
-        $sql = "INSERT INTO $tableItemProperty (c_id, tool,ref,insert_date,insert_user_id,lastedit_date,lastedit_type, lastedit_user_id, $to_field, visibility, start_visible, end_visible, session_id)
-                VALUES ($course_id, '$tool', $item_id, '$time', $user_id, '$time', '$last_edit_type', $user_id, $toValueCondition, $visibility, $startVisible, $endVisible, $sessionCondition)";
-        Database::query($sql);
-        $id = Database::insert_id();
+        $objCourse = $em->find('ChamiloCoreBundle:Course', intval($course_id));
+        $objTime = new DateTime('now', new DateTimeZone('UTC'));
+        $objUser = $em->find('ChamiloUserBundle:User', intval($user_id));
+        $objGroup = $em->find('ChamiloCourseBundle:CGroupInfo', intval($to_group_id));
+        $objToUser = $em->find('ChamiloUserBundle:User', intval($to_user_id));
+        $objSession = $em->find('ChamiloCoreBundle:Session', intval($session_id));
+
+        $startVisibleDate = !empty($start_visible) ? new DateTime($start_visible, new DateTimeZone('UTC')) : null;
+        $endVisibleDate = !empty($endVisibleDate) ? new DateTime($endVisibleDate, new DateTimeZone('UTC')) : null;
+
+        $cItemProperty = new \Chamilo\CourseBundle\Entity\CItemProperty($objCourse);
+        $cItemProperty
+            ->setTool($tool)
+            ->setRef($item_id)
+            ->setInsertDate($objTime)
+            ->setInsertUser($objUser)
+            ->setLasteditDate($objTime)
+            ->setLasteditType($last_edit_type)
+            ->setGroup($objGroup)
+            ->setToUser($objToUser)
+            ->setVisibility($visibility)
+            ->setStartVisible($startVisibleDate)
+            ->setEndVisible($endVisibleDate)
+            ->setSession($objSession);
+
+        $em->persist($cItemProperty);
+        $em->flush();
+
+        $id = $cItemProperty->getIid();
+
         if ($id) {
-            $sql = "UPDATE $tableItemProperty SET id = iid WHERE iid = $id";
-            Database::query($sql);
+            $cItemProperty->setId($id);
+            $em->merge($cItemProperty);
+            $em->flush();
+
             return false;
         }
     }

+ 2 - 2
src/Chamilo/CourseBundle/Component/CourseCopy/CourseRestorer.php

@@ -2603,9 +2603,9 @@ class CourseRestorer
 						if ($lp->lp_type =='2') {
 							// if is an sco
 							$old_refs[$new_item_id]= $ref;
-						} else {
+						} elseif (isset($new_item_ids[$ref])) {
                             $old_refs[$new_item_id]= $new_item_ids[$ref];
-						}
+                        }
 					}
 
 					$prerequisite_ids[$new_item_id] = $item['prerequisite'];

+ 15 - 5
src/Chamilo/CourseBundle/Entity/CItemProperty.php

@@ -126,12 +126,13 @@ class CItemProperty
 
     /**
      * CItemProperty constructor.
+     * @param Course $course
      */
     public function __construct(Course $course)
     {
         $this->course = $course;
-        $this->insertDate = new \DateTime();
-        $this->lasteditDate = new \DateTime();
+        $this->insertDate = new \DateTime('now', new \DateTimeZone('UTC'));
+        $this->lasteditDate = new \DateTime('now', new \DateTimeZone('UTC'));
     }
 
     /**
@@ -189,7 +190,7 @@ class CItemProperty
      *
      * @return CItemProperty
      */
-    public function setLasteditDate($lasteditDate)
+    public function setLasteditDate(\DateTime $lasteditDate)
     {
         $this->lasteditDate = $lasteditDate;
 
@@ -309,7 +310,7 @@ class CItemProperty
      *
      * @return CItemProperty
      */
-    public function setStartVisible($startVisible)
+    public function setStartVisible(\DateTime $startVisible = null)
     {
         $this->startVisible = $startVisible;
 
@@ -333,7 +334,7 @@ class CItemProperty
      *
      * @return CItemProperty
      */
-    public function setEndVisible($endVisible)
+    public function setEndVisible(\DateTime $endVisible = null)
     {
         $this->endVisible = $endVisible;
 
@@ -474,4 +475,13 @@ class CItemProperty
 
         return $this;
     }
+
+    /**
+     * Get iid
+     * @return int
+     */
+    public function getIid()
+    {
+        return $this->iid;
+    }
 }