mirror of
https://github.com/moodle/moodle.git
synced 2025-04-21 00:12:56 +02:00
MDL-23875 add option to limit self-enrol to cohort members only
This commit is contained in:
parent
882fb83519
commit
dd6b1f15cf
@ -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.
|
||||
*
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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');
|
||||
|
@ -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.
|
||||
|
||||
|
@ -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));
|
||||
|
@ -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;
|
Loading…
x
Reference in New Issue
Block a user