Ejemplo n.º 1
0
 /**
  * Prints the session and course list (user_portal.php)
  * @param int $user_id
  * @return string
  */
 public function returnCoursesAndSessions($user_id)
 {
     global $_configuration;
     $gamificationModeIsActive = api_get_setting('platform.gamification_mode');
     $load_history = isset($_GET['history']) && intval($_GET['history']) == 1 ? true : false;
     if ($load_history) {
         // Load sessions in category in *history*
         $session_categories = UserManager::get_sessions_by_category($user_id, true);
     } else {
         // Load sessions in category
         $session_categories = UserManager::get_sessions_by_category($user_id, false);
     }
     $html = '';
     // Showing history title
     if ($load_history) {
         $html .= Display::page_subheader(get_lang('HistoryTrainingSession'));
         if (empty($session_categories)) {
             $html .= get_lang('YouDoNotHaveAnySessionInItsHistory');
         }
     }
     $courses_html = '';
     $special_courses = '';
     $sessionCount = 0;
     $courseCount = 0;
     // If we're not in the history view...
     if (!isset($_GET['history'])) {
         // Display special courses.
         $specialCourses = CourseManager::display_special_courses($user_id, $this->load_directories_preview);
         $special_courses = $specialCourses['html'];
         // Display courses.
         $courses = CourseManager::display_courses($user_id, $this->load_directories_preview);
         $courses_html .= $courses['html'];
         $courseCount = $specialCourses['course_count'] + $courses['course_count'];
     }
     $sessions_with_category = '';
     $sessions_with_no_category = '';
     $sessionTitleLink = api_get_configuration_value('courses_list_session_title_link');
     $sessionTitleLink = $sessionTitleLink === false ? 1 : $sessionTitleLink;
     if (is_array($session_categories)) {
         foreach ($session_categories as $session_category) {
             $session_category_id = $session_category['session_category']['id'];
             // Sessions and courses that are not in a session category
             if (empty($session_category_id) && isset($session_category['sessions'])) {
                 // Independent sessions
                 foreach ($session_category['sessions'] as $session) {
                     $session_id = $session['session_id'];
                     // Don't show empty sessions.
                     if (count($session['courses']) < 1) {
                         continue;
                     }
                     // Courses inside the current session.
                     $date_session_start = $session['access_start_date'];
                     $date_session_end = $session['access_end_date'];
                     $coachAccessStartDate = $session['coach_access_start_date'];
                     $coachAccessEndDate = $session['coach_access_end_date'];
                     $session_now = time();
                     $count_courses_session = 0;
                     // Loop course content
                     $html_courses_session = [];
                     $atLeastOneCourseIsVisible = false;
                     foreach ($session['courses'] as $course) {
                         $is_coach_course = api_is_coach($session_id, $course['real_id']);
                         $allowed_time = 0;
                         $dif_time_after = 0;
                         if (!empty($date_session_start) && $date_session_start != '0000-00-00 00:00:00') {
                             if ($is_coach_course) {
                                 $allowed_time = api_strtotime($coachAccessStartDate);
                             } else {
                                 $allowed_time = api_strtotime($date_session_start);
                             }
                             if (!isset($_GET['history'])) {
                                 if (!empty($date_session_end) && $date_session_end != '0000-00-00 00:00:00') {
                                     $endSessionToTms = api_strtotime($date_session_end);
                                     if ($session_now > $endSessionToTms) {
                                         $dif_time_after = $session_now - $endSessionToTms;
                                         $dif_time_after = round($dif_time_after / 86400);
                                     }
                                 }
                             }
                         }
                         if ($session_now > $allowed_time) {
                             // Read only and accessible.
                             $atLeastOneCourseIsVisible = true;
                             if (api_get_setting('session.hide_courses_in_sessions') == 'false') {
                                 $courseUserHtml = CourseManager::get_logged_user_course_html($course, $session_id, 'session_course_item', true, $this->load_directories_preview);
                                 if (isset($courseUserHtml[1])) {
                                     $course_session = $courseUserHtml[1];
                                     $course_session['skill'] = isset($courseUserHtml['skill']) ? $courseUserHtml['skill'] : '';
                                     $html_courses_session[] = $course_session;
                                 }
                             }
                             $count_courses_session++;
                         }
                     }
                     // No courses to show.
                     if ($atLeastOneCourseIsVisible == false) {
                         if (empty($html_courses_session)) {
                             continue;
                         }
                     }
                     if ($count_courses_session > 0) {
                         $params = array('id' => $session_id);
                         $session_box = Display::get_session_title_box($session_id);
                         $extra_info = !empty($session_box['coach']) ? $session_box['coach'] : null;
                         $extra_info .= !empty($session_box['coach']) ? ' - ' . $session_box['dates'] : $session_box['dates'];
                         $extra_info .= isset($session_box['duration']) ? ' ' . $session_box['duration'] : null;
                         $params['extra_fields'] = $session_box['extra_fields'];
                         $params['show_link_to_session'] = !api_is_drh() && $sessionTitleLink;
                         $params['title'] = $session_box['title'];
                         $params['subtitle'] = $extra_info;
                         $params['show_actions'] = api_is_platform_admin() ? true : false;
                         if (api_get_setting('session.hide_courses_in_sessions') == 'false') {
                             // $params['extra'] .=  $html_courses_session;
                         }
                         $params['description'] = $session_box['description'];
                         $params['show_description'] = $session_box['show_description'];
                         $params['courses'] = $html_courses_session;
                         $params['show_simple_session_info'] = false;
                         if (isset($_configuration['show_simple_session_info']) && $_configuration['show_simple_session_info']) {
                             $params['show_simple_session_info'] = true;
                         }
                         if ($gamificationModeIsActive) {
                             $params['stars'] = GamificationUtils::getSessionStars($params['id'], $this->user_id);
                             $params['progress'] = GamificationUtils::getSessionProgress($params['id'], $this->user_id);
                             $params['points'] = GamificationUtils::getSessionPoints($params['id'], $this->user_id);
                         }
                         $this->tpl->assign('session', $params);
                         $this->tpl->assign('gamification_mode', $gamificationModeIsActive);
                         $sessions_with_no_category .= $this->tpl->fetch($this->tpl->get_template('/user_portal/session.tpl'));
                         $sessionCount++;
                     }
                 }
             } else {
                 // All sessions included in
                 $count_courses_session = 0;
                 $html_sessions = '';
                 if (isset($session_category['sessions'])) {
                     foreach ($session_category['sessions'] as $session) {
                         $session_id = $session['session_id'];
                         // Don't show empty sessions.
                         if (count($session['courses']) < 1) {
                             continue;
                         }
                         $date_session_start = $session['access_start_date'];
                         $date_session_end = $session['access_end_date'];
                         $coachAccessStartDate = $session['coach_access_start_date'];
                         $coachAccessEndDate = $session['coach_access_end_date'];
                         $session_now = time();
                         $html_courses_session = [];
                         $count = 0;
                         foreach ($session['courses'] as $course) {
                             $is_coach_course = api_is_coach($session_id, $course['real_id']);
                             $dif_time_after = 0;
                             $allowed_time = 0;
                             if ($is_coach_course) {
                                 // 24 hours = 86400
                                 if ($date_session_start != '0000-00-00 00:00:00') {
                                     $allowed_time = api_strtotime($coachAccessStartDate);
                                 }
                                 if (!isset($_GET['history'])) {
                                     if ($date_session_end != '0000-00-00 00:00:00') {
                                         $endSessionToTms = api_strtotime($date_session_end);
                                         if ($session_now > $endSessionToTms) {
                                             $dif_time_after = $session_now - $endSessionToTms;
                                             $dif_time_after = round($dif_time_after / 86400);
                                         }
                                     }
                                 }
                             } else {
                                 $allowed_time = api_strtotime($date_session_start);
                             }
                             if ($session_now > $allowed_time) {
                                 if (api_get_setting('session.hide_courses_in_sessions') == 'false') {
                                     $c = CourseManager::get_logged_user_course_html($course, $session_id, 'session_course_item');
                                     $html_courses_session[] = $c[1];
                                 }
                                 $count_courses_session++;
                                 $count++;
                             }
                         }
                         $sessionParams = array();
                         //Category
                         if ($count > 0) {
                             $session_box = Display::get_session_title_box($session_id);
                             $sessionParams['id'] = $session_id;
                             $sessionParams['show_link_to_session'] = !api_is_drh() && $sessionTitleLink;
                             $sessionParams['title'] = $session_box['title'];
                             $sessionParams['subtitle'] = (!empty($session_box['coach']) ? $session_box['coach'] . ' | ' : '') . $session_box['dates'];
                             $sessionParams['show_actions'] = api_is_platform_admin();
                             $sessionParams['courses'] = $html_courses_session;
                             $sessionParams['show_simple_session_info'] = false;
                             if (isset($_configuration['show_simple_session_info']) && $_configuration['show_simple_session_info']) {
                                 $sessionParams['show_simple_session_info'] = true;
                             }
                             $this->tpl->assign('session', $sessionParams);
                             $html_sessions .= $this->tpl->fetch($this->tpl->get_template('user_portal/session.tpl'));
                             $sessionCount++;
                         }
                     }
                 }
                 if ($count_courses_session > 0) {
                     $categoryParams = array('id' => $session_category['session_category']['id'], 'title' => $session_category['session_category']['name'], 'show_actions' => api_is_platform_admin(), 'subtitle' => null, 'sessions' => $html_sessions);
                     $session_category_start_date = $session_category['session_category']['date_start'];
                     $session_category_end_date = $session_category['session_category']['date_end'];
                     if (!empty($session_category_start_date) && $session_category_start_date != '0000-00-00' && !empty($session_category_end_date) && $session_category_end_date != '0000-00-00') {
                         $categoryParams['subtitle'] = sprintf(get_lang('FromDateXToDateY'), $session_category['session_category']['date_start'], $session_category['session_category']['date_end']);
                     } else {
                         if (!empty($session_category_start_date) && $session_category_start_date != '0000-00-00') {
                             $categoryParams['subtitle'] = get_lang('From') . ' ' . $session_category_start_date;
                         }
                         if (!empty($session_category_end_date) && $session_category_end_date != '0000-00-00') {
                             $categoryParams['subtitle'] = get_lang('Until') . ' ' . $session_category_end_date;
                         }
                     }
                     $this->tpl->assign('session_category', $categoryParams);
                     $sessions_with_category .= $this->tpl->fetch("{$this->tpl->templateFolder}/user_portal/session_category.tpl");
                 }
             }
         }
     }
     return ['html' => $sessions_with_category . $sessions_with_no_category . $courses_html . $special_courses, 'session_count' => $sessionCount, 'course_count' => $courseCount];
 }