tutor_report.lib.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Helper library for weekly reports
  5. * @package chamilo.plugin.ticket
  6. */
  7. /**
  8. * @param $course_code
  9. * @return array|bool
  10. */
  11. function initializeReport($course_code)
  12. {
  13. $course_info = api_get_course_info($course_code);
  14. $table_reporte_semanas = Database::get_main_table('rp_reporte_semanas');
  15. $table_students_report = Database::get_main_table('rp_students_report');
  16. $table_semanas_curso = Database::get_main_table('rp_semanas_curso');
  17. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  18. $table_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  19. $table_post = Database::get_course_table(TABLE_FORUM_POST);
  20. $table_work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  21. $course_code = Database::escape_string($course_code);
  22. $res = Database::query("SELECT COUNT(*) as cant FROM $table_reporte_semanas WHERE course_code = '".$course_code."'");
  23. $sqlWeeks = "SELECT semanas FROM $table_semanas_curso WHERE course_code = '$course_code'";
  24. $resWeeks = Database::query($sqlWeeks);
  25. $weeks = Database::fetch_object($resWeeks);
  26. $obj = Database::fetch_object($res);
  27. $weeksCount = (!isset($_POST['weeksNumber'])) ? (($weeks->semanas == 0) ? 7 : $weeks->semanas) : $_POST['weeksNumber'];
  28. $weeksCount = Database::escape_string($weeksCount);
  29. Database::query("REPLACE INTO $table_semanas_curso (course_code , semanas) VALUES ('$course_code','$weeksCount')");
  30. if (intval($obj->cant) != $weeksCount) {
  31. if (intval($obj->cant) > $weeksCount) {
  32. $sql = "DELETE FROM $table_reporte_semanas
  33. WHERE week_id > $weeksCount AND course_code = '$course_code'";
  34. Database::query($sql);
  35. } else {
  36. for ($i = $obj->cant + 1; $i <= $weeksCount; $i++) {
  37. if (!Database::query("INSERT INTO $table_reporte_semanas (week_id, course_code, forum_id, work_id, quiz_id, pc_id)
  38. VALUES ($i, '$course_code', '0', '0', '0', '0' )")) {
  39. return false;
  40. }
  41. }
  42. }
  43. }
  44. $sql = "REPLACE INTO $table_students_report (user_id, week_report_id, work_ok , thread_ok , quiz_ok , pc_ok)
  45. SELECT cu.user_id, rs.id, 0, 0, 0, 0
  46. FROM $table_course_rel_user cu
  47. INNER JOIN $courseTable c
  48. ON (c.id = cu.c_id)
  49. LEFT JOIN $table_reporte_semanas rs ON c.code = rs.course_code
  50. WHERE cu.status = 5 AND rs.course_code = '$course_code'
  51. ORDER BY cu.user_id, rs.id";
  52. if (!Database::query($sql)) {
  53. return false;
  54. } else {
  55. $page = (!isset($_GET['page'])) ? 1 : $_GET['page'];
  56. Database::query("UPDATE $table_students_report sr SET sr.work_ok = 1
  57. WHERE CONCAT (sr.user_id,',',sr.week_report_id)
  58. IN (SELECT DISTINCT CONCAT(w.user_id,',',rs.id)
  59. FROM $table_work w JOIN $table_reporte_semanas rs ON w.parent_id = rs.work_id)");
  60. Database::query("UPDATE $table_students_report sr SET sr.thread_ok = 1
  61. WHERE CONCAT (sr.user_id,',',sr.week_report_id)
  62. IN (SELECT DISTINCT CONCAT(f.poster_id,',',rs.id)
  63. FROM $table_post f JOIN $table_reporte_semanas rs ON f.thread_id = rs.forum_id)");
  64. return showResults($course_info, $weeksCount, $page);
  65. }
  66. }
  67. /**
  68. * @param $courseInfo
  69. * @param $weeksCount
  70. * @param $page
  71. * @return array
  72. */
  73. function showResults($courseInfo, $weeksCount, $page)
  74. {
  75. $course_code = $courseInfo['code'];
  76. $page = intval($page);
  77. $weeksCount = intval($weeksCount);
  78. $tableWeeklyReport = Database::get_main_table('rp_reporte_semanas');
  79. $tableStudentsReport = Database::get_main_table('rp_students_report');
  80. //$table_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  81. $tableUser = Database::get_main_table(TABLE_MAIN_USER);
  82. $tableThread = Database::get_course_table(TABLE_FORUM_THREAD);
  83. $tableWork = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  84. $results = array();
  85. $tableExport = array();
  86. $sqlHeader = "SELECT rs.id as id,rs.week_id, w.title AS work_title, t.thread_title ,'EVALUATION' as eval_title ,'QUIZ' as pc_title
  87. FROM $tableWeeklyReport rs
  88. LEFT JOIN $tableThread t ON t.thread_id = rs.forum_id
  89. LEFT JOIN $tableWork w ON w.id = rs.work_id
  90. WHERE rs.course_code = '$course_code'
  91. ORDER BY rs.week_id";
  92. $resultHeader = Database::query($sqlHeader);
  93. $ids = array();
  94. $line = '<tr>
  95. <th ></th>';
  96. $lineHeaderExport = array(null, null);
  97. $lineHeaderExport2 = array(null, ull);
  98. while ($rowe = Database::fetch_assoc($resultHeader)) {
  99. $lineHeaderExport[] = utf8_decode('Work'.$rowe['week_id']);
  100. $lineHeaderExport[] = utf8_decode('Forum'.$rowe['week_id']);
  101. //$fila_export_encabezado[] = utf8_decode('Eval'.$rowe['week_id']);
  102. //$fila_export_encabezado[] = utf8_decode('PC'.$rowe['week_id']);
  103. $lineHeaderExport2[] = utf8_decode($rowe['work_title']);
  104. $lineHeaderExport2[] = utf8_decode($rowe['thread_title']);
  105. //$fila_export_encabezado2[] = utf8_decode($rowe['eval_title']);
  106. //$fila_export_encabezado2[] = utf8_decode($rowe['pc_title']);
  107. $fila_export = array('Work'.$rowe['week_id'], 'Forum'.$rowe['week_id'], 'Eval'.$rowe['week_id'], 'PC'.$rowe['week_id']);
  108. if ($rowe['week_id'] > (($page - 1) * 7) && $rowe['week_id'] <= (7 * $page)) {
  109. $ids[$rowe['week_id']] = $rowe['id'];
  110. $line .= '<th>
  111. <a href="#" onClick="showContent(' . "'tarea".$rowe['week_id']."'".');">Work'.$rowe['week_id'].'
  112. <div class="blackboard_hide" id="tarea' . $rowe['week_id'].'">'.$rowe['work_title'].'</div>
  113. </a></th>';
  114. $line .= '<th>
  115. <a href="#" onClick="showContent(' . "'foro".$rowe['week_id']."'".');">Forum'.$rowe['week_id'].'
  116. <div class="blackboard_hide" id="foro' . $rowe['week_id'].'">'.$rowe['thread_title'].'</div>
  117. </a>
  118. </th>';
  119. }
  120. }
  121. $tableExport[] = $lineHeaderExport;
  122. $tableExport[] = $lineHeaderExport2;
  123. $line .= '</tr>';
  124. $html = '<form action="tutor.php" name="semanas" id="semanas" method="POST">
  125. <div class="row">
  126. ' . get_lang('SelectWeeksSpan').'
  127. <select name="weeksNumber" id="weeksNumber" onChange="submit();">
  128. <option value="7" ' . (($weeksCount == 7) ? 'selected="selected"' : "").'>7 weeks</option>
  129. <option value="14" ' . (($weeksCount == 14) ? 'selected="selected"' : "").'>14 weeks</option>
  130. </select>';
  131. if ($weeksCount == 14) {
  132. $html .= '<span style="float:right;"><a href="tutor.php?page='.(($page == 1) ? 2 : 1).'">'.(($page == 1) ? "Siguiente" : "Anterior").'</a></span>';
  133. }
  134. $html .= '<span style="float:right;"><a href="'.api_get_self().'?action=export'.$get_parameter.$get_parameter2.'">'.Display::return_icon('export_excel.png', get_lang('Export'), '', '32').'</a></span>';
  135. $html .= '</form>';
  136. $html .= '<table class="reports">';
  137. $html .= '<tr>
  138. <th ></th>';
  139. for ($i = (7 * $page - 6); $i <= $page * 7; $i++) {
  140. $html .= '<th colspan="2">Week '.$i.'<a href="assign_tickets.php?id='.$ids[$i].'" class="ajax">'.Display::return_icon('edit.png', get_lang('Edit'), array('width' => '16', 'height' => '16'), 22).'</a></th>';
  141. }
  142. $html .= '</tr>';
  143. $html .= $line;
  144. $sql = "SELECT u.username , u.user_id , CONCAT(u.lastname,' ', u.firstname ) as fullname , rs.week_id , sr.work_ok ,sr.thread_ok , sr.quiz_ok , sr.pc_ok , rs.course_code
  145. FROM $tableStudentsReport sr
  146. JOIN $tableWeeklyReport rs ON sr.week_report_id = rs.id
  147. JOIN $tableUser u ON u.user_id = sr.user_id
  148. WHERE rs.course_code = '$course_code'
  149. ORDER BY u.lastname , u.username , rs.week_id
  150. ";
  151. $result = Database::query($sql);
  152. while ($row = Database::fetch_assoc($result)) {
  153. $resultadose[$row['username']][$row['week_id']] = $row;
  154. if ($row['week_id'] > (($page - 1) * 7) && $row['week_id'] <= (7 * $page)) {
  155. $results[$row['username']][$row['week_id']] = $row;
  156. if (count($results[$row['username']]) == 7) {
  157. $html .= showStudentResult($results[$row['username']], $page);
  158. }
  159. }
  160. if (count($resultadose[$row['username']]) == $weeksCount) {
  161. $tableExport[] = showStudentResultExport($resultadose[$row['username']], $weeksCount);
  162. }
  163. }
  164. $html .= '
  165. </table>';
  166. return array('show' => $html, 'export' => $tableExport);
  167. }
  168. /**
  169. * @param $datos
  170. * @param $pagina
  171. * @return string
  172. */
  173. function showStudentResult($datos, $pagina)
  174. {
  175. $inicio = (7 * $pagina - 6);
  176. $fila = '<tr>';
  177. $fila .= '<td><a href="'.api_get_path(WEB_CODE_PATH).'user/userInfo.php?'.api_get_cidreq().'&uInfo='.$datos[$inicio]['user_id'].'">'.$datos[$inicio]['username'].'</a></td>';
  178. foreach ($datos as $dato) {
  179. $fila .= '<td align="center">'.(($dato['work_ok'] == 1) ? Display::return_icon('check.png') : Display::return_icon('aspa.png')).'</td>';
  180. $fila .= '<td align="center">'.(($dato['thread_ok'] == 1) ? Display::return_icon('check.png') : Display::return_icon('aspa.png')).'</td>';
  181. }
  182. $fila .= '</tr>';
  183. return $fila;
  184. }
  185. /**
  186. * @param $data
  187. * @param $numero_semanas
  188. * @return array
  189. */
  190. function showStudentResultExport($data, $numero_semanas)
  191. {
  192. $fila = array();
  193. $fila[] = utf8_decode($data[1]['username']);
  194. $fila[] = utf8_decode($data[1]['fullname']);
  195. foreach ($data as $line) {
  196. $fila[] = ($line['work_ok'] == 1) ? get_lang('Yes') : get_lang('No');
  197. $fila[] = ($line['thread_ok'] == 1) ? get_lang('Yes') : get_lang('No');
  198. }
  199. return $fila;
  200. }