Ejemplo n.º 1
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;
}
Ejemplo n.º 2
0
 /**
  * Performs class_enrolment update
  * @throws moodle_exception If there was an error in passed parameters.
  * @throws data_object_exception If there was an error creating the entity.
  * @param array $data The incoming data parameter.
  * @return array An array of parameters, if successful.
  */
 public static function class_enrolment_update(array $data)
 {
     global $USER, $DB;
     if (static::require_elis_dependencies() !== true) {
         throw new moodle_exception('ws_function_requires_elis', 'local_datahub');
     }
     static $completestatuses = array('not completed' => STUSTATUS_NOTCOMPLETE, 'not_completed' => STUSTATUS_NOTCOMPLETE, 'notcompleted' => STUSTATUS_NOTCOMPLETE, 'failed' => 'STUSTATUS_FAILED', 'passed' => STUSTATUS_PASSED);
     // Parameter validation.
     $params = self::validate_parameters(self::class_enrolment_update_parameters(), array('data' => $data));
     // Context validation.
     $context = context_user::instance($USER->id);
     self::validate_context($context);
     $data = (object) $data;
     // Parse class
     if (empty($data->class_idnumber) || !($classid = $DB->get_field(pmclass::TABLE, 'id', array('idnumber' => $data->class_idnumber)))) {
         throw new data_object_exception('ws_class_enrolment_update_fail_invalid_class', 'local_datahub', '', $data);
     }
     // Capability checking.
     require_capability('local/elisprogram:class_enrol', \local_elisprogram\context\pmclass::instance($classid));
     // Initialize version1elis importplugin for utility functions.
     $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis');
     $userparams = array();
     $userid = $importplugin->get_userid_from_record($data, '', $userparams);
     if ($userid == false) {
         $a = new stdClass();
         if (empty($userparams)) {
             $a->userparams = '{empty}';
         } else {
             $a->userparams = '';
             foreach ($userparams as $userfield => $uservalue) {
                 $subfield = strpos($userfield, '_');
                 $userfield = substr($userfield, $subfield === false ? 0 : $subfield + 1);
                 if (!empty($a->userparams)) {
                     $a->userparams .= ', ';
                 }
                 $a->userparams .= "{$userfield}: '{$uservalue}'";
             }
         }
         throw new data_object_exception('ws_class_enrolment_update_fail_invalid_user', 'local_datahub', '', $a);
     }
     if (!($stuid = $DB->get_field(student::TABLE, 'id', array('classid' => $classid, 'userid' => $userid)))) {
         throw new data_object_exception('ws_class_enrolment_update_fail_missing_enrolment', 'local_datahub', '', $data);
     }
     $stu = new student($stuid);
     $stu->load();
     $record = new stdClass();
     $record->userid = $userid;
     $record->classid = $classid;
     if (isset($data->completestatus)) {
         $completestatus = strtolower($data->completestatus);
         if (isset($completestatuses[$completestatus])) {
             $record->completestatusid = $completestatuses[$completestatus];
             if ($record->completestatusid != STUSTATUS_NOTCOMPLETE) {
                 if (empty($stu->completetime) && !isset($data->completetime)) {
                     $record->completetime = rlip_timestamp();
                 }
             }
         } else {
             throw new data_object_exception('ws_class_enrolment_update_fail_invalid_completestatus', 'local_datahub', '', $data);
         }
     }
     if (isset($data->grade) && is_numeric($data->grade)) {
         $record->grade = $data->grade;
     }
     if (isset($data->credits) && is_numeric($data->credits)) {
         $record->credits = $data->credits;
     }
     if (isset($data->locked)) {
         $record->locked = $data->locked ? 1 : 0;
     }
     if (isset($data->enrolmenttime)) {
         if ($enrolmenttime = $importplugin->parse_date($data->enrolmenttime)) {
             $record->enrolmenttime = $enrolmenttime;
         } else {
             throw new data_object_exception('ws_class_enrolment_update_fail_invalid_enrolmenttime', 'local_datahub', '', $data);
         }
     }
     if (isset($data->completetime)) {
         if ($completetime = $importplugin->parse_date($data->completetime)) {
             $record->completetime = $completetime;
         } else {
             throw new data_object_exception('ws_class_enrolment_update_fail_invalid_completetime', 'local_datahub', '', $data);
         }
     }
     $stu->set_from_data($record);
     $stu->save();
     // Respond.
     if (!empty($stu->id)) {
         return array('messagecode' => get_string('ws_class_enrolment_update_success_code', 'local_datahub'), 'message' => get_string('ws_class_enrolment_update_success_msg', 'local_datahub'), 'record' => $stu->to_array());
     } else {
         throw new data_object_exception('ws_class_enrolment_update_fail', 'local_datahub');
     }
 }