Skip to content

Commit 6b0c51a

Browse files
committed
Merge branch 'MDL-81781' of https://github.com/paulholden/moodle
2 parents 50b0cd3 + 2eb7db3 commit 6b0c51a

File tree

5 files changed

+42
-22
lines changed

5 files changed

+42
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
issueNumber: MDL-81781
2+
notes:
3+
core:
4+
- message: >
5+
The `\core\dataformat::get_format_instance` method is now public, and
6+
can be used to retrieve a writer instance for a given dataformat
7+
type: changed

lib/classes/dataformat.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
namespace core;
2626

2727
use coding_exception;
28+
use core\dataformat\base;
2829
use core_php_time_limit;
2930
use stored_file;
3031

@@ -41,15 +42,15 @@ class dataformat {
4142
* Return an instance of a dataformat writer from given dataformat type
4243
*
4344
* @param string $dataformat
44-
* @return dataformat\base
45-
* @throws coding_exception
45+
* @return base
46+
*
47+
* @throws coding_exception For unknown dataformat
4648
*/
47-
protected static function get_format_instance(string $dataformat): \core\dataformat\base {
49+
public static function get_format_instance(string $dataformat): base {
4850
$classname = 'dataformat_' . $dataformat . '\writer';
4951
if (!class_exists($classname)) {
5052
throw new coding_exception('Invalid dataformat', $dataformat);
5153
}
52-
5354
return new $classname();
5455
}
5556

lib/tablelib.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
*/
5252
define('TABLE_SHOW_ALL_PAGE_SIZE', 5000);
5353

54+
use core\dataformat;
5455
use core_table\local\filter\filterset;
5556

5657
/**
@@ -2315,11 +2316,7 @@ public function __construct(&$table, $dataformat) {
23152316
throw new coding_exception("Output can not be buffered before instantiating table_dataformat_export_format");
23162317
}
23172318

2318-
$classname = 'dataformat_' . $dataformat . '\writer';
2319-
if (!class_exists($classname)) {
2320-
throw new coding_exception("Unable to locate dataformat/$dataformat/classes/writer.php");
2321-
}
2322-
$this->dataformat = new $classname;
2319+
$this->dataformat = dataformat::get_format_instance($dataformat);
23232320

23242321
// The dataformat export time to first byte could take a while to generate...
23252322
set_time_limit(0);

lib/tests/dataformat_test.php

+21-11
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,45 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

17-
/**
18-
* Tests for the dataformat plugins
19-
*
20-
* @package core
21-
* @copyright 2020 Paul Holden <[email protected]>
22-
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23-
*/
24-
2517
namespace core;
2618

19+
use coding_exception;
2720
use context_system;
2821
use core_component;
2922

3023
/**
31-
* Dataformat tests
24+
* Tests for the dataformat plugins
3225
*
3326
* @package core
3427
* @covers \core\dataformat
3528
* @copyright 2020 Paul Holden <[email protected]>
3629
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3730
*/
38-
class dataformat_test extends \advanced_testcase {
31+
final class dataformat_test extends \advanced_testcase {
32+
33+
/**
34+
* Test getting writer instance for given dataformat
35+
*/
36+
public function test_get_format_instance(): void {
37+
$instance = dataformat::get_format_instance('pdf');
38+
$this->assertInstanceOf(\dataformat_pdf\writer::class, $instance);
39+
}
40+
41+
/**
42+
* Test getting writer instance for invalid dataformat
43+
*/
44+
public function test_get_format_instance_invalid(): void {
45+
$this->expectException(coding_exception::class);
46+
$this->expectExceptionMessage('Invalid dataformat (weird)');
47+
dataformat::get_format_instance('weird');
48+
}
3949

4050
/**
4151
* Data provider to return array of dataformat types
4252
*
4353
* @return array
4454
*/
45-
public function write_data_provider(): array {
55+
public static function write_data_provider(): array {
4656
$data = [];
4757

4858
$dataformats = core_component::get_plugin_list('dataformat');

report/log/classes/table_log.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2323
*/
2424

25+
use core\dataformat;
2526
use core\report_helper;
2627

2728
defined('MOODLE_INTERNAL') || die;
29+
2830
global $CFG;
2931
require_once($CFG->libdir . '/tablelib.php');
3032

@@ -299,8 +301,11 @@ public function col_eventname($event) {
299301
* @return string HTML for the description column
300302
*/
301303
public function col_description($event) {
302-
// Description.
303-
return format_text($event->get_description(), FORMAT_PLAIN);
304+
if (empty($this->download) || dataformat::get_format_instance($this->download)->supports_html()) {
305+
return format_text($event->get_description(), FORMAT_PLAIN);
306+
} else {
307+
return $event->get_description();
308+
}
304309
}
305310

306311
/**

0 commit comments

Comments
 (0)