StudentFollowUpPlugin.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Symfony\Component\Filesystem\Filesystem;
  4. /**
  5. * Class StudentFollowUpPlugin
  6. */
  7. class StudentFollowUpPlugin extends Plugin
  8. {
  9. public $hasEntity = true;
  10. /**
  11. * @return StudentFollowUpPlugin
  12. */
  13. public static function create()
  14. {
  15. static $result = null;
  16. return $result ? $result : $result = new self();
  17. }
  18. /**
  19. * StudentFollowUpPlugin constructor.
  20. */
  21. protected function __construct()
  22. {
  23. parent::__construct(
  24. '0.1',
  25. 'Julio Montoya',
  26. array(
  27. 'tool_enable' => 'boolean'
  28. )
  29. );
  30. }
  31. /**
  32. *
  33. */
  34. public function install()
  35. {
  36. $pluginEntityPath = $this->getEntityPath();
  37. if (!is_dir($pluginEntityPath)) {
  38. mkdir($pluginEntityPath, api_get_permissions_for_new_directories());
  39. }
  40. $fs = new Filesystem();
  41. $fs->mirror(__DIR__.'/Entity/', $pluginEntityPath, null, ['override']);
  42. $sql = "CREATE TABLE sfu_post (id INT AUTO_INCREMENT NOT NULL, insert_user_id INT NOT NULL, user_id INT NOT NULL, parent_id INT DEFAULT NULL, title VARCHAR(255) NOT NULL, content LONGTEXT DEFAULT NULL, external_care_id VARCHAR(255) DEFAULT NULL, created_at DATETIME DEFAULT NULL, updated_at DATETIME DEFAULT NULL, private TINYINT(1) NOT NULL, external_source TINYINT(1) NOT NULL, tags LONGTEXT NOT NULL COMMENT '(DC2Type:array)', attachment VARCHAR(255) NOT NULL, lft INT DEFAULT NULL, rgt INT DEFAULT NULL, lvl INT DEFAULT NULL, root INT DEFAULT NULL, INDEX IDX_35F9473C9C859CC3 (insert_user_id), INDEX IDX_35F9473CA76ED395 (user_id), INDEX IDX_35F9473C727ACA70 (parent_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;";
  43. Database::query($sql);
  44. $sql = "ALTER TABLE sfu_post ADD CONSTRAINT FK_35F9473C9C859CC3 FOREIGN KEY (insert_user_id) REFERENCES user (id);";
  45. Database::query($sql);
  46. $sql = "ALTER TABLE sfu_post ADD CONSTRAINT FK_35F9473CA76ED395 FOREIGN KEY (user_id) REFERENCES user (id);";
  47. Database::query($sql);
  48. $sql = "ALTER TABLE sfu_post ADD CONSTRAINT FK_35F9473C727ACA70 FOREIGN KEY (parent_id) REFERENCES sfu_post (id) ON DELETE SET NULL;";
  49. Database::query($sql);
  50. }
  51. /**
  52. *
  53. */
  54. public function uninstall()
  55. {
  56. $pluginEntityPath = $this->getEntityPath();
  57. $fs = new Filesystem();
  58. if ($fs->exists($pluginEntityPath)) {
  59. $fs->remove($pluginEntityPath);
  60. }
  61. $table = Database::get_main_table('sfu_post');
  62. $sql = "DROP TABLE IF EXISTS $table";
  63. Database::query($sql);
  64. }
  65. /**
  66. * @return string
  67. */
  68. public function getEntityPath()
  69. {
  70. return api_get_path(SYS_PATH).'src/Chamilo/PluginBundle/Entity/'.$this->getCamelCaseName();
  71. }
  72. /**
  73. * @param int $studentId
  74. * @param int $currentUserId
  75. * @return array
  76. */
  77. public static function getPermissions($studentId, $currentUserId)
  78. {
  79. $params = ['variable = ? AND subkey = ?' => ['status', 'studentfollowup']];
  80. $result = api_get_settings_params_simple($params);
  81. $installed = false;
  82. if (!empty($result) && $result['selected_value'] === 'installed') {
  83. $installed = true;
  84. }
  85. if ($installed == false) {
  86. return [
  87. 'is_allow' => false,
  88. 'show_private' => false,
  89. ];
  90. }
  91. if ($studentId === $currentUserId) {
  92. $isAllow = true;
  93. $showPrivate = true;
  94. } else {
  95. $isDrh = api_is_drh();
  96. $isCareTaker = false;
  97. $isDrhRelatedViaPost = false;
  98. $isCourseCoach = false;
  99. $isDrhRelatedToSession = false;
  100. // Only admins and DRH that follow the user
  101. $isAdmin = api_is_platform_admin();
  102. // Check if user is care taker
  103. if ($isDrh) {
  104. $criteria = [
  105. 'user' => $studentId,
  106. 'insertUser' => $currentUserId
  107. ];
  108. $repo = Database::getManager()->getRepository('ChamiloPluginBundle:StudentFollowUp\CarePost');
  109. $post = $repo->findOneBy($criteria);
  110. if ($post) {
  111. $isDrhRelatedViaPost = true;
  112. }
  113. // Check if course session coach
  114. $sessions = SessionManager::get_sessions_by_user($studentId);
  115. if (!empty($sessions)) {
  116. foreach ($sessions as $session) {
  117. $sessionId = $session['session_id'];
  118. $sessionDrhInfo = SessionManager::getSessionFollowedByDrh(
  119. $currentUserId,
  120. $sessionId
  121. );
  122. if (!empty($sessionDrhInfo)) {
  123. $isDrhRelatedToSession = true;
  124. break;
  125. }
  126. foreach ($session['courses'] as $course) {
  127. //$isCourseCoach = api_is_coach($sessionId, $course['real_id']);
  128. $coachList = SessionManager::getCoachesByCourseSession(
  129. $sessionId,
  130. $course['real_id']
  131. );
  132. if (!empty($coachList) && in_array($currentUserId, $coachList)) {
  133. $isCourseCoach = true;
  134. break(2);
  135. }
  136. }
  137. }
  138. }
  139. $isCareTaker = $isDrhRelatedViaPost && $isDrhRelatedToSession;
  140. }
  141. $isAllow = $isAdmin || $isCareTaker || $isDrhRelatedToSession || $isCourseCoach;
  142. $showPrivate = $isAdmin || $isCareTaker;
  143. }
  144. return [
  145. 'is_allow' => $isAllow,
  146. 'show_private' => $showPrivate,
  147. ];
  148. }
  149. }