Browse Source

List the sessions and courses for sessions catalog - refs #7275

Angel Fernando Quiroz Campos 10 years ago
parent
commit
d06fd476b8

+ 8 - 1
main/auth/courses.php

@@ -76,7 +76,7 @@ if (api_is_platform_admin() || api_is_course_admin() || api_is_allowed_to_create
 }
 
 // filter actions
-$actions = array('sortmycourses', 'createcoursecategory', 'subscribe', 'deletecoursecategory', 'display_courses', 'display_random_courses', 'subscribe_user_with_password');
+$actions = array('sortmycourses', 'createcoursecategory', 'subscribe', 'deletecoursecategory', 'display_courses', 'display_random_courses', 'subscribe_user_with_password', 'display_sessions');
 $action = 'display_random_courses';
 $nameTools = get_lang('SortMyCourses');
 
@@ -99,6 +99,10 @@ if ($action == 'display_random_courses' || $action == 'display_courses' ) {
 	$nameTools = get_lang('CourseManagement');
 }
 
+if ($action == 'display_sessions') {
+    $nameTools = get_lang('CourseManagement');
+}
+
 // Breadcrumbs.
 $interbreadcrumb[] = array('url' => api_get_path(WEB_PATH).'user_portal.php', 'name' => get_lang('MyCourses'));
 
@@ -215,4 +219,7 @@ switch ($action) {
     case 'display_courses':
         $courses_controller->courses_categories($action, $_GET['category_code']);
         break;
+    case 'display_sessions':
+        $courses_controller->sessions($action);
+        break;
 }

+ 36 - 0
main/auth/courses_controller.php

@@ -82,6 +82,8 @@ class CoursesController { // extends Controller {
     public function courses_categories($action, $category_code = null, $message = '', $error = '', $content = null) {
         $data = array();
         $browse_course_categories = $this->model->browse_course_categories();
+        
+        global $_configuration;
 
         if ($action == 'display_random_courses') {
             $data['browse_courses_in_category'] = $this->model->browse_courses_in_category(null, 10);
@@ -119,6 +121,12 @@ class CoursesController { // extends Controller {
         $data['message']          = $message;
         $data['content']          = $content;
         $data['error']            = $error;
+        
+        $data['catalogShowCoursesSessions'] = 0;
+        
+        if (isset($_configuration['catalog_show_courses_sessions'])) {
+            $data['catalogShowCoursesSessions'] = $_configuration['catalog_show_courses_sessions'];
+        }
 
         // render to the view
         $this->view->set_data($data);
@@ -299,4 +307,32 @@ class CoursesController { // extends Controller {
             $this->courses_categories('subcribe', $category_code, $message, $error);
         }
     }
+    
+    /**
+     * It's used for listing sessions, render to sessions view in course catalog
+     * @param string $action
+     */
+    public function sessions($action)
+    {
+        $data = array();
+        $browse_course_categories = $this->model->browse_course_categories();
+
+        global $_configuration;
+
+        $data['browse_course_categories'] = $browse_course_categories;
+        $data['action'] = Security::remove_XSS($action);
+        $data['browseSessions'] = $this->model->browseSessions();
+
+        $data['catalogShowCoursesSessions'] = 0;
+
+        if (isset($_configuration['catalog_show_courses_sessions'])) {
+            $data['catalogShowCoursesSessions'] = $_configuration['catalog_show_courses_sessions'];
+        }
+
+        $this->view->set_data($data);
+        $this->view->set_layout('layout');
+        $this->view->set_template('courses_categories');
+        $this->view->render();
+    }
+
 }

+ 32 - 0
main/inc/ajax/course.ajax.php

@@ -232,6 +232,38 @@ switch ($action) {
             }
         }
         break;
+    case 'display_sessions_courses':
+        $sessionId = Security::remove_XSS($_GET['session']);
+        $userTable = Database::get_main_table(TABLE_MAIN_USER);
+
+        $coursesData = SessionManager::get_course_list_by_session_id($sessionId);
+
+        $courses = array();
+
+        foreach ($coursesData as $courseId => $course) {
+            $coachData = SessionManager::getCoachesByCourseSession($sessionId, $course['code']);
+            
+            $coachName = '';
+
+            if (!empty($coachData)) {
+                $userResult = Database::select('lastname,firstname', $userTable, array(
+                    'where' => array(
+                        'user_id = ?' => $coachData[0]
+                    )
+                ), 'first');
+
+                $coachName = api_get_person_name($userResult['firstname'], $userResult['lastname']);
+           }
+           
+           $courses[] = array(
+               'id' => $courseId,
+               'name' => $course['title'],
+               'coachName' => $coachName,
+           );
+        }
+
+        echo json_encode($courses);
+        break;
     default:
         echo '';
 }

