CourseHomeNotifyPlugin.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\PluginBundle\Entity\CourseHomeNotify\Notification;
  4. use Chamilo\PluginBundle\Entity\CourseHomeNotify\NotificationRelUser;
  5. use Symfony\Component\Filesystem\Filesystem;
  6. /**
  7. * Class CourseHomeNotifyPlugin.
  8. */
  9. class CourseHomeNotifyPlugin extends Plugin
  10. {
  11. const SETTING_ENABLED = 'enabled';
  12. /**
  13. * CourseHomeNotifyPlugin constructor.
  14. */
  15. protected function __construct()
  16. {
  17. $settings = [
  18. self::SETTING_ENABLED => 'boolean',
  19. ];
  20. parent::__construct('0.1', 'Angel Fernando Quiroz Campos', $settings);
  21. $this->isCoursePlugin = true;
  22. $this->addCourseTool = false;
  23. $this->setCourseSettings();
  24. }
  25. /**
  26. * @return CourseHomeNotifyPlugin|null
  27. */
  28. public static function create()
  29. {
  30. static $result = null;
  31. return $result ? $result : $result = new self();
  32. }
  33. /**
  34. * Install process.
  35. * Create table in database. And setup Doctirne entity.
  36. */
  37. public function install()
  38. {
  39. $pluginEntityPath = $this->getEntityPath();
  40. if (!is_dir($pluginEntityPath)) {
  41. if (!is_writable(dirname($pluginEntityPath))) {
  42. $message = get_lang('ErrorCreatingDir').': '.$pluginEntityPath;
  43. Display::addFlash(Display::return_message($message, 'error'));
  44. return;
  45. }
  46. mkdir($pluginEntityPath, api_get_permissions_for_new_directories());
  47. }
  48. $fs = new Filesystem();
  49. $fs->mirror(__DIR__.'/Entity/', $pluginEntityPath, null, ['override']);
  50. $schema = Database::getManager()->getConnection()->getSchemaManager();
  51. if (false === $schema->tablesExist('course_home_notify_notification')) {
  52. $sql = "CREATE TABLE course_home_notify_notification_rel_user (id INT AUTO_INCREMENT NOT NULL, notification_id INT NOT NULL, user_id INT NOT NULL, INDEX IDX_13E723DDEF1A9D84 (notification_id), INDEX IDX_13E723DDA76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB";
  53. Database::query($sql);
  54. $sql = "CREATE TABLE course_home_notify_notification (id INT AUTO_INCREMENT NOT NULL, c_id INT NOT NULL, content LONGTEXT NOT NULL, expiration_link VARCHAR(255) NOT NULL, hash VARCHAR(255) NOT NULL, INDEX IDX_7C6C1B0191D79BD3 (c_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB";
  55. Database::query($sql);
  56. $sql = "ALTER TABLE course_home_notify_notification_rel_user ADD CONSTRAINT FK_13E723DDEF1A9D84 FOREIGN KEY (notification_id) REFERENCES course_home_notify_notification (id) ON DELETE CASCADE";
  57. Database::query($sql);
  58. $sql = "ALTER TABLE course_home_notify_notification_rel_user ADD CONSTRAINT FK_13E723DDA76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE";
  59. Database::query($sql);
  60. $sql = "ALTER TABLE course_home_notify_notification ADD CONSTRAINT FK_7C6C1B0191D79BD3 FOREIGN KEY (c_id) REFERENCES course (id) ON DELETE CASCADE";
  61. Database::query($sql);
  62. }
  63. }
  64. /**
  65. * @return string
  66. */
  67. public function getEntityPath()
  68. {
  69. return api_get_path(SYS_PATH).'src/Chamilo/PluginBundle/Entity/'.$this->getCamelCaseName();
  70. }
  71. /**
  72. * Uninstall process.
  73. * Remove Doctrine entity. And drop table in database.
  74. */
  75. public function uninstall()
  76. {
  77. $pluginEntityPath = $this->getEntityPath();
  78. $fs = new Filesystem();
  79. if ($fs->exists($pluginEntityPath)) {
  80. $fs->remove($pluginEntityPath);
  81. }
  82. $table = Database::get_main_table('course_home_notify_notification_rel_user');
  83. Database::query("DROP TABLE IF EXISTS $table");
  84. $table = Database::get_main_table('course_home_notify_notification');
  85. Database::query("DROP TABLE IF EXISTS $table");
  86. }
  87. /**
  88. * @param string $region
  89. *
  90. * @return string
  91. */
  92. public function renderRegion($region)
  93. {
  94. if (
  95. 'main_bottom' !== $region
  96. || strpos($_SERVER['SCRIPT_NAME'], 'course_home/course_home.php') === false
  97. ) {
  98. return '';
  99. }
  100. $courseId = api_get_course_int_id();
  101. $userId = api_get_user_id();
  102. if (empty($courseId) || empty($userId)) {
  103. return '';
  104. }
  105. $course = api_get_course_entity($courseId);
  106. $user = api_get_user_entity($userId);
  107. $em = Database::getManager();
  108. /** @var Notification $notification */
  109. $notification = $em
  110. ->getRepository('ChamiloPluginBundle:CourseHomeNotify\Notification')
  111. ->findOneBy(['course' => $course]);
  112. if (!$notification) {
  113. return '';
  114. }
  115. $modalFooter = '';
  116. $modalConfig = ['show' => true];
  117. if ($notification->getExpirationLink()) {
  118. /** @var NotificationRelUser $notificationUser */
  119. $notificationUser = $em
  120. ->getRepository('ChamiloPluginBundle:CourseHomeNotify\NotificationRelUser')
  121. ->findOneBy(['notification' => $notification, 'user' => $user]);
  122. if ($notificationUser) {
  123. return '';
  124. }
  125. $contentUrl = api_get_path(WEB_PLUGIN_PATH).$this->get_name().'/content.php?hash='.$notification->getHash();
  126. $link = Display::toolbarButton(
  127. $this->get_lang('PleaseFollowThisLink'),
  128. $contentUrl,
  129. 'external-link',
  130. 'link',
  131. ['id' => 'course-home-notify-link', 'target' => '_blank']
  132. );
  133. $modalConfig['keyboard'] = false;
  134. $modalConfig['backdrop'] = 'static';
  135. $modalFooter = '<div class="modal-footer">'.$link.'</div>';
  136. }
  137. $modal = '<div id="course-home-notify-modal" class="modal" tabindex="-1" role="dialog">
  138. <div class="modal-dialog" role="document">
  139. <div class="modal-content">
  140. <div class="modal-header">
  141. <button type="button" class="close" data-dismiss="modal" aria-label="'.get_lang('Close').'">
  142. <span aria-hidden="true">&times;</span>
  143. </button>
  144. <h4 class="modal-title">'.$this->get_lang('CourseNotice').'</h4>
  145. </div>
  146. <div class="modal-body">
  147. '.$notification->getContent().'
  148. </div>
  149. '.$modalFooter.'
  150. </div>
  151. </div>
  152. </div>';
  153. $modal .= "<script>
  154. $(document).ready(function () {
  155. \$('#course-home-notify-modal').modal(".json_encode($modalConfig).");
  156. \$('#course-home-notify-link').on('click', function () {
  157. $('#course-home-notify-modal').modal('hide');
  158. });
  159. });
  160. </script>";
  161. return $modal;
  162. }
  163. /**
  164. * Set the course settings.
  165. */
  166. private function setCourseSettings()
  167. {
  168. if ('true' !== $this->get(self::SETTING_ENABLED)) {
  169. return;
  170. }
  171. $name = $this->get_name();
  172. $button = Display::toolbarButton(
  173. $this->get_lang('SetNotification'),
  174. api_get_path(WEB_PLUGIN_PATH).$name.'/configure.php?'.api_get_cidreq(),
  175. 'cog',
  176. 'primary'
  177. );
  178. $this->course_settings = [
  179. [
  180. 'name' => '<p>'.$this->get_comment().'</p>'.$button.'<hr>',
  181. 'type' => 'html',
  182. ],
  183. ];
  184. }
  185. }