lp_tracking.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 (!$isBoss &&
  35. !api_is_platform_admin(true) &&
  36. !api_is_drh() &&
  37. !api_is_course_tutor() &&
  38. !CourseManager::is_course_teacher(api_get_user_id(), $courseCode) &&
  39. !Tracking::is_allowed_to_coach_student(api_get_user_id(), $user_id)
  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('Reporting'),
  56. ];
  57. } else {
  58. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Reporting')];
  59. $interbreadcrumb[] = ['url' => 'student.php', 'name' => get_lang('My learners')];
  60. $interbreadcrumb[] = ['url' => "myStudents.php?student=$user_id", 'name' => get_lang('Learner details')];
  61. $nameTools = get_lang('Learner details in course');
  62. }
  63. $interbreadcrumb[] = [
  64. 'url' => "myStudents.php?student=$user_id&course=$courseCode&details=true&origin=$origin",
  65. 'name' => get_lang('Learner details in course'),
  66. ];
  67. $nameTools = get_lang('Learnpath details');
  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(false, false, true)) {
  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. case 'matching':
  120. $list = explode(',', $data);
  121. if (!empty($list)) {
  122. foreach ($list as &$item) {
  123. $item = cut($item, 30);
  124. }
  125. $interaction['student_response_formatted'] = implode('<br />', $list);
  126. }
  127. $questions[$questionCounter]['options'][] = $interaction;
  128. $numberChoices++;
  129. break;
  130. }
  131. }
  132. }
  133. $counter = 1;
  134. $table = new HTML_Table(['class' => 'table data_table']);
  135. $row = 0;
  136. $scoreDisplay = new ScoreDisplay();
  137. $globalTotal = 0;
  138. $globalTotalCount = 0;
  139. foreach ($questions as $data) {
  140. // Question title
  141. $table->setCellContents($row, 0, $data['question']);
  142. $table->setCellAttributes($row, 0, ['colspan' => '3', 'style' => 'text-align:center; font-weight:bold']);
  143. $choiceCounter = 1;
  144. $row++;
  145. $total = 0;
  146. // Question options
  147. foreach ($data['options'] as $option) {
  148. if ($option['result'] === 'correct') {
  149. $total++;
  150. $globalTotal++;
  151. }
  152. $table->setCellContents($row, 0, 'Q'.$choiceCounter);
  153. $table->setCellContents($row, 1, $option['student_response_formatted']);
  154. $result = Display::return_icon('icon_check.png', null, [], ICON_SIZE_SMALL);
  155. if ($option['result'] === 'wrong') {
  156. $result = Display::return_icon('icon_error.png', null, [], ICON_SIZE_SMALL);
  157. }
  158. $table->setCellContents($row, 2, $result);
  159. $choiceCounter++;
  160. $row++;
  161. }
  162. // Question total
  163. $table->setCellContents($row, 0, get_lang('Total'));
  164. $table->setCellContents($row, 1, $data['question']);
  165. $totalOptions = count($data['options']);
  166. $arrayScore = [0 => $total, 1 => $totalOptions];
  167. $scoreToString = $scoreDisplay->display_score($arrayScore);
  168. $table->setCellContents($row, 2, $scoreToString);
  169. $table->setCellAttributes($row, 0, ['style' => 'font-weight:bold']);
  170. $table->setCellAttributes($row, 1, ['style' => 'font-weight:bold']);
  171. $table->setCellAttributes($row, 2, ['style' => 'font-weight:bold']);
  172. $categories[] = [
  173. 'name' => $data['question'],
  174. 'score' => $scoreDisplay->display_score($arrayScore, SCORE_DIV),
  175. 'score_numeric' => $scoreDisplay->display_score($arrayScore, SCORE_NUMERIC),
  176. 'score_percentage' => $scoreDisplay->display_score($arrayScore, SCORE_PERCENT),
  177. ];
  178. $tpl->assign('categories', $categories);
  179. $globalTotalCount += $totalOptions;
  180. $row++;
  181. }
  182. $globalScoreTotal = [0 => $globalTotal, 1 => $globalTotalCount];
  183. $score = $scoreDisplay->display_score($globalScoreTotal);
  184. $generalScore[] = [
  185. 'score' => $scoreDisplay->display_score($globalScoreTotal, SCORE_DIV),
  186. 'score_numeric' => $scoreDisplay->display_score($globalScoreTotal, SCORE_NUMERIC),
  187. 'score_percentage' => $scoreDisplay->display_score($globalScoreTotal, SCORE_PERCENT),
  188. ];
  189. $tpl->assign('general_score', $generalScore);
  190. $tpl->assign('global_total', $score);
  191. $tableToString = $table->toHtml();
  192. $duration = learnpathItem::getScormTimeFromParameter('js', $itemView->getTotalTime());
  193. $dataLpInfo = [
  194. 'name' => $lp->getName(),
  195. 'attempt' => $itemView->getViewCount(),
  196. 'score' => $score,
  197. 'duration' => $duration,
  198. 'start_time' => api_get_local_time($itemView->getStartTime()),
  199. 'start_date' => api_get_local_time($itemView->getStartTime(), null, null, null, false),
  200. 'end_time' => api_get_local_time($endTime),
  201. 'candidate' => $studentName,
  202. ];
  203. $tpl->assign('data', $dataLpInfo);
  204. $contentText = $tpl->fetch($tpl->get_template('my_space/pdf_tracking_lp.tpl'));
  205. $content = $contentText.'<pagebreak>'.$tableToString;
  206. $pdf = new PDF('A4', 'P', ['margin_footer' => 4, 'top' => 40, 'bottom' => 25]);
  207. $table = new HTML_Table(['class' => 'table', 'style' => 'display: block; margin-bottom: 50px;']);
  208. $logo = ChamiloApi::getPlatformLogo(
  209. $theme,
  210. [
  211. 'title' => '',
  212. 'style' => 'max-width:180px, margin-bottom: 100px;',
  213. 'id' => 'header-logo',
  214. ]
  215. );
  216. $table->setCellContents(0, 0, $logo);
  217. $secondLogo = api_get_path(SYS_PATH).'custompages/url-images/'.api_get_current_access_url_id().'_url_image_2.png';
  218. $logo2 = Display::img($secondLogo, null, ['style' => 'height:70px;']);
  219. $table->setCellContents(0, 1, $logo2);
  220. $table->setCellAttributes(0, 1, ['style' => 'display:block;float:right;text-align:right']);
  221. $pdf->set_custom_header($table->toHtml());
  222. $background = api_get_path(SYS_PATH).'custompages/url-images/'.api_get_current_access_url_id().'_pdf_background.png';
  223. $content = '<html><body style="background-image-resize: 5; background-position: top left; background-image: url('.$background.');">'.$content.'</body></html>';
  224. $pdf->content_to_pdf(
  225. $content,
  226. null,
  227. $courseInfo['code'].'_'.$lp->getName().'_'.api_get_local_time(),
  228. $courseInfo['code'],
  229. 'D',
  230. false,
  231. null,
  232. false,
  233. true
  234. );
  235. break;
  236. }
  237. $output = require_once api_get_path(SYS_CODE_PATH).'lp/lp_stats.php';
  238. $actions = [];
  239. $actions[] = Display::url(
  240. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM),
  241. 'javascript:history.back();'
  242. );
  243. $actions[] = Display::url(
  244. Display::return_icon('printer.png', get_lang('Print'), '', ICON_SIZE_MEDIUM),
  245. 'javascript:window.print();'
  246. );
  247. $actions[] = Display::url(
  248. Display::return_icon('export_csv.png', get_lang('CSV export'), '', ICON_SIZE_MEDIUM),
  249. api_get_self().'?export=csv&'.Security::remove_XSS($_SERVER['QUERY_STRING'])
  250. );
  251. Display::display_header($nameTools);
  252. echo Display::toolbarAction(
  253. 'actions',
  254. [implode(PHP_EOL, $actions)]
  255. );
  256. $table_title = $session_id
  257. ? Display::return_icon('session.png', get_lang('Session')).PHP_EOL.api_get_session_name($session_id).PHP_EOL
  258. : PHP_EOL;
  259. $table_title .= Display::return_icon('course.png', get_lang('Course')).PHP_EOL.$courseInfo['name'].PHP_EOL
  260. .Display::return_icon('user.png', get_lang('User')).' '.$name;
  261. echo Display::page_header($table_title);
  262. echo Display::page_subheader(
  263. Display::return_icon('learnpath.png', get_lang('Learning path')).PHP_EOL.$lp_title
  264. );
  265. echo $output;
  266. Display::display_footer();