+ 26 - 1
main/inc/lib/auth.lib.php

@@ -521,4 +521,29 @@ class Auth
             return array('message' => $message, 'content' => $content);
         }
     }
-}
+    
+    public function browseSessions()
+    {
+        require_once api_get_path(LIBRARY_PATH) . 'sessionmanager.lib.php';
+
+        $sessions = SessionManager::get_sessions_list();
+        $sessionsToBrowse = array();
+        $userId = api_get_user_id();
+
+        foreach ($sessions as $session) {
+            if ($session['nbr_courses'] > 0) {
+                $sessionToBrowse = array(
+                    'id' => $session['id'],
+                    'name' => $session['name'],
+                    'coach_name' => api_get_person_name($session['firstname'], $session['lastname']),
+                    'is_subscribed' => SessionManager::isUserSusbcribedAsStudent($session['id'], $userId)
+                );
+
+                $sessionsToBrowse[] = $sessionToBrowse;
+            }
+        }
+
+        return $sessionsToBrowse;
+    }
+
+}

+ 5 - 0
main/inc/lib/main_api.lib.php

@@ -327,6 +327,11 @@ define('ICON_SIZE_HUGE',    128);
 
 define('SHOW_TEXT_NEAR_ICONS', false);
 
+//Session catalog
+define('CATALOG_COURSES', 0);
+define('CATALOG_SESSIONS', 1);
+define('CATALOG_COURSES_SESSIONS', 2);
+
 /**
  * Inclusion of internationalization libraries
  */

+ 25 - 0
main/inc/lib/sessionmanager.lib.php

@@ -4857,4 +4857,29 @@ class SessionManager
 
         return $values;
     }
+
+    /**
+     * Check if user is subscribed inside a session as student
+     * @param int $sessionId The session id
+     * @param int $userId The user id
+     * @return boolean Whether is subscribed
+     */
+    public static function isUserSusbcribedAsStudent($sessionId, $userId) {
+        $sessionRelUserTable = Database::get_main_table(TABLE_MAIN_SESSION_USER);
+
+        $sessionId = Database::escape_string($sessionId);
+        $userId = Database::escape_string($userId);
+
+        $sql = "SELECT COUNT(1) AS qty FROM $sessionRelUserTable "
+                . "WHERE id_session = $sessionId AND id_user = $userId AND relation_type = 0";
+
+        $result = Database::fetch_assoc(Database::query($sql));
+
+        if (!empty($result) && $result['qty'] > 0) {
+            return true;
+        }
+
+        return false;
+    }
+
 }

+ 126 - 0
main/template/default/auth/courses_categories.php

@@ -7,6 +7,17 @@
 * @package chamilo.auth
 */
 $stok = Security::get_token();
+
+$showCourses = false;
+$showSessions = false;
+
+if ($catalogShowCoursesSessions == CATALOG_COURSES || $catalogShowCoursesSessions == CATALOG_COURSES_SESSIONS) {
+    $showCourses = true;
+}
+
+if ($catalogShowCoursesSessions == CATALOG_COURSES_SESSIONS || $catalogShowCoursesSessions == CATALOG_SESSIONS) {
+    $showSessions = true;
+}
 ?>
 <script>
     $(document).ready( function() {
@@ -26,6 +37,52 @@ $stok = Security::get_token();
                 }
             });
         });
+    
+        $('.courses-list-btn').toggle(function (e) {
+            e.preventDefault();
+
+            var $el = $(this);
+            var sessionId = getSessionId(this);
+
+            $el.children('img').remove();
+            $el.prepend('<?php echo Display::display_icon('nolines_minus.gif'); ?>');
+
+            $.ajax({
+                url: '<?php echo api_get_path(WEB_AJAX_PATH) . 'course.ajax.php' ?>',
+                type: 'GET',
+                dataType: 'json',
+                data: {
+                    a: 'display_sessions_courses',
+                    session: sessionId
+                },
+                success: function (response){
+                    var $container = $el.prev('.course-list');
+
+                    var $courseList = $('<ul>');
+
+                    $.each(response, function (index, course){
+                        $courseList.append('<li><div><strong>' + course.name + '</strong><br>' + course.coachName + '</div></li>');
+                    });
+
+                    $container.append($courseList).show(250);
+                }
+            });
+        }, function (e) {
+            e.preventDefault();
+
+            var $el = $(this);
+            var $container = $el.prev('.course-list');
+            $container.hide(250).empty();
+            
+            $el.children('img').remove();
+            $el.prepend('<?php echo Display::display_icon('nolines_plus.gif'); ?>');
+        });
+        
+        var getSessionId = function (el){
+            var parts = el.id.split('_');
+            
+            return parseInt(parts[1], 10);
+        };
     });
 </script>
 
