course_home.ajax.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CTool;
  4. use ChamiloSession as Session;
  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 && 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($requestedVisible);
  110. $em->persist($toolItem);
  111. }
  112. }
  113. $em->flush();
  114. }
  115. }
  116. $response = [
  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. $course_info = api_get_course_info($_GET['code']);
  129. $content = get_lang('NoDescription');
  130. if (!empty($course_info)) {
  131. if (api_get_setting('course_catalog_hide_private') === 'true' &&
  132. $course_info['visibility'] == COURSE_VISIBILITY_REGISTERED
  133. ) {
  134. echo get_lang('PrivateAccess');
  135. break;
  136. }
  137. $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  138. $sql = "SELECT * FROM $table
  139. WHERE c_id = ".$course_info['real_id']." AND session_id = 0
  140. ORDER BY id";
  141. $result = Database::query($sql);
  142. if (Database::num_rows($result) > 0) {
  143. while ($description = Database::fetch_object($result)) {
  144. $descriptions[$description->id] = $description;
  145. }
  146. // Function that displays the details of the course description in html.
  147. $content = CourseManager::get_details_course_description_html(
  148. $descriptions,
  149. api_get_system_encoding(),
  150. false
  151. );
  152. }
  153. }
  154. echo $content;
  155. break;
  156. case 'session_courses_lp_default':
  157. /**
  158. * @todo this functions need to belong to a class or a special
  159. * wrapper to process the AJAX petitions from the jqgrid
  160. */
  161. require_once __DIR__.'/../global.inc.php';
  162. $now = time();
  163. $page = (int) $_REQUEST['page']; //page
  164. $limit = (int) $_REQUEST['rows']; // quantity of rows
  165. //index to filter
  166. $sidx = isset($_REQUEST['sidx']) && !empty($_REQUEST['sidx']) ? $_REQUEST['sidx'] : 'id';
  167. $sord = $_REQUEST['sord']; //asc or desc
  168. if (!in_array($sord, ['asc', 'desc'])) {
  169. $sord = 'desc';
  170. }
  171. $session_id = (int) $_REQUEST['session_id'];
  172. $course_id = (int) $_REQUEST['course_id'];
  173. //Filter users that does not belong to the session
  174. if (!api_is_platform_admin()) {
  175. $new_session_list = UserManager::get_personal_session_course_list(api_get_user_id());
  176. $my_session_list = [];
  177. foreach ($new_session_list as $item) {
  178. if (!empty($item['session_id'])) {
  179. $my_session_list[] = $item['session_id'];
  180. }
  181. }
  182. if (!in_array($session_id, $my_session_list)) {
  183. break;
  184. }
  185. }
  186. $start = $limit * $page - $limit;
  187. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  188. $count = 0;
  189. $temp = [];
  190. foreach ($course_list as $item) {
  191. $list = new LearnpathList(api_get_user_id(), $item['code'], $session_id);
  192. $flat_list = $list->get_flat_list();
  193. $lps[$item['code']] = $flat_list;
  194. $course_url = api_get_path(WEB_COURSE_PATH).$item['directory'].'/?id_session='.$session_id;
  195. $item['title'] = Display::url($item['title'], $course_url, ['target' => SESSION_LINK_TARGET]);
  196. foreach ($flat_list as $lp_id => $lp_item) {
  197. $temp[$count]['id'] = $lp_id;
  198. $lp = new learnpath($item['code'], $lp_id, api_get_user_id());
  199. if ($lp->progress_db == 100) {
  200. continue;
  201. }
  202. $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';
  203. $last_date = Tracking::get_last_connection_date_on_the_course(
  204. api_get_user_id(),
  205. $item,
  206. $session_id,
  207. false
  208. );
  209. if (empty($lp_item['modified_on'])) {
  210. $lp_date = api_get_local_time($lp_item['created_on']);
  211. $image = 'new.gif';
  212. $label = get_lang('LearnpathAdded');
  213. } else {
  214. $lp_date = api_get_local_time($lp_item['modified_on']);
  215. $image = 'moderator_star.png';
  216. $label = get_lang('LearnpathUpdated');
  217. }
  218. $icons = '';
  219. if (strtotime($last_date) < strtotime($lp_date)) {
  220. $icons = Display::return_icon($image, get_lang('TitleNotification').': '.$label.' - '.$lp_date);
  221. }
  222. if (!empty($lp_item['publicated_on'])) {
  223. $date = substr($lp_item['publicated_on'], 0, 10);
  224. } else {
  225. $date = '-';
  226. }
  227. // Checking LP publicated and expired_on dates
  228. if (!empty($lp_item['publicated_on'])) {
  229. if ($now < api_strtotime($lp_item['publicated_on'], 'UTC')) {
  230. continue;
  231. }
  232. }
  233. if (!empty($lp_item['expired_on'])) {
  234. if ($now > api_strtotime($lp_item['expired_on'], 'UTC')) {
  235. continue;
  236. }
  237. }
  238. $temp[$count]['cell'] = [
  239. $date,
  240. $item['title'],
  241. Display::url($icons.' '.$lp_item['lp_name'], $lp_url, ['target' => SESSION_LINK_TARGET]),
  242. ];
  243. $temp[$count]['course'] = strip_tags($item['title']);
  244. $temp[$count]['lp'] = $lp_item['lp_name'];
  245. $temp[$count]['date'] = $lp_item['publicated_on'];
  246. $count++;
  247. }
  248. }
  249. $temp = msort($temp, $sidx, $sord);
  250. $i = 0;
  251. $response = new stdClass();
  252. foreach ($temp as $key => $row) {
  253. $row = $row['cell'];
  254. if (!empty($row)) {
  255. if ($key >= $start && $key < ($start + $limit)) {
  256. $response->rows[$i]['id'] = $key;
  257. $response->rows[$i]['cell'] = [$row[0], $row[1], $row[2]];
  258. $i++;
  259. }
  260. }
  261. }
  262. $total_pages = 0;
  263. if ($count > 0 && $limit > 0) {
  264. $total_pages = ceil($count / $limit);
  265. }
  266. $response->total = $total_pages;
  267. if ($page > $total_pages) {
  268. $response->page = $total_pages;
  269. } else {
  270. $response->page = $page;
  271. }
  272. $response->records = $count;
  273. echo json_encode($response);
  274. break;
  275. case 'session_courses_lp_by_week':
  276. require_once __DIR__.'/../global.inc.php';
  277. $now = time();
  278. $page = (int) $_REQUEST['page']; //page
  279. $limit = (int) $_REQUEST['rows']; // quantity of rows
  280. $sidx = isset($_REQUEST['sidx']) && !empty($_REQUEST['sidx']) ? $_REQUEST['sidx'] : 'course';
  281. $sidx = str_replace(['week desc,', ' '], '', $sidx);
  282. $sord = $_REQUEST['sord']; //asc or desc
  283. if (!in_array($sord, ['asc', 'desc'])) {
  284. $sord = 'desc';
  285. }
  286. $session_id = (int) $_REQUEST['session_id'];
  287. $course_id = (int) $_REQUEST['course_id'];
  288. //Filter users that does not belong to the session
  289. if (!api_is_platform_admin()) {
  290. $new_session_list = UserManager::get_personal_session_course_list(api_get_user_id());
  291. $my_session_list = [];
  292. foreach ($new_session_list as $item) {
  293. if (!empty($item['session_id'])) {
  294. $my_session_list[] = $item['session_id'];
  295. }
  296. }
  297. if (!in_array($session_id, $my_session_list)) {
  298. break;
  299. }
  300. }
  301. $start = $limit * $page - $limit;
  302. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  303. $count = 0;
  304. $temp = [];
  305. foreach ($course_list as $item) {
  306. if (isset($course_id) && !empty($course_id)) {
  307. if ($course_id != $item['id']) {
  308. continue;
  309. }
  310. }
  311. $list = new LearnpathList(
  312. api_get_user_id(),
  313. $item['code'],
  314. $session_id,
  315. 'lp.publicatedOn DESC'
  316. );
  317. $flat_list = $list->get_flat_list();
  318. $lps[$item['code']] = $flat_list;
  319. $item['title'] = Display::url(
  320. $item['title'],
  321. api_get_path(WEB_COURSE_PATH).$item['directory'].'/?id_session='.$session_id,
  322. ['target' => SESSION_LINK_TARGET]
  323. );
  324. foreach ($flat_list as $lp_id => $lp_item) {
  325. $temp[$count]['id'] = $lp_id;
  326. $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';
  327. $last_date = Tracking::get_last_connection_date_on_the_course(
  328. api_get_user_id(),
  329. $item,
  330. $session_id,
  331. false
  332. );
  333. if (empty($lp_item['modified_on'])) {
  334. $lp_date = api_get_local_time($lp_item['created_on']);
  335. $image = 'new.gif';
  336. $label = get_lang('LearnpathAdded');
  337. } else {
  338. $lp_date = api_get_local_time($lp_item['modified_on']);
  339. $image = 'moderator_star.png';
  340. $label = get_lang('LearnpathUpdated');
  341. }
  342. if (strtotime($last_date) < strtotime($lp_date)) {
  343. $icons = Display::return_icon($image, get_lang('TitleNotification').': '.$label.' - '.$lp_date);
  344. }
  345. if (!empty($lp_item['publicated_on'])) {
  346. $date = substr($lp_item['publicated_on'], 0, 10);
  347. } else {
  348. $date = '-';
  349. }
  350. // Checking LP publicated and expired_on dates
  351. if (!empty($lp_item['publicated_on'])) {
  352. $week_data = date('Y', api_strtotime($lp_item['publicated_on'], 'UTC')).' - '.get_week_from_day($lp_item['publicated_on']);
  353. if ($now < api_strtotime($lp_item['publicated_on'], 'UTC')) {
  354. continue;
  355. }
  356. } else {
  357. $week_data = '';
  358. }
  359. if (!empty($lp_item['expired_on'])) {
  360. if ($now > api_strtotime($lp_item['expired_on'], 'UTC')) {
  361. continue;
  362. }
  363. }
  364. $temp[$count]['cell'] = [
  365. $week_data,
  366. $date,
  367. $item['title'],
  368. Display::url($icons.' '.$lp_item['lp_name'], $lp_url, ['target' => SESSION_LINK_TARGET]),
  369. ];
  370. $temp[$count]['course'] = strip_tags($item['title']);
  371. $temp[$count]['lp'] = $lp_item['lp_name'];
  372. $count++;
  373. }
  374. }
  375. if (!empty($sidx)) {
  376. $temp = msort($temp, $sidx, $sord);
  377. }
  378. $response = new stdClass();
  379. $i = 0;
  380. foreach ($temp as $key => $row) {
  381. $row = $row['cell'];
  382. if (!empty($row)) {
  383. if ($key >= $start && $key < ($start + $limit)) {
  384. $response->rows[$i]['id'] = $key;
  385. $response->rows[$i]['cell'] = [$row[0], $row[1], $row[2], $row[3]];
  386. $i++;
  387. }
  388. }
  389. }
  390. $total_pages = 0;
  391. if ($count > 0 && $limit > 0) {
  392. $total_pages = ceil($count / $limit);
  393. }
  394. $response->total = $total_pages;
  395. if ($page > $total_pages) {
  396. $response->page = $total_pages;
  397. } else {
  398. $response->page = $page;
  399. }
  400. $response->records = $count;
  401. echo json_encode($response);
  402. break;
  403. case 'session_courses_lp_by_course':
  404. require_once __DIR__.'/../global.inc.php';
  405. $now = time();
  406. $page = (int) $_REQUEST['page']; //page
  407. $limit = (int) $_REQUEST['rows']; // quantity of rows
  408. $sidx = isset($_REQUEST['sidx']) && !empty($_REQUEST['sidx']) ? $_REQUEST['sidx'] : 'id';
  409. $sidx = str_replace(['course asc,', ' '], '', $sidx);
  410. $sord = $_REQUEST['sord']; //asc or desc
  411. if (!in_array($sord, ['asc', 'desc'])) {
  412. $sord = 'desc';
  413. }
  414. $session_id = (int) $_REQUEST['session_id'];
  415. $course_id = (int) $_REQUEST['course_id'];
  416. //Filter users that does not belong to the session
  417. if (!api_is_platform_admin()) {
  418. $new_session_list = UserManager::get_personal_session_course_list(api_get_user_id());
  419. $my_session_list = [];
  420. foreach ($new_session_list as $item) {
  421. if (!empty($item['session_id'])) {
  422. $my_session_list[] = $item['session_id'];
  423. }
  424. }
  425. if (!in_array($session_id, $my_session_list)) {
  426. break;
  427. }
  428. }
  429. $start = $limit * $page - $limit;
  430. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  431. $count = 0;
  432. $temp = [];
  433. foreach ($course_list as $item) {
  434. if (isset($course_id) && !empty($course_id)) {
  435. if ($course_id != $item['id']) {
  436. continue;
  437. }
  438. }
  439. $list = new LearnpathList(
  440. api_get_user_id(),
  441. $item['code'],
  442. $session_id
  443. );
  444. $flat_list = $list->get_flat_list();
  445. $lps[$item['code']] = $flat_list;
  446. $item['title'] = Display::url(
  447. $item['title'],
  448. api_get_path(WEB_COURSE_PATH).$item['directory'].'/?id_session='.$session_id,
  449. ['target' => SESSION_LINK_TARGET]
  450. );
  451. foreach ($flat_list as $lp_id => $lp_item) {
  452. $temp[$count]['id'] = $lp_id;
  453. $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';
  454. $last_date = Tracking::get_last_connection_date_on_the_course(
  455. api_get_user_id(),
  456. $item,
  457. $session_id,
  458. false
  459. );
  460. if (empty($lp_item['modified_on'])) {
  461. $lp_date = api_get_local_time($lp_item['created_on']);
  462. $image = 'new.gif';
  463. $label = get_lang('LearnpathAdded');
  464. } else {
  465. $lp_date = api_get_local_time($lp_item['modified_on']);
  466. $image = 'moderator_star.png';
  467. $label = get_lang('LearnpathUpdated');
  468. }
  469. $icons = '';
  470. if (strtotime($last_date) < strtotime($lp_date)) {
  471. $icons = Display::return_icon($image, get_lang('TitleNotification').': '.$label.' - '.$lp_date);
  472. }
  473. if (!empty($lp_item['publicated_on'])) {
  474. $date = substr($lp_item['publicated_on'], 0, 10);
  475. } else {
  476. $date = '-';
  477. }
  478. // Checking LP publicated and expired_on dates
  479. if (!empty($lp_item['publicated_on'])) {
  480. if ($now < api_strtotime($lp_item['publicated_on'], 'UTC')) {
  481. continue;
  482. }
  483. }
  484. if (!empty($lp_item['expired_on'])) {
  485. if ($now > api_strtotime($lp_item['expired_on'], 'UTC')) {
  486. continue;
  487. }
  488. }
  489. $temp[$count]['cell'] = [
  490. $date,
  491. $item['title'],
  492. Display::url($icons.' '.$lp_item['lp_name'], $lp_url, ['target' => SESSION_LINK_TARGET]),
  493. ];
  494. $temp[$count]['course'] = strip_tags($item['title']);
  495. $temp[$count]['lp'] = $lp_item['lp_name'];
  496. $temp[$count]['date'] = $lp_item['publicated_on'];
  497. $count++;
  498. }
  499. }
  500. $temp = msort($temp, $sidx, $sord);
  501. $response = new stdClass();
  502. $i = 0;
  503. foreach ($temp as $key => $row) {
  504. $row = $row['cell'];
  505. if (!empty($row)) {
  506. if ($key >= $start && $key < ($start + $limit)) {
  507. $response->rows[$i]['id'] = $key;
  508. $response->rows[$i]['cell'] = [$row[0], $row[1], $row[2]];
  509. $i++;
  510. }
  511. }
  512. }
  513. $total_pages = 0;
  514. if ($count > 0 && $limit > 0) {
  515. $total_pages = ceil($count / $limit);
  516. }
  517. $response->total = $total_pages;
  518. $response->page = $page;
  519. if ($page > $total_pages) {
  520. $response->page = $total_pages;
  521. }
  522. $response->records = $count;
  523. echo json_encode($response);
  524. break;
  525. case 'get_notification':
  526. $courseId = isset($_REQUEST['course_id']) ? (int) $_REQUEST['course_id'] : 0;
  527. $sessionId = isset($_REQUEST['session_id']) ? (int) $_REQUEST['session_id'] : 0;
  528. $status = isset($_REQUEST['status']) ? (int) $_REQUEST['status'] : 0;
  529. if (empty($courseId)) {
  530. break;
  531. }
  532. require_once __DIR__.'/../global.inc.php';
  533. $courseInfo = api_get_course_info_by_id($courseId);
  534. $courseInfo['id_session'] = $sessionId;
  535. $courseInfo['status'] = $status;
  536. $id = 'notification_'.$courseId.'_'.$sessionId.'_'.$status;
  537. $notificationId = Session::read($id);
  538. if ($notificationId) {
  539. echo Display::show_notification($courseInfo, false);
  540. Session::erase($notificationId);
  541. }
  542. break;
  543. default:
  544. echo '';
  545. }
  546. exit;