Browse Source

Add hosting settings in the AccessUrl object

Add Course entity listener to check the hosting settings.
jmontoya 9 years ago
parent
commit
2ce201578a

+ 1 - 0
app/config/parameters.yml.dist

@@ -54,3 +54,4 @@ parameters:
 
     # Hide fields in the main/user/user.php page
     # hide_user_field_from_list : ['username']
+    hosting_total_size_limit: 0

+ 5 - 2
main/admin/course_add.php

@@ -1,8 +1,6 @@
 <?php
 /* For licensing terms, see /license.txt */
 
-use Chamilo\CoreBundle\Framework\Container;
-
 /**
  *	@package chamilo.admin
  */
@@ -186,5 +184,10 @@ if ($form->validate()) {
 // Display the form.
 $content = $form->returnForm();
 
+$repo = Database::getManager()->getRepository('ChamiloCoreBundle:Course');
+$url = Database::getManager()->getRepository('ChamiloCoreBundle:AccessUrlRelCourse')->find(1);
+$limit = $repo->getCountActiveCoursesByUrl($url);
+var_dump($limit );
+
 echo $content;
 

+ 22 - 16
main/admin/course_edit.php

@@ -271,7 +271,6 @@ $form->setDefaults($courseInfo);
 // Validate form
 if ($form->validate()) {
     $course = $form->getSubmitValues();
-
     $visibility = $course['visibility'];
 
     global $_configuration;
@@ -331,21 +330,28 @@ if ($form->validate()) {
         $department_url = 'http://' . $department_url;
     }
 
-    Database::query($sql);
-
-    $params = [
-        'course_language' => $course_language,
-        'title' => $title,
-        'category_code' => $category_code,
-        'visual_code' => $visual_code,
-        'department_name' => $department_name,
-        'department_url' => $department_url,
-        'disk_quota' => $disk_quota,
-        'visibility' => $visibility,
-        'subscribe' => $subscribe,
-        'unsubscribe' => $unsubscribe,
-    ];
-    Database::update($course_table, $params, ['id = ?' => $courseId]);
+    $manager = Database::getManager();
+    $courseObj = $manager->getRepository('ChamiloCoreBundle:Course')->find($courseId);
+    if ($courseObj) {
+        $url = $manager->getRepository('ChamiloCoreBundle:AccessUrl')->find(api_get_current_access_url_id());
+
+        $courseObj
+            ->setTitle($title)
+            ->setCourseLanguage($course_language)
+            ->setCategoryCode($category_code)
+            ->setVisualCode($visual_code)
+            ->setDepartmentName($department_name)
+            ->setDepartmentUrl($department_url)
+            ->setDiskQuota(intval($disk_quota))
+            ->setVisibility($visibility)
+            ->setSubscribe(intval($subscribe))
+            ->setUnsubscribe(intval($unsubscribe))
+            ->setCurrentUrl($url)
+        ;
+
+        $manager->persist($courseObj);
+        $manager->flush();
+    }
 
     // update the extra fields
     $courseFieldValue = new ExtraFieldValue('course');

+ 6 - 0
main/inc/lib/add_course.lib.inc.php

@@ -3,6 +3,7 @@
 
 use Chamilo\CourseBundle\Entity\CTool;
 use Chamilo\CoreBundle\Entity\Course;
+use Chamilo\CoreBundle\Entity\AccessUrlRelCourse;
 
 /**
  * Class AddCourse
@@ -1162,6 +1163,10 @@ class AddCourse
         if ($ok_to_register_course) {
 
             $manager = Database::getManager();
+            $url = $manager->getRepository('ChamiloCoreBundle:AccessUrl')->find(api_get_current_access_url_id());
+
+            $accessRelCourse = new AccessUrlRelCourse();
+            $accessRelCourse->setUrl($url);
 
             $course = new Course();
             $course
@@ -1180,6 +1185,7 @@ class AddCourse
                 ->setDepartmentUrl($department_url)
                 ->setSubscribe(intval($subscribe))
                 ->setVisualCode($visual_code)
+                ->addUrls($accessRelCourse)
             ;
 
             $manager->persist($course);

+ 7 - 2
src/Chamilo/CoreBundle/Admin/AccessUrlAdmin.php

@@ -24,10 +24,15 @@ class AccessUrlAdmin extends Admin
     {
         $formMapper
             ->add('url', 'url')
-            //->add('code') //if no type is specified, SonataAdminBundle tries to guess it
             ->add('description', 'ckeditor')
             ->add('active')
-            ->add('url_type', 'text')
+            ->add('limitCourses')
+            ->add('limitActiveCourses')
+            ->add('limitSessions')
+            ->add('limitUsers')
+            ->add('limitTeachers')
+            ->add('limitDiskSpace')
+            ->add('email', 'email')
         ;
     }
 

+ 233 - 0
src/Chamilo/CoreBundle/Entity/AccessUrl.php

@@ -64,6 +64,55 @@ class AccessUrl
      */
     private $urlType;
 
+    /**
+     * @var integer
+     *
+     * @ORM\Column(name="limit_courses", type="integer", nullable=true, unique=false)
+     */
+    private $limitCourses;
+
+    /**
+     * @var integer
+     *
+     * @ORM\Column(name="limit_active_courses", type="integer", nullable=true, unique=false)
+     */
+    private $limitActiveCourses;
+
+    /**
+     * @var integer
+     *
+     * @ORM\Column(name="limit_sessions", type="integer", nullable=true, unique=false)
+     */
+    private $limitSessions;
+
+    /**
+     * @var integer
+     *
+     * @ORM\Column(name="limit_users", type="integer", nullable=true, unique=false)
+     */
+    private $limitUsers;
+
+    /**
+     * @var integer
+     *
+     * @ORM\Column(name="limit_teachers", type="integer", nullable=true, unique=false)
+     */
+    private $limitTeachers;
+
+    /**
+     * @var integer
+     *
+     * @ORM\Column(name="limit_disk_space", type="integer", nullable=true, unique=false)
+     */
+    private $limitDiskSpace;
+
+    /**
+     * @var string
+     *
+     * @ORM\Column(name="email", type="string", length=255, nullable=true, unique=false)
+     */
+    private $email;
+
     /**
      * @ORM\OneToMany(targetEntity="AccessUrlRelCourse", mappedBy="url", cascade={"persist"}, orphanRemoval=true)
      **/
@@ -243,4 +292,188 @@ class AccessUrl
     {
         return $this->urlType;
     }
+
+    /**
+     * @return int
+     */
+    public function getLimitActiveCourses()
+    {
+        return $this->limitActiveCourses;
+    }
+
+    /**
+     * @param int $limitActiveCourses
+     * @return AccessUrl
+     */
+    public function setLimitActiveCourses($limitActiveCourses)
+    {
+        $this->limitActiveCourses = $limitActiveCourses;
+        return $this;
+    }
+
+    /**
+     * @return int
+     */
+    public function getLimitSessions()
+    {
+        return $this->limitSessions;
+    }
+
+    /**
+     * @param int $limitSessions
+     * @return AccessUrl
+     */
+    public function setLimitSessions($limitSessions)
+    {
+        $this->limitSessions = $limitSessions;
+        return $this;
+    }
+
+    /**
+     * @return int
+     */
+    public function getLimitUsers()
+    {
+        return $this->limitUsers;
+    }
+
+    /**
+     * @param int $limitUsers
+     * @return AccessUrl
+     */
+    public function setLimitUsers($limitUsers)
+    {
+        $this->limitUsers = $limitUsers;
+        return $this;
+    }
+
+    /**
+     * @return int
+     */
+    public function getLimitTeachers()
+    {
+        return $this->limitTeachers;
+    }
+
+    /**
+     * @param int $limitTeachers
+     * @return AccessUrl
+     */
+    public function setLimitTeachers($limitTeachers)
+    {
+        $this->limitTeachers = $limitTeachers;
+        return $this;
+    }
+
+    /**
+     * @return int
+     */
+    public function getLimitDiskSpace()
+    {
+        return $this->limitDiskSpace;
+    }
+
+    /**
+     * @param int $limitDiskSpace
+     * @return AccessUrl
+     */
+    public function setLimitDiskSpace($limitDiskSpace)
+    {
+        $this->limitDiskSpace = $limitDiskSpace;
+        return $this;
+    }
+
+    /**
+     * @return string
+     */
+    public function getEmail()
+    {
+        return $this->email;
+    }
+
+    /**
+     * @param string $email
+     * @return AccessUrl
+     */
+    public function setEmail($email)
+    {
+        $this->email = $email;
+        return $this;
+    }
+
+    /**
+     * @return mixed
+     */
+    public function getCourse()
+    {
+        return $this->course;
+    }
+
+    /**
+     * @param mixed $course
+     * @return AccessUrl
+     */
+    public function setCourse($course)
+    {
+        $this->course = $course;
+        return $this;
+    }
+
+    /**
+     * @return mixed
+     */
+    public function getSettings()
+    {
+        return $this->settings;
+    }
+
+    /**
+     * @param mixed $settings
+     * @return AccessUrl
+     */
+    public function setSettings($settings)
+    {
+        $this->settings = $settings;
+        return $this;
+    }
+
+    /**
+     * @return mixed
+     */
+    public function getSessionCategory()
+    {
+        return $this->sessionCategory;
+    }
+
+    /**
+     * @param mixed $sessionCategory
+     * @return AccessUrl
+     */
+    public function setSessionCategory($sessionCategory)
+    {
+        $this->sessionCategory = $sessionCategory;
+        return $this;
+    }
+
+    /**
+     * @return int
+     */
+    public function getLimitCourses()
+    {
+        return $this->limitCourses;
+    }
+
+    /**
+     * @param int $limitCourses
+     * @return AccessUrl
+     */
+    public function setLimitCourses($limitCourses)
+    {
+        $this->limitCourses = $limitCourses;
+        return $this;
+    }
+
+
+
+
 }

+ 4 - 5
src/Chamilo/CoreBundle/Entity/AccessUrlRelCourse.php

@@ -66,24 +66,23 @@ class AccessUrlRelCourse
     }
 
     /**
-     * @return mixed
+     * @return AccessUrl
      */
     public function getUrl()
     {
         return $this->url;
     }
 
