IndexController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. namespace ChamiloLMS\Controller;
  4. use Silex\Application;
  5. use Symfony\Component\HttpFoundation\Response;
  6. /**
  7. *
  8. */
  9. class IndexController
  10. {
  11. /**
  12. * @param \Silex\Application $app
  13. * @return \Symfony\Component\HttpFoundation\Response
  14. */
  15. public function indexAction(Application $app)
  16. {
  17. $request = $app['request'];
  18. //$token = $app['security']->getToken();
  19. //Actions
  20. $logout = $request->get('logout');
  21. if (!empty($logout)) {
  22. $this->logout();
  23. $app->redirect($app['url_generator']->generator('index'));
  24. }
  25. //$article = $app['orm.em']->getRepository('Entity\EntityCourse');
  26. //$courses_query = $app['orm.em']->createQuery('SELECT a FROM Entity\EntityCourse a');
  27. //$a = new EntityCourse();
  28. //$article = $app['orm.em']->getRepository('EntityCourse');
  29. //var_dump($article);
  30. //$courses_query = $app['orm.em']->createQuery('SELECT a FROM Entity\EntityCourse a');
  31. /*
  32. $paginator = new Doctrine\ORM\Tools\Pagination\Paginator($courses_query, $fetchJoinCollection = true);
  33. $c = count($paginator);
  34. foreach ($paginator as $course) {
  35. echo $course->getCode() . "\n";
  36. }
  37. exit; */
  38. //$app['orm.em']->find('EntityCourse', 1);
  39. //var_dump($app['orm.ems']['mysql']);
  40. // Defines wether or not anonymous visitors can see a list of the courses on the Chamilo homepage that are open to the world.
  41. //$_setting['display_courses_to_anonymous_users'] = 'true';
  42. // Delete session neccesary for legal terms
  43. if (api_get_setting('allow_terms_conditions') == 'true') {
  44. unset($_SESSION['term_and_condition']);
  45. }
  46. //If we are not logged in and customapages activated
  47. if (!api_get_user_id() && CustomPages::enabled()) {
  48. $logged_out = $request->get('loggedout');
  49. if ($logged_out) {
  50. CustomPages::display(CustomPages::LOGGED_OUT);
  51. } else {
  52. CustomPages::display(CustomPages::INDEX_UNLOGGED);
  53. }
  54. }
  55. //$this->check_last_login();
  56. if (api_get_setting('display_categories_on_homepage') == 'true') {
  57. $app['template']->assign('course_category_block', \PageController::return_courses_in_categories());
  58. }
  59. // Facebook connexion, if activated
  60. if (api_is_facebook_auth_activated() && !api_get_user_id()) {
  61. facebook_connect();
  62. }
  63. $this->set_login_form($app);
  64. if (!api_is_anonymous()) {
  65. \PageController::return_profile_block();
  66. \PageController::return_user_image_block();
  67. if (api_is_platform_admin()) {
  68. \PageController::return_course_block();
  69. } else {
  70. \PageController::return_teacher_link();
  71. }
  72. }
  73. //Hot courses & announcements
  74. $hot_courses = null;
  75. $announcements_block = null;
  76. // When loading a chamilo page do not include the hot courses and news
  77. if (!isset($_REQUEST['include'])) {
  78. if (api_get_setting('show_hot_courses') == 'true') {
  79. $hot_courses = \PageController::return_hot_courses();
  80. }
  81. $announcements_block = \PageController::return_announcements();
  82. }
  83. $app['template']->assign('hot_courses', $hot_courses);
  84. $app['template']->assign('announcements_block', $announcements_block);
  85. //Homepage
  86. $app['template']->assign('home_page_block', \PageController::return_home_page());
  87. //Navigation links
  88. $nav_links = $app['template']->return_navigation_links();
  89. $app['template']->assign('navigation_course_links', $nav_links);
  90. $app['template']->assign('main_navigation_block', $nav_links);
  91. \PageController::return_notice();
  92. \PageController::return_help();
  93. if (api_is_platform_admin() || api_is_drh()) {
  94. \PageController::return_skills_links();
  95. }
  96. $response = $app['template']->render_layout('layout_2_col.tpl');
  97. //return new Response($response, 200, array('Cache-Control' => 's-maxage=3600, public'));
  98. return new Response($response, 200, array());
  99. }
  100. /**
  101. *
  102. * @todo This piece of code should probably move to local.inc.php where the actual login procedure is handled.
  103. * @todo Check if this code is used. I think this code is never executed because after clicking the submit button
  104. * the code does the stuff in local.inc.php and then redirects to index.php or user_portal.php depending
  105. * on api_get_setting('page_after_login').
  106. * @deprecated seems not to be used
  107. */
  108. function check_last_login()
  109. {
  110. if (!empty($_POST['submitAuth'])) {
  111. // The user has been already authenticated, we are now to find the last login of the user.
  112. if (!empty($this->user_id)) {
  113. $track_login_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  114. $sql_last_login = "SELECT login_date
  115. FROM $track_login_table
  116. WHERE login_user_id = '".$this->user_id."'
  117. ORDER BY login_date DESC LIMIT 1";
  118. $result_last_login = Database::query($sql_last_login);
  119. if (!$result_last_login) {
  120. if (Database::num_rows($result_last_login) > 0) {
  121. $user_last_login_datetime = Database::fetch_array($result_last_login);
  122. $user_last_login_datetime = $user_last_login_datetime[0];
  123. Session::write('user_last_login_datetime', $user_last_login_datetime);
  124. }
  125. }
  126. Database::free_result($result_last_login);
  127. if (api_is_platform_admin()) {
  128. // decode all open event informations and fill the track_c_* tables
  129. include api_get_path(LIBRARY_PATH).'stats.lib.inc.php';
  130. decodeOpenInfos();
  131. }
  132. }
  133. } else {
  134. // Only if login form was not sent because if the form is sent the user was already on the page.
  135. event_open();
  136. }
  137. }
  138. /**
  139. * @param Silex\Application $app
  140. */
  141. function set_login_form(Application $app)
  142. {
  143. $user_id = api_get_user_id();
  144. $login_form = null;
  145. if (!$user_id || api_is_anonymous($user_id)) {
  146. // Only display if the user isn't logged in.
  147. $app['template']->assign('login_language_form', api_display_language_form(true));
  148. //self::display_login_form($app);
  149. $app['template']->assign('login_form', self::display_login_form($app));
  150. if (api_get_setting('allow_lostpassword') == 'true' || api_get_setting('allow_registration') == 'true') {
  151. $login_form .= '<ul class="nav nav-list">';
  152. if (api_get_setting('allow_registration') != 'false') {
  153. $login_form .= '<li><a href="main/auth/inscription.php">'.get_lang('Reg').'</a></li>';
  154. }
  155. if (api_get_setting('allow_lostpassword') == 'true') {
  156. $login_form .= '<li><a href="main/auth/lostPassword.php">'.get_lang('LostPassword').'</a></li>';
  157. }
  158. $login_form .= '</ul>';
  159. }
  160. $app['template']->assign('login_options', $login_form);
  161. }
  162. }
  163. function logout()
  164. {
  165. $user_id = api_get_user_id();
  166. online_logout($user_id, true);
  167. }
  168. /**
  169. * @param \Silex\Application $app
  170. * @return string
  171. */
  172. function display_login_form(Application $app)
  173. {
  174. /* {{ form_widget(form) }}
  175. $form = $app['form.factory']->createBuilder('form')
  176. ->add('name')
  177. ->add('email')
  178. ->add('gender', 'choice', array(
  179. 'choices' => array(1 => 'male', 2 => 'female'),
  180. 'expanded' => true,
  181. ))
  182. ->getForm();
  183. return $app['template']->assign('form', $form->createView());
  184. */
  185. $form = new \FormValidator('formLogin', 'POST', null, null, array('class' => 'form-vertical'));
  186. $form->addElement(
  187. 'text',
  188. 'login',
  189. get_lang('UserName'),
  190. array('class' => 'span2 autocapitalize_off', 'autofocus' => 'autofocus')
  191. );
  192. $form->addElement('password', 'password', get_lang('Pass'), array('class' => 'span2'));
  193. $form->addElement('style_submit_button', 'submitAuth', get_lang('LoginEnter'), array('class' => 'btn'));
  194. $html = $form->return_form();
  195. if (api_get_setting('openid_authentication') == 'true') {
  196. include_once 'main/auth/openid/login.php';
  197. $html .= '<div>'.openid_form().'</div>';
  198. }
  199. return $html;
  200. }
  201. }