start.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\PluginBundle\Entity\EmbedRegistry\Embed;
  4. require_once __DIR__.'/../../main/inc/global.inc.php';
  5. api_block_anonymous_users();
  6. api_protect_course_script(true);
  7. $plugin = EmbedRegistryPlugin::create();
  8. if ('false' === $plugin->get(EmbedRegistryPlugin::SETTING_ENABLED)) {
  9. api_not_allowed(true);
  10. }
  11. $isAllowedToEdit = api_is_allowed_to_edit(true);
  12. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
  13. $em = Database::getManager();
  14. $embedRepo = $em->getRepository('ChamiloPluginBundle:EmbedRegistry\Embed');
  15. $course = api_get_course_entity(api_get_course_int_id());
  16. $session = api_get_session_entity(api_get_session_id());
  17. $actions = [];
  18. $view = new Template($plugin->getToolTitle());
  19. $view->assign('is_allowed_to_edit', $isAllowedToEdit);
  20. switch ($action) {
  21. case 'add':
  22. if (!$isAllowedToEdit) {
  23. api_not_allowed(true);
  24. }
  25. $actions[] = Display::url(
  26. Display::return_icon('back.png', get_lang('Back'), [], ICON_SIZE_MEDIUM),
  27. api_get_self()
  28. );
  29. $form = new FormValidator('frm_edit');
  30. $form->addText(
  31. 'title',
  32. [get_lang('Title'), $plugin->get_lang('EmbedTitleHelp')],
  33. true
  34. );
  35. $form->addDateRangePicker(
  36. 'range',
  37. [get_lang('DateRange'), $plugin->get_lang('EmbedDateRangeHelp')]
  38. );
  39. $form->addTextarea(
  40. 'html_code',
  41. [$plugin->get_lang('HtmlCode'), $plugin->get_lang('HtmlCodeHelp')],
  42. ['rows' => 5],
  43. true
  44. );
  45. $form->addButtonUpdate(get_lang('Add'));
  46. $form->addHidden('action', 'add');
  47. if ($form->validate()) {
  48. $values = $form->exportValues();
  49. $startDate = api_get_utc_datetime($values['range_start'], false, true);
  50. $endDate = api_get_utc_datetime($values['range_end'], false, true);
  51. $embed = new Embed();
  52. $embed
  53. ->setTitle($values['title'])
  54. ->setDisplayStartDate($startDate)
  55. ->setDisplayEndDate($endDate)
  56. ->setHtmlCode($values['html_code'])
  57. ->setCourse($course)
  58. ->setSession($session);
  59. $em->persist($embed);
  60. $em->flush();
  61. Display::addFlash(
  62. Display::return_message(get_lang('Added'), 'success')
  63. );
  64. header('Location: '.api_get_self());
  65. exit;
  66. }
  67. $view->assign('header', $plugin->get_lang('CreateEmbeddable'));
  68. $view->assign('form', $form->returnForm());
  69. $externalUrl = $plugin->get(EmbedRegistryPlugin::SETTING_EXTERNAL_URL);
  70. if (!empty($externalUrl)) {
  71. $view->assign('external_url', $externalUrl);
  72. }
  73. break;
  74. case 'edit':
  75. if (!$isAllowedToEdit) {
  76. api_not_allowed(true);
  77. }
  78. $actions[] = Display::url(
  79. Display::return_icon('back.png', get_lang('Back'), [], ICON_SIZE_MEDIUM),
  80. api_get_self()
  81. );
  82. $embedId = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
  83. if (!$embedId) {
  84. break;
  85. }
  86. /** @var Embed|null $embed */
  87. $embed = $embedRepo->find($embedId);
  88. if (!$embed) {
  89. Display::addFlash(Display::return_message($plugin->get_lang('ContentNotFound'), 'danger'));
  90. break;
  91. }
  92. $form = new FormValidator('frm_edit');
  93. $form->addText('title', get_lang('Title'), true);
  94. $form->addDateRangePicker('range', get_lang('DateRange'));
  95. $form->addTextarea('html_code', $plugin->get_lang('HtmlCode'), ['rows' => 5], true);
  96. $form->addButtonUpdate(get_lang('Edit'));
  97. $form->addHidden('id', $embed->getId());
  98. $form->addHidden('action', 'edit');
  99. if ($form->validate()) {
  100. $values = $form->exportValues();
  101. $startDate = api_get_utc_datetime($values['range_start'], false, true);
  102. $endDate = api_get_utc_datetime($values['range_end'], false, true);
  103. $embed
  104. ->setTitle($values['title'])
  105. ->setDisplayStartDate($startDate)
  106. ->setDisplayEndDate($endDate)
  107. ->setHtmlCode($values['html_code']);
  108. $em->persist($embed);
  109. $em->flush();
  110. Display::addFlash(
  111. Display::return_message(get_lang('Updated'), 'success')
  112. );
  113. header('Location: '.api_get_self());
  114. exit;
  115. }
  116. $form->setDefaults(
  117. [
  118. 'title' => $embed->getTitle(),
  119. 'range' => api_get_local_time($embed->getDisplayStartDate())
  120. .' / '
  121. .api_get_local_time($embed->getDisplayEndDate()),
  122. 'html_code' => $embed->getHtmlCode(),
  123. ]
  124. );
  125. $view->assign('header', $plugin->get_lang('EditEmbeddable'));
  126. $view->assign('form', $form->returnForm());
  127. break;
  128. case 'delete':
  129. $embedId = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
  130. if (!$embedId) {
  131. break;
  132. }
  133. /** @var Embed|null $embed */
  134. $embed = $embedRepo->find($embedId);
  135. if (!$embed) {
  136. Display::addFlash(Display::return_message($plugin->get_lang('ContentNotFound'), 'danger'));
  137. break;
  138. }
  139. $em->remove($embed);
  140. $em->flush();
  141. Display::addFlash(
  142. Display::return_message(get_lang('Deleted'), 'success')
  143. );
  144. header('Location: '.api_get_self());
  145. exit;
  146. default:
  147. $currentEmbed = $plugin->getCurrentEmbed($course, $session);
  148. /** @var array|Embed[] $embeds */
  149. $embeds = $embedRepo->findBy(['course' => $course, 'session' => $session]);
  150. $tableData = [];
  151. foreach ($embeds as $embed) {
  152. $data = [
  153. $embed->getTitle(),
  154. api_convert_and_format_date($embed->getDisplayStartDate()),
  155. api_convert_and_format_date($embed->getDisplayEndDate()),
  156. $embed,
  157. ];
  158. if ($isAllowedToEdit) {
  159. $data[] = $embed;
  160. }
  161. $tableData[] = $data;
  162. }
  163. if ($isAllowedToEdit) {
  164. $btnAdd = Display::toolbarButton(
  165. $plugin->get_lang('CreateEmbeddable'),
  166. api_get_self().'?action=add',
  167. 'file-code-o',
  168. 'primary'
  169. );
  170. $view->assign(
  171. 'actions',
  172. Display::toolbarAction($plugin->get_name(), [$btnAdd])
  173. );
  174. if (in_array($action, ['add', 'edit'])) {
  175. $view->assign('form', $form->returnForm());
  176. }
  177. }
  178. if ($currentEmbed) {
  179. $view->assign('current_embed', $currentEmbed);
  180. $view->assign(
  181. 'current_link',
  182. Display::toolbarButton(
  183. $plugin->get_lang('LaunchContent'),
  184. $plugin->getViewUrl($embed),
  185. 'rocket',
  186. 'info'
  187. )
  188. );
  189. }
  190. $table = new SortableTableFromArray($tableData, 1);
  191. $table->set_header(0, get_lang('Title'));
  192. $table->set_header(1, get_lang('AvailableFrom'), true, 'th-header text-center', ['class' => 'text-center']);
  193. $table->set_header(2, get_lang('AvailableTill'), true, 'th-header text-center', ['class' => 'text-center']);
  194. if ($isAllowedToEdit) {
  195. $table->set_header(3, get_lang('Members'), false, 'th-header text-right', ['class' => 'text-right']);
  196. $table->set_column_filter(
  197. 3,
  198. function (Embed $value) use ($plugin) {
  199. return $plugin->getMembersCount($value);
  200. }
  201. );
  202. }
  203. $table->set_header(
  204. $isAllowedToEdit ? 4 : 3,
  205. get_lang('Actions'),
  206. false,
  207. 'th-header text-right',
  208. ['class' => 'text-right']
  209. );
  210. $table->set_column_filter(
  211. $isAllowedToEdit ? 4 : 3,
  212. function (Embed $value) use ($isAllowedToEdit, $plugin) {
  213. $actions = [];
  214. $actions[] = Display::url(
  215. Display::return_icon('external_link.png', get_lang('View')),
  216. $plugin->getViewUrl($value)
  217. );
  218. if ($isAllowedToEdit) {
  219. $actions[] = Display::url(
  220. Display::return_icon('edit.png', get_lang('Edit')),
  221. api_get_self().'?action=edit&id='.$value->getId()
  222. );
  223. $actions[] = Display::url(
  224. Display::return_icon('delete.png', get_lang('Delete')),
  225. api_get_self().'?action=delete&id='.$value->getId()
  226. );
  227. }
  228. return implode(PHP_EOL, $actions);
  229. }
  230. );
  231. $view->assign('embeds', $embeds);
  232. $view->assign('table', $table->return_table());
  233. }
  234. $content = $view->fetch('embedregistry/view/start.tpl');
  235. if ($actions) {
  236. $actions = implode(PHP_EOL, $actions);
  237. $view->assign(
  238. 'actions',
  239. Display::toolbarAction($plugin->get_name(), [$actions])
  240. );
  241. }
  242. $view->assign('content', $content);
  243. $view->display_one_col_template();