Skip to content

Commit 09a2659

Browse files
Cleanup (#33)
* Fixed course_settings GET param * Shows error if no gateways are enabled * Throw error if client ID is not set * Added plugin->requires to version.php * Course cost cannot be set below 0 * Fixed Travis errors * Minor Cleanup * Update purchase.php Added newline to end of file * lib cleanup * version cleanup * settings cleanup * Purchase cleanup * course settings cleanup * course settings form cleanup * purchase form cleanup * paymentgateway cleanup * object payment gateway cleanup * settings cleanup 2 * get string fixes * function name fix * Fixed bug * Various fixes * Paypal Cleanup * Various Cleanups * End of file err fix * Author fix * Travis Fixes * Travis Fixes * Travis fixes * Travis Fixes * Moved currency setting to main settings * Fixed Travis error * Fixed Travis error Co-authored-by: Mitch1404 <[email protected]>
1 parent 8694244 commit 09a2659

20 files changed

+311
-375
lines changed

classes/form/course_settings_form.php

+22-11
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,36 @@
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

1717
/**
18-
* Creates a settings page for a course.
19-
*
20-
* File payment_settings_form.php
21-
* Encoding UTF-8
18+
* Form for the course settings page.
2219
*
2320
* @package tool_paymentplugin
21+
* @author Mitchell Halpin
22+
* @author Haruki Nakagawa
2423
*
2524
* @copyright MAHQ
2625
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2726
**/
2827

2928
namespace tool_paymentplugin\form;
29+
3030
defined('MOODLE_INTERNAL') || die();
3131

3232
require_once($CFG->libdir.'/formslib.php');
3333

3434
class course_settings_form extends \moodleform {
3535

36+
/**
37+
* Creates the form for course settings.
38+
*
39+
* @return void
40+
*/
3641
public function definition() {
3742
global $DB;
3843

3944
$thisform = $this->_form;
4045
$courseid = $this->_customdata['id'];
41-
4246
$thisform->addElement('text', 'coursecost', 'Course Cost');
4347
$thisform->setType('coursecost', PARAM_INT);
44-
4548
$tablename = 'tool_paymentplugin_course';
4649
$cost = 0;
4750

@@ -51,11 +54,19 @@ public function definition() {
5154
}
5255

5356
$thisform->setDefault('coursecost', $cost);
54-
5557
$this->add_action_buttons(true);
5658
}
59+
60+
/**
61+
* Additional validation checks
62+
*
63+
* @return array of errors
64+
*/
65+
public function validation($data, $files) {
66+
$errors = parent::validation($data, $files);
67+
if ($data['coursecost'] < 0) {
68+
$errors['coursecost'] = get_string('errorcoursecost', 'tool_paymentplugin');
69+
}
70+
return $errors;
71+
}
5772
}
58-
// DB->insert_record();
59-
// See https://docs.moodle.org/dev/Data_manipulation_API for reference.
60-
// See https://docs.moodle.org/dev/XMLDB_editor for reference.
61-
// See https://docs.moodle.org/dev/Upgrade_API#install.php for reference.

classes/form/purchase_form.php

+8-9
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

1717
/**
18-
* Creates a settings page for a course.
19-
*
20-
* File purchase.php
21-
* Encoding UTF-8
18+
* Course Purchase Form.
2219
*
2320
* @package tool_paymentplugin
21+
* @author Haruki Nakagawa
2422
*
2523
* @copyright MAHQ
2624
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2725
**/
2826

29-
namespace tool_paymentplugin\form;
27+
namespace tool_paymentplugin\form;
3028

3129
defined('MOODLE_INTERNAL') || die();
3230

@@ -35,21 +33,22 @@
3533

3634
class purchase_form extends \moodleform {
3735

36+
/**
37+
* Creates the form for course purchases.
38+
*
39+
* @return void
40+
*/
3841
public function definition() {
3942
global $DB;
4043

4144
$thisform = $this->_form;
4245
$courseid = $this->_customdata['id'];
4346

4447
$thehtml = '<div class="purchase-buttons">';
45-
4648
$paymentgateways = paymentgateway::get_all_enabled_gateway_objects();
47-
$size = count($paymentgateways);
48-
4949
foreach ($paymentgateways as $paymentgateway) {
5050
$thehtml .= $paymentgateway->payment_button($courseid);
5151
}
52-
5352
$thehtml .= '</div>';
5453

5554
$thisform->addElement('html', $thehtml);

classes/paymentgateway/object_paymentgateway.php

+33-7
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
/**
1818
* Abstract class for all payment gateway objects.
1919
*
20-
* File object_paymentgateway.php
21-
* Encoding UTF-8
22-
*
2320
* @package tool_paymentplugin
21+
* @author Mitchell Halpin
2422
*
2523
* @copyright MAHQ
2624
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@@ -31,28 +29,56 @@
3129
defined ('MOODLE_INTERNAL') || die();
3230

3331
abstract class object_paymentgateway {
32+
33+
// Name of payment gateway.
3434
public $name;
3535

36+
/**
37+
* @param string name of gateway
38+
*/
3639
public function __construct($name) {
3740
$this->name = $name;
3841
}
3942

43+
/**
44+
* Gets the display name with 'Payment Gateway' appened on the end.
45+
*
46+
* @return string name of gateway + 'Payment Gateway'
47+
*/
48+
public function get_display_name_appended() {
49+
return get_string('pluginname', 'paymentgateway_'.$this->name).' '.get_string('paymentgateway', 'tool_paymentplugin');
50+
}
51+
52+
/**
53+
* Gets the display name of the gateway.
54+
*
55+
* @return string name of gateway
56+
*/
4057
public function get_display_name() {
4158
return get_string('pluginname', 'paymentgateway_'.$this->name);
4259
}
43-
public function get_readable_name() {
44-
return get_string('pluginnamebasic', 'paymentgateway_'.$this->name);
45-
}
4660

61+
/**
62+
* Checks if the plugin is enabled in the admin settings for this plugin.
63+
*
64+
* @return boolean TRUE if enabled, FALSE otherwise.
65+
*/
4766
public function is_enabled() {
4867
$enabled = get_config('paymentgateway_'.$this->name, 'enabled') &&
49-
!get_config('tool_paymentplugin_gsettings', 'disablePurchases');
68+
!get_config('tool_paymentplugin_settings', 'disableall');
5069
if ($enabled == 1) {
5170
return true;
5271
}
5372
return false;
5473
}
5574

75+
/**
76+
* Gets the pyment gateway button in a html acceptable form.
77+
*
78+
* @param int course id
79+
*
80+
* @return html of the payment gateway button.
81+
*/
5682
public function payment_button($courseid) {
5783
}
5884
}

classes/plugininfo/paymentgateway.php

+18-16
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717
/**
1818
* Defines the subplugin type 'paymentgateway'
1919
*
20-
* File paymentgateway.php
21-
* Encoding UTF-8
22-
*
2320
* @package tool_paymentplugin
24-
* Should the author from the mfa file be put in as author here?
21+
* @author Mitchell Halpin - Based heavily off code done by 'Catalyst AU'
2522
*
2623
* @copyright MAHQ
2724
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@@ -31,12 +28,10 @@
3128

3229
defined('MOODLE_INTERNAL') || die();
3330

34-
// See https://docs.moodle.org/dev/Subplugins#Settings_pages.
3531
class paymentgateway extends \core\plugininfo\base {
3632

3733
/**
38-
* Finds all payment gateways.
39-
* @author Catalyst AU
34+
* gets all payment gateways.
4035
*
4136
* @return array of gateway objects.
4237
*/
@@ -52,7 +47,11 @@ public static function get_all_gateway_objects() {
5247
return self::sort_gateways_by_order($return);
5348
}
5449

55-
50+
/**
51+
* Gets all enabled payment gateway objects.
52+
*
53+
* @return array of gateway objects
54+
*/
5655
public static function get_all_enabled_gateway_objects() {
5756
$gateways = self::get_all_gateway_objects();
5857
$returnarr = array();
@@ -64,7 +63,13 @@ public static function get_all_enabled_gateway_objects() {
6463
return $returnarr;
6564
}
6665

67-
66+
/**
67+
* Get specific gateway object
68+
*
69+
* @param string name of payment gateway
70+
*
71+
* @return paymentgateway or null
72+
*/
6873
public static function get_gateway_object($name) {
6974
foreach (\core_plugin_manager::instance()->get_plugins_of_type('paymentgateway') as $gateway) {
7075
if ($gateway->name == $name) {
@@ -76,10 +81,8 @@ public static function get_gateway_object($name) {
7681
}
7782
}
7883

79-
8084
/**
8185
* Sorts payment gateways by configured order.
82-
* @author Catalyst AU
8386
*
8487
* @param array of gateway objects
8588
*
@@ -103,16 +106,15 @@ public static function sort_gateways_by_order($unsorted) {
103106
return $sorted;
104107
}
105108

106-
/* Loads factor settings to the settings tree
107-
*
108-
* This function usually includes settings.php file in plugins folder.
109-
* Alternatively it can create a link to some settings page (instance of admin_externalpage)
109+
/**
110+
* Loads all payment gateway settings.
110111
*
111112
* @param \part_of_admin_tree $adminroot
112113
* @param string $parentnodename
113114
* @param bool $hassiteconfig whether the current user has moodle/site:config capability
115+
*
116+
* @return void or null
114117
*/
115-
116118
public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
117119

118120
if (!$this->is_installed_and_upgraded()) {

course_settings.php

+13-21
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
/**
1818
* Creates a settings page for a course.
1919
*
20-
* File course_settings.php
21-
* Encoding UTF-8
22-
*
2320
* @package tool_paymentplugin
21+
* @author Mitchell Halpin
2422
*
2523
* @copyright MAHQ
2624
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@@ -29,34 +27,28 @@
2927
require_once(__DIR__.'/../../../config.php');
3028
use tool_paymentplugin\form\course_settings_form;
3129

32-
$courseid = optional_param('id', 0, PARAM_INT);
33-
if (empty($courseid)) {
34-
throw new moodle_exception('No valid course id detected.');
35-
}
36-
30+
// Page Setup.
31+
$courseid = required_param('id', PARAM_INT);
32+
$PAGE->set_url(new moodle_url('/admin/tool/paymentplugin/course_settings.php', array('id' => $courseid)));
3733
$course = get_course($courseid);
34+
$context = \context_course::instance($course->id);
35+
$PAGE->set_context($context);
36+
$PAGE->set_pagelayout('admin');
37+
38+
// Check user access.
3839
require_login($courseid, true);
39-
$coursecontext = context_course::instance(course_get_format($course)->get_course()->id);
40-
require_capability('moodle/course:create', $coursecontext);
40+
require_capability('moodle/course:create', $context);
4141

4242
// Set up the page.
4343
$title = get_string('coursesettings_management:title', 'tool_paymentplugin');
44-
$PAGE->set_url('/admin/tool/paymentplugin/course_settings.php');
45-
$PAGE->set_pagelayout('admin'); // What this do?
46-
$PAGE->set_context(context_course::instance($courseid));
47-
$PAGE->set_cacheable(false); // What this do?
48-
4944
$PAGE->set_heading($title);
5045
$PAGE->navbar->add($title, new moodle_url('/admin/tool/paymentplugin/course_settings.php'));
5146

52-
// Displaying the page.
47+
// Display Page.
5348
echo $OUTPUT->header();
5449

55-
// Create settings form.
56-
$args = array(
57-
'course' => $course,
58-
'id' => $courseid,
59-
);
50+
// Course Settings Form.
51+
$args = array('course' => $course, 'id' => $courseid);
6052
$paymentform = new course_settings_form(new moodle_url('/admin/tool/paymentplugin/course_settings.php',
6153
array('id' => $courseid)), $args);
6254

lang/en/tool_paymentplugin.php

+18-17
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
/**
1818
* Language file for payment plugin. EN.
1919
*
20-
* File tool_paymentplugin.php
21-
* Encoding UTF-8
22-
*
2320
* @package tool_paymentplugin
2421
*
2522
* @copyright MAHQ
@@ -28,23 +25,23 @@
2825

2926
// General.
3027
$string['pluginname'] = 'Payment Plugin';
31-
$string['gsettings'] = 'Payment Plugin Settings';
32-
33-
// Global Settings.
34-
$string['gsettingsdisableallpurchase'] = 'Disable All Payment Gateways';
35-
$string['gsettingsmulti1'] = 'Installed Payment Gateways';
28+
$string['adminsettingsheading'] = 'Payment Plugin Settings';
29+
$string['paymentgateway'] = 'Payment Gateway';
3630

37-
// Course Settings.
38-
$string['coursesettings:title'] = "Course Enrolment";
39-
$string['coursesettings_management:title'] = "Payment Settings";
31+
// Admin Settings.
32+
$string['gatewaydisableall:text'] = 'Disable All Payment Gateways';
33+
$string['gatewaylist:heading'] = 'Enable\Disable Payment Gateways';
34+
$string['gatewaylist:desc'] = '{$a->installed} installed payment gateways. {$a->enabled} currently enabled.';
35+
$string['gatewayenable:text'] = 'Enable {$a}';
4036

41-
// Subplugin Settings.
42-
$string['tool_paymentplugin_subsettings/heading'] = "Enable\Disable Payment Gateways";
43-
$string['tool_paymentplugin_subsettings/headingdesc'] = " installed payment gateways.";
44-
$string['tool_paymentplugin_subsettings/headingdesc2'] = " currently enabled.";
37+
$string['settings:currency'] = 'Currency';
38+
$string['settings:currencydesc'] = 'currency under development';
39+
$string['settings:currencyUSD'] = 'USD';
40+
$string['settings:currencyAUD'] = 'AUD';
4541

46-
$string['settingsdisablepurchase'] = 'Enable';
47-
$string['settingsdisablepurchasedesc'] = 'If box is checked, users will be able to make purchases via this payment gateway.';
42+
// Course Settings.
43+
$string['coursesettings:title'] = 'Course Enrolment';
44+
$string['coursesettings_management:title'] = 'Payment Settings';
4845

4946
// Subplugin Types.
5047
$string['subplugintype_paymentgateway'] = 'Payment Gateway';
@@ -53,3 +50,7 @@
5350
// Purchase page.
5451
$string['purchasepagetitle'] = 'Purchase course';
5552
$string['purchasepagecourse'] = 'You are buying the course: "{$a->name}" for ${$a->cost}.';
53+
54+
// Errors.
55+
$string['errorcoursecost'] = 'Please insert a valid number.';
56+
$string['errornothingenabled'] = 'No payment gateways have been enabled! Please contact the site administrator for details.';

0 commit comments

Comments
 (0)