block_session.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * This file is part of session block plugin for dashboard,
  4. * it should be required inside dashboard controller for showing it into dashboard interface from plattform.
  5. *
  6. * @package chamilo.dashboard
  7. *
  8. * @author Christian Fasanando
  9. */
  10. /**
  11. * This class is used like controller for this session block plugin,
  12. * the class name must be registered inside path.info file
  13. * (e.g: controller = "BlockSession"), so dashboard controller will be instantiate it.
  14. *
  15. * @package chamilo.dashboard
  16. */
  17. class BlockSession extends Block
  18. {
  19. private $user_id;
  20. private $sessions;
  21. private $permission = [DRH, SESSIONADMIN];
  22. /**
  23. * Constructor.
  24. */
  25. public function __construct($user_id)
  26. {
  27. $this->user_id = $user_id;
  28. $this->path = 'block_session';
  29. if ($this->is_block_visible_for_user($user_id)) {
  30. $this->sessions = SessionManager::get_sessions_followed_by_drh($user_id);
  31. }
  32. }
  33. /**
  34. * This method check if a user is allowed to see the block inside dashboard interface.
  35. *
  36. * @param int User id
  37. *
  38. * @return bool Is block visible for user
  39. */
  40. public function is_block_visible_for_user($user_id)
  41. {
  42. $user_info = api_get_user_info($user_id);
  43. $user_status = $user_info['status'];
  44. $is_block_visible_for_user = false;
  45. if (UserManager::is_admin($user_id) || in_array($user_status, $this->permission)) {
  46. $is_block_visible_for_user = true;
  47. }
  48. return $is_block_visible_for_user;
  49. }
  50. /**
  51. * This method return content html containing
  52. * information about sessions and its position for showing it inside dashboard interface
  53. * it's important to use the name 'get_block' for beeing used from dashboard controller.
  54. *
  55. * @return array column and content html
  56. */
  57. public function get_block()
  58. {
  59. $column = 2;
  60. $data = [];
  61. $html = $this->getBlockCard(
  62. get_lang('Your sessions'),
  63. $this->getContent()
  64. );
  65. $data['column'] = $column;
  66. $data['content_html'] = $html;
  67. return $data;
  68. }
  69. /**
  70. * This method return a content html, it's used inside get_block method for showing it inside dashboard interface.
  71. *
  72. * @return string content html
  73. */
  74. public function getContent()
  75. {
  76. $content = '';
  77. $sessions = $this->sessions;
  78. if (count($sessions) > 0) {
  79. $sessions_table = '<table class="data_table" width:"95%">';
  80. $sessions_table .= '<tr>
  81. <th >'.get_lang('Title').'</th>
  82. <th >'.get_lang('Date').'</th>
  83. <th width="100px">'.get_lang('Number of courses per session').'</th>
  84. </tr>';
  85. $i = 1;
  86. foreach ($sessions as $session) {
  87. $session_id = intval($session['id']);
  88. $title = $session['name'];
  89. if (!empty($session['access_start_date'])) {
  90. $dateFrom = api_convert_and_format_date(
  91. $session['access_start_date'],
  92. DATE_FORMAT_SHORT,
  93. date_default_timezone_get()
  94. );
  95. $dateUntil = api_convert_and_format_date(
  96. $session['access_end_date'],
  97. DATE_FORMAT_SHORT,
  98. date_default_timezone_get()
  99. );
  100. $date = vsprintf(get_lang('From %s to %s'), [$dateFrom, $dateUntil]);
  101. } else {
  102. $date = ' - ';
  103. }
  104. $count_courses_in_session = count(Tracking::get_courses_list_from_session($session_id));
  105. if ($i % 2 == 0) {
  106. $class_tr = 'row_odd';
  107. } else {
  108. $class_tr = 'row_even';
  109. }
  110. $sessions_table .= '<tr class="'.$class_tr.'">
  111. <td>'.$title.'</td>
  112. <td align="center">'.$date.'</td>
  113. <td align="center">'.$count_courses_in_session.'</td>
  114. </tr>';
  115. $i++;
  116. }
  117. $sessions_table .= '</table>';
  118. $content .= $sessions_table;
  119. } else {
  120. $content .= get_lang('There is no available information about your sessions');
  121. }
  122. if (count($sessions) > 0) {
  123. $content .= '<div style="text-align:right;margin-top:10px;"><a href="'.api_get_path(WEB_CODE_PATH).'mySpace/session.php">'.get_lang('See more').'</a></div>';
  124. }
  125. return $content;
  126. }
  127. /**
  128. * Get number of sessions.
  129. *
  130. * @return int
  131. */
  132. public function get_number_of_sessions()
  133. {
  134. return count($this->sessions);
  135. }
  136. }