Skip to content

Commit

Permalink
EDEKAN-564: for php < 8 use only
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Mayer committed Mar 13, 2024
1 parent 0dc6ee3 commit 7fe5299
Show file tree
Hide file tree
Showing 5 changed files with 934 additions and 4 deletions.
8 changes: 4 additions & 4 deletions classes/coversheet/create_coversheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@

namespace quiz_archiver\coversheet;

<<<<<<< HEAD
use admin_setting_heading;
use coding_exception;
use quiz_archiver\external\get_attempts_metadata;
=======
use coding_exception;
>>>>>>> 493386a (EDEKAN-564: Some adjustments for coversheet generation)

defined('MOODLE_INTERNAL') || die();

Expand Down Expand Up @@ -146,9 +142,13 @@ private static function get_attempt_metadata(int $attemptid): object {
* @param string|int|object|array $params
* @return string
*/
<<<<<<< HEAD
private static function check_class_and_method(string $classpath, string $method, string|int|object|array $params): string {
<<<<<<< HEAD
=======
=======
private static function check_class_and_method(string $classpath, string $method, $params): string {
>>>>>>> 36eaf8b (EDEKAN-564: for php < 8 use only)
\local_debugger\performance\debugger::print_debug('test', 'get_coversheet', 'POS 5');
>>>>>>> 493386a (EDEKAN-564: Some adjustments for coversheet generation)

Expand Down
247 changes: 247 additions & 0 deletions classes/coversheet/create_coversheet_BACKUP_16491.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Handles everything that is needed for coversheet creation.
*
* @package quiz_archiver
* @copyright ISB Bayern, 2024
* @author Dr. Peter Mayer
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace quiz_archiver\coversheet;

<<<<<<< HEAD
use admin_setting_heading;
use coding_exception;
use quiz_archiver\external\get_attempts_metadata;
=======
use coding_exception;
>>>>>>> 493386a (EDEKAN-564: Some adjustments for coversheet generation)

defined('MOODLE_INTERNAL') || die();

/**
* Handles everything that is needed for coversheet creation.
*
* @package quiz_archiver
* @copyright ISB Bayern, 2024
* @author Dr. Peter Mayer
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class create_coversheet {

/**
* Create the coversheet.
* @param int $attemptid
* @return string
*/
public static function get_coversheet(int $attemptid): string {
global $OUTPUT;

$config = get_config('quiz_archiver');

if (empty($config->enable_pdf_coversheet)) {
return '';
}

// Get all needed attempt metadata. E.g. User, timestarted, timefinished etc.
$attemptmetadata = self::get_attempt_metadata($attemptid);

// Find all placeholders.
preg_match_all('/{{(.*)_(.*)}}/', $config->dynamic_pdf_content, $matches);
<<<<<<< HEAD
=======
\local_debugger\performance\debugger::print_debug('test', 'get_coversheet', 'POS 1');
>>>>>>> 493386a (EDEKAN-564: Some adjustments for coversheet generation)

// Now replace all placeholders.
$html = $config->dynamic_pdf_content;
foreach ($matches[0] as $key => $placeholder) {
$classpath = '\quiz_archiver\coversheet\placeholder\\' . $matches[1][$key];
<<<<<<< HEAD
$method = $matches[2][$key];
$replacement = self::check_class_and_method($classpath, $method, $attemptmetadata);
=======
\local_debugger\performance\debugger::print_debug('test', 'get_coversheet', 'POS 2');
$method = $matches[2][$key];
\local_debugger\performance\debugger::print_debug('test', 'get_coversheet', 'POS 3');
$replacement = self::check_class_and_method($classpath, $method, $attemptmetadata);
\local_debugger\performance\debugger::print_debug('test', 'get_coversheet', 'POS 4');
>>>>>>> 493386a (EDEKAN-564: Some adjustments for coversheet generation)
$html = preg_replace('/' . $placeholder . '/', $replacement, $html);
}
\local_debugger\performance\debugger::print_debug('test', 'html', $html);

$filename = !empty($config->pdfcoversheetbackgroundimage) ? $config->pdfcoversheetbackgroundimage : null;
$fs = get_file_storage();
$context = \context_system::instance();

$templatecontext = [];

$backgroundimage = $fs->get_file($context->id, 'quiz_archiver', 'pdfcoversheetbackgroundimage', 0, '/', $filename);
if (!empty($backgroundimage)) {
$imgdata64 = base64_encode($backgroundimage->get_content());
$templatecontext['backgroundimage64'] = 'data:image/png;base64,' . $imgdata64;
}
$templatecontext['html'] = $html;
$templatecontext['styles'] = 'page-break-after: always; width: 100%; height: 100vh;';

$html = $OUTPUT->render_from_template('quiz_archiver/pdfcoversheet_html_sceleton', $templatecontext);
// $html = '<div style="' . join(' ', $styles) . '">' . $html . '</div>';
return $html;
}

/**
* Gets the metadata of all attempts made inside this quiz, excluding previews.
*
* @param array|null $filter_attemptids If given, only attempts with the given
* IDs will be returned.
*
* @return object
* @throws \dml_exception
*/
private static function get_attempt_metadata(int $attemptid): object {
global $DB;

$fields = [
'qa.id AS attemptid',
'qa.userid',
'qa.quiz as quizinstance',
'qa.attempt as attemptnumber',
'qa.state',
'qa.timestart',
'qa.timefinish',
'q.course as courseid',
];

$sql = "SELECT " . join(", ", $fields) .
" FROM {quiz_attempts} qa " .
"LEFT JOIN {user} u ON qa.userid = u.id " .
"LEFT JOIN {quiz} q on q.id = qa.quiz " .
"WHERE qa.id = :qaid";

// Get all requested attempt
return $DB->get_record_sql($sql, ["qaid" => $attemptid]);
}

/**
* Checks and executes the callback method.
* @param string $classpath
* @param string $method
* @param string|int|object|array $params
* @return string
*/
<<<<<<< HEAD
private static function check_class_and_method(string $classpath, string $method, string|int|object|array $params): string {
<<<<<<< HEAD
=======
=======
private static function check_class_and_method(string $classpath, string $method, $params): string {
>>>>>>> 36eaf8b (EDEKAN-564: for php < 8 use only)
\local_debugger\performance\debugger::print_debug('test', 'get_coversheet', 'POS 5');
>>>>>>> 493386a (EDEKAN-564: Some adjustments for coversheet generation)

if (!class_exists($classpath)) {
return 'Class ' . $classpath . ' not found.';
}
<<<<<<< HEAD
=======
\local_debugger\performance\debugger::print_debug('test', 'get_coversheet', 'POS 6');
>>>>>>> 493386a (EDEKAN-564: Some adjustments for coversheet generation)

$class = new $classpath();
if (!method_exists($class, $method)) {
return 'Placeholder for ' . $method . ' not found.';
}
<<<<<<< HEAD
=======
\local_debugger\performance\debugger::print_debug('test', 'get_coversheet', '\n' . $classpath);
\local_debugger\performance\debugger::print_debug('test', 'get_coversheet', '\n' . $method);
>>>>>>> 493386a (EDEKAN-564: Some adjustments for coversheet generation)

return $class::$method($params);
}

/**
* Get all possible placeholders in a mustache context format.
*
* @return array
*/
public static function get_possible_placeholders(): array {
global $CFG;
$placeholders = [];
$dir = $CFG->dirroot . "/mod/quiz/report/archiver/classes/coversheet/placeholder";
$basenames = self::get_all_files_in_directory($dir);
$i = 1;
foreach ($basenames as $basename) {
$active = '';
$show = '';
if($i == 1) {
$show = 'show';
$active = 'active';
}
$placeholders[] = [
'placeholders' => self::get_placeholders($basename, "\quiz_archiver\coversheet\placeholder\\$basename"),
'metadata' => [
'tabid' => 'qa_' . $basename . '_tab',
'tab' => get_string($basename, 'quiz_archiver'),
'active' => $active,
'show' => $show,
],
];

$i++;
}

return $placeholders;
}

/**
* Get the array of the placeholders.
*
* @param string $basename
* @param string $classname
* @return array
*/
private static function get_placeholders(string $basename, string $classname): array {
$methods = get_class_methods($classname);
$placeholders = [];
foreach ($methods as $method) {
$placeholders[] = $basename . "_" . $method;
}
return $placeholders;
}

/**
* Get all basenames of files in a specific directory
*
* @param string $dir
* @return array
* @throws coding_exception
*/
private static function get_all_files_in_directory(string $dir): array {
$files = scandir($dir);
$basenames = [];
foreach ($files as $file) {
if (is_file($dir . '/' . $file)) {
$basenames[] = basename($file, '.php');
}
}
return $basenames;
}
}
Loading

0 comments on commit 7fe5299

Please sign in to comment.