Browse Source

Merge pull request #693 from AngelFQC/7363

Add certificates search - refs #7636
Angel Fernando Quiroz Campos 10 years ago
parent
commit
5a6b260774

+ 1 - 0
index.php

@@ -165,6 +165,7 @@ $controller->tpl->assign('navigation_course_links', $controller->return_navigati
 $controller->tpl->assign('notice_block', $controller->return_notice());
 $controller->tpl->assign('main_navigation_block', $controller->return_navigation_links());
 $controller->tpl->assign('help_block', $controller->return_help());
+$controller->tpl->assign('certificates_search_block', $controller->returnCertificatesSearchBlock());
 
 if (api_is_platform_admin() || api_is_drh()) {
     $controller->tpl->assign('skills_block', $controller->return_skills_links());

+ 114 - 0
main/gradebook/lib/GradebookUtils.php

@@ -1173,4 +1173,118 @@ class GradebookUtils
                WHERE id = '.$id;
         Database::query($sql);
     }
+
+    /**
+     * 
+     * Get the achieved certificates for a user in courses
+     * @param int $userId The user id
+     * @param type $includeNonPublicCertificates Whether include the non-plublic certificates
+     * @return array
+     */
+    public static function getUserCertificatesInCourses($userId, $includeNonPublicCertificates = true)
+    {
+        $userId = intval($userId);
+        $courseList = [];
+
+        $courses = CourseManager::get_courses_list_by_user_id($userId);
+
+        foreach ($courses as $course) {
+            if (!$includeNonPublicCertificates) {
+                $allowPublicCertificates = api_get_course_setting('allow_public_certificates', $course['code']);
+
+                if (empty($allowPublicCertificates)) {
+                    continue;
+                }
+            }
+
+            $courseGradebookCategory = Category::load(null, null, $course['code']);
+
+            if (empty($courseGradebookCategory)) {
+                continue;
+            }
+
+            $courseGradebookId = $courseGradebookCategory[0]->get_id();
+
+            $certificateInfo = GradebookUtils::get_certificate_by_user_id($courseGradebookId, $userId);
+
+            if (empty($certificateInfo)) {
+                continue;
+            }
+
+            $courseInfo = api_get_course_info($course['code']);
+
+            $courseList[] = [
+                'course' => $courseInfo['title'],
+                'score' => $certificateInfo['score_certificate'],
+                'date' => api_format_date($certificateInfo['created_at'], DATE_FORMAT_SHORT),
+                'link' => api_get_path(WEB_PATH) . "certificates/index.php?id={$certificateInfo['id']}"
+            ];
+        }
+
+        return $courseList;
+    }
+
+    /**
+     * Get the achieved certificates for a user in course sessions
+     * @param int $userId The user id
+     * @param type $includeNonPublicCertificates Whether include the non-plublic certificates
+     * @return array
+     */
+    public static function getUserCertificatesInSessions($userId, $includeNonPublicCertificates = true)
+    {
+        $userId = intval($userId);
+        $sessionList = [];
+
+        $sessions = SessionManager::get_sessions_by_user($userId);
+
+        foreach ($sessions as $session) {
+            if (empty($session['courses'])) {
+                continue;
+            }
+
+            $sessionCourses = SessionManager::get_course_list_by_session_id($session['session_id']);
+
+            foreach ($sessionCourses as $course) {
+                if (!$includeNonPublicCertificates) {
+                    $allowPublicCertificates = api_get_course_setting('allow_public_certificates', $course['code']);
+
+                    if (empty($allowPublicCertificates)) {
+                        continue;
+                    }
+                }
+
+                $courseGradebookCategory = Category::load(
+                    null,
+                    null,
+                    $course['code'],
+                    null,
+                    null,
+                    $session['session_id']
+                );
+
+                if (empty($courseGradebookCategory)) {
+                    continue;
+                }
+
+                $courseGradebookId = $courseGradebookCategory[0]->get_id();
+
+                $certificateInfo = GradebookUtils::get_certificate_by_user_id($courseGradebookId, $userId);
+
+                if (empty($certificateInfo)) {
+                    continue;
+                }
+
+                $sessionList[] = [
+                    'session' => $session['session_name'],
+                    'course' => $course['title'],
+                    'score' => $certificateInfo['score_certificate'],
+                    'date' => api_format_date($certificateInfo['created_at'], DATE_FORMAT_SHORT),
+                    'link' => api_get_path(WEB_PATH) . "certificates/index.php?id={$certificateInfo['id']}"
+                ];
+            }
+        }
+
+        return $sessionList;
+    }
+
 }

