feat: Add HTML to text conversion and enhance Excel export

Integrates soundasleep/html2text library for HTML to text conversion in user grade export.
Applies consistent styling in Excel export, enhancing readability.
Includes email field in exported user details.
This commit is contained in:
Kumi 2024-11-27 15:36:37 +01:00
parent 9923bab2fb
commit 56494f7d15
Signed by: kumi
GPG key ID: ECBCC9082395383F
3 changed files with 112 additions and 19 deletions

View file

@ -1,5 +1,6 @@
{ {
"require": { "require": {
"phpoffice/phpspreadsheet": "^3.5" "phpoffice/phpspreadsheet": "^3.5",
"soundasleep/html2text": "^2.1"
} }
} }

57
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "94fc9216e5758e59b249edbc838bebaf", "content-hash": "5a6414864f5fcd93d59b027740e52298",
"packages": [ "packages": [
{ {
"name": "maennchen/zipstream-php", "name": "maennchen/zipstream-php",
@ -504,6 +504,61 @@
"source": "https://github.com/php-fig/simple-cache/tree/3.0.0" "source": "https://github.com/php-fig/simple-cache/tree/3.0.0"
}, },
"time": "2021-10-29T13:26:27+00:00" "time": "2021-10-29T13:26:27+00:00"
},
{
"name": "soundasleep/html2text",
"version": "2.1.0",
"source": {
"type": "git",
"url": "https://github.com/soundasleep/html2text.git",
"reference": "83502b6f8f1aaef8e2e238897199d64f284b4af3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/soundasleep/html2text/zipball/83502b6f8f1aaef8e2e238897199d64f284b4af3",
"reference": "83502b6f8f1aaef8e2e238897199d64f284b4af3",
"shasum": ""
},
"require": {
"ext-dom": "*",
"ext-libxml": "*",
"php": "^7.3|^8.0"
},
"require-dev": {
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": "^7.0|^8.0|^9.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Soundasleep\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jevon Wright",
"homepage": "https://jevon.org",
"role": "Developer"
}
],
"description": "A PHP script to convert HTML into a plain text format",
"homepage": "https://github.com/soundasleep/html2text",
"keywords": [
"email",
"html",
"php",
"text"
],
"support": {
"email": "support@jevon.org",
"issues": "https://github.com/soundasleep/html2text/issues",
"source": "https://github.com/soundasleep/html2text/tree/2.1.0"
},
"time": "2023-01-06T09:28:15+00:00"
} }
], ],
"packages-dev": [], "packages-dev": [],

View file

