Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippImhof committed Sep 7, 2024
1 parent d9536b2 commit 53c6817
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
9 changes: 8 additions & 1 deletion essaydownload_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,19 @@ protected function pdf_layout_fields(MoodleQuickForm $mform) {
$mform->addHelpButton('fontsize', 'fontsize', 'quiz_essaydownload');
}

/**
* Validation of our settings form, e. g. font size or page margins.
*
* @param array $data submitted data in form ['fieldname' => value]
* @param array $files array of uploaded files ['element_name' => tmp_file_path]
* @return array errors in form ['element_name' => 'error message'] or [] if no errors
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);

// No further validation to be done if using plain text format.
if ($data['fileformat'] === 'txt') {
return;
return $errors;
}

$margins = [$data['marginleft'], $data['marginright'], $data['margintop'], $data['marginbottom']];
Expand Down
21 changes: 18 additions & 3 deletions report.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ protected function process_and_download(): void {
* @param string $type the notification type, e. g. 'error' or 'info' or 'warn'
* @return void
*/
protected function notification(string $message, string $type = 'error') {
protected function notification(string $message, string $type = 'error'): void {
global $OUTPUT;

// Printing the standard header. We'll set $hasquestions and $hasstudents to true here,
Expand Down Expand Up @@ -486,7 +486,16 @@ protected static function clean_filename(string $filename): string {
return clean_filename(str_replace(' ', '_', $filename));
}

protected function generate_pdf(string $text, string $header = '', string $subheader = '', string $author = '') {
/**
* Generate a PDF file from a given HTML code.
*
* @param string $text HTML code to be typeset
* @param string $header upper line of the header, printed in bold face
* @param string $subheader lower line of the header
* @param string $author author name to be stored in the document information field
* @return string PDF code
*/
protected function generate_pdf(string $text, string $header = '', string $subheader = '', string $author = ''): string {
// The text might contain \xC2\xA0 for a unicode NON-BREAK SPACE character. This can confuse TCPDF, so we
// rather remove it here.
$text = str_replace("\xc2\xa0", " ", $text);
Expand All @@ -498,11 +507,17 @@ protected function generate_pdf(string $text, string $header = '', string $subhe
$doc->SetTitle('');
$doc->SetKeywords('');
$doc->SetSubject('');

// The configured top margin is used for the distance between the page's top border and the start of the header.
$doc->setHeaderMargin($this->options->margintop);

// In order for the document's text to be reasonably separated from the header (and its rule), we add some space
// relative to linespacing and font size.
$doc->SetMargins($this->options->marginleft, $this->options->margintop + $this->options->linespacing * $this->options->fontsize, $this->options->marginright);
$doc->SetMargins(
$this->options->marginleft,
$this->options->margintop + $this->options->linespacing * $this->options->fontsize,
$this->options->marginright
);
$doc->setPrintFooter(false);

if ($this->options->font === 'serif') {
Expand Down
3 changes: 1 addition & 2 deletions tests/report_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
require_once($CFG->dirroot . '/mod/quiz/report/essaydownload/tests/helper.php');
require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');


/**
* Tests for Essay responses downloader plugin (quiz_essaydownload)
*
Expand Down Expand Up @@ -128,7 +127,7 @@ public function test_long_names_being_shortened(): void {
$student = \phpunit_util::get_data_generator()->create_user(
[
'firstname' => 'ExtremelyLongFirstNameForThisVerySpecificPerson',
'lastname' => 'OneThingIsSureThisLastNameIsNotGoingToEndVerySoon'
'lastname' => 'OneThingIsSureThisLastNameIsNotGoingToEndVerySoon',
]
);
\phpunit_util::get_data_generator()->enrol_user($student->id, $course->id, 'student');
Expand Down

0 comments on commit 53c6817

Please sign in to comment.