skill.ajax.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls.
  5. */
  6. require_once __DIR__.'/../global.inc.php';
  7. $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
  8. api_block_anonymous_users();
  9. Skill::isAllowed(api_get_user_id());
  10. $skill = new Skill();
  11. $gradebook = new Gradebook();
  12. $skillGradeBook = new SkillRelGradebook();
  13. $userId = api_get_user_id();
  14. switch ($action) {
  15. case 'add':
  16. if (api_is_platform_admin() || api_is_drh()) {
  17. if (isset($_REQUEST['id']) && !empty($_REQUEST['id'])) {
  18. $skillId = $skill->edit($_REQUEST);
  19. } else {
  20. $skillId = $skill->add($_REQUEST);
  21. }
  22. }
  23. echo $skillId;
  24. break;
  25. case 'delete_skill':
  26. if (api_is_platform_admin() || api_is_drh()) {
  27. echo $skill->delete($_REQUEST['skill_id']);
  28. }
  29. break;
  30. case 'find_skills':
  31. $returnSkills = [[
  32. 'items' => [],
  33. ]];
  34. if (isset($_REQUEST['q']) && !empty($_REQUEST['q'])) {
  35. $skills = $skill->find('all', ['where' => ['name LIKE %?% ' => $_REQUEST['q']]]);
  36. foreach ($skills as $skill) {
  37. $returnSkills['items'][] = [
  38. 'id' => $skill['id'],
  39. 'text' => $skill['name'],
  40. ];
  41. }
  42. }
  43. header('Content-Type: application/json');
  44. echo json_encode($returnSkills);
  45. break;
  46. case 'get_gradebooks':
  47. $gradebooks = $gradebook_list = $gradebook->get_all();
  48. $gradebook_list = [];
  49. //Only course gradebook with certificate
  50. if (!empty($gradebooks)) {
  51. foreach ($gradebooks as $gradebook) {
  52. if ($gradebook['parent_id'] == 0 &&
  53. !empty($gradebook['certif_min_score']) &&
  54. !empty($gradebook['document_id'])
  55. ) {
  56. $gradebook_list[] = $gradebook;
  57. }
  58. }
  59. }
  60. echo json_encode($gradebook_list);
  61. break;
  62. case 'find_gradebooks':
  63. $return = [];
  64. if (isset($_REQUEST['tag']) && !empty($_REQUEST['tag'])) {
  65. $gradebooks = $gradebook->find('all', ['where' => ['name LIKE %?% ' => $_REQUEST['tag']]]);
  66. foreach ($gradebooks as $item) {
  67. $item['key'] = $item['name'];
  68. $item['value'] = $item['id'];
  69. $return[] = $item;
  70. }
  71. }
  72. echo json_encode($return);
  73. break;
  74. case 'get_course_info_popup':
  75. $courseInfo = api_get_course_info($_REQUEST['code']);
  76. $courses = CourseManager::processHotCourseItem(
  77. [
  78. ['c_id' => $courseInfo['real_id']],
  79. ]
  80. );
  81. Display::display_no_header();
  82. Display::$global_template->assign('hot_courses', $courses);
  83. $template = Display::$global_template->get_template('layout/hot_course_item_popup.tpl');
  84. echo Display::$global_template->fetch($template);
  85. break;
  86. case 'gradebook_exists':
  87. $data = $gradebook->get($_REQUEST['gradebook_id']);
  88. if (!empty($data)) {
  89. echo 1;
  90. } else {
  91. echo 0;
  92. }
  93. break;
  94. case 'get_skills_by_profile':
  95. $skillRelProfile = new SkillRelProfile();
  96. $profile_id = isset($_REQUEST['profile_id']) ? $_REQUEST['profile_id'] : null;
  97. $skills = $skillRelProfile->getSkillsByProfile($profile_id);
  98. echo json_encode($skills);
  99. break;
  100. case 'get_saved_profiles':
  101. $skillProfile = new SkillProfile();
  102. $profiles = $skillProfile->get_all();
  103. Display::display_no_header();
  104. Display::$global_template->assign('profiles', $profiles);
  105. $template = Display::$global_template->get_template('skill/profile_item.tpl');
  106. echo Display::$global_template->fetch($template);
  107. break;
  108. case 'get_skills':
  109. $loadUserData = isset($_REQUEST['load_user_data']) ? $_REQUEST['load_user_data'] : null;
  110. $id = intval($_REQUEST['id']);
  111. $skills = $skill->get_all($loadUserData, false, $id);
  112. echo json_encode($skills);
  113. break;
  114. case 'get_skill_info':
  115. $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  116. $skillInfo = $skill->getSkillInfo($id);
  117. echo json_encode($skillInfo);
  118. break;
  119. case 'get_skill_course_info':
  120. $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  121. $skillInfo = $skill->getSkillInfo($id);
  122. $courses = $skill->getCoursesBySkill($id);
  123. $sessions = $skill->getSessionsBySkill($id);
  124. $html = '';
  125. if (!empty($courses) || !empty($sessions)) {
  126. Display::display_no_header();
  127. Display::$global_template->assign('skill', $skillInfo);
  128. Display::$global_template->assign('courses', $courses);
  129. Display::$global_template->assign('sessions', $sessions);
  130. $template = Display::$global_template->get_template('skill/skill_info.tpl');
  131. $html = Display::$global_template->fetch($template);
  132. }
  133. echo $html;
  134. break;
  135. case 'get_skills_tree_json':
  136. header('Content-Type: application/json');
  137. $userId = isset($_REQUEST['load_user']) && $_REQUEST['load_user'] == 1 ? api_get_user_id() : 0;
  138. $skill_id = isset($_REQUEST['skill_id']) ? intval($_REQUEST['skill_id']) : 0;
  139. $depth = isset($_REQUEST['main_depth']) ? intval($_REQUEST['main_depth']) : 2;
  140. $all = $skill->getSkillsTreeToJson($userId, $skill_id, false, $depth);
  141. echo $all;
  142. break;
  143. case 'get_user_skill':
  144. $skillId = isset($_REQUEST['profile_id']) ? intval($_REQUEST['profile_id']) : 0;
  145. $skill = $skill->userHasSkill($userId, $skillId);
  146. if ($skill) {
  147. echo 1;
  148. } else {
  149. echo 0;
  150. }
  151. break;
  152. case 'get_all_user_skills':
  153. $skills = $skill->getUserSkills($userId, true);
  154. echo json_encode($skills);
  155. break;
  156. case 'get_user_skills':
  157. $skills = $skill->getUserSkills($userId, true);
  158. Display::display_no_header();
  159. Display::$global_template->assign('skills', $skills);
  160. $template = Display::$global_template->get_template('skill/user_skills.tpl');
  161. echo Display::$global_template->fetch($template);
  162. break;
  163. case 'get_gradebook_info':
  164. $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  165. $info = $gradebook->get($id);
  166. echo json_encode($info);
  167. break;
  168. case 'load_children':
  169. $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  170. $load_user_data = isset($_REQUEST['load_user_data']) ? $_REQUEST['load_user_data'] : null;
  171. $skills = $skill->getChildren($id, $load_user_data);
  172. $return = [];
  173. foreach ($skills as $skill) {
  174. if (isset($skill['data']) && !empty($skill['data'])) {
  175. $return[$skill['data']['id']] = [
  176. 'id' => $skill['data']['id'],
  177. 'name' => $skill['data']['name'],
  178. 'passed' => $skill['data']['passed'],
  179. ];
  180. }
  181. }
  182. $success = true;
  183. if (empty($return)) {
  184. $success = false;
  185. }
  186. $result = [
  187. 'success' => $success,
  188. 'data' => $return,
  189. ];
  190. echo json_encode($result);
  191. break;
  192. case 'load_direct_parents':
  193. $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  194. $skills = $skill->getDirectParents($id);
  195. $return = [];
  196. foreach ($skills as $skill) {
  197. $return[$skill['data']['id']] = [
  198. 'id' => $skill['data']['id'],
  199. 'parent_id' => $skill['data']['parent_id'],
  200. 'name' => $skill['data']['name'],
  201. ];
  202. }
  203. echo json_encode($return);
  204. break;
  205. case 'profile_matches':
  206. $skill_rel_user = new SkillRelUser();
  207. $skills = !empty($_REQUEST['skill_id']) ? $_REQUEST['skill_id'] : [];
  208. $total_skills_to_search = $skills;
  209. $users = $skill_rel_user->getUserBySkills($skills);
  210. $user_list = [];
  211. $count_skills = count($skills);
  212. $ordered_user_list = null;
  213. if (!empty($users)) {
  214. foreach ($users as $user) {
  215. $user_info = api_get_user_info($user['user_id']);
  216. $user_list[$user['user_id']]['user'] = $user_info;
  217. $my_user_skills = $skill_rel_user->getUserSkills($user['user_id']);
  218. $user_skill_list = [];
  219. foreach ($my_user_skills as $skill_item) {
  220. $user_skill_list[] = $skill_item['skill_id'];
  221. }
  222. $user_skills = [];
  223. $found_counts = 0;
  224. foreach ($skills as $skill_id) {
  225. $found = false;
  226. if (in_array($skill_id, $user_skill_list)) {
  227. $found = true;
  228. $found_counts++;
  229. $user_skills[$skill_id] = ['skill_id' => $skill_id, 'found' => $found];
  230. }
  231. }
  232. foreach ($my_user_skills as $my_skill) {
  233. if (!isset($user_skills[$my_skill['skill_id']])) {
  234. $user_skills[$my_skill['skill_id']] = [
  235. 'skill_id' => $my_skill['skill_id'],
  236. 'found' => false,
  237. ];
  238. }
  239. $total_skills_to_search[$my_skill['skill_id']] = $my_skill['skill_id'];
  240. }
  241. $user_list[$user['user_id']]['skills'] = $user_skills;
  242. $user_list[$user['user_id']]['total_found_skills'] = $found_counts;
  243. }
  244. foreach ($user_list as $user_id => $user_data) {
  245. $ordered_user_list[$user_data['total_found_skills']][] = $user_data;
  246. }
  247. if (!empty($ordered_user_list)) {
  248. krsort($ordered_user_list);
  249. }
  250. }
  251. Display::display_no_header();
  252. Display::$global_template->assign('order_user_list', $ordered_user_list);
  253. Display::$global_template->assign('total_search_skills', $count_skills);
  254. $skill_list = [];
  255. if (!empty($total_skills_to_search)) {
  256. $total_skills_to_search = $skill->getSkillsInfo($total_skills_to_search);
  257. foreach ($total_skills_to_search as $skill_info) {
  258. $skill_list[$skill_info['id']] = $skill_info;
  259. }
  260. }
  261. Display::$global_template->assign('skill_list', $skill_list);
  262. $template = Display::$global_template->get_template('skill/profile.tpl');
  263. echo Display::$global_template->fetch($template);
  264. break;
  265. case 'delete_gradebook_from_skill':
  266. case 'remove_skill':
  267. if (api_is_platform_admin() || api_is_drh()) {
  268. if (!empty($_REQUEST['skill_id']) && !empty($_REQUEST['gradebook_id'])) {
  269. $skill_item = $skillGradeBook->getSkillInfo(
  270. $_REQUEST['skill_id'],
  271. $_REQUEST['gradebook_id']
  272. );
  273. if (!empty($skill_item)) {
  274. $skillGradeBook->delete($skill_item['id']);
  275. echo 1;
  276. } else {
  277. echo 0;
  278. }
  279. } else {
  280. echo 0;
  281. }
  282. }
  283. break;
  284. case 'get_profile':
  285. $skillRelProfile = new SkillRelProfile();
  286. $profileId = isset($_REQUEST['profile_id']) ? intval($_REQUEST['profile_id']) : null;
  287. $profile = $skillRelProfile->getProfileInfo($profileId);
  288. echo json_encode($profile);
  289. break;
  290. case 'save_profile':
  291. if (api_is_platform_admin() || api_is_drh()) {
  292. $skill_profile = new SkillProfile();
  293. $params = $_REQUEST;
  294. $params['skills'] = isset($params['skill_id']) ? $params['skill_id'] : null;
  295. $profileId = isset($_REQUEST['profile']) ? intval($_REQUEST['profile']) : null;
  296. if ($profileId > 0) {
  297. $skill_profile->updateProfileInfo(
  298. $profileId,
  299. $params['name'],
  300. $params['description']
  301. );
  302. $skill_data = 1;
  303. } else {
  304. $skill_data = $skill_profile->save($params);
  305. }
  306. if (!empty($skill_data)) {
  307. echo 1;
  308. } else {
  309. echo 0;
  310. }
  311. }
  312. break;
  313. case 'delete_profile':
  314. if (api_is_platform_admin() || api_is_drh()) {
  315. $profileId = $_REQUEST['profile'];
  316. $skillProfile = new SkillProfile();
  317. $isDeleted = $skillProfile->delete($profileId);
  318. echo json_encode([
  319. 'status' => $isDeleted,
  320. ]);
  321. }
  322. break;
  323. case 'skill_exists':
  324. $skill_data = $skill->get($_REQUEST['skill_id']);
  325. if (!empty($skill_data)) {
  326. echo 1;
  327. } else {
  328. echo 0;
  329. }
  330. break;
  331. case 'search_skills':
  332. $returnSkills = [];
  333. if (isset($_REQUEST['q']) && !empty($_REQUEST['q'])) {
  334. $skills = $skill->find(
  335. 'all',
  336. [
  337. 'where' => ['name LIKE %?% ' => $_REQUEST['q']],
  338. ]
  339. );
  340. foreach ($skills as $skill) {
  341. $returnSkills[] = [
  342. 'id' => $skill['id'],
  343. 'text' => $skill['name'],
  344. ];
  345. }
  346. }
  347. echo json_encode(['items' => $returnSkills]);
  348. break;
  349. case 'search_skills_in_course':
  350. $courseId = isset($_REQUEST['course_id']) ? (int) $_REQUEST['course_id'] : 0;
  351. $sessionId = isset($_REQUEST['session_id']) ? (int) $_REQUEST['session_id'] : null;
  352. if (empty($courseId)) {
  353. exit;
  354. }
  355. $em = Database::getManager();
  356. $skills = $em->getRepository('ChamiloSkillBundle:SkillRelCourse')->findBy(
  357. ['course' => $courseId, 'session' => $sessionId]
  358. );
  359. $returnSkills = [];
  360. /** @var \Chamilo\SkillBundle\Entity\SkillRelCourse $skill */
  361. foreach ($skills as $skill) {
  362. $returnSkills[] = [
  363. 'id' => $skill->getSkill()->getId(),
  364. 'text' => $skill->getSkill()->getName(),
  365. ];
  366. }
  367. echo json_encode([
  368. 'items' => $returnSkills,
  369. ]);
  370. break;
  371. case 'update_skill_rel_user':
  372. $allowSkillInTools = api_get_configuration_value('allow_skill_rel_items');
  373. if (empty($allowSkillInTools)) {
  374. exit;
  375. }
  376. if (!api_is_allowed_to_edit()) {
  377. exit;
  378. }
  379. $creatorId = api_get_user_id();
  380. $typeId = isset($_REQUEST['type_id']) ? (int) $_REQUEST['type_id'] : 0;
  381. $itemId = isset($_REQUEST['item_id']) ? (int) $_REQUEST['item_id'] : 0;
  382. $skillId = isset($_REQUEST['skill_id']) ? (int) $_REQUEST['skill_id'] : 0;
  383. $userId = isset($_REQUEST['user_id']) ? (int) $_REQUEST['user_id'] : 0;
  384. $courseId = isset($_REQUEST['course_id']) ? (int) $_REQUEST['course_id'] : 0;
  385. $sessionId = isset($_REQUEST['session_id']) ? (int) $_REQUEST['session_id'] : 0;
  386. $resultId = isset($_REQUEST['result_id']) ? (int) $_REQUEST['result_id'] : 0;
  387. if (!empty($typeId) && !empty($itemId) && !empty($skillId) && !empty($userId) && !empty($courseId)) {
  388. $em = Database::getManager();
  389. $user = api_get_user_entity($userId);
  390. $skill = $em->getRepository('ChamiloCoreBundle:Skill')->find($skillId);
  391. if (empty($user) || empty($skill)) {
  392. exit;
  393. }
  394. $course = api_get_course_entity($courseId);
  395. if (empty($course)) {
  396. exit;
  397. }
  398. $session = $em->getRepository('ChamiloCoreBundle:Session')->find($sessionId);
  399. /** @var \Chamilo\SkillBundle\Entity\SkillRelItem $skillRelItem */
  400. $skillRelItem = $em->getRepository('ChamiloSkillBundle:SkillRelItem')->findOneBy(
  401. ['itemId' => $itemId, 'itemType' => $typeId, 'skill' => $skillId]
  402. );
  403. if ($skillRelItem) {
  404. $criteria = [
  405. 'user' => $userId,
  406. 'skillRelItem' => $skillRelItem,
  407. ];
  408. $skillRelItemRelUser = $em->getRepository('ChamiloSkillBundle:SkillRelItemRelUser')->findOneBy($criteria);
  409. if ($skillRelItemRelUser) {
  410. $em->remove($skillRelItemRelUser);
  411. $em->flush();
  412. $skillRelItemRelUser = null;
  413. } else {
  414. $skillRelItemRelUser = new Chamilo\SkillBundle\Entity\SkillRelItemRelUser();
  415. $skillRelItemRelUser
  416. ->setUser($user)
  417. ->setSkillRelItem($skillRelItem)
  418. ->setResultId($resultId)
  419. ->setCreatedBy($creatorId)
  420. ->setUpdatedBy($creatorId)
  421. ;
  422. $em->persist($skillRelItemRelUser);
  423. $em->flush();
  424. }
  425. }
  426. echo Skill::getUserSkillStatusLabel($skillRelItem, $skillRelItemRelUser, false);
  427. }
  428. break;
  429. case 'assign_user_to_skill':
  430. $allowSkillInTools = api_get_configuration_value('allow_skill_rel_items');
  431. if (empty($allowSkillInTools)) {
  432. exit;
  433. }
  434. if (!api_is_allowed_to_edit()) {
  435. exit;
  436. }
  437. $skillId = isset($_REQUEST['skill_id']) ? (int) $_REQUEST['skill_id'] : 0;
  438. $userId = isset($_REQUEST['user_id']) ? (int) $_REQUEST['user_id'] : 0;
  439. $courseId = isset($_REQUEST['course_id']) ? (int) $_REQUEST['course_id'] : 0;
  440. $sessionId = isset($_REQUEST['session_id']) ? (int) $_REQUEST['session_id'] : null;
  441. if (empty($skillId) || empty($userId)) {
  442. exit;
  443. }
  444. $em = Database::getManager();
  445. $skillRepo = $em->getRepository('ChamiloCoreBundle:Skill');
  446. $skill = $skillRepo->find($skillId);
  447. $user = api_get_user_entity($userId);
  448. if (empty($skill) || empty($user)) {
  449. exit;
  450. }
  451. $skillUserRepo = $em->getRepository('ChamiloCoreBundle:SkillRelUser');
  452. $criteria = [
  453. 'user' => $user,
  454. 'skill' => $skill,
  455. ];
  456. $skillRelUsers = $skillUserRepo->findBy($criteria);
  457. if (empty($skillRelUsers)) {
  458. $skillUser = new \Chamilo\CoreBundle\Entity\SkillRelUser();
  459. $skillUser->setUser($user);
  460. $skillUser->setSkill($skill);
  461. /*if ($showLevels) {
  462. $level = $skillLevelRepo->find(intval($values['acquired_level']));
  463. $skillUser->setAcquiredLevel($level);
  464. }*/
  465. $course = api_get_course_entity($courseId);
  466. $skillUser->setCourse($course);
  467. if (!empty($sessionId)) {
  468. $session = $em->getRepository('ChamiloCoreBundle:Session')->find($sessionId);
  469. $skillUser->setSession($session);
  470. }
  471. $skillUser->setArgumentation('');
  472. $skillUser->setArgumentationAuthorId(api_get_user_id());
  473. $skillUser->setAcquiredSkillAt(new DateTime());
  474. $skillUser->setAssignedBy(0);
  475. $em->persist($skillUser);
  476. $em->flush();
  477. $result = 'success';
  478. } else {
  479. foreach ($skillRelUsers as $skillRelUser) {
  480. $em->remove($skillRelUser);
  481. }
  482. $em->flush();
  483. $result = 'danger';
  484. }
  485. echo $result;
  486. break;
  487. default:
  488. echo '';
  489. }
  490. exit;