Kaynağa Gözat

Create My Progress page for gamification mode - refs BT#10092 #TMI

Angel Fernando Quiroz Campos 9 yıl önce
ebeveyn
işleme
ec916c4533

+ 107 - 0
main/gamification/my_progress.php

@@ -0,0 +1,107 @@
+<?php
+
+/* For licensing terms, see /license.txt */
+/**
+ * See the progress for a user when the gamification mode is active
+ * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
+ * @package chamilo.gamification
+ */
+$cidReset = true;
+require_once '../inc/global.inc.php';
+
+$this_section = SECTION_TRACKING;
+$nameTools = get_lang('MyProgress');
+
+api_block_anonymous_users();
+
+if (api_get_setting('gamification_mode') == '0') {
+    api_not_allowed(true);
+}
+
+$userId = api_get_user_id();
+$sessionId = isset($_GET['session_id']) ? intval($_GET['session_id']) : 0;
+$allowAccess = false;
+
+$userManager = UserManager::getManager();
+$entityManager = Database::getManager();
+
+$user = $userManager->findUserBy(['id' => $userId]);
+$sessionCourseSubscriptions = $user->getSessionCourseSubscriptions();
+$currentSession = $entityManager->find('ChamiloCoreBundle:Session', $sessionId);
+
+$sessionList = [];
+
+foreach ($sessionCourseSubscriptions as $subscription) {
+    $session = $subscription->getSession();
+
+    if ($currentSession && $currentSession->getId() === $session->getId()) {
+        $allowAccess = true;
+    }
+
+    $sessionList[] = $session;
+}
+
+if ($currentSession && !$allowAccess) {
+    api_not_allowed(true);
+}
+
+$template = new Template($nameTools);
+$template->assign('user', $user);
+$template->assign(
+    'user_avatar',
+    SocialManager::show_social_avatar_block('home', 0, $user->getId())
+);
+$template->assign(
+    'gamification_stars',
+     GamificationUtils::getTotalUserStars($user->getId(), $user->getStatus())
+);
+$template->assign(
+    'gamification_points',
+     GamificationUtils::getTotalUserPoints($user->getId(), $user->getStatus())
+);
+$template->assign(
+    'gamification_progress',
+     GamificationUtils::getSessionProgress($user->getId(), $user->getStatus())
+);
+$template->assign('sessions', $sessionList);
+$template->assign('current_session', $currentSession);
+
+if ($currentSession) {
+    $sessionData = [];
+
+    $sessionCourses = $currentSession->getCourses();
+
+    foreach ($sessionCourses as $sessionCourse) {
+        $course = $sessionCourse->getCourse();
+
+        $courseData = [
+            'title' => $course->getTitle(),
+            'stats' => []
+        ];
+
+        $learningPathList = new LearnpathList($user->getId(), $course->getCode(), $currentSession->getId());
+
+        foreach ($learningPathList->list as $learningPathId => $learningPath) {
+            $courseData['stats'][] = [
+                $learningPath['lp_name'],
+                'newscorm/lp_controller.php?' . http_build_query([
+                    'action' => 'stats',
+                    'cidReq' => $course->getCode(),
+                    'id_session' => $currentSession->getId(),
+                    'gidReq' => 0,
+                    'lp_id' => $learningPathId
+                ]) . api_get_cidreq()
+            ];
+        }
+
+        $sessionData[$course->getId()] = $courseData;
+    }
+
+    $template->assign('session_data', $sessionData);
+}
+
+$layout = $template->get_template('gamification/my_progress.tpl');
+
+$template->assign('header', $nameTools);
+$template->assign('content', $template->fetch($layout));
+$template->display_one_col_template();

+ 52 - 0
main/inc/lib/GamificationUtils.php

@@ -160,4 +160,56 @@ class GamificationUtils
         return $totalStars / count($courses);
     }
 
