/** * Create a test curriculum. * @return curriculum The test curriculum. */ public function create_curriculum(field &$field = null) { $data = new stdClass(); $data->courseid = ''; $data->idnumber = 'testprg'; $data->name = 'Test Program'; $data->description = ''; $data->reqcredits = ''; $data->priority = '0'; $data->timetocomplete = ''; $data->frequency = ''; if (!empty($field)) { $fieldvar = 'field_' . $field->shortname; $data->{$fieldvar} = 'test field data'; } $cur = new curriculum(); $cur->set_from_data($data); $cur->save(); return $cur; }
/** * 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'); } }
function curriculum_update($record, $filename) { global $CFG, $DB; //field length checking $lengthcheck = $this->check_program_field_lengths($record, $filename); if (!$lengthcheck) { return false; } if (isset($record->idnumber)) { if (!$DB->record_exists(curriculum::TABLE, array('idnumber' => $record->idnumber))) { $identifier = $this->get_field_mapping('idnumber'); $this->fslogger->log_failure("{$identifier} value of \"{$record->idnumber}\" does not refer to a valid program.", 0, $filename, $this->linenumber, $record, "curriculum"); return false; } } if (!$this->validate_program_data('update', $record, $filename)) { return false; } $record = $this->add_custom_field_prefixes($record); //custom field validation if (!$this->validate_custom_field_data('update', $record, $filename, 'curriculum')) { return false; } $id = $DB->get_field(curriculum::TABLE, 'id', array('idnumber' => $record->idnumber)); $record->id = $id; if (!$this->validate_program_data('update', $record, $filename)) { return false; } $cur = new curriculum(); $cur->set_from_data($record); $cur->save(); //log success $success_message = "Program with idnumber \"{$record->idnumber}\" successfully updated."; $this->fslogger->log_success($success_message, 0, $filename, $this->linenumber); return true; }