Пример #1
0
 /**
  * Creates course enrol form, checks if form submitted
  * and enrols user if necessary. It can also redirect.
  *
  * @param stdClass $instance
  * @return string html text, usually a form in a text box
  */
 public function enrol_page_hook(stdClass $instance)
 {
     global $CFG, $OUTPUT, $USER;
     require_once "{$CFG->dirroot}/enrol/self/locallib.php";
     $enrolstatus = $this->can_self_enrol($instance);
     if (true === $enrolstatus) {
         // This user can self enrol using this instance.
         $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);
             }
         }
     } else {
         // This user can not self enrol using this instance. Using an empty form to keep
         // the UI consistent with other enrolment plugins that returns a form.
         $data = new stdClass();
         $data->header = $this->get_instance_name($instance);
         $data->info = $enrolstatus;
         // The can_self_enrol call returns a button to the login page if the user is a
         // guest, setting the login url to the form if that is the case.
         $url = isguestuser() ? get_login_url() : null;
         $form = new enrol_self_empty_form($url, $data);
     }
     ob_start();
     $form->display();
     $output = ob_get_clean();
     return $OUTPUT->box($output);
 }
Пример #2
0
 /**
  * Creates course enrol form, checks if form submitted
  * and enrols user if necessary. It can also redirect.
  *
  * @param stdClass $instance
  * @return string html text, usually a form in a text box
  */
 public function enrol_page_hook(stdClass $instance)
 {
     global $CFG, $OUTPUT, $SESSION, $USER, $DB;
     if (isguestuser()) {
         // can not enrol guest!!
         return null;
     }
     if ($DB->record_exists('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) {
         //TODO: maybe we should tell them they are already enrolled, but can not access the course
         return null;
     }
     if ($instance->enrolstartdate != 0 and $instance->enrolstartdate > time()) {
         //TODO: inform that we can not enrol yet
         return null;
     }
     if ($instance->enrolenddate != 0 and $instance->enrolenddate < time()) {
         //TODO: inform that enrolment is not possible any more
         return null;
     }
     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 $OUTPUT->notification(get_string('maxenrolledreached', 'enrol_self'));
         }
     }
     require_once "{$CFG->dirroot}/enrol/self/locallib.php";
     require_once "{$CFG->dirroot}/group/lib.php";
     $form = new enrol_self_enrol_form(NULL, $instance);
     $instanceid = optional_param('instance', 0, PARAM_INT);
     if ($instance->id == $instanceid) {
         if ($data = $form->get_data()) {
             $enrol = enrol_get_plugin('self');
             $timestart = time();
             if ($instance->enrolperiod) {
                 $timeend = $timestart + $instance->enrolperiod;
             } else {
                 $timeend = 0;
             }
             $this->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend);
             add_to_log($instance->courseid, 'course', 'enrol', '../enrol/users.php?id=' . $instance->courseid, $instance->courseid);
             //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
                 $groups = $DB->get_records('groups', array('courseid' => $instance->courseid), 'id', 'id, enrolmentkey');
                 foreach ($groups as $group) {
                     if (empty($group->enrolmentkey)) {
                         continue;
                     }
                     if ($group->enrolmentkey === $data->enrolpassword) {
                         groups_add_member($group->id, $USER->id);
                         break;
                     }
                 }
             }
             // send welcome
             if ($instance->customint4) {
                 $this->email_welcome_message($instance, $USER);
             }
         }
     }
     ob_start();
     $form->display();
     $output = ob_get_clean();
     return $OUTPUT->box($output);
 }
Пример #3
0
 /**
  * Creates course enrol form, checks if form submitted
  * and enrols user if necessary. It can also redirect.
  *
  * @param stdClass $instance
  * @return string html text, usually a form in a text box
  */
 public function enrol_page_hook(stdClass $instance)
 {
     global $CFG, $OUTPUT, $USER;
     require_once "{$CFG->dirroot}/enrol/self/locallib.php";
     $enrolstatus = $this->can_self_enrol($instance);
     // 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);
     }
 }
Пример #4
0
 /**
  * Creates course enrol form, checks if form submitted
  * and enrols user if necessary. It can also redirect.
  *
  * @param stdClass $instance
  * @return string html text, usually a form in a text box
  */
 public function enrol_page_hook(stdClass $instance)
 {
     global $CFG, $OUTPUT, $SESSION, $USER, $DB;
     if (isguestuser()) {
         // Can not enrol guest!!
         return null;
     }
     if ($DB->record_exists('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) {
         //TODO: maybe we should tell them they are already enrolled, but can not access the course
         return null;
     }
     if ($instance->enrolstartdate != 0 and $instance->enrolstartdate > time()) {
         //TODO: inform that we can not enrol yet
         return null;
     }
     if ($instance->enrolenddate != 0 and $instance->enrolenddate < time()) {
         //TODO: inform that enrolment is not possible any more
         return null;
     }
     if (!$instance->customint6) {
         // New enrols not allowed.
         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";
     $form = new enrol_self_enrol_form(NULL, $instance);
     $instanceid = optional_param('instance', 0, PARAM_INT);
     if ($instance->id == $instanceid) {
         if ($data = $form->get_data()) {
             $enrol = enrol_get_plugin('self');
             $timestart = time();
             if ($instance->enrolperiod) {
                 $timeend = $timestart + $instance->enrolperiod;
             } else {
                 $timeend = 0;
             }
             $this->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend);
             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
                 $groups = $DB->get_records('groups', array('courseid' => $instance->courseid), 'id', 'id, enrolmentkey');
                 foreach ($groups as $group) {
                     if (empty($group->enrolmentkey)) {
                         continue;
                     }
                     if ($group->enrolmentkey === $data->enrolpassword) {
                         groups_add_member($group->id, $USER->id);
                         break;
                     }
                 }
             }
             // Send welcome message.
             if ($instance->customint4) {
                 $this->email_welcome_message($instance, $USER);
             }
         }
     }
     ob_start();
     $form->display();
     $output = ob_get_clean();
     return $OUTPUT->box($output);
 }