Skip to content

Commit

Permalink
Add new design to referral page thanks to @Buda9
Browse files Browse the repository at this point in the history
  • Loading branch information
fmido88 authored Jun 16, 2024
1 parent 4628a87 commit da90679
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 13 deletions.
10 changes: 10 additions & 0 deletions amd/build/referral.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions amd/build/referral.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 134 additions & 0 deletions amd/src/referral.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* TODO describe module referral
*
* @module enrol_wallet/referral
* @copyright 2024 YOUR NAME <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

import $ from 'jquery';
import {get_strings} from 'core/str';
import Prefetch from 'core/prefetch';

// Prefetch required string while the page loading.
Prefetch.prefetchStrings('enrol_wallet', ['referral_copy_to_clipboard', 'referral_copied']);

let copyTooltip;
let copiedTooltip;
let shareUrl;
let copyButtonCode;
let copyButtonUrl;
let uniqueId;
/**
*
*/
function shareOnFacebook() {
let fbBase = 'https://www.facebook.com/sharer/sharer.php?u=';
window.open(fbBase + encodeURIComponent(shareUrl), 'facebook-share-dialog', 'width=800,height=600');
}

/**
*
*/
function shareOnWhatsApp() {
window.open('https://wa.me/?text=' + encodeURIComponent(shareUrl));
}

/**
*
*/
function shareOnTelegram() {
window.open('https://t.me/share/url?url=' + encodeURIComponent(shareUrl));
}

/**
* Copy the text in the input value.
* @param {string} target
* @param {DOMElement} element
*/
function copyText(target, element) {
let input = $('#' + target + '_' + uniqueId);

navigator.clipboard.writeText(input[0].value);

element.setAttribute('tooltip', copiedTooltip);
}

const resetTooltip = (element) => {
element.setAttribute('tooltip', copyTooltip);
};

/**
* Add listeners to copy and sharing buttons.
*/
function addListeners() {
copyButtonUrl.addEventListener('click', () => {
copyText('url', copyButtonUrl);
});
copyButtonUrl.addEventListener('mouseleave', () => {
resetTooltip(copyButtonUrl);
});
copyButtonCode.addEventListener('click', () => {
copyText('code', copyButtonCode);
});
copyButtonCode.addEventListener('mouseleave', () => {
resetTooltip(copyButtonCode);
});

$('a[href="#shareOnFacebook"]').on("click", () => {
shareOnFacebook();
});
$('a[href="#shareOnTelegram"]').on("click", () => {
shareOnTelegram();
});
$('a[href="#shareOnWhatsApp"]').on("click", () => {
shareOnWhatsApp();
});
}

