skill.ajax.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. /**
  5. * Responses to AJAX calls
  6. */
  7. //require_once '../global.inc.php';
  8. $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
  9. if (api_get_setting('skill.allow_skills_tool') != 'true') {
  10. exit;
  11. }
  12. Container::$legacyTemplate = 'layout_one_col_no_content.html.twig';
  13. api_block_anonymous_users();
  14. $skill = new Skill();
  15. $gradebook = new Gradebook();
  16. $skill_gradebook = new SkillRelGradebook();
  17. $userId = api_get_user_id();
  18. switch ($action) {
  19. case 'add':
  20. if (api_is_platform_admin() || api_is_drh()) {
  21. if (isset($_REQUEST['id']) && !empty($_REQUEST['id'])) {
  22. $skill_id = $skill->edit($_REQUEST);
  23. } else {
  24. $skill_id = $skill->add($_REQUEST);
  25. }
  26. }
  27. echo $skill_id;
  28. break;
  29. case 'delete_skill':
  30. if (api_is_platform_admin() || api_is_drh()) {
  31. echo $skill->delete($_REQUEST['skill_id']);
  32. }
  33. break;
  34. case 'find_skills':
  35. $skills = $skill->find('all', array('where' => array('name LIKE %?% '=>$_REQUEST['tag'])));
  36. $return_skills = array();
  37. foreach ($skills as $skill) {
  38. $skill['key'] = $skill['name'];
  39. $skill['value'] = $skill['id'];
  40. $return_skills[] = $skill;
  41. }
  42. echo json_encode($return_skills);
  43. break;
  44. case 'get_gradebooks':
  45. $gradebooks = $gradebook_list = $gradebook->get_all();
  46. $gradebook_list = array();
  47. //Only course gradebook with certificate
  48. if (!empty($gradebooks)) {
  49. foreach ($gradebooks as $gradebook) {
  50. if ($gradebook['parent_id'] == 0 && !empty($gradebook['certif_min_score']) && !empty($gradebook['document_id'])) {
  51. $gradebook_list[] = $gradebook;
  52. //$gradebook['name'] = $gradebook['name'];
  53. //$gradebook_list[] = $gradebook;
  54. } else {
  55. // $gradebook['name'] = $gradebook_list[$gradebook['parent_id']]['name'].' > '.$gradebook['name'];
  56. //$gradebook_list[] = $gradebook;
  57. }
  58. }
  59. }
  60. echo json_encode($gradebook_list);
  61. break;
  62. case 'find_gradebooks':
  63. $gradebooks = $gradebook->find('all', array('where' => array('name LIKE %?% ' => $_REQUEST['tag'])));
  64. $return = array();
  65. foreach ($gradebooks as $item) {
  66. $item['key'] = $item['name'];
  67. $item['value'] = $item['id'];
  68. $return[] = $item;
  69. }
  70. echo json_encode($return);
  71. break;
  72. case 'get_course_info_popup':
  73. $course_info = api_get_course_info($_REQUEST['code']);
  74. $courses = CourseManager::process_hot_course_item(
  75. [
  76. ['c_id' => $course_info['real_id']]
  77. ]
  78. );
  79. Display::display_no_header();
  80. Display::$global_template->assign('hot_courses', $courses);
  81. echo Display::$global_template->fetch('default/layout/hot_course_item_popup.tpl');
  82. break;
  83. case 'gradebook_exists':
  84. $data = $gradebook->get($_REQUEST['gradebook_id']);
  85. if (!empty($data)) {
  86. echo 1;
  87. } else {
  88. echo 0;
  89. }
  90. break;
  91. case 'get_skills_by_profile':
  92. $skill_rel_profile = new SkillRelProfile();
  93. $profile_id = isset($_REQUEST['profile_id']) ? $_REQUEST['profile_id'] : null;
  94. $skills = $skill_rel_profile->get_skills_by_profile($profile_id);
  95. echo json_encode($skills);
  96. break;
  97. case 'get_saved_profiles':
  98. $skill_profile = new SkillProfile();
  99. $profiles = $skill_profile->get_all();
  100. Display::display_no_header();
  101. Display::$global_template->assign('profiles', $profiles);
  102. echo Display::$global_template->fetch('default/skill/profile_item.tpl');
  103. break;
  104. case 'get_skills':
  105. $load_user_data = isset($_REQUEST['load_user_data']) ? $_REQUEST['load_user_data'] : null;
  106. $id = intval($_REQUEST['id']);
  107. $skills = $skill->get_all($load_user_data, false, $id);
  108. echo json_encode($skills);
  109. break;
  110. case 'get_skill_info':
  111. $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  112. $skill_info = $skill->get_skill_info($id);
  113. echo json_encode($skill_info);
  114. break;
  115. case 'get_skill_course_info':
  116. $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  117. $skill_info = $skill->get_skill_info($id);
  118. $courses = $skill->get_courses_by_skill($id);
  119. $sessions = $skill->getSessionsBySkill($id);
  120. $html = '';
  121. if (!empty($courses) || !empty($sessions)) {
  122. Display::display_no_header();
  123. Display::$global_template->assign('skill', $skill_info);
  124. Display::$global_template->assign('courses', $courses);
  125. Display::$global_template->assign('sessions', $sessions);
  126. $html = Display::$global_template->fetch('default/skill/skill_info.tpl');
  127. }
  128. echo $html;
  129. break;
  130. case 'get_skills_tree_json':
  131. $userId = isset($_REQUEST['load_user']) && $_REQUEST['load_user'] == 1 ? api_get_user_id() : 0;
  132. $skill_id = isset($_REQUEST['skill_id']) ? intval($_REQUEST['skill_id']) : 0;
  133. $depth = isset($_REQUEST['main_depth']) ? intval($_REQUEST['main_depth']) : 2;
  134. $all = $skill->get_skills_tree_json($userId, $skill_id, false, $depth);
  135. echo $all;
  136. break;
  137. case 'get_user_skill':
  138. $skillId = isset($_REQUEST['profile_id']) ? intval($_REQUEST['profile_id']) : 0;
  139. $skill = $skill->user_has_skill($userId, $skillId);
  140. if ($skill) {
  141. echo 1;
  142. } else {
  143. echo 0;
  144. }
  145. break;
  146. case 'get_all_user_skills':
  147. $skills = $skill->get_user_skills($userId, true);
  148. echo json_encode($skills);
  149. break;
  150. case 'get_user_skills':
  151. $skills = $skill->get_user_skills($userId, true);
  152. Display::display_no_header();
  153. Display::$global_template->assign('skills', $skills);
  154. echo Display::$global_template->fetch('default/skill/user_skills.tpl');
  155. break;
  156. case 'get_gradebook_info':
  157. $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  158. $info = $gradebook->get($id);
  159. echo json_encode($info);
  160. break;
  161. case 'load_children':
  162. $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  163. $load_user_data = isset($_REQUEST['load_user_data']) ? $_REQUEST['load_user_data'] : null;
  164. $skills = $skill->get_children($id, $load_user_data);
  165. $return = array();
  166. foreach ($skills as $skill) {
  167. if (isset($skill['data']) && !empty($skill['data'])) {
  168. $return[$skill['data']['id']] = array(
  169. 'id' => $skill['data']['id'],
  170. 'name' => $skill['data']['name'],
  171. 'passed'=> $skill['data']['passed']
  172. );
  173. }
  174. }
  175. $success = true;
  176. if (empty($return)) {
  177. $success = false;
  178. }
  179. $result = array (
  180. 'success' => $success,
  181. 'data' => $return
  182. );
  183. echo json_encode($result);
  184. break;
  185. case 'load_direct_parents':
  186. $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  187. $skills = $skill->get_direct_parents($id);
  188. $return = array();
  189. foreach ($skills as $skill) {
  190. $return [$skill['data']['id']] = array (
  191. 'id' => $skill['data']['id'],
  192. 'parent_id' => $skill['data']['parent_id'],
  193. 'name' => $skill['data']['name']
  194. );
  195. }
  196. echo json_encode($return);
  197. break;
  198. case 'profile_matches':
  199. $skill_rel_user = new SkillRelUser();
  200. $skills = (!empty($_REQUEST['skill_id'])?$_REQUEST['skill_id']:array());
  201. $total_skills_to_search = $skills;
  202. $users = $skill_rel_user->get_user_by_skills($skills);
  203. $user_list = array();
  204. $count_skills = count($skills);
  205. $ordered_user_list = null;
  206. if (!empty($users)) {
  207. foreach ($users as $user) {
  208. $user_info = api_get_user_info($user['user_id']);
  209. $user_list[$user['user_id']]['user'] = $user_info;
  210. $my_user_skills = $skill_rel_user->get_user_skills($user['user_id']);
  211. $user_skill_list = array();
  212. foreach ($my_user_skills as $skill_item) {
  213. $user_skill_list[] = $skill_item['skill_id'];
  214. }
  215. $user_skills = array();
  216. $found_counts = 0;
  217. foreach ($skills as $skill_id) {
  218. $found = false;
  219. if (in_array($skill_id, $user_skill_list)) {
  220. $found = true;
  221. $found_counts++;
  222. $user_skills[$skill_id] = array('skill_id' => $skill_id, 'found' => $found);
  223. }
  224. }
  225. foreach ($my_user_skills as $my_skill) {
  226. if (!isset($user_skills[$my_skill['skill_id']])) {
  227. $user_skills[$my_skill['skill_id']] = array(
  228. 'skill_id' => $my_skill['skill_id'],
  229. 'found' => false
  230. );
  231. }
  232. $total_skills_to_search[$my_skill['skill_id']] = $my_skill['skill_id'];
  233. }
  234. $user_list[$user['user_id']]['skills'] = $user_skills;
  235. $user_list[$user['user_id']]['total_found_skills'] = $found_counts;
  236. }
  237. foreach ($user_list as $user_id => $user_data) {
  238. $ordered_user_list[$user_data['total_found_skills']][] = $user_data;
  239. }
  240. if (!empty($ordered_user_list)) {
  241. krsort($ordered_user_list);
  242. }
  243. }
  244. Display::display_no_header();
  245. Display::$global_template->assign('order_user_list', $ordered_user_list);
  246. Display::$global_template->assign('total_search_skills', $count_skills);
  247. $skill_list = array();
  248. if (!empty($total_skills_to_search)) {
  249. $total_skills_to_search = $skill->get_skills_info($total_skills_to_search);
  250. foreach ($total_skills_to_search as $skill_info) {
  251. $skill_list[$skill_info['id']] = $skill_info;
  252. }
  253. }
  254. Display::$global_template->assign('skill_list', $skill_list);
  255. echo Display::$global_template->fetch('default/skill/profile.tpl');
  256. break;
  257. case 'delete_gradebook_from_skill':
  258. case 'remove_skill':
  259. if (api_is_platform_admin() || api_is_drh()) {
  260. if (!empty($_REQUEST['skill_id']) && !empty($_REQUEST['gradebook_id'])) {
  261. $skill_item = $skill_gradebook->get_skill_info(
  262. $_REQUEST['skill_id'],
  263. $_REQUEST['gradebook_id']
  264. );
  265. if (!empty($skill_item)) {
  266. $skill_gradebook->delete($skill_item['id']);
  267. echo 1;
  268. } else {
  269. echo 0;
  270. }
  271. } else {
  272. echo 0;
  273. }
  274. }
  275. break;
  276. case 'get_profile':
  277. $skillRelProfile = new SkillRelProfile();
  278. $profileId = isset($_REQUEST['profile_id']) ? intval($_REQUEST['profile_id']) : null;
  279. $profile = $skillRelProfile->getProfileInfo($profileId);
  280. echo json_encode($profile);
  281. break;
  282. case 'save_profile':
  283. if (api_is_platform_admin() || api_is_drh()) {
  284. $skill_profile = new SkillProfile();
  285. $params = $_REQUEST;
  286. $params['skills'] = $params['skill_id'];
  287. $profileId = isset($_REQUEST['profile']) ? intval($_REQUEST['profile']) : null;
  288. if ($profileId > 0) {
  289. $skill_data = $skill_profile->updateProfileInfo(
  290. $profileId,
  291. $params['name'],
  292. $params['description']
  293. );
  294. } else {
  295. $skill_data = $skill_profile->save($params);
  296. }
  297. if (!empty($skill_data)) {
  298. echo 1;
  299. } else {
  300. echo 0;
  301. }
  302. }
  303. break;
  304. case 'delete_profile':
  305. if (api_is_platform_admin() || api_is_drh()) {
  306. $profileId = $_REQUEST['profile'];
  307. $skillProfile = new SkillProfile();
  308. $isDeleted = $skillProfile->delete($profileId);
  309. echo json_encode(array(
  310. 'status' => $isDeleted
  311. ));
  312. }
  313. break;
  314. case 'skill_exists':
  315. $skill_data = $skill->get($_REQUEST['skill_id']);
  316. if (!empty($skill_data)) {
  317. echo 1;
  318. } else {
  319. echo 0;
  320. }
  321. break;
  322. case 'search_skills':
  323. $skills = $skill->find(
  324. 'all',
  325. [
  326. 'where' => ['name LIKE %?% ' => $_REQUEST['q']]
  327. ]
  328. );
  329. $returnSkills = [];
  330. foreach ($skills as $skill) {
  331. $returnSkills[] = [
  332. 'id' => $skill['id'],
  333. 'text' => $skill['name']
  334. ];
  335. }
  336. echo json_encode([
  337. 'items' => $returnSkills
  338. ]);
  339. break;
  340. default:
  341. echo '';
  342. }
  343. exit;