Ver código fonte

Fix query see BT#12735

The problem.
Course A is accessible on URL 1 and URL 2
Session S i created on URL 2 and include Course A
on the page plugin/buycourses/src/configuration.php on URL 1 the course does not appear.

Expected result

Course A should appear on the page plugin/buycourses/src/configuration.php
on URL 1 and should not appear on URL 2 (because it is included in a session of this URL).
jmontoyaa 7 anos atrás
pai
commit
7d70c2ef81
1 arquivos alterados com 98 adições e 77 exclusões
  1. 98 77
      plugin/buycourses/src/buy_course_plugin.class.php

+ 98 - 77
plugin/buycourses/src/buy_course_plugin.class.php

@@ -3,6 +3,7 @@
 
 use Chamilo\CoreBundle\Entity\Session;
 use Chamilo\CoreBundle\Entity\Course;
+use Doctrine\ORM\Query\Expr\Join;
 
 /**
  * Plugin class for the BuyCourses plugin
@@ -83,7 +84,7 @@ class BuyCoursesPlugin extends Plugin
                 'transfer_enable' => 'boolean',
                 'culqi_enable' => 'boolean',
                 'commissions_enable' => 'boolean',
-                'unregistered_users_enable' => 'boolean'
+                'unregistered_users_enable' => 'boolean',
             )
         );
     }
@@ -114,7 +115,7 @@ class BuyCoursesPlugin extends Plugin
             self::TABLE_PAYPAL_PAYOUTS,
             self::TABLE_SERVICES,
             self::TABLE_SERVICES_SALE,
-            self::TABLE_GLOBAL_CONFIG
+            self::TABLE_GLOBAL_CONFIG,
         );
         $em = Database::getManager();
         $cn = $em->getConnection();
@@ -145,7 +146,7 @@ class BuyCoursesPlugin extends Plugin
             self::TABLE_PAYPAL_PAYOUTS,
             self::TABLE_SERVICES_SALE,
             self::TABLE_SERVICES,
-            self::TABLE_GLOBAL_CONFIG
+            self::TABLE_GLOBAL_CONFIG,
         );
 
         foreach ($tablesToBeDeleted as $tableToBeDeleted) {
@@ -219,7 +220,7 @@ class BuyCoursesPlugin extends Plugin
             '*',
             Database::get_main_table(self::TABLE_CURRENCY),
             [
-                'where' => ['status = ?' => true]
+                'where' => ['status = ?' => true],
             ],
             'first'
         );
@@ -271,7 +272,7 @@ class BuyCoursesPlugin extends Plugin
                 'username' => $params['username'],
                 'password' => $params['password'],
                 'signature' => $params['signature'],
-                'sandbox' => isset($params['sandbox'])
+                'sandbox' => isset($params['sandbox']),
             ],
             ['id = ?' => 1]
         );
@@ -303,7 +304,7 @@ class BuyCoursesPlugin extends Plugin
             [
                 'name' => $params['tname'],
                 'account' => $params['taccount'],
-                'swift' => $params['tswift']
+                'swift' => $params['tswift'],
             ]
         );
     }
@@ -339,30 +340,50 @@ class BuyCoursesPlugin extends Plugin
      */
     private function getCourses()
     {
-        $entityManager = Database::getManager();
-        $query = $entityManager->createQueryBuilder();
+        $em = Database::getManager();
         $urlId = api_get_current_access_url_id();
 
-        $courses = $query
+        $qb = $em->createQueryBuilder();
+        $qb2 = $em->createQueryBuilder();
+        $qb3 = $em->createQueryBuilder();
+
+        $qb = $qb
             ->select('c')
             ->from('ChamiloCoreBundle:Course', 'c')
-            ->leftJoin(
-                'ChamiloCoreBundle:SessionRelCourse',
-                'sc',
-                \Doctrine\ORM\Query\Expr\Join::WITH,
-                'c = sc.course'
-            )->innerJoin(
-                'ChamiloCoreBundle:AccessUrlRelCourse',
-                'ac',
-                \Doctrine\ORM\Query\Expr\Join::WITH,
-                'c = ac.course'
+            ->where(
+                $qb->expr()->notIn(
+                    'c',
+                    $qb2
+                        ->select('course2')
+                        ->from('ChamiloCoreBundle:SessionRelCourse', 'sc')
+                        ->join('sc.course', 'course2')
+                        ->innerJoin(
+                            'ChamiloCoreBundle:AccessUrlRelSession',
+                            'us',
+                            Join::WITH,
+                            'us.sessionId = sc.session'
+                        )->where(
+                            $qb->expr()->eq('us.accessUrlId ', $urlId)
+                        )
+                        ->getDQL()
+                )
+            )->andWhere(
+                $qb->expr()->in(
+                    'c',
+                    $qb3
+                        ->select('course3')
+                        ->from('ChamiloCoreBundle:AccessUrlRelCourse', 'uc')
+                        ->join('uc.course', 'course3')
+                        ->where(
+                            $qb3->expr()->eq('uc.url ', $urlId)
+                        )
+                        ->getDQL()
+                )
             )
-            ->where($query->expr()->isNull('sc.course'))
-            ->andWhere($query->expr()->eq('ac.url', $urlId))
-            ->getQuery()
-            ->getResult();
+            ->getQuery();
 
-        return $courses;
+        $courses = $qb->getResult();
+           return $courses;
     }
 
     /**
@@ -389,9 +410,9 @@ class BuyCoursesPlugin extends Plugin
                 'where' => [
                     'i.product_id = ? AND i.product_type = ?' => [
                         intval($productId),
-                        intval($itemType)
-                    ]
-                ]
+                        intval($itemType),
+                    ],
+                ],
             ],
             'first'
         );
@@ -463,9 +484,9 @@ class BuyCoursesPlugin extends Plugin
                         $userId,
                         self::PRODUCT_TYPE_SESSION,
                         $session->getId(),
-                        self::SALE_STATUS_PENDING
-                    ]
-                ]
+                        self::SALE_STATUS_PENDING,
+                    ],
+                ],
             ],
             'first'
         );
@@ -477,7 +498,7 @@ class BuyCoursesPlugin extends Plugin
         // Check if user is already subscribe to session
         $userSubscription = $scuRepo->findBy([
             'session' => $session,
-            'user' => $userId
+            'user' => $userId,
         ]);
 
         if (!empty($userSubscription)) {
@@ -523,7 +544,7 @@ class BuyCoursesPlugin extends Plugin
 
                 $sessionCourseData = [
                     'title' => $course->getTitle(),
-                    'coaches' => []
+                    'coaches' => [],
                 ];
 
                 $userCourseSubscriptions = $session->getUserCourseSubscriptionsByStatus(
@@ -573,9 +594,9 @@ class BuyCoursesPlugin extends Plugin
                         $userId,
                         self::PRODUCT_TYPE_COURSE,
                         $course->getId(),
-                        self::SALE_STATUS_PENDING
-                    ]
-                ]
+                        self::SALE_STATUS_PENDING,
+                    ],
+                ],
             ],
             'first'
         );
@@ -587,7 +608,7 @@ class BuyCoursesPlugin extends Plugin
         // Check if user is already subscribe to course
         $userSubscription = $cuRepo->findBy([
             'course' => $course,
-            'user' => $userId
+            'user' => $userId,
         ]);
 
         if (!empty($userSubscription)) {
@@ -631,7 +652,7 @@ class BuyCoursesPlugin extends Plugin
                 'price' => $item['price'],
                 'currency' => $item['iso_code'],
                 'teachers' => [],
-                'enrolled' => $this->getUserStatusForCourse(api_get_user_id(), $course)
+                'enrolled' => $this->getUserStatusForCourse(api_get_user_id(), $course),
             ];
 
             foreach ($course->getTeachers() as $courseUser) {
@@ -685,7 +706,7 @@ class BuyCoursesPlugin extends Plugin
             'teachers' => [],
             'price' => $item['price'],
             'currency' => $item['iso_code'],
-            'course_img' => null
+            'course_img' => null,
         ];
 
         $courseTeachers = $course->getTeachers();
@@ -733,7 +754,7 @@ class BuyCoursesPlugin extends Plugin
             'access_start_date' => $session->getAccessStartDate(),
             'access_end_date' => $session->getAccessEndDate(),
             'coach_access_start_date' => $session->getCoachAccessStartDate(),
-            'coach_access_end_date' => $session->getCoachAccessEndDate()
+            'coach_access_end_date' => $session->getCoachAccessEndDate(),
         ]);
 
         $sessionInfo = [
@@ -743,7 +764,7 @@ class BuyCoursesPlugin extends Plugin
             'courses' => [],
             'price' => $item['price'],
             'currency' => $item['iso_code'],
-            'image' => null
+            'image' => null,
         ];
 
         $fieldValue = new ExtraFieldValue('session');
@@ -763,7 +784,7 @@ class BuyCoursesPlugin extends Plugin
 
             $sessionCourseData = [
                 'title' => $course->getTitle(),
-                'coaches' => []
+                'coaches' => [],
             ];
 
             $userCourseSubscriptions = $session->getUserCourseSubscriptionsByStatus(
@@ -793,7 +814,7 @@ class BuyCoursesPlugin extends Plugin
             '*',
             Database::get_main_table(self::TABLE_ITEM),
             [
-                'where' => ['id = ?' => intval($itemId)]
+                'where' => ['id = ?' => intval($itemId)],
             ],
             'first'
         );
@@ -850,7 +871,7 @@ class BuyCoursesPlugin extends Plugin
             'product_id' => $item['product_id'],
             'price' => $item['price'],
             'status' => self::SALE_STATUS_PENDING,
-            'payment_type' => intval($paymentType)
+            'payment_type' => intval($paymentType),
         ];
 
         return Database::insert(self::TABLE_SALE, $values);
@@ -867,7 +888,7 @@ class BuyCoursesPlugin extends Plugin
             '*',
             Database::get_main_table(self::TABLE_SALE),
             [
-                'where' => ['id = ?' => intval($saleId)]
+                'where' => ['id = ?' => intval($saleId)],
             ],
             'first'
         );
@@ -894,7 +915,7 @@ class BuyCoursesPlugin extends Plugin
             "$saleTable s $innerJoins",
             [
                 'where' => ['s.payment_type = ? AND s.status = ?' => [intval($paymentType), self::SALE_STATUS_COMPLETED]],
-                'order' => 'id DESC'
+                'order' => 'id DESC',
             ]
         );
     }
@@ -910,7 +931,7 @@ class BuyCoursesPlugin extends Plugin
             '*',
             Database::get_main_table(self::TABLE_CURRENCY),
             [
-                'where' => ['id = ?' => intval($currencyId)]
+                'where' => ['id = ?' => intval($currencyId)],
             ],
             'first'
         );
@@ -990,7 +1011,7 @@ class BuyCoursesPlugin extends Plugin
         return [
             self::PAYMENT_TYPE_PAYPAL => 'PayPal',
             self::PAYMENT_TYPE_TRANSFER => $this->get_lang('BankTransfer'),
-            self::PAYMENT_TYPE_CULQI => 'Culqi'
+            self::PAYMENT_TYPE_CULQI => 'Culqi',
         ];
     }
 
@@ -1015,7 +1036,7 @@ class BuyCoursesPlugin extends Plugin
             "$saleTable s $innerJoins",
             [
                 'where' => ['s.status = ?' => intval($status)],
-                'order' => 'id DESC'
+                'order' => 'id DESC',
             ]
         );
     }
@@ -1029,7 +1050,7 @@ class BuyCoursesPlugin extends Plugin
         return [
             self::SALE_STATUS_CANCELED => $this->get_lang('SaleStatusCanceled'),
             self::SALE_STATUS_PENDING => $this->get_lang('SaleStatusPending'),
-            self::SALE_STATUS_COMPLETED => $this->get_lang('SaleStatusCompleted')
+            self::SALE_STATUS_COMPLETED => $this->get_lang('SaleStatusCompleted'),
         ];
     }
 
@@ -1042,7 +1063,7 @@ class BuyCoursesPlugin extends Plugin
         return [
             self::PAYOUT_STATUS_CANCELED => $this->get_lang('PayoutStatusCanceled'),
             self::PAYOUT_STATUS_PENDING => $this->get_lang('PayoutStatusPending'),
-            self::PAYOUT_STATUS_COMPLETED => $this->get_lang('PayoutStatusCompleted')
+            self::PAYOUT_STATUS_COMPLETED => $this->get_lang('PayoutStatusCompleted'),
         ];
     }
 
@@ -1054,7 +1075,7 @@ class BuyCoursesPlugin extends Plugin
     {
         return [
             self::PRODUCT_TYPE_COURSE => get_lang('Course'),
-            self::PRODUCT_TYPE_SESSION => get_lang('Session')
+            self::PRODUCT_TYPE_SESSION => get_lang('Session'),
         ];
     }
 
@@ -1068,7 +1089,7 @@ class BuyCoursesPlugin extends Plugin
             self::SERVICE_TYPE_USER => get_lang('User'),
             self::SERVICE_TYPE_COURSE => get_lang('Course'),
             self::SERVICE_TYPE_SESSION => get_lang('Session'),
-            self::SERVICE_TYPE_LP_FINAL_ITEM => get_lang('TemplateTitleCertificate')
+            self::SERVICE_TYPE_LP_FINAL_ITEM => get_lang('TemplateTitleCertificate'),
         ];
     }
 
@@ -1094,7 +1115,7 @@ class BuyCoursesPlugin extends Plugin
 
         $innerJoin = "$itemTable i ON s.id = i.product_id";
         $whereConditions = [
-            'i.product_type = ? ' => self::PRODUCT_TYPE_SESSION
+            'i.product_type = ? ' => self::PRODUCT_TYPE_SESSION,
         ];
 
         if (!empty($name)) {
@@ -1151,7 +1172,7 @@ class BuyCoursesPlugin extends Plugin
         $max = floatval($max);
 
         $whereConditions = [
-            'i.product_type = ? ' => self::PRODUCT_TYPE_COURSE
+            'i.product_type = ? ' => self::PRODUCT_TYPE_COURSE,
         ];
 
         if (!empty($name)) {
@@ -1269,9 +1290,9 @@ class BuyCoursesPlugin extends Plugin
                 'where' => [
                     'u.username LIKE %?% OR ' => $term,
                     'u.lastname LIKE %?% OR ' => $term,
-                    'u.firstname LIKE %?%' => $term
+                    'u.firstname LIKE %?%' => $term,
                 ],
-                'order' => 'id DESC'
+                'order' => 'id DESC',
             ]
         );
     }
@@ -1301,9 +1322,9 @@ class BuyCoursesPlugin extends Plugin
             "$saleTable s $innerJoins",
             [
                 'where' => [
-                    'u.id = ? AND s.status = ?' => [intval($id), self::SALE_STATUS_COMPLETED]
+                    'u.id = ? AND s.status = ?' => [intval($id), self::SALE_STATUS_COMPLETED],
                 ],
-                'order' => 'id DESC'
+                'order' => 'id DESC',
             ]
         );
     }
@@ -1325,7 +1346,7 @@ class BuyCoursesPlugin extends Plugin
             'course_visibility' => $course->getVisibility(),
             'visible' => false,
             'currency' =>  empty($defaultCurrency) ? null : $defaultCurrency['iso_code'],
-            'price' => 0.00
+            'price' => 0.00,
         ];
 
         $item = $this->getItemByProduct($course->getId(), self::PRODUCT_TYPE_COURSE);
@@ -1365,7 +1386,7 @@ class BuyCoursesPlugin extends Plugin
             'session_display_end_date' => null,
             'visible' => false,
             'currency' =>  empty($defaultCurrency) ? null : $defaultCurrency['iso_code'],
-            'price' => 0.00
+            'price' => 0.00,
         ];
 
         $displayStartDate = $session->getDisplayStartDate();
@@ -1391,8 +1412,8 @@ class BuyCoursesPlugin extends Plugin
             [
                 'where' => [
                     'i.product_id = ? AND ' => $session->getId(),
-                    'i.product_type = ?' => self::PRODUCT_TYPE_SESSION
-                ]
+                    'i.product_type = ?' => self::PRODUCT_TYPE_SESSION,
+                ],
             ],
             'first'
         );
@@ -1420,7 +1441,7 @@ class BuyCoursesPlugin extends Plugin
             '*',
             $beneficiaryTable,
             ['where' => [
-                'item_id = ?' => intval($itemId)
+                'item_id = ?' => intval($itemId),
             ]]
         );
     }
@@ -1473,7 +1494,7 @@ class BuyCoursesPlugin extends Plugin
             $itemData,
             [
                 'product_id = ? AND ' => intval($productId),
-                'product_type' => $productType
+                'product_type' => $productType,
             ]
         );
     }
@@ -1510,7 +1531,7 @@ class BuyCoursesPlugin extends Plugin
                 [
                     'item_id' => intval($itemId),
                     'user_id' => intval($userId),
-                    'commissions' => intval($commissions)
+                    'commissions' => intval($commissions),
                 ]
             );
         }
@@ -1571,7 +1592,7 @@ class BuyCoursesPlugin extends Plugin
             "*",
             $extraFieldTable,
             [
-                'where' => ['variable = ?' => 'paypal']
+                'where' => ['variable = ?' => 'paypal'],
             ],
             'first'
         );
@@ -1592,7 +1613,7 @@ class BuyCoursesPlugin extends Plugin
             "p.* , u.firstname, u.lastname, efv.value as paypal_account, s.reference as sale_reference, s.price as item_price, c.iso_code",
             "$payoutsTable p $innerJoins",
             [
-                'where' => ['p.status = ? '.$condition.' '.$condition2 => $status]
+                'where' => ['p.status = ? '.$condition.' '.$condition2 => $status],
             ],
             $typeResult
         );
@@ -1614,7 +1635,7 @@ class BuyCoursesPlugin extends Plugin
             "*",
             $extraFieldTable,
             [
-                'where' => ['variable = ?' => 'paypal']
+                'where' => ['variable = ?' => 'paypal'],
             ],
             'first'
         );
@@ -1628,7 +1649,7 @@ class BuyCoursesPlugin extends Plugin
             "value",
             $extraFieldValues,
             [
-                'where' => ['field_id = ? AND item_id = ?' => [intval($paypalFieldId), intval($userId)]]
+                'where' => ['field_id = ? AND item_id = ?' => [intval($paypalFieldId), intval($userId)]],
             ],
             'first'
         );
@@ -1668,7 +1689,7 @@ class BuyCoursesPlugin extends Plugin
                     'sale_id' => intval($saleId),
                     'user_id' => $beneficiary['user_id'],
                     'commission' => number_format((floatval($teachersCommission) * intval($beneficiary['commissions'])) / 100, 2),
-                    'status' => self::PAYOUT_STATUS_PENDING
+                    'status' => self::PAYOUT_STATUS_PENDING,
                 ]
             );
         }
@@ -1742,7 +1763,7 @@ class BuyCoursesPlugin extends Plugin
                 'visibility' => intval($service['visibility']),
                 'image' => '',
                 'video_url' => $service['video_url'],
-                'service_information' => $service['service_information']
+                'service_information' => $service['service_information'],
             ]
         );
 
@@ -1794,7 +1815,7 @@ class BuyCoursesPlugin extends Plugin
                 'visibility' => intval($service['visibility']),
                 'image' => 'simg-'.$id.'.png',
                 'video_url' => $service['video_url'],
-                'service_information' => $service['service_information']
+                'service_information' => $service['service_information'],
             ],
             ['id = ?' => intval($id)]
         );
@@ -1894,7 +1915,7 @@ class BuyCoursesPlugin extends Plugin
         return [
             self::SERVICE_STATUS_CANCELLED => $this->get_lang('SaleStatusCancelled'),
             self::SERVICE_STATUS_PENDING => $this->get_lang('SaleStatusPending'),
-            self::SERVICE_STATUS_COMPLETED => $this->get_lang('SaleStatusCompleted')
+            self::SERVICE_STATUS_COMPLETED => $this->get_lang('SaleStatusCompleted'),
         ];
     }
 
@@ -2083,7 +2104,7 @@ class BuyCoursesPlugin extends Plugin
         $userTable = Database::get_main_table(TABLE_MAIN_USER);
 
         $whereConditions = [
-            's.id <> ? ' => 0
+            's.id <> ? ' => 0,
         ];
 
         if (!empty($name)) {
@@ -2188,7 +2209,7 @@ class BuyCoursesPlugin extends Plugin
             'date_start' => api_get_utc_datetime(),
             'date_end' => date_format(date_add(date_create(api_get_utc_datetime()), date_interval_create_from_date_string($service['duration_days'].' days')), 'Y-m-d H:i:s'),
             'status' => self::SERVICE_STATUS_PENDING,
-            'payment_type' => intval($paymentType)
+            'payment_type' => intval($paymentType),
         ];
 
         $returnedServiceSaleId = Database::insert(self::TABLE_SERVICES_SALE, $values);
@@ -2208,7 +2229,7 @@ class BuyCoursesPlugin extends Plugin
             [
                 'commerce_code' => $params['commerce_code'],
                 'api_key' => $params['api_key'],
-                'integration' => $params['integration']
+                'integration' => $params['integration'],
             ],
             ['id = ?' => 1]
         );
@@ -2238,7 +2259,7 @@ class BuyCoursesPlugin extends Plugin
         return Database::update(
             Database::get_main_table(self::TABLE_GLOBAL_CONFIG),
             [
-                'terms_and_conditions' => $params['terms_and_conditions']
+                'terms_and_conditions' => $params['terms_and_conditions'],
             ],
             ['id = ?' => 1]
         );