diff --git a/cohort/lib.php b/cohort/lib.php index 223644b52e6..67c2bf4c2f6 100644 --- a/cohort/lib.php +++ b/cohort/lib.php @@ -155,6 +155,18 @@ function cohort_remove_member($cohortid, $userid) { events_trigger('cohort_member_removed', (object)array('cohortid'=>$cohortid, 'userid'=>$userid)); } +/** + * Is this user a cohort member? + * @param int $cohortid + * @param int $userid + * @return bool + */ +function cohort_is_member($cohortid, $userid) { + global $DB; + + return $DB->record_exists('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid)); +} + /** * Returns list of visible cohorts in course. * diff --git a/enrol/self/edit.php b/enrol/self/edit.php index 4b38df4daba..5422e2dcc6a 100644 --- a/enrol/self/edit.php +++ b/enrol/self/edit.php @@ -54,8 +54,9 @@ if ($instanceid) { // 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->id = null; + $instance->courseid = $course->id; + $instance->customint5 = 0; } $mform = new enrol_self_edit_form(NULL, array($instance, $plugin, $context)); @@ -74,6 +75,7 @@ if ($mform->is_cancelled()) { $instance->customint2 = $data->customint2; $instance->customint3 = $data->customint3; $instance->customint4 = $data->customint4; + $instance->customint5 = $data->customint5; $instance->customtext1 = $data->customtext1; $instance->roleid = $data->roleid; $instance->enrolperiod = $data->enrolperiod; @@ -88,7 +90,7 @@ if ($mform->is_cancelled()) { } else { $fields = array('status'=>$data->status, 'name'=>$data->name, 'password'=>$data->password, 'customint1'=>$data->customint1, 'customint2'=>$data->customint2, - 'customint3'=>$data->customint3, 'customint4'=>$data->customint4, 'customtext1'=>$data->customtext1, + 'customint3'=>$data->customint3, 'customint4'=>$data->customint4, 'customint5'=>$data->customint5, 'customtext1'=>$data->customtext1, 'roleid'=>$data->roleid, 'enrolperiod'=>$data->enrolperiod, 'enrolstartdate'=>$data->enrolstartdate, 'enrolenddate'=>$data->enrolenddate); $plugin->add_instance($course, $fields); } diff --git a/enrol/self/edit_form.php b/enrol/self/edit_form.php index 7f77cfce25b..1e80714e4d9 100644 --- a/enrol/self/edit_form.php +++ b/enrol/self/edit_form.php @@ -32,6 +32,8 @@ require_once($CFG->libdir.'/formslib.php'); class enrol_self_edit_form extends moodleform { function definition() { + global $DB; + $mform = $this->_form; list($instance, $plugin, $context) = $this->_customdata; @@ -100,6 +102,38 @@ class enrol_self_edit_form extends moodleform { $mform->addHelpButton('customint3', 'maxenrolled', 'enrol_self'); $mform->setType('customint3', PARAM_INT); + $cohorts = array(0 => get_string('no')); + list($sqlparents, $params) = $DB->get_in_or_equal($context->get_parent_context_ids(), SQL_PARAMS_NAMED); + $params['current'] = $instance->customint5; + $sql = "SELECT id, name, idnumber, contextid + FROM {cohort} + WHERE contextid $sqlparents OR id = :current + ORDER BY name ASC, idnumber ASC"; + $rs = $DB->get_recordset_sql($sql, $params); + foreach ($rs as $c) { + $ccontext = context::instance_by_id($c->contextid); + if ($c->id != $instance->customint5 and !has_capability('moodle/cohort:view', $ccontext)) { + continue; + } + $cohorts[$c->id] = format_string($c->name, true, array('context'=>$context)); + if ($c->idnumber) { + $cohorts[$c->id] .= ' ['.s($c->idnumber).']'; + } + } + if (!isset($cohorts[$instance->customint5])) { + // Somebody deleted a cohort, better keep the wrong value so that random ppl can not enrol. + $cohorts[$instance->customint5] = get_string('unknowncohort', 'cohort', $instance->customint5); + } + $rs->close(); + if (count($cohorts) > 1) { + $mform->addElement('select', 'customint5', get_string('cohortonly', 'enrol_self'), $cohorts); + $mform->addHelpButton('customint5', 'cohortonly', 'enrol_self'); + } else { + $mform->addElement('hidden', 'customint5'); + $mform->setType('customint5', PARAM_INT); + $mform->setConstant('customint5', 0); + } + $mform->addElement('advcheckbox', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_self')); $mform->setDefault('customint4', $plugin->get_config('sendcoursewelcomemessage')); $mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_self'); diff --git a/enrol/self/lang/en/enrol_self.php b/enrol/self/lang/en/enrol_self.php index 95595fc7f95..cae46daa41c 100644 --- a/enrol/self/lang/en/enrol_self.php +++ b/enrol/self/lang/en/enrol_self.php @@ -24,6 +24,9 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +$string['cohortnonmemberinfo'] = 'Only members of cohort \'{$a}\' can self-enrol.'; +$string['cohortonly'] = 'Only cohort members'; +$string['cohortonly_help'] = 'Self enrolment may be restricted to members of a specified cohort only. Note that changing this setting has no effect on existing enrolments.'; $string['customwelcomemessage'] = 'Custom welcome message'; $string['customwelcomemessage_help'] = 'A custom welcome message may be added as plain text or Moodle-auto format, including HTML tags and multi-lang tags. diff --git a/enrol/self/lib.php b/enrol/self/lib.php index 771ec763e0b..8724aef8c39 100644 --- a/enrol/self/lib.php +++ b/enrol/self/lib.php @@ -101,7 +101,16 @@ class enrol_self_plugin extends enrol_plugin { } public function show_enrolme_link(stdClass $instance) { - return ($instance->status == ENROL_INSTANCE_ENABLED); + global $CFG, $USER; + + if ($instance->status != ENROL_INSTANCE_ENABLED) { + return false; + } + if ($instance->customint5) { + require_once("$CFG->dirroot/cohort/lib.php"); + return cohort_is_member($instance->customint5, $USER->id); + } + return true; } /** @@ -189,6 +198,18 @@ class enrol_self_plugin extends enrol_plugin { return null; } + if ($instance->customint5) { + require_once("$CFG->dirroot/cohort/lib.php"); + if (!cohort_is_member($instance->customint5, $USER->id)) { + $cohort = $DB->get_record('cohort', array('id'=>$instance->customint5)); + if (!$cohort) { + return null; + } + $a = format_string($cohort->name, true, array('context'=>context::instance_by_id($cohort->contextid))); + return $OUTPUT->box(markdown_to_html(get_string('cohortnonmemberinfo', 'enrol_self', $a))); + } + } + require_once("$CFG->dirroot/enrol/self/locallib.php"); require_once("$CFG->dirroot/group/lib.php"); @@ -245,6 +266,7 @@ class enrol_self_plugin extends enrol_plugin { 'customint2' => $this->get_config('longtimenosee'), 'customint3' => $this->get_config('maxenrolled'), 'customint4' => $this->get_config('sendcoursewelcomemessage'), + 'customint5' => 0, 'enrolperiod' => $this->get_config('enrolperiod', 0), 'status' => $this->get_config('status'), 'roleid' => $this->get_config('roleid', 0)); diff --git a/enrol/self/version.php b/enrol/self/version.php index 73f1cdeefc5..e5a04c83918 100644 --- a/enrol/self/version.php +++ b/enrol/self/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2012061700; // The current plugin version (Date: YYYYMMDDXX) -$plugin->requires = 2012061700; // Requires this Moodle version +$plugin->version = 2012082300; // The current plugin version (Date: YYYYMMDDXX) +$plugin->requires = 2012082300; // Requires this Moodle version $plugin->component = 'enrol_self'; // Full name of the plugin (used for diagnostics) $plugin->cron = 180; \ No newline at end of file