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