This commit is contained in:
Huong Nguyen 2024-06-13 10:12:37 +07:00
commit 6b0c51a2ca
No known key found for this signature in database
GPG Key ID: 40D88AB693A3E72A
5 changed files with 42 additions and 22 deletions

View File

@ -0,0 +1,7 @@
issueNumber: MDL-81781
notes:
core:
- message: >
The `\core\dataformat::get_format_instance` method is now public, and
can be used to retrieve a writer instance for a given dataformat
type: changed

View File

@ -25,6 +25,7 @@
namespace core;
use coding_exception;
use core\dataformat\base;
use core_php_time_limit;
use stored_file;
@ -41,15 +42,15 @@ class dataformat {
* Return an instance of a dataformat writer from given dataformat type
*
* @param string $dataformat
* @return dataformat\base
* @throws coding_exception
* @return base
*
* @throws coding_exception For unknown dataformat
*/
protected static function get_format_instance(string $dataformat): \core\dataformat\base {
public static function get_format_instance(string $dataformat): base {
$classname = 'dataformat_' . $dataformat . '\writer';
if (!class_exists($classname)) {
throw new coding_exception('Invalid dataformat', $dataformat);
}
return new $classname();
}

View File

@ -51,6 +51,7 @@ define('TABLE_P_BOTTOM', 2);
*/
define('TABLE_SHOW_ALL_PAGE_SIZE', 5000);
use core\dataformat;
use core_table\local\filter\filterset;
/**
@ -2315,11 +2316,7 @@ class table_dataformat_export_format extends table_default_export_format_parent
throw new coding_exception("Output can not be buffered before instantiating table_dataformat_export_format");
}
$classname = 'dataformat_' . $dataformat . '\writer';
if (!class_exists($classname)) {
throw new coding_exception("Unable to locate dataformat/$dataformat/classes/writer.php");
}
$this->dataformat = new $classname;
$this->dataformat = dataformat::get_format_instance($dataformat);
// The dataformat export time to first byte could take a while to generate...
set_time_limit(0);

View File

@ -14,35 +14,45 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Tests for the dataformat plugins
*
* @package core
* @copyright 2020 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use coding_exception;
use context_system;
use core_component;
/**
* Dataformat tests
* Tests for the dataformat plugins
*
* @package core
* @covers \core\dataformat
* @copyright 2020 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class dataformat_test extends \advanced_testcase {
final class dataformat_test extends \advanced_testcase {
/**
* Test getting writer instance for given dataformat
*/
public function test_get_format_instance(): void {
$instance = dataformat::get_format_instance('pdf');
$this->assertInstanceOf(\dataformat_pdf\writer::class, $instance);
}
/**
* Test getting writer instance for invalid dataformat
*/
public function test_get_format_instance_invalid(): void {
$this->expectException(coding_exception::class);
$this->expectExceptionMessage('Invalid dataformat (weird)');
dataformat::get_format_instance('weird');
}
/**
* Data provider to return array of dataformat types
*
* @return array
*/
public function write_data_provider(): array {
public static function write_data_provider(): array {
$data = [];
$dataformats = core_component::get_plugin_list('dataformat');

View File

@ -22,9 +22,11 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core\dataformat;
use core\report_helper;
defined('MOODLE_INTERNAL') || die;
global $CFG;
require_once($CFG->libdir . '/tablelib.php');
@ -299,8 +301,11 @@ class report_log_table_log extends table_sql {
* @return string HTML for the description column
*/
public function col_description($event) {
// Description.
return format_text($event->get_description(), FORMAT_PLAIN);
if (empty($this->download) || dataformat::get_format_instance($this->download)->supports_html()) {
return format_text($event->get_description(), FORMAT_PLAIN);
} else {
return $event->get_description();
}
}
/**