/** * Performs program 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 program_update(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_update_parameters(), array('data' => $data)); // Context validation. $context = context_user::instance($USER->id); self::validate_context($context); $data = (object) $data; // Validate program exists if (!($curid = $DB->get_field(curriculum::TABLE, 'id', array('idnumber' => $data->idnumber)))) { throw new data_object_exception('ws_program_update_fail_invalid_idnumber', 'local_datahub', '', $data); } // Capability checking. require_capability('local/elisprogram:program_edit', \local_elisprogram\context\program::instance($curid)); // More validation if (isset($data->reqcredits)) { $reqcredits = (string) $data->reqcredits; $digits = strlen($reqcredits); $decies = 0; if (($decpos = strpos($reqcredits, '.')) !== false) { $decies = $digits - $decpos - 1; $digits = $decpos; } if (!is_numeric($reqcredits) || $digits > 8 || $decies > 2) { throw new data_object_exception('ws_program_update_fail_invalid_reqcredits', 'local_datahub', '', $data); } } if (isset($data->timetocomplete)) { $datedelta = new datedelta($data->timetocomplete); if (!$datedelta->getDateString()) { throw new data_object_exception('ws_program_update_fail_invalid_timetocomplete', 'local_datahub', '', $data); } } if (isset($data->frequency)) { $datedelta = new datedelta($data->frequency); if (!$datedelta->getDateString()) { throw new data_object_exception('ws_program_update_fail_invalid_frequency', 'local_datahub', '', $data); } } if (isset($data->priority)) { if ($data->priority < 0 || $data->priority > 10) { throw new data_object_exception('ws_program_update_fail_invalid_priority', 'local_datahub', '', $data); } } $prg = new curriculum($curid); $prg->load(); $prg->set_from_data($data); $prg->save(); // Respond. if (!empty($prg->id)) { $prgrec = (array) $DB->get_record(curriculum::TABLE, array('id' => $prg->id)); $prgobj = $prg->to_array(); // convert multi-valued custom field arrays to comma-separated listing $fields = self::get_program_custom_fields(); foreach ($fields as $field) { // Generate name using custom field prefix. $fullfieldname = data_object_with_custom_fields::CUSTOM_FIELD_PREFIX . $field->shortname; if ($field->multivalued && isset($prgobj[$fullfieldname]) && is_array($prgobj[$fullfieldname])) { $prgobj[$fullfieldname] = implode(',', $prgobj[$fullfieldname]); } } return array('messagecode' => get_string('ws_program_update_success_code', 'local_datahub'), 'message' => get_string('ws_program_update_success_msg', 'local_datahub'), 'record' => array_merge($prgrec, $prgobj)); } else { throw new data_object_exception('ws_program_update_fail', 'local_datahub'); } }