Passed
Push — master ( 18111a...f46ab8 )
by Julito
12:19
created

SkillRelUserModel::getAssertionUrl()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 12
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Entity\SkillRelUser;
6
7
class SkillRelUserModel extends Model
8
{
9
    public $columns = [
10
        'id',
11
        'user_id',
12
        'skill_id',
13
        'acquired_skill_at',
14
        'assigned_by',
15
        'course_id',
16
        'session_id',
17
    ];
18
19
    public function __construct()
20
    {
21
        $this->table = Database::get_main_table(TABLE_MAIN_SKILL_REL_USER);
22
    }
23
24
    /**
25
     * @param array $skill_list
26
     *
27
     * @return array
28
     */
29
    public function getUserBySkills($skill_list)
30
    {
31
        $users = [];
32
        if (!empty($skill_list)) {
33
            $skill_list = array_map('intval', $skill_list);
34
            $skill_list = implode("', '", $skill_list);
35
36
            $sql = "SELECT user_id FROM {$this->table}
37
                    WHERE skill_id IN ('$skill_list') ";
38
39
            $result = Database::query($sql);
40
            $users = Database::store_result($result, 'ASSOC');
41
        }
42
43
        return $users;
44
    }
45
46
    /**
47
     * Get the achieved skills for the user.
48
     *
49
     * @param int $userId
50
     * @param int $courseId  Optional. The course id
51
     * @param int $sessionId Optional. The session id
52
     *
53
     * @return array The skill list. Otherwise return false
54
     */
55
    public function getUserSkills($userId, $courseId = 0, $sessionId = 0)
56
    {
57
        if (empty($userId)) {
58
            return [];
59
        }
60
61
        $courseId = (int) $courseId;
62
        $sessionId = $sessionId ? (int) $sessionId : null;
63
        $whereConditions = [
64
            'user_id = ? ' => (int) $userId,
65
        ];
66
67
        if ($courseId > 0) {
68
            $whereConditions['AND course_id = ? '] = $courseId;
69
            $whereConditions['AND session_id = ?'] = $sessionId;
70
        }
71
72
        $result = Database::select(
73
            'skill_id',
74
            $this->table,
75
            [
76
                'where' => $whereConditions,
77
            ],
78
            'all'
79
        );
80
81
        return $result;
82
    }
83
84
    /**
85
     * Get the relation data between user and skill.
86
     *
87
     * @param int $userId    The user id
88
     * @param int $skillId   The skill id
89
     * @param int $courseId  Optional. The course id
90
     * @param int $sessionId Optional. The session id
91
     *
92
     * @return array The relation data. Otherwise return false
93
     */
94
    public function getByUserAndSkill($userId, $skillId, $courseId = 0, $sessionId = 0)
95
    {
96
        $sql = "SELECT * FROM {$this->table} WHERE user_id = %d AND skill_id = %d ";
97
98
        if ($courseId > 0) {
99
            $sql .= "AND course_id = %d ".api_get_session_condition($sessionId, true);
100
        }
101
102
        $sql = sprintf(
103
            $sql,
104
            $userId,
105
            $skillId,
106
            $courseId
107
        );
108
109
        $result = Database::query($sql);
110
111
        return Database::fetch_assoc($result);
112
    }
113
114
    /**
115
     * Get the URL for the issue.
116
     *
117
     * @return string
118
     */
119
    public static function getIssueUrl(SkillRelUser $skillIssue)
120
    {
121
        return api_get_path(WEB_PATH)."badge/{$skillIssue->getId()}";
122
    }
123
124
    /**
125
     * Get the URL for the All issues page.
126
     *
127
     * @return string
128
     */
129
    public static function getIssueUrlAll(SkillRelUser $skillIssue)
130
    {
131
        return api_get_path(WEB_PATH)."skill/{$skillIssue->getSkill()->getId()}/user/{$skillIssue->getUser()->getId()}";
132
    }
133
134
    /**
135
     * Get the URL for the assertion.
136
     *
137
     * @return string
138
     */
139
    public static function getAssertionUrl(SkillRelUser $skillIssue)
140
    {
141
        $url = api_get_path(WEB_CODE_PATH).'badge/assertion.php?';
142
143
        $url .= http_build_query([
144
            'user' => $skillIssue->getUser()->getId(),
145
            'skill' => $skillIssue->getSkill()->getId(),
146
            'course' => $skillIssue->getCourse() ? $skillIssue->getCourse()->getId() : 0,
147
            'session' => $skillIssue->getSession() ? $skillIssue->getSession()->getId() : 0,
148
        ]);
149
150
        return $url;
151
    }
152
}
153