MDL-43056 tool_uploadcourse: Add capability to upload courses from file

An entrypoint capability has been added that allows accessing the
upload tool. Further relevant capability checks are then performed
depending on the action being taken during the upload process.

Co-authored-by: Marina Glancy <marina@moodle.com>
This commit is contained in:
David Woloszyn 2024-01-31 11:29:33 +11:00
parent f30110b5eb
commit 4807a4dd5f
27 changed files with 717 additions and 53 deletions

View file

@ -620,15 +620,19 @@ class enrol_cohort_plugin extends enrol_plugin {
* @return lang_string|null Error
*/
public function validate_plugin_data_context(array $enrolmentdata, ?int $courseid = null) : ?lang_string {
$error = null;
if (isset($enrolmentdata['customint1'])) {
$cohortid = $enrolmentdata['customint1'];
$coursecontext = \context_course::instance($courseid);
if (!cohort_get_cohort($cohortid, $coursecontext)) {
$error = new lang_string('contextcohortnotallowed', 'cohort', $enrolmentdata['cohortidnumber']);
return new lang_string('contextcohortnotallowed', 'cohort', $enrolmentdata['cohortidnumber']);
}
}
return $error;
$enrolmentdata += [
'customint1' => null,
'customint2' => null,
'roleid' => 0,
];
return parent::validate_plugin_data_context($enrolmentdata, $courseid);
}
/**

View file

@ -246,7 +246,11 @@ class lib_test extends \advanced_testcase {
$enrolmentdata = [
'customint1' => $cohort1->id,
'cohortidnumber' => $cohort1->idnumber,
'courseid' => $course->id,
'id' => null,
'status' => ENROL_INSTANCE_ENABLED,
];
$enrolmentdata = $cohortplugin->fill_enrol_custom_fields($enrolmentdata, $course->id);
$error = $cohortplugin->validate_plugin_data_context($enrolmentdata, $course->id);
$this->assertNull($error);
}
@ -313,6 +317,7 @@ class lib_test extends \advanced_testcase {
*/
public function test_validate_enrol_plugin_data() {
$this->resetAfterTest();
$this->setAdminUser();
$cat = $this->getDataGenerator()->create_category();
$cat1 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);

View file

@ -519,6 +519,16 @@ class enrol_guest_plugin extends enrol_plugin {
return $instance;
}
/**
* Fill custom fields data for a given enrolment plugin.
*
* @param array $enrolmentdata enrolment data.
* @param int $courseid Course ID.
* @return array Updated enrolment data with custom fields info.
*/
public function fill_enrol_custom_fields(array $enrolmentdata, int $courseid): array {
return $enrolmentdata + ['password' => ''];
}
}
/**

View file

@ -675,6 +675,20 @@ class enrol_manual_plugin extends enrol_plugin {
}
return $instance;
}
/**
* Fill custom fields data for a given enrolment plugin.
*
* @param array $enrolmentdata enrolment data.
* @param int $courseid Course ID.
* @return array Updated enrolment data with custom fields info.
*/
public function fill_enrol_custom_fields(array $enrolmentdata, int $courseid): array {
return $enrolmentdata + [
'expirynotify' => 0,
'expirythreshold' => 0,
];
}
}
/**

View file

@ -468,7 +468,10 @@ class enrol_meta_plugin extends enrol_plugin {
} else if (isset($enrolmentdata['groupname'])) {
$enrolmentdata['customint2'] = groups_get_group_by_name($courseid, $enrolmentdata['groupname']);
}
return $enrolmentdata;
return $enrolmentdata + [
'customint1' => null,
'customint2' => null,
];
}
/**

View file

@ -1108,13 +1108,13 @@ class plugin_test extends \advanced_testcase {
$enrolmentdata = $metaplugin->fill_enrol_custom_fields($enrolmentdata, $course1->id);
$this->assertArrayHasKey('customint1', $enrolmentdata);
$this->assertEquals($course2->id, $enrolmentdata['customint1']);
$this->assertArrayNotHasKey('customint2', $enrolmentdata);
$this->assertNull($enrolmentdata['customint2']);
$enrolmentdata['metacoursename'] = 'notexist';
$enrolmentdata = $metaplugin->fill_enrol_custom_fields($enrolmentdata, $course1->id);
$this->assertArrayHasKey('customint1', $enrolmentdata);
$this->assertFalse($enrolmentdata['customint1']);
$this->assertArrayNotHasKey('customint2', $enrolmentdata);
$this->assertNull($enrolmentdata['customint2']);
$enrolmentdata['metacoursename'] = $course2->shortname;

View file

@ -1110,6 +1110,16 @@ class enrol_self_plugin extends enrol_plugin {
return $instance;
}
/**
* Fill custom fields data for a given enrolment plugin.
*
* @param array $enrolmentdata enrolment data.
* @param int $courseid Course ID.
* @return array Updated enrolment data with custom fields info.
*/
public function fill_enrol_custom_fields(array $enrolmentdata, int $courseid): array {
return $enrolmentdata + ['password' => ''];
}
}
/**