course_home.ajax.php 23 KB

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