mirror of
https://github.com/moodle/moodle.git
synced 2025-04-21 16:32:18 +02:00
MDL-40723 use will_have_course_capability() to verify future course creator capabilities when creating courses
This commit is contained in:
parent
a56ec91842
commit
dba745265f
@ -99,11 +99,14 @@ class course_edit_form extends moodleform {
|
||||
$mform->addElement('select', 'visible', get_string('visible'), $choices);
|
||||
$mform->addHelpButton('visible', 'visible');
|
||||
$mform->setDefault('visible', $courseconfig->visible);
|
||||
if (!has_capability('moodle/course:visibility', $context)) {
|
||||
$mform->hardFreeze('visible');
|
||||
if (!empty($course->id)) {
|
||||
if (!empty($course->id)) {
|
||||
if (!has_capability('moodle/course:visibility', $coursecontext)) {
|
||||
$mform->hardFreeze('visible');
|
||||
$mform->setConstant('visible', $course->visible);
|
||||
} else {
|
||||
}
|
||||
} else {
|
||||
if (!will_have_course_capability('moodle/course:visibility', $categorycontext)) {
|
||||
$mform->hardFreeze('visible');
|
||||
$mform->setConstant('visible', $courseconfig->visible);
|
||||
}
|
||||
}
|
||||
|
@ -183,7 +183,11 @@ class enrol_guest_plugin extends enrol_plugin {
|
||||
}
|
||||
|
||||
$header = $this->get_instance_name($instance);
|
||||
$config = has_capability('enrol/guest:config', $context);
|
||||
if (!$i) {
|
||||
$config = will_have_course_capability('enrol/guest:config', $context);
|
||||
} else {
|
||||
$config = has_capability('enrol/guest:config', $context);
|
||||
}
|
||||
|
||||
$mform->addElement('header', 'enrol_guest_header_'.$i, $header);
|
||||
|
||||
@ -196,12 +200,27 @@ class enrol_guest_plugin extends enrol_plugin {
|
||||
$mform->setAdvanced('enrol_guest_status_'.$i, $this->get_config('status_adv'));
|
||||
if (!$config) {
|
||||
$mform->hardFreeze('enrol_guest_status_'.$i);
|
||||
if (!$i) {
|
||||
$mform->setConstant('enrol_guest_status_'.$i, $this->get_config('status'));
|
||||
} else {
|
||||
$mform->setConstant('enrol_guest_status_'.$i, $instance->status);
|
||||
}
|
||||
}
|
||||
|
||||
$mform->addElement('passwordunmask', 'enrol_guest_password_'.$i, get_string('password', 'enrol_guest'));
|
||||
$mform->addHelpButton('enrol_guest_password_'.$i, 'password', 'enrol_guest');
|
||||
if (!$config) {
|
||||
$mform->hardFreeze('enrol_guest_password_'.$i);
|
||||
if (!$i) {
|
||||
if ($this->get_config('requirepassword')) {
|
||||
$password = generate_password(20);
|
||||
} else {
|
||||
$password = '';
|
||||
}
|
||||
$mform->setConstant('enrol_guest_password_'.$i, $password);
|
||||
} else {
|
||||
$mform->setConstant('enrol_guest_password_'.$i, $instance->password);
|
||||
}
|
||||
} else {
|
||||
$mform->disabledIf('enrol_guest_password_'.$i, 'enrol_guest_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
|
||||
}
|
||||
@ -279,57 +298,46 @@ class enrol_guest_plugin extends enrol_plugin {
|
||||
public function course_updated($inserted, $course, $data) {
|
||||
global $DB;
|
||||
|
||||
$context = context_course::instance($course->id);
|
||||
|
||||
if (has_capability('enrol/guest:config', $context)) {
|
||||
if ($inserted) {
|
||||
if (isset($data->enrol_guest_status_0)) {
|
||||
$fields = array('status'=>$data->enrol_guest_status_0);
|
||||
if ($fields['status'] == ENROL_INSTANCE_ENABLED) {
|
||||
$fields['password'] = $data->enrol_guest_password_0;
|
||||
} else {
|
||||
if ($this->get_config('requirepassword')) {
|
||||
$fields['password'] = generate_password(20);
|
||||
}
|
||||
}
|
||||
$this->add_instance($course, $fields);
|
||||
if ($inserted) {
|
||||
if (isset($data->enrol_guest_status_0)) {
|
||||
$fields = array('status'=>$data->enrol_guest_status_0);
|
||||
if ($fields['status'] == ENROL_INSTANCE_ENABLED) {
|
||||
$fields['password'] = $data->enrol_guest_password_0;
|
||||
} else {
|
||||
if ($this->get_config('defaultenrol')) {
|
||||
$this->add_default_instance($course);
|
||||
if ($this->get_config('requirepassword')) {
|
||||
$fields['password'] = generate_password(20);
|
||||
}
|
||||
}
|
||||
$this->add_instance($course, $fields);
|
||||
} else {
|
||||
$instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'guest'));
|
||||
foreach ($instances as $instance) {
|
||||
$i = $instance->id;
|
||||
|
||||
if (isset($data->{'enrol_guest_status_'.$i})) {
|
||||
$reset = ($instance->status != $data->{'enrol_guest_status_'.$i});
|
||||
|
||||
$instance->status = $data->{'enrol_guest_status_'.$i};
|
||||
$instance->timemodified = time();
|
||||
if ($instance->status == ENROL_INSTANCE_ENABLED) {
|
||||
if ($instance->password !== $data->{'enrol_guest_password_'.$i}) {
|
||||
$reset = true;
|
||||
}
|
||||
$instance->password = $data->{'enrol_guest_password_'.$i};
|
||||
}
|
||||
$DB->update_record('enrol', $instance);
|
||||
|
||||
if ($reset) {
|
||||
$context->mark_dirty();
|
||||
}
|
||||
}
|
||||
if ($this->get_config('defaultenrol')) {
|
||||
$this->add_default_instance($course);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
if ($inserted) {
|
||||
if ($this->get_config('defaultenrol')) {
|
||||
$this->add_default_instance($course);
|
||||
$instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'guest'));
|
||||
foreach ($instances as $instance) {
|
||||
$i = $instance->id;
|
||||
|
||||
if (isset($data->{'enrol_guest_status_'.$i})) {
|
||||
$reset = ($instance->status != $data->{'enrol_guest_status_'.$i});
|
||||
|
||||
$instance->status = $data->{'enrol_guest_status_'.$i};
|
||||
$instance->timemodified = time();
|
||||
if ($instance->status == ENROL_INSTANCE_ENABLED) {
|
||||
if ($instance->password !== $data->{'enrol_guest_password_'.$i}) {
|
||||
$reset = true;
|
||||
}
|
||||
$instance->password = $data->{'enrol_guest_password_'.$i};
|
||||
}
|
||||
$DB->update_record('enrol', $instance);
|
||||
|
||||
if ($reset) {
|
||||
$context = context_course::instance($course->id);
|
||||
$context->mark_dirty();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// bad luck, user can not change anything
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1612,7 +1612,7 @@ class accesslib_testcase extends advanced_testcase {
|
||||
|
||||
try {
|
||||
will_have_course_capability('moodle/course:visibility', $syscontext, $creator);
|
||||
$this->fail('Exception expected when non course/category contet passed to will_have_course_capability()');
|
||||
$this->fail('Exception expected when non course/category context passed to will_have_course_capability()');
|
||||
} catch (moodle_exception $e) {
|
||||
$this->assertInstanceOf('coding_exception', $e);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user