skill.ajax.php 13 KB

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