+ 48 - 0
main/gradebook/my_certificates.php

@@ -0,0 +1,48 @@
+<?php
+
+/* For licensing terms, see /license.txt */
+/**
+ * List of achieved certificates by the current user
+ * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
+ * @package chamilo.gradebook
+ */
+$cidReset = true;
+
+require_once '../inc/global.inc.php';
+
+if (api_is_anonymous()) {
+    api_not_allowed(true);
+}
+
+$userId = api_get_user_id();
+
+$courseList = GradebookUtils::getUserCertificatesInCourses($userId);
+$sessionList = GradebookUtils::getUserCertificatesInSessions($userId);
+
+$template = new Template(get_lang('MyCertificates'));
+
+$template->assign('course_list', $courseList);
+$template->assign('session_list', $sessionList);
+$content = $template->fetch('default/gradebook/my_certificates.tpl');
+
+if (empty($courseList) || empty($sessionList)) {
+    $template->assign(
+        'message',
+        Display::return_message(get_lang('YouNotYetAchievedCertificates'), 'warning')
+    );
+}
+
+if (api_get_setting('allow_public_certificates') == 'true') {
+    $template->assign(
+        'actions',
+        Display::toolbarButton(
+            get_lang('SearchCertificates'),
+            api_get_path(WEB_CODE_PATH) . "gradebook/search.php",
+            'search',
+            'info'
+        )
+    );
+}
+
+$template->assign('content', $content);
+$template->display_one_col_template();

+ 85 - 0
main/gradebook/search.php

@@ -0,0 +1,85 @@
+<?php
+
+/* For licensing terms, see /license.txt */
+/**
+ * Search user certificates if them are publics
+ * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
+ * @package chamilo.gradebook
+ */
+use \ChamiloSession as Session;
+
+$cidReset = true;
+
+require_once '../inc/global.inc.php';
+
+if (api_get_setting('allow_public_certificates') != 'true') {
+    api_not_allowed(
+        true,
+        Display::return_message(get_lang('CertificatesNotPublic'), 'warning')
+    );
+}
+
+$userId = isset($_GET['id']) ? intval($_GET['id']) : 0;
+
+$userList = $userInfo = $courseList = $sessionList = [];
+
+$searchForm = new FormValidator('search_form', 'post', null, null);
+$searchForm->addText('firstname', get_lang('Firstname'));
+$searchForm->addText('lastname', get_lang('Lastname'));
+$searchForm->addButtonSearch();
+
+if ($searchForm->validate()) {
+    $firstname = $searchForm->getSubmitValue('firstname');
+    $lastname = $searchForm->getSubmitValue('lastname');
+
+    $userList = UserManager::getUserByName($firstname, $lastname);
+
+    if (empty($userList)) {
+        Session::write('message', Display::return_message(get_lang('NoResults'), 'warning'));
+
+        Header::location(api_get_self());
+    }
+} elseif ($userId > 0) {
+    $userInfo = api_get_user_info($userId);
+
+    if (empty($userInfo)) {
+        Session::write('message', Display::return_message(get_lang('NoUser'), 'warning'));
+
+        Header::location(api_get_self());
+    }
+
+    $courseList = GradebookUtils::getUserCertificatesInCourses($userId, false);
+    $sessionList = GradebookUtils::getUserCertificatesInSessions($userId, false);
+
+    if (empty($courseList) && empty($sessionList)) {
+        Session::write(
+            'message',
+            Display::return_message(
+                sprintf(get_lang('TheUserXNotYetAchievedCertificates'), $userInfo['complete_name']),
+                'warning'
+            )
+        );
+
+        Header::location(api_get_self());
+    }
+}
+
+$template = new Template(get_lang('SearchCertificates'));
+
+$template->assign('search_form', $searchForm->returnForm());
+$template->assign('user_list', $userList);
+$template->assign('user_info', $userInfo);
+$template->assign('course_list', $courseList);
+$template->assign('session_list', $sessionList);
+
+if (Session::has('message')) {
+    $template->assign('message', Session::read('message'));
+    Session::erase('message');
+}
+
+$content = $template->fetch('default/gradebook/search.tpl');
+
+$template->assign('header', get_lang('SearchCertificates'));
+$template->assign('content', $content);
+
+$template->display_one_col_template();

