Tracking: Fix queries, use entities

Replace PHPExcel with Export::arrayToXls
pull/3924/head
Julio Montoya 4 years ago
parent d20ae844af
commit 4b6eaa319b
  1. 203
      public/main/tracking/exams.php
  2. 2
      public/main/tracking/lp_report.php
  3. 5
      src/CoreBundle/Entity/ResourceLink.php

@ -2,9 +2,9 @@
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
/** use Chamilo\CoreBundle\Framework\Container;
* Exams script. use Chamilo\CourseBundle\Entity\CQuiz;
*/
require_once __DIR__.'/../inc/global.inc.php'; require_once __DIR__.'/../inc/global.inc.php';
$toolTable = Database::get_course_table(TABLE_TOOL_LIST); $toolTable = Database::get_course_table(TABLE_TOOL_LIST);
@ -22,10 +22,11 @@ if (isset($_GET['export'])) {
$exportToXLS = true; $exportToXLS = true;
} }
$courseInfo = api_get_course_info();
$global = false;
if (api_is_platform_admin() && empty($_GET['cidReq'])) { if (api_is_platform_admin() && empty($_GET['cidReq'])) {
$global = true; $global = true;
} else {
$global = false;
} }
$courseList = []; $courseList = [];
@ -94,13 +95,12 @@ $exerciseId = isset($_REQUEST['exercise_id']) ? intval($_REQUEST['exercise_id'])
$form->setDefaults(['score' => $filter_score]); $form->setDefaults(['score' => $filter_score]);
if (!$exportToXLS) { if (!$exportToXLS) {
Display :: display_header(get_lang('Reporting')); Display:: display_header(get_lang('Reporting'));
$actionsLeft = $actionsRight = ''; $actionsLeft = $actionsRight = '';
if ($global) { if ($global) {
$actionsLeft .= '<a href="'.api_get_path(WEB_CODE_PATH).'auth/my_progress.php">'. $actionsLeft .= '<a href="'.api_get_path(WEB_CODE_PATH).'auth/my_progress.php">'.
Display::return_icon('statistics.png', get_lang('View my progress'), '', ICON_SIZE_MEDIUM); Display::return_icon('statistics.png', get_lang('View my progress'), '', ICON_SIZE_MEDIUM);
$actionsLeft .= '</a>'; $actionsLeft .= '</a>';
$courseInfo = api_get_course_info();
$actionsRight .= '<a href="'.api_get_self().'?export=1&score='.$filter_score.'&exercise_id='.$exerciseId.'">'. $actionsRight .= '<a href="'.api_get_self().'?export=1&score='.$filter_score.'&exercise_id='.$exerciseId.'">'.
Display::return_icon('export_excel.png', get_lang('Excel export'), '', ICON_SIZE_MEDIUM).'</a>'; Display::return_icon('export_excel.png', get_lang('Excel export'), '', ICON_SIZE_MEDIUM).'</a>';
@ -173,6 +173,7 @@ $export_array_global = $export_array = [];
$s_css_class = null; $s_css_class = null;
if (!empty($courseList)) { if (!empty($courseList)) {
$quizRepo = Container::getQuizRepository();
foreach ($courseList as $course) { foreach ($courseList as $course) {
$courseId = $course->getId(); $courseId = $course->getId();
$sessionList = SessionManager::get_session_by_course($courseId); $sessionList = SessionManager::get_session_by_course($courseId);
@ -185,20 +186,31 @@ if (!empty($courseList)) {
} }
if ($global) { if ($global) {
// @todo use CQuizRepository $qb = $quizRepo->getResourcesByCourse($course);
$sql = "SELECT count(iid) as count $qb->select('count(resource)');
$exerciseCount = $qb->getQuery()->getSingleScalarResult();
/*$sql = "SELECT count(iid) as count
FROM $quizTable AS quiz FROM $quizTable AS quiz
WHERE c_id = $courseId AND active = 1 AND (session_id = 0 OR session_id IS NULL)"; WHERE c_id = $courseId AND active = 1 AND (session_id = 0 OR session_id IS NULL)";
$result = Database::query($sql); $result = Database::query($sql);
$countExercises = Database::store_result($result); $countExercises = Database::store_result($result);
$exerciseCount = $countExercises[0]['count']; $exerciseCount = $countExercises[0]['count'];*/
$qb = $quizRepo->getResourcesByCourse($course);
$qb->select('count(resource)');
$qb->andWhere('links.session IS NOT NULL');
$exerciseSessionCount = $qb->getQuery()->getSingleScalarResult();
$sql = "SELECT count(iid) as count /*$sql = "SELECT count(iid) as count
FROM $quizTable AS quiz FROM $quizTable AS quiz
WHERE c_id = $courseId AND active = 1 AND session_id <> 0"; WHERE c_id = $courseId AND active = 1 AND session_id <> 0";
$result = Database::query($sql); $result = Database::query($sql);
$countExercises = Database::store_result($result); $countExercises = Database::store_result($result);
$exerciseSessionCount = $countExercises[0]['count']; $exerciseSessionCount = $countExercises[0]['count'];*/
$exerciseCount = $exerciseCount + $exerciseCount * count($newSessionList) + $exerciseSessionCount; $exerciseCount = $exerciseCount + $exerciseCount * count($newSessionList) + $exerciseSessionCount;
// Add course and session list. // Add course and session list.
@ -217,15 +229,21 @@ if (!empty($courseList)) {
// If main tool is visible. // If main tool is visible.
if (1 == Database::result($result, 0, 'visibility')) { if (1 == Database::result($result, 0, 'visibility')) {
$exercises = [];
// Getting the exam list. // Getting the exam list.
if ($global) { if ($global) {
$sql = "SELECT quiz.title, iid, session_id $qb = $quizRepo->getResourcesByCourse($course);
$exercises = $qb->getQuery()->getResult();
/*$sql = "SELECT quiz.title, iid, session_id
FROM $quizTable AS quiz FROM $quizTable AS quiz
WHERE c_id = $courseId AND active = 1 WHERE c_id = $courseId AND active = 1
ORDER BY session_id, quiz.title ASC"; ORDER BY session_id, quiz.title ASC";*/
} else { } else {
//$sessionCondition = api_get_session_condition($sessionId, true, false); //$sessionCondition = api_get_session_condition($sessionId, true, false);
if (!empty($exerciseId)) { if (!empty($exerciseId)) {
$exercises = [];
$exercises[] = $quizRepo->find($exerciseId);
/*
$sql = "SELECT quiz.title, iid, session_id $sql = "SELECT quiz.title, iid, session_id
FROM $quizTable AS quiz FROM $quizTable AS quiz
WHERE WHERE
@ -233,9 +251,12 @@ if (!empty($courseList)) {
active = 1 AND active = 1 AND
id = $exerciseId id = $exerciseId
$sessionCondition $sessionCondition
ORDER BY session_id, quiz.title ASC"; ORDER BY session_id, quiz.title ASC";
*/
} else { } else {
$qb = $quizRepo->getResourcesByCourse($course, api_get_session_entity());
$exercises = $qb->getQuery()->getResult();
/*
$sql = "SELECT quiz.title, iid, session_id $sql = "SELECT quiz.title, iid, session_id
FROM $quizTable AS quiz FROM $quizTable AS quiz
WHERE WHERE
@ -243,14 +264,24 @@ if (!empty($courseList)) {
active = 1 active = 1
$sessionCondition $sessionCondition
ORDER BY session_id, quiz.title ASC"; ORDER BY session_id, quiz.title ASC";
*/
} }
} }
$resultExercises = Database::query($sql); if (!empty($exercises)) {
/** @var CQuiz $exercise */
foreach ($exercises as $exercise) {
$links = $exercise->getResourceNode()->getResourceLinks();
$exerciseSessionId = null;
foreach ($links as $link) {
if ($link->hasSession()) {
$exerciseSessionId = $link->getSession()->getId();
break;
}
}
if (Database::num_rows($resultExercises) > 0) { //$exerciseSessionId = $exercise['session_id'];
while ($exercise = Database::fetch_array($resultExercises, 'ASSOC')) {
$exerciseSessionId = $exercise['session_id'];
if (empty($exerciseSessionId)) { if (empty($exerciseSessionId)) {
if ($global) { if ($global) {
@ -267,7 +298,10 @@ if (!empty($courseList)) {
); );
$html .= $result['html']; $html .= $result['html'];
$export_array_global = array_merge($export_array_global, $result['export_array_global']); $export_array_global = array_merge(
$export_array_global,
$result['export_array_global']
);
} }
// Load base course. // Load base course.
@ -394,102 +428,57 @@ function export_complete_report_xls($filename, $array)
{ {
global $global, $filter_score; global $global, $filter_score;
$spreadsheet = new PHPExcel(); $list = [];
$spreadsheet->setActiveSheetIndex(0);
$worksheet = $spreadsheet->getActiveSheet();
$line = 1;
$column = 0; //skip the first column (row titles)
if ($global) { if ($global) {
$worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Courses')); $headers[] = get_lang('Courses');
$column++; $headers[] = get_lang('Tests');
$worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Tests')); $headers[] = get_lang('Taken');
$column++; $headers[] = get_lang('Not taken');
$worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Taken')); $headers[] = sprintf(get_lang('Pass minimum %s'), $filter_score).'%';
$column++; $headers[] = get_lang('Fail');
$worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Not taken')); $headers[] = get_lang('Total learners');
$column++;
$worksheet->setCellValueByColumnAndRow($column, $line, sprintf(get_lang('Pass minimum %s'), $filter_score).'%'); $list[] = $headers;
$column++;
$worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Fail'));
$column++;
$worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Total learners'));
$column++;
$line++;
foreach ($array as $row) { foreach ($array as $row) {
$column = 0; $listItem = [];
foreach ($row as $item) { foreach ($row as $item) {
$worksheet->setCellValueByColumnAndRow($column, $line, html_entity_decode(strip_tags($item))); $listItem[] = html_entity_decode(strip_tags($item));
$column++;
} }
$line++; $list[] = $listItem;
} }
$line++;
} else { } else {
$worksheet->setCellValueByColumnAndRow(0, $line, get_lang('Tests')); $headers[] = get_lang('Tests');
$worksheet->setCellValueByColumnAndRow(1, $line, get_lang('User')); $headers[] = get_lang('User');
$worksheet->setCellValueByColumnAndRow(2, $line, get_lang('Username')); $headers[] = get_lang('Username');
$worksheet->setCellValueByColumnAndRow(3, $line, get_lang('Percentage')); $headers[] = get_lang('Percentage');
$worksheet->setCellValueByColumnAndRow(4, $line, get_lang('Status')); $headers[] = get_lang('Status');
$worksheet->setCellValueByColumnAndRow(5, $line, get_lang('Attempts')); $headers[] = get_lang('Attempts');
$line++;
$list[] = $headers;
foreach ($array as $row) { foreach ($array as $row) {
$worksheet->setCellValueByColumnAndRow( $listItem = [];
0, $listItem[] = html_entity_decode(strip_tags($row['exercise']));
$line,
html_entity_decode(strip_tags($row['exercise']))
);
foreach ($row['users'] as $key => $user) { foreach ($row['users'] as $key => $user) {
$worksheet->setCellValueByColumnAndRow( $listItem[] = html_entity_decode(strip_tags($user));
1, $listItem[] = $row['usernames'][$key];
$line,
html_entity_decode(strip_tags($user))
);
$worksheet->setCellValueByColumnAndRow(
2,
$line,
$row['usernames'][$key]
);
$column = 3;
foreach ($row['results'][$key] as $result_item) { foreach ($row['results'][$key] as $result_item) {
$worksheet->setCellValueByColumnAndRow( $listItem[] = html_entity_decode(strip_tags($result_item));
$column,
$line,
html_entity_decode(strip_tags($result_item))
);
$column++;
} }
$line++; $line++;
} }
$list[] = $listItem;
} }
} }
$file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename); Export::arrayToXls($list, $filename);
$writer = new PHPExcel_Writer_Excel2007($spreadsheet);
$writer->save($file);
DocumentManager::file_send_for_download($file, true, $filename);
exit;
} }
/** function processStudentList($filter_score, $global, Cquiz $exercise, $courseInfo, $sessionId, $newSessionList)
* @param $filter_score
* @param $global
* @param $exercise
* @param $courseInfo
* @param $sessionId
* @param $newSessionList
*
* @return array
*/
function processStudentList($filter_score, $global, $exercise, $courseInfo, $sessionId, $newSessionList)
{ {
if ((isset($exercise['id']) && empty($exercise['id'])) || /*if ((isset($exercise['id']) && empty($exercise['id'])) ||
!isset($exercise['id']) !isset($exercise['id'])
) { ) {
return [ return [
@ -497,7 +486,7 @@ function processStudentList($filter_score, $global, $exercise, $courseInfo, $ses
'export_array_global' => [], 'export_array_global' => [],
'total_students' => 0, 'total_students' => 0,
]; ];
} }*/
$exerciseStatsTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); $exerciseStatsTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
$courseId = api_get_course_int_id($courseInfo['code']); $courseId = api_get_course_int_id($courseInfo['code']);
@ -535,7 +524,7 @@ function processStudentList($filter_score, $global, $exercise, $courseInfo, $ses
$html .= '<td>'; $html .= '<td>';
} }
$html .= $exercise['title']; $html .= $exercise->getTitle();
if ($global && !empty($sessionId)) { if ($global && !empty($sessionId)) {
$sessionName = isset($newSessionList[$sessionId]) ? $newSessionList[$sessionId] : null; $sessionName = isset($newSessionList[$sessionId]) ? $newSessionList[$sessionId] : null;
@ -546,7 +535,7 @@ function processStudentList($filter_score, $global, $exercise, $courseInfo, $ses
$globalRow = [ $globalRow = [
$courseInfo['title'], $courseInfo['title'],
$exercise['title'], $exercise->getTitle(),
]; ];
$total_with_parameter_score = 0; $total_with_parameter_score = 0;
@ -555,13 +544,15 @@ function processStudentList($filter_score, $global, $exercise, $courseInfo, $ses
$studentResult = []; $studentResult = [];
$export_array = []; $export_array = [];
$exerciseId = $exercise->getIid();
foreach ($students as $student) { foreach ($students as $student) {
$studentId = isset($student['user_id']) ? $student['user_id'] : $student['id_user']; $studentId = isset($student['user_id']) ? $student['user_id'] : $student['id_user'];
$sql = "SELECT COUNT(ex.exe_id) as count $sql = "SELECT COUNT(ex.exe_id) as count
FROM $exerciseStatsTable AS ex FROM $exerciseStatsTable AS ex
WHERE WHERE
ex.c_id = $courseId AND ex.c_id = $courseId AND
ex.exe_exo_id = ".$exercise['id']." AND ex.exe_exo_id = ".$exerciseId." AND
exe_user_id= $studentId AND exe_user_id= $studentId AND
session_id = $sessionId session_id = $sessionId
"; ";
@ -573,7 +564,7 @@ function processStudentList($filter_score, $global, $exercise, $courseInfo, $ses
WHERE WHERE
exe_user_id = $studentId AND exe_user_id = $studentId AND
c_id = $courseId AND c_id = $courseId AND
exe_exo_id = ".$exercise['id']." AND exe_exo_id = ".$exerciseId." AND
session_id = $sessionId session_id = $sessionId
ORDER BY score DESC ORDER BY score DESC
LIMIT 1"; LIMIT 1";
@ -610,8 +601,7 @@ function processStudentList($filter_score, $global, $exercise, $courseInfo, $ses
$userRow .= '</td>'; $userRow .= '</td>';
$userRow .= '<td>'.$userInfo['username'].'</td>'; $userRow .= '<td>'.$userInfo['username'].'</td>';
// Best result // Best result.
if (!empty($attempts['count'])) { if (!empty($attempts['count'])) {
$userRow .= '<td>'; $userRow .= '<td>';
$userRow .= $percentageScore; $userRow .= $percentageScore;
@ -660,7 +650,7 @@ function processStudentList($filter_score, $global, $exercise, $courseInfo, $ses
} }
} }
$row_not_global['exercise'] = $exercise['title']; $row_not_global['exercise'] = $exercise->getTitle();
if (!$global) { if (!$global) {
if (!empty($studentResult)) { if (!empty($studentResult)) {
@ -734,4 +724,5 @@ function processStudentList($filter_score, $global, $exercise, $courseInfo, $ses
'total_students' => $totalStudents, 'total_students' => $totalStudents,
]; ];
} }
Display :: display_footer();
Display:: display_footer();

@ -414,7 +414,7 @@ foreach ($lps as $lp) {
if (!empty($action)) { if (!empty($action)) {
switch ($action) { switch ($action) {
case 'export': case 'export':
$data = getData(0, 100000, null, null); $data = getData(0, 100000, null, null, null);
$data = array_merge([$headers], $data); $data = array_merge([$headers], $data);
$name = api_get_course_id().'_'.get_lang('Learnpath').'_'.get_lang('Export'); $name = api_get_course_id().'_'.get_lang('Learnpath').'_'.get_lang('Export');
Export::arrayToXls($data, $name); Export::arrayToXls($data, $name);

@ -204,6 +204,11 @@ class ResourceLink
return $this; return $this;
} }
public function hasSession(): bool
{
return null !== $this->session;
}
public function hasCourse(): bool public function hasCourse(): bool
{ {
return null !== $this->course; return null !== $this->course;

Loading…
Cancel
Save