MDL-81274 mod_data: observe current approved value on record import.

This commit is contained in:
Paul Holden
2024-04-22 12:48:56 +01:00
parent 143c2cd5be
commit e48ff65880
5 changed files with 89 additions and 14 deletions
@@ -133,7 +133,15 @@ class csv_entries_importer extends entries_importer {
$authorid = $author->id;
}
}
if ($recordid = data_add_record($data, 0, $authorid)) { // Add instance to data_record.
// Determine presence of "approved" field within the record to import.
$approved = true;
if (array_key_exists(get_string('approved', 'data'), $fieldnames)) {
$approvedindex = $fieldnames[get_string('approved', 'data')];
$approved = !empty($record[$approvedindex]);
}
if ($recordid = data_add_record($data, 0, $authorid, $approved)) { // Add instance to data_record.
foreach ($fields as $field) {
$fieldid = $fieldnames[$field->field->name];
if (isset($record[$fieldid])) {
+4 -2
View File
@@ -1110,9 +1110,11 @@ function data_numentries($data, $userid=null) {
* @param object $data
* @param int $groupid
* @param int $userid
* @param bool $approved If specified, and the user has the capability to approve entries, then this value
* will be used as the approved status of the new record
* @return bool
*/
function data_add_record($data, $groupid = 0, $userid = null) {
function data_add_record($data, $groupid = 0, $userid = null, bool $approved = true) {
global $USER, $DB;
$cm = get_coursemodule_from_instance('data', $data->id);
@@ -1124,7 +1126,7 @@ function data_add_record($data, $groupid = 0, $userid = null) {
$record->groupid = $groupid;
$record->timecreated = $record->timemodified = time();
if (has_capability('mod/data:approve', $context)) {
$record->approved = 1;
$record->approved = $approved;
} else {
$record->approved = 0;
}
+70 -11
View File
@@ -27,10 +27,12 @@ use zip_archive;
*
* @package mod_data
* @category test
* @covers \mod_data\local\importer\entries_importer
* @covers \mod_data\local\importer\csv_entries_importer
* @copyright 2019 Tobias Reischmann
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class entries_import_test extends \advanced_testcase {
final class entries_import_test extends \advanced_testcase {
/**
* Set up function.
@@ -271,11 +273,74 @@ class entries_import_test extends \advanced_testcase {
}
/**
* Tests the import including files from a zip archive.
* Data provider for {@see test_import_without_approved}
*
* @covers \mod_data\local\importer\entries_importer
* @covers \mod_data\local\importer\csv_entries_importer
* @return void
* @return array[]
*/
public static function import_without_approved_provider(): array {
return [
'Teacher can approve entries' => ['teacher', [1, 1]],
'Student cannot approve entries' => ['student', [0, 0]],
];
}
/**
* Test importing file without approved status column
*
* @param string $user
* @param int[] $expected
*
* @dataProvider import_without_approved_provider
*/
public function test_import_without_approved(string $user, array $expected): void {
$testdata = $this->get_test_data();
['data' => $data, 'cm' => $cm] = $testdata;
$this->setUser($testdata[$user]);
$importer = new csv_entries_importer(__DIR__ . '/fixtures/test_data_import.csv', 'test_data_import.csv');
$importer->import_csv($cm, $data, 'UTF-8', 'comma');
$records = $this->get_data_records($data->id);
$this->assertEquals($expected, array_column($records, 'approved'));
}
/**
* Data provider for {@see test_import_with_approved}
*
* @return array[]
*/
public static function import_with_approved_provider(): array {
return [
'Teacher can approve entries' => ['teacher', [1, 0]],
'Student cannot approve entries' => ['student', [0, 0]],
];
}
/**
* Test importing file with approved status column
*
* @param string $user
* @param int[] $expected
*
* @dataProvider import_with_approved_provider
*/
public function test_import_with_approved(string $user, array $expected): void {
$testdata = $this->get_test_data();
['data' => $data, 'cm' => $cm] = $testdata;
$this->setUser($testdata[$user]);
$importer = new csv_entries_importer(__DIR__ . '/fixtures/test_data_import_with_approved.csv',
'test_data_import_with_approved.csv');
$importer->import_csv($cm, $data, 'UTF-8', 'comma');
$records = $this->get_data_records($data->id);
$this->assertEquals($expected, array_column($records, 'approved'));
}
/**
* Tests the import including files from a zip archive.
*/
public function test_import_with_files(): void {
[
@@ -323,10 +388,6 @@ class entries_import_test extends \advanced_testcase {
/**
* Tests the import including files from a zip archive.
*
* @covers \mod_data\local\importer\entries_importer
* @covers \mod_data\local\importer\csv_entries_importer
* @return void
*/
public function test_import_with_files_missing_file(): void {
[
@@ -377,8 +438,6 @@ class entries_import_test extends \advanced_testcase {
/**
* Tests if the amount of imported records is counted properly.
*
* @covers \mod_data\local\importer\csv_entries_importer::import_csv
* @covers \mod_data\local\importer\csv_entries_importer::get_added_records_messages
* @dataProvider get_added_record_messages_provider
* @param string $datafilecontent the content of the datafile to test as string
* @param int $expectedcount the expected count of messages depending on the datafile content
@@ -0,0 +1,3 @@
ID,Param2,Approved
1,"My first entry",1
2,"My second entry",0
1 ID Param2 Approved
2 1 My first entry 1
3 2 My second entry 0
+3
View File
@@ -1,6 +1,9 @@
This files describes API changes in /mod/data - plugins,
information provided here is intended especially for developers.
== 4.3.5 ==
* The `data_add_record` method accepts a new `$approved` parameter to set the corresponding state of the new record
== 4.3 ==
* Function data_export_xls() has been deprecated and moved to deprecatedlib, because xls support has already been dropped.
* Functions data_export_csv(), data_export_ods() and data_get_exportdata() have been deprecated due to a bigger