Exemplo n.º 1
0
 /**
  * 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;
 }
Exemplo n.º 2
0
/**
 * Notifies that students have not passed their classes via the notifications where applicable,
 * setting enrolment status to failed where applicable
 *
 * @param int $pmuserid  optional userid to update, default(0) updates all users
 */
function pm_update_student_enrolment($pmuserid = 0)
{
    global $DB;
    require_once elispm::lib('data/student.class.php');
    require_once elispm::lib('notifications.php');
    //look for all enrolments where status is incomplete / in progress and end time has passed
    $select = 'completestatusid = :status AND endtime > 0 AND endtime < :time';
    $params = array('status' => STUSTATUS_NOTCOMPLETE, 'time' => time());
    if ($pmuserid) {
        $select .= ' AND userid = :userid';
        $params['userid'] = $pmuserid;
    }
    $students = $DB->get_recordset_select(student::TABLE, $select, $params);
    if (!empty($students)) {
        foreach ($students as $s) {
            // Send notification, if enabled.
            $sendnotification = !empty(elis::$config->local_elisprogram->notify_incompletecourse_user) ? true : false;
            if ($sendnotification === true) {
                $a = $DB->get_field(pmclass::TABLE, 'idnumber', array('id' => $s->classid));
                $message = get_string('incomplete_course_message', 'local_elisprogram', $a);
                $cuser = new user($s->userid);
                $from = get_admin();
                notification::notify($message, $cuser, $from);
            }
            //set status to failed
            $s->completetime = time();
            $s->completestatusid = STUSTATUS_FAILED;
            $stu = new student($s->id);
            $stu->set_from_data($s);
            $stu->update();
        }
    }
    return true;
}
Exemplo n.º 3
0
 /**
  *
  */
 public function save()
 {
     $new = false;
     try {
         validation_helper::is_unique_userid_classid($this);
     } catch (Exception $e) {
         // already on waitlist
         return true;
     }
     if (!isset($this->id)) {
         $new = true;
         if (empty($this->position)) {
             $max = $this->_db->get_field(waitlist::TABLE, 'MAX(position)', array('classid' => $this->classid));
             $this->position = $max + 1;
         }
     }
     parent::save();
     $sendnotification = !empty(elis::$config->local_elisprogram->notify_addedtowaitlist_user) ? true : false;
     if ($new && $sendnotification === true) {
         $subject = get_string('waitlist', self::LANG_FILE);
         $pmclass = new pmclass($this->classid);
         $sparam = new stdClass();
         $sparam->idnumber = $pmclass->idnumber;
         $message = get_string('added_to_waitlist_message', self::LANG_FILE, $sparam);
         $cuser = new user($this->userid);
         $cuser->load();
         $from = get_admin();
         notification::notify($message, $cuser, $from);
         //email_to_user($user, $from, $subject, $message); // *TBD*
     }
 }
Exemplo n.º 4
0
 /**
  *
  * @global <type> $CURMAN
  */
 public function add()
 {
     global $CURMAN;
     if (empty($this->position)) {
         //SELECT MIN(userid) FROM eli_crlm_wait_list WHERE 1
         $sql = 'SELECT ' . sql_max('position') . ' as max 
                 FROM ' . $CURMAN->db->prefix_table(WATLSTTABLE) . ' as wl
                 WHERE wl.classid = ' . $this->classid;
         $max_record = get_record_sql($sql);
         $max = $max_record->max;
         $this->position = $max + 1;
     }
     $subject = get_string('waitlist', 'block_curr_admin');
     $cmclass = new cmclass($this->classid);
     $message = get_string('added_to_waitlist_message', 'block_curr_admin', $cmclass->idnumber);
     $user = cm_get_moodleuser($this->userid);
     $from = get_admin();
     // Send a notification and e-mail only if the Moodle user exists
     if ($user != null) {
         notification::notify($message, $user, $from);
         email_to_user($user, $from, $subject, $message);
     }
     parent::add();
 }
Exemplo n.º 5
0
/**
 *
 */
