Skip to content

Commit

Permalink
Resolve : mix of inline php and functions in the same files. #10
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericgrebotasd committed Feb 29, 2024
1 parent c2f1d7d commit 64518d5
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 57 deletions.
115 changes: 58 additions & 57 deletions emailconfirmation.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

require_once(__DIR__ . '/../../config.php');
require_once('lib.php');

require_login();

Expand Down Expand Up @@ -52,7 +53,7 @@
redirect(new moodle_url($returnurl));
} else if ($fromform = $mform->get_data()) {
// In this case you process validated data. $mform->get_data() returns data posted in form.
$infoapiuser = local_ibob_get_info_user($fromform->emailconfirmationcode);
$infoapiuser = get_info_user($fromform->emailconfirmationcode);
$returnedhtml = html_writer::start_div();
if ($infoapiuser) {
if (time() > $infoapiuser->confirmation_expiration_date) { // Valid code but expiration date reached.
Expand All @@ -61,8 +62,8 @@
$returnlink = html_writer::link(new moodle_url($urlnewcode), get_string('emailconfirmationlinknewcode', 'local_ibob'));
$returnedhtml .= html_writer::tag('p', $returnlink);
} else { // Code is valid.
local_ibob_update_api_key_user($infoapiuser);
local_ibob_delete_badges_user($infoapiuser->user_id);
update_api_key_user($infoapiuser);
delete_badges_user($infoapiuser->user_id);
redirect(new moodle_url($returnurl));
}
} else { // Code is invalid.
Expand All @@ -86,57 +87,57 @@
$content .= $mform->render();
$content .= $OUTPUT->footer();
echo $content;

/**
* Delete user badge.
*
* @param int $userid
* @return void
*/
function local_ibob_delete_badges_user(int $userid) {
global $DB;
$DB->delete_records('local_ibob_badge_issued', ['userid' => $userid]);
}

/**
* Update api user key.
*
* @param object $infoapiuser
* @return mixed
*/
function local_ibob_update_api_key_user($infoapiuser) {
global $DB;
$ouserapikey = new stdClass();
$ouserapikey->id = $infoapiuser->id;
$ouserapikey->timemodified = time();
$ouserapikey->key_field = json_encode(['email' => $infoapiuser->confirmation_email_wanted]);
$ouserapikey->confirmation_needed = 0;
$ouserapikey->confirmation_code = '';
$ouserapikey->confirmation_expiration_date = null;
$ouserapikey->confirmation_email_wanted = '';
$apikeyuser = $DB->update_record('local_ibob_user_apikey', $ouserapikey);
return $apikeyuser;
}

/**
* Get user info.
*
* @param int $code
* @return mixed
*/
function local_ibob_get_info_user(int $code) {
global $DB;
$sql = "SELECT *
FROM {local_ibob_user_apikey}
WHERE confirmation_code = :confirmation_code";
return $DB->get_record_sql($sql, ["confirmation_code" => $code]);
}

/**
* Generate confirmation code.
*
* @return int
*/
function local_ibob_generate_confirmation_code() {
return mt_rand(1000, 9999);
}
//
///**
// * Delete user badge.
// *
// * @param int $userid
// * @return void
// */
//function delete_badges_user(int $userid) {
// global $DB;
// $DB->delete_records('local_ibob_badge_issued', ['userid' => $userid]);
//}
//
///**
// * Update api user key.
// *
// * @param object $infoapiuser
// * @return mixed
// */
//function update_api_key_user($infoapiuser) {
// global $DB;
// $ouserapikey = new stdClass();
// $ouserapikey->id = $infoapiuser->id;
// $ouserapikey->timemodified = time();
// $ouserapikey->key_field = json_encode(['email' => $infoapiuser->confirmation_email_wanted]);
// $ouserapikey->confirmation_needed = 0;
// $ouserapikey->confirmation_code = '';
// $ouserapikey->confirmation_expiration_date = null;
// $ouserapikey->confirmation_email_wanted = '';
// $apikeyuser = $DB->update_record('local_ibob_user_apikey', $ouserapikey);
// return $apikeyuser;
//}
//
///**
// * Get user info.
// *
// * @param int $code
// * @return mixed
// */
//function get_info_user(int $code) {
// global $DB;
// $sql = "SELECT *
// FROM {local_ibob_user_apikey}
// WHERE confirmation_code = :confirmation_code";
// return $DB->get_record_sql($sql, ["confirmation_code" => $code]);
//}
//
///**
// * Generate confirmation code.
// *
// * @return int
// */
//function generate_confirmation_code() {
// return mt_rand(1000, 9999);
//}
55 changes: 55 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,58 @@ function local_ibob_extend_settings_navigation(settings_navigation $navigation)
}
}
}


/**
* Delete user badge.
*
* @param int $userid
* @return void
*/
function delete_badges_user(int $userid) {
global $DB;
$DB->delete_records('local_ibob_badge_issued', ['userid' => $userid]);
}

/**
* Update api user key.
*
* @param object $infoapiuser
* @return mixed
*/
function update_api_key_user($infoapiuser) {
global $DB;
$ouserapikey = new stdClass();
$ouserapikey->id = $infoapiuser->id;
$ouserapikey->timemodified = time();
$ouserapikey->key_field = json_encode(['email' => $infoapiuser->confirmation_email_wanted]);
$ouserapikey->confirmation_needed = 0;
$ouserapikey->confirmation_code = '';
$ouserapikey->confirmation_expiration_date = null;
$ouserapikey->confirmation_email_wanted = '';
$apikeyuser = $DB->update_record('local_ibob_user_apikey', $ouserapikey);
return $apikeyuser;
}

/**
* Get user info.
*
* @param int $code
* @return mixed
*/
function get_info_user(int $code) {
global $DB;
$sql = "SELECT *
FROM {local_ibob_user_apikey}
WHERE confirmation_code = :confirmation_code";
return $DB->get_record_sql($sql, ["confirmation_code" => $code]);
}

/**
* Generate confirmation code.
*
* @return int
*/
function generate_confirmation_code() {
return mt_rand(1000, 9999);
}

0 comments on commit 64518d5

Please sign in to comment.