course_home.ajax.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. // @todo refactor this script, create a class that manage the jqgrid requests
  4. /**
  5. * Responses to AJAX calls
  6. */
  7. $action = $_GET['a'];
  8. switch ($action) {
  9. case 'set_visibility':
  10. $course_id = api_get_course_int_id();
  11. if (api_is_allowed_to_edit(null, true)) {
  12. $tool_table = Database::get_course_table(TABLE_TOOL_LIST);
  13. $tool_info = api_get_tool_information($_GET['id']);
  14. $tool_visibility = $tool_info['visibility'];
  15. $tool_image = $tool_info['image'];
  16. if (api_get_setting('course.homepage_view') != 'activity_big') {
  17. $tool_image = Display::return_icon($tool_image, null, null, null, null, true);
  18. $na_image = str_replace('.gif', '_na.gif', $tool_image);
  19. } else {
  20. // Display::return_icon() also checks in the app/Resources/public/css/themes/{theme}/icons folder
  21. $tool_image = (substr($tool_image, 0, strpos($tool_image, '.'))).'.png';
  22. $tool_image = Display::return_icon(
  23. $tool_image,
  24. get_lang(ucfirst($tool_info['name'])),
  25. null,
  26. ICON_SIZE_BIG,
  27. null,
  28. true
  29. );
  30. $na_image = str_replace('.png', '_na.png', $tool_image);
  31. }
  32. if (isset($tool_info['custom_icon']) && !empty($tool_info['custom_icon'])) {
  33. $tool_image = CourseHome::getCustomWebIconPath().$tool_info['custom_icon'];
  34. $na_image = CourseHome::getCustomWebIconPath().CourseHome::getDisableIcon($tool_info['custom_icon']);
  35. }
  36. $requested_image = ($tool_visibility == 0 ) ? $tool_image : $na_image;
  37. $requested_class = ($tool_visibility == 0 ) ? '' : 'text-muted';
  38. $requested_message = ($tool_visibility == 0 ) ? 'is_active' : 'is_inactive';
  39. $requested_view = ($tool_visibility == 0 ) ? 'visible.png' : 'invisible.png';
  40. $requested_visible = ($tool_visibility == 0 ) ? 1 : 0;
  41. $requested_view = ($tool_visibility == 0 ) ? 'visible.png' : 'invisible.png';
  42. $requested_visible = ($tool_visibility == 0 ) ? 1 : 0;
  43. // HIDE AND REACTIVATE TOOL
  44. if ($_GET["id"] == strval(intval($_GET["id"]))) {
  45. $sql = "UPDATE $tool_table SET
  46. visibility = $requested_visible
  47. WHERE c_id = $course_id AND id='" . intval($_GET['id']) . "'";
  48. Database::query($sql);
  49. }
  50. $response_data = array(
  51. 'image' => $requested_image,
  52. 'tclass' => $requested_class,
  53. 'message' => $requested_message,
  54. 'view' => $requested_view
  55. );
  56. echo json_encode($response_data);
  57. }
  58. break;
  59. case 'show_course_information':
  60. // Get the name of the database course.
  61. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  62. $course_info = api_get_course_info($_GET['code']);
  63. if (
  64. api_get_setting('course_catalog_hide_private') === 'true' &&
  65. $course_info['visibility'] == COURSE_VISIBILITY_REGISTERED
  66. ) {
  67. echo get_lang('PrivateAccess');
  68. break;
  69. }
  70. $sql = "SELECT * FROM $tbl_course_description
  71. WHERE c_id = ".$course_info['real_id']." AND session_id = 0
  72. ORDER BY id";
  73. $result = Database::query($sql);
  74. if (Database::num_rows($result) > 0 ) {
  75. while ($description = Database::fetch_object($result)) {
  76. $descriptions[$description->id] = $description;
  77. }
  78. // Function that displays the details of the course description in html.
  79. echo CourseManager::get_details_course_description_html(
  80. $descriptions,
  81. api_get_system_encoding(),
  82. false
  83. );
  84. } else {
  85. echo get_lang('NoDescription');
  86. }
  87. break;
  88. case 'session_courses_lp_default':
  89. /**
  90. * @todo this functions need to belong to a class or a special
  91. * wrapper to process the AJAX petitions from the jqgrid
  92. */
  93. $now = time();
  94. $page = intval($_REQUEST['page']); //page
  95. $limit = intval($_REQUEST['rows']); // quantity of rows
  96. //index to filter
  97. $sidx = isset($_REQUEST['sidx']) && !empty($_REQUEST['sidx']) ? $_REQUEST['sidx'] : 'id';
  98. $sord = $_REQUEST['sord']; //asc or desc
  99. if (!in_array($sord, array('asc','desc'))) {
  100. $sord = 'desc';
  101. }
  102. $session_id = intval($_REQUEST['session_id']);
  103. $course_id = intval($_REQUEST['course_id']);
  104. //Filter users that does not belong to the session
  105. if (!api_is_platform_admin()) {
  106. $new_session_list = UserManager::get_personal_session_course_list(api_get_user_id());
  107. $my_session_list = array();
  108. foreach($new_session_list as $item) {
  109. if (!empty($item['id_session']))
  110. $my_session_list[] = $item['id_session'];
  111. }
  112. if (!in_array($session_id, $my_session_list)) {
  113. break;
  114. }
  115. }
  116. $start = $limit*$page - $limit;
  117. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  118. $count = 0;
  119. $temp = array();
  120. foreach ($course_list as $item) {
  121. $list = new LearnpathList(api_get_user_id(), $item['code'], $session_id);
  122. $flat_list = $list->get_flat_list();
  123. $lps[$item['code']] = $flat_list;
  124. $course_url = api_get_path(
  125. WEB_COURSE_PATH
  126. ).$item['directory'].'/?id_session='.$session_id;
  127. $item['title'] = Display::url(
  128. $item['title'],
  129. $course_url,
  130. array('target' => SESSION_LINK_TARGET)
  131. );
  132. foreach ($flat_list as $lp_id => $lp_item) {
  133. $temp[$count]['id']= $lp_id;
  134. $lp = new learnpath($item['code'], $lp_id, api_get_user_id());
  135. if ($lp->progress_db == 100) {
  136. continue;
  137. }
  138. $lp_url = api_get_path(WEB_CODE_PATH) . 'lp/lp_controller.php?cidReq='.$item['code'].'&id_session='.$session_id.'&lp_id='.$lp_id.'&action=view';
  139. $last_date = Tracking::get_last_connection_date_on_the_course(
  140. api_get_user_id(),
  141. $item,
  142. $session_id,
  143. false
  144. );
  145. if (empty($lp_item['modified_on'])) {
  146. $lp_date = api_get_local_time($lp_item['created_on']);
  147. $image = 'new.gif';
  148. $label = get_lang('LearnpathAdded');
  149. } else {
  150. $lp_date = api_get_local_time($lp_item['modified_on']);
  151. $image = 'moderator_star.png';
  152. $label = get_lang('LearnpathUpdated');
  153. }
  154. $icons = '';
  155. if (strtotime($last_date) < strtotime($lp_date)) {
  156. $icons = Display::return_icon($image, get_lang('TitleNotification').': '.$label.' - '.$lp_date);
  157. }
  158. if (!empty($lp_item['publicated_on'])) {
  159. $date = substr($lp_item['publicated_on'], 0, 10);
  160. } else {
  161. $date = '-';
  162. }
  163. // Checking LP publicated and expired_on dates
  164. if (!empty($lp_item['publicated_on'])) {
  165. if ($now < api_strtotime($lp_item['publicated_on'], 'UTC')) {
  166. continue;
  167. }
  168. }
  169. if (!empty($lp_item['expired_on'])) {
  170. if ($now > api_strtotime($lp_item['expired_on'], 'UTC')) {
  171. continue;
  172. }
  173. }
  174. $temp[$count]['cell'] = array(
  175. $date,
  176. $item['title'],
  177. Display::url($icons.' '.$lp_item['lp_name'], $lp_url, array('target'=>SESSION_LINK_TARGET))
  178. );
  179. $temp[$count]['course'] = strip_tags($item['title']);
  180. $temp[$count]['lp'] = $lp_item['lp_name'];
  181. $temp[$count]['date'] = $lp_item['publicated_on'];
  182. $count++;
  183. }
  184. }
  185. $temp = ArrayClass::msort($temp, $sidx, $sord);
  186. $i =0;
  187. $response = new stdClass();
  188. foreach($temp as $key=>$row) {
  189. $row = $row['cell'];
  190. if (!empty($row)) {
  191. if ($key >= $start && $key < ($start + $limit)) {
  192. $response->rows[$i]['id']= $key;
  193. $response->rows[$i]['cell']=array($row[0], $row[1], $row[2]);
  194. $i++;
  195. }
  196. }
  197. }
  198. if($count > 0 && $limit > 0) {
  199. $total_pages = ceil($count/$limit);
  200. } else {
  201. $total_pages = 0;
  202. }
  203. $response->total = $total_pages;
  204. if ($page > $total_pages) {
  205. $response->page= $total_pages;
  206. } else {
  207. $response->page = $page;
  208. }
  209. $response->records = $count;
  210. echo json_encode($response);
  211. break;
  212. case 'session_courses_lp_by_week':
  213. $now = time();
  214. $page = intval($_REQUEST['page']); //page
  215. $limit = intval($_REQUEST['rows']); // quantity of rows
  216. $sidx = isset($_REQUEST['sidx']) && !empty($_REQUEST['sidx']) ? $_REQUEST['sidx'] : 'course';
  217. $sidx = str_replace(array('week desc,', ' '), '', $sidx);
  218. $sord = $_REQUEST['sord']; //asc or desc
  219. if (!in_array($sord, array('asc','desc'))) {
  220. $sord = 'desc';
  221. }
  222. $session_id = intval($_REQUEST['session_id']);
  223. $course_id = intval($_REQUEST['course_id']);
  224. //Filter users that does not belong to the session
  225. if (!api_is_platform_admin()) {
  226. $new_session_list = UserManager::get_personal_session_course_list(api_get_user_id());
  227. $my_session_list = array();
  228. foreach($new_session_list as $item) {
  229. if (!empty($item['id_session']))
  230. $my_session_list[] = $item['id_session'];
  231. }
  232. if (!in_array($session_id, $my_session_list)) {
  233. break;
  234. }
  235. }
  236. $start = $limit*$page - $limit;
  237. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  238. $count = 0;
  239. $temp = array();
  240. foreach ($course_list as $item) {
  241. if (isset($course_id) && !empty($course_id)) {
  242. if ($course_id != $item['id']) {
  243. continue;
  244. }
  245. }
  246. $list = new LearnpathList(
  247. api_get_user_id(),
  248. $item['code'],
  249. $session_id,
  250. 'lp.publicatedOn DESC'
  251. );
  252. $flat_list = $list->get_flat_list();
  253. $lps[$item['code']] = $flat_list;
  254. $item['title'] = Display::url(
  255. $item['title'],
  256. api_get_path(WEB_COURSE_PATH).$item['directory'].'/?id_session='.$session_id,
  257. array('target' => SESSION_LINK_TARGET)
  258. );
  259. foreach ($flat_list as $lp_id => $lp_item) {
  260. $temp[$count]['id']= $lp_id;
  261. $lp_url = api_get_path(WEB_CODE_PATH) . 'lp/lp_controller.php?cidReq='.$item['code'].'&id_session='.$session_id.'&lp_id='.$lp_id.'&action=view';
  262. $last_date = Tracking::get_last_connection_date_on_the_course(
  263. api_get_user_id(),
  264. $item,
  265. $session_id,
  266. false
  267. );
  268. if (empty($lp_item['modified_on'])) {
  269. $lp_date = api_get_local_time($lp_item['created_on']);
  270. $image = 'new.gif';
  271. $label = get_lang('LearnpathAdded');
  272. } else {
  273. $lp_date = api_get_local_time($lp_item['modified_on']);
  274. $image = 'moderator_star.png';
  275. $label = get_lang('LearnpathUpdated');
  276. }
  277. if (strtotime($last_date) < strtotime($lp_date)) {
  278. $icons = Display::return_icon($image, get_lang('TitleNotification').': '.$label.' - '.$lp_date);
  279. }
  280. if (!empty($lp_item['publicated_on'])) {
  281. $date = substr($lp_item['publicated_on'], 0, 10);
  282. } else {
  283. $date = '-';
  284. }
  285. // Checking LP publicated and expired_on dates
  286. if (!empty($lp_item['publicated_on'])) {
  287. $week_data = date('Y', api_strtotime($lp_item['publicated_on'], 'UTC')).' - '.get_week_from_day($lp_item['publicated_on']);
  288. if ($now < api_strtotime($lp_item['publicated_on'], 'UTC')) {
  289. continue;
  290. }
  291. } else {
  292. $week_data = '';
  293. }
  294. if (!empty($lp_item['expired_on'])) {
  295. if ($now > api_strtotime($lp_item['expired_on'], 'UTC')) {
  296. continue;
  297. }
  298. }
  299. $temp[$count]['cell'] = array(
  300. $week_data,
  301. $date,
  302. $item['title'],
  303. Display::url($icons.' '.$lp_item['lp_name'], $lp_url, array('target' => SESSION_LINK_TARGET)),
  304. );
  305. $temp[$count]['course'] = strip_tags($item['title']);
  306. $temp[$count]['lp'] = $lp_item['lp_name'];
  307. $count++;
  308. }
  309. }
  310. if (!empty($sidx)) {
  311. $temp = ArrayClass::msort($temp, $sidx, $sord);
  312. }
  313. $response = new stdClass();
  314. $i =0;
  315. foreach($temp as $key=>$row) {
  316. $row = $row['cell'];
  317. if (!empty($row)) {
  318. if ($key >= $start && $key < ($start + $limit)) {
  319. $response->rows[$i]['id']= $key;
  320. $response->rows[$i]['cell']=array($row[0], $row[1], $row[2],$row[3]);
  321. $i++;
  322. }
  323. }
  324. }
  325. if ($count > 0 && $limit > 0) {
  326. $total_pages = ceil($count/$limit);
  327. } else {
  328. $total_pages = 0;
  329. }
  330. $response->total = $total_pages;
  331. if ($page > $total_pages) {
  332. $response->page = $total_pages;
  333. } else {
  334. $response->page = $page;
  335. }
  336. $response->records = $count;
  337. echo json_encode($response);
  338. break;
  339. case 'session_courses_lp_by_course':
  340. $now = time();
  341. $page = intval($_REQUEST['page']); //page
  342. $limit = intval($_REQUEST['rows']); // quantity of rows
  343. $sidx = isset($_REQUEST['sidx']) && !empty($_REQUEST['sidx']) ? $_REQUEST['sidx'] : 'id';
  344. $sidx = str_replace(array('course asc,', ' '), '', $sidx);
  345. $sord = $_REQUEST['sord']; //asc or desc
  346. if (!in_array($sord, array('asc', 'desc'))) {
  347. $sord = 'desc';
  348. }
  349. $session_id = intval($_REQUEST['session_id']);
  350. $course_id = intval($_REQUEST['course_id']);
  351. //Filter users that does not belong to the session
  352. if (!api_is_platform_admin()) {
  353. $new_session_list = UserManager::get_personal_session_course_list(api_get_user_id());
  354. $my_session_list = array();
  355. foreach($new_session_list as $item) {
  356. if (!empty($item['id_session']))
  357. $my_session_list[] = $item['id_session'];
  358. }
  359. if (!in_array($session_id, $my_session_list)) {
  360. break;
  361. }
  362. }
  363. $start = $limit*$page - $limit;
  364. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  365. $count = 0;
  366. foreach ($course_list as $item) {
  367. if (isset($course_id) && !empty($course_id)) {
  368. if ($course_id != $item['id']) {
  369. continue;
  370. }
  371. }
  372. $list = new LearnpathList(api_get_user_id(),$item['code'],$session_id);
  373. $flat_list = $list->get_flat_list();
  374. $lps[$item['code']] = $flat_list;
  375. $item['title'] = Display::url(
  376. $item['title'],
  377. api_get_path(WEB_COURSE_PATH).$item['directory'].'/?id_session='.$session_id, array('target'=>SESSION_LINK_TARGET)
  378. );
  379. foreach($flat_list as $lp_id => $lp_item) {
  380. $temp[$count]['id']= $lp_id;
  381. $lp_url = api_get_path(WEB_CODE_PATH) . 'lp/lp_controller.php?cidReq='.$item['code'].'&id_session='.$session_id.'&lp_id='.$lp_id.'&action=view';
  382. $last_date = Tracking::get_last_connection_date_on_the_course(
  383. api_get_user_id(),
  384. $item,
  385. $session_id,
  386. false
  387. );
  388. if (empty($lp_item['modified_on'])) {
  389. $lp_date = api_get_local_time($lp_item['created_on']);
  390. $image = 'new.gif';
  391. $label = get_lang('LearnpathAdded');
  392. } else {
  393. $lp_date = api_get_local_time($lp_item['modified_on']);
  394. $image = 'moderator_star.png';
  395. $label = get_lang('LearnpathUpdated');
  396. }
  397. $icons = '';
  398. if (strtotime($last_date) < strtotime($lp_date)) {
  399. $icons = Display::return_icon($image, get_lang('TitleNotification').': '.$label.' - '.$lp_date);
  400. }
  401. if (!empty($lp_item['publicated_on'])) {
  402. $date = substr($lp_item['publicated_on'], 0, 10);
  403. } else {
  404. $date = '-';
  405. }
  406. //Checking LP publicated and expired_on dates
  407. if (!empty($lp_item['publicated_on'])) {
  408. if ($now < api_strtotime($lp_item['publicated_on'], 'UTC')) {
  409. continue;
  410. }
  411. }
  412. if (!empty($lp_item['expired_on'])) {
  413. if ($now > api_strtotime($lp_item['expired_on'], 'UTC')) {
  414. continue;
  415. }
  416. }
  417. $temp[$count]['cell'] = array(
  418. $date,
  419. $item['title'],
  420. Display::url($icons.' '.$lp_item['lp_name'], $lp_url, array('target'=>SESSION_LINK_TARGET))
  421. );
  422. $temp[$count]['course'] = strip_tags($item['title']);
  423. $temp[$count]['lp'] = $lp_item['lp_name'];
  424. $temp[$count]['date'] = $lp_item['publicated_on'];
  425. $count++;
  426. }
  427. }
  428. $temp = ArrayClass::msort($temp, $sidx, $sord);
  429. $response = new stdClass();
  430. $i =0;
  431. foreach ($temp as $key => $row) {
  432. $row = $row['cell'];
  433. if (!empty($row)) {
  434. if ($key >= $start && $key < ($start + $limit)) {
  435. $response->rows[$i]['id']= $key;
  436. $response->rows[$i]['cell']=array($row[0], $row[1], $row[2]);
  437. $i++;
  438. }
  439. }
  440. }
  441. if ($count > 0 && $limit > 0) {
  442. $total_pages = ceil($count / $limit);
  443. } else {
  444. $total_pages = 0;
  445. }
  446. $response->total = $total_pages;
  447. if ($page > $total_pages) {
  448. $response->page = $total_pages;
  449. } else {
  450. $response->page = $page;
  451. }
  452. $response->records = $count;
  453. echo json_encode($response);
  454. break;
  455. default:
  456. echo '';
  457. }
  458. exit;