skill.ajax.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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::isAllow();
  10. $skill = new Skill();
  11. $gradebook = new Gradebook();
  12. $skill_gradebook = 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. $skill_id = $skill->edit($_REQUEST);
  19. } else {
  20. $skill_id = $skill->add($_REQUEST);
  21. }
  22. }
  23. echo $skill_id;
  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. $skills = $skill->find('all', array('where' => array('name LIKE %?% '=>$_REQUEST['q'])));
  32. $return_skills = array([
  33. 'items' => []
  34. ]);
  35. foreach ($skills as $skill) {
  36. $return_skills['items'][] = [
  37. 'id' => $skill['id'],
  38. 'text' => $skill['name']
  39. ];
  40. }
  41. header('Content-Type: application/json');
  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. $template = Display::$global_template->get_template('layout/hot_course_item_popup.tpl');
  82. echo Display::$global_template->fetch($template);
  83. break;
  84. case 'gradebook_exists':
  85. $data = $gradebook->get($_REQUEST['gradebook_id']);
  86. if (!empty($data)) {
  87. echo 1;
  88. } else {
  89. echo 0;
  90. }
  91. break;
  92. case 'get_skills_by_profile':
  93. $skill_rel_profile = new SkillRelProfile();
  94. $profile_id = isset($_REQUEST['profile_id']) ? $_REQUEST['profile_id'] : null;
  95. $skills = $skill_rel_profile->get_skills_by_profile($profile_id);
  96. echo json_encode($skills);
  97. break;
  98. case 'get_saved_profiles':
  99. $skill_profile = new SkillProfile();
  100. $profiles = $skill_profile->get_all();
  101. Display::display_no_header();
  102. Display::$global_template->assign('profiles', $profiles);
  103. $template = Display::$global_template->get_template('skill/profile_item.tpl');
  104. echo Display::$global_template->fetch($template);
  105. break;
  106. case 'get_skills':
  107. $load_user_data = isset($_REQUEST['load_user_data']) ? $_REQUEST['load_user_data'] : null;
  108. $id = intval($_REQUEST['id']);
  109. $skills = $skill->get_all($load_user_data, false, $id);
  110. echo json_encode($skills);
  111. break;
  112. case 'get_skill_info':
  113. $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  114. $skill_info = $skill->get_skill_info($id);
  115. echo json_encode($skill_info);
  116. break;
  117. case 'get_skill_course_info':
  118. $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  119. $skill_info = $skill->get_skill_info($id);
  120. $courses = $skill->get_courses_by_skill($id);
  121. $sessions = $skill->getSessionsBySkill($id);
  122. $html = '';
  123. if (!empty($courses) || !empty($sessions)) {
  124. Display::display_no_header();
  125. Display::$global_template->assign('skill', $skill_info);
  126. Display::$global_template->assign('courses', $courses);
  127. Display::$global_template->assign('sessions', $sessions);
  128. $template = Display::$global_template->get_template('skill/skill_info.tpl');
  129. $html = Display::$global_template->fetch($template);
  130. }
  131. echo $html;
  132. break;
  133. case 'get_skills_tree_json':
  134. $userId = isset($_REQUEST['load_user']) && $_REQUEST['load_user'] == 1 ? api_get_user_id() : 0;
  135. $skill_id = isset($_REQUEST['skill_id']) ? intval($_REQUEST['skill_id']) : 0;
  136. $depth = isset($_REQUEST['main_depth']) ? intval($_REQUEST['main_depth']) : 2;
  137. $all = $skill->get_skills_tree_json($userId, $skill_id, false, $depth);
  138. echo $all;
  139. break;
  140. case 'get_user_skill':
  141. $skillId = isset($_REQUEST['profile_id']) ? intval($_REQUEST['profile_id']) : 0;
  142. $skill = $skill->userHasSkill($userId, $skillId);
  143. if ($skill) {
  144. echo 1;
  145. } else {
  146. echo 0;
  147. }
  148. break;
  149. case 'get_all_user_skills':
  150. $skills = $skill->getUserSkills($userId, true);
  151. echo json_encode($skills);
  152. break;
  153. case 'get_user_skills':
  154. $skills = $skill->getUserSkills($userId, true);
  155. Display::display_no_header();
  156. Display::$global_template->assign('skills', $skills);
  157. $template = Display::$global_template->get_template('skill/user_skills.tpl');
  158. echo Display::$global_template->fetch($template);
  159. break;
  160. case 'get_gradebook_info':
  161. $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  162. $info = $gradebook->get($id);
  163. echo json_encode($info);
  164. break;
  165. case 'load_children':
  166. $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  167. $load_user_data = isset($_REQUEST['load_user_data']) ? $_REQUEST['load_user_data'] : null;
  168. $skills = $skill->get_children($id, $load_user_data);
  169. $return = array();
  170. foreach ($skills as $skill) {
  171. if (isset($skill['data']) && !empty($skill['data'])) {
  172. $return[$skill['data']['id']] = array(
  173. 'id' => $skill['data']['id'],
  174. 'name' => $skill['data']['name'],
  175. 'passed'=> $skill['data']['passed']
  176. );
  177. }
  178. }
  179. $success = true;
  180. if (empty($return)) {
  181. $success = false;
  182. }
  183. $result = array(
  184. 'success' => $success,
  185. 'data' => $return
  186. );
  187. echo json_encode($result);
  188. break;
  189. case 'load_direct_parents':
  190. $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  191. $skills = $skill->get_direct_parents($id);
  192. $return = array();
  193. foreach ($skills as $skill) {
  194. $return [$skill['data']['id']] = array(
  195. 'id' => $skill['data']['id'],
  196. 'parent_id' => $skill['data']['parent_id'],
  197. 'name' => $skill['data']['name']
  198. );
  199. }
  200. echo json_encode($return);
  201. break;
  202. case 'profile_matches':
  203. $skill_rel_user = new SkillRelUser();
  204. $skills = (!empty($_REQUEST['skill_id']) ? $_REQUEST['skill_id'] : array());
  205. $total_skills_to_search = $skills;
  206. $users = $skill_rel_user->get_user_by_skills($skills);
  207. $user_list = array();
  208. $count_skills = count($skills);
  209. $ordered_user_list = null;
  210. if (!empty($users)) {
  211. foreach ($users as $user) {
  212. $user_info = api_get_user_info($user['user_id']);
  213. $user_list[$user['user_id']]['user'] = $user_info;
  214. $my_user_skills = $skill_rel_user->getUserSkills($user['user_id']);
  215. $user_skill_list = array();
  216. foreach ($my_user_skills as $skill_item) {
  217. $user_skill_list[] = $skill_item['skill_id'];
  218. }
  219. $user_skills = array();
  220. $found_counts = 0;
  221. foreach ($skills as $skill_id) {
  222. $found = false;
  223. if (in_array($skill_id, $user_skill_list)) {
  224. $found = true;
  225. $found_counts++;
  226. $user_skills[$skill_id] = array('skill_id' => $skill_id, 'found' => $found);
  227. }
  228. }
  229. foreach ($my_user_skills as $my_skill) {
  230. if (!isset($user_skills[$my_skill['skill_id']])) {
  231. $user_skills[$my_skill['skill_id']] = array(
  232. 'skill_id' => $my_skill['skill_id'],
  233. 'found' => false
  234. );
  235. }
  236. $total_skills_to_search[$my_skill['skill_id']] = $my_skill['skill_id'];
  237. }
  238. $user_list[$user['user_id']]['skills'] = $user_skills;
  239. $user_list[$user['user_id']]['total_found_skills'] = $found_counts;
  240. }
  241. foreach ($user_list as $user_id => $user_data) {
  242. $ordered_user_list[$user_data['total_found_skills']][] = $user_data;
  243. }
  244. if (!empty($ordered_user_list)) {
  245. krsort($ordered_user_list);
  246. }
  247. }
  248. Display::display_no_header();
  249. Display::$global_template->assign('order_user_list', $ordered_user_list);
  250. Display::$global_template->assign('total_search_skills', $count_skills);
  251. $skill_list = array();
  252. if (!empty($total_skills_to_search)) {
  253. $total_skills_to_search = $skill->get_skills_info($total_skills_to_search);
  254. foreach ($total_skills_to_search as $skill_info) {
  255. $skill_list[$skill_info['id']] = $skill_info;
  256. }
  257. }
  258. Display::$global_template->assign('skill_list', $skill_list);
  259. $template = Display::$global_template->get_template('skill/profile.tpl');
  260. echo Display::$global_template->fetch($template);
  261. break;
  262. case 'delete_gradebook_from_skill':
  263. case 'remove_skill':
  264. if (api_is_platform_admin() || api_is_drh()) {
  265. if (!empty($_REQUEST['skill_id']) && !empty($_REQUEST['gradebook_id'])) {
  266. $skill_item = $skill_gradebook->get_skill_info(
  267. $_REQUEST['skill_id'],
  268. $_REQUEST['gradebook_id']
  269. );
  270. if (!empty($skill_item)) {
  271. $skill_gradebook->delete($skill_item['id']);
  272. echo 1;
  273. } else {
  274. echo 0;
  275. }
  276. } else {
  277. echo 0;
  278. }
  279. }
  280. break;
  281. case 'get_profile':
  282. $skillRelProfile = new SkillRelProfile();
  283. $profileId = isset($_REQUEST['profile_id']) ? intval($_REQUEST['profile_id']) : null;
  284. $profile = $skillRelProfile->getProfileInfo($profileId);
  285. echo json_encode($profile);
  286. break;
  287. case 'save_profile':
  288. if (api_is_platform_admin() || api_is_drh()) {
  289. $skill_profile = new SkillProfile();
  290. $params = $_REQUEST;
  291. //$params['skills'] = isset($_SESSION['skills']) ? $_SESSION['skills'] : null;
  292. $params['skills'] = $params['skill_id'];
  293. $profileId = isset($_REQUEST['profile']) ? intval($_REQUEST['profile']) : null;
  294. if ($profileId > 0) {
  295. $skill_data = $skill_profile->updateProfileInfo(
  296. $profileId,
  297. $params['name'],
  298. $params['description']
  299. );
  300. } else {
  301. $skill_data = $skill_profile->save($params);
  302. }
  303. if (!empty($skill_data)) {
  304. echo 1;
  305. } else {
  306. echo 0;
  307. }
  308. }
  309. break;
  310. case 'delete_profile':
  311. if (api_is_platform_admin() || api_is_drh()) {
  312. $profileId = $_REQUEST['profile'];
  313. $skillProfile = new SkillProfile();
  314. $isDeleted = $skillProfile->delete($profileId);
  315. echo json_encode(array(
  316. 'status' => $isDeleted
  317. ));
  318. }
  319. break;
  320. case 'skill_exists':
  321. $skill_data = $skill->get($_REQUEST['skill_id']);
  322. if (!empty($skill_data)) {
  323. echo 1;
  324. } else {
  325. echo 0;
  326. }
  327. break;
  328. case 'search_skills':
  329. $skills = $skill->find(
  330. 'all',
  331. [
  332. 'where' => ['name LIKE %?% ' => $_REQUEST['q']]
  333. ]
  334. );
  335. $returnSkills = [];
  336. foreach ($skills as $skill) {
  337. $returnSkills[] = [
  338. 'id' => $skill['id'],
  339. 'text' => $skill['name']
  340. ];
  341. }
  342. echo json_encode([
  343. 'items' => $returnSkills
  344. ]);
  345. break;
  346. default:
  347. echo '';
  348. }
  349. exit;