-
     /**
-     * @param $course
+     * @param Course $course
      */
-    public function setCourse($course)
+    public function setCourse(Course $course)
     {
         $this->course = $course;
     }
 
     /**
-     * @return mixed
+     * @return Course
      */
     public function getCourse()
     {

+ 45 - 1
src/Chamilo/CoreBundle/Entity/Course.php

@@ -282,6 +282,11 @@ class Course
      **/
     protected $currentSession;
 
+    /**
+     * @var AccessUrl
+     **/
+    protected $currentUrl;
+
     /**
      * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\SkillRelUser", mappedBy="course", cascade={"persist"})
      */
@@ -342,6 +347,7 @@ class Course
         $this->lastEdit = new \DateTime();
 
         $this->users = new ArrayCollection();
+        $this->urls = new ArrayCollection();
         $this->gradebookCategories = new ArrayCollection();
         $this->gradebookEvaluations = new ArrayCollection();
         $this->gradebookLinks = new ArrayCollection();
@@ -1266,12 +1272,50 @@ class Course
     public function setCurrentSession(Session $session)
     {
         // If the session is registered in the course session list.
-        if ($this->getSessions()->contains($session->getId())) {
+        /*if ($this->getSessions()->contains($session->getId())) {
             $this->currentSession = $session;
+        }*/
+
+        $list = $this->getSessions();
+        /** @var SessionRelCourse $item */
+        foreach ($list as $item) {
+            if ($item->getSession()->getId() == $session->getId()) {
+                $this->currentSession = $session;
+                break;
+            }
         }
+
         return $this;
     }
 
+    /**
+     * @param AccessUrl $url
+     *
+     * @return $this
+     */
+    public function setCurrentUrl(AccessUrl $url)
+    {
+        $urlList = $this->getUrls();
+        /** @var AccessUrlRelCourse $item */
+        foreach ($urlList as $item) {
+            if ($item->getUrl()->getId() == $url->getId()) {
+                $this->currentUrl = $url;
+                break;
+            }
+        }
+
+        return $this;
+    }
+
+    /**
+     * @return AccessUrl
+     */
+    public function getCurrentUrl()
+    {
+        return $this->currentUrl;
+    }
+
+
     /**
      * Get issuedSkills
      * @return ArrayCollection

+ 70 - 0
src/Chamilo/CoreBundle/Entity/Listener/CourseListener.php

@@ -3,6 +3,8 @@
 
 namespace Chamilo\CoreBundle\Entity\Listener;
 
+use Chamilo\CoreBundle\Entity\AccessUrl;
+use Chamilo\CoreBundle\Entity\AccessUrlRelCourse;
 use Chamilo\CoreBundle\Entity\Tool;
 use Chamilo\CourseBundle\ToolChain;
 use Doctrine\ORM\Event\LifecycleEventArgs;
@@ -27,13 +29,43 @@ class CourseListener
     }
 
     /**
+     * This code is executed when a new course is created.
+     *
      * new object : prePersist
      * edited object: preUpdate
+     *
      * @param Course $course
      * @param LifecycleEventArgs $args
+     *
+     * @throws \Exception
      */
     public function prePersist(Course $course, LifecycleEventArgs $args)
     {
+        /** @var AccessUrlRelCourse $urlRelCourse */
+        $urlRelCourse = $course->getUrls()->first();
+        $url = $urlRelCourse->getUrl();
+
+        $repo = $args->getEntityManager()->getRepository('ChamiloCoreBundle:Course');
+        $limit = $url->getLimitCourses();
+
+        if (!empty($limit)) {
+            $count = $repo->getCountCoursesByUrl($url);
+            if ($count >= $limit) {
+                throw new \Exception('Limit courses reached');
+            }
+        }
+
+        if ($course->getVisibility() != COURSE_VISIBILITY_HIDDEN) {
+            $limit = $url->getLimitActiveCourses();
+
+            if (!empty($limit)) {
+                $count = $repo->getCountActiveCoursesByUrl($url);
+                if ($count >= $limit) {
+                    throw new \Exception('Limit active courses reached');
+                }
+            }
+        }
+
         $this->toolChain->addToolsInCourse($course);
         /*
         error_log('ddd');
@@ -42,4 +74,42 @@ class CourseListener
         $args->getEntityManager()->persist($course);
         $args->getEntityManager()->flush();*/
     }
+
+    /**
+     * This code is executed when a course is updated
+     *
+     * @param Course $course
+     * @param LifecycleEventArgs $args
+     */
+    public function preUpdate(Course $course, LifecycleEventArgs $args)
+    {
+        $url = $course->getCurrentUrl();
+
+        $repo = $args->getEntityManager()->getRepository('ChamiloCoreBundle:Course');
+        $limit = $url->getLimitCourses();
+
+        if (!empty($limit)) {
+            $count = $repo->getCountCoursesByUrl($url);
+            if ($count >= $limit) {
+                throw new \Exception('Limit courses reached');
+            }
+        }
+
+        if ($course->getVisibility() != COURSE_VISIBILITY_HIDDEN) {
+            $limit = $url->getLimitActiveCourses();
+
+            if (!empty($limit)) {
+                $count = $repo->getCountActiveCoursesByUrl($url);
+                if ($count >= $limit) {
+                    throw new \Exception('Limit active courses reached');
+                }
+            }
+        }
+
+        /*if ($eventArgs->getEntity() instanceof User) {
+            if ($eventArgs->hasChangedField('name') && $eventArgs->getNewValue('name') == 'Alice') {
+                $eventArgs->setNewValue('name', 'Bob');
+            }
+        }*/
+    }
 }

+ 35 - 0
src/Chamilo/CoreBundle/Entity/Repository/CourseRepository.php

@@ -3,6 +3,7 @@
 
 namespace Chamilo\CoreBundle\Entity\Repository;
 
+use Chamilo\CoreBundle\Entity\AccessUrl;
 use Chamilo\CoreBundle\Entity\Course;
 use Doctrine\ORM\EntityRepository;
 use Doctrine\ORM\Query\Expr\Join;
@@ -86,6 +87,40 @@ class CourseRepository extends EntityRepository
         return $queryBuilder;
     }
 