function cm_update_student_enrolment()
{
    global $CURMAN;
    //for every student
    $select = 'completestatusid = ' . STUSTATUS_NOTCOMPLETE . ' AND endtime > 0 AND endtime < ' . time();
    $students = $CURMAN->db->get_records_select(STUTABLE, $select);
    if (!empty($students)) {
        foreach ($students as $s) {
            $a = $CURMAN->db->get_field('crlm_class', 'idnumber', 'id', $s->classid);
            $subject = get_string('incomplete_course_subject', 'block_curr_admin');
            $message = get_string('incomplete_course_message', 'block_curr_admin', $a);
            $user = cm_get_moodleuser($s->userid);
            $from = get_admin();
            notification::notify($message, $user, $from);
            email_to_user($user, $from, $subject, $message);
            $s->completetime = 0;
            $s->completestatusid = STUSTATUS_FAILED;
            $CURMAN->db->update_record(STUTABLE, $s);
        }
    }
    return true;
}
Exemplo n.º 6
0
 /**
  * Helper method for performing actions specific to the back-end approval process
  *
  * @param  object  $request   the request record
  * @param  object  $formdata  the submitted form data
  * @uses   $DB
  * @uses   $USER
  */
 function approve_request($request, $formdata)
 {
     global $DB, $USER;
     // process course-specific actions
     $courseid = $this->approve_request_course_actions($request, $formdata);
     // process class-specific actions
     $this->approve_request_class_actions($request, $formdata, $courseid);
     // update the request with additional information
     $request->requeststatus = 'approved';
     $request->statusnote = $formdata->comments;
     $DB->update_record('block_courserequest', $request);
     // TBV: addslashes_recursive() not required in Moodle 2 ?
     // notify the user of the request status
     $statusnote = null;
     if (!empty($request->statusnote)) {
         $statusnote = $request->statusnote;
     }
     $request->newcourseid = $courseid;
     $notice = block_courserequest_get_approval_message($request, $statusnote);
     $requser = $DB->get_record('user', array('id' => $request->userid));
     $requser->firstname = $formdata->first;
     $requser->lastname = $formdata->last;
     $requser->email = $formdata->email;
     notification::notify($notice, $requser, $USER);
     // redirect back to the listing page
     redirect($this->url, get_string('requestapproved', 'block_courserequest'), 3);
 }
