skill.ajax.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. 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['tag'])));
  32. $return_skills = array();
  33. foreach ($skills as $skill) {
  34. $skill['caption'] = $skill['name'];
  35. $skill['value'] = $skill['id'];
  36. $return_skills[] = $skill;
  37. }
  38. echo json_encode($return_skills);
  39. break;
  40. case 'get_gradebooks':
  41. $gradebooks = $gradebook_list = $gradebook->get_all();
  42. $gradebook_list = array();
  43. //Only course gradebook with certificate
  44. if (!empty($gradebooks)) {
  45. foreach ($gradebooks as $gradebook) {
  46. if ($gradebook['parent_id'] == 0 && !empty($gradebook['certif_min_score']) && !empty($gradebook['document_id'])) {
  47. $gradebook_list[] = $gradebook;
  48. //$gradebook['name'] = $gradebook['name'];
  49. //$gradebook_list[] = $gradebook;
  50. } else {
  51. // $gradebook['name'] = $gradebook_list[$gradebook['parent_id']]['name'].' > '.$gradebook['name'];
  52. //$gradebook_list[] = $gradebook;
  53. }
  54. }
  55. }
  56. echo json_encode($gradebook_list);
  57. break;
  58. case 'find_gradebooks':
  59. $gradebooks = $gradebook->find('all', array('where' => array('name LIKE %?% ' => $_REQUEST['tag'])));
  60. $return = array();
  61. foreach ($gradebooks as $item) {
  62. $item['caption'] = $item['name'];
  63. $item['value'] = $item['id'];
  64. $return[] = $item;
  65. }
  66. echo json_encode($return);
  67. break;
  68. case 'get_course_info_popup':
  69. $course_info = api_get_course_info($_REQUEST['code']);
  70. $course_info['course_code'] = $course_info['code'];
  71. $courses = CourseManager::process_hot_course_item(array($course_info));
  72. Display::display_no_header();
  73. Display::$global_template->assign('hot_courses', $courses);
  74. echo Display::$global_template->fetch('default/layout/hot_course_item_popup.tpl');
  75. break;
  76. case 'gradebook_exists':
  77. $data = $gradebook->get($_REQUEST['gradebook_id']);
  78. if (!empty($data)) {
  79. echo 1;
  80. } else {
  81. echo 0;
  82. }
  83. break;
  84. case 'get_skills_by_profile':
  85. $skill_rel_profile = new SkillRelProfile();
  86. $profile_id = isset($_REQUEST['profile_id']) ? $_REQUEST['profile_id'] : null;
  87. $skills = $skill_rel_profile->get_skills_by_profile($profile_id);
  88. echo json_encode($skills);
  89. break;
  90. case 'get_saved_profiles':
  91. $skill_profile = new SkillProfile();
  92. $profiles = $skill_profile->get_all();
  93. Display::display_no_header();
  94. Display::$global_template->assign('profiles', $profiles);
  95. echo Display::$global_template->fetch('default/skill/profile_item.tpl');
  96. break;
  97. case 'get_skills':
  98. $load_user_data = isset($_REQUEST['load_user_data']) ? $_REQUEST['load_user_data'] : null;
  99. $id = intval($_REQUEST['id']);
  100. $skills = $skill->get_all($load_user_data, false, $id);
  101. echo json_encode($skills);
  102. break;
  103. case 'get_skill_info':
  104. $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  105. $skill_info = $skill->get_skill_info($id);
  106. echo json_encode($skill_info);
  107. break;
  108. case 'get_skill_course_info':
  109. $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  110. $skill_info = $skill->get_skill_info($id);
  111. $courses = $skill->get_courses_by_skill($id);
  112. $html = '';
  113. if (!empty($courses)) {
  114. $html = sprintf(get_lang('ToGetToLearnXYouWillNeedToTakeOneOfTheFollowingCourses'), '<i>'.$skill_info['name'].'</i>').'<br />';
  115. foreach ($courses as $course) {
  116. $url = '#';
  117. $attributes = array('class' => 'course_description_popup', 'rel' => $course['code']);
  118. $html .= Display::url(sprintf(get_lang('SkillXWithCourseX'), $skill_info['name'], $course['title']), $url, $attributes).'<br />';
  119. }
  120. }
  121. echo $html;
  122. break;
  123. case 'get_skills_tree_json':
  124. $user_id = isset($_REQUEST['load_user']) && $_REQUEST['load_user'] == 1 ? api_get_user_id() : 0;
  125. $skill_id = isset($_REQUEST['skill_id']) ? $_REQUEST['skill_id'] : 0;
  126. $depth = isset($_REQUEST['main_depth']) ? $_REQUEST['main_depth'] : 2;
  127. $all = $skill->get_skills_tree_json($user_id, $skill_id, false, $depth);
  128. echo $all;
  129. break;
  130. case 'get_user_skills':
  131. $skills = $skill->get_user_skills($user_id, true);
  132. Display::display_no_header();
  133. Display::$global_template->assign('skills', $skills);
  134. echo Display::$global_template->fetch('default/skill/user_skills.tpl');
  135. break;
  136. case 'get_gradebook_info':
  137. $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  138. $info = $gradebook->get($id);
  139. echo json_encode($info);
  140. break;
  141. case 'load_children':
  142. $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  143. $load_user_data = isset($_REQUEST['load_user_data']) ? $_REQUEST['load_user_data'] : null;
  144. $skills = $skill->get_children($id, $load_user_data);
  145. $return = array();
  146. foreach ($skills as $skill) {
  147. if (isset($skill['data']) && !empty($skill['data'])) {
  148. $return[$skill['data']['id']] = array(
  149. 'id' => $skill['data']['id'],
  150. 'name' => $skill['data']['name'],
  151. 'passed'=> $skill['data']['passed']);
  152. }
  153. }
  154. $success = true;
  155. if (empty($return)) {
  156. $success = false;
  157. }
  158. $result = array (
  159. 'success' => $success,
  160. 'data' => $return
  161. );
  162. echo json_encode($result);
  163. break;
  164. case 'load_direct_parents':
  165. $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  166. $skills = $skill->get_direct_parents($id);
  167. $return = array();
  168. foreach($skills as $skill) {
  169. $return [$skill['data']['id']] = array (
  170. 'id' => $skill['data']['id'],
  171. 'parent_id' => $skill['data']['parent_id'],
  172. 'name' => $skill['data']['name']
  173. );
  174. }
  175. echo json_encode($return);
  176. break;
  177. case 'profile_matches':
  178. $skill_rel_user = new SkillRelUser();
  179. $skills = $_REQUEST['skill_id'];
  180. $total_skills_to_search = $skills;
  181. $users = $skill_rel_user->get_user_by_skills($skills);
  182. $user_list = array();
  183. $count_skills = count($skills);
  184. if (!empty($users)) {
  185. foreach ($users as $user) {
  186. $user_info = api_get_user_info($user['user_id']);
  187. $user_list[$user['user_id']]['user'] = $user_info;
  188. $my_user_skills = $skill_rel_user->get_user_skills($user['user_id']);
  189. $user_skill_list = array();
  190. foreach ($my_user_skills as $skill_item) {
  191. $user_skill_list[] = $skill_item['skill_id'];
  192. }
  193. $user_skills = array();
  194. $found_counts = 0;
  195. foreach ($skills as $skill_id) {
  196. $found = false;
  197. if (in_array($skill_id, $user_skill_list)) {
  198. $found = true;
  199. $found_counts++;
  200. $user_skills[$skill_id] = array('skill_id' => $skill_id, 'found' => $found);
  201. }
  202. }
  203. foreach ($my_user_skills as $my_skill) {
  204. if (!isset($user_skills[$my_skill['skill_id']])) {
  205. $user_skills[$my_skill['skill_id']] = array('skill_id' => $my_skill['skill_id'], 'found' => false);
  206. }
  207. $total_skills_to_search[$my_skill['skill_id']] = $my_skill['skill_id'];
  208. }
  209. $user_list[$user['user_id']]['skills'] = $user_skills;
  210. $user_list[$user['user_id']]['total_found_skills'] = $found_counts;
  211. }
  212. $ordered_user_list = array();
  213. foreach ($user_list as $user_id => $user_data) {
  214. $ordered_user_list[$user_data['total_found_skills']][] = $user_data;
  215. }
  216. if (!empty($ordered_user_list)) {
  217. arsort($ordered_user_list);
  218. }
  219. }
  220. Display::display_no_header();
  221. Display::$global_template->assign('order_user_list', $ordered_user_list);
  222. Display::$global_template->assign('total_search_skills', $count_skills);
  223. $skill_list = array();
  224. if (!empty($total_skills_to_search)) {
  225. $total_skills_to_search = $skill->get_skills_info($total_skills_to_search);
  226. foreach ($total_skills_to_search as $skill_info) {
  227. $skill_list[$skill_info['id']] = $skill_info;
  228. }
  229. }
  230. Display::$global_template->assign('skill_list', $skill_list);
  231. echo Display::$global_template->fetch('default/skill/profile.tpl');
  232. break;
  233. case 'delete_gradebook_from_skill':
  234. case 'remove_skill':
  235. if (api_is_platform_admin() || api_is_drh()) {
  236. if (!empty($_REQUEST['skill_id']) && !empty($_REQUEST['gradebook_id'])) {
  237. $skill_item = $skill_gradebook->get_skill_info($_REQUEST['skill_id'], $_REQUEST['gradebook_id']);
  238. if (!empty($skill_item)) {
  239. $skill_gradebook->delete($skill_item['id']);
  240. echo 1;
  241. } else {
  242. echo 0;
  243. }
  244. } else {
  245. echo 0;
  246. }
  247. }
  248. break;
  249. case 'save_profile':
  250. if (api_is_platform_admin() || api_is_drh()) {
  251. $skill_profile = new SkillProfile();
  252. $params = $_REQUEST;
  253. //$params['skills'] = isset($_SESSION['skills']) ? $_SESSION['skills'] : null;
  254. $params['skills'] = $params['skill_id'];
  255. $skill_data = $skill_profile->save($params);
  256. if (!empty($skill_data)) {
  257. echo 1;
  258. } else {
  259. echo 0;
  260. }
  261. }
  262. break;
  263. case 'skill_exists':
  264. $skill_data = $skill->get($_REQUEST['skill_id']);
  265. if (!empty($skill_data)) {
  266. echo 1;
  267. } else {
  268. echo 0;
  269. }
  270. break;
  271. default:
  272. echo '';
  273. }
  274. exit;