Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
UserHelper | |
0.00% |
0 / 30 |
|
0.00% |
0 / 9 |
380 | |
0.00% |
0 / 1 |
renderHtmlUser | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
getUser | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getUserStringProp | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
getUserLabel | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getUserLogin | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getUserMail | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getUserFirstName | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
getUserLastName | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
getUserName | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2014 (original work) Open Assessment Technologies SA; |
19 | * |
20 | * |
21 | */ |
22 | |
23 | namespace oat\tao\helpers; |
24 | |
25 | use oat\generis\model\GenerisRdf; |
26 | use oat\generis\model\OntologyRdfs; |
27 | use oat\oatbox\service\ServiceManager; |
28 | use oat\oatbox\user\User; |
29 | use core_kernel_classes_Resource; |
30 | use core_kernel_users_GenerisUser; |
31 | use Jig\Utils\StringUtils; |
32 | use oat\oatbox\user\UserService; |
33 | |
34 | /** |
35 | * Utility class to render a User |
36 | * |
37 | * @author Joel Bout <joel@taotesting.com> |
38 | * @package tao |
39 | */ |
40 | class UserHelper |
41 | { |
42 | public static function renderHtmlUser($userId) |
43 | { |
44 | // assume generis user |
45 | $user = self::getUser($userId); |
46 | $labels = $user->getPropertyValues(OntologyRdfs::RDFS_LABEL); |
47 | $mails = $user->getPropertyValues(GenerisRdf::PROPERTY_USER_MAIL); |
48 | $label = !empty($labels) ? (string)reset($labels) : '(' . $userId . ')'; |
49 | $label = StringUtils::wrapLongWords($label); |
50 | $mail = !empty($mails) ? (string)reset($mails) : ''; |
51 | return !empty($mail) |
52 | ? '<a href="mailto:' . $mail . '">' . $label . '</a>' |
53 | : $label; |
54 | } |
55 | |
56 | /** |
57 | * Gets a user from a URI |
58 | * |
59 | * @todo Make a stronger helper which take care of provider (LDAP, OAUTH, etc.) |
60 | * |
61 | * @param string $userId |
62 | * @return User |
63 | */ |
64 | public static function getUser($userId) |
65 | { |
66 | $userService = ServiceManager::getServiceManager()->get(UserService::SERVICE_ID); |
67 | return $userService->getUser($userId); |
68 | } |
69 | |
70 | /** |
71 | * Gets the value of a string property from a user |
72 | * @param User $user |
73 | * @param string $property |
74 | * @return string |
75 | */ |
76 | public static function getUserStringProp(User $user, $property) |
77 | { |
78 | $value = $user->getPropertyValues($property); |
79 | return empty($value) ? '' : current($value); |
80 | } |
81 | |
82 | /** |
83 | * Gets the user's label |
84 | * @param User $user |
85 | * @return string |
86 | */ |
87 | public static function getUserLabel(User $user) |
88 | { |
89 | return self::getUserStringProp($user, OntologyRdfs::RDFS_LABEL); |
90 | } |
91 | |
92 | /** |
93 | * Gets the user's login |
94 | * @param User $user |
95 | * @return string |
96 | */ |
97 | public static function getUserLogin(User $user) |
98 | { |
99 | return self::getUserStringProp($user, GenerisRdf::PROPERTY_USER_LOGIN); |
100 | } |
101 | |
102 | /** |
103 | * Gets the user's email |
104 | * @param User $user |
105 | * @return string |
106 | */ |
107 | public static function getUserMail(User $user) |
108 | { |
109 | return self::getUserStringProp($user, GenerisRdf::PROPERTY_USER_MAIL); |
110 | } |
111 | |
112 | /** |
113 | * Gets the user's first name |
114 | * @param User $user |
115 | * @param bool $defaultToLabel |
116 | * @return string |
117 | */ |
118 | public static function getUserFirstName(User $user, $defaultToLabel = false) |
119 | { |
120 | $firstName = self::getUserStringProp($user, GenerisRdf::PROPERTY_USER_FIRSTNAME); |
121 | |
122 | if (empty($firstName) && $defaultToLabel) { |
123 | $firstName = self::getUserLabel($user); |
124 | } |
125 | |
126 | return $firstName; |
127 | } |
128 | |
129 | /** |
130 | * Gets the user's last name |
131 | * @param User $user |
132 | * @param bool $defaultToLabel |
133 | * @return string |
134 | */ |
135 | public static function getUserLastName(User $user, $defaultToLabel = false) |
136 | { |
137 | $lastName = self::getUserStringProp($user, GenerisRdf::PROPERTY_USER_LASTNAME); |
138 | |
139 | if (empty($lastName) && $defaultToLabel) { |
140 | $lastName = self::getUserLabel($user); |
141 | } |
142 | |
143 | return $lastName; |
144 | } |
145 | |
146 | /** |
147 | * Gets the full user name |
148 | * @param User $user |
149 | * @param bool $defaultToLabel |
150 | * @return string |
151 | */ |
152 | public static function getUserName(User $user, $defaultToLabel = false) |
153 | { |
154 | $firstName = self::getUserStringProp($user, GenerisRdf::PROPERTY_USER_FIRSTNAME); |
155 | $lastName = self::getUserStringProp($user, GenerisRdf::PROPERTY_USER_LASTNAME); |
156 | |
157 | $userName = trim($firstName . ' ' . $lastName); |
158 | |
159 | if (empty($userName) && $defaultToLabel) { |
160 | $userName = self::getUserLabel($user); |
161 | } |
162 | |
163 | return $userName; |
164 | } |
165 | } |