Skip to content

Commit

Permalink
Merge branch 'MDL-81365-main' of https://github.com/ilyatregubov/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
HuongNV13 committed Mar 28, 2024
2 parents 1320a2a + 6f41a2f commit 61c8cda
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 28 deletions.
10 changes: 3 additions & 7 deletions enrol/cohort/lib.php
Expand Up @@ -620,19 +620,15 @@ public function fill_enrol_custom_fields(array $enrolmentdata, int $courseid): a
* @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)) {
return new lang_string('contextcohortnotallowed', 'cohort', $enrolmentdata['cohortidnumber']);
$error = new lang_string('contextcohortnotallowed', 'cohort', $enrolmentdata['cohortidnumber']);
}
}
$enrolmentdata += [
'customint1' => null,
'customint2' => null,
'roleid' => 0,
];
return parent::validate_plugin_data_context($enrolmentdata, $courseid);
return $error;
}

/**
Expand Down
22 changes: 1 addition & 21 deletions lib/enrollib.php
Expand Up @@ -3548,33 +3548,13 @@ public function validate_enrol_plugin_data(array $enrolmentdata, ?int $courseid
* This is called from the tool_uploadcourse if the plugin supports instance creation in
* upload course ({@see self::is_csv_upload_supported()})
*
* The fallback is to call the edit_instance_validation() but it will be better if the plugins
* implement this method to return better error messages.
* Override it if plugin can validate provided data in relevant context.
*
* @param array $enrolmentdata enrolment data to validate.
* @param int|null $courseid Course ID.
* @return lang_string|null Error
*/
public function validate_plugin_data_context(array $enrolmentdata, ?int $courseid = null): ?lang_string {
global $DB;

if ($courseid) {
$enrolmentdata += ['courseid' => $courseid, 'id' => 0, 'status' => ENROL_INSTANCE_ENABLED];
$instance = (object)[
'id' => null,
'courseid' => $courseid,
'status' => $enrolmentdata['status'],
'type' => $this->get_name(),
];
if (array_key_exists('role', $enrolmentdata)) {
$instance->roleid = $DB->get_field('role', 'id', ['shortname' => $enrolmentdata['role']]);
}
$formerrors = $this->edit_instance_validation($enrolmentdata, [], $instance, context_course::instance($courseid));
if (!empty($formerrors)) {
$errors = array_map(fn($key) => "{$key}: {$formerrors[$key]}", array_keys($formerrors));
return new lang_string('errorcannotcreateorupdateenrolment', 'tool_uploadcourse', $errors);
}
}
return null;
}

Expand Down

0 comments on commit 61c8cda

Please sign in to comment.