Merge branch 'MDL-45301_master_v3' of https://github.com/TomoTsuyuki/moodle

This commit is contained in:
Jun Pataleta 2023-02-20 10:15:17 +08:00
commit b6432d5475
19 changed files with 160 additions and 9 deletions

View file

@ -105,6 +105,7 @@
<FIELD NAME="originalcourseid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="the id of the source course when a new course originates from a restore of another course on the same site."/>
<FIELD NAME="showactivitydates" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Whether to display activity dates to user. 0 = do not display, 1 = display activity dates"/>
<FIELD NAME="showcompletionconditions" TYPE="int" LENGTH="1" NOTNULL="false" SEQUENCE="false" COMMENT="Whether to display completion conditions to user. 0 = do not display, 1 = display conditions"/>
<FIELD NAME="pdfexportfont" TYPE="char" LENGTH="50" NOTNULL="false" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>

View file

@ -2984,5 +2984,19 @@ privatefiles,moodle|/user/files.php';
upgrade_main_savepoint(true, 2023020800.00);
}
if ($oldversion < 2023021700.01) {
// Define field pdfexportfont to be added to course.
$table = new xmldb_table('course');
$field = new xmldb_field('pdfexportfont', XMLDB_TYPE_CHAR, '50', null, false, false, null, 'showcompletionconditions');
// Conditionally launch add field pdfexportfont.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2023021700.01);
}
return true;
}

View file

@ -250,4 +250,32 @@ class pdf extends TCPDF {
return $families;
}
/**
* Get font list from config.
* @return array|string[]
*/
public function get_export_fontlist(): array {
global $CFG;
$fontlist = [];
if (!empty($CFG->pdfexportfont)) {
if (is_array($CFG->pdfexportfont)) {
$fontlist = $CFG->pdfexportfont;
} else {
$fontlist[$CFG->pdfexportfont] = $CFG->pdfexportfont;
}
}
// Verify fonts.
$availablefonts = $this->get_font_families();
foreach ($fontlist as $key => $value) {
if (empty($availablefonts[$key])) {
unset($fontlist[$key]);
}
}
if (empty($fontlist)) {
// Default font if there is no value set in CFG.
$fontlist = ['freesans' => 'FreeSans'];
}
return $fontlist;
}
}

View file

@ -66,4 +66,35 @@ class pdflib_test extends \advanced_testcase {
$this->assertGreaterThan(100000, strlen($res));
$this->assertLessThan(120000, strlen($res));
}
/**
* Test get_export_fontlist function.
*
* @covers ::get_export_fontlist
*
* @return void
*/
public function test_get_export_fontlist(): void {
global $CFG;
require_once($CFG->libdir.'/pdflib.php');
$this->resetAfterTest();
$pdf = new \pdf();
$fontlist = $pdf->get_export_fontlist();
$this->assertCount(1, $fontlist);
$this->assertArrayHasKey('freesans', $fontlist);
$CFG->pdfexportfont = [
'kozminproregular' => 'Kozmin Pro Regular',
'stsongstdlight' => 'STSong stdlight',
'invalidfont' => 'Invalid'
];
$fontlist = $pdf->get_export_fontlist();
$this->assertCount(2, $fontlist);
$this->assertArrayNotHasKey('freesans', $fontlist);
$this->assertArrayHasKey('kozminproregular', $fontlist);
$this->assertArrayHasKey('stsongstdlight', $fontlist);
$this->assertArrayNotHasKey('invalidfont', $fontlist);
}
}