configure.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once __DIR__.'/../../main/inc/global.inc.php';
  4. use Chamilo\PluginBundle\Entity\CourseHomeNotify\Notification;
  5. $plugin = CourseHomeNotifyPlugin::create();
  6. $courseId = api_get_course_int_id();
  7. if (
  8. empty($courseId) ||
  9. 'true' !== $plugin->get(CourseHomeNotifyPlugin::SETTING_ENABLED)
  10. ) {
  11. api_not_allowed(true);
  12. }
  13. $action = isset($_GET['action']) ? $_GET['action'] : '';
  14. $course = api_get_course_entity($courseId);
  15. $em = Database::getManager();
  16. /** @var Notification $notification */
  17. $notification = $em
  18. ->getRepository('ChamiloPluginBundle:CourseHomeNotify\Notification')
  19. ->findOneBy(['course' => $course]);
  20. $actionLinks = '';
  21. if ($notification) {
  22. $actionLinks = Display::url(
  23. Display::return_icon('delete.png', $plugin->get_lang('DeleteNotification'), [], ICON_SIZE_MEDIUM),
  24. api_get_self().'?'.api_get_cidreq().'&action=delete'
  25. );
  26. if ('delete' === $action) {
  27. $em->remove($notification);
  28. $em->flush();
  29. Display::addFlash(
  30. Display::return_message($plugin->get_lang('NotificationDeleted'), 'success')
  31. );
  32. header('Location: '.api_get_course_url());
  33. exit;
  34. }
  35. } else {
  36. $notification = new Notification();
  37. }
  38. $form = new FormValidator('frm_course_home_notify');
  39. $form->addHeader($plugin->get_lang('AddNotification'));
  40. $form->applyFilter('title', 'trim');
  41. $form->addHtmlEditor('content', get_lang('Content'), true, false, ['ToolbarSet' => 'Minimal']);
  42. $form->addUrl(
  43. 'expiration_link',
  44. [$plugin->get_lang('ExpirationLink'), $plugin->get_lang('ExpirationLinkHelp')],
  45. false,
  46. ['placeholder' => 'https://']
  47. );
  48. $form->addButtonSave(get_lang('Save'));
  49. if ($form->validate()) {
  50. $values = $form->exportValues();
  51. $notification
  52. ->setContent($values['content'])
  53. ->setExpirationLink($values['expiration_link'])
  54. ->setCourse($course)
  55. ->setHash(md5(uniqid()));
  56. $em->persist($notification);
  57. $em->flush();
  58. Display::addFlash(
  59. Display::return_message($plugin->get_lang('NotificationAdded'), 'success')
  60. );
  61. header('Location: '.api_get_course_url());
  62. exit;
  63. }
  64. if ($notification) {
  65. $form->setDefaults(
  66. [
  67. 'content' => $notification->getContent(),
  68. 'expiration_link' => $notification->getExpirationLink(),
  69. ]
  70. );
  71. }
  72. $template = new Template($plugin->get_title());
  73. $template->assign('header', $plugin->get_title());
  74. if ($actionLinks) {
  75. $template->assign('actions', Display::toolbarAction('course-home-notify-actions', ['', $actionLinks]));
  76. }
  77. $template->assign('content', $form->returnForm());
  78. $template->display_one_col_template();