StudentFollowUpPlugin.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\SessionRelCourseRelUser;
  4. use Symfony\Component\Filesystem\Filesystem;
  5. /**
  6. * Class StudentFollowUpPlugin.
  7. */
  8. class StudentFollowUpPlugin extends Plugin
  9. {
  10. public $hasEntity = true;
  11. /**
  12. * StudentFollowUpPlugin constructor.
  13. */
  14. protected function __construct()
  15. {
  16. parent::__construct(
  17. '0.1',
  18. 'Julio Montoya',
  19. [
  20. 'tool_enable' => 'boolean',
  21. ]
  22. );
  23. }
  24. /**
  25. * @return StudentFollowUpPlugin
  26. */
  27. public static function create()
  28. {
  29. static $result = null;
  30. return $result ? $result : $result = new self();
  31. }
  32. public function install()
  33. {
  34. $pluginEntityPath = $this->getEntityPath();
  35. if (!is_dir($pluginEntityPath)) {
  36. if (!is_writable(dirname($pluginEntityPath))) {
  37. $message = get_lang('Can\'t create the directory. Please contact your system administrator.').': '.$pluginEntityPath;
  38. Display::addFlash(Display::return_message($message, 'error'));
  39. return false;
  40. }
  41. mkdir($pluginEntityPath, api_get_permissions_for_new_directories());
  42. }
  43. $fs = new Filesystem();
  44. $fs->mirror(__DIR__.'/Entity/', $pluginEntityPath, null, ['override']);
  45. $schema = Database::getManager()->getConnection()->getSchemaManager();
  46. if ($schema->tablesExist('sfu_post') === false) {
  47. $sql = "CREATE TABLE IF NOT EXISTS 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;";
  48. Database::query($sql);
  49. $sql = "ALTER TABLE sfu_post ADD CONSTRAINT FK_35F9473C9C859CC3 FOREIGN KEY (insert_user_id) REFERENCES user (id);";
  50. Database::query($sql);
  51. $sql = "ALTER TABLE sfu_post ADD CONSTRAINT FK_35F9473CA76ED395 FOREIGN KEY (user_id) REFERENCES user (id);";
  52. Database::query($sql);
  53. $sql = "ALTER TABLE sfu_post ADD CONSTRAINT FK_35F9473C727ACA70 FOREIGN KEY (parent_id) REFERENCES sfu_post (id) ON DELETE SET NULL;";
  54. Database::query($sql);
  55. }
  56. }
  57. public function uninstall()
  58. {
  59. $pluginEntityPath = $this->getEntityPath();
  60. $fs = new Filesystem();
  61. if ($fs->exists($pluginEntityPath)) {
  62. $fs->remove($pluginEntityPath);
  63. }
  64. $table = Database::get_main_table('sfu_post');
  65. $sql = "DROP TABLE IF EXISTS $table";
  66. Database::query($sql);
  67. }
  68. /**
  69. * @return string
  70. */
  71. public function getEntityPath()
  72. {
  73. return api_get_path(SYS_PATH).'src/Chamilo/PluginBundle/Entity/'.$this->getCamelCaseName();
  74. }
  75. /**
  76. * @param int $studentId
  77. * @param int $currentUserId
  78. *
  79. * @return array
  80. */
  81. public static function getPermissions($studentId, $currentUserId)
  82. {
  83. $installed = AppPlugin::getInstance()->isInstalled('studentfollowup');
  84. if ($installed === false) {
  85. return [
  86. 'is_allow' => false,
  87. 'show_private' => false,
  88. ];
  89. }
  90. if ($studentId === $currentUserId) {
  91. $isAllow = true;
  92. $showPrivate = true;
  93. } else {
  94. $isDrh = api_is_drh();
  95. $isCareTaker = false;
  96. $isDrhRelatedViaPost = false;
  97. $isCourseCoach = false;
  98. $isDrhRelatedToSession = false;
  99. // Only admins and DRH that follow the user
  100. $isAdmin = api_is_platform_admin();
  101. // Check if user is care taker
  102. if ($isDrh) {
  103. $criteria = [
  104. 'user' => $studentId,
  105. 'insertUser' => $currentUserId,
  106. ];
  107. $repo = Database::getManager()->getRepository('ChamiloPluginBundle:StudentFollowUp\CarePost');
  108. $post = $repo->findOneBy($criteria);
  109. if ($post) {
  110. $isDrhRelatedViaPost = true;
  111. }
  112. }
  113. // Check if course session coach
  114. $sessions = SessionManager::get_sessions_by_user($studentId, false, true);
  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. $coachList = SessionManager::getCoachesByCourseSession(
  128. $sessionId,
  129. $course['real_id']
  130. );
  131. if (!empty($coachList) && in_array($currentUserId, $coachList)) {
  132. $isCourseCoach = true;
  133. break 2;
  134. }
  135. }
  136. }
  137. }
  138. $isCareTaker = $isDrhRelatedViaPost && $isDrhRelatedToSession;
  139. $isAllow = $isAdmin || $isCareTaker || $isDrhRelatedToSession || $isCourseCoach;
  140. $showPrivate = $isAdmin || $isCareTaker;
  141. }
  142. return [
  143. 'is_allow' => $isAllow,
  144. 'show_private' => $showPrivate,
  145. ];
  146. }
  147. /**
  148. * @param string $status
  149. * @param int $currentUserId
  150. * @param int $sessionId
  151. * @param int $start
  152. * @param int $limit
  153. *
  154. * @return array
  155. */
  156. public static function getUsers($status, $currentUserId, $sessionId, $start, $limit)
  157. {
  158. $sessions = [];
  159. $courses = [];
  160. $sessionsFull = [];
  161. switch ($status) {
  162. case COURSEMANAGER:
  163. $sessionsFull = SessionManager::getSessionsCoachedByUser($currentUserId);
  164. $sessions = array_column($sessionsFull, 'id');
  165. if (!empty($sessionId)) {
  166. $sessions = [$sessionId];
  167. }
  168. // Get session courses where I'm coach
  169. $courseList = SessionManager::getCoursesListByCourseCoach($currentUserId);
  170. $courses = [];
  171. /** @var SessionRelCourseRelUser $courseItem */
  172. foreach ($courseList as $courseItem) {
  173. $courses[] = $courseItem->getCourse()->getId();
  174. }
  175. break;
  176. case DRH:
  177. $sessionsFull = SessionManager::get_sessions_followed_by_drh($currentUserId);
  178. $sessions = array_column($sessionsFull, 'id');
  179. if (!empty($sessionId)) {
  180. $sessions = [$sessionId];
  181. }
  182. $courses = [];
  183. foreach ($sessions as $sessionId) {
  184. $sessionDrhInfo = SessionManager::getSessionFollowedByDrh(
  185. $currentUserId,
  186. $sessionId
  187. );
  188. if ($sessionDrhInfo && isset($sessionDrhInfo['course_list'])) {
  189. $courses = array_merge($courses, array_column($sessionDrhInfo['course_list'], 'id'));
  190. }
  191. }
  192. break;
  193. }
  194. $userList = SessionManager::getUsersByCourseAndSessionList(
  195. $sessions,
  196. $courses,
  197. $start,
  198. $limit
  199. );
  200. return [
  201. 'users' => $userList,
  202. 'sessions' => $sessionsFull,
  203. ];
  204. }
  205. /**
  206. * @return int
  207. */
  208. public static function getPageSize()
  209. {
  210. return 20;
  211. }
  212. /**
  213. * @param int $userId
  214. */
  215. public function doWhenDeletingUser($userId)
  216. {
  217. $userId = (int) $userId;
  218. Database::query("DELETE FROM sfu_post WHERE user_id = $userId");
  219. Database::query("DELETE FROM sfu_post WHERE insert_user_id = $userId");
  220. }
  221. }