+    /**
+     * Get number of courses in URL
+     * @param AccessUrl $url
+     *
+     * @return int
+     */
+    public function getCountCoursesByUrl(AccessUrl $url)
+    {
+        return $this->createQueryBuilder('a')
+            ->select('COUNT(a)')
+            ->innerJoin('c.urls', 'u')
+            ->where('u = :u')
+            ->setParameters(['u' => $url])
+            ->getQuery()
+            ->getSingleScalarResult();
+    }
+
+    /**
+     * Get number of active courses in URL
+     * @param AccessUrl $url
+     *
+     * @return int
+     */
+    public function getCountActiveCoursesByUrl(AccessUrl $url)
+    {
+        return $this->createQueryBuilder('c')
+            ->select('COUNT(c)')
+            ->innerJoin('c.urls', 'u')
+            ->where('c.visibility <> :visibility AND u = :u')
+            ->setParameters(['visibility' => COURSE_VISIBILITY_HIDDEN, 'u' => $url])
+            ->getQuery()
+            ->getSingleScalarResult();
+    }
+
     /**
      *
      * Gets the teachers subscribed in the course

+ 29 - 0
src/Chamilo/CoreBundle/Migrations/Schema/V2_0_0/Version20160217145628.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace Chamilo\CoreBundle\Migrations\Schema\V_2_0_0;
+
+use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
+use Doctrine\DBAL\Schema\Schema;
+use Oro\Bundle\MigrationBundle\Migration\QueryBag;
+
+/**
+ * Auto-generated Migration: Please modify to your needs!
+ */
+class Version20160217145628 extends AbstractMigrationChamilo
+{
+    /**
+     * @param Schema $schema
+     */
+    public function up(Schema $schema, QueryBag $queries)
+    {
+        $queries->addQuery('ALTER TABLE access_url ADD limit_courses INT DEFAULT NULL, ADD limit_active_courses INT DEFAULT NULL, ADD limit_sessions INT DEFAULT NULL, ADD limit_users INT DEFAULT NULL, ADD limit_teachers INT DEFAULT NULL, ADD limit_disk_space INT DEFAULT NULL, ADD email VARCHAR(255) DEFAULT NULL');
+    }
+
+    /**
+     * @param Schema $schema
+     */
+    public function down(Schema $schema, QueryBag $queries)
+    {
+        $queries->addQuery('ALTER TABLE access_url DROP limit_courses, DROP limit_active_courses, DROP limit_sessions, DROP limit_users, DROP limit_teachers, DROP limit_disk_space, DROP email');
+    }
+}