StudentFollowUpPlugin.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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('ErrorCreatingDir').': '.$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. $params = ['variable = ? AND subkey = ?' => ['status', 'studentfollowup']];
  84. $result = api_get_settings_params_simple($params);
  85. $installed = false;
  86. if (!empty($result) && $result['selected_value'] === 'installed') {
  87. $installed = true;
  88. }
  89. if ($installed == false) {
  90. return [
  91. 'is_allow' => false,
  92. 'show_private' => false,
  93. ];
  94. }
  95. if ($studentId === $currentUserId) {
  96. $isAllow = true;
  97. $showPrivate = true;
  98. } else {
  99. $isDrh = api_is_drh();
  100. $isCareTaker = false;
  101. $isDrhRelatedViaPost = false;
  102. $isCourseCoach = false;
  103. $isDrhRelatedToSession = false;
  104. // Only admins and DRH that follow the user
  105. $isAdmin = api_is_platform_admin();
  106. // Check if user is care taker
  107. if ($isDrh) {
  108. $criteria = [
  109. 'user' => $studentId,
  110. 'insertUser' => $currentUserId,
  111. ];
  112. $repo = Database::getManager()->getRepository('ChamiloPluginBundle:StudentFollowUp\CarePost');
  113. $post = $repo->findOneBy($criteria);
  114. if ($post) {
  115. $isDrhRelatedViaPost = true;
  116. }
  117. }
  118. // Check if course session coach
  119. $sessions = SessionManager::get_sessions_by_user($studentId, false, true);
  120. if (!empty($sessions)) {
  121. foreach ($sessions as $session) {
  122. $sessionId = $session['session_id'];
  123. $sessionDrhInfo = SessionManager::getSessionFollowedByDrh(
  124. $currentUserId,
  125. $sessionId
  126. );
  127. if (!empty($sessionDrhInfo)) {
  128. $isDrhRelatedToSession = true;
  129. break;
  130. }
  131. foreach ($session['courses'] as $course) {
  132. $coachList = SessionManager::getCoachesByCourseSession(
  133. $sessionId,
  134. $course['real_id']
  135. );
  136. if (!empty($coachList) && in_array($currentUserId, $coachList)) {
  137. $isCourseCoach = true;
  138. break 2;
  139. }
  140. }
  141. }
  142. }
  143. $isCareTaker = $isDrhRelatedViaPost && $isDrhRelatedToSession;
  144. $isAllow = $isAdmin || $isCareTaker || $isDrhRelatedToSession || $isCourseCoach;
  145. $showPrivate = $isAdmin || $isCareTaker;
  146. }
  147. return [
  148. 'is_allow' => $isAllow,
  149. 'show_private' => $showPrivate,
  150. ];
  151. }
  152. /**
  153. * @param string $status
  154. * @param int $currentUserId
  155. * @param int $sessionId
  156. * @param int $start
  157. * @param int $limit
  158. *
  159. * @return array
  160. */
  161. public static function getUsers($status, $currentUserId, $sessionId, $start, $limit)
  162. {
  163. $sessions = [];
  164. $courses = [];
  165. $sessionsFull = [];
  166. switch ($status) {
  167. case COURSEMANAGER:
  168. /*$sessions = SessionManager::get_sessions_by_user($currentUserId);
  169. $sessions = array_column($sessions, 'session_id');*/
  170. $sessionsFull = SessionManager::getSessionsCoachedByUser($currentUserId);
  171. $sessions = array_column($sessionsFull, 'id');
  172. if (!empty($sessionId)) {
  173. $sessions = [$sessionId];
  174. }
  175. // Get session courses where I'm coach
  176. $courseList = SessionManager::getCoursesListByCourseCoach($currentUserId);
  177. $courses = [];
  178. /** @var SessionRelCourseRelUser $courseItem */
  179. foreach ($courseList as $courseItem) {
  180. $courses[] = $courseItem->getCourse()->getId();
  181. }
  182. break;
  183. case DRH:
  184. $sessionsFull = SessionManager::get_sessions_followed_by_drh($currentUserId);
  185. $sessions = array_column($sessionsFull, 'id');
  186. if (!empty($sessionId)) {
  187. $sessions = [$sessionId];
  188. }
  189. $courses = [];
  190. foreach ($sessions as $sessionId) {
  191. $sessionDrhInfo = SessionManager::getSessionFollowedByDrh(
  192. $currentUserId,
  193. $sessionId
  194. );
  195. if ($sessionDrhInfo && isset($sessionDrhInfo['course_list'])) {
  196. $courses = array_merge($courses, array_column($sessionDrhInfo['course_list'], 'id'));
  197. }
  198. }
  199. break;
  200. }
  201. $userList = SessionManager::getUsersByCourseAndSessionList(
  202. $sessions,
  203. $courses,
  204. $start,
  205. $limit
  206. );
  207. /*$userList = [];
  208. foreach ($sessions as $sessionId) {
  209. foreach ($courses as $courseId) {
  210. $courseInfo = ['real_id' => $courseId];
  211. $userFromSessionList = SessionManager::getUsersByCourseSession(
  212. $sessionId,
  213. $courseInfo
  214. );
  215. $userList = array_merge($userList, $userFromSessionList);
  216. }
  217. $userList = array_unique($userList);
  218. }*/
  219. return [
  220. 'users' => $userList,
  221. 'sessions' => $sessionsFull,
  222. ];
  223. }
  224. /**
  225. * @return int
  226. */
  227. public static function getPageSize()
  228. {
  229. return 20;
  230. }
  231. /**
  232. * @param int $userId
  233. */
  234. public function doWhenDeletingUser($userId)
  235. {
  236. $userId = (int) $userId;
  237. Database::query("DELETE FROM sfu_post WHERE user_id = $userId");
  238. Database::query("DELETE FROM sfu_post WHERE insert_user_id = $userId");
  239. }
  240. }