Exemplo n.º 7
0
 function display_create()
 {
     // action_create()
     global $CFG, $DB;
     $target = $this->get_new_page(array('action' => 'create'), true);
     $form = new create_form($target->url);
     $data = $form->get_data();
     if ($form->is_cancelled()) {
         redirect($this->url);
         // TBV
         return;
     } else {
         if ($data) {
             global $USER;
             require_once $CFG->dirroot . '/local/elisprogram/lib/data/course.class.php';
             $request = new stdClass();
             $request->userid = $USER->id;
             $request->firstname = $data->first;
             $request->lastname = $data->last;
             $request->email = $data->email;
             if (empty($data->title)) {
                 $course_record = $DB->get_record(course::TABLE, array('id' => $data->courseid));
                 $data->title = $course_record->name;
             }
             $request->title = $data->title;
             $request->courseid = $data->courseid;
             $request->usecoursetemplate = empty($data->usecoursetemplate) ? 0 : 1;
             $request->requeststatus = 'pending';
             $request->timemodified = $request->timecreated = time();
             $id = $DB->insert_record('block_courserequest', $request);
             $fields = $DB->get_records('block_courserequest_fields');
             $fields = $fields ? $fields : array();
             foreach ($fields as $reqfield) {
                 $field = new field($reqfield->fieldid);
                 if (!$field->id || !isset($field->owners['manual'])) {
                     // skip nonexistent fields, or fields without manual editing
                     continue;
                 }
                 $fielddata = new stdClass();
                 $fielddata->requestid = $id;
                 $fielddata->fieldid = $reqfield->fieldid;
                 // key that represents the appropriate attribute on the object
                 $field_key = "field_{$field->shortname}";
                 // make sure the field was enabled, especially for preventing the
                 // storage of course fields when using an existing course
                 if (isset($data->{$field_key})) {
                     // check for multiple fields
                     if (is_array($data->{$field_key})) {
                         $fielddata->data = serialize($data->{$field_key});
                         $fielddata->multiple = '1';
                     } else {
                         $fielddata->data = $data->{$field_key};
                     }
                     // remember the context level that the field corresponds to
                     $fielddata->contextlevel = $reqfield->contextlevel;
                     $DB->insert_record('block_courserequest_data', $fielddata);
                 }
             }
             require_once $CFG->dirroot . '/local/elisprogram/lib/notifications.php';
             $syscontext = context_system::instance();
             $config = get_config('block_courserequest');
             if (has_capability('block/courserequest:approve', $syscontext)) {
                 // Since we want to automatically approve requests for people approval permission, let's go ahead and create the course/class
                 $requestid = $id;
                 if (!($request = $DB->get_record('block_courserequest', array('id' => $requestid)))) {
                     $target = str_replace($CFG->wwwroot, '', $this->url);
                     print_error('errorinvalidrequestid', 'block_courserequest', $target, $requestid);
                 }
                 $target = $this->get_new_page(array('action' => 'approveconfirm'));
                 $approveform = new pending_request_approve_form($target->url, $request);
                 $request->request = $request->id;
                 $approveform->set_data($request);
                 // We're not actually approving the request, redirect back to the approval table.
                 if ($approveform->is_cancelled()) {
                     redirect($this->url, '', 0);
                 }
                 // Do we have to create a brand new course?
                 if (empty($request->courseid)) {
                     $crsdata = array('name' => $request->title, 'idnumber' => $data->crsidnumber, 'syllabus' => '');
                     $newcourse = new course($crsdata);
                     if (!empty($config->use_course_fields)) {
                         // course fields are enabled, so add the relevant data
                         $this->add_custom_fields($request->id, 'course', $newcourse);
                     }
                     $newcourse->save();
                     // ->add()
                     // do the course role assignment, if applicable
                     if (!empty($config->course_role)) {
                         if ($context = \local_elisprogram\context\course::instance($newcourse->id)) {
                             // TBD: role_assign() now throws exceptions!
                             $result = role_assign($config->course_role, $request->userid, $context->id, ECR_CD_ROLE_COMPONENT);
                         }
                     }
                     $courseid = $newcourse->id;
                 } else {
                     $courseid = $request->courseid;
                     // TBV: addslashes()
                 }
                 // Create the new class if we are using an existing course, or if
                 // create_class_with_course is on.
                 if (!empty($request->courseid) || !empty($config->create_class_with_course)) {
                     require_once $CFG->dirroot . '/local/elisprogram/lib/data/pmclass.class.php';
                     $clsdata = array('name' => $request->title, 'courseid' => $courseid, 'idnumber' => $data->clsidnumber, 'starttimehour' => 25, 'starttimeminute' => 61, 'endtimehour' => 25, 'endtimeminute' => 61);
                     $newclass = new pmclass($clsdata);
                     $newclass->autocreate = false;
                     if (!empty($config->use_class_fields)) {
                         // class fields are enabled, so add the relevant data
                         $this->add_custom_fields($request->id, 'class', $newclass);
                     }
                     $newclass->save();
                     // ->add()
                 }
                 // Update the request record to mark it as being approved.
                 $request->requeststatus = 'approved';
                 $request->statusnote = '';
                 $DB->update_record('block_courserequest', $request);
                 // TBV: addslashes_object()
                 // assign role to requester in the newly created class
                 if (!empty($newclass->id)) {
                     if (!empty($config->class_role)) {
                         $context = \local_elisprogram\context\pmclass::instance($newclass->id);
                         // TBD: role_assign() now throws exceptions!
                         role_assign($config->class_role, $request->userid, $context->id, ECR_CI_ROLE_COMPONENT);
                     }
                 }
                 // create a new Moodle course from the course template if applicable
                 if (!empty($data->usecoursetemplate) && !empty($newclass->id)) {
                     moodle_attach_class($newclass->id, 0, '', true, true, true);
                     // copy role over into Moodle course
                     if (!empty($config->class_role)) {
                         require_once $CFG->dirroot . '/local/elisprogram/lib/data/classmoodlecourse.class.php';
                         if ($class_moodle_record = $DB->get_record(classmoodlecourse::TABLE, array('classid' => $newclass->id))) {
                             $context = context_course::instance($class_moodle_record->moodlecourseid);
                             // TBD: role_assign() now throws exceptions!
                             role_assign($config->class_role, $request->userid, $context->id, ECR_MC_ROLE_COMPONENT);
                         }
                     }
                 }
                 // Send a notification to the requesting user that their course / class is ready.
                 // set additional course / class information for use in the message
                 if (isset($newclass->idnumber) && isset($newclass->id)) {
                     $request->classidnumber = $newclass->idnumber;
                     $request->classid = $newclass->id;
                 }
                 $request->newcourseid = $courseid;
                 // calculate the actual message
                 $notice = block_courserequest_get_approval_message($request);
                 // send it to the requester
                 notification::notify($notice, $DB->get_record('user', array('id' => $request->userid)));
                 print_string('request_submitted_and_auto_approved', 'block_courserequest');
             } else {
                 // find users with approve capabilities in the system context
                 $admin = get_users_by_capability($syscontext, 'block/courserequest:approve');
                 foreach ($admin as $userto) {
                     notification::notify(get_string('new_request_notification', 'block_courserequest', $data), $userto);
                 }
                 print_string('request_submitted', 'block_courserequest');
             }
             redirect($this->url);
             return;
         }
     }
     $form->display();
 }