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

Add option for footer with page number #26

Merged
merged 3 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions classes/customTCPDF.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.

namespace quiz_essaydownload;

use pdf;

defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/pdflib.php');

/**
* Override TCPDF in order to have a custom footer that includes the page number.
*
* @package quiz_essaydownload
* @copyright 2024 Philipp Imhof
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class customTCPDF extends pdf {
/** @var int footer's distance from bottom of page in millimeters */
const FOOTER_POSITION = 15;

// @codingStandardsIgnoreLine
public function Footer() {
$this->SetY(-self::FOOTER_POSITION);

// We cannot use getAliasNumPage(), because there seems to be a bug in TCPDF that will cause the
// footer to be badly centered. The same is true for getAliasNbPages(), but we don't need that
// here.
if (empty($this->pagegroups)) {
$pageno = $this->PageNo();
} else {
$pageno = $this->getGroupPageNo();
}

$this->Cell(0, 10, get_string('pagenumber', 'quiz_essaydownload', $pageno), 'T', 0, 'C');
}

}
8 changes: 8 additions & 0 deletions essaydownload_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ protected function pdf_layout_fields(MoodleQuickForm $mform) {
$mform->addGroup($margingroup, 'margingroup', get_string('margins', 'quiz_essaydownload'), ' ', false);
$mform->disabledIf('margingroup', 'fileformat', 'neq', 'pdf');

$mform->addElement(
'advcheckbox',
'includefooter',
get_string('footer', 'quiz_essaydownload'),
get_string('includefooter', 'quiz_essaydownload')
);
$mform->disabledIf('includefooter', 'fileformat', 'neq', 'pdf');

$mform->addElement('select', 'linespacing', get_string('linespacing', 'quiz_essaydownload'), [
'1' => get_string('linesingle', 'quiz_essaydownload'),
'1.5' => get_string('lineoneandhalf', 'quiz_essaydownload'),
Expand Down
6 changes: 6 additions & 0 deletions essaydownload_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class quiz_essaydownload_options extends quiz_essaydownload_options_parent_class
/** @var string how to organise the sub folders in the archive (by question or by attempt) */
public $groupby = 'byattempt';

/** @var bool whether a footer containing the page number should be added to PDFs */
public $includefooter = false;

/** @var float line spacing for PDF export */
public $linespacing = 1;

Expand Down Expand Up @@ -118,6 +121,7 @@ public function get_initial_form_data() {
$toform->font = $this->font;
$toform->fontsize = $this->fontsize;
$toform->groupby = $this->groupby;
$toform->includefooter = $this->includefooter;
$toform->linespacing = $this->linespacing;
$toform->marginbottom = $this->marginbottom;
$toform->marginleft = $this->marginleft;
Expand All @@ -144,6 +148,7 @@ public function setup_from_form_data($fromform): void {
$this->font = $fromform->font ?? '';
$this->fontsize = $fromform->fontsize ?? '';
$this->groupby = $fromform->groupby;
$this->includefooter = $fromform->includefooter;
$this->linespacing = $fromform->linespacing ?? '';
$this->marginbottom = $fromform->marginbottom ?? '';
$this->marginleft = $fromform->marginleft ?? '';
Expand All @@ -166,6 +171,7 @@ public function setup_from_params() {
$this->font = optional_param('font', $this->font, PARAM_ALPHA);
$this->fontsize = optional_param('fontsize', $this->fontsize, PARAM_INT);
$this->groupby = optional_param('groupby', $this->groupby, PARAM_ALPHA);
$this->includefooter = optional_param('includefooter', $this->includefooter, PARAM_BOOL);
$this->linespacing = optional_param('linespacing', $this->linespacing, PARAM_FLOAT);
$this->marginbottom = optional_param('marginbottom', $this->marginbottom, PARAM_INT);
$this->marginleft = optional_param('marginleft', $this->marginleft, PARAM_INT);
Expand Down
3 changes: 3 additions & 0 deletions lang/en/quiz_essaydownload.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@
$string['fontserif'] = 'Serif';
$string['fontsize'] = 'Font size (points)';
$string['fontsize_help'] = 'Note that when using the original HTML formatted text, the actual font size may still be different, according to the formatting';
$string['footer'] = 'Footer';
$string['generaloptions'] = 'General options';
$string['groupby'] = 'Group by';
$string['groupby_help'] = 'The archive can be structured by question or by attempt:<ul><li>If you group by question, the archive will have a folder for every question. Inside each folder, you will have a folder for every attempt.</li><li>If you group by attempt, the archive will have a folder for every attempt. Inside each folder, you will have a folder for every question.</li></ul>';
$string['includeattachments'] = 'Also download possible attachments included in a student\'s answer.';
$string['includeattachments_help'] = 'Any attachment is provided as-is. Please note that attachments might contain malware.';
$string['includefooter'] = 'Add footer with page number to each page.';
$string['includequestiontext'] = 'Also include question text.';
$string['includequestiontext_help'] = 'Including the question text might be useful if your quiz uses random questions.';
$string['lastfirst'] = 'Last name - First name';
Expand All @@ -64,6 +66,7 @@
$string['page'] = 'Page format';
$string['pagea4'] = 'A4';
$string['pageletter'] = 'Letter';
$string['pagenumber'] = 'Page {$a}';
$string['pdfoptions'] = 'PDF settings';
$string['plugindescription'] = 'Download text answers and attachment files submitted in response to essay questions in a quiz.';
$string['pluginname'] = 'Essay responses downloader plugin (quiz_essaydownload)';
Expand Down
20 changes: 15 additions & 5 deletions report.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use core_files\archive_writer;
use core\dml\sql_join;
use quiz_essaydownload\customTCPDF;

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

Expand All @@ -40,6 +41,7 @@ class_alias('\quiz_attempts_report', '\quiz_essaydownload_report_parent_alias');
class_alias('\quiz_attempt', '\quiz_essaydownload_quiz_attempt_alias');
}

require_once($CFG->dirroot . '/mod/quiz/report/essaydownload/classes/customTCPDF.php');
require_once($CFG->dirroot . '/mod/quiz/report/essaydownload/essaydownload_form.php');
require_once($CFG->dirroot . '/mod/quiz/report/essaydownload/essaydownload_options.php');
require_once($CFG->libdir . '/pdflib.php');
Expand Down Expand Up @@ -505,7 +507,7 @@ protected function generate_pdf(string $text, string $header = '', string $subhe
$text = $this->workaround_atto_font_size_issue($text);
}

$doc = new pdf('P', 'mm', $this->options->pageformat);
$doc = new customTCPDF('P', 'mm', $this->options->pageformat);

$doc->SetCreator('quiz_essaydownload plugin for Moodle LMS');
$doc->SetAuthor($author);
Expand All @@ -523,7 +525,6 @@ protected function generate_pdf(string $text, string $header = '', string $subhe
$this->options->margintop + $this->options->linespacing * $this->options->fontsize,
$this->options->marginright
);
$doc->setPrintFooter(false);

if ($this->options->font === 'serif') {
$fontname = 'freeserif';
Expand All @@ -534,10 +535,19 @@ protected function generate_pdf(string $text, string $header = '', string $subhe
}
$doc->SetFont($fontname, '', $this->options->fontsize);
$doc->setHeaderFont([$fontname, '', $this->options->fontsize]);

$doc->setHeaderData('', 0, $header, $subheader);
$doc->setFooterData();
$doc->SetAutoPageBreak(true, $this->options->marginbottom);

// The user may choose to add a footer to each page, by default, there is none and thus no
// need for additional space.
$doc->setPrintFooter(false);
$spaceforfooter = 0;
if ($this->options->includefooter) {
$doc->setPrintFooter(true);
$spaceforfooter = customTCPDF::FOOTER_POSITION;
// Using 80% of the base font size seems good.
$doc->setFooterFont([$fontname, '', round(0.8 * $this->options->fontsize)]);
}
$doc->SetAutoPageBreak(true, $this->options->marginbottom + $spaceforfooter);

$doc->AddPage();
$linespacebase = 1.25;
Expand Down
1 change: 1 addition & 0 deletions tests/behat/form.feature
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Feature: Validation and display of the form
And the "marginright" "field" should be disabled
And the "margintop" "field" should be disabled
And the "marginbottom" "field" should be disabled
And the "includefooter" "field" should be disabled
And the "linespacing" "select" should be disabled
And the "font" "select" should be disabled
And the "fontsize" "field" should be disabled
Expand Down
Loading