| 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 |  |  |  * PayPayl condition. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @package availability_paypal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @copyright 2015 Daniel Neis Araujo <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | namespace availability_paypal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | defined('MOODLE_INTERNAL') || die(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * PayPal condition. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @package availability_paypal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * @copyright 2014 The Open University | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | class condition extends \core_availability\condition { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * Constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * @param \stdClass $structure Data structure from JSON decode | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * @throws \coding_exception If invalid data structure. | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function __construct($structure) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if (isset($structure->businessemail)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $this->businessemail = $structure->businessemail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         if (isset($structure->currency)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $this->currency = $structure->currency; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         if (isset($structure->cost)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $this->cost = $structure->cost; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         if (isset($structure->itemname)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $this->itemname = $structure->itemname; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         if (isset($structure->itemnumber)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $this->itemnumber = $structure->itemnumber; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * Returns info to be saved. | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * @return stdClass | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     public function save() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $result = (object)array('type' => 'paypal'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if ($this->businessemail) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $result->businessemail = $this->businessemail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if ($this->currency) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $result->currency = $this->currency; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if ($this->cost) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $result->cost = $this->cost; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         if ($this->itemname) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $result->itemname = $this->itemname; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         if ($this->itemnumber) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $result->itemnumber = $this->itemnumber; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return $result; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * Returns a JSON object which corresponds to a condition of this type. | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * Intended for unit testing, as normally the JSON values are constructed | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      * by JavaScript code. | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * @param string $businessemail The email of paypal to be credited | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      * @param string $currency      The currency to charge the user | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      * @param string $cost          The cost to charge the user | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      * @return stdClass Object representing condition | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     public static function get_json($businessemail, $currency, $cost) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         return (object)array('type' => 'paypal', 'businessemail' => $businessemail, 'currency' => $currency, 'cost' => $cost); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * Returns true if the user can access the context, false otherwise | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * @param bool $not Set true if we are inverting the condition | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      * @param info $info Item we're checking | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      * @param bool $grabthelot Performance hint: if true, caches information | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      *   required for all course-modules, to make the front page and similar | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      *   pages work more quickly (works only for current user) | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      * @param int $userid User ID to check availability for | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      * @return bool True if available | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     public function is_available($not, \core_availability\info $info, $grabthelot, $userid) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         global $DB; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         // Should double-check with paypal everytime ? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $context = $info->get_context(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $allow = $DB->record_exists('availability_paypal_tnx', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                                   array('userid' => $userid, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                                         'contextid' => $context->id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                                         'payment_status' => 'Completed')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         if ($not) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             $allow = !$allow; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         return $allow; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |      * Shows the description using the different lang strings for the standalone | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |      * version or the full one. | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |      * @param bool $full Set true if this is the 'full information' view | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |      * @param bool $not  True if NOT is in force | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |      * @param \core_availability\info $info Information about the availability condition and module context | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |      * @return string    The string about the condition and it's status | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     public function get_description($full, $not, \core_availability\info $info) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         return $this->get_either_description($not, false, $info); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |      * Shows the description using the different lang strings for the standalone | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |      * version or the full one. | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |      * @param bool $not        True if NOT is in force | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |      * @param bool $standalone True to use standalone lang strings | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |      * @param bool $info       Information about the availability condition and module context | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |      * @return string          The string about the condition and it's status | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     protected function get_either_description($not, $standalone, $info) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $context = $info->get_context(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $url = new \moodle_url('/availability/condition/paypal/view.php?contextid='.$context->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         if ($not) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             return get_string('notdescription', 'availability_paypal', $url->out()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             return get_string('eitherdescription', 'availability_paypal', $url->out()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |      * Function used by backup restore | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |      * @param int $restoreid | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |      * @param int $courseid | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |      * @param \base_logger $logger | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  |      * @param string $name | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     public function update_after_restore($restoreid, $courseid, \base_logger $logger, $name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         // Update the date, if restoring with changed date. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         $dateoffset = \core_availability\info::get_restore_date_offset($restoreid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         if ($dateoffset) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             $this->time += $dateoffset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         return false; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |      * Returns a string to debug | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     protected function get_debug_string() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         return gmdate('Y-m-d H:i:s'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 181 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 182 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 183 |  |  |  | 
            
                        
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.