/** * Performs program_enrolment creation * @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 program_enrolment_create(array $data) { global $USER, $DB; if (static::require_elis_dependencies() !== true) { throw new moodle_exception('ws_function_requires_elis', 'local_datahub'); } // Parameter validation. $params = self::validate_parameters(self::program_enrolment_create_parameters(), array('data' => $data)); // Context validation. $context = context_user::instance($USER->id); self::validate_context($context); $data = (object) $data; // Parse program if (empty($data->program_idnumber) || !($curid = $DB->get_field(curriculum::TABLE, 'id', array('idnumber' => $data->program_idnumber)))) { throw new data_object_exception('ws_program_enrolment_create_fail_invalid_program', 'local_datahub', '', $data); } // Capability checking. require_capability('local/elisprogram:program_enrol', \local_elisprogram\context\program::instance($curid)); // 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_program_enrolment_create_fail_invalid_user', 'local_datahub', '', $a); } $record = new stdClass(); $record->userid = $userid; $record->curriculumid = $curid; if (isset($data->credits)) { $record->credits = $data->credits; } if (isset($data->locked)) { $record->locked = $data->locked ? 1 : 0; } if (isset($data->timecompleted)) { $record->timecompleted = $importplugin->parse_date($data->timecompleted); } if (isset($data->timeexpired)) { $record->timeexpired = $importplugin->parse_date($data->timeexpired); } $stucur = new curriculumstudent($record); $stucur->save(); // Respond. if (!empty($stucur->id)) { return array('messagecode' => get_string('ws_program_enrolment_create_success_code', 'local_datahub'), 'message' => get_string('ws_program_enrolment_create_success_msg', 'local_datahub'), 'record' => $stucur->to_array()); } else { throw new data_object_exception('ws_program_enrolment_create_fail', 'local_datahub'); } }