Skip to content

Commit 6c6c099

Browse files
Use AJAX to redirect to course after transaction is processed (#50)
* Function skeleton * Added web service and external functions * Fixed SQL query * Page redirect * Fixed Travis errors * Removed empty field * Removed grunt tasks * Version upgrade
1 parent 532c85a commit 6c6c099

8 files changed

+175
-10
lines changed

amd/build/purchase.min.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/purchase.min.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/src/purchase.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// This file is part of Moodle - http://moodle.org/
2+
//
3+
// Moodle is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// Moodle is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
15+
16+
/**
17+
* AJAX request for the purchase.php page.
18+
*
19+
* @package tool_paymentplugin
20+
* @module tool_paymentplugin/purchase
21+
* @author Haruki Nakagawa
22+
* @copyright 2020 MAHQ
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
26+
define(['core/ajax'], function(Ajax) {
27+
return {
28+
purchasecheck: function(courseid, userid, redirecturl) {
29+
var request = {
30+
methodname: 'tool_paymentplugin_check_enrolled',
31+
args: {
32+
courseid: courseid,
33+
userid: userid
34+
}
35+
};
36+
Ajax.call([request])[0].done(
37+
function(data) {
38+
if (data == true) {
39+
document.location = redirecturl;
40+
}
41+
}
42+
).fail();
43+
setInterval(this.purchasecheck, 10000, courseid, userid, redirecturl);
44+
}
45+
};
46+
});

classes/external/external.php

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* External functions for tool_paymentplugin.
19+
*
20+
* @package tool_paymentplugin
21+
* @author Haruki Nakagawa
22+
* @copyright 2020 MAHQ
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
26+
namespace tool_paymentplugin\external;
27+
28+
defined('MOODLE_INTERNAL') || die();
29+
30+
use external_api;
31+
use external_function_parameters;
32+
use external_value;
33+
34+
require_once("$CFG->libdir/externallib.php");
35+
36+
class external extends external_api {
37+
38+
/**
39+
* Returns description of check_enrolled() parameters.
40+
* @return external_function_parameters
41+
*/
42+
public static function check_enrolled_parameters() {
43+
return new external_function_parameters(
44+
array(
45+
'courseid' => new external_value(PARAM_INT, 'id of course'),
46+
'userid' => new external_value(PARAM_INT, 'id of user')
47+
)
48+
);
49+
}
50+
51+
/**
52+
* Checks database to see if a user is enrolled in a course, and redirects the user if so.
53+
*
54+
* @param int $courseid
55+
* @param int $userid
56+
* @return bool
57+
*/
58+
public static function check_enrolled($courseid, $userid) {
59+
global $DB;
60+
61+
$params = self::validate_parameters(self::check_enrolled_parameters(), array(
62+
'courseid' => $courseid,
63+
'userid' => $userid
64+
));
65+
66+
$courseid = $params['courseid'];
67+
$userid = $params['userid'];
68+
69+
$query = "SELECT *
70+
FROM {user_enrolments}
71+
JOIN {enrol} ON {user_enrolments}.enrolid = {enrol}.id
72+
WHERE {enrol}.courseid = $courseid AND {user_enrolments}.userid = $userid";
73+
74+
$enrolled = $DB->record_exists_sql($query);
75+
return $enrolled;
76+
}
77+
78+
/**
79+
* Returns description of check_enrolled() return values.
80+
* @return external_value the value returned from the function.
81+
*/
82+
public static function check_enrolled_returns() {
83+
return new external_value(PARAM_BOOL, 'Whether the user is enrolled in the course.');
84+
}
85+
}

classes/payment_manager.php

-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
use dml_exception;
3030
use moodle_exception;
3131

32-
use function PHPSTORM_META\type;
33-
3432
defined ('MOODLE_INTERNAL') || die();
3533

3634
class payment_manager {

db/services.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Declaration of web service functions for tool_paymentplugin.
19+
*
20+
* @package tool_paymentplugin
21+
* @author Haruki Nakagawa
22+
* @copyright 2020 MAHQ
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
defined('MOODLE_INTERNAL') || die;
26+
27+
$functions = array(
28+
'tool_paymentplugin_check_enrolled' => array(
29+
'classname' => 'tool_paymentplugin\external\external',
30+
'methodname' => 'check_enrolled',
31+
'classpath' => 'admin/tool/paymentplugin/classes/external/external.php',
32+
'description' => 'Checks database to see if a user is enrolled in a course.',
33+
'type' => 'read',
34+
'ajax' => true
35+
)
36+
);

purchase.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,16 @@
3333
// Page Setup.
3434
$courseid = required_param('id', PARAM_INT);
3535

36-
// Check if purchase exists and if user is enrolled.
37-
if ($DB->record_exists('tool_paymentplugin_purchases', array('courseid' => $courseid, 'userid' => $USER->id, 'success' => 1))) {
38-
if (is_enrolled(context_course::instance($courseid))) {
39-
redirect(new moodle_url("$CFG->wwwroot/course/view.php?id=$courseid"), "You have already purchased this course.");
40-
}
41-
}
42-
4336
// Page Setup.
4437
$PAGE->set_url(new moodle_url('/admin/tool/paymentplugin/purchase.php', array('id' => $courseid)));
4538
$course = $DB->get_record('course', array('id' => $courseid));
4639
$context = \context_course::instance($course->id);
4740
$PAGE->set_context($context);
4841

42+
// Call Javascript to automatically redirect user to course once enrolment occurs.
43+
$redirecturl = "$CFG->wwwroot/course/view.php?id=$courseid";
44+
$PAGE->requires->js_call_amd('tool_paymentplugin/purchase', 'purchasecheck', array($course->id, $USER->id, $redirecturl));
45+
4946
// Page Display.
5047
$PAGE->set_title(get_string('purchasepagetitle', 'tool_paymentplugin'));
5148
$PAGE->set_heading(get_string('purchasepagetitle', 'tool_paymentplugin'));

version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626
defined('MOODLE_INTERNAL') || die();
2727

28-
$plugin->version = 2020101500;
28+
$plugin->version = 2020102300;
2929
$plugin->requires = '2018051713';
3030
$plugin->component = 'tool_paymentplugin';
3131
$plugin->maturity = MATURITY_STABLE;

0 commit comments

Comments
 (0)