+ 32 - 0
main/inc/lib/usermanager.lib.php

@@ -5356,4 +5356,36 @@ EOF;
 
         return Display::url(Display::img($userProfile['file']), $userInfo['profile_url']);
     }
+
+    /**
+     * Get users whose name matches $firstname and $lastname
+     * @param string $firstname Firstname to search
+     * @param string $lastname Lastname to search
+     * @return array The user list
+     */
+    public static function getUserByName($firstname, $lastname)
+    {
+        $firstname = Database::escape_string($firstname);
+        $lastname = Database::escape_string($lastname);
+
+        $userTable = Database::get_main_table(TABLE_MAIN_USER);
+
+        $sql = <<<SQL
+            SELECT id, username, lastname, firstname
+            FROM $userTable
+            WHERE firstname LIKE '$firstname%' AND
+                lastname LIKE '$lastname%'
+SQL;
+
+        $result = Database::query($sql);
+
+        $users = [];
+
+        while ($resultData = Database::fetch_object($result)) {
+            $users[] = $resultData;
+        }
+
+        return $users;
+    }
+
 }

+ 37 - 0
main/inc/lib/userportal.lib.php

@@ -1425,4 +1425,41 @@ class IndexManager
     {
         return CourseManager::return_hot_courses();
     }
+
+    /**
+     * Generate the block for show a panel with links to My Certificates and Certificates Search pages
+     * @return string The HTML code for the panel
+     */
+    public function returnCertificatesSearchBlock()
+    {
+        $certificatesItem = Display::tag(
+            'li',
+            Display::url(
+                get_lang('MyCertificates'),
+                api_get_path(WEB_CODE_PATH) . "gradebook/my_certificates.php"
+            )
+        );
+
+        $searchItem = null;
+
+        if (api_get_setting('allow_public_certificates') == 'true') {
+            $searchItem = Display::tag(
+                'li',
+                Display::url(
+                    get_lang('Search'),
+                    api_get_path(WEB_CODE_PATH) . "gradebook/search.php"
+                )
+            );
+        }
+
+        return Display::panel(
+            Display::tag(
+                'ul',
+                implode(' ', [$certificatesItem, $searchItem]),
+                ['class' => 'nav nav-pills nav-stacked']
+            ),
+            get_lang('Certificates')
+        );
+    }
+
 }

+ 4 - 0
main/lang/english/trad4all.inc.php

@@ -7233,4 +7233,8 @@ $InternalLogin = "Internal login";
 $AlreadyLoggedIn = "You are already logged in";
 $Draggable = "Draggable";
 $Incorrect = "Incorrect";
+$YouNotYetAchievedCertificates = "You not yet achieved certificates";
+$SearchCertificates = "Search certificates";
+$TheUserXNotYetAchievedCertificates = "The user %s not yet achieved certificates";
+$CertificatesNotPublic = "Certificates not public";
 ?>

