Skip to content

Commit

Permalink
MDL-40127 Enrolments: Added status to instance info and removed displ…
Browse files Browse the repository at this point in the history
…aying of enrolment form with notice

get_enrol_info will return status of enrolment instance, to notify webservice if enrolment is possible or not
If enrolment is not possible then don't show enrolment form
show_enrolme_link will use can_self_enrol to varify if enrolment link should be shown or not.
  • Loading branch information
Rajesh Taneja authored and danpoltawski committed Jul 2, 2013
1 parent 85d1c53 commit cc1b501
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 48 deletions.
77 changes: 40 additions & 37 deletions enrol/self/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,11 @@ public function allow_manage(stdClass $instance) {
}

public function show_enrolme_link(stdClass $instance) {
global $CFG, $USER;

if ($instance->status != ENROL_INSTANCE_ENABLED) {
return false;
}

if (!$instance->customint6) {
// New enrols not allowed.
if (true !== $this->can_self_enrol($instance, false)) {
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 @@ -208,7 +198,7 @@ public function enrol_self(stdClass $instance, $data = null) {
add_to_log($instance->courseid, 'course', 'enrol', '../enrol/users.php?id='.$instance->courseid, $instance->courseid); //TODO: There should be userid somewhere!

if ($instance->password and $instance->customint1 and $data->enrolpassword !== $instance->password) {
// it must be a group enrolment, let's assign group too
// It must be a group enrolment, let's assign group too.
$groups = $DB->get_records('groups', array('courseid'=>$instance->courseid), 'id', 'id, enrolmentkey');
foreach ($groups as $group) {
if (empty($group->enrolmentkey)) {
Expand Down Expand Up @@ -240,60 +230,72 @@ public function enrol_page_hook(stdClass $instance) {

$enrolstatus = $this->can_self_enrol($instance);

$form = new enrol_self_enrol_form(NULL, array('instance' => $instance, 'enrolstatus' => $enrolstatus));
$instanceid = optional_param('instance', 0, PARAM_INT);
if ($instance->id == $instanceid) {
if ($data = $form->get_data()) {
$this->enrol_self($instance, $data);
// Don't show enrolment instance form, if user can't enrol using it.
if (true === $enrolstatus) {
$form = new enrol_self_enrol_form(NULL, $instance);
$instanceid = optional_param('instance', 0, PARAM_INT);
if ($instance->id == $instanceid) {
if ($data = $form->get_data()) {
$this->enrol_self($instance, $data);
}
}
}

ob_start();
$form->display();
$output = ob_get_clean();

return $OUTPUT->box($output);
ob_start();
$form->display();
$output = ob_get_clean();
return $OUTPUT->box($output);
}
}

/**
* Checks if user can self enrol.
*
* @param stdClass $instance enrolment instance
* @return bool|array true if successful, else error code and message.
* @param bool $checkuserenrolment if true will check if user enrolment is inactive.
* used by navigation to improve performance.
* @return bool|string true if successful, else error message or false.
*/
public function can_self_enrol(stdClass $instance) {
global $DB, $USER;
public function can_self_enrol(stdClass $instance, $checkuserenrolment = true) {
global $DB, $USER, $CFG;

if (isguestuser()) {
// Can not enrol guest!!
return array('canntenrol' => get_string('canntenrol', 'enrol_self'));
if ($checkuserenrolment) {
if (isguestuser()) {
// Can not enrol guest.
return get_string('canntenrol', 'enrol_self');
}
// Check if user is already enroled.
if ($DB->get_record('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) {
return get_string('canntenrol', 'enrol_self');
}
}

if ($instance->status != ENROL_INSTANCE_ENABLED) {
return get_string('canntenrol', 'enrol_self');
}

if ($instance->enrolstartdate != 0 and $instance->enrolstartdate > time()) {
//TODO: inform that we can not enrol yet
return array('canntenrol' => get_string('canntenrol', 'enrol_self'));
return get_string('canntenrol', 'enrol_self');
}

if ($instance->enrolenddate != 0 and $instance->enrolenddate < time()) {
//TODO: inform that enrolment is not possible any more
return array('canntenrol' => get_string('canntenrol', 'enrol_self'));
return get_string('canntenrol', 'enrol_self');
}

if (!$instance->customint6) {
// New enrols not allowed.
return array('canntenrol' => get_string('canntenrol', 'enrol_self'));
return get_string('canntenrol', 'enrol_self');
}

if ($DB->record_exists('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) {
return array('canntenrol' => get_string('canntenrol', 'enrol_self'));
return get_string('canntenrol', 'enrol_self');
}

if ($instance->customint3 > 0) {
// Max enrol limit specified.
$count = $DB->count_records('user_enrolments', array('enrolid' => $instance->id));
if ($count >= $instance->customint3) {
// Bad luck, no more self enrolments here.
return array('maxenrolledreached' => get_string('maxenrolledreached', 'enrol_self'));
return get_string('maxenrolledreached', 'enrol_self');
}
}

Expand All @@ -305,7 +307,7 @@ public function can_self_enrol(stdClass $instance) {
return null;
}
$a = format_string($cohort->name, true, array('context' => context::instance_by_id($cohort->contextid)));
return array('cohortnonmemberinfo' => markdown_to_html(get_string('cohortnonmemberinfo', 'enrol_self', $a)));
return markdown_to_html(get_string('cohortnonmemberinfo', 'enrol_self', $a));
}
}

Expand All @@ -326,6 +328,7 @@ public function get_enrol_info(stdClass $instance) {
$instanceinfo->courseid = $instance->courseid;
$instanceinfo->type = $this->get_name();
$instanceinfo->name = $this->get_instance_name($instance);
$instanceinfo->status = $this->can_self_enrol($instance);

if ($instance->password) {
$instanceinfo->requiredparam = new stdClass();
Expand Down
11 changes: 2 additions & 9 deletions enrol/self/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,19 @@ class enrol_self_enrol_form extends moodleform {
* @return string form identifier
*/
protected function get_form_identifier() {
$formid = $this->_customdata['instance']->id.'_'.get_class($this);
$formid = $this->_customdata->id.'_'.get_class($this);
return $formid;
}

public function definition() {
$mform = $this->_form;
$instance = $this->_customdata['instance'];
$instance = $this->_customdata;
$this->instance = $instance;
$plugin = enrol_get_plugin('self');

$heading = $plugin->get_instance_name($instance);
$mform->addElement('header', 'selfheader', $heading);

if (true !== $this->_customdata['enrolstatus']) {
// If enrol status then show message, else show general error message.
$statusmsg = array_values($this->_customdata['enrolstatus']);
$mform->addElement('static', 'notice', '', $statusmsg[0]);
return;
}

if ($instance->password) {
// Change the id of self enrolment key input as there can be multiple self enrolment methods.
$mform->addElement('passwordunmask', 'enrolpassword', get_string('password', 'enrol_self'),
Expand Down
6 changes: 4 additions & 2 deletions lib/enrollib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1714,9 +1714,11 @@ public function enrol_page_hook(stdClass $instance) {
* Checks if user can self enrol.
*
* @param stdClass $instance enrolment instance
* @return bool true if current user can self enrol, else error code and message.
* @param bool $checkuserenrolment if true will check if user enrolment is inactive.
* used by navigation to improve performance.
* @return bool|string true if successful, else error message or false
*/
public function can_self_enrol(stdClass $instance) {
public function can_self_enrol(stdClass $instance, $checkuserenrolment = true) {
return false;
}

Expand Down

0 comments on commit cc1b501

Please sign in to comment.