@@ -33,6 +90,7 @@ $stok = Security::get_token();
     <div class="span3">
         <div id="course_category_well" class="well">
             <ul class="nav nav-list">
+                <?php if ($showCourses) { ?>
                 <?php if (intval($_GET['hidden_links']) != 1) { ?>
                 <form class="form-search" method="post" action="<?php echo api_get_self(); ?>?action=subscribe&amp;hidden_links=0">
                     <fieldset>
@@ -127,11 +185,66 @@ $stok = Security::get_token();
                 }
             }
             ?>
+                <?php } ?>
+                <?php if ($showSessions) { ?>
+                    <li class="nav-header"><?php echo get_lang('Sessions'); ?></li>
+                    <li>
+                        <?php if ($action == 'display_sessions') { ?>
+                            <strong><?php echo get_lang('SessionList'); ?></strong>
+                        <?php } else { ?>
+                            <a href="<?php echo api_get_self() ?>?action=display_sessions&hidden_links=<?php echo $hidden_links ?>"><?php echo get_lang('SessionList'); ?></a>
+                        <?php } ?>
+                    </li>
+                    <form>
+                        <div class="control-group">
+                            <button class="btn" type="submit"><?php echo get_lang('Search'); ?></button>
+                        </div>
+                    </form>
+                <?php } ?>
         </div>
     </div>
 
     <div class="span9">
         <?php
+        switch ($action) {
+            case 'display_sessions':
+                if (!empty($browseSessions)) {
+                    foreach ($browseSessions as $session) {
+                        $sessionId = $session['id'];
+                        ?>
+                        <div class="well_border">
+                            <div class="row">
+                                <div class="span1">
+                                    <span class="thumbnail"><?php
+                                        $sessionIconParams = array(
+                                            'id' => 'session_img_' . $sessionId
+                                        );
+
+                                        echo Display::return_icon('window_list.png', $session['name'], $sessionIconParams, ICON_SIZE_LARGE);
+                                        ?></span>
+                                </div>
+                                <div class="span7 categories-course-description">
+                                    <h3><?php echo $session['name'] ?></h3>
+                                    <p><?php echo $session['coach_name'] ?></p>
+                                    <div>
+                                        <div class="course-list"></div>
+                                        <a class="btn btn-link courses-list-btn" id="showsesion_<?php echo $sessionId ?>" href="#"><?php echo Display::display_icon('nolines_plus.gif'); ?> <?php echo get_lang('Courses') ?></a>
+                                        <?php if ($session['is_subscribed']) { ?>
+                                            <?php echo display_already_registered_in_session_label(); ?>
+                                        <?php } else { ?>
+                                            <a class="btn btn-primary" href=""><?php echo get_lang('Subscribe'); ?></a>
+                                        <?php } ?>
+                                    </div>
+                                </div>
+                            </div>
+                        </div>
+                        <?php
+                    }
+                } else {
+                    Display::display_warning_message(get_lang('ThereAreNoCoursesInThisCategory'));
+                }
+                break;
+            default:
         if (!empty($message)) { Display::display_confirmation_message($message, false); }
         if (!empty($error)) { Display::display_error_message($error, false); }
 
@@ -223,6 +336,8 @@ $stok = Security::get_token();
             if (!isset($_REQUEST['subscribe_user_with_password']) && !isset($_REQUEST['subscribe_course'])) {
                 Display::display_warning_message(get_lang('ThereAreNoCoursesInThisCategory'));
             }
+        }
+                break;
         }
         ?>
     </div>
@@ -336,3 +451,14 @@ function display_unregister_button($course, $stok, $search_term, $code)
 {
     echo ' <a class="btn btn-primary" href="'. api_get_self().'?action=unsubscribe&amp;sec_token='.$stok.'&amp;unsubscribe='.$course['code'].'&amp;search_term='.$search_term.'&amp;category_code='.$code.'">'.get_lang('Unsubscribe').'</a>';
 }
+
+/**
+ * Generate a label if the user has been  registered in session
+ * @return string The label
+ */
+function display_already_registered_in_session_label()
+{
+    $icon = Display::return_icon('students.gif', get_lang('Student'));
+
+    return Display::label($icon . ' ' . get_lang("AlreadyRegisteredToSession"), "info");
+}