[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 * Paypal enrolment plugin. 19 * 20 * This plugin allows you to set up paid courses. 21 * 22 * @package enrol_paypal 23 * @copyright 2010 Eugene Venter 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 */ 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 /** 30 * Paypal enrolment plugin implementation. 31 * @author Eugene Venter - based on code by Martin Dougiamas and others 32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 33 */ 34 class enrol_paypal_plugin extends enrol_plugin { 35 36 public function get_currencies() { 37 // See https://www.paypal.com/cgi-bin/webscr?cmd=p/sell/mc/mc_intro-outside, 38 // 3-character ISO-4217: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_currency_codes 39 $codes = array( 40 'AUD', 'BRL', 'CAD', 'CHF', 'CZK', 'DKK', 'EUR', 'GBP', 'HKD', 'HUF', 'ILS', 'JPY', 41 'MXN', 'MYR', 'NOK', 'NZD', 'PHP', 'PLN', 'RUB', 'SEK', 'SGD', 'THB', 'TRY', 'TWD', 'USD'); 42 $currencies = array(); 43 foreach ($codes as $c) { 44 $currencies[$c] = new lang_string($c, 'core_currencies'); 45 } 46 47 return $currencies; 48 } 49 50 /** 51 * Returns optional enrolment information icons. 52 * 53 * This is used in course list for quick overview of enrolment options. 54 * 55 * We are not using single instance parameter because sometimes 56 * we might want to prevent icon repetition when multiple instances 57 * of one type exist. One instance may also produce several icons. 58 * 59 * @param array $instances all enrol instances of this type in one course 60 * @return array of pix_icon 61 */ 62 public function get_info_icons(array $instances) { 63 return array(new pix_icon('icon', get_string('pluginname', 'enrol_paypal'), 'enrol_paypal')); 64 } 65 66 public function roles_protected() { 67 // users with role assign cap may tweak the roles later 68 return false; 69 } 70 71 public function allow_unenrol(stdClass $instance) { 72 // users with unenrol cap may unenrol other users manually - requires enrol/paypal:unenrol 73 return true; 74 } 75 76 public function allow_manage(stdClass $instance) { 77 // users with manage cap may tweak period and status - requires enrol/paypal:manage 78 return true; 79 } 80 81 public function show_enrolme_link(stdClass $instance) { 82 return ($instance->status == ENROL_INSTANCE_ENABLED); 83 } 84 85 /** 86 * Sets up navigation entries. 87 * 88 * @param object $instance 89 * @return void 90 */ 91 public function add_course_navigation($instancesnode, stdClass $instance) { 92 if ($instance->enrol !== 'paypal') { 93 throw new coding_exception('Invalid enrol instance type!'); 94 } 95 96 $context = context_course::instance($instance->courseid); 97 if (has_capability('enrol/paypal:config', $context)) { 98 $managelink = new moodle_url('/enrol/paypal/edit.php', array('courseid'=>$instance->courseid, 'id'=>$instance->id)); 99 $instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING); 100 } 101 } 102 103 /** 104 * Returns edit icons for the page with list of instances 105 * @param stdClass $instance 106 * @return array 107 */ 108 public function get_action_icons(stdClass $instance) { 109 global $OUTPUT; 110 111 if ($instance->enrol !== 'paypal') { 112 throw new coding_exception('invalid enrol instance!'); 113 } 114 $context = context_course::instance($instance->courseid); 115 116 $icons = array(); 117 118 if (has_capability('enrol/paypal:config', $context)) { 119 $editlink = new moodle_url("/enrol/paypal/edit.php", array('courseid'=>$instance->courseid, 'id'=>$instance->id)); 120 $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit'), 'core', 121 array('class' => 'iconsmall'))); 122 } 123 124 return $icons; 125 } 126 127 /** 128 * Returns link to page which may be used to add new instance of enrolment plugin in course. 129 * @param int $courseid 130 * @return moodle_url page url 131 */ 132 public function get_newinstance_link($courseid) { 133 $context = context_course::instance($courseid, MUST_EXIST); 134 135 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/paypal:config', $context)) { 136 return NULL; 137 } 138 139 // multiple instances supported - different cost for different roles 140 return new moodle_url('/enrol/paypal/edit.php', array('courseid'=>$courseid)); 141 } 142 143 /** 144 * Creates course enrol form, checks if form submitted 145 * and enrols user if necessary. It can also redirect. 146 * 147 * @param stdClass $instance 148 * @return string html text, usually a form in a text box 149 */ 150 function enrol_page_hook(stdClass $instance) { 151 global $CFG, $USER, $OUTPUT, $PAGE, $DB; 152 153 ob_start(); 154 155 if ($DB->record_exists('user_enrolments', array('userid'=>$USER->id, 'enrolid'=>$instance->id))) { 156 return ob_get_clean(); 157 } 158 159 if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) { 160 return ob_get_clean(); 161 } 162 163 if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) { 164 return ob_get_clean(); 165 } 166 167 $course = $DB->get_record('course', array('id'=>$instance->courseid)); 168 $context = context_course::instance($course->id); 169 170 $shortname = format_string($course->shortname, true, array('context' => $context)); 171 $strloginto = get_string("loginto", "", $shortname); 172 $strcourses = get_string("courses"); 173 174 // Pass $view=true to filter hidden caps if the user cannot see them 175 if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC', 176 '', '', '', '', false, true)) { 177 $users = sort_by_roleassignment_authority($users, $context); 178 $teacher = array_shift($users); 179 } else { 180 $teacher = false; 181 } 182 183 if ( (float) $instance->cost <= 0 ) { 184 $cost = (float) $this->get_config('cost'); 185 } else { 186 $cost = (float) $instance->cost; 187 } 188 189 if (abs($cost) < 0.01) { // no cost, other enrolment methods (instances) should be used 190 echo '<p>'.get_string('nocost', 'enrol_paypal').'</p>'; 191 } else { 192 193 // Calculate localised and "." cost, make sure we send PayPal the same value, 194 // please note PayPal expects amount with 2 decimal places and "." separator. 195 $localisedcost = format_float($cost, 2, true); 196 $cost = format_float($cost, 2, false); 197 198 if (isguestuser()) { // force login only for guest user, not real users with guest role 199 if (empty($CFG->loginhttps)) { 200 $wwwroot = $CFG->wwwroot; 201 } else { 202 // This actually is not so secure ;-), 'cause we're 203 // in unencrypted connection... 204 $wwwroot = str_replace("http://", "https://", $CFG->wwwroot); 205 } 206 echo '<div class="mdl-align"><p>'.get_string('paymentrequired').'</p>'; 207 echo '<p><b>'.get_string('cost').": $instance->currency $localisedcost".'</b></p>'; 208 echo '<p><a href="'.$wwwroot.'/login/">'.get_string('loginsite').'</a></p>'; 209 echo '</div>'; 210 } else { 211 //Sanitise some fields before building the PayPal form 212 $coursefullname = format_string($course->fullname, true, array('context'=>$context)); 213 $courseshortname = $shortname; 214 $userfullname = fullname($USER); 215 $userfirstname = $USER->firstname; 216 $userlastname = $USER->lastname; 217 $useraddress = $USER->address; 218 $usercity = $USER->city; 219 $instancename = $this->get_instance_name($instance); 220 221 include($CFG->dirroot.'/enrol/paypal/enrol.html'); 222 } 223 224 } 225 226 return $OUTPUT->box(ob_get_clean()); 227 } 228 229 /** 230 * Restore instance and map settings. 231 * 232 * @param restore_enrolments_structure_step $step 233 * @param stdClass $data 234 * @param stdClass $course 235 * @param int $oldid 236 */ 237 public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) { 238 global $DB; 239 if ($step->get_task()->get_target() == backup::TARGET_NEW_COURSE) { 240 $merge = false; 241 } else { 242 $merge = array( 243 'courseid' => $data->courseid, 244 'enrol' => $this->get_name(), 245 'roleid' => $data->roleid, 246 'cost' => $data->cost, 247 'currency' => $data->currency, 248 ); 249 } 250 if ($merge and $instances = $DB->get_records('enrol', $merge, 'id')) { 251 $instance = reset($instances); 252 $instanceid = $instance->id; 253 } else { 254 $instanceid = $this->add_instance($course, (array)$data); 255 } 256 $step->set_mapping('enrol', $oldid, $instanceid); 257 } 258 259 /** 260 * Restore user enrolment. 261 * 262 * @param restore_enrolments_structure_step $step 263 * @param stdClass $data 264 * @param stdClass $instance 265 * @param int $oldinstancestatus 266 * @param int $userid 267 */ 268 public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) { 269 $this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, $data->status); 270 } 271 272 /** 273 * Gets an array of the user enrolment actions 274 * 275 * @param course_enrolment_manager $manager 276 * @param stdClass $ue A user enrolment object 277 * @return array An array of user_enrolment_actions 278 */ 279 public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) { 280 $actions = array(); 281 $context = $manager->get_context(); 282 $instance = $ue->enrolmentinstance; 283 $params = $manager->get_moodlepage()->url->params(); 284 $params['ue'] = $ue->id; 285 if ($this->allow_unenrol($instance) && has_capability("enrol/paypal:unenrol", $context)) { 286 $url = new moodle_url('/enrol/unenroluser.php', $params); 287 $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id)); 288 } 289 if ($this->allow_manage($instance) && has_capability("enrol/paypal:manage", $context)) { 290 $url = new moodle_url('/enrol/editenrolment.php', $params); 291 $actions[] = new user_enrolment_action(new pix_icon('t/edit', ''), get_string('edit'), $url, array('class'=>'editenrollink', 'rel'=>$ue->id)); 292 } 293 return $actions; 294 } 295 296 public function cron() { 297 $trace = new text_progress_trace(); 298 $this->process_expirations($trace); 299 } 300 301 /** 302 * Execute synchronisation. 303 * @param progress_trace $trace 304 * @return int exit code, 0 means ok 305 */ 306 public function sync(progress_trace $trace) { 307 $this->process_expirations($trace); 308 return 0; 309 } 310 311 /** 312 * Is it possible to delete enrol instance via standard UI? 313 * 314 * @param stdClass $instance 315 * @return bool 316 */ 317 public function can_delete_instance($instance) { 318 $context = context_course::instance($instance->courseid); 319 return has_capability('enrol/paypal:config', $context); 320 } 321 322 /** 323 * Is it possible to hide/show enrol instance via standard UI? 324 * 325 * @param stdClass $instance 326 * @return bool 327 */ 328 public function can_hide_show_instance($instance) { 329 $context = context_course::instance($instance->courseid); 330 return has_capability('enrol/paypal:config', $context); 331 } 332 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |