StudentFollowUpPlugin.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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);
  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. //$isCourseCoach = api_is_coach($sessionId, $course['real_id']);
  133. $coachList = SessionManager::getCoachesByCourseSession(
  134. $sessionId,
  135. $course['real_id']
  136. );
  137. if (!empty($coachList) && in_array($currentUserId, $coachList)) {
  138. $isCourseCoach = true;
  139. break 2;
  140. }
  141. }
  142. }
  143. }
  144. $isCareTaker = $isDrhRelatedViaPost && $isDrhRelatedToSession;
  145. $isAllow = $isAdmin || $isCareTaker || $isDrhRelatedToSession || $isCourseCoach;
  146. $showPrivate = $isAdmin || $isCareTaker;
  147. }
  148. return [
  149. 'is_allow' => $isAllow,
  150. 'show_private' => $showPrivate,
  151. ];
  152. }
  153. /**
  154. * @param string $status
  155. * @param int $currentUserId
  156. * @param int $start
  157. * @param int $limit
  158. *
  159. * @return array
  160. */
  161. public static function getUsers($status, $currentUserId, $start, $limit)
  162. {
  163. switch ($status) {
  164. case COURSEMANAGER:
  165. $sessions = SessionManager::get_sessions_by_user($currentUserId);
  166. $sessions = array_column($sessions, 'session_id');
  167. // Get session courses where I'm coach
  168. $courseList = SessionManager::getCoursesListByCourseCoach($currentUserId);
  169. $courses = [];
  170. /** @var SessionRelCourseRelUser $courseItem */
  171. foreach ($courseList as $courseItem) {
  172. $courses[] = $courseItem->getCourse()->getId();
  173. }
  174. break;
  175. case DRH:
  176. $sessions = SessionManager::get_sessions_followed_by_drh($currentUserId);
  177. $sessions = array_column($sessions, 'id');
  178. $courses = [];
  179. foreach ($sessions as $sessionId) {
  180. $sessionDrhInfo = SessionManager::getSessionFollowedByDrh(
  181. $currentUserId,
  182. $sessionId
  183. );
  184. if ($sessionDrhInfo && isset($sessionDrhInfo['course_list'])) {
  185. $courses = array_merge($courses, array_column($sessionDrhInfo['course_list'], 'id'));
  186. }
  187. }
  188. break;
  189. }
  190. $userList = SessionManager::getUsersByCourseAndSessionList(
  191. $sessions,
  192. $courses,
  193. $start,
  194. $limit
  195. );
  196. /*$userList = [];
  197. foreach ($sessions as $sessionId) {
  198. foreach ($courses as $courseId) {
  199. $courseInfo = ['real_id' => $courseId];
  200. $userFromSessionList = SessionManager::getUsersByCourseSession(
  201. $sessionId,
  202. $courseInfo
  203. );
  204. $userList = array_merge($userList, $userFromSessionList);
  205. }
  206. $userList = array_unique($userList);
  207. }*/
  208. return $userList;
  209. }
  210. /**
  211. * @return int
  212. */
  213. public static function getPageSize()
  214. {
  215. return 20;
  216. }
  217. /**
  218. * @param int $userId
  219. */
  220. public function doWhenDeletingUser($userId)
  221. {
  222. $userId = (int) $userId;
  223. Database::query("DELETE FROM sfu_post WHERE user_id = $userId");
  224. Database::query("DELETE FROM sfu_post WHERE insert_user_id = $userId");
  225. }
  226. }