1 | <?php |
||
2 | /* For licensing terms, see /license.txt */ |
||
3 | |||
4 | use Chamilo\CoreBundle\Entity\Skill; |
||
0 ignored issues
–
show
|
|||
5 | use Skill as SkillManager; |
||
6 | |||
7 | /** |
||
8 | * Page for assign skills to a user. |
||
9 | * |
||
10 | * @author: Jose Loguercio <[email protected]> |
||
11 | */ |
||
12 | require_once __DIR__.'/../inc/global.inc.php'; |
||
13 | |||
14 | $userId = isset($_REQUEST['user']) ? (int) $_REQUEST['user'] : 0; |
||
15 | |||
16 | if (empty($userId)) { |
||
17 | api_not_allowed(true); |
||
18 | } |
||
19 | |||
20 | SkillManager::isAllowed($userId); |
||
21 | |||
22 | $user = api_get_user_entity($userId); |
||
23 | |||
24 | if (!$user) { |
||
0 ignored issues
–
show
|
|||
25 | api_not_allowed(true); |
||
26 | } |
||
27 | |||
28 | $entityManager = Database::getManager(); |
||
29 | $skillManager = new SkillManager(); |
||
30 | $skillRepo = $entityManager->getRepository('ChamiloCoreBundle:Skill'); |
||
31 | $skillRelSkill = $entityManager->getRepository('ChamiloCoreBundle:SkillRelSkill'); |
||
32 | $skillLevelRepo = $entityManager->getRepository('ChamiloSkillBundle:Level'); |
||
33 | $skillUserRepo = $entityManager->getRepository('ChamiloCoreBundle:SkillRelUser'); |
||
34 | |||
35 | $skillLevels = api_get_configuration_value('skill_levels_names'); |
||
36 | |||
37 | $skillsOptions = ['' => get_lang('Select')]; |
||
38 | $acquiredLevel = ['' => get_lang('None')]; |
||
39 | $formDefaultValues = []; |
||
40 | |||
41 | if (empty($skillLevels)) { |
||
42 | $skills = $skillRepo->findBy([ |
||
43 | 'status' => Skill::STATUS_ENABLED, |
||
44 | ]); |
||
45 | /** @var Skill $skill */ |
||
46 | foreach ($skills as $skill) { |
||
47 | $skillsOptions[$skill->getId()] = $skill->getName(); |
||
48 | } |
||
49 | } else { |
||
50 | // Get only root elements |
||
51 | $skills = $skillManager->getChildren(1); |
||
52 | foreach ($skills as $skill) { |
||
53 | $skillsOptions[$skill['data']['id']] = $skill['data']['name']; |
||
54 | } |
||
55 | } |
||
56 | $skillIdFromGet = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0; |
||
57 | $currentValue = isset($_REQUEST['current_value']) ? (int) $_REQUEST['current_value'] : 0; |
||
58 | $currentLevel = isset($_REQUEST['current']) ? (int) str_replace('sub_skill_id_', '', $_REQUEST['current']) : 0; |
||
59 | |||
60 | $subSkillList = isset($_REQUEST['sub_skill_list']) ? explode(',', $_REQUEST['sub_skill_list']) : []; |
||
61 | $subSkillList = array_unique($subSkillList); |
||
62 | |||
63 | if (!empty($subSkillList)) { |
||
64 | // Compare asked skill with current level |
||
65 | $correctLevel = false; |
||
66 | if (isset($subSkillList[$currentLevel]) && $subSkillList[$currentLevel] == $currentValue) { |
||
67 | $correctLevel = true; |
||
68 | } |
||
69 | |||
70 | // Level is wrong probably user change the level. Fix the subSkillList array |
||
71 | if (!$correctLevel) { |
||
72 | $newSubSkillList = []; |
||
73 | $counter = 0; |
||
74 | foreach ($subSkillList as $subSkillId) { |
||
75 | if ($counter == $currentLevel) { |
||
76 | $subSkillId = $currentValue; |
||
77 | } |
||
78 | $newSubSkillList[$counter] = $subSkillId; |
||
79 | if ($counter == $currentLevel) { |
||
80 | break; |
||
81 | } |
||
82 | $counter++; |
||
83 | } |
||
84 | $subSkillList = $newSubSkillList; |
||
85 | } |
||
86 | } |
||
87 | |||
88 | if (!empty($currentLevel)) { |
||
89 | $level = $currentLevel + 1; |
||
90 | if ($level < count($subSkillList)) { |
||
91 | $remove = count($subSkillList) - $currentLevel; |
||
92 | $newSubSkillList = array_slice($subSkillList, 0, count($subSkillList) - $level); |
||
93 | $subSkillList = $newSubSkillList; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | $skillId = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : key($skillsOptions); |
||
98 | $skill = $skillRepo->find($skillId); |
||
99 | $profile = false; |
||
100 | if ($skill) { |
||
101 | $profile = $skill->getProfile(); |
||
102 | } |
||
103 | |||
104 | if (!empty($subSkillList)) { |
||
105 | $skillFromLastSkill = $skillRepo->find(end($subSkillList)); |
||
106 | if ($skillFromLastSkill) { |
||
107 | $profile = $skillFromLastSkill->getProfile(); |
||
108 | } |
||
109 | } |
||
110 | |||
111 | if (!$profile) { |
||
112 | $skillRelSkill = new SkillRelSkill(); |
||
113 | $parents = $skillRelSkill->getSkillParents($skillId); |
||
114 | krsort($parents); |
||
115 | foreach ($parents as $parent) { |
||
116 | $skillParentId = $parent['skill_id']; |
||
117 | $profile = $skillRepo->find($skillParentId)->getProfile(); |
||
118 | |||
119 | if ($profile) { |
||
120 | break; |
||
121 | } |
||
122 | |||
123 | if (!$profile && $parent['parent_id'] == 0) { |
||
124 | $profile = $skillLevelRepo->findAll(); |
||
125 | $profile = isset($profile[0]) ? $profile[0] : false; |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | |||
130 | if ($profile) { |
||
131 | $profileId = $profile->getId(); |
||
132 | $levels = $skillLevelRepo->findBy([ |
||
133 | 'profile' => $profileId, |
||
134 | ]); |
||
135 | $profileLevels = []; |
||
136 | foreach ($levels as $level) { |
||
137 | $profileLevels[$level->getPosition()][$level->getId()] = $level->getName(); |
||
138 | } |
||
139 | |||
140 | ksort($profileLevels); // Sort the array by Position. |
||
141 | |||
142 | foreach ($profileLevels as $profileLevel) { |
||
143 | $profileId = key($profileLevel); |
||
144 | $acquiredLevel[$profileId] = $profileLevel[$profileId]; |
||
145 | } |
||
146 | } |
||
147 | |||
148 | $formDefaultValues = ['skill' => $skillId]; |
||
149 | $newSubSkillList = []; |
||
150 | $disableList = []; |
||
151 | |||
152 | $currentUrl = api_get_self().'?user='.$userId.'¤t='.$currentLevel; |
||
153 | |||
154 | $form = new FormValidator('assign_skill', 'POST', $currentUrl); |
||
155 | $form->addHeader(get_lang('AssignSkill')); |
||
156 | $form->addText('user_name', get_lang('UserName'), false); |
||
157 | |||
158 | $levelName = get_lang('Skill'); |
||
159 | if (!empty($skillLevels)) { |
||
160 | if (isset($skillLevels['levels'][1])) { |
||
161 | $levelName = get_lang($skillLevels['levels'][1]); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | $form->addSelect('skill', $levelName, $skillsOptions, ['id' => 'skill']); |
||
166 | |||
167 | if (!empty($skillIdFromGet)) { |
||
168 | if (empty($subSkillList)) { |
||
169 | $subSkillList[] = $skillIdFromGet; |
||
170 | } |
||
171 | $oldSkill = $skillRepo->find($skillIdFromGet); |
||
172 | $counter = 0; |
||
173 | foreach ($subSkillList as $subSkillId) { |
||
174 | $children = $skillManager->getChildren($subSkillId); |
||
175 | |||
176 | if (isset($subSkillList[$counter - 1])) { |
||
177 | $oldSkill = $skillRepo->find($subSkillList[$counter]); |
||
178 | } |
||
179 | $skillsOptions = []; |
||
180 | if ($oldSkill) { |
||
181 | $skillsOptions = [$oldSkill->getId() => ' -- '.$oldSkill->getName()]; |
||
182 | } |
||
183 | |||
184 | if ($counter < count($subSkillList) - 1) { |
||
185 | $disableList[] = 'sub_skill_id_'.($counter + 1); |
||
186 | } |
||
187 | |||
188 | foreach ($children as $child) { |
||
189 | $skillsOptions[$child['id']] = $child['data']['name']; |
||
190 | } |
||
191 | |||
192 | $levelName = get_lang('SubSkill'); |
||
193 | if (!empty($skillLevels)) { |
||
194 | if (isset($skillLevels['levels'][$counter + 2])) { |
||
195 | $levelName = get_lang($skillLevels['levels'][$counter + 2]); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | $form->addSelect( |
||
200 | 'sub_skill_id_'.($counter + 1), |
||
201 | $levelName, |
||
202 | $skillsOptions, |
||
203 | [ |
||
204 | 'id' => 'sub_skill_id_'.($counter + 1), |
||
205 | 'class' => 'sub_skill', |
||
206 | ] |
||
207 | ); |
||
208 | |||
209 | if (isset($subSkillList[$counter + 1])) { |
||
210 | $nextSkill = $skillRepo->find($subSkillList[$counter + 1]); |
||
211 | if ($nextSkill) { |
||
212 | $formDefaultValues['sub_skill_id_'.($counter + 1)] = $nextSkill->getId(); |
||
213 | } |
||
214 | } |
||
215 | $newSubSkillList[] = $subSkillId; |
||
216 | $counter++; |
||
217 | } |
||
218 | $subSkillList = $newSubSkillList; |
||
219 | } |
||
220 | |||
221 | $subSkillListToString = implode(',', $subSkillList); |
||
222 | |||
223 | $currentUrl = api_get_self().'?user='.$userId.'¤t='.$currentLevel.'&sub_skill_list='.$subSkillListToString; |
||
224 | |||
225 | $form->addHidden('sub_skill_list', $subSkillListToString); |
||
226 | $form->addHidden('user', $user->getId()); |
||
227 | $form->addHidden('id', $skillId); |
||
228 | $form->addRule('skill', get_lang('ThisFieldIsRequired'), 'required'); |
||
229 | |||
230 | $showLevels = api_get_configuration_value('hide_skill_levels') === false; |
||
231 | |||
232 | if ($showLevels) { |
||
233 | $form->addSelect('acquired_level', get_lang('AcquiredLevel'), $acquiredLevel); |
||
234 | //$form->addRule('acquired_level', get_lang('ThisFieldIsRequired'), 'required'); |
||
235 | } |
||
236 | |||
237 | $form->addTextarea('argumentation', get_lang('Argumentation'), ['rows' => 6]); |
||
238 | $form->addRule('argumentation', get_lang('ThisFieldIsRequired'), 'required'); |
||
239 | $form->addRule( |
||
240 | 'argumentation', |
||
241 | sprintf(get_lang('ThisTextShouldBeAtLeastXCharsLong'), 10), |
||
242 | 'mintext', |
||
243 | 10 |
||
244 | ); |
||
245 | $form->applyFilter('argumentation', 'trim'); |
||
246 | $form->addButtonSave(get_lang('Save')); |
||
247 | $form->setDefaults($formDefaultValues); |
||
248 | |||
249 | if ($form->validate()) { |
||
250 | $values = $form->exportValues(); |
||
251 | $skillToProcess = $values['id']; |
||
252 | if (!empty($subSkillList)) { |
||
253 | $counter = 1; |
||
254 | foreach ($subSkillList as $subSkill) { |
||
255 | if (isset($values["sub_skill_id_$counter"])) { |
||
256 | $skillToProcess = $values["sub_skill_id_$counter"]; |
||
257 | } |
||
258 | $counter++; |
||
259 | } |
||
260 | } |
||
261 | $skill = $skillRepo->find($skillToProcess); |
||
262 | |||
263 | if (!$skill) { |
||
264 | Display::addFlash( |
||
265 | Display::return_message(get_lang('SkillNotFound'), 'error') |
||
266 | ); |
||
267 | |||
268 | header('Location: '.api_get_self().'?'.$currentUrl); |
||
269 | exit; |
||
270 | } |
||
271 | |||
272 | if ($user->hasSkill($skill)) { |
||
273 | Display::addFlash( |
||
274 | Display::return_message( |
||
275 | sprintf( |
||
276 | get_lang('TheUserXHasAlreadyAchievedTheSkillY'), |
||
277 | UserManager::formatUserFullName($user), |
||
278 | $skill->getName() |
||
279 | ), |
||
280 | 'warning' |
||
281 | ) |
||
282 | ); |
||
283 | |||
284 | header('Location: '.$currentUrl); |
||
285 | exit; |
||
286 | } |
||
287 | |||
288 | $skillUser = $skillManager->addSkillToUserBadge( |
||
289 | $user, |
||
290 | $skill, |
||
291 | $values['acquired_level'], |
||
292 | $values['argumentation'], |
||
293 | api_get_user_id() |
||
294 | ); |
||
295 | |||
296 | // Send email depending of children_auto_threshold |
||
297 | $skillRelSkill = new SkillRelSkill(); |
||
298 | $skillModel = new \Skill(); |
||
299 | $parents = $skillModel->getDirectParents($skillToProcess); |
||
300 | |||
301 | $extraFieldValue = new ExtraFieldValue('skill'); |
||
302 | foreach ($parents as $parentInfo) { |
||
303 | $parentId = $parentInfo['skill_id']; |
||
304 | $parentData = $skillModel->get($parentId); |
||
305 | |||
306 | $data = $extraFieldValue->get_values_by_handler_and_field_variable($parentId, 'children_auto_threshold'); |
||
307 | if (!empty($data) && !empty($data['value'])) { |
||
308 | // Search X children |
||
309 | $requiredSkills = $data['value']; |
||
310 | $children = $skillRelSkill->getChildren($parentId); |
||
311 | $counter = 0; |
||
312 | foreach ($children as $child) { |
||
313 | if ($skillModel->userHasSkill($userId, $child['id'])) { |
||
314 | $counter++; |
||
315 | } |
||
316 | } |
||
317 | |||
318 | if ($counter >= $requiredSkills) { |
||
319 | $bossList = UserManager::getStudentBossList($userId); |
||
320 | if (!empty($bossList)) { |
||
321 | Display::addFlash(Display::return_message(get_lang('MessageSent'))); |
||
322 | $url = api_get_path(WEB_CODE_PATH).'badge/assign.php?user='.$userId.'&id='.$parentId; |
||
323 | $link = Display::url($url, $url); |
||
324 | $subject = get_lang('StudentHadEnoughSkills'); |
||
325 | $message = sprintf( |
||
326 | get_lang('StudentXHadEnoughSkillsToGetSkillXToAssignClickHereX'), |
||
327 | UserManager::formatUserFullName($user), |
||
328 | $parentData['name'], |
||
329 | $link |
||
330 | ); |
||
331 | foreach ($bossList as $boss) { |
||
332 | MessageManager::send_message_simple( |
||
333 | $boss['boss_id'], |
||
334 | $subject, |
||
335 | $message |
||
336 | ); |
||
337 | } |
||
338 | } |
||
339 | break; |
||
340 | } |
||
341 | } |
||
342 | } |
||
343 | |||
344 | Display::addFlash( |
||
345 | Display::return_message( |
||
346 | sprintf( |
||
347 | get_lang('SkillXAssignedToUserY'), |
||
348 | $skill->getName(), |
||
349 | UserManager::formatUserFullName($user) |
||
350 | ), |
||
351 | 'success' |
||
352 | ) |
||
353 | ); |
||
354 | |||
355 | Display::addFlash( |
||
356 | Display::return_message( |
||
357 | sprintf( |
||
358 | get_lang('ToAssignNewSkillToUserClickLinkX'), |
||
359 | api_get_self().'?'.http_build_query(['user' => $user->getId()]) |
||
360 | ), |
||
361 | 'info', |
||
362 | false |
||
363 | ) |
||
364 | ); |
||
365 | |||
366 | header('Location: '.api_get_path(WEB_PATH)."badge/{$skillUser->getId()}"); |
||
367 | exit; |
||
368 | } |
||
369 | |||
370 | $form->setDefaults(['user_name' => UserManager::formatUserFullName($user, true)]); |
||
371 | $form->freeze(['user_name']); |
||
372 | |||
373 | if (api_is_drh()) { |
||
374 | $interbreadcrumb[] = [ |
||
375 | 'url' => api_get_path(WEB_CODE_PATH).'mySpace/index.php', |
||
376 | "name" => get_lang('MySpace'), |
||
377 | ]; |
||
378 | if ($user->getStatus() == COURSEMANAGER) { |
||
379 | $interbreadcrumb[] = [ |
||
380 | "url" => api_get_path(WEB_CODE_PATH).'mySpace/teachers.php', |
||
381 | 'name' => get_lang('Teachers'), |
||
382 | ]; |
||
383 | } else { |
||
384 | $interbreadcrumb[] = [ |
||
385 | "url" => api_get_path(WEB_CODE_PATH).'mySpace/student.php', |
||
386 | 'name' => get_lang('MyStudents'), |
||
387 | ]; |
||
388 | } |
||
389 | $interbreadcrumb[] = [ |
||
390 | 'url' => api_get_path(WEB_CODE_PATH).'mySpace/myStudents.php?student='.$userId, |
||
391 | 'name' => UserManager::formatUserFullName($user), |
||
392 | ]; |
||
393 | } else { |
||
394 | $interbreadcrumb[] = [ |
||
395 | 'url' => api_get_path(WEB_CODE_PATH).'admin/index.php', |
||
396 | 'name' => get_lang('PlatformAdmin'), |
||
397 | ]; |
||
398 | $interbreadcrumb[] = [ |
||
399 | 'url' => api_get_path(WEB_CODE_PATH).'admin/user_list.php', |
||
400 | 'name' => get_lang('UserList'), |
||
401 | ]; |
||
402 | $interbreadcrumb[] = [ |
||
403 | 'url' => api_get_path(WEB_CODE_PATH).'admin/user_information.php?user_id='.$userId, |
||
404 | 'name' => UserManager::formatUserFullName($user), |
||
405 | ]; |
||
406 | } |
||
407 | |||
408 | $url = api_get_path(WEB_CODE_PATH).'badge/assign.php?user='.$userId; |
||
409 | |||
410 | $disableSelect = ''; |
||
411 | if ($disableList) { |
||
412 | foreach ($disableList as $name) { |
||
413 | //$disableSelect .= "$('#".$name."').prop('disabled', true);"; |
||
414 | //$disableSelect .= "$('#".$name."').selectpicker('refresh');"; |
||
415 | } |
||
416 | } |
||
417 | |||
418 | $htmlHeadXtra[] = '<script> |
||
419 | $(function() { |
||
420 | $("#skill").on("change", function() { |
||
421 | $(location).attr("href", "'.$url.'&id="+$(this).val()); |
||
422 | }); |
||
423 | $(".sub_skill").on("change", function() { |
||
424 | $(location).attr("href", "'.$url.'&id='.$skillIdFromGet.'¤t_value="+$(this).val()+"¤t="+$(this).attr("id")+"&sub_skill_list='.$subSkillListToString.',"+$(this).val()); |
||
425 | }); |
||
426 | '.$disableSelect.' |
||
427 | }); |
||
428 | </script>'; |
||
429 | |||
430 | $template = new Template(get_lang('AddSkill')); |
||
431 | $template->assign('content', $form->returnForm()); |
||
432 | $template->display_one_col_template(); |
||
433 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: