/**
  * Perform all necessary tasks to add a student enrolment to the system.
  *
  * @param array $checks what checks to perform before adding enrolling the
  * user.  e.g. array('prereq' => 1, 'waitlist' => 1) will check that
  * prerequisites are satisfied, and that the class is not full
  * @param boolean $notify whether or not notifications should be sent if a
  * check fails
  */
 function add($checks = array(), $notify = false)
 {
     global $CURMAN, $CFG, $USER;
     $status = true;
     if ($CURMAN->db->record_exists(STUTABLE, 'userid', $this->userid, 'classid', $this->classid)) {
         // already enrolled -- pretend we succeeded
         return true;
     }
     // check that the student can be enrolled first
     if (!empty($checks['prereq'])) {
         // check prerequisites
         $cmclass = new cmclass($this->classid);
         // get all the curricula that the user is in
         $curricula = curriculumstudent::get_curricula($this->userid);
         foreach ($curricula as $curriculum) {
             $curcrs = new curriculumcourse();
             $curcrs->courseid = $cmclass->courseid;
             $curcrs->curriculumid = $curriculum->curid;
             if (!$curcrs->prerequisites_satisfied($this->userid)) {
                 // prerequisites not satisfied
                 if ($notify) {
                     $data = new stdClass();
                     $data->userid = $this->userid;
                     $data->classid = $this->classid;
                     //$data->trackid = $trackid;
                     events_trigger('crlm_prereq_unsatisfied', $data);
                 }
                 $status = new Object();
                 $status->message = get_string('unsatisfiedprereqs', 'block_curr_admin');
                 $status->code = 'unsatisfiedprereqs';
                 return $status;
             }
         }
     }
     if (!empty($checks['waitlist'])) {
         // check class enrolment limit
         $cmclass = new cmclass($this->classid);
         $limit = $cmclass->maxstudents;
         if (!empty($limit) && $limit <= student::count_enroled($this->classid)) {
             // class is full
             // put student on wait list
             $wait_list = new waitlist($this);
             $wait_list->timecreated = time();
             $wait_list->position = 0;
             $wait_list->add();
             if ($notify) {
                 $subject = get_string('user_waitlisted', 'block_curr_admin');
                 $a = new object();
                 $a->user = $this->user->idnumber;
                 $a->cmclass = $cmclass->idnumber;
                 $message = get_string('user_waitlisted_msg', 'block_curr_admin', $a);
                 $from = $user = get_admin();
                 notification::notify($message, $user, $from);
                 email_to_user($user, $from, $subject, $message);
             }
             $status = new Object();
             $status->message = get_string('user_waitlisted', 'block_curr_admin');
             $status->code = 'user_waitlisted';
             return $status;
         }
     }
     //set end time based on class duration
     $studentclass = new cmclass($this->classid);
     if (empty($this->endtime)) {
         if (isset($studentclass->duration) && $studentclass->duration) {
             $this->endtime = $this->enrolmenttime + $studentclass->duration;
         } else {
             // no class duration -> no end time
             $this->endtime = 0;
         }
     }
     $status = $this->data_insert_record();
     // TBD: we should check this!
     /// Get the Moodle user ID or create a new account for this user.
     if (!($muserid = cm_get_moodleuserid($this->userid))) {
         $user = new user($this->userid);
         if (!($muserid = $user->synchronize_moodle_user(true, true))) {
             $status = new Object();
             $status->message = get_string('errorsynchronizeuser', 'block_curr_admin');
             $muserid = false;
         }
     }
     /// Enrol them into the Moodle class.
     if ($moodlecourseid = moodle_get_course($this->classid)) {
         if ($mcourse = get_record('course', 'id', $moodlecourseid)) {
             $enrol = $mcourse->enrol;
             if (!$enrol) {
                 $enrol = $CFG->enrol;
             }
             if ($CURMAN->config->restrict_to_elis_enrolment_plugin && $enrol != 'elis') {
                 $status = new Object();
                 $status->message = get_string('error_not_using_elis_enrolment', 'block_curr_admin');
                 return $status;
             }
             $timestart = $this->enrolmenttime;
             $timeend = $this->endtime;
             if ($role = get_default_course_role($mcourse)) {
                 $context = get_context_instance(CONTEXT_COURSE, $mcourse->id);
                 if (!empty($muserid)) {
                     if (!role_assign($role->id, $muserid, 0, $context->id, $timestart, $timeend, 0, 'manual')) {
                         $status = new Object();
                         $status->message = get_string('errorroleassign', 'block_curr_admin');
                     }
                 }
             }
         }
     } else {
         if (!empty($muserid)) {
             $sturole = $CURMAN->config->enrolment_role_sync_student_role;
             // ELIS-2776: must still trigger events for notifications
             $ra = new stdClass();
             $ra->roleid = !empty($sturole) ? $sturole : get_field('role', 'id', 'shortname', 'student');
             $ra->contextid = context_level_base::get_custom_context_level('class', 'block_curr_admin');
             // TBD
             $ra->userid = $muserid;
             $ra->component = '';
             // TBD: 'enrol_elis'
             $ra->itemid = $this->classid;
             // TBD
             $ra->timemodified = time();
             $ra->modifierid = empty($USER->id) ? 0 : $USER->id;
             events_trigger('role_assigned', $ra);
         }
     }
     return $status;
 }
 /**
  *
  */
 public function action_waitlistconfirm()
 {
     $id = required_param('userid', PARAM_INT);
     $form_url = new moodle_url(null, array('s' => $this->pagename, 'section' => $this->section, 'action' => 'waitlistconfirm'));
     $waitlistform = new waitlistaddform($form_url, array('student_ids' => $id));
     if ($data = $waitlistform->get_data()) {
         $now = time();
         foreach ($data->userid as $uid) {
             if (isset($data->enrol[$uid]) && isset($data->classid[$uid]) && isset($data->enrolmenttime[$uid])) {
                 if ($data->enrol[$uid] == 1) {
                     $wait_record = new object();
                     $wait_record->userid = $uid;
                     $wait_record->classid = $data->classid[$uid];
                     $wait_record->enrolmenttime = $data->enrolmenttime[$uid];
                     $wait_record->timecreated = $now;
                     $wait_record->position = 0;
                     $wait_list = new waitlist($wait_record);
                     $wait_list->add();
                 } else {
                     if ($data->enrol[$uid] == 2) {
                         $user = new user($uid);
                         $student_data = array();
                         $student_data['classid'] = $data->classid[$uid];
                         $student_data['userid'] = $uid;
                         $student_data['enrolmenttime'] = $data->enrolmenttime[$uid];
                         $student_data['timecreated'] = $now;
                         $student_data['completestatusid'] = STUSTATUS_NOTCOMPLETE;
                         $newstu = new student($student_data);
                         $status = $newstu->add();
                         if ($status !== true) {
                             if (!empty($status->message)) {
                                 echo cm_error(get_string('record_not_created_reason', 'block_curr_admin', $status->message));
                             } else {
                                 echo cm_error(get_string('record_not_created', 'block_curr_admin'));
                             }
                         }
                     }
                 }
             }
         }
     }
     $this->action_default();
 }
 function action_savewaitlist()
 {
     global $USER, $CURMAN;
     $classid = cm_get_param('id', 0, PARAM_INT);
     $form = $this->create_waitlistform($classid);
     if ($form->is_cancelled()) {
         $this->action_available();
     } else {
         if ($data = $form->get_data()) {
             $class = new cmclass($classid);
             $userid = cm_get_crlmuserid($USER->id);
             $position = $CURMAN->db->get_field(WATLSTTABLE, sql_max('position'), 'classid', $classid) + 1;
             $wait_record = new object();
             $wait_record->userid = $userid;
             $wait_record->classid = $classid;
             $wait_record->enrolmenttime = $class->startdate;
             $wait_record->timecraeted = time();
             $wait_record->position = $position;
             $wait_list = new waitlist($wait_record);
             $wait_list->add();
             $this->action_waitlist();
         }
     }
 }