IndexController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. namespace ChamiloLMS\Controller;
  4. use Silex\Application;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpKernel\HttpKernelInterface;
  8. use Symfony\Component\Finder\Finder;
  9. /**
  10. * @package ChamiloLMS.Controller
  11. * @author Julio Montoya <gugli100@gmail.com>
  12. */
  13. class IndexController extends CommonController
  14. {
  15. public $section;
  16. public $languageFiles = array('courses', 'index', 'admin');
  17. /**
  18. * @param \Silex\Application $app
  19. *
  20. * @return \Symfony\Component\HttpFoundation\Response
  21. */
  22. public function indexAction(Application $app)
  23. {
  24. /** @var \Template $template */
  25. $template = $app['template'];
  26. /*
  27. $token = $app['security']->getToken();
  28. if (null !== $token) {
  29. $user = $token->getUser();
  30. }*/
  31. /*\ChamiloSession::write('name', 'clara');
  32. var_dump(\ChamiloSession::read('name'));
  33. var_dump($_SESSION['name']);*/
  34. //var_dump(\ChamiloSession::read('aaa'));
  35. /*\ChamiloSession::write('name', 'clar');
  36. echo \ChamiloSession::read('name');
  37. $app['session']->set('name', 'julio');
  38. echo $app['session']->get('name');*/
  39. /*
  40. $token = $app['security']->getToken();
  41. if (null !== $token) {
  42. $user = $token->getUser();
  43. var_dump($user );
  44. }
  45. if ($app['security']->isGranted('ROLE_ADMIN')) {
  46. }*/
  47. /** @var \Entity\User $user */
  48. /*$em = $app['orm.ems']['db_write'];
  49. $user = $em->getRepository('Entity\User')->find(6);
  50. $role = $em->getRepository('Entity\Role')->findOneByRole('ROLE_STUDENT');
  51. $user->getRolesObj()->add($role);
  52. $em->persist($user);
  53. $em->flush();*/
  54. //$user->roles->add($status);
  55. /*$roles = $user->getRolesObj();
  56. foreach ($roles as $role) {
  57. }*/
  58. // $countries = Intl::getRegionBundle()->getCountryNames('es');
  59. //var_dump($countries);
  60. /*$formatter = new \IntlDateFormatter(\Locale::getDefault(), \IntlDateFormatter::NONE, \IntlDateFormatter::NONE);
  61. //http://userguide.icu-project.org/formatparse/datetime for date formats
  62. $formatter->setPattern("EEEE d MMMM Y");
  63. echo $formatter->format(time());*/
  64. //@todo improve this JS includes should be added using twig
  65. $extra = array(
  66. api_get_jquery_libraries_js(array('bxslider')),
  67. '<script>
  68. $(document).ready(function(){
  69. $("#slider").bxSlider({
  70. infiniteLoop : true,
  71. auto : true,
  72. pager : true,
  73. autoHover : true,
  74. pause : 10000
  75. });
  76. });
  77. </script>'
  78. );
  79. if (api_get_setting('use_virtual_keyboard') == 'true') {
  80. $extra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/keyboard/keyboard.css');
  81. $extra[] = api_get_js('keyboard/jquery.keyboard.js');
  82. }
  83. $app['extraJS'] = $extra;
  84. $app['this_section'] = SECTION_CAMPUS;
  85. $request = $app['request'];
  86. if (api_get_setting('allow_terms_conditions') == 'true') {
  87. unset($_SESSION['term_and_condition']);
  88. }
  89. // If we are not logged in and custompages activated
  90. if (!api_get_user_id() && \CustomPages::enabled()) {
  91. $loggedOut = $request->get('loggedout');
  92. if ($loggedOut) {
  93. \CustomPages::display(\CustomPages::LOGGED_OUT);
  94. } else {
  95. \CustomPages::display(\CustomPages::INDEX_UNLOGGED);
  96. }
  97. }
  98. /** @var \PageController $pageController */
  99. $pageController = $app['page_controller'];
  100. if (api_get_setting('display_categories_on_homepage') == 'true') {
  101. $template->assign('course_category_block', $pageController->return_courses_in_categories());
  102. }
  103. // @todo Custom Facebook connection lib could be replaced with opauth
  104. // Facebook connection, if activated
  105. if (api_is_facebook_auth_activated() && !api_get_user_id()) {
  106. facebook_connect();
  107. }
  108. $this->setLoginForm($app);
  109. if (!api_is_anonymous()) {
  110. $pageController->return_profile_block();
  111. $pageController->return_user_image_block();
  112. if (api_is_platform_admin()) {
  113. $pageController->return_course_block();
  114. } else {
  115. $pageController->return_teacher_link();
  116. }
  117. }
  118. // Hot courses & announcements
  119. $hotCourses = null;
  120. $announcementsBlock = null;
  121. // When loading a chamilo page do not include the hot courses and news
  122. if (!isset($_REQUEST['include'])) {
  123. if (api_get_setting('show_hot_courses') == 'true') {
  124. $hotCourses = $pageController->return_hot_courses();
  125. }
  126. $announcementsBlock = $pageController->return_announcements();
  127. }
  128. $template->assign('hot_courses', $hotCourses);
  129. $template->assign('announcements_block', $announcementsBlock);
  130. // Homepage
  131. $template->assign('home_page_block', $pageController->returnHomePage());
  132. // Navigation links
  133. $pageController->returnNavigationLinks($template->getNavigationLinks());
  134. $pageController->return_notice();
  135. $pageController->return_help();
  136. if (api_is_platform_admin() || api_is_drh()) {
  137. $pageController->return_skills_links();
  138. }
  139. $response = $template->render_layout('layout_2_col.tpl');
  140. //return new Response($response, 200, array('Cache-Control' => 's-maxage=3600, public'));
  141. return new Response($response, 200, array());
  142. }
  143. /**
  144. * @param Application $app
  145. * @return Response
  146. */
  147. public function loginAction(Application $app)
  148. {
  149. $request = $app['request'];
  150. $app['template']->assign('error', $app['security.last_error']($request));
  151. $extra = array();
  152. if (api_get_setting('use_virtual_keyboard') == 'true') {
  153. $extra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/keyboard/keyboard.css');
  154. $extra[] = api_get_js('keyboard/jquery.keyboard.js');
  155. }
  156. $app['extraJS'] = $extra;
  157. $response = $app['template']->render_template('auth/login.tpl');
  158. return new Response($response, 200, array('Cache-Control' => 's-maxage=3600, public'));
  159. }
  160. /**
  161. * @param \Silex\Application $app
  162. */
  163. public function setLoginForm(Application $app)
  164. {
  165. $userId = api_get_user_id();
  166. $loginForm = null;
  167. if (!$userId || api_is_anonymous($userId)) {
  168. // Only display if the user isn't logged in
  169. $app['template']->assign('login_language_form', api_display_language_form(true));
  170. $app['template']->assign('login_form', self::displayLoginForm($app));
  171. if (api_get_setting('allow_lostpassword') == 'true' || api_get_setting('allow_registration') == 'true') {
  172. $loginForm .= '<ul class="nav nav-list">';
  173. if (api_get_setting('allow_registration') != 'false') {
  174. $loginForm .= '<li><a href="'.api_get_path(WEB_CODE_PATH).'auth/inscription.php">'.get_lang('Reg').'</a></li>';
  175. }
  176. if (api_get_setting('allow_lostpassword') == 'true') {
  177. $loginForm .= '<li><a href="'.api_get_path(WEB_CODE_PATH).'auth/lostPassword.php">'.get_lang('LostPassword').'</a></li>';
  178. }
  179. $loginForm .= '</ul>';
  180. }
  181. $app['template']->assign('login_options', $loginForm);
  182. }
  183. }
  184. /**
  185. * @param \Silex\Application $app
  186. *
  187. * @return string
  188. */
  189. public function displayLoginForm(Application $app)
  190. {
  191. /* {{ form_widget(form) }}
  192. $form = $app['form.factory']->createBuilder('form')
  193. ->add('name')
  194. ->add('email')
  195. ->add('gender', 'choice', array(
  196. 'choices' => array(1 => 'male', 2 => 'female'),
  197. 'expanded' => true,
  198. ))
  199. ->getForm();
  200. return $app['template']->assign('form', $form->createView());
  201. */
  202. $form = new \FormValidator('formLogin', 'POST', $app['url_generator']->generate('admin_login_check'), null, array('class' => 'form-vertical'));
  203. $form->addElement(
  204. 'text',
  205. 'username',
  206. get_lang('UserName'),
  207. array(
  208. 'class' => 'input-medium autocapitalize_off virtualkey',
  209. 'autofocus' => 'autofocus'
  210. )
  211. );
  212. $form->addElement(
  213. 'password',
  214. 'password',
  215. get_lang('Pass'),
  216. array(
  217. 'class' => 'input-medium virtualkey'
  218. )
  219. );
  220. $form->addElement('style_submit_button', 'submitAuth', get_lang('LoginEnter'), array('class' => 'btn'));
  221. $html = $form->return_form();
  222. /*if (api_get_setting('openid_authentication') == 'true') {
  223. include_once 'main/auth/openid/login.php';
  224. $html .= '<div>'.openid_form().'</div>';
  225. }*/
  226. /** Verify if settings is active to set keyboard. Included extra class in form input elements */
  227. if (api_get_setting('use_virtual_keyboard') == 'true') {
  228. $html .= "<script>
  229. $(function(){
  230. $('.virtualkey').keyboard({
  231. layout:'custom',
  232. customLayout: {
  233. 'default': [
  234. '1 2 3 4 5 6 7 8 9 0 {bksp}',
  235. 'q w e r t y u i o p',
  236. 'a s d f g h j k l',
  237. 'z x c v b n m',
  238. '{cancel} {accept}'
  239. ]
  240. }
  241. });
  242. });
  243. </script>";
  244. }
  245. return $html;
  246. }
  247. /**
  248. * @todo move all this getDocument* Actions into another controller
  249. * @param Application $app
  250. * @return \Symfony\Component\HttpFoundation\BinaryFileResponse|void
  251. */
  252. public function getDocumentTemplateAction(Application $app)
  253. {
  254. try {
  255. $file = $app['request']->get('file');
  256. $file = $app['chamilo.filesystem']->get('document_templates/'.$file);
  257. return $app->sendFile($file->getPathname());
  258. } catch (\InvalidArgumentException $e) {
  259. return $app->abort(404, 'File not found');
  260. }
  261. }
  262. /**
  263. * Gets a document from the data/courses/MATHS/document/file.jpg to the user
  264. * @todo check permissions
  265. * @param Application $app
  266. * @param string $courseCode
  267. * @param string $file
  268. * @return \Symfony\Component\HttpFoundation\BinaryFileResponse|void
  269. */
  270. public function getDocumentAction(Application $app, $courseCode, $file)
  271. {
  272. try {
  273. $file = $app['chamilo.filesystem']->getCourseDocument($courseCode, $file);
  274. return $app->sendFile($file->getPathname());
  275. } catch (\InvalidArgumentException $e) {
  276. return $app->abort(404, 'File not found');
  277. }
  278. }
  279. /**
  280. * Gets a document from the data/courses/MATHS/scorm/file.jpg to the user
  281. * @todo check permissions
  282. * @param Application $app
  283. * @param string $courseCode
  284. * @param string $file
  285. * @return \Symfony\Component\HttpFoundation\BinaryFileResponse|void
  286. */
  287. public function getScormDocumentAction(Application $app, $courseCode, $file)
  288. {
  289. try {
  290. $file = $app['chamilo.filesystem']->getCourseScormDocument($courseCode, $file);
  291. return $app->sendFile($file->getPathname());
  292. } catch (\InvalidArgumentException $e) {
  293. return $app->abort(404, 'File not found');
  294. }
  295. }
  296. /**
  297. * Gets a document from the data/default_platform_document/* folder
  298. * @param Application $app
  299. * @param string $file
  300. * @return \Symfony\Component\HttpFoundation\BinaryFileResponse|void
  301. */
  302. public function getDefaultPlatformDocumentAction(Application $app, $file)
  303. {
  304. try {
  305. $file = $app['chamilo.filesystem']->get('default_platform_document/'.$file);
  306. return $app->sendFile($file->getPathname());
  307. } catch (\InvalidArgumentException $e) {
  308. return $app->abort(404, 'File not found');
  309. }
  310. }
  311. /**
  312. * Gets a document from the data/default_platform_document/* folder
  313. * @param Application $app
  314. * @param string $file
  315. * @return \Symfony\Component\HttpFoundation\BinaryFileResponse|void
  316. */
  317. public function getDefaultCourseDocumentAction(Application $app, $file)
  318. {
  319. try {
  320. $file = $app['chamilo.filesystem']->get('default_course_document/'.$file);
  321. return $app->sendFile($file->getPathname());
  322. } catch (\InvalidArgumentException $e) {
  323. return $app->abort(404, 'File not found');
  324. }
  325. }
  326. /**
  327. * @param Application $app
  328. * @param $groupId
  329. * @param $file
  330. * @return \Symfony\Component\HttpFoundation\BinaryFileResponse|void
  331. */
  332. public function getGroupFile(Application $app, $groupId, $file)
  333. {
  334. try {
  335. $file = $app['chamilo.filesystem']->get('upload/groups/'.$groupId.'/'.$file);
  336. return $app->sendFile($file->getPathname());
  337. } catch (\InvalidArgumentException $e) {
  338. return $app->abort(404, 'File not found');
  339. }
  340. }
  341. /**
  342. * @param Application $app
  343. * @param $file
  344. * @return \Symfony\Component\HttpFoundation\BinaryFileResponse|void
  345. */
  346. public function getUserFile(Application $app, $file)
  347. {
  348. try {
  349. $file = $app['chamilo.filesystem']->get('upload/users/'.$file);
  350. return $app->sendFile($file->getPathname());
  351. } catch (\InvalidArgumentException $e) {
  352. return $app->abort(404, 'File not found');
  353. }
  354. }
  355. /**
  356. * Reacts on a failed login.
  357. * Displays an explanation with a link to the registration form.
  358. *
  359. * @todo use twig template to prompt errors + move this into a helper
  360. */
  361. private function handleLoginFailed($error)
  362. {
  363. $message = get_lang('InvalidId');
  364. if (!isset($error)) {
  365. if (api_is_self_registration_allowed()) {
  366. $message = get_lang('InvalidForSelfRegistration');
  367. }
  368. } else {
  369. switch ($error) {
  370. case '':
  371. if (api_is_self_registration_allowed()) {
  372. $message = get_lang('InvalidForSelfRegistration');
  373. }
  374. break;
  375. case 'account_expired':
  376. $message = get_lang('AccountExpired');
  377. break;
  378. case 'account_inactive':
  379. $message = get_lang('AccountInactive');
  380. break;
  381. case 'user_password_incorrect':
  382. $message = get_lang('InvalidId');
  383. break;
  384. case 'access_url_inactive':
  385. $message = get_lang('AccountURLInactive');
  386. break;
  387. case 'unrecognize_sso_origin':
  388. //$message = get_lang('SSOError');
  389. break;
  390. }
  391. }
  392. return \Display::return_message($message, 'error');
  393. }
  394. function dashboardAction(Application $app)
  395. {
  396. $template = $app['template'];
  397. $template->assign('content', 'welcome!');
  398. $response = $template->render_layout('layout_2_col.tpl');
  399. //return new Response($response, 200, array('Cache-Control' => 's-maxage=3600, public'));
  400. return new Response($response, 200, array());
  401. }
  402. }