/**
* Initiate the module.
*
* @param {string} url the referral url
* @param {string} id the unique id of the template
*/
export const init = (url, id) => {
var strings = [
{
key: 'referral_copy_to_clipboard',
component: 'enrol_wallet'
},
{
key: 'referral_copied',
component: 'enrol_wallet',
}
];
get_strings(strings).then(function(results) {
copyTooltip = results[0];
copiedTooltip = results[1];
return true;
}).fail(() => {
return false;
});
shareUrl = url;
uniqueId = id;
copyButtonUrl = document.getElementById('copy_url_' + id);
copyButtonCode = document.getElementById('copy_code_' + id);

addListeners();
};
27 changes: 20 additions & 7 deletions classes/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class pages {
*/
public static function process_referral_page($userid = 0) {
global $DB, $USER, $CFG, $OUTPUT;
if (!(bool)get_config( 'enrol_wallet', 'referral_enabled')) {
echo 'Referrals not enabled';
if (!(bool)get_config('enrol_wallet', 'referral_enabled')) {
echo get_string('referral_not_enabled', 'enrol_wallet');
return;
}

Expand All @@ -67,13 +67,14 @@ public static function process_referral_page($userid = 0) {
}

if ($isparent) {
echo 'Parents not allow to access referral program.';
echo get_string('referral_noparents', 'enrol_wallet');
return;
}

$amount = get_config('enrol_wallet', 'referral_amount');
$maxref = get_config('enrol_wallet', 'referral_max');

// If the referral code not exist for this user, create a new one.
$exist = $DB->get_record('enrol_wallet_referral', ['userid' => $user->id]);
if (!$exist) {
$data = (object)[
Expand All @@ -84,11 +85,23 @@ public static function process_referral_page($userid = 0) {
$exist = $DB->get_record('enrol_wallet_referral', ['userid' => $user->id]);
}

$holdgift = $DB->get_record('enrol_wallet_hold_gift', ['referred' => $user->username]);
$signup = new moodle_url('/login/signup.php', ['refcode' => $exist->code]);

// Check if there is a hold gift for this user.
$holdgift = $DB->get_record('enrol_wallet_hold_gift', ['referred' => $user->username]);
// Check if there is past referrals by this user.
$refusers = $DB->get_records('enrol_wallet_hold_gift', ['referrer' => $user->id]);

$output = '';

$templatedata = [
'amount' => $amount,
'url' => $signup->out(),
'code' => $exist->code,
'mail_subject' => rawurlencode(get_string('referral_share_subject', 'enrol_wallet')),
'mail_body' => rawurlencode(get_string('referral_share_body', 'enrol_wallet'))
];
$output .= $OUTPUT->render_from_template('enrol_wallet/referral', $templatedata);
if (!empty($holdgift)) {
$referrer = core_user::get_user($holdgift->referrer);
$a = [
Expand All @@ -99,6 +112,8 @@ public static function process_referral_page($userid = 0) {
$output .= $OUTPUT->notification($message);
}

$output .= html_writer::start_div('wrapper referral-page-past-invites');
$output .= $OUTPUT->heading(get_string('referral_past', 'enrol_wallet'));
if (!empty($refusers)) {
$table = new html_table;
$headers = [
Expand Down Expand Up @@ -126,10 +141,10 @@ public static function process_referral_page($userid = 0) {
$message = get_string('noreferraldata', 'enrol_wallet');
$output .= $OUTPUT->notification($message);
}
$output .= html_writer::end_div();

$mform = new MoodleQuickForm('referral_info', 'get', null);

$signup = new moodle_url('/login/signup.php', ['refcode' => $exist->code]);
$mform->addElement('static', 'refurl', get_string('referral_url', 'enrol_wallet'), $signup->out(false));
$mform->addHelpButton('refurl', 'referral_url', 'enrol_wallet');

Expand All @@ -155,8 +170,6 @@ public static function process_referral_page($userid = 0) {
$mform->disabledIf('refremain', 'disable', 'neq', 1);
}

echo $OUTPUT->heading(get_string('referral_past', 'enrol_wallet'));

echo $output;

echo $OUTPUT->heading(get_string('referral_data', 'enrol_wallet'));
Expand Down
9 changes: 4 additions & 5 deletions extra/referral.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,20 @@
$isparent = false;
if (file_exists("$CFG->dirroot/auth/parent/auth.php")) {
require_once("$CFG->dirroot/auth/parent/auth.php");
require_once("$CFG->dirroot/auth/parent/lib.php");
$authparent = new auth_plugin_parent;
$isparent = $authparent->is_parent($USER);
}

if ($isparent) {
redirect(new moodle_url('/'), 'Parents not allow to access referral program.');
redirect(new moodle_url('/'), get_string('referral_noparents', 'enrol_wallet'));
}

if (!(bool)get_config('referral_enabled', 'enrol_wallet')) {
redirect(new moodle_url('/'));
if (empty(get_config('enrol_wallet', 'referral_enabled'))) {
redirect(new moodle_url('/'), get_string('referral_not_enabled', 'enrol_wallet'));
}

// Adding some security.
require_login();
require_login(null, false);
$thisurl = new moodle_url('/enrol/wallet/extra/referral.php');

$PAGE->set_url($thisurl);
Expand Down
Loading

0 comments on commit da90679

Please sign in to comment.