aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorValentin Popov <info@valentineus.link>2017-12-03 17:36:34 +0300
committerValentin Popov <info@valentineus.link>2017-12-03 17:36:34 +0300
commit7a0a20fcdc103623e369a160ef31f52c17ab36a2 (patch)
tree595839574f12f678c0ab9cecbe04503ac76728b8
parent44a4729bab82e8bae3990ca9ca815257ca64374b (diff)
downloadtool_managertokens-7a0a20fcdc103623e369a160ef31f52c17ab36a2.tar.xz
tool_managertokens-7a0a20fcdc103623e369a160ef31f52c17ab36a2.zip
Added additional action handlers
-rw-r--r--lib.php96
1 files changed, 63 insertions, 33 deletions
diff --git a/lib.php b/lib.php
index aab80b7..cb54ac5 100644
--- a/lib.php
+++ b/lib.php
@@ -24,6 +24,8 @@
defined("MOODLE_INTERNAL") || die();
+require_once(__DIR__ . "/locallib.php");
+
/**
* It checks the ability to activate the token and produces it.
*
@@ -37,8 +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 = tool_managertokens_standardization_record($token);
- $token->scope = $token->scope + 1;
+ $token->scope = intval($token->scope) + 1;
$token->timelastuse = time();
$DB->update_record("tool_managertokens_tokens", $token, false);
}
@@ -67,11 +68,11 @@ function tool_managertokens_create_record($options) {
global $DB;
if (!isset($options->targetid)) {
- print_error("missingparam", "error", "", "targetid");
+ print_error("missingparam", "error", null, "targetid");
}
if (!isset($options->targettype)) {
- $options->targettype = "user";
+ $options->targettype = "null";
}
if (!isset($options->token)) {
@@ -79,7 +80,7 @@ function tool_managertokens_create_record($options) {
}
if ($DB->record_exists("tool_managertokens_tokens", array("token" => $options->token))) {
- print_error("duplicatefieldname", "error", "", "token");
+ print_error("duplicatefieldname", "error", null, "token");
}
$token = new stdClass();
@@ -112,6 +113,21 @@ function tool_managertokens_create_record($options) {
}
/**
+ * Specifies the user for authorization.
+ *
+ * @param object $token
+ */
+function tool_managertokens_definition_user($token) {
+ $user = false;
+
+ if ($token->targettype == "user") {
+ $user = core_user::get_user($token->targetid);
+ }
+
+ return $user;
+}
+
+/**
* Removes all entries in the table.
* Attention! All current records are deleted!
*
@@ -151,10 +167,7 @@ function tool_managertokens_find_record($key = 0) {
global $DB;
$select = "id = '$key' OR token = '$key'";
- if ($token = $DB->get_record_select("tool_managertokens_tokens", $select, null, "*", IGNORE_MISSING)) {
- $token = tool_managertokens_standardization_record($token);
- }
-
+ $token = $DB->get_record_select("tool_managertokens_tokens", $select, null, "*", IGNORE_MISSING);
return $token;
}
@@ -173,6 +186,46 @@ function tool_managertokens_get_list($limitfrom = 0, $limitnum = 0) {
}
/**
+ * Performs additional actions for the user.
+ *
+ * @param object $token
+ * @param object $user
+ */
+function tool_managertokens_perform_additional_action($token, $user) {
+ global $DB;
+
+ /* Redirect user */
+ if ($token->extendedaction == "redirect") {
+ $redirect = new moodle_url($token->extendedoptions);
+ redirect($redirect);
+ }
+
+ /* Enroll in the local group */
+ if ($token->extendedaction == "group") {
+ $groupid = intval($token->extendedoptions);
+ if ($DB->record_exists("groups", array("id" => $groupid))) {
+ tool_managertokens_enroll_user_for_group($user, $groupid);
+ }
+ }
+
+ /* Enroll in the global group */
+ if ($token->extendedaction == "cohort") {
+ $cohortid = intval($token->extendedoptions);
+ if ($DB->record_exists("cohort", array("id" => $cohortid))) {
+ tool_managertokens_enroll_user_for_cohort($user, $cohortid);
+ }
+ }
+
+ /* Enroll in the global course */
+ if ($token->extendedaction == "course") {
+ $courseid = intval($token->extendedoptions);
+ if ($DB->record_exists("course", array("id" => $courseid))) {
+ tool_managertokens_enroll_user_for_course($user, $courseid);
+ }
+ }
+}
+
+/**
* Restores data from a backup.
* Attention! All current records are deleted!
*
@@ -199,7 +252,7 @@ function tool_managertokens_update_record($options) {
$result = false;
if (!isset($options->id)) {
- print_error("missingparam", "error", "", "id");
+ print_error("missingparam", "error", null, "id");
}
if ($token = $DB->get_record("tool_managertokens_tokens", array("id" => $options->id), "*", IGNORE_MISSING)) {
@@ -238,27 +291,4 @@ function tool_managertokens_update_record($options) {
}
return boolval($result);
-}
-
-/**
- * Standardizes the source document.
- *
- * @param object $record
- * @return object
- */
-function tool_managertokens_standardization_record($record) {
- $record->id = intval($record->id);
- $record->enabled = boolval($record->enabled);
- $record->extendedaction = strval($record->extendedaction);
- $record->extendedoptions = strval($record->extendedoptions);
- $record->limited = intval($record->limited);
- $record->scope = intval($record->scope);
- $record->targetid = intval($record->targetid);
- $record->targettype = strval($record->targettype);
- $record->timecreated = intval($record->timecreated);
- $record->timelastuse = intval($record->timelastuse);
- $record->timelimited = intval($record->timelimited);
- $record->timemodified = intval($record->timemodified);
- $record->token = strval($record->token);
- return $record;
} \ No newline at end of file