Merge branch 'MDL-81274-404' of https://github.com/paulholden/moodle into MOODLE_404_STABLE

This commit is contained in:
Ilya Tregubov 2024-04-24 09:44:17 +08:00
commit 16f7008c68
5 changed files with 89 additions and 14 deletions

View File

@ -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])) {

View File

@ -1156,9 +1156,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);
@ -1170,7 +1172,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;
}

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

View File

@ -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

View File

@ -1,6 +1,9 @@
This files describes API changes in /mod/data - plugins,
information provided here is intended especially for developers.
=== 4.4.1 ===
* The `data_add_record` method accepts a new `$approved` parameter to set the corresponding state of the new record
=== 4.4 ===
* The following behat steps are now deprecated. Use data generators instead:
- I add a ":fieldtype" field to ":activityname" database and I fill the form with: