Jelajahi Sumber

Inyecting course and session.

Julio Montoya 11 tahun lalu
induk
melakukan
34dc416bbb

+ 24 - 0
main/inc/Entity/CurriculumCategory.php

@@ -115,11 +115,35 @@ class CurriculumCategory
      */
     private $course;
 
+    /**
+     * @ORM\ManyToOne(targetEntity="Session")
+     * @ORM\JoinColumn(name="session_id", referencedColumnName="id")
+     */
+    private $session;
+
     public function __construct()
     {
         $this->items = new ArrayCollection();
     }
 
+    /**
+     * @return mixed
+     */
+    public function getSession()
+    {
+        return $this->session;
+    }
+
+    /**
+     * @param Session $session
+     * @return mixed
+     */
+    public function setSession(Session $session)
+    {
+        $this->session = $session;
+    }
+
+
     /**
      * @return mixed
      */

+ 10 - 6
main/inc/global.inc.php

@@ -492,7 +492,7 @@ use Symfony\Component\Finder\Finder;
 $app->before(
 
     function () use ($app) {
-        // Checking configuration file
+        // Checking configuration file.
         if (!file_exists($app['configuration_file']) && !file_exists($app['configuration_yml_file'])) {
             return new RedirectResponse(api_get_path(WEB_CODE_PATH).'install');
             $app->abort(500, "Configuration file was not found");
@@ -513,6 +513,7 @@ $app->before(
 
         // Starting the session for more info see: http://silex.sensiolabs.org/doc/providers/session.html
         $request->getSession()->start();
+
         /** @var ChamiloLMS\Component\DataFilesystem\DataFilesystem $filesystem */
         $filesystem = $app['chamilo.filesystem'];
 
@@ -521,12 +522,12 @@ $app->before(
             $filesystem->createFolders($app['temp.paths']->folders);
         }
 
-        // If assetic is enabled copy folders from theme inside web/
+        // If Assetic is enabled copy folders from theme inside "web/"
         if ($app['assetic.auto_dump_assets']) {
             $filesystem->copyFolders($app['temp.paths']->copyFolders);
         }
 
-        // Check and modify the date of user in the track.e.online table
+        // Check and modify the date of user in the track.e.online table.
         Online::loginCheck(api_get_user_id());
 
         // Setting access_url id (multiple url feature)
@@ -538,11 +539,12 @@ $app->before(
             Session::write('url_id', 1);
         }
 
-        // Loading portal settings from DB
+        // Loading portal settings from DB.
         $settings_refresh_info = api_get_settings_params_simple(array('variable = ?' => 'settings_latest_update'));
         $settings_latest_update = $settings_refresh_info ? $settings_refresh_info['selected_value'] : null;
 
         $_setting = Session::read('_setting');
+
         if (empty($_setting)) {
             api_set_settings_and_plugins();
         } else {
@@ -630,7 +632,6 @@ $app->before(
 
         /** Translator component. */
         // Platform lang
-
         $language = api_get_setting('platformLanguage');
         $iso = api_get_language_isocode($language);
         $app['translator']->setLocale($iso);
@@ -673,7 +674,6 @@ $app->before(
             'exercice'
         );
 
-
         $languageFilesToAdd = array();
         /* Loading translations depending of the "section" folder after main
            for example the section is exercice here: web/main/exercice/result.php
@@ -789,6 +789,10 @@ $app->before(
             // Converting /courses/XXX/ to a Entity/Course object
             $course = $app['orm.em']->getRepository('Entity\Course')->findOneByCode($course);
             $app['course'] = $course;
+
+            $sessionId = $request->get('id_session');
+            $session = $app['orm.em']->getRepository('Entity\Session')->findOneById($sessionId);
+            $app['course_session'] = $session;
         }
     }
 );

+ 2 - 2
main/inc/services.php

@@ -21,8 +21,8 @@ $app['root_dir'] = $app['root_sys'];
 $app->register(new Flint\Provider\RoutingServiceProvider(), array(
     'routing.resource' => $app['sys_config_path'].'routing.yml',
     'routing.options' => array(
-        'cache_dir' => $app['debug'] == true ? null : $app['sys_temp_path']
-        //'cache_dir' => $app['sys_temp_path']
+        //'cache_dir' => $app['debug'] == true ? null : $app['sys_temp_path']
+        'cache_dir' => $app['sys_temp_path']
     ),
 ));
 

+ 94 - 14
src/ChamiloLMS/Controller/BaseController.php

@@ -9,9 +9,11 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
 use Doctrine\ORM\Query;
 use Doctrine\ORM\NoResultException;
 use Silex\Application;
+
 use Flint\Controller\Controller as FlintController;
 
 use Symfony\Component\Routing\Annotation\Route;
@@ -33,14 +35,18 @@ abstract class BaseController extends FlintController
     public function __construct(Application $app)
     {
         $this->app = $app;
-        // In order to use the Flint Controller
+        // In order to use the Flint Controller.
         $this->pimple = $app;
 
         // Inserting course
         /** @var \Entity\Course $app['course'] */
-        if (isset($app['course'])) {
+        $course = $this->getCourse();
+        if ($course) {
             $template = $this->get('template');
-            $template->assign('course', $app['course']);
+            $template->assign('course', $course);
+
+            $session = $this->getSession();
+            $template->assign('course_session', $session);
         }
     }
 
@@ -49,17 +55,28 @@ abstract class BaseController extends FlintController
      */
     protected function getCourse()
     {
-        if (isset($this->app['course'])) {
+        if (isset($this->app['course']) && !empty($this->app['course'])) {
             return $this->app['course'];
         }
         return false;
     }
 
+    /**
+     * @return \Entity\Session
+     */
+    protected function getSession()
+    {
+        if (isset($this->app['course_session']) && !empty($this->app['course_session'])) {
+            return $this->app['course_session'];
+        }
+        return null;
+    }
+
     /**
      * This method should return the entity's repository.
      *
      * @abstract
-     * @return EntityRepository
+     * @return \Doctrine\ORM\EntityRepository
      */
     abstract protected function getRepository();
 
@@ -103,6 +120,32 @@ abstract class BaseController extends FlintController
         return $this->get('request');
     }
 
+    /**
+     * Get a user from the Security Context
+     *
+     * @return mixed
+     *
+     * @throws \LogicException If SecurityBundle is not available
+     *
+     * @see Symfony\Component\Security\Core\Authentication\Token\TokenInterface::getUser()
+     */
+    public function getUser()
+    {
+        if (!$this->has('security.context')) {
+            throw new \LogicException('The SecurityServiceProvider is not registered in your application.');
+        }
+
+        if (null === $token = $this->get('security.context')->getToken()) {
+            return null;
+        }
+
+        if (!is_object($user = $token->getUser())) {
+            return null;
+        }
+
+        return $user;
+    }
+
     protected function createNotFoundException($message = 'Not Found', \Exception $previous = null)
     {
         return $this->app->abort(404, $message);
@@ -119,18 +162,38 @@ abstract class BaseController extends FlintController
      * @param array
      * @return mixed
      */
-    protected function createUrl($label, $params = array())
+    protected function createUrl($label, $parameters = array())
     {
         $links = $this->generateLinks();
-        $courseCode = $this->getRequest()->get('course');
-        $params['course'] = $courseCode;
+        $course = $this->getCourse();
+        $parameters['course'] = $course->getCode();
+        $session = $this->getSession();
+        if (!empty($session)) {
+            $parameters['id_session'] = $session->getId();
+        }
+
         if (isset($links) && is_array($links) && isset($links[$label])) {
-            $url = $this->generateUrl($links[$label], $params);
+            $url = $this->generateUrl($links[$label], $parameters);
             return $url;
         }
         return $url = $this->generateUrl($links['list_link']);
     }
 
+     /**
+     * @see Symfony\Component\Routing\RouterInterface::generate()
+     */
+    public function generateUrl($name, array $parameters = array(), $reference = UrlGeneratorInterface::ABSOLUTE_PATH)
+    {
+        $course = $this->getCourse();
+        $parameters['course'] = $course->getCode();
+        $session = $this->getSession();
+        if (!empty($session)) {
+            $parameters['id_session'] = $session->getId();
+        }
+
+        return parent::generateUrl($name, $parameters, $reference);
+    }
+
     // CRUD default actions
 
     /**
@@ -290,10 +353,8 @@ abstract class BaseController extends FlintController
      */
     protected function getList($format = 'json')
     {
-        $list = $this->getRepository()
-            ->createQueryBuilder('e')
-            ->getQuery()->getResult(Query::HYDRATE_ARRAY);
-
+        $qb = $this->getRepository()->createQueryBuilder('e');
+        $list = $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);
         switch ($format) {
             case 'json':
                 return new JsonResponse($list);
@@ -304,6 +365,24 @@ abstract class BaseController extends FlintController
         }
     }
 
+    /**
+     * @param \Doctrine\ORM\QueryBuilder $qb
+     */
+    protected function setCourseParameters(\Doctrine\ORM\QueryBuilder & $qb)
+    {
+        $course = $this->getCourse();
+        if ($course) {
+            $qb->andWhere('e.cId = :id');
+            $qb->setParameter('session_id', $course->getId());
+
+            $session = $this->getSession();
+            if (!empty($session)) {
+                $qb->andWhere('e.cId = :session_id');
+                $qb->setParameter('session_id', $session->getId());
+            }
+        }
+    }
+
     /**
      * Base "read" action.
      *
@@ -441,7 +520,8 @@ abstract class BaseController extends FlintController
             return $this->getRepository()->createQueryBuilder('e')
                 ->where('e.id = :id')
                 ->setParameter('id', $id)
-                ->getQuery()->getSingleResult(Query::HYDRATE_ARRAY);
+                ->getQuery()
+                ->getSingleResult(Query::HYDRATE_ARRAY);
         } catch (NoResultException $ex) {
             return false;
         }