Exemple #1
0
 /**
  * Checks if user can self enrol.
  *
  * @param stdClass $instance enrolment instance
  * @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, $checkuserenrolment = true)
 {
     global $CFG, $DB, $OUTPUT, $USER;
     if ($checkuserenrolment) {
         if (isguestuser()) {
             // Can not enrol guest.
             return get_string('noguestaccess', 'enrol') . $OUTPUT->continue_button(get_login_url());
         }
         // 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()) {
         return get_string('canntenrolearly', 'enrol_self', userdate($instance->enrolstartdate));
     }
     if ($instance->enrolenddate != 0 and $instance->enrolenddate < time()) {
         return get_string('canntenrollate', 'enrol_self', userdate($instance->enrolenddate));
     }
     if (!$instance->customint6) {
         // New enrols not allowed.
         return get_string('canntenrol', 'enrol_self');
     }
     if ($DB->record_exists('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) {
         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 get_string('maxenrolledreached', 'enrol_self');
         }
     }
     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 markdown_to_html(get_string('cohortnonmemberinfo', 'enrol_self', $a));
         }
     }
     return true;
 }
Exemple #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->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);
 }
Exemple #3
0
 public function test_cohort_is_member()
 {
     global $DB;
     $this->resetAfterTest();
     $cohort = $this->getDataGenerator()->create_cohort();
     $user = $this->getDataGenerator()->create_user();
     $this->assertFalse(cohort_is_member($cohort->id, $user->id));
     cohort_add_member($cohort->id, $user->id);
     $this->assertTrue(cohort_is_member($cohort->id, $user->id));
 }
 /**
  * Check if the current logged in user has permission to view bulk emails.
  * Ideally this would use a capability check, but that requires a system level role assigned to all teachers,
  * so it checks user is in a cohort instead.
  * FIXME: Hardcoded for SSIS
  *
  * @throws Exception
  * @return boolean
  */
 private function check_permissions()
 {
     global $USER;
     require_login();
     if (is_siteadmin()) {
         return true;
     }
     $cohortids = array(73, 114);
     foreach ($cohortids as $cohortid) {
         if (cohort_is_member($cohortid, $USER->id)) {
             return true;
         }
     }
     throw new Exception("You don't have permission to do that.");
 }