Skip to content

Commit

Permalink
to squash
Browse files Browse the repository at this point in the history
  • Loading branch information
PM84 committed Mar 10, 2024
1 parent 328606d commit 94414eb
Show file tree
Hide file tree
Showing 8 changed files with 452 additions and 8 deletions.
2 changes: 1 addition & 1 deletion classes/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ public function generate_full_page(int $attemptid, array $sections, bool $fix_re
// Build HTML tree
$html = "";
$html .= $OUTPUT->header();
// $html .= \quiz_archiver\local\coversheet\create_coversheet::get_coversheet($attemptid);
$html .= \quiz_archiver\coversheet\create_coversheet::get_coversheet($attemptid);
$html .= self::generate($attemptid, $sections);
$html .= $OUTPUT->footer();

Expand Down
136 changes: 132 additions & 4 deletions classes/coversheet/create_coversheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace quiz_archiver\local\coversheet;
namespace quiz_archiver\coversheet;

use admin_setting_heading;
use coding_exception;
use quiz_archiver\external\get_attempts_metadata;

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

Expand All @@ -40,19 +42,145 @@
class create_coversheet {

/**
* Create the coversheet. Only this method should be called from outside.
* Create the coversheet.
* @param int $attemptid
* @return string
*/
public static function get_coversheet(int $attemptid): string {

$config = get_config('quiz_archiver');

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

return $config->dynamic_pdf_content;
// 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);

// Now replace all placeholders.
$html = $config->dynamic_pdf_content;
foreach ($matches[0] as $key => $placeholder) {
$classpath = '\quiz_archiver\coversheet\placeholder\\' . $matches[1][$key];
$method = $matches[2][$key];
$replacement = self::check_class_and_method($classpath, $method, $attemptmetadata);
$html = preg_replace('/' . $placeholder . '/', $replacement, $html);
}

\local_debugger\performance\debugger::print_debug('test', 'get_coversheet', $html);
return '<div style="page-break-after: always;">' . $config->dynamic_pdf_content . '</div>';
}

/**
* 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
*/
private static function check_class_and_method(string $classpath, string $method, string|int|object|array $params): string {

if (!class_exists($classpath)) {
return 'Class ' . $classpath . ' not found.';
}

$class = new $classpath();
if (!method_exists($class, $method)) {
return 'Placeholder for ' . $method . ' not found.';
}

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);
foreach ($basenames as $basename) {
$placeholders[] = [
'placeholders' => self::get_placeholders($basename, "\quiz_archiver\coversheet\placeholder\\$basename"),
'metadata' => [
'tabid' => 'qa_' . $basename . '_tab',
'tab' => get_string($basename, 'quiz_archiver'),
],
];
}

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;
}
}
101 changes: 101 additions & 0 deletions classes/coversheet/placeholder/course.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?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 course callback to get corse info.
*
* @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\placeholder;

use dml_exception;

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

/**
* Handles course callback to get corse info.
*
* @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 course {

/**
* Get course fullname.
* @param object $params
* @return string
*/
public static function fullname(object $params): string {
$course = get_course($params->courseid);
return $course->fullname;
}

/**
* Get course shortname.
* @param object $params
* @return string
*/
public static function shortname(object $params): string {
$course = get_course($params->courseid);
return $course->shortname;
}

/**
* Get course summary.
* @param object $params
* @return string
*/
public static function summary(object $params): string {
$course = get_course($params->courseid);
return $course->summary;
}

/**
* Get course format.
* @param object $params
* @return string
*/
public static function format(object $params): string {
$course = get_course($params->courseid);
return $course->format;
}

/**
* Get course startdate.
* @param object $params
* @return string
*/
public static function startdate(object $params): string {
$course = get_course($params->courseid);
return $course->startdate;
}

/**
* Get course enddate.
* @param object $params
* @return string
*/
public static function enddate(object $params): string {
$course = get_course($params->courseid);
return $course->enddate;
}
}
Loading

0 comments on commit 94414eb

Please sign in to comment.