skill.ajax.php 14 KB

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