lp_tracking.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
  4. use Chamilo\CourseBundle\Entity\CLpItemView;
  5. /**
  6. * Learning paths reporting.
  7. *
  8. * @package chamilo.reporting
  9. */
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. $cidReset = true;
  12. $from_myspace = false;
  13. $from_link = '';
  14. if (isset($_GET['from']) && $_GET['from'] == 'myspace') {
  15. $from_link = '&from=myspace';
  16. $this_section = SECTION_TRACKING;
  17. } else {
  18. $this_section = SECTION_COURSES;
  19. }
  20. $session_id = isset($_REQUEST['id_session']) ? (int) $_REQUEST['id_session'] : api_get_session_id();
  21. $export_csv = isset($_GET['export']) && $_GET['export'] == 'csv';
  22. $user_id = isset($_GET['student_id']) ? (int) $_GET['student_id'] : api_get_user_id();
  23. $courseCode = isset($_GET['course']) ? Security::remove_XSS($_GET['course']) : api_get_course_id();
  24. $origin = api_get_origin();
  25. $lp_id = (int) $_GET['lp_id'];
  26. $csv_content = [];
  27. $courseInfo = api_get_course_info($courseCode);
  28. if (empty($courseInfo) || empty($lp_id)) {
  29. api_not_allowed(api_get_origin() !== 'learnpath');
  30. }
  31. $userInfo = api_get_user_info($user_id);
  32. $name = $userInfo['complete_name'];
  33. $isBoss = UserManager::userIsBossOfStudent(api_get_user_id(), $user_id);
  34. if (!api_is_platform_admin(true) &&
  35. !CourseManager::is_course_teacher(api_get_user_id(), $courseCode) &&
  36. !$isBoss &&
  37. !Tracking::is_allowed_to_coach_student(api_get_user_id(), $user_id) &&
  38. !api_is_drh() &&
  39. !api_is_course_tutor()
  40. ) {
  41. api_not_allowed(api_get_origin() !== 'learnpath');
  42. }
  43. if ($origin === 'user_course') {
  44. $interbreadcrumb[] = [
  45. 'url' => api_get_path(WEB_COURSE_PATH).$courseInfo['directory'],
  46. 'name' => $courseInfo['name'],
  47. ];
  48. $interbreadcrumb[] = [
  49. 'url' => "../user/user.php?cidReq=$courseCode",
  50. 'name' => get_lang('Users'),
  51. ];
  52. } elseif ($origin === 'tracking_course') {
  53. $interbreadcrumb[] = [
  54. 'url' => "../tracking/courseLog.php?cidReq=$courseCode&id_session=$session_id",
  55. 'name' => get_lang('Tracking'),
  56. ];
  57. } else {
  58. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('MySpace')];
  59. $interbreadcrumb[] = ['url' => 'student.php', 'name' => get_lang('MyStudents')];
  60. $interbreadcrumb[] = ['url' => "myStudents.php?student=$user_id", 'name' => get_lang('StudentDetails')];
  61. $nameTools = get_lang('DetailsStudentInCourse');
  62. }
  63. $interbreadcrumb[] = [
  64. 'url' => "myStudents.php?student=$user_id&course=$courseCode&details=true&origin=$origin",
  65. 'name' => get_lang('DetailsStudentInCourse'),
  66. ];
  67. $nameTools = get_lang('LearningPathDetails');
  68. $sql = 'SELECT name FROM '.Database::get_course_table(TABLE_LP_MAIN).'
  69. WHERE c_id = '.$courseInfo['real_id'].' AND id='.$lp_id;
  70. $rs = Database::query($sql);
  71. $lp_title = Database::result($rs, 0, 0);
  72. $origin = 'tracking';
  73. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
  74. switch ($action) {
  75. case 'export_stats':
  76. if (!api_is_allowed_to_edit()) {
  77. api_not_allowed();
  78. }
  79. $tpl = new Template(null, false, false);
  80. $itemId = isset($_REQUEST['extend_id']) ? $_REQUEST['extend_id'] : 0;
  81. $itemViewId = isset($_REQUEST['extend_attempt_id']) ? $_REQUEST['extend_attempt_id'] : 0;
  82. $em = Database::getManager();
  83. $repo = $em->getRepository('ChamiloCourseBundle:CLpItemView');
  84. /** @var CLpItemView $itemView */
  85. $itemView = $repo->find($itemViewId);
  86. if (!$itemView) {
  87. api_not_allowed();
  88. }
  89. $view = $em->getRepository('ChamiloCourseBundle:CLpView')->find($itemView->getLpViewId());
  90. $lp = $em->getRepository('ChamiloCourseBundle:CLp')->find($view->getLpId());
  91. $duration = learnpathItem::getScormTimeFromParameter('js', $itemView->getTotalTime());
  92. $endTime = $itemView->getStartTime() + $itemView->getTotalTime();
  93. $list1 = learnpath::get_iv_interactions_array($itemViewId, $courseInfo['real_id']);
  94. $counter = 0;
  95. $table = new HTML_Table();
  96. $total = 0;
  97. $numberChoices = 0;
  98. $questionCounter = 0;
  99. $studentName = '';
  100. $questions = [];
  101. $categories = [];
  102. foreach ($list1 as $id => $interaction) {
  103. $counter++;
  104. if ($counter === 1) {
  105. continue;
  106. } elseif ($counter === 2) {
  107. $studentName = $interaction['student_response_formatted'];
  108. } else {
  109. $data = $interaction['student_response_formatted'];
  110. switch ($interaction['type']) {
  111. case 'fill-in':
  112. $questionCounter++;
  113. $questions[$questionCounter]['question'] = $data;
  114. break;
  115. case 'choice':
  116. $questions[$questionCounter]['options'][] = $interaction;
  117. $numberChoices++;
  118. break;
  119. }
  120. }
  121. }
  122. $counter = 1;
  123. $table = new HTML_Table(['class' => 'table data_table']);
  124. $row = 0;
  125. $scoreDisplay = new ScoreDisplay();
  126. $globalTotal = 0;
  127. $globalTotalCount = 0;
  128. foreach ($questions as $data) {
  129. // Question title
  130. $table->setCellContents($row, 0, $data['question']);
  131. $table->setCellAttributes($row, 0, ['colspan' => '3', 'style' => 'text-align:center; font-weight:bold']);
  132. $choiceCounter = 1;
  133. $row++;
  134. $total = 0;
  135. // Question options
  136. foreach ($data['options'] as $option) {
  137. if ($option['result'] === 'correct') {
  138. $total++;
  139. $globalTotal++;
  140. }
  141. $table->setCellContents($row, 0, 'Q'.$choiceCounter);
  142. $table->setCellContents($row, 1, $option['student_response_formatted']);
  143. $result = Display::return_icon('icon_check.png', null, [], ICON_SIZE_SMALL);
  144. if ($option['result'] === 'wrong') {
  145. $result = Display::return_icon('icon_error.png', null, [], ICON_SIZE_SMALL);
  146. }
  147. $table->setCellContents($row, 2, $result);
  148. $choiceCounter++;
  149. $row++;
  150. }
  151. // Question total
  152. $table->setCellContents($row, 0, get_lang('Total'));
  153. $table->setCellContents($row, 1, $data['question']);
  154. $totalOptions = count($data['options']);
  155. $arrayScore = [0 => $total, 1 => $totalOptions];
  156. $scoreToString = $scoreDisplay->display_score($arrayScore);
  157. $table->setCellContents($row, 2, $scoreToString);
  158. $table->setCellAttributes($row, 0, ['style' => 'font-weight:bold']);
  159. $table->setCellAttributes($row, 1, ['style' => 'font-weight:bold']);
  160. $table->setCellAttributes($row, 2, ['style' => 'font-weight:bold']);
  161. $categories[] = [
  162. 'name' => $data['question'],
  163. 'score' => $scoreDisplay->display_score($arrayScore, SCORE_DIV),
  164. 'score_numeric' => $scoreDisplay->display_score($arrayScore, SCORE_NUMERIC),
  165. 'score_percentage' => $scoreDisplay->display_score($arrayScore, SCORE_PERCENT),
  166. ];
  167. $tpl->assign('categories', $categories);
  168. $globalTotalCount += $totalOptions;
  169. $row++;
  170. }
  171. $globalScoreTotal = [0 => $globalTotal, 1 => $globalTotalCount];
  172. $score = $scoreDisplay->display_score($globalScoreTotal);
  173. $generalScore[] = [
  174. 'score' => $scoreDisplay->display_score($globalScoreTotal, SCORE_DIV),
  175. 'score_numeric' => $scoreDisplay->display_score($globalScoreTotal, SCORE_NUMERIC),
  176. 'score_percentage' => $scoreDisplay->display_score($globalScoreTotal, SCORE_PERCENT),
  177. ];
  178. $tpl->assign('general_score', $generalScore);
  179. $tpl->assign('global_total', $score);
  180. $tableToString = $table->toHtml();
  181. $duration = learnpathItem::getScormTimeFromParameter('js', $itemView->getTotalTime());
  182. $dataLpInfo = [
  183. 'name' => $lp->getName(),
  184. 'attempt' => $itemView->getViewCount(),
  185. 'score' => $score,
  186. 'duration' => $duration,
  187. 'start_time' => api_get_local_time($itemView->getStartTime()),
  188. 'start_date' => api_get_local_time($itemView->getStartTime(), null, null, null, false),
  189. 'end_time' => api_get_local_time($endTime),
  190. 'candidate' => $studentName,
  191. ];
  192. $tpl->assign('data', $dataLpInfo);
  193. $contentText = $tpl->fetch($tpl->get_template('my_space/pdf_tracking_lp.tpl'));
  194. $content = $contentText.'<pagebreak>'.$tableToString;
  195. $pdf = new PDF('A4', 'P', ['margin_footer' => 4, 'top' => 40, 'bottom' => 25]);
  196. $table = new HTML_Table(['class' => 'table', 'style' => 'display: block; margin-bottom: 50px;']);
  197. $logo = ChamiloApi::getPlatformLogo(
  198. $theme,
  199. [
  200. 'title' => '',
  201. 'style' => 'max-width:180px, margin-bottom: 100px;',
  202. 'id' => 'header-logo',
  203. ]
  204. );
  205. $table->setCellContents(0, 0, $logo);
  206. $secondLogo = api_get_path(SYS_PATH).'custompages/url-images/'.api_get_current_access_url_id().'_url_image_2.png';
  207. $logo2 = Display::img($secondLogo, null, ['style' => 'height:70px;']);
  208. $table->setCellContents(0, 1, $logo2);
  209. $table->setCellAttributes(0, 1, ['style' => 'display:block;float:right;text-align:right']);
  210. $pdf->set_custom_header($table->toHtml());
  211. $background = api_get_path(SYS_PATH).'custompages/url-images/'.api_get_current_access_url_id().'_pdf_background.png';
  212. $content =
  213. '<html>
  214. <body style="background-image-resize: 5; background-position: top left; background-image: url('.$background.');">'
  215. .$content.'</body></html>';
  216. $pdf->content_to_pdf(
  217. $content,
  218. null,
  219. $courseInfo['code'].'_'.$lp->getName().'_'.api_get_local_time(),
  220. $courseInfo['code'],
  221. 'D',
  222. false,
  223. null,
  224. false,
  225. true
  226. );
  227. break;
  228. }
  229. $output = require_once api_get_path(SYS_CODE_PATH).'lp/lp_stats.php';
  230. $actions = [];
  231. $actions[] = Display::url(
  232. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM),
  233. 'javascript:history.back();'
  234. );
  235. $actions[] = Display::url(
  236. Display::return_icon('printer.png', get_lang('Print'), '', ICON_SIZE_MEDIUM),
  237. 'javascript:window.print();'
  238. );
  239. $actions[] = Display::url(
  240. Display::return_icon('export_csv.png', get_lang('ExportAsCSV'), '', ICON_SIZE_MEDIUM),
  241. api_get_self().'?export=csv&'.Security::remove_XSS($_SERVER['QUERY_STRING'])
  242. );
  243. Display::display_header($nameTools);
  244. echo Display::toolbarAction(
  245. 'actions',
  246. [implode(PHP_EOL, $actions)]
  247. );
  248. $table_title = $session_id
  249. ? Display::return_icon('session.png', get_lang('Session')).PHP_EOL.api_get_session_name($session_id).PHP_EOL
  250. : PHP_EOL;
  251. $table_title .= Display::return_icon('course.png', get_lang('Course')).PHP_EOL.$courseInfo['name'].PHP_EOL
  252. .Display::return_icon('user.png', get_lang('User')).' '.$name;
  253. echo Display::page_header($table_title);
  254. echo Display::page_subheader(
  255. Display::return_icon('learnpath.png', get_lang('ToolLearnpath')).PHP_EOL.$lp_title
  256. );
  257. echo $output;
  258. Display::display_footer();