course_home.ajax.php 20 KB

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