course_home.ajax.php 21 KB

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