From adbe7b68f6e9187e9a507361e650377681f4a36b Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sun, 3 Dec 2017 18:50:16 +0400 Subject: Refactoring the function to create a record --- lib.php | 42 ++++++++++-------------------------------- 1 file changed, 10 insertions(+), 32 deletions(-) diff --git a/lib.php b/lib.php index cb54ac5..8168dbe 100644 --- a/lib.php +++ b/lib.php @@ -39,7 +39,7 @@ function tool_managertokens_activate_token($token = "") { $selecttimelimited = "timelimited = 0 OR (timecreated + timelimited) > " . time(); $select = "enabled = 1 AND token = '$token' AND ($selectlimited) AND ($selecttimelimited)"; 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(); $DB->update_record("tool_managertokens_tokens", $token, false); } @@ -67,45 +67,23 @@ function tool_managertokens_create_backup() { function tool_managertokens_create_record($options) { 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->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->targetid = intval($options->targetid); - $token->targettype = strval($options->targettype); - $token->timemodified = $token->timecreated; - $token->token = strval($options->token); - - if (!empty($options->enabled)) { - $token->enabled = boolval($options->enabled); - } + $token->timelimited = !empty($options->timelimited) ? intval($options->timelimited) : 0; + $token->timemodified = time(); + $token->token = !empty($options->token) ? strval($options->token) : generate_password(12); if (!empty($options->extendedaction) && !empty($options->extendedoptions)) { $token->extendedaction = strval($options->extendedaction); $token->extendedoptions = strval($options->extendedoptions); } - if (!empty($options->limited)) { - $token->limited = intval($options->limited); - } - - if (!empty($options->timelimited)) { - $token->timelimited = intval($options->timelimited); + if ($DB->record_exists("tool_managertokens_tokens", array("token" => $token->token))) { + print_error("duplicatefieldname", "error", null, "token"); } $recordid = $DB->insert_record("tool_managertokens_tokens", $token, true, false); -- cgit v1.2.3