forked from Wunderbyte-GmbH/moodle-local_shopping_cart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
149 lines (133 loc) · 5.91 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
// 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/>.
/**
* Moodle hooks for local_shopping_cart
* @package local_shopping_cart
* @copyright 2021 Wunderbyte GmbH
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use local_shopping_cart\shopping_cart;
// Define constants.
// First entry in shopping cart history. This means that payment was initiated, but not successfully completed.
define('PAYMENT_PENDING', 0);
// Pending will be switched to aborted, once we can be sure that the payment process will not be continued.
define('PAYMENT_ABORTED', 1);
// Payment was successful.
define('PAYMENT_SUCCESS', 2);
// Canceled payments mean that items - which have already been paid for - are canceled after successful checkout.
define('PAYMENT_CANCELED', 3);
// Payment methods.
define('PAYMENT_METHOD_ONLINE', 0); // Payment via payment gateway (which is usually connected with a credit card).
define('PAYMENT_METHOD_CASHIER', 1); // Payment at cashier's office (unknown if cash, debit or credit card).
define('PAYMENT_METHOD_CREDITS', 2); // Payment via credits.
define('PAYMENT_METHOD_CASHIER_CASH', 3); // Payment at cashier's office using cash.
define('PAYMENT_METHOD_CASHIER_DEBITCARD', 4); // Payment at cashier's office using a debit card.
define('PAYMENT_METHOD_CASHIER_CREDITCARD', 5); // Payment at cashier's office using a credit card.
define('PAYMENT_METHOD_CREDITS_PAID_BACK', 6); // Credits removed and paid back to user.
/**
* Adds module specific settings to the settings block
*
* @param navigation_node $navigation The node to add module settings to
* @return void
*/
function local_shopping_cart_extend_navigation(navigation_node $navigation) {
$context = context_system::instance();
if (has_capability('local/shopping_cart:cashier', $context)) {
$nodehome = $navigation->get('home');
if (empty($nodehome)) {
$nodehome = $navigation;
}
$pluginname = get_string('pluginname', 'local_shopping_cart');
$link = new moodle_url('/local/shopping_cart/cashier.php', array());
$icon = new pix_icon('i/shopping_cart', $pluginname, 'local_shopping_cart');
$nodecreatecourse = $nodehome->add($pluginname, $link, navigation_node::NODETYPE_LEAF,
$pluginname, 'shopping_cart_cashier', $icon);
$nodecreatecourse->showinflatnavigation = true;
}
}
/**
* Renders the popup.
*
* @param renderer_base $renderer
* @return string The HTML
*/
function local_shopping_cart_render_navbar_output(\renderer_base $renderer) {
global $USER, $CFG;
// Early bail out conditions.
if (!isloggedin() || isguestuser()) {
return '';
}
$output = '';
$cache = shopping_cart::local_shopping_cart_get_cache_data($USER->id);
$output .= $renderer->render_from_template('local_shopping_cart/shopping_cart_popover', $cache);
return $output;
}
/**
* Get icon mapping for font-awesome.
*
* @return array
*/
function local_shopping_cart_get_fontawesome_icon_map() {
return [
'local_shopping_cart:i/shopping_cart' => 'fa-shopping-cart',
'local_shopping_cart:t/selected' => 'fa-check',
'local_shopping_cart:t/subscribed' => 'fa-envelope-o',
'local_shopping_cart:t/unsubscribed' => 'fa-envelope-open-o',
'local_shopping_cart:t/star' => 'fa-star',
];
}
/**
* Callback checking permissions and preparing the file for serving plugin files, see File API.
*
* @param stdClass $course the course object
* @param stdClass $cm the course module object
* @param stdClass $context the context
* @param string $filearea the name of the file area
* @param array $args extra arguments (itemid, path)
* @param bool $forcedownload whether or not force download
* @param array $options additional options affecting the file serving
* @return bool false if the file not found, just send the file otherwise and do not return anything
*/
function local_shopping_cart_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
// Check the contextlevel is as expected - if your plugin is a block.
// We need context course if wee like to acces template files.
if (!in_array($context->contextlevel, array(CONTEXT_SYSTEM))) {
return false;
}
// Make sure the filearea is one of those used by the plugin.
if ($filearea !== 'local_shopping_cart_receiptimage') {
return false;
}
// Make sure the user is logged in and has access to the module.
// Leave this line out if you set the itemid to null in make_pluginfile_url (set $itemid to 0 instead).
$itemid = array_shift($args); // The first item in the $args array.
$filename = array_pop($args); // The last item in the $args array.
if (!$args) {
// Var $args is empty => the path is '/'.
$filepath = '/';
} else {
// Var $args contains elements of the filepath.
$filepath = '/' . implode('/', $args) . '/';
}
// Retrieve the file from the Files API.
$fs = get_file_storage();
$file = $fs->get_file($context->id, 'local_shopping_cart', $filearea, $itemid, $filepath, $filename);
if (!$file) {
return false; // The file does not exist.
}
// Send the file back to the browser - in this case with a cache lifetime of 1 day and no filtering.
send_stored_file($file, 0, 0, true, $options);
}