[ Index ] |
PHP Cross Reference of Unnamed Project |
[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 $found = false; 64 foreach ($instances as $instance) { 65 if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) { 66 continue; 67 } 68 if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) { 69 continue; 70 } 71 $found = true; 72 break; 73 } 74 if ($found) { 75 return array(new pix_icon('icon', get_string('pluginname', 'enrol_paypal'), 'enrol_paypal')); 76 } 77 return array(); 78 } 79 80 public function roles_protected() { 81 // users with role assign cap may tweak the roles later 82 return false; 83 } 84 85 public function allow_unenrol(stdClass $instance) { 86 // users with unenrol cap may unenrol other users manually - requires enrol/paypal:unenrol 87 return true; 88 } 89 90 public function allow_manage(stdClass $instance) { 91 // users with manage cap may tweak period and status - requires enrol/paypal:manage 92 return true; 93 } 94 95 public function show_enrolme_link(stdClass $instance) { 96 return ($instance->status == ENROL_INSTANCE_ENABLED); 97 } 98 99 /** 100 * Returns true if the user can add a new instance in this course. 101 * @param int $courseid 102 * @return boolean 103 */ 104 public function can_add_instance($courseid) { 105 $context = context_course::instance($courseid, MUST_EXIST); 106 107 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/paypal:config', $context)) { 108 return false; 109 } 110 111 // multiple instances supported - different cost for different roles 112 return true; 113 } 114 115 /** 116 * We are a good plugin and don't invent our own UI/validation code path. 117 * 118 * @return boolean 119 */ 120 public function use_standard_editing_ui() { 121 return true; 122 } 123 124 /** 125 * Add new instance of enrol plugin. 126 * @param object $course 127 * @param array $fields instance fields 128 * @return int id of new instance, null if can not be created 129 */ 130 public function add_instance($course, array $fields = null) { 131 if ($fields && !empty($fields['cost'])) { 132 $fields['cost'] = unformat_float($fields['cost']); 133 } 134 return parent::add_instance($course, $fields); 135 } 136 137 /** 138 * Update instance of enrol plugin. 139 * @param stdClass $instance 140 * @param stdClass $data modified instance fields 141 * @return boolean 142 */ 143 public function update_instance($instance, $data) { 144 if ($data) { 145 $data->cost = unformat_float($data->cost); 146 } 147 return parent::update_instance($instance, $data); 148 } 149 150 /** 151 * Creates course enrol form, checks if form submitted 152 * and enrols user if necessary. It can also redirect. 153 * 154 * @param stdClass $instance 155 * @return string html text, usually a form in a text box 156 */ 157 function enrol_page_hook(stdClass $instance) { 158 global $CFG, $USER, $OUTPUT, $PAGE, $DB; 159 160 ob_start(); 161 162 if ($DB->record_exists('user_enrolments', array('userid'=>$USER->id, 'enrolid'=>$instance->id))) { 163 return ob_get_clean(); 164 } 165 166 if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) { 167 return ob_get_clean(); 168 } 169 170 if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) { 171 return ob_get_clean(); 172 } 173 174 $course = $DB->get_record('course', array('id'=>$instance->courseid)); 175 $context = context_course::instance($course->id); 176 177 $shortname = format_string($course->shortname, true, array('context' => $context)); 178 $strloginto = get_string("loginto", "", $shortname); 179 $strcourses = get_string("courses"); 180 181 // Pass $view=true to filter hidden caps if the user cannot see them 182 if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC', 183 '', '', '', '', false, true)) { 184 $users = sort_by_roleassignment_authority($users, $context); 185 $teacher = array_shift($users); 186 } else { 187 $teacher = false; 188 } 189 190 if ( (float) $instance->cost <= 0 ) { 191 $cost = (float) $this->get_config('cost'); 192 } else { 193 $cost = (float) $instance->cost; 194 } 195 196 if (abs($cost) < 0.01) { // no cost, other enrolment methods (instances) should be used 197 echo '<p>'.get_string('nocost', 'enrol_paypal').'</p>'; 198 } else { 199 200 // Calculate localised and "." cost, make sure we send PayPal the same value, 201 // please note PayPal expects amount with 2 decimal places and "." separator. 202 $localisedcost = format_float($cost, 2, true); 203 $cost = format_float($cost, 2, false); 204 205 if (isguestuser()) { // force login only for guest user, not real users with guest role 206 if (empty($CFG->loginhttps)) { 207 $wwwroot = $CFG->wwwroot; 208 } else { 209 // This actually is not so secure ;-), 'cause we're 210 // in unencrypted connection... 211 $wwwroot = str_replace("http://", "https://", $CFG->wwwroot); 212 } 213 echo '<div class="mdl-align"><p>'.get_string('paymentrequired').'</p>'; 214 echo '<p><b>'.get_string('cost').": $instance->currency $localisedcost".'</b></p>'; 215 echo '<p><a href="'.$wwwroot.'/login/">'.get_string('loginsite').'</a></p>'; 216 echo '</div>'; 217 } else { 218 //Sanitise some fields before building the PayPal form 219 $coursefullname = format_string($course->fullname, true, array('context'=>$context)); 220 $courseshortname = $shortname; 221 $userfullname = fullname($USER); 222 $userfirstname = $USER->firstname; 223 $userlastname = $USER->lastname; 224 $useraddress = $USER->address; 225 $usercity = $USER->city; 226 $instancename = $this->get_instance_name($instance); 227 228 include($CFG->dirroot.'/enrol/paypal/enrol.html'); 229 } 230 231 } 232 233 return $OUTPUT->box(ob_get_clean()); 234 } 235 236 /** 237 * Restore instance and map settings. 238 * 239 * @param restore_enrolments_structure_step $step 240 * @param stdClass $data 241 * @param stdClass $course 242 * @param int $oldid 243 */ 244 public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) { 245 global $DB; 246 if ($step->get_task()->get_target() == backup::TARGET_NEW_COURSE) { 247 $merge = false; 248 } else { 249 $merge = array( 250 'courseid' => $data->courseid, 251 'enrol' => $this->get_name(), 252 'roleid' => $data->roleid, 253 'cost' => $data->cost, 254 'currency' => $data->currency, 255 ); 256 } 257 if ($merge and $instances = $DB->get_records('enrol', $merge, 'id')) { 258 $instance = reset($instances); 259 $instanceid = $instance->id; 260 } else { 261 $instanceid = $this->add_instance($course, (array)$data); 262 } 263 $step->set_mapping('enrol', $oldid, $instanceid); 264 } 265 266 /** 267 * Restore user enrolment. 268 * 269 * @param restore_enrolments_structure_step $step 270 * @param stdClass $data 271 * @param stdClass $instance 272 * @param int $oldinstancestatus 273 * @param int $userid 274 */ 275 public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) { 276 $this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, $data->status); 277 } 278 279 /** 280 * Gets an array of the user enrolment actions 281 * 282 * @param course_enrolment_manager $manager 283 * @param stdClass $ue A user enrolment object 284 * @return array An array of user_enrolment_actions 285 */ 286 public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) { 287 $actions = array(); 288 $context = $manager->get_context(); 289 $instance = $ue->enrolmentinstance; 290 $params = $manager->get_moodlepage()->url->params(); 291 $params['ue'] = $ue->id; 292 if ($this->allow_unenrol($instance) && has_capability("enrol/paypal:unenrol", $context)) { 293 $url = new moodle_url('/enrol/unenroluser.php', $params); 294 $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id)); 295 } 296 if ($this->allow_manage($instance) && has_capability("enrol/paypal:manage", $context)) { 297 $url = new moodle_url('/enrol/editenrolment.php', $params); 298 $actions[] = new user_enrolment_action(new pix_icon('t/edit', ''), get_string('edit'), $url, array('class'=>'editenrollink', 'rel'=>$ue->id)); 299 } 300 return $actions; 301 } 302 303 public function cron() { 304 $trace = new text_progress_trace(); 305 $this->process_expirations($trace); 306 } 307 308 /** 309 * Return an array of valid options for the status. 310 * 311 * @return array 312 */ 313 protected function get_status_options() { 314 $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'), 315 ENROL_INSTANCE_DISABLED => get_string('no')); 316 return $options; 317 } 318 319 /** 320 * Return an array of valid options for the roleid. 321 * 322 * @param stdClass $instance 323 * @param context $context 324 * @return array 325 */ 326 protected function get_roleid_options($instance, $context) { 327 if ($instance->id) { 328 $roles = get_default_enrol_roles($context, $instance->roleid); 329 } else { 330 $roles = get_default_enrol_roles($context, $this->get_config('roleid')); 331 } 332 return $roles; 333 } 334 335 336 /** 337 * Add elements to the edit instance form. 338 * 339 * @param stdClass $instance 340 * @param MoodleQuickForm $mform 341 * @param context $context 342 * @return bool 343 */ 344 public function edit_instance_form($instance, MoodleQuickForm $mform, $context) { 345 346 $mform->addElement('text', 'name', get_string('custominstancename', 'enrol')); 347 $mform->setType('name', PARAM_TEXT); 348 349 $options = $this->get_status_options(); 350 $mform->addElement('select', 'status', get_string('status', 'enrol_paypal'), $options); 351 $mform->setDefault('status', $this->get_config('status')); 352 353 $mform->addElement('text', 'cost', get_string('cost', 'enrol_paypal'), array('size' => 4)); 354 $mform->setType('cost', PARAM_RAW); 355 $mform->setDefault('cost', format_float($this->get_config('cost'), 2, true)); 356 357 $paypalcurrencies = $this->get_currencies(); 358 $mform->addElement('select', 'currency', get_string('currency', 'enrol_paypal'), $paypalcurrencies); 359 $mform->setDefault('currency', $this->get_config('currency')); 360 361 $roles = $this->get_roleid_options($instance, $context); 362 $mform->addElement('select', 'roleid', get_string('assignrole', 'enrol_paypal'), $roles); 363 $mform->setDefault('roleid', $this->get_config('roleid')); 364 365 $options = array('optional' => true, 'defaultunit' => 86400); 366 $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_paypal'), $options); 367 $mform->setDefault('enrolperiod', $this->get_config('enrolperiod')); 368 $mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_paypal'); 369 370 $options = array('optional' => true); 371 $mform->addElement('date_time_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_paypal'), $options); 372 $mform->setDefault('enrolstartdate', 0); 373 $mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_paypal'); 374 375 $options = array('optional' => true); 376 $mform->addElement('date_time_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_paypal'), $options); 377 $mform->setDefault('enrolenddate', 0); 378 $mform->addHelpButton('enrolenddate', 'enrolenddate', 'enrol_paypal'); 379 380 if (enrol_accessing_via_instance($instance)) { 381 $warningtext = get_string('instanceeditselfwarningtext', 'core_enrol'); 382 $mform->addElement('static', 'selfwarn', get_string('instanceeditselfwarning', 'core_enrol'), $warningtext); 383 } 384 } 385 386 /** 387 * Perform custom validation of the data used to edit the instance. 388 * 389 * @param array $data array of ("fieldname"=>value) of submitted data 390 * @param array $files array of uploaded files "element_name"=>tmp_file_path 391 * @param object $instance The instance loaded from the DB 392 * @param context $context The context of the instance we are editing 393 * @return array of "element_name"=>"error_description" if there are errors, 394 * or an empty array if everything is OK. 395 * @return void 396 */ 397 public function edit_instance_validation($data, $files, $instance, $context) { 398 $errors = array(); 399 400 if (!empty($data['enrolenddate']) and $data['enrolenddate'] < $data['enrolstartdate']) { 401 $errors['enrolenddate'] = get_string('enrolenddaterror', 'enrol_paypal'); 402 } 403 404 $cost = str_replace(get_string('decsep', 'langconfig'), '.', $data['cost']); 405 if (!is_numeric($cost)) { 406 $errors['cost'] = get_string('costerror', 'enrol_paypal'); 407 } 408 409 $validstatus = array_keys($this->get_status_options()); 410 $validcurrency = array_keys($this->get_currencies()); 411 $validroles = array_keys($this->get_roleid_options($instance, $context)); 412 $tovalidate = array( 413 'name' => PARAM_TEXT, 414 'status' => $validstatus, 415 'currency' => $validcurrency, 416 'roleid' => $validroles, 417 'enrolperiod' => PARAM_INT, 418 'enrolstartdate' => PARAM_INT, 419 'enrolenddate' => PARAM_INT 420 ); 421 422 $typeerrors = $this->validate_param_types($data, $tovalidate); 423 $errors = array_merge($errors, $typeerrors); 424 425 return $errors; 426 } 427 428 /** 429 * Execute synchronisation. 430 * @param progress_trace $trace 431 * @return int exit code, 0 means ok 432 */ 433 public function sync(progress_trace $trace) { 434 $this->process_expirations($trace); 435 return 0; 436 } 437 438 /** 439 * Is it possible to delete enrol instance via standard UI? 440 * 441 * @param stdClass $instance 442 * @return bool 443 */ 444 public function can_delete_instance($instance) { 445 $context = context_course::instance($instance->courseid); 446 return has_capability('enrol/paypal:config', $context); 447 } 448 449 /** 450 * Is it possible to hide/show enrol instance via standard UI? 451 * 452 * @param stdClass $instance 453 * @return bool 454 */ 455 public function can_hide_show_instance($instance) { 456 $context = context_course::instance($instance->courseid); 457 return has_capability('enrol/paypal:config', $context); 458 } 459 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |