Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session: Add batch session export to CSV - refs BT#22096 #5853

Merged
merged 7 commits into from
Dec 4, 2024
Binary file added public/img/copy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/icons/32/attendance_list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/icons/32/scorms.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/icons/32/scorms_na.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/icons/32/security.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/icons/32/security_na.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/icons/32/tools.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/icons/32/tools_na.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/icons/32/user.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/icons/32/user_na.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/save_pack.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,318 changes: 1,318 additions & 0 deletions public/main/inc/lib/TrackingCourseLog.php

Large diffs are not rendered by default.

44 changes: 3 additions & 41 deletions public/main/inc/lib/exercise.lib.php
Original file line number Diff line number Diff line change
@@ -3000,47 +3000,9 @@ public static function get_all_exercises(

$repo = Container::getQuizRepository();

return $repo->findAllByCourse($course, $session, (string) $search, $active);

// Show courses by active status
/*if (true == $search_all_sessions) {
$conditions = [
'where' => [
$active_sql.' c_id = ? '.$needle_where.$timeConditions => [
$course_id,
$needle,
],
],
'order' => 'title',
];
} else {
if (empty($session_id)) {
$conditions = [
'where' => [
$active_sql.' (session_id = 0 OR session_id IS NULL) AND c_id = ? '.$needle_where.$timeConditions => [
$course_id,
$needle,
],
],
'order' => 'title',
];
} else {
$conditions = [
'where' => [
$active_sql.' (session_id = 0 OR session_id IS NULL OR session_id = ? ) AND c_id = ? '.$needle_where.$timeConditions => [
$session_id,
$course_id,
$needle,
],
],
'order' => 'title',
];
}
}

$table = Database::get_course_table(TABLE_QUIZ_TEST);

return Database::select('*', $table, $conditions);*/
return $repo->findAllByCourse($course, $session, (string) $search, $active)
->getQuery()
->getResult();
}

/**
45 changes: 39 additions & 6 deletions public/main/inc/lib/export.lib.inc.php
Original file line number Diff line number Diff line change
@@ -26,14 +26,9 @@ public function __construct()
/**
* Export tabular data to CSV-file.
*
* @param array $data
* @param string $filename
* @param bool $writeOnly Whether to only write on disk or also send for download
* @param string $enclosure
*
* @return mixed csv raw data | false if no data to export | string file path if success in $writeOnly mode
*/
public static function arrayToCsv($data, $filename = 'export', $writeOnly = false, $enclosure = '"')
public static function arrayToCsv(array $data, string $filename = 'export', bool $writeOnly = false, string $enclosure = '"')
{
if (empty($data)) {
return false;
@@ -55,6 +50,44 @@ public static function arrayToCsv($data, $filename = 'export', $writeOnly = fals
return $filePath;
}

/**
* Converts an array of data into a CSV file and optionally sends it for download.
*
* @return string|void Returns the file path if $writeOnly is true, otherwise sends the file for download and exits.
*/
public static function arrayToCsvSimple(array $data, string $filename = 'export', bool $writeOnly = false)
{
$file = api_get_path(SYS_ARCHIVE_PATH) . uniqid('') . '.csv';

$handle = fopen($file, 'w');

if ($handle === false) {
throw new \RuntimeException("Unable to create or open the file: $file");
}

if (is_array($data)) {
foreach ($data as $row) {
$line = '';
if (is_array($row)) {
foreach ($row as $value) {
$line .= '"' . str_replace('"', '""', (string)$value) . '";';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A cast statement must be followed by a single space

}
}
fwrite($handle, rtrim($line, ';') . "\n");
}
}

fclose($handle);

if (!$writeOnly) {
DocumentManager::file_send_for_download($file, true, $filename . '.csv');
unlink($file);
exit;
}

return $file;
}

/**
* Export tabular data to XLS-file.
*/
34 changes: 24 additions & 10 deletions public/main/inc/lib/extra_field_value.lib.php
Original file line number Diff line number Diff line change
@@ -220,6 +220,10 @@ public function saveFieldValues(

$tags = [];
foreach ($tagValues as $tagValue) {
if (is_array($tagValue)) {
$tagValue = reset($tagValue);
}

if (empty($tagValue)) {
continue;
}
@@ -338,16 +342,26 @@ public function saveFieldValues(

break;
case ExtraField::FIELD_TYPE_DATE:
$d = DateTime::createFromFormat('Y-m-d', $value);
$valid = $d && $d->format('Y-m-d') === $value;
if ($valid) {
$newParams = [
'item_id' => $params['item_id'],
'field_id' => $extraFieldInfo['id'],
'field_value' => $value,
'comment' => $comment,
];
$this->save($newParams, $showQuery);
if (is_array($value)) {
if (empty($value)) {
break;
}
$value = reset($value);
}

if (is_string($value) && !empty($value)) {
$d = DateTime::createFromFormat('Y-m-d', $value);
$valid = $d && $d->format('Y-m-d') === $value;

if ($valid) {
$newParams = [
'item_id' => $params['item_id'],
'field_id' => $extraFieldInfo['id'],
'field_value' => $value,
'comment' => $comment,
];
$this->save($newParams, $showQuery);
}
}
break;
case ExtraField::FIELD_TYPE_DATETIME:
Loading
Loading