+ 4 - 0
main/lang/spanish/trad4all.inc.php

@@ -7237,4 +7237,8 @@ $SetTutor = "Hacer tutor";
 $UniqueAnswerImage = "Respuesta de imagen única";
 $Draggable = "Arrastrable";
 $Incorrect = "Incorrecto";
+$YouNotYetAchievedCertificates = "Aún no ha logrado certificados";
+$SearchCertificates = "Buscar certificados";
+$TheUserXNotYetAchievedCertificates = "El usuario %s aún no ha logrado certificados";
+$CertificatesNotPublic = "Los certificados no son públicos";
 ?>

+ 63 - 0
main/template/default/gradebook/my_certificates.tpl

@@ -0,0 +1,63 @@
+{% if course_list is not empty %}
+    <h1 class="page-header">{{ "Courses"|get_lang }}</h1>
+
+    <div class="table-responsive">
+        <table class="table table-hover table-striped">
+            <thead>
+                <tr>
+                    <th>{{ "Course"|get_lang }}</th>
+                    <th class="text-right">{{ "Score"|get_lang }}</th>
+                    <th class="text-center">{{ "Fecha"|get_lang }}</th>
+                    <th class="text-right">&nbsp;</th>
+                </tr>
+            </thead>
+            <tbody>
+                {% for row in course_list %}
+                    <tr>
+                        <td>{{ row.course }}</td>
+                        <td class="text-right">{{ row.score }}</td>
+                        <td class="text-center">{{ row.date }}</td>
+                        <td class="text-right">
+                            <a href="{{ row.link }}" target="_blank" class="btn btn-default">
+                                <i class="fa fa-external-link"></i> {{ 'Certificate'|get_lang }}
+                            </a>
+                        </td>
+                    </tr>
+                {% endfor %}
+            </tbody>
+        </table>
+    </div>
+{% endif %}
+
+{% if session_list is not empty %}
+    <h1 class="page-header">{{ "Sessions"|get_lang }}</h1>
+
+    <div class="table-responsive">
+        <table class="table table-hover table-striped">
+            <thead>
+                <tr>
+                    <th>{{ "Session"|get_lang }}</th>
+                    <th>{{ "Course"|get_lang }}</th>
+                    <th class="text-right">{{ "Score"|get_lang }}</th>
+                    <th class="text-center">{{ "Fecha"|get_lang }}</th>
+                    <th class="text-right">&nbsp;</th>
+                </tr>
+            </thead>
+            <tbody>
+                {% for row in session_list %}
+                    <tr>
+                        <td>{{ row.session }}</td>
+                        <td>{{ row.course }}</td>
+                        <td class="text-right">{{ row.score }}</td>
+                        <td class="text-center">{{ row.date }}</td>
+                        <td class="text-right">
+                            <a href="{{ row.link }}" target="_blank" class="btn btn-default">
+                                <i class="fa fa-external-link"></i> {{ 'Certificate'|get_lang }}
+                            </a>
+                        </td>
+                    </tr>
+                {% endfor %}
+            </tbody>
+        </table>
+    </div>
+{% endif %}

+ 98 - 0
main/template/default/gradebook/search.tpl

