coaches.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Coaches reporting.
  5. */
  6. ob_start();
  7. $cidReset = true;
  8. require_once __DIR__.'/../inc/global.inc.php';
  9. $this_section = SECTION_TRACKING;
  10. $nameTools = get_lang('Coaches');
  11. api_block_anonymous_users();
  12. $interbreadcrumb[] = ["url" => "index.php", "name" => get_lang('Reporting')];
  13. if (isset($_GET["id_student"])) {
  14. $interbreadcrumb[] = ["url" => "student.php", "name" => get_lang('Learners')];
  15. }
  16. Display :: display_header($nameTools);
  17. api_display_tool_title($nameTools);
  18. // Database Table Definitions
  19. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  20. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  21. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  22. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  23. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  24. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  25. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  26. $tbl_track_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  27. /**
  28. * MAIN PART.
  29. */
  30. if (isset($_POST['export'])) {
  31. $order_clause = api_is_western_name_order(PERSON_NAME_DATA_EXPORT) ? ' ORDER BY firstname, lastname' : ' ORDER BY lastname, firstname';
  32. } else {
  33. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname' : ' ORDER BY lastname, firstname';
  34. }
  35. if (isset($_GET["id_student"])) {
  36. $id_student = intval($_GET["id_student"]);
  37. $sql_coachs = "SELECT DISTINCT srcru.user_id as id_coach
  38. FROM $tbl_session_rel_course_rel_user as srcru
  39. WHERE srcru.user_id='$id_student' AND srcru.status=2";
  40. } else {
  41. if (api_is_platform_admin()) {
  42. $sql_coachs = "SELECT DISTINCT
  43. srcru.user_id as id_coach, user_id, lastname, firstname
  44. FROM $tbl_user, $tbl_session_rel_course_rel_user srcru
  45. WHERE
  46. srcru.user_id=user_id AND
  47. srcru.status=2 ".$order_clause;
  48. } else {
  49. $sql_coachs = "SELECT DISTINCT user_id as id_coach, user.user_id, lastname, firstname
  50. FROM
  51. $tbl_user as user,
  52. $tbl_session_rel_course_user as srcu,
  53. $tbl_course_user as course_rel_user,
  54. $tbl_course as c
  55. WHERE
  56. c.id = course_rel_user.c_id AND
  57. c.id = srcu.c_id AND
  58. course_rel_user.status='1' AND
  59. course_rel_user.user_id='".api_get_user_id()."' AND
  60. srcu.user_id = user.user_id AND
  61. srcu.status = 2
  62. ".$order_clause;
  63. }
  64. }
  65. $result_coachs = Database::query($sql_coachs);
  66. if (api_is_western_name_order()) {
  67. echo '<table class="data_table">
  68. <tr>
  69. <th>'.get_lang('First name').'</th>
  70. <th>'.get_lang('Last name').'</th>
  71. <th>'.get_lang('Connection time').'</th>
  72. <th>'.get_lang('Courses').'</th>
  73. <th>'.get_lang('Learners').'</th>
  74. </tr>';
  75. } else {
  76. echo '<table class="data_table">
  77. <tr>
  78. <th>'.get_lang('Last name').'</th>
  79. <th>'.get_lang('First name').'</th>
  80. <th>'.get_lang('Connection time').'</th>
  81. <th>'.get_lang('Courses').'</th>
  82. <th>'.get_lang('Learners').'</th>
  83. </tr>';
  84. }
  85. if (api_is_western_name_order(PERSON_NAME_DATA_EXPORT)) {
  86. $header[] = get_lang('First name');
  87. $header[] = get_lang('Last name');
  88. } else {
  89. $header[] = get_lang('Last name');
  90. $header[] = get_lang('First name');
  91. }
  92. $header[] = get_lang('Connection time');
  93. if (Database::num_rows($result_coachs) > 0) {
  94. while ($coachs = Database::fetch_array($result_coachs)) {
  95. $id_coach = $coachs["id_coach"];
  96. if (isset($_GET["id_student"])) {
  97. $sql_infos_coach = "SELECT lastname, firstname
  98. FROM $tbl_user
  99. WHERE user_id='$id_coach'";
  100. $result_coachs_infos = Database::query($sql_infos_coach);
  101. $lastname = Database::result($result_coachs_infos, 0, "lastname");
  102. $firstname = Database::result($result_coachs_infos, 0, "firstname");
  103. } else {
  104. $lastname = $coachs["lastname"];
  105. $firstname = $coachs["firstname"];
  106. }
  107. $sql_connection_time = "SELECT login_date, logout_date
  108. FROM $tbl_track_login
  109. WHERE login_user_id ='$id_coach' AND logout_date <> 'null'";
  110. $result_connection_time = Database::query($sql_connection_time);
  111. $nb_seconds = 0;
  112. while ($connections = Database::fetch_array($result_connection_time)) {
  113. $login_date = $connections["login_date"];
  114. $logout_date = $connections["logout_date"];
  115. $timestamp_login_date = strtotime($login_date);
  116. $timestamp_logout_date = strtotime($logout_date);
  117. $nb_seconds += ($timestamp_logout_date - $timestamp_login_date);
  118. }
  119. if ($nb_seconds == 0) {
  120. $s_connection_time = '';
  121. } else {
  122. $s_connection_time = api_time_to_hms($nb_seconds);
  123. }
  124. if ($i % 2 == 0) {
  125. $css_class = "row_odd";
  126. if ($i % 20 == 0 && $i != 0) {
  127. if (api_is_western_name_order()) {
  128. echo '<tr>
  129. <th>'.get_lang('First name').'</th>
  130. <th>'.get_lang('Last name').'</th>
  131. <th>'.get_lang('Connection time').'</th>
  132. <th>'.get_lang('Courses').'</th>
  133. <th>'.get_lang('Learners').'</th>
  134. </tr>';
  135. } else {
  136. echo '<tr>
  137. <th>'.get_lang('Last name').'</th>
  138. <th>'.get_lang('First name').'</th>
  139. <th>'.get_lang('Connection time').'</th>
  140. <th>'.get_lang('Courses').'</th>
  141. <th>'.get_lang('Learners').'</th>
  142. </tr>';
  143. }
  144. }
  145. } else {
  146. $css_class = "row_even";
  147. }
  148. $i++;
  149. if (api_is_western_name_order()) {
  150. echo '<tr class="'.$css_class.'">
  151. <td>'.$firstname.'</td><td>'.$lastname.'</td><td>'.$s_connection_time.'</td>
  152. <td>
  153. <a href="course.php?type=coach&user_id='.$id_coach.'">
  154. '.Display::return_icon('2rightarrow.png', get_lang('Details')).'
  155. </a>
  156. </td>
  157. <td>
  158. <a href="student.php?type=coach&user_id='.$id_coach.'">
  159. '.Display::return_icon('2rightarrow.png', get_lang('Details')).'
  160. </a>
  161. </td>
  162. </tr>';
  163. } else {
  164. echo '<tr class="'.$css_class.'">
  165. <td>'.$lastname.'</td><td>'.$firstname.'</td>
  166. <td>'.$s_connection_time.'</td>
  167. <td>
  168. <a href="course.php?type=coach&user_id='.$id_coach.'">
  169. '.Display::return_icon('2rightarrow.png', get_lang('Details')).'</a></td>
  170. <td>
  171. <a href="student.php?type=coach&user_id='.$id_coach.'">
  172. '.Display::return_icon('2rightarrow.png', get_lang('Details')).'</a>
  173. </td>
  174. </tr>';
  175. }
  176. if (api_is_western_name_order(PERSON_NAME_DATA_EXPORT)) {
  177. $data[$id_coach]["firstname"] = $firstname;
  178. $data[$id_coach]["lastname"] = $lastname;
  179. } else {
  180. $data[$id_coach]["lastname"] = $lastname;
  181. $data[$id_coach]["firstname"] = $firstname;
  182. }
  183. $data[$id_coach]["connection_time"] = $s_connection_time;
  184. }
  185. } else {
  186. // No results
  187. echo '<tr><td colspan="5">'.get_lang("There is no result yet").'</td></tr>';
  188. }
  189. echo '</table>';
  190. if (isset($_POST['export'])) {
  191. export_csv($header, $data, 'coaches.csv');
  192. }
  193. echo "<br /><br />";
  194. echo "
  195. <br /><br />
  196. <form method='post' action='coaches.php'>
  197. <button type='submit' class='save' name='export' value='".get_lang('Excel export')."'>
  198. ".get_lang('Excel export')."
  199. </button>
  200. <form>
  201. ";
  202. Display::display_footer();