parent
818e4b4970
commit
e3df5c252c
@ -0,0 +1,224 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Controller; |
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||||
use Symfony\Component\HttpFoundation\Response; |
||||
use Symfony\Component\HttpFoundation\JsonResponse; |
||||
use Symfony\Component\HttpFoundation\Request; |
||||
use Knp\Menu\Matcher\Matcher; |
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
||||
use Symfony\Component\Routing\Annotation\Route; |
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
||||
use Symfony\Component\DependencyInjection\Container; |
||||
|
||||
use Knp\Menu\FactoryInterface as MenuFactoryInterface; |
||||
use Knp\Menu\ItemInterface as MenuItemInterface; |
||||
use Knp\Menu\Renderer\ListRenderer; |
||||
|
||||
use Sylius\Bundle\ResourceBundle\Controller\ResourceController; |
||||
|
||||
/** |
||||
* Each entity controller must extends this class. |
||||
* |
||||
* @abstract |
||||
*/ |
||||
abstract class BaseResourceController extends ResourceController |
||||
{ |
||||
/** |
||||
* @return \Symfony\Component\Security\Core\SecurityContextInterface |
||||
*/ |
||||
public function getSecurity() |
||||
{ |
||||
return $this->container->get('security.context'); |
||||
} |
||||
|
||||
/** |
||||
* @return object |
||||
*/ |
||||
public function getTemplate() |
||||
{ |
||||
return $this->container->get('templating'); |
||||
} |
||||
|
||||
/** |
||||
* @return NotFoundHttpException |
||||
*/ |
||||
public function abort() |
||||
{ |
||||
return new NotFoundHttpException(); |
||||
} |
||||
|
||||
/** |
||||
* Converts string 'Chamilo\CoreBundle\Controller\Admin\QuestionManager' into |
||||
* 'admin/question_manager' |
||||
*/ |
||||
public function getTemplatePath() |
||||
{ |
||||
$parts = $this->classParts; |
||||
|
||||
$newPath = array(); |
||||
foreach ($parts as $part) { |
||||
if (in_array($part, array('chamilo_lms', 'controller')) |
||||
//strpos($part, '_controller') > 0 |
||||
) { |
||||
continue; |
||||
} |
||||
$newPath[] = $part; |
||||
} |
||||
|
||||
$template = implode('/', $newPath); |
||||
return str_replace('_controller', '', $template); |
||||
} |
||||
|
||||
/** |
||||
* Transforms 'QuestionManagerController' to 'question_manager.controller' |
||||
* @return string |
||||
*/ |
||||
public function getControllerAlias() |
||||
{ |
||||
$parts = $this->classParts; |
||||
$parts = array_reverse($parts); |
||||
$alias = str_replace('_controller', '.controller', $parts[0]); |
||||
return $alias; |
||||
} |
||||
|
||||
/** |
||||
* Translator shortcut |
||||
* @param string $variable |
||||
* @return string |
||||
*/ |
||||
public function trans($variable) |
||||
{ |
||||
return $this->container->get('translator')->trans($variable); |
||||
} |
||||
|
||||
/** |
||||
* Returns the class name label |
||||
* @example RoleController -> Role |
||||
* |
||||
* @return string the class name label |
||||
*/ |
||||
public function getClassNameLabel() |
||||
{ |
||||
return $this->classNameLabel; |
||||
} |
||||
|
||||
/** |
||||
* @return MenuFactoryInterface |
||||
*/ |
||||
public function getMenuFactory() |
||||
{ |
||||
return $this->container->get('knp_menu.factory'); |
||||
} |
||||
|
||||
/** |
||||
* @param string $action |
||||
* @return MenuItemInterface |
||||
*/ |
||||
protected function getBreadcrumbs($action) |
||||
{ |
||||
$breadcrumbs = $this->buildBreadcrumbs($action); |
||||
|
||||
return $breadcrumbs; |
||||
} |
||||
|
||||
/** Main home URL |
||||
* @return MenuItemInterface |
||||
*/ |
||||
protected function getHomeBreadCrumb() |
||||
{ |
||||
$menu = $this->getMenuFactory()->createItem( |
||||
'root', |
||||
array( |
||||
'childrenAttributes' => array( |
||||
'class' => 'breadcrumb', |
||||
'currentClass' => 'active' |
||||
) |
||||
) |
||||
); |
||||
|
||||
$menu->addChild( |
||||
$this->trans('Home'), |
||||
array('uri' => $this->generateUrl('home')) |
||||
); |
||||
|
||||
return $menu; |
||||
} |
||||
|
||||
/** |
||||
* @param $action |
||||
* @param MenuItemInterface $menu |
||||
* @return MenuItemInterface |
||||
*/ |
||||
public function buildBreadcrumbs($action, MenuItemInterface $menu = null) |
||||
{ |
||||
if (!$menu) { |
||||
$menu = $this->getHomeBreadCrumb(); |
||||
} |
||||
|
||||
$menu->addChild( |
||||
$this->trans($this->getClassnameLabel().'List'), |
||||
array('uri' => $this->generateControllerUrl('listingAction')) |
||||
); |
||||
|
||||
$action = str_replace( |
||||
array($this->getControllerAlias().':', 'Action'), |
||||
'', |
||||
$action |
||||
); |
||||
|
||||
switch ($action) { |
||||
case 'add': |
||||
case 'edit': |
||||
$menu->addChild( |
||||
$this->trans($this->getClassnameLabel().ucfirst($action)) |
||||
//array('uri' => $this->generateControllerUrl($action.'Action')) |
||||
); |
||||
break; |
||||
} |
||||
|
||||
return $menu; |
||||
} |
||||
|
||||
/** |
||||
* @param array $breadCrumbList |
||||
* @return string |
||||
*/ |
||||
protected function parseLegacyBreadCrumb($breadCrumbList = array()) |
||||
{ |
||||
$menu = $this->getHomeBreadCrumb(); |
||||
foreach ($breadCrumbList as $item) { |
||||
$menu->addChild( |
||||
$this->trans($item['title']), |
||||
array('uri' => $item['url']) |
||||
); |
||||
} |
||||
|
||||
$renderer = new ListRenderer(new \Knp\Menu\Matcher\Matcher()); |
||||
$result = $renderer->render($menu); |
||||
|
||||
return $result; |
||||
} |
||||
|
||||
/** |
||||
* Renders the current controller template |
||||
* @param string $name |
||||
* @param array $elements |
||||
* @return mixed |
||||
*/ |
||||
public function renderTemplate($name, $elements = array()) |
||||
{ |
||||
$name = $this->getTemplatePath().'/'.$name; |
||||
|
||||
$renderer = new ListRenderer(new \Knp\Menu\Matcher\Matcher()); |
||||
$action = $this->getRequest()->get('_route'); |
||||
$result = $renderer->render($this->getBreadcrumbs($action)); |
||||
$elements['new_breadcrumb'] = $result; |
||||
|
||||
return $this->getTemplate()->renderTemplate($name, $elements); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,220 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CourseBundle\Controller; |
||||
|
||||
use Chamilo\CoreBundle\Controller\BaseController; |
||||
use Chamilo\CoreBundle\Controller\BaseResourceController; |
||||
use Knp\Menu\FactoryInterface as MenuFactoryInterface; |
||||
use Knp\Menu\ItemInterface as MenuItemInterface; |
||||
use Knp\Menu\Renderer\ListRenderer; |
||||
use Symfony\Component\HttpFoundation\Request; |
||||
use Chamilo\CoreBundle\Entity\Course; |
||||
use Chamilo\CoreBundle\Entity\Session; |
||||
use Chamilo\CourseBundle\Controller\ToolInterface; |
||||
|
||||
/** |
||||
* Each entity controller must extends this class. |
||||
* |
||||
* @abstract |
||||
*/ |
||||
abstract class ToolBaseCrudController extends BaseResourceController implements ToolInterface |
||||
{ |
||||
protected $course; |
||||
protected $session; |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function setCourse(Course $course) |
||||
{ |
||||
$this->course = $course; |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function getCourse() |
||||
{ |
||||
return $this->course; |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function setSession(Session $session) |
||||
{ |
||||
$this->session = $session; |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function getSession() |
||||
{ |
||||
return $this->session; |
||||
} |
||||
|
||||
/** |
||||
* @param $action |
||||
* @param MenuItemInterface $menu |
||||
* @return MenuItemInterface |
||||
*/ |
||||
public function buildBreadcrumbs($action, MenuItemInterface $menu = null) |
||||
{ |
||||
if (!$menu) { |
||||
$menu = $this->getHomeBreadCrumb(); |
||||
} |
||||
|
||||
// Tool home |
||||
$menu->addChild( |
||||
$this->trans($this->getClassnameLabel()), |
||||
array( |
||||
'uri' => $this->generateControllerUrl( |
||||
'indexAction', |
||||
array( |
||||
'courseCode' => $this->getCourse()->getCode() |
||||
) |
||||
) |
||||
) |
||||
); |
||||
|
||||
$action = str_replace( |
||||
array($this->getControllerAlias().':', 'Action'), |
||||
'', |
||||
$action |
||||
); |
||||
|
||||
switch ($action) { |
||||
case 'add': |
||||
case 'edit': |
||||
$menu->addChild( |
||||
$this->trans($this->getClassnameLabel().ucfirst($action)) |
||||
//array('uri' => $this->generateControllerUrl($action.'Action')) |
||||
); |
||||
break; |
||||
} |
||||
|
||||
return $menu; |
||||
} |
||||
|
||||
/** |
||||
* Converts string 'Chamilo\CourseBundle\Controller\CourseHome\CourseHomeController' into |
||||
* 'tool/course_home' |
||||
*/ |
||||
public function getTemplatePath() |
||||
{ |
||||
$parts = $this->getClassParts(); |
||||
|
||||
$newPath = array(); |
||||
foreach ($parts as $part) { |
||||
if (in_array($part, array('chamilo', 'controller')) || |
||||
strpos($part, '_controller') > 0 |
||||
) { |
||||
continue; |
||||
} |
||||
$newPath[] = $part; |
||||
} |
||||
|
||||
$template = implode('/', $newPath); |
||||
return str_replace('_controller', '', $template); |
||||
} |
||||
|
||||
/** |
||||
* Before middleware for the ToolBaseController |
||||
* |
||||
* @param Request $request |
||||
*/ |
||||
public function before(Request $request) |
||||
{ |
||||
$cidReset = $this->get('cidReset'); |
||||
$cidReq = $request->get('cidReq'); |
||||
$sessionHandler = $request->getSession(); |
||||
|
||||
if (empty($cidReq)) { |
||||
$cidReq = $request->get('courseCode'); |
||||
} |
||||
|
||||
$sessionId = $request->get('id_session'); |
||||
$groupId = $request->get('gidReq'); |
||||
|
||||
$tempCourseId = api_get_course_id(); |
||||
$tempGroupId = api_get_group_id(); |
||||
$tempSessionId = api_get_session_id(); |
||||
|
||||
$courseReset = false; |
||||
if ((!empty($cidReq) && $tempCourseId != $cidReq) || empty($tempCourseId) || empty($tempCourseId) == -1) { |
||||
$courseReset = true; |
||||
} |
||||
|
||||
if (isset($cidReset) && $cidReset == 1) { |
||||
$courseReset = true; |
||||
} |
||||
|
||||
$sessionHandler->set('courseReset', $courseReset); |
||||
|
||||
$groupReset = false; |
||||
if ($tempGroupId != $groupId || empty($tempGroupId)) { |
||||
$groupReset = true; |
||||
} |
||||
|
||||
$sessionReset = false; |
||||
if ($tempSessionId != $sessionId || empty($tempSessionId)) { |
||||
$sessionReset = true; |
||||
} |
||||
|
||||
if ($courseReset) { |
||||
|
||||
if (!empty($cidReq) && $cidReq != -1) { |
||||
$courseInfo = api_get_course_info($cidReq, true, true); |
||||
|
||||
if (!empty($courseInfo)) { |
||||
$courseCode = $courseInfo['code']; |
||||
$courseId = $courseInfo['real_id']; |
||||
|
||||
$sessionHandler->set('_real_cid', $courseId); |
||||
$sessionHandler->set('_cid', $courseCode); |
||||
$sessionHandler->set('_course', $courseInfo); |
||||
|
||||
} else { |
||||
$this->abort(404, $this->trans('Course not available')); |
||||
} |
||||
} else { |
||||
$sessionHandler->remove('_real_cid'); |
||||
$sessionHandler->remove('_cid'); |
||||
$sessionHandler->remove('_course'); |
||||
} |
||||
} |
||||
|
||||
$courseCode = api_get_course_id(); |
||||
|
||||
if (!empty($courseCode) && $courseCode != -1) { |
||||
//$tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); |
||||
$time = api_get_utc_datetime(); |
||||
$sql = "UPDATE course SET last_visit= '$time' WHERE code='$courseCode'"; |
||||
$this->getDatabase()->query($sql); |
||||
} |
||||
|
||||
if ($sessionReset) { |
||||
$sessionHandler->remove('session_name'); |
||||
$sessionHandler->remove('id_session'); |
||||
|
||||
if (!empty($sessionId)) { |
||||
$sessionInfo = api_get_session_info($sessionId); |
||||
if (empty($sessionInfo)) { |
||||
$this->abort(404, $this->trans('Session not available')); |
||||
} else { |
||||
$sessionHandler->set('id_session', $sessionId); |
||||
} |
||||
} |
||||
} |
||||
|
||||
if ($groupReset) { |
||||
$sessionHandler->remove('_gid'); |
||||
if (!empty($groupId)) { |
||||
$sessionHandler->set('_gid', $groupId); |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,36 @@ |
||||
<?php |
||||
|
||||
namespace Chamilo\CourseBundle\Controller; |
||||
|
||||
use Chamilo\CoreBundle\Entity\Course; |
||||
use Chamilo\CoreBundle\Entity\Session; |
||||
|
||||
/** |
||||
* Interface ToolInterface |
||||
* This functions are loaded in the CourseListener.php |
||||
* @package Chamilo\CourseBundle\Controller |
||||
*/ |
||||
interface ToolInterface |
||||
{ |
||||
/** |
||||
* @param Course $course |
||||
* @return mixed |
||||
*/ |
||||
public function setCourse(Course $course); |
||||
|
||||
/** |
||||
* @param Session $session |
||||
* @return mixed |
||||
*/ |
||||
public function setSession(Session $session); |
||||
|
||||
/** |
||||
* @return Course |
||||
*/ |
||||
public function getCourse(); |
||||
|
||||
/** |
||||
* @return Session |
||||
*/ |
||||
public function getSession(); |
||||
} |
||||
Loading…
Reference in new issue