@@ -0,0 +1,98 @@
+{{ search_form }}
+
+{% if user_list is not empty %}
+    <div class="table-responsive">
+        <table class="table table-hover table-striped">
+            <thead>
+                <tr>
+                    <th>{{ "FirstName"|get_lang }}</th>
+                    <th>{{ "LastName"|get_lang }}</th>
+                    <th class="text-center">{{ "Username"|get_lang }}</th>
+                    <th class="text-right">&nbsp;</th>
+                </tr>
+            </thead>
+            <tbody>
+                {% for user in user_list %}
+                    <tr>
+                        <td>{{ user.firstname }}</td>
+                        <td>{{ user.lastname }}</td>
+                        <td class="text-center">{{ user.username }}</td>
+                        <td class="text-right">
+                            <a href="{{ _p.web_main }}gradebook/search.php?id={{ user.id }}" class="btn btn-default">
+                                <i class="fa fa-external-link"></i> {{ "Certificates"|get_lang }}
+                            </a>
+                        </td>
+                    </tr>
+                {% endfor %}
+            </tbody>
+        </table>
+    </div>
+{% endif %}
+
+{% if course_list is not empty or session_list is not empty %}
+    <h2>{{ user_info.complete_name }}</h2>
+
+    {% if course_list is not empty %}
+        <h3 class="page-header">{{ "Courses"|get_lang }}</h3>
+
+        <div class="table-responsive">
+            <table class="table table-hover table-striped">
+                <thead>
+                    <tr>
+                        <th>{{ "Course"|get_lang }}</th>
+                        <th class="text-right">{{ "Score"|get_lang }}</th>
+                        <th class="text-center">{{ "Fecha"|get_lang }}</th>
+                        <th class="text-right">&nbsp;</th>
+                    </tr>
+                </thead>
+                <tbody>
+                    {% for row in course_list %}
+                        <tr>
+                            <td>{{ row.course }}</td>
+                            <td class="text-right">{{ row.score }}</td>
+                            <td class="text-center">{{ row.date }}</td>
+                            <td class="text-right">
+                                <a href="{{ row.link }}" target="_blank" class="btn btn-default">
+                                    <i class="fa fa-external-link"></i> {{ 'Certificate'|get_lang }}
+                                </a>
+                            </td>
+                        </tr>
+                    {% endfor %}
+                </tbody>
+            </table>
+        </div>
+    {% endif %}
+
+    {% if session_list is not empty %}
+        <h3 class="page-header">{{ "Sessions"|get_lang }}</h3>
+
+        <div class="table-responsive">
+            <table class="table table-hover table-striped">
+                <thead>
+                    <tr>
+                        <th>{{ "Session"|get_lang }}</th>
+                        <th>{{ "Course"|get_lang }}</th>
+                        <th class="text-right">{{ "Score"|get_lang }}</th>
+                        <th class="text-center">{{ "Fecha"|get_lang }}</th>
+                        <th class="text-right">&nbsp;</th>
+                    </tr>
+                </thead>
+                <tbody>
+                    {% for row in session_list %}
+                        <tr>
+                            <td>{{ row.session }}</td>
+                            <td>{{ row.course }}</td>
+                            <td class="text-right">{{ row.score }}</td>
+                            <td class="text-center">{{ row.date }}</td>
+                            <td class="text-right">
+                                <a href="{{ row.link }}" target="_blank" class="btn btn-default">
+                                    <i class="fa fa-external-link"></i> {{ 'Certificate'|get_lang }}
+                                </a>
+                            </td>
+                        </tr>
+                    {% endfor %}
+                </tbody>
+            </table>
+        </div>
+    {% endif %}
+{% endif %}

+ 3 - 0
main/template/default/layout/layout_2_col.tpl

@@ -42,6 +42,9 @@
         {# Skills #}
         {{ skills_block }}
 
+        {# Certificates search block #}
+        {{ certificates_search_block }}
+
 		{# Notice #}
 		{{ notice_block }}
 

+ 1 - 0
user_portal.php

@@ -183,6 +183,7 @@ $controller->tpl->assign('course_block', $controller->return_course_block());
 $controller->tpl->assign('navigation_course_links', $controller->return_navigation_links());
 $controller->tpl->assign('search_block', $controller->return_search_block());
 $controller->tpl->assign('classes_block', $controller->return_classes_block());
+$controller->tpl->assign('certificates_search_block', $controller->returnCertificatesSearchBlock());
 
 //if (api_is_platform_admin() || api_is_drh()) {
 $controller->tpl->assign('skills_block', $controller->return_skills_links());