Refactoring the function to create a record
This commit is contained in:
parent
7a0a20fcdc
commit
adbe7b68f6
40
lib.php
40
lib.php
@ -67,45 +67,23 @@ function tool_managertokens_create_backup() {
|
|||||||
function tool_managertokens_create_record($options) {
|
function tool_managertokens_create_record($options) {
|
||||||
global $DB;
|
global $DB;
|
||||||
|
|
||||||
if (!isset($options->targetid)) {
|
|
||||||
print_error("missingparam", "error", null, "targetid");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isset($options->targettype)) {
|
|
||||||
$options->targettype = "null";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isset($options->token)) {
|
|
||||||
$options->token = generate_password(12);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($DB->record_exists("tool_managertokens_tokens", array("token" => $options->token))) {
|
|
||||||
print_error("duplicatefieldname", "error", null, "token");
|
|
||||||
}
|
|
||||||
|
|
||||||
$token = new stdClass();
|
$token = new stdClass();
|
||||||
$token->enabled = false;
|
$token->enabled = !empty($options->enabled) ? boolval($options->enabled) : false;
|
||||||
|
$token->limited = !empty($options->limited) ? intval($options->limited) : 0;
|
||||||
|
$token->targetid = !empty($options->targetid) ? intval($options->targetid) : 0;
|
||||||
|
$token->targettype = !empty($options->targettype) ? strval($options->targettype) : "null";
|
||||||
$token->timecreated = time();
|
$token->timecreated = time();
|
||||||
$token->targetid = intval($options->targetid);
|
$token->timelimited = !empty($options->timelimited) ? intval($options->timelimited) : 0;
|
||||||
$token->targettype = strval($options->targettype);
|
$token->timemodified = time();
|
||||||
$token->timemodified = $token->timecreated;
|
$token->token = !empty($options->token) ? strval($options->token) : generate_password(12);
|
||||||
$token->token = strval($options->token);
|
|
||||||
|
|
||||||
if (!empty($options->enabled)) {
|
|
||||||
$token->enabled = boolval($options->enabled);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($options->extendedaction) && !empty($options->extendedoptions)) {
|
if (!empty($options->extendedaction) && !empty($options->extendedoptions)) {
|
||||||
$token->extendedaction = strval($options->extendedaction);
|
$token->extendedaction = strval($options->extendedaction);
|
||||||
$token->extendedoptions = strval($options->extendedoptions);
|
$token->extendedoptions = strval($options->extendedoptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($options->limited)) {
|
if ($DB->record_exists("tool_managertokens_tokens", array("token" => $token->token))) {
|
||||||
$token->limited = intval($options->limited);
|
print_error("duplicatefieldname", "error", null, "token");
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($options->timelimited)) {
|
|
||||||
$token->timelimited = intval($options->timelimited);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$recordid = $DB->insert_record("tool_managertokens_tokens", $token, true, false);
|
$recordid = $DB->insert_record("tool_managertokens_tokens", $token, true, false);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user