my_students.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Doctrine\Common\Collections\Criteria;
  4. use Doctrine\ORM\Tools\Pagination\Paginator;
  5. require_once __DIR__.'/../../main/inc/global.inc.php';
  6. api_block_anonymous_users();
  7. $plugin = StudentFollowUpPlugin::create();
  8. $currentUserId = api_get_user_id();
  9. $currentPage = isset($_REQUEST['page']) ? (int) $_REQUEST['page'] : 1;
  10. $keyword = isset($_REQUEST['keyword']) ? Security::remove_XSS($_REQUEST['keyword']) : '';
  11. $sessionId = isset($_REQUEST['session_id']) ? (int) $_REQUEST['session_id'] : 0;
  12. $selectedTag = isset($_REQUEST['tag']) ? Security::remove_XSS($_REQUEST['tag']) : '';
  13. $totalItems = 0;
  14. $items = [];
  15. $tags = [];
  16. $showPrivate = false;
  17. $pageSize = StudentFollowUpPlugin::getPageSize();
  18. $firstResults = $pageSize * ($currentPage - 1);
  19. $pagesCount = 0;
  20. $isAdmin = api_is_platform_admin();
  21. $userList = [];
  22. if (!$isAdmin) {
  23. $status = COURSEMANAGER;
  24. if (api_is_drh()) {
  25. $status = DRH;
  26. }
  27. $data = StudentFollowUpPlugin::getUsers(
  28. $status,
  29. $currentUserId,
  30. $sessionId,
  31. $firstResults,
  32. $pageSize
  33. );
  34. $userList = $data['users'];
  35. $fullSessionList = $data['sessions'];
  36. } else {
  37. $fullSessionList = SessionManager::getSessionsCoachedByUser($currentUserId);
  38. }
  39. if (!empty($sessionId)) {
  40. $userList = SessionManager::get_users_by_session($sessionId);
  41. $userList = array_column($userList, 'user_id');
  42. }
  43. $tagList = [];
  44. if (!empty($userList) || $isAdmin) {
  45. $em = Database::getManager();
  46. $qb = $em->createQueryBuilder();
  47. $criteria = Criteria::create();
  48. if (!$isAdmin) {
  49. $criteria->where(Criteria::expr()->in('user', $userList));
  50. }
  51. if (!empty($sessionId)) {
  52. $criteria->where(Criteria::expr()->in('user', $userList));
  53. }
  54. if ($showPrivate === false) {
  55. $criteria->andWhere(Criteria::expr()->eq('private', false));
  56. }
  57. $qb
  58. ->select('p')
  59. ->distinct()
  60. ->from('ChamiloPluginBundle:StudentFollowUp\CarePost', 'p')
  61. ->join('p.user', 'u')
  62. ->addCriteria($criteria)
  63. ->setFirstResult($firstResults)
  64. ->setMaxResults($pageSize)
  65. ->groupBy('p.user')
  66. ->orderBy('p.createdAt', 'desc')
  67. ;
  68. if (!empty($keyword)) {
  69. $keywordToArray = explode(' ', $keyword);
  70. if (is_array($keywordToArray)) {
  71. foreach ($keywordToArray as $key) {
  72. $key = trim($key);
  73. if (empty($key)) {
  74. continue;
  75. }
  76. $qb
  77. ->andWhere('u.firstname LIKE :keyword OR u.lastname LIKE :keyword OR u.username LIKE :keyword')
  78. ->setParameter('keyword', "%$key%")
  79. ;
  80. }
  81. } else {
  82. $qb
  83. ->andWhere('u.firstname LIKE :keyword OR u.lastname LIKE :keyword OR u.username LIKE :keyword')
  84. ->setParameter('keyword', "%$keyword%")
  85. ;
  86. }
  87. }
  88. $queryBuilderOriginal = clone $qb;
  89. if (!empty($selectedTag)) {
  90. $qb->andWhere('p.tags LIKE :tags ');
  91. $qb->setParameter('tags', "%$selectedTag%");
  92. }
  93. $query = $qb->getQuery();
  94. $items = new Paginator($query);
  95. $queryBuilderOriginal->select('p.tags')
  96. ->distinct(false)
  97. ->setFirstResult(null)
  98. ->setMaxResults(null)
  99. ->groupBy('p.id')
  100. ;
  101. $tags = $queryBuilderOriginal->getQuery()->getResult();
  102. //var_dump($queryBuilderOriginal->getQuery()->getSQL());
  103. $tagList = [];
  104. foreach ($tags as $tag) {
  105. $itemTags = $tag['tags'];
  106. foreach ($itemTags as $itemTag) {
  107. if (in_array($itemTag, array_keys($tagList))) {
  108. $tagList[$itemTag]++;
  109. } else {
  110. $tagList[$itemTag] = 1;
  111. }
  112. }
  113. }
  114. $totalItems = $items->count();
  115. $pagesCount = ceil($totalItems / $pageSize);
  116. }
  117. $pagination = '';
  118. $url = api_get_self().'?session_id='.$sessionId.'&tag='.$selectedTag.'&keyword='.$keyword.'&';
  119. if ($totalItems > 1 && $pagesCount > 1) {
  120. $pagination .= '<ul class="pagination">';
  121. for ($i = 0; $i < $pagesCount; $i++) {
  122. $newPage = $i + 1;
  123. if ($currentPage == $newPage) {
  124. $pagination .= '<li class="active"><a href="'.$url.'page='.$newPage.'">'.$newPage.'</a></li>';
  125. } else {
  126. $pagination .= '<li><a href="'.$url.'page='.$newPage.'">'.$newPage.'</a></li>';
  127. }
  128. }
  129. $pagination .= '</ul>';
  130. }
  131. // Create a search-box
  132. $form = new FormValidator('search_simple', 'get', null, null, null, FormValidator::LAYOUT_HORIZONTAL);
  133. $form->addText(
  134. 'keyword',
  135. get_lang('Search'),
  136. false,
  137. [
  138. 'aria-label' => get_lang('Search users'),
  139. ]
  140. );
  141. if (!empty($fullSessionList)) {
  142. $options = array_column($fullSessionList, 'name', 'id');
  143. $options[0] = get_lang('Please select an option');
  144. ksort($options);
  145. $form->addSelect('session_id', get_lang('Session'), $options);
  146. }
  147. if (!empty($tagList)) {
  148. $tagOptions = [];
  149. arsort($tagList);
  150. foreach ($tagList as $tag => $counter) {
  151. $tagOptions[$tag] = $tag.' ('.$counter.')';
  152. }
  153. $form->addSelect('tag', get_lang('Tags'), $tagOptions, ['placeholder' => get_lang('Please select an option')]);
  154. }
  155. $form->addButtonSearch(get_lang('Search'));
  156. $defaults = [
  157. 'session_id' => $sessionId,
  158. 'keyword' => $keyword,
  159. 'tag' => $selectedTag,
  160. ];
  161. $form->setDefaults($defaults);
  162. $tpl = new Template($plugin->get_lang('plugin_title'));
  163. $tpl->assign('users', $items);
  164. $tpl->assign('form', $form->returnForm());
  165. $url = api_get_path(WEB_PLUGIN_PATH).'studentfollowup/posts.php?';
  166. $tpl->assign('post_url', $url);
  167. $url = api_get_path(WEB_CODE_PATH).'mySpace/myStudents.php?';
  168. $tpl->assign('my_students_url', $url);
  169. $tpl->assign('pagination', $pagination);
  170. $tpl->assign('care_title', $plugin->get_lang('Student care detail view'));
  171. $content = $tpl->fetch('/'.$plugin->get_name().'/view/my_students.html.twig');
  172. $tpl->assign('content', $content);
  173. $tpl->display_one_col_template();