@ -11,6 +11,11 @@ require_once('vendor/autoload.php');
use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use Soundasleep\Html2Text;
admin_externalpage_setup('report_usergrades_details', '', null, '', array('capability' => 'report/usergrades:view')); admin_externalpage_setup('report_usergrades_details', '', null, '', array('capability' => 'report/usergrades:view'));
@ -26,29 +31,46 @@ if ($data = $mform->get_data()) {
$courses = enrol_get_users_courses($user->id, true); $courses = enrol_get_users_courses($user->id, true);
$questions = $DB->get_records('question'); $questions = $DB->get_records('question');
if (isset($data->exportxls)) { // Assume a form field named 'exportxls' for Excel export action if (isset($data->exportxls)) { //
// Create a new Spreadsheet object // Create a new Spreadsheet object
$spreadsheet = new Spreadsheet(); $spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet(); $sheet = $spreadsheet->getActiveSheet();
// Set default font and size
$spreadsheet->getDefaultStyle()->getFont()->setName('Arial')->setSize(10);
$row = 1; $row = 1;
// Add the user details // Add the user details
$sheet->setCellValue('A' . $row, get_string('username', 'report_usergrades')); $headers = [
$sheet->setCellValue('B' . $row, $user->username); get_string('field', 'report_usergrades'),
get_string('value', 'report_usergrades'),
];
$details = [
[get_string('username', 'report_usergrades'), $user->username],
[get_string('firstname', 'report_usergrades'), $user->firstname],
[get_string('lastname', 'report_usergrades'), $user->lastname],
[get_string('email', 'report_usergrades'), $user->email]
];
// Apply header styling
$sheet->fromArray($headers, NULL, 'A' . $row);
$sheet->getStyle('A' . $row . ':B' . $row)->getFont()->setBold(true);
$sheet->getStyle('A' . $row . ':B' . $row)->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB("ff808080");
$row++; $row++;
$sheet->setCellValue('A' . $row, get_string('firstname', 'report_usergrades'));
$sheet->setCellValue('B' . $row, $user->firstname); // Fill in the user details
$row++; foreach ($details as $detail) {
$sheet->setCellValue('A' . $row, get_string('lastname', 'report_usergrades')); $sheet->fromArray($detail, NULL, 'A' . $row);
$sheet->setCellValue('B' . $row, $user->lastname); $row++;
$row++; }
$sheet->setCellValue('A' . $row, get_string('email', 'report_usergrades'));
$sheet->setCellValue('B' . $row, $user->email);
$row += 2; // Extra space before courses $row += 2; // Extra space before courses
// Start iterating the courses // Start iterating the courses
foreach ($courses as $course) { foreach ($courses as $course) {
$sheet->setCellValue('A' . $row, get_string('course', 'report_usergrades') . ': ' . $course->fullname); $sheet->setCellValue('A' . $row, get_string('course', 'report_usergrades') . ': ' . $course->fullname);
$sheet->getStyle('A' . $row)->getFont()->setBold(true);
$row++; $row++;
$quizzes = $DB->get_records('quiz', array('course' => $course->id)); $quizzes = $DB->get_records('quiz', array('course' => $course->id));
@ -59,15 +81,22 @@ if ($data = $mform->get_data()) {
if ($quiz_attempts) { if ($quiz_attempts) {
$sheet->setCellValue('A' . $row, get_string('quiz', 'report_usergrades') . ': ' . $quiz->name); $sheet->setCellValue('A' . $row, get_string('quiz', 'report_usergrades') . ': ' . $quiz->name);
$sheet->getStyle('A' . $row)->getFont()->setBold(true);
$row++; $row++;
foreach ($quiz_attempts as $attempt) { foreach ($quiz_attempts as $attempt) {
$sheet->setCellValue('A' . $row, get_string('attempt', 'report_usergrades') . ' ' . $attempt->attempt); $sheet->setCellValue('A' . $row, get_string('attempt', 'report_usergrades') . ' ' . $attempt->attempt);
$sheet->getStyle('A' . $row)->getFont()->setItalic(true);
$row++; $row++;
$sheet->setCellValue('A' . $row, get_string('question', 'report_usergrades')); $attempt_header = [
$sheet->setCellValue('B' . $row, get_string('response', 'report_usergrades')); get_string('question', 'report_usergrades'),
$sheet->setCellValue('C' . $row, get_string('grade', 'report_usergrades')); get_string('response', 'report_usergrades'),
get_string('grade', 'report_usergrades')
];
$sheet->fromArray($attempt_header, NULL, 'A' . $row);
$sheet->getStyle('A' . $row . ':C' . $row)->getFont()->setBold(true)->getColor()->setARGB(Color::COLOR_WHITE);
$sheet->getStyle('A' . $row . ':C' . $row)->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB(Color::COLOR_DARKBLUE);
$row++; $row++;
$question_usages = $DB->get_records('question_usages', array('id' => $attempt->uniqueid)); $question_usages = $DB->get_records('question_usages', array('id' => $attempt->uniqueid));
@ -83,9 +112,12 @@ if ($data = $mform->get_data()) {
foreach ($question_grades as $question_grade) { foreach ($question_grades as $question_grade) {
if ($question_grade->fraction) { if ($question_grade->fraction) {
$sheet->setCellValue('A' . $row, $question->name . ': ' . $question->questiontext); $data = [
$sheet->setCellValue('B' . $row, $response); $question->name . ': ' . Html2Text::convert($question->questiontext),
$sheet->setCellValue('C' . $row, $question_grade->fraction); $response,
$question_grade->fraction
];
$sheet->fromArray($data, NULL, 'A' . $row);
$row++; $row++;
} }
} }
@ -94,6 +126,7 @@ if ($data = $mform->get_data()) {
$total_grade = $DB->get_record('quiz_grades', array('quiz' => $quiz->id, 'userid' => $user->id)); $total_grade = $DB->get_record('quiz_grades', array('quiz' => $quiz->id, 'userid' => $user->id));
$sheet->setCellValue('A' . $row, get_string('totalgrade', 'report_usergrades') . ': ' . $total_grade->grade . ' / ' . $quiz->grade); $sheet->setCellValue('A' . $row, get_string('totalgrade', 'report_usergrades') . ': ' . $total_grade->grade . ' / ' . $quiz->grade);
$sheet->getStyle('A' . $row . ':C' . $row)->getFont()->setBold(true);
$row += 2; // Extra space before next attempt or quiz $row += 2; // Extra space before next attempt or quiz
} }
} }
@ -102,6 +135,10 @@ if ($data = $mform->get_data()) {
$row += 2; // Extra space between courses $row += 2; // Extra space between courses
} }
$sheet->getColumnDimension('A')->setWidth(50);
$sheet->getColumnDimension('B')->setWidth(50);
$sheet->getColumnDimension('C')->setWidth(10);
// Create writer and output the file // Create writer and output the file
$writer = new Xlsx($spreadsheet); $writer = new Xlsx($spreadsheet);
$filename = 'user_grades_' . $user->id . '.xlsx'; $filename = 'user_grades_' . $user->id . '.xlsx';