skill.ajax.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls
  5. */
  6. require_once '../global.inc.php';
  7. require_once api_get_path(LIBRARY_PATH).'skill.lib.php';
  8. require_once api_get_path(LIBRARY_PATH).'gradebook.lib.php';
  9. $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
  10. if (api_get_setting('allow_skills_tool') != 'true') {
  11. exit;
  12. }
  13. api_block_anonymous_users();
  14. $skill = new Skill();
  15. $gradebook = new Gradebook();
  16. $skill_gradebook = new SkillRelGradebook();
  17. switch ($action) {
  18. case 'add':
  19. if (api_is_platform_admin() || api_is_drh()) {
  20. if (isset($_REQUEST['id']) && !empty($_REQUEST['id'])) {
  21. $skill_id = $skill->edit($_REQUEST);
  22. } else {
  23. $skill_id = $skill->add($_REQUEST);
  24. }
  25. }
  26. echo $skill_id;
  27. break;
  28. case 'delete_skill':
  29. if (api_is_platform_admin() || api_is_drh()) {
  30. echo $skill->delete($_REQUEST['skill_id']);
  31. }
  32. break;
  33. case 'find_skills':
  34. $skills = $skill->find('all', array('where' => array('name LIKE %?% '=>$_REQUEST['tag'])));
  35. $return_skills = array();
  36. foreach ($skills as $skill) {
  37. $skill['caption'] = $skill['name'];
  38. $skill['value'] = $skill['id'];
  39. $return_skills[] = $skill;
  40. }
  41. echo json_encode($return_skills);
  42. break;
  43. case 'get_gradebooks':
  44. $gradebooks = $gradebook_list = $gradebook->get_all();
  45. $gradebook_list = array();
  46. //Only course gradebook with certificate
  47. if (!empty($gradebooks)) {
  48. foreach ($gradebooks as $gradebook) {
  49. if ($gradebook['parent_id'] == 0 && !empty($gradebook['certif_min_score']) && !empty($gradebook['document_id'])) {
  50. $gradebook_list[] = $gradebook;
  51. //$gradebook['name'] = $gradebook['name'];
  52. //$gradebook_list[] = $gradebook;
  53. } else {
  54. // $gradebook['name'] = $gradebook_list[$gradebook['parent_id']]['name'].' > '.$gradebook['name'];
  55. //$gradebook_list[] = $gradebook;
  56. }
  57. }
  58. }
  59. echo json_encode($gradebook_list);
  60. break;
  61. case 'find_gradebooks':
  62. $gradebooks = $gradebook->find('all', array('where' => array('name LIKE %?% ' => $_REQUEST['tag'])));
  63. $return = array();
  64. foreach ($gradebooks as $item) {
  65. $item['caption'] = $item['name'];
  66. $item['value'] = $item['id'];
  67. $return[] = $item;
  68. }
  69. echo json_encode($return);
  70. break;
  71. case 'get_course_info_popup':
  72. $course_info = api_get_course_info($_REQUEST['code']);
  73. $course_info['course_code'] = $course_info['code'];
  74. $courses = CourseManager::process_hot_course_item(array($course_info));
  75. Display::display_no_header();
  76. Display::$global_template->assign('hot_courses', $courses);
  77. echo Display::$global_template->fetch('default/layout/hot_course_item_popup.tpl');
  78. break;
  79. case 'gradebook_exists':
  80. $data = $gradebook->get($_REQUEST['gradebook_id']);
  81. if (!empty($data)) {
  82. echo 1;
  83. } else {
  84. echo 0;
  85. }
  86. break;
  87. case 'get_skills_by_profile':
  88. $skill_rel_profile = new SkillRelProfile();
  89. $profile_id = isset($_REQUEST['profile_id']) ? $_REQUEST['profile_id'] : null;
  90. $skills = $skill_rel_profile->get_skills_by_profile($profile_id);
  91. echo json_encode($skills);
  92. break;
  93. case 'get_saved_profiles':
  94. $skill_profile = new SkillProfile();
  95. $profiles = $skill_profile->get_all();
  96. Display::display_no_header();
  97. Display::$global_template->assign('profiles', $profiles);
  98. echo Display::$global_template->fetch('default/skill/profile_item.tpl');
  99. break;
  100. case 'get_skills':
  101. $load_user_data = isset($_REQUEST['load_user_data']) ? $_REQUEST['load_user_data'] : null;
  102. $id = intval($_REQUEST['id']);
  103. $skills = $skill->get_all($load_user_data, false, $id);
  104. echo json_encode($skills);
  105. break;
  106. case 'get_skill_info':
  107. $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  108. $skill_info = $skill->get_skill_info($id);
  109. echo json_encode($skill_info);
  110. break;
  111. case 'get_skill_course_info':
  112. $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  113. $skill_info = $skill->get_skill_info($id);
  114. $courses = $skill->get_courses_by_skill($id);
  115. $html = '';
  116. if (!empty($courses)) {
  117. $html = sprintf(
  118. get_lang('ToGetToLearnXYouWillNeedToTakeOneOfTheFollowingCourses'),
  119. '<i>'.$skill_info['name'].'</i>'
  120. ).'<br />';
  121. foreach ($courses as $course) {
  122. $url = '#';
  123. $attributes = array('class' => 'course_description_popup', 'rel' => $course['code']);
  124. $html .=
  125. Display::url(
  126. sprintf(
  127. get_lang('SkillXWithCourseX'),
  128. $skill_info['name'],
  129. $course['title']
  130. ),
  131. $url,
  132. $attributes
  133. ).'<br />';
  134. }
  135. }
  136. echo $html;
  137. break;
  138. case 'get_skills_tree_json':
  139. $user_id = isset($_REQUEST['load_user']) && $_REQUEST['load_user'] == 1 ? api_get_user_id() : 0;
  140. $skill_id = isset($_REQUEST['skill_id']) ? intval($_REQUEST['skill_id']) : 0;
  141. $depth = isset($_REQUEST['main_depth']) ? intval($_REQUEST['main_depth']) : 2;
  142. $all = $skill->get_skills_tree_json($user_id, $skill_id, false, $depth);
  143. echo $all;
  144. break;
  145. case 'get_user_skill':
  146. $userId = api_get_user_id();
  147. $skillId = isset($_REQUEST['profile_id']) ? intval($_REQUEST['profile_id']) : 0;
  148. $skill = $skill->user_has_skill($userId, $skillId);
  149. if ($skill) {
  150. echo 1;
  151. } else {
  152. echo 0;
  153. }
  154. break;
  155. case 'get_user_skills':
  156. $skills = $skill->get_user_skills($user_id, true);
  157. Display::display_no_header();
  158. Display::$global_template->assign('skills', $skills);
  159. echo Display::$global_template->fetch('default/skill/user_skills.tpl');
  160. break;
  161. case 'get_gradebook_info':
  162. $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  163. $info = $gradebook->get($id);
  164. echo json_encode($info);
  165. break;
  166. case 'load_children':
  167. $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  168. $load_user_data = isset($_REQUEST['load_user_data']) ? $_REQUEST['load_user_data'] : null;
  169. $skills = $skill->get_children($id, $load_user_data);
  170. $return = array();
  171. foreach ($skills as $skill) {
  172. if (isset($skill['data']) && !empty($skill['data'])) {
  173. $return[$skill['data']['id']] = array(
  174. 'id' => $skill['data']['id'],
  175. 'name' => $skill['data']['name'],
  176. 'passed'=> $skill['data']['passed']
  177. );
  178. }
  179. }
  180. $success = true;
  181. if (empty($return)) {
  182. $success = false;
  183. }
  184. $result = array (
  185. 'success' => $success,
  186. 'data' => $return
  187. );
  188. echo json_encode($result);
  189. break;
  190. case 'load_direct_parents':
  191. $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  192. $skills = $skill->get_direct_parents($id);
  193. $return = array();
  194. foreach ($skills as $skill) {
  195. $return [$skill['data']['id']] = array (
  196. 'id' => $skill['data']['id'],
  197. 'parent_id' => $skill['data']['parent_id'],
  198. 'name' => $skill['data']['name']
  199. );
  200. }
  201. echo json_encode($return);
  202. break;
  203. case 'profile_matches':
  204. $skill_rel_user = new SkillRelUser();
  205. $skills = $_REQUEST['skill_id'];
  206. $total_skills_to_search = $skills;
  207. $users = $skill_rel_user->get_user_by_skills($skills);
  208. $user_list = array();
  209. $count_skills = count($skills);
  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->get_user_skills($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. $ordered_user_list = array();
  242. foreach ($user_list as $user_id => $user_data) {
  243. $ordered_user_list[$user_data['total_found_skills']][] = $user_data;
  244. }
  245. if (!empty($ordered_user_list)) {
  246. krsort($ordered_user_list);
  247. }
  248. }
  249. Display::display_no_header();
  250. Display::$global_template->assign('order_user_list', $ordered_user_list);
  251. Display::$global_template->assign('total_search_skills', $count_skills);
  252. $skill_list = array();
  253. if (!empty($total_skills_to_search)) {
  254. $total_skills_to_search = $skill->get_skills_info($total_skills_to_search);
  255. foreach ($total_skills_to_search as $skill_info) {
  256. $skill_list[$skill_info['id']] = $skill_info;
  257. }
  258. }
  259. Display::$global_template->assign('skill_list', $skill_list);
  260. echo Display::$global_template->fetch('default/skill/profile.tpl');
  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. default:
  329. echo '';
  330. }
  331. exit;