Example #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, $SESSION, $USER;
     if ($instance->password === '') {
         return null;
     }
     if (isset($USER->enrol['tempguest'][$instance->courseid]) and $USER->enrol['tempguest'][$instance->courseid] > time()) {
         // no need to show the guest access when user can already enter course as guest
         return null;
     }
     require_once "{$CFG->dirroot}/enrol/guest/locallib.php";
     $form = new enrol_guest_enrol_form(NULL, $instance);
     $instanceid = optional_param('instance', 0, PARAM_INT);
     if ($instance->id == $instanceid) {
         if ($data = $form->get_data()) {
             // add guest role
             $context = context_course::instance($instance->courseid);
             $USER->enrol_guest_passwords[$instance->id] = $data->guestpassword;
             // this is a hack, ideally we should not add stuff to $USER...
             if (isset($USER->enrol['tempguest'][$instance->courseid])) {
                 remove_temp_course_roles($context);
             }
             load_temp_course_role($context, $CFG->guestroleid);
             $USER->enrol['tempguest'][$instance->courseid] = ENROL_MAX_TIMESTAMP;
             // go to the originally requested page
             if (!empty($SESSION->wantsurl)) {
                 $destination = $SESSION->wantsurl;
                 unset($SESSION->wantsurl);
             } else {
                 $destination = "{$CFG->wwwroot}/course/view.php?id={$instance->courseid}";
             }
             redirect($destination);
         }
     }
     ob_start();
     $form->display();
     $output = ob_get_clean();
     return $OUTPUT->box($output, 'generalbox');
 }
Example #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;
     if (empty($instance->password)) {
         return null;
     }
     require_once "{$CFG->dirroot}/enrol/guest/locallib.php";
     $form = new enrol_guest_enrol_form(NULL, $instance);
     $instanceid = optional_param('instance', 0, PARAM_INT);
     if ($instance->id == $instanceid) {
         if ($data = $form->get_data()) {
             // set up primitive require_login() caching
             unset($USER->enrol['enrolled'][$instance->courseid]);
             $USER->enrol['tempguest'][$instance->courseid] = time() + 60 * 60 * 8;
             // 8 hours access before asking for pw again
             // add guest role
             $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
             load_temp_course_role($context, $CFG->guestroleid);
             // go to the originally requested page
             if (!empty($SESSION->wantsurl)) {
                 $destination = $SESSION->wantsurl;
                 unset($SESSION->wantsurl);
             } else {
                 $destination = "{$CFG->wwwroot}/course/view.php?id={$instance->courseid}";
             }
             redirect($destination);
         }
     }
     ob_start();
     $form->display();
     $output = ob_get_clean();
     return $OUTPUT->box($output, 'generalbox');
 }