Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/enrol/paypal/lib.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00028 defined('MOODLE_INTERNAL') || die();
00029 
00035 class enrol_paypal_plugin extends enrol_plugin {
00036 
00049     public function get_info_icons(array $instances) {
00050         return array(new pix_icon('icon', get_string('pluginname', 'enrol_paypal'), 'enrol_paypal'));
00051     }
00052 
00053     public function roles_protected() {
00054         // users with role assign cap may tweak the roles later
00055         return false;
00056     }
00057 
00058     public function allow_unenrol(stdClass $instance) {
00059         // users with unenrol cap may unenrol other users manually - requires enrol/paypal:unenrol
00060         return true;
00061     }
00062 
00063     public function allow_manage(stdClass $instance) {
00064         // users with manage cap may tweak period and status - requires enrol/paypal:manage
00065         return true;
00066     }
00067 
00068     public function show_enrolme_link(stdClass $instance) {
00069         return ($instance->status == ENROL_INSTANCE_ENABLED);
00070     }
00071 
00078     public function add_course_navigation($instancesnode, stdClass $instance) {
00079         if ($instance->enrol !== 'paypal') {
00080              throw new coding_exception('Invalid enrol instance type!');
00081         }
00082 
00083         $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
00084         if (has_capability('enrol/paypal:config', $context)) {
00085             $managelink = new moodle_url('/enrol/paypal/edit.php', array('courseid'=>$instance->courseid, 'id'=>$instance->id));
00086             $instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING);
00087         }
00088     }
00089 
00095     public function get_action_icons(stdClass $instance) {
00096         global $OUTPUT;
00097 
00098         if ($instance->enrol !== 'paypal') {
00099             throw new coding_exception('invalid enrol instance!');
00100         }
00101         $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
00102 
00103         $icons = array();
00104 
00105         if (has_capability('enrol/paypal:config', $context)) {
00106             $editlink = new moodle_url("/enrol/paypal/edit.php", array('courseid'=>$instance->courseid, 'id'=>$instance->id));
00107             $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('i/edit', get_string('edit'), 'core', array('class'=>'icon')));
00108         }
00109 
00110         return $icons;
00111     }
00112 
00118     public function get_newinstance_link($courseid) {
00119         $context = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST);
00120 
00121         if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/paypal:config', $context)) {
00122             return NULL;
00123         }
00124 
00125         // multiple instances supported - different cost for different roles
00126         return new moodle_url('/enrol/paypal/edit.php', array('courseid'=>$courseid));
00127     }
00128 
00136     function enrol_page_hook(stdClass $instance) {
00137         global $CFG, $USER, $OUTPUT, $PAGE, $DB;
00138 
00139         ob_start();
00140 
00141         if ($DB->record_exists('user_enrolments', array('userid'=>$USER->id, 'enrolid'=>$instance->id))) {
00142             return ob_get_clean();
00143         }
00144 
00145         if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) {
00146             return ob_get_clean();
00147         }
00148 
00149         if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) {
00150             return ob_get_clean();
00151         }
00152 
00153         $course = $DB->get_record('course', array('id'=>$instance->courseid));
00154         $context = get_context_instance(CONTEXT_COURSE, $course->id);
00155 
00156         $shortname = format_string($course->shortname, true, array('context' => $context));
00157         $strloginto = get_string("loginto", "", $shortname);
00158         $strcourses = get_string("courses");
00159 
00160         // Pass $view=true to filter hidden caps if the user cannot see them
00161         if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC',
00162                                              '', '', '', '', false, true)) {
00163             $users = sort_by_roleassignment_authority($users, $context);
00164             $teacher = array_shift($users);
00165         } else {
00166             $teacher = false;
00167         }
00168 
00169         if ( (float) $instance->cost <= 0 ) {
00170             $cost = (float) $this->get_config('cost');
00171         } else {
00172             $cost = (float) $instance->cost;
00173         }
00174 
00175         if (abs($cost) < 0.01) { // no cost, other enrolment methods (instances) should be used
00176             echo '<p>'.get_string('nocost', 'enrol_paypal').'</p>';
00177         } else {
00178 
00179             if (isguestuser()) { // force login only for guest user, not real users with guest role
00180                 if (empty($CFG->loginhttps)) {
00181                     $wwwroot = $CFG->wwwroot;
00182                 } else {
00183                     // This actually is not so secure ;-), 'cause we're
00184                     // in unencrypted connection...
00185                     $wwwroot = str_replace("http://", "https://", $CFG->wwwroot);
00186                 }
00187                 echo '<div class="mdl-align"><p>'.get_string('paymentrequired').'</p>';
00188                 echo '<p><b>'.get_string('cost').": $instance->currency $cost".'</b></p>';
00189                 echo '<p><a href="'.$wwwroot.'/login/">'.get_string('loginsite').'</a></p>';
00190                 echo '</div>';
00191             } else {
00192                 //Sanitise some fields before building the PayPal form
00193                 $coursefullname  = format_string($course->fullname, true, array('context'=>$context));
00194                 $courseshortname = $shortname;
00195                 $userfullname    = fullname($USER);
00196                 $userfirstname   = $USER->firstname;
00197                 $userlastname    = $USER->lastname;
00198                 $useraddress     = $USER->address;
00199                 $usercity        = $USER->city;
00200                 $instancename    = $this->get_instance_name($instance);
00201 
00202                 include($CFG->dirroot.'/enrol/paypal/enrol.html');
00203             }
00204 
00205         }
00206 
00207         return $OUTPUT->box(ob_get_clean());
00208     }
00209 
00210 }
 All Data Structures Namespaces Files Functions Variables Enumerations