From eac37ff46a93bb177f5f05c44eba7d883c55eb43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Wed, 7 Nov 2012 18:57:14 +0100 Subject: [PATCH] MDL-36431 fix multiple problems with self enrol defaults This patch adds new method for retrieving of default properties of enrol_self instances and uses it both for automatic adding of instances and add instance form - this will guarantee both are using the same data in the future. --- enrol/self/edit.php | 21 ++++++++++-------- enrol/self/edit_form.php | 13 +---------- enrol/self/lib.php | 48 +++++++++++++++++++++++++--------------- 3 files changed, 43 insertions(+), 39 deletions(-) diff --git a/enrol/self/edit.php b/enrol/self/edit.php index e1260cf7a7e..e683d18c58f 100644 --- a/enrol/self/edit.php +++ b/enrol/self/edit.php @@ -43,26 +43,29 @@ if (!enrol_is_enabled('self')) { redirect($return); } +/** @var enrol_self_plugin $plugin */ $plugin = enrol_get_plugin('self'); if ($instanceid) { $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'self', 'id'=>$instanceid), '*', MUST_EXIST); - // Merge these two settings to one value for the single selection element. - if ($instance->notifyall and $instance->expirynotify) { - $instance->expirynotify = 2; - } - unset($instance->notifyall); } else { require_capability('moodle/course:enrolconfig', $context); // No instance yet, we have to add new instance. navigation_node::override_active_url(new moodle_url('/enrol/instances.php', array('id'=>$course->id))); - $instance = new stdClass(); - $instance->id = null; - $instance->courseid = $course->id; - $instance->customint5 = 0; + + $instance = (object)$plugin->get_instance_defaults(); + $instance->id = null; + $instance->courseid = $course->id; + $instance->status = ENROL_INSTANCE_ENABLED; // Do not use default for automatically created instances here. } +// Merge these two settings to one value for the single selection element. +if ($instance->notifyall and $instance->expirynotify) { + $instance->expirynotify = 2; +} +unset($instance->notifyall); + $mform = new enrol_self_edit_form(NULL, array($instance, $plugin, $context)); if ($mform->is_cancelled()) { diff --git a/enrol/self/edit_form.php b/enrol/self/edit_form.php index 6431db60ae2..94287df2c25 100644 --- a/enrol/self/edit_form.php +++ b/enrol/self/edit_form.php @@ -44,7 +44,6 @@ class enrol_self_edit_form extends moodleform { ENROL_INSTANCE_DISABLED => get_string('no')); $mform->addElement('select', 'status', get_string('status', 'enrol_self'), $options); $mform->addHelpButton('status', 'status', 'enrol_self'); - $mform->setDefault('status', $plugin->get_config('status')); $mform->addElement('passwordunmask', 'password', get_string('password', 'enrol_self')); $mform->addHelpButton('password', 'password', 'enrol_self'); @@ -56,18 +55,11 @@ class enrol_self_edit_form extends moodleform { 0 => get_string('no')); $mform->addElement('select', 'customint1', get_string('groupkey', 'enrol_self'), $options); $mform->addHelpButton('customint1', 'groupkey', 'enrol_self'); - $mform->setDefault('customint1', $plugin->get_config('groupkey')); - if ($instance->id) { - $roles = $this->extend_assignable_roles($context, $instance->roleid); - } else { - $roles = $this->extend_assignable_roles($context, $plugin->get_config('roleid')); - } + $roles = $this->extend_assignable_roles($context, $instance->roleid); $mform->addElement('select', 'roleid', get_string('role', 'enrol_self'), $roles); - $mform->setDefault('roleid', $plugin->get_config('roleid')); $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_self'), array('optional' => true, 'defaultunit' => 86400)); - $mform->setDefault('enrolperiod', $plugin->get_config('enrolperiod')); $mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_self'); $options = array(0 => get_string('no'), 1 => get_string('expirynotifyenroller', 'core_enrol'), 2 => get_string('expirynotifyall', 'core_enrol')); @@ -100,11 +92,9 @@ class enrol_self_edit_form extends moodleform { 14 * 3600 * 24 => get_string('numdays', '', 14), 7 * 3600 * 24 => get_string('numdays', '', 7)); $mform->addElement('select', 'customint2', get_string('longtimenosee', 'enrol_self'), $options); - $mform->setDefault('customint2', $plugin->get_config('longtimenosee')); $mform->addHelpButton('customint2', 'longtimenosee', 'enrol_self'); $mform->addElement('text', 'customint3', get_string('maxenrolled', 'enrol_self')); - $mform->setDefault('customint3', $plugin->get_config('maxenrolled')); $mform->addHelpButton('customint3', 'maxenrolled', 'enrol_self'); $mform->setType('customint3', PARAM_INT); @@ -141,7 +131,6 @@ class enrol_self_edit_form extends moodleform { } $mform->addElement('advcheckbox', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_self')); - $mform->setDefault('customint4', $plugin->get_config('sendcoursewelcomemessage')); $mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_self'); $mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'enrol_self'), array('cols'=>'60', 'rows'=>'8')); diff --git a/enrol/self/lib.php b/enrol/self/lib.php index 18273730b79..0547e068374 100644 --- a/enrol/self/lib.php +++ b/enrol/self/lib.php @@ -264,24 +264,7 @@ class enrol_self_plugin extends enrol_plugin { * @return int id of new instance */ public function add_default_instance($course) { - $expirynotify = $this->get_config('expirynotify', 0); - if ($expirynotify == 2) { - $expirynotify = 1; - $notifyall = 1; - } else { - $notifyall = 0; - } - $fields = array('customint1' => $this->get_config('groupkey'), - 'customint2' => $this->get_config('longtimenosee'), - 'customint3' => $this->get_config('maxenrolled'), - 'customint4' => $this->get_config('sendcoursewelcomemessage'), - 'customint5' => 0, - 'enrolperiod' => $this->get_config('enrolperiod', 0), - 'expirynotify'=> $expirynotify, - 'notifyall' => $notifyall, - 'expirythreshold' => $this->get_config('expirythreshold', 86400), - 'status' => $this->get_config('status'), - 'roleid' => $this->get_config('roleid', 0)); + $fields = $this->get_instance_defaults(); if ($this->get_config('requirepassword')) { $fields['password'] = generate_password(20); @@ -290,6 +273,35 @@ class enrol_self_plugin extends enrol_plugin { return $this->add_instance($course, $fields); } + /** + * Returns defaults for new instances. + * @return array + */ + public function get_instance_defaults() { + $expirynotify = $this->get_config('expirynotify'); + if ($expirynotify == 2) { + $expirynotify = 1; + $notifyall = 1; + } else { + $notifyall = 0; + } + + $fields = array(); + $fields['status'] = $this->get_config('status'); + $fields['roleid'] = $this->get_config('roleid'); + $fields['enrolperiod'] = $this->get_config('enrolperiod'); + $fields['expirynotify'] = $expirynotify; + $fields['notifyall'] = $notifyall; + $fields['expirythreshold'] = $this->get_config('expirythreshold'); + $fields['customint1'] = $this->get_config('groupkey'); + $fields['customint2'] = $this->get_config('longtimenosee'); + $fields['customint3'] = $this->get_config('maxenrolled'); + $fields['customint4'] = $this->get_config('sendcoursewelcomemessage'); + $fields['customint5'] = 0; + + return $fields; + } + /** * Send welcome email to specified user. *