StudentFollowUpPlugin.php 8.3 KB

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