course_home.ajax.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. $courseInfo = api_get_course_info($item['code']);
  192. $list = new LearnpathList(api_get_user_id(), $courseInfo, $session_id);
  193. $flat_list = $list->get_flat_list();
  194. $lps[$item['code']] = $flat_list;
  195. $course_url = api_get_path(WEB_COURSE_PATH).$item['directory'].'/?id_session='.$session_id;
  196. $item['title'] = Display::url($item['title'], $course_url, ['target' => SESSION_LINK_TARGET]);
  197. foreach ($flat_list as $lp_id => $lp_item) {
  198. $temp[$count]['id'] = $lp_id;
  199. $lp = new learnpath($item['code'], $lp_id, api_get_user_id());
  200. if ($lp->progress_db == 100) {
  201. continue;
  202. }
  203. $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';
  204. $last_date = Tracking::get_last_connection_date_on_the_course(
  205. api_get_user_id(),
  206. $item,
  207. $session_id,
  208. false
  209. );
  210. if (empty($lp_item['modified_on'])) {
  211. $lp_date = api_get_local_time($lp_item['created_on']);
  212. $image = 'new.gif';
  213. $label = get_lang('LearnpathAdded');
  214. } else {
  215. $lp_date = api_get_local_time($lp_item['modified_on']);
  216. $image = 'moderator_star.png';
  217. $label = get_lang('LearnpathUpdated');
  218. }
  219. $icons = '';
  220. if (strtotime($last_date) < strtotime($lp_date)) {
  221. $icons = Display::return_icon($image, get_lang('TitleNotification').': '.$label.' - '.$lp_date);
  222. }
  223. if (!empty($lp_item['publicated_on'])) {
  224. $date = substr($lp_item['publicated_on'], 0, 10);
  225. } else {
  226. $date = '-';
  227. }
  228. // Checking LP publicated and expired_on dates
  229. if (!empty($lp_item['publicated_on'])) {
  230. if ($now < api_strtotime($lp_item['publicated_on'], 'UTC')) {
  231. continue;
  232. }
  233. }
  234. if (!empty($lp_item['expired_on'])) {
  235. if ($now > api_strtotime($lp_item['expired_on'], 'UTC')) {
  236. continue;
  237. }
  238. }
  239. $temp[$count]['cell'] = [
  240. $date,
  241. $item['title'],
  242. Display::url($icons.' '.$lp_item['lp_name'], $lp_url, ['target' => SESSION_LINK_TARGET]),
  243. ];
  244. $temp[$count]['course'] = strip_tags($item['title']);
  245. $temp[$count]['lp'] = $lp_item['lp_name'];
  246. $temp[$count]['date'] = $lp_item['publicated_on'];
  247. $count++;
  248. }
  249. }
  250. $temp = msort($temp, $sidx, $sord);
  251. $i = 0;
  252. $response = new stdClass();
  253. foreach ($temp as $key => $row) {
  254. $row = $row['cell'];
  255. if (!empty($row)) {
  256. if ($key >= $start && $key < ($start + $limit)) {
  257. $response->rows[$i]['id'] = $key;
  258. $response->rows[$i]['cell'] = [$row[0], $row[1], $row[2]];
  259. $i++;
  260. }
  261. }
  262. }
  263. $total_pages = 0;
  264. if ($count > 0 && $limit > 0) {
  265. $total_pages = ceil($count / $limit);
  266. }
  267. $response->total = $total_pages;
  268. if ($page > $total_pages) {
  269. $response->page = $total_pages;
  270. } else {
  271. $response->page = $page;
  272. }
  273. $response->records = $count;
  274. echo json_encode($response);
  275. break;
  276. case 'session_courses_lp_by_week':
  277. require_once __DIR__.'/../global.inc.php';
  278. $now = time();
  279. $page = (int) $_REQUEST['page']; //page
  280. $limit = (int) $_REQUEST['rows']; // quantity of rows
  281. $sidx = isset($_REQUEST['sidx']) && !empty($_REQUEST['sidx']) ? $_REQUEST['sidx'] : 'course';
  282. $sidx = str_replace(['week desc,', ' '], '', $sidx);
  283. $sord = $_REQUEST['sord']; //asc or desc
  284. if (!in_array($sord, ['asc', 'desc'])) {
  285. $sord = 'desc';
  286. }
  287. $session_id = (int) $_REQUEST['session_id'];
  288. $course_id = (int) $_REQUEST['course_id'];
  289. //Filter users that does not belong to the session
  290. if (!api_is_platform_admin()) {
  291. $new_session_list = UserManager::get_personal_session_course_list(api_get_user_id());
  292. $my_session_list = [];
  293. foreach ($new_session_list as $item) {
  294. if (!empty($item['session_id'])) {
  295. $my_session_list[] = $item['session_id'];
  296. }
  297. }
  298. if (!in_array($session_id, $my_session_list)) {
  299. break;
  300. }
  301. }
  302. $start = $limit * $page - $limit;
  303. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  304. $count = 0;
  305. $temp = [];
  306. foreach ($course_list as $item) {
  307. if (isset($course_id) && !empty($course_id)) {
  308. if ($course_id != $item['id']) {
  309. continue;
  310. }
  311. }
  312. $list = new LearnpathList(
  313. api_get_user_id(),
  314. api_get_course_info($item['code']),
  315. $session_id,
  316. 'lp.publicatedOn DESC'
  317. );
  318. $flat_list = $list->get_flat_list();
  319. $lps[$item['code']] = $flat_list;
  320. $item['title'] = Display::url(
  321. $item['title'],
  322. api_get_path(WEB_COURSE_PATH).$item['directory'].'/?id_session='.$session_id,
  323. ['target' => SESSION_LINK_TARGET]
  324. );
  325. foreach ($flat_list as $lp_id => $lp_item) {
  326. $temp[$count]['id'] = $lp_id;
  327. $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';
  328. $last_date = Tracking::get_last_connection_date_on_the_course(
  329. api_get_user_id(),
  330. $item,
  331. $session_id,
  332. false
  333. );
  334. if (empty($lp_item['modified_on'])) {
  335. $lp_date = api_get_local_time($lp_item['created_on']);
  336. $image = 'new.gif';
  337. $label = get_lang('LearnpathAdded');
  338. } else {
  339. $lp_date = api_get_local_time($lp_item['modified_on']);
  340. $image = 'moderator_star.png';
  341. $label = get_lang('LearnpathUpdated');
  342. }
  343. if (strtotime($last_date) < strtotime($lp_date)) {
  344. $icons = Display::return_icon($image, get_lang('TitleNotification').': '.$label.' - '.$lp_date);
  345. }
  346. if (!empty($lp_item['publicated_on'])) {
  347. $date = substr($lp_item['publicated_on'], 0, 10);
  348. } else {
  349. $date = '-';
  350. }
  351. // Checking LP publicated and expired_on dates
  352. if (!empty($lp_item['publicated_on'])) {
  353. $week_data = date('Y', api_strtotime($lp_item['publicated_on'], 'UTC')).' - '.get_week_from_day($lp_item['publicated_on']);
  354. if ($now < api_strtotime($lp_item['publicated_on'], 'UTC')) {
  355. continue;
  356. }
  357. } else {
  358. $week_data = '';
  359. }
  360. if (!empty($lp_item['expired_on'])) {
  361. if ($now > api_strtotime($lp_item['expired_on'], 'UTC')) {
  362. continue;
  363. }
  364. }
  365. $temp[$count]['cell'] = [
  366. $week_data,
  367. $date,
  368. $item['title'],
  369. Display::url($icons.' '.$lp_item['lp_name'], $lp_url, ['target' => SESSION_LINK_TARGET]),
  370. ];
  371. $temp[$count]['course'] = strip_tags($item['title']);
  372. $temp[$count]['lp'] = $lp_item['lp_name'];
  373. $count++;
  374. }
  375. }
  376. if (!empty($sidx)) {
  377. $temp = msort($temp, $sidx, $sord);
  378. }
  379. $response = new stdClass();
  380. $i = 0;
  381. foreach ($temp as $key => $row) {
  382. $row = $row['cell'];
  383. if (!empty($row)) {
  384. if ($key >= $start && $key < ($start + $limit)) {
  385. $response->rows[$i]['id'] = $key;
  386. $response->rows[$i]['cell'] = [$row[0], $row[1], $row[2], $row[3]];
  387. $i++;
  388. }
  389. }
  390. }
  391. $total_pages = 0;
  392. if ($count > 0 && $limit > 0) {
  393. $total_pages = ceil($count / $limit);
  394. }
  395. $response->total = $total_pages;
  396. if ($page > $total_pages) {
  397. $response->page = $total_pages;
  398. } else {
  399. $response->page = $page;
  400. }
  401. $response->records = $count;
  402. echo json_encode($response);
  403. break;
  404. case 'session_courses_lp_by_course':
  405. require_once __DIR__.'/../global.inc.php';
  406. $now = time();
  407. $page = (int) $_REQUEST['page']; //page
  408. $limit = (int) $_REQUEST['rows']; // quantity of rows
  409. $sidx = isset($_REQUEST['sidx']) && !empty($_REQUEST['sidx']) ? $_REQUEST['sidx'] : 'id';
  410. $sidx = str_replace(['course asc,', ' '], '', $sidx);
  411. $sord = $_REQUEST['sord']; //asc or desc
  412. if (!in_array($sord, ['asc', 'desc'])) {
  413. $sord = 'desc';
  414. }
  415. $session_id = (int) $_REQUEST['session_id'];
  416. $course_id = (int) $_REQUEST['course_id'];
  417. //Filter users that does not belong to the session
  418. if (!api_is_platform_admin()) {
  419. $new_session_list = UserManager::get_personal_session_course_list(api_get_user_id());
  420. $my_session_list = [];
  421. foreach ($new_session_list as $item) {
  422. if (!empty($item['session_id'])) {
  423. $my_session_list[] = $item['session_id'];
  424. }
  425. }
  426. if (!in_array($session_id, $my_session_list)) {
  427. break;
  428. }
  429. }
  430. $start = $limit * $page - $limit;
  431. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  432. $count = 0;
  433. $temp = [];
  434. foreach ($course_list as $item) {
  435. if (isset($course_id) && !empty($course_id)) {
  436. if ($course_id != $item['id']) {
  437. continue;
  438. }
  439. }
  440. $list = new LearnpathList(
  441. api_get_user_id(),
  442. api_get_course_info($item['code']),
  443. $session_id
  444. );
  445. $flat_list = $list->get_flat_list();
  446. $lps[$item['code']] = $flat_list;
  447. $item['title'] = Display::url(
  448. $item['title'],
  449. api_get_path(WEB_COURSE_PATH).$item['directory'].'/?id_session='.$session_id,
  450. ['target' => SESSION_LINK_TARGET]
  451. );
  452. foreach ($flat_list as $lp_id => $lp_item) {
  453. $temp[$count]['id'] = $lp_id;
  454. $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';
  455. $last_date = Tracking::get_last_connection_date_on_the_course(
  456. api_get_user_id(),
  457. $item,
  458. $session_id,
  459. false
  460. );
  461. if (empty($lp_item['modified_on'])) {
  462. $lp_date = api_get_local_time($lp_item['created_on']);
  463. $image = 'new.gif';
  464. $label = get_lang('LearnpathAdded');
  465. } else {
  466. $lp_date = api_get_local_time($lp_item['modified_on']);
  467. $image = 'moderator_star.png';
  468. $label = get_lang('LearnpathUpdated');
  469. }
  470. $icons = '';
  471. if (strtotime($last_date) < strtotime($lp_date)) {
  472. $icons = Display::return_icon($image, get_lang('TitleNotification').': '.$label.' - '.$lp_date);
  473. }
  474. if (!empty($lp_item['publicated_on'])) {
  475. $date = substr($lp_item['publicated_on'], 0, 10);
  476. } else {
  477. $date = '-';
  478. }
  479. // Checking LP publicated and expired_on dates
  480. if (!empty($lp_item['publicated_on'])) {
  481. if ($now < api_strtotime($lp_item['publicated_on'], 'UTC')) {
  482. continue;
  483. }
  484. }
  485. if (!empty($lp_item['expired_on'])) {
  486. if ($now > api_strtotime($lp_item['expired_on'], 'UTC')) {
  487. continue;
  488. }
  489. }
  490. $temp[$count]['cell'] = [
  491. $date,
  492. $item['title'],
  493. Display::url($icons.' '.$lp_item['lp_name'], $lp_url, ['target' => SESSION_LINK_TARGET]),
  494. ];
  495. $temp[$count]['course'] = strip_tags($item['title']);
  496. $temp[$count]['lp'] = $lp_item['lp_name'];
  497. $temp[$count]['date'] = $lp_item['publicated_on'];
  498. $count++;
  499. }
  500. }
  501. $temp = msort($temp, $sidx, $sord);
  502. $response = new stdClass();
  503. $i = 0;
  504. foreach ($temp as $key => $row) {
  505. $row = $row['cell'];
  506. if (!empty($row)) {
  507. if ($key >= $start && $key < ($start + $limit)) {
  508. $response->rows[$i]['id'] = $key;
  509. $response->rows[$i]['cell'] = [$row[0], $row[1], $row[2]];
  510. $i++;
  511. }
  512. }
  513. }
  514. $total_pages = 0;
  515. if ($count > 0 && $limit > 0) {
  516. $total_pages = ceil($count / $limit);
  517. }
  518. $response->total = $total_pages;
  519. $response->page = $page;
  520. if ($page > $total_pages) {
  521. $response->page = $total_pages;
  522. }
  523. $response->records = $count;
  524. echo json_encode($response);
  525. break;
  526. case 'get_notification':
  527. $courseId = isset($_REQUEST['course_id']) ? (int) $_REQUEST['course_id'] : 0;
  528. $sessionId = isset($_REQUEST['session_id']) ? (int) $_REQUEST['session_id'] : 0;
  529. $status = isset($_REQUEST['status']) ? (int) $_REQUEST['status'] : 0;
  530. if (empty($courseId)) {
  531. break;
  532. }
  533. require_once __DIR__.'/../global.inc.php';
  534. $courseInfo = api_get_course_info_by_id($courseId);
  535. $courseInfo['id_session'] = $sessionId;
  536. $courseInfo['status'] = $status;
  537. $id = 'notification_'.$courseId.'_'.$sessionId.'_'.$status;
  538. $notificationId = Session::read($id);
  539. if ($notificationId) {
  540. echo Display::show_notification($courseInfo, false);
  541. Session::erase($notificationId);
  542. }
  543. break;
  544. default:
  545. echo '';
  546. }
  547. exit;