Skip to content

Commit

Permalink
MDL-23875 add option to limit self-enrol to cohort members only
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Aug 26, 2012
1 parent 882fb83 commit dd6b1f1
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 6 deletions.
12 changes: 12 additions & 0 deletions cohort/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
8 changes: 5 additions & 3 deletions enrol/self/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@
// 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));
Expand All @@ -74,6 +75,7 @@
$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;
Expand All @@ -88,7 +90,7 @@

} 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);
}
Expand Down
34 changes: 34 additions & 0 deletions enrol/self/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
class enrol_self_edit_form extends moodleform {

function definition() {
global $DB;

$mform = $this->_form;

list($instance, $plugin, $context) = $this->_customdata;
Expand Down Expand Up @@ -100,6 +102,38 @@ function definition() {
$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');
Expand Down
3 changes: 3 additions & 0 deletions enrol/self/lang/en/enrol_self.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = 'Select a cohort if you want to restrict self enrolment only to members of this cohort. Change of this setting does not affect 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.
Expand Down
24 changes: 23 additions & 1 deletion enrol/self/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,16 @@ public function allow_manage(stdClass $instance) {
}

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;
}

/**
Expand Down Expand Up @@ -189,6 +198,18 @@ public function enrol_page_hook(stdClass $instance) {
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");

Expand Down Expand Up @@ -245,6 +266,7 @@ public function add_default_instance($course) {
'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));
Expand Down
4 changes: 2 additions & 2 deletions enrol/self/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit dd6b1f1

Please sign in to comment.