Refactoring the function to create a record

This commit is contained in:
Valentin Popov 2017-12-03 18:50:16 +04:00
parent 7a0a20fcdc
commit adbe7b68f6

42
lib.php
View File

@ -39,7 +39,7 @@ function tool_managertokens_activate_token($token = "") {
$selecttimelimited = "timelimited = 0 OR (timecreated + timelimited) > " . time(); $selecttimelimited = "timelimited = 0 OR (timecreated + timelimited) > " . time();
$select = "enabled = 1 AND token = '$token' AND ($selectlimited) AND ($selecttimelimited)"; $select = "enabled = 1 AND token = '$token' AND ($selectlimited) AND ($selecttimelimited)";
if ($token = $DB->get_record_select("tool_managertokens_tokens", $select, null, "*", IGNORE_MISSING)) { if ($token = $DB->get_record_select("tool_managertokens_tokens", $select, null, "*", IGNORE_MISSING)) {
$token->scope = intval($token->scope) + 1; $token->scope = intval($token->scope) + 1;
$token->timelastuse = time(); $token->timelastuse = time();
$DB->update_record("tool_managertokens_tokens", $token, false); $DB->update_record("tool_managertokens_tokens", $token, false);
} }
@ -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);