+    /**
+     * Get the stars on sessions with gamification mode
+     * @param int $userId The user ID
+     * @param int $userStatus The user Status
+     * @return int
+     */
+    public static function getTotalUserStars($userId, $userStatus)
+    {
+        $stars = 0;
+
+        $sessions = SessionManager::getSessionsFollowedByUser(
+            $userId,
+            $userStatus
+        );
+
+        if (empty($sessions)) {
+            return 0;
+        }
+
+        foreach ($sessions as $session) {
+            $stars += self::getSessionStars($session['id'], $userId);
+        }
+
+        return $stars;
+    }
+
+    /**
+     * Get the total progress on sessions with gamification mode
+     * @param int $userId The user ID
+     * @param int $userStatus The user Status
+     * @return int
+     */
+    public static function getTotalUserProgress($userId, $userStatus)
+    {
+        $progress = 0;
+
+        $sessions = SessionManager::getSessionsFollowedByUser(
+            $userId,
+            $userStatus
+        );
+
+        if (empty($sessions)) {
+            return 0;
+        }
+
+        foreach ($sessions as $session) {
+            $progress += self::getSessionProgress($session['id'], $userId);
+        }
+
+        return $progress;
+    }
+
 }

+ 9 - 1
main/inc/lib/banner.lib.php

@@ -64,8 +64,16 @@ function get_tabs() {
         $navigation['session_my_space']['title'] = get_lang('MySpace');
         $navigation['session_my_space']['key'] = 'my-space';
     } else {
+        $navigation['session_my_progress']['url'] = api_get_path(WEB_CODE_PATH);
             // Link to my progress
-            $navigation['session_my_progress']['url'] = api_get_path(WEB_CODE_PATH).'auth/my_progress.php';
+        switch (api_get_setting('gamification_mode')) {
+            case 1:
+                $navigation['session_my_progress']['url'] .= 'gamification/my_progress.php';
+                break;
+            default:
+                $navigation['session_my_progress']['url'] .= 'auth/my_progress.php';
+        }
+
             $navigation['session_my_progress']['title'] = get_lang('MyProgress');
             $navigation['session_my_progress']['key'] = 'my-progress';
     }

+ 50 - 0
main/template/default/gamification/my_progress.tpl

@@ -0,0 +1,50 @@
+<div class="row">
+    <div class="col-md-3">
+        <div class="well">
+            {{ user_avatar }}
+            <p>{{ user.getCompleteName() }}</p>
+            <p>{{ 'Stars'|get_lang ~ ' ' ~ gamification_stars }}</p>
+            <p>{{ 'XPoints'|get_lang|format(gamification_points) }}</p>
+            <p>{{ 'GamificationProgress'|get_lang ~ ' ' ~ gamification_progress }}</p>
+        </div>
+
+        <h4>{{ 'ShowProgress'|get_lang }}</h4>
+        <div class="list-group">
+            {% for session in sessions %}
+                <a href="{{ _p.self ~ '?' ~ {"session_id": session.getId}|url_encode() }}" class="list-group-item {{ current_session and session.getId == current_session.getId ? 'active' }}">{{ session.getName }}</a>
+            {% endfor %}
+        </div>
+    </div>
+
+    <div class="col-md-9">
+        {% if current_session %}
+            <h2 class="page-header">{{ current_session.getName() }}</h2>
+
+            {% for course_id, course in session_data %}
+                <h3>{{ course.title }}</h3>
+
+                <div class="panel-group" id="course-accordion" role="tablist" aria-multiselectable="true">
+                    {% for stats_url in course.stats %}
+                        {% set panel_id = course_id ~ '-' ~ loop.index %}
+                        <div class="panel panel-default">
+                            <div class="panel-heading" role="tab" id="heading-{{ panel_id }}">
+                                <h4 class="panel-title">
+                                    <a role="button" data-toggle="collapse" data-parent="#course-accordion" href="#collapse-{{ panel_id }}" aria-expanded="true" aria-controls="collapse-{{ panel_id }}">
+                                        {{ stats_url.0 }}
+                                    </a>
+                                </h4>
+                            </div>
+                            <div id="collapse-{{ panel_id }}" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="heading-{{ panel_id }}">
+                                <div class="panel-body">
+                                    <div class="embed-responsive embed-responsive-4by3">
+                                        <iframe src="{{ _p.web_main ~ stats_url.1 }}"></iframe>
+                                    </div>
+                                </div>
+                            </div>
+                        </div>
+                    {% endfor %}
+                </div>
+            {% endfor %}
+        {% endif %}
+    </div>
+</div>