Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/enrol/authorize/locallib.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 
00029 if (!defined('MOODLE_INTERNAL')) {
00030     die('Direct access to this script is forbidden.');
00031 }
00032 
00033 define('ORDER_CAPTURE', 'capture');
00034 define('ORDER_DELETE',  'delete');
00035 define('ORDER_REFUND',  'refund');
00036 define('ORDER_VOID',    'void');
00037 
00042 function authorize_print_orders($courseid, $userid) {
00043     global $course;
00044     global $CFG, $USER, $SITE, $DB, $OUTPUT, $PAGE;
00045     global $strs, $authstrs;
00046 
00047     $plugin = enrol_get_plugin('authorize');
00048 
00049     require_once($CFG->libdir.'/tablelib.php');
00050 
00051     $perpage = optional_param('perpage', 10, PARAM_INT);
00052     $showonlymy = optional_param('showonlymy', 0, PARAM_BOOL);
00053     $searchquery = optional_param('searchquery', '0', PARAM_INT);
00054     $searchtype = optional_param('searchtype', 'orderid', PARAM_ALPHA);
00055     $status = optional_param('status', AN_STATUS_NONE, PARAM_INT);
00056 
00057     $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
00058 
00059     $searchmenu = array('orderid' => $authstrs->orderid, 'transid' => $authstrs->transid, 'cclastfour' => $authstrs->cclastfour);
00060     $buttons = "<form method='post' action='index.php' autocomplete='off'><div>";
00061     $buttons .= html_writer::select($searchmenu, 'searchtype', $searchtype, false);
00062     $buttons .= "<input type='text' size='16' name='searchquery' value='' />";
00063     $buttons .= "<input type='submit' value='$strs->search' />";
00064     $buttons .= "</div></form>";
00065 
00066     if (has_capability('enrol/authorize:uploadcsv', get_context_instance(CONTEXT_USER, $USER->id))) {
00067         $buttons .= "<form method='get' action='uploadcsv.php'><div><input type='submit' value='".get_string('uploadcsv', 'enrol_authorize')."' /></div></form>";
00068     }
00069 
00070     $canmanagepayments = has_capability('enrol/authorize:managepayments', $coursecontext);
00071     if ($showonlymy || !$canmanagepayments) {
00072         $userid = $USER->id;
00073     }
00074 
00075     $baseurl = $CFG->wwwroot.'/enrol/authorize/index.php?user='.$userid;
00076 
00077     $params = array('userid'=>$userid);
00078     $sql = "SELECT c.id, c.fullname FROM {course} c JOIN {enrol_authorize} e ON c.id = e.courseid ";
00079     $sql .= ($userid > 0) ? "WHERE (e.userid=:userid) " : '';
00080     $sql .= "ORDER BY c.sortorder, c.fullname";
00081     if (($popupcrs = $DB->get_records_sql_menu($sql, $params))) {
00082         $popupcrs = array($SITE->id => $SITE->fullname) + $popupcrs;
00083     }
00084     $popupmenu = empty($popupcrs) ? '' : $OUTPUT->single_select(new moodle_url($baseurl.'&status='.$status), 'course', $popupcrs, $courseid, null, 'coursesmenu');
00085     $popupmenu .= '<br />';
00086     $statusmenu = array(
00087         AN_STATUS_NONE => $strs->all,
00088         AN_STATUS_AUTH | AN_STATUS_UNDERREVIEW | AN_STATUS_APPROVEDREVIEW => $authstrs->allpendingorders,
00089         AN_STATUS_AUTH => $authstrs->authorizedpendingcapture,
00090         AN_STATUS_AUTHCAPTURE => $authstrs->authcaptured,
00091         AN_STATUS_CREDIT => $authstrs->refunded,
00092         AN_STATUS_VOID => $authstrs->cancelled,
00093         AN_STATUS_EXPIRE => $authstrs->expired,
00094         AN_STATUS_UNDERREVIEW => $authstrs->underreview,
00095         AN_STATUS_APPROVEDREVIEW => $authstrs->approvedreview,
00096         AN_STATUS_REVIEWFAILED => $authstrs->reviewfailed,
00097         AN_STATUS_TEST => $authstrs->tested
00098     );
00099 
00100     $popupmenu .= $OUTPUT->single_select(new moodle_url($baseurl.'&course='.$courseid), 'status', $statusmenu, $status, null, 'statusmenu');
00101     if ($canmanagepayments) {
00102         $popupmenu .= '<br />';
00103         $PAGE->requires->js('/enrol/authorize/authorize.js');
00104         $aid = $OUTPUT->add_action_handler(new component_action('click', 'authorize_jump_to_mypayments', array('userid' => $USER->id, 'status' => $status)));
00105         $popupmenu .= html_writer::checkbox('enrol_authorize', 1, $userid == $USER->id, get_string('mypaymentsonly', 'enrol_authorize'), array('id'=>$aid));
00106     }
00107 
00108     if (SITEID != $courseid) {
00109         $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
00110         $PAGE->navbar->add($shortname, new moodle_url('/course/view.php', array('id'=>$course->id)));
00111     }
00112     $PAGE->navbar->add($authstrs->paymentmanagement, 'index.php');
00113     $PAGE->set_title("$course->shortname: $authstrs->paymentmanagement");
00114     $PAGE->set_heading($authstrs->paymentmanagement);
00115     $PAGE->set_headingmenu($popupmenu);
00116     $PAGE->set_button($buttons);
00117     echo $OUTPUT->header();
00118 
00119     $table = new flexible_table('enrol-authorize');
00120     $table->set_attribute('width', '100%');
00121     $table->set_attribute('cellspacing', '0');
00122     $table->set_attribute('cellpadding', '3');
00123     $table->set_attribute('id', 'orders');
00124     $table->set_attribute('class', 'generaltable generalbox');
00125 
00126     if ($perpage > 100) { $perpage = 100; }
00127     $perpagemenus = array(5 => 5, 10 => 10, 20 => 20, 50 => 50, 100 => 100);
00128     $perpagemenu = $OUTPUT->single_select(new moodle_url($baseurl.'&status='.$status.'&course='.$courseid), 'perpage', $perpagemenus, $perpage, array(''=>'choosedots'), 'perpagemenu');
00129     $table->define_columns(array('id', 'userid', 'timecreated', 'status', 'action'));
00130     $table->define_headers(array($authstrs->orderid, $authstrs->shopper, $strs->time, $strs->status, $perpagemenu));
00131     $table->define_baseurl($baseurl."&amp;status=$status&amp;course=$courseid&amp;perpage=$perpage");
00132 
00133     $table->no_sorting('action');
00134     $table->sortable(true, 'id', SORT_DESC);
00135     $table->pageable(true);
00136     $table->setup();
00137 
00138     $select = "SELECT e.id, e.paymentmethod, e.refundinfo, e.transid, e.courseid, e.userid, e.status, e.ccname, e.timecreated, e.settletime ";
00139     $from   = "FROM {enrol_authorize} e ";
00140     $where  = "WHERE (1=1) ";
00141     $params = array();
00142 
00143     if (!empty($searchquery)) {
00144         switch($searchtype) {
00145             case 'orderid':
00146                 $where = "WHERE (e.id = :searchquery) ";
00147                 $params['searchquery'] = $searchquery;
00148                 break;
00149 
00150             case 'transid':
00151                 $where = "WHERE (e.transid = :searchquery) ";
00152                 $params['searchquery'] = $searchquery;
00153                 break;
00154 
00155             case 'cclastfour':
00156                 $searchquery = sprintf("%04d", $searchquery);
00157                 $where = "WHERE (e.refundinfo = :searchquery) AND (e.paymentmethod=:method) ";
00158                 $params['searchquery'] = $searchquery;
00159                 $params['method'] = AN_METHOD_CC;
00160                 break;
00161         }
00162     }
00163     else {
00164         switch ($status)
00165         {
00166             case AN_STATUS_NONE:
00167                 if (!$plugin->get_config('an_test')) {
00168                     $where .= "AND (e.status != :status) ";
00169                     $params['status'] = AN_STATUS_NONE;
00170                 }
00171                 break;
00172 
00173             case AN_STATUS_TEST:
00174                 $newordertime = time() - 120; // -2 minutes. Order may be still in process.
00175                 $where .= "AND (e.status = :status) AND (e.transid = '0') AND (e.timecreated < :newordertime) ";
00176                 $params['status'] = AN_STATUS_NONE;
00177                 $params['newordertime'] = $newordertime;
00178                 break;
00179 
00180             case AN_STATUS_AUTH | AN_STATUS_UNDERREVIEW | AN_STATUS_APPROVEDREVIEW:
00181                 $where .= 'AND (e.status IN(:status1,:status2,:status3)) ';
00182                 $params['status1'] = AN_STATUS_AUTH;
00183                 $params['status2'] = AN_STATUS_UNDERREVIEW;
00184                 $params['status3'] = AN_STATUS_APPROVEDREVIEW;
00185                 break;
00186 
00187             case AN_STATUS_CREDIT:
00188                 $from .= "INNER JOIN {enrol_authorize_refunds} r ON e.id = r.orderid ";
00189                 $where .= "AND (e.status = :status) ";
00190                 $params['status'] = AN_STATUS_AUTHCAPTURE;
00191                 break;
00192 
00193             default:
00194                 $where .= "AND (e.status = :status) ";
00195                 $params['status'] = $status;
00196                 break;
00197         }
00198 
00199         if (SITEID != $courseid) {
00200             $where .= "AND (e.courseid = :courseid) ";
00201             $params['courseid'] = $courseid;
00202         }
00203     }
00204 
00205     // This must be always LAST where!!!
00206     if ($userid > 0) {
00207         $where .= "AND (e.userid = :userid) ";
00208         $params['userid'] = $userid;
00209     }
00210 
00211     if (($sort = $table->get_sql_sort())) {
00212         $sort = ' ORDER BY ' . $sort;
00213     }
00214 
00215     $totalcount = $DB->count_records_sql('SELECT COUNT(*) ' . $from . $where, $params);
00216     $table->initialbars($totalcount > $perpage);
00217     $table->pagesize($perpage, $totalcount);
00218 
00219     if (($records = $DB->get_records_sql($select . $from . $where . $sort, $params, $table->get_page_start(), $table->get_page_size()))) {
00220         foreach ($records as $record) {
00221             $actionstatus = authorize_get_status_action($record);
00222             $color = authorize_get_status_color($actionstatus->status);
00223             $actions = '';
00224 
00225             if (empty($actionstatus->actions)) {
00226                 $actions .= $strs->none;
00227             }
00228             else {
00229                 foreach ($actionstatus->actions as $val) {
00230                     $actions .= authorize_print_action_button($record->id, $val);
00231                 }
00232             }
00233 
00234             $table->add_data(array(
00235                 "<a href='index.php?order=$record->id'>$record->id</a>",
00236                 $record->ccname,
00237                 userdate($record->timecreated),
00238                 "<font style='color:$color'>" . $authstrs->{$actionstatus->status} . "</font>",
00239                 $actions
00240             ));
00241         }
00242     }
00243 
00244     $table->print_html();
00245     echo $OUTPUT->footer();
00246 }
00247 
00253 function authorize_print_order($orderid)
00254 {
00255     global $CFG, $USER, $DB, $OUTPUT, $PAGE;
00256     global $strs, $authstrs;
00257 
00258     $plugin = enrol_get_plugin('authorize');
00259     $an_test = $plugin->get_config('an_test');
00260 
00261     $do = optional_param('do', '', PARAM_ALPHA);
00262     $unenrol = optional_param('unenrol', 0, PARAM_BOOL);
00263     $confirm = optional_param('confirm', 0, PARAM_BOOL);
00264 
00265     if (!$order = $DB->get_record('enrol_authorize', array('id'=>$orderid))) {
00266         print_error('orderidnotfound', '',
00267                 "$CFG->wwwroot/enrol/authorize/index.php", $orderid);
00268     }
00269 
00270     if (!$course = $DB->get_record('course', array('id'=>$order->courseid))) {
00271         print_error('invalidcourseid', '', "$CFG->wwwroot/enrol/authorize/index.php");
00272     }
00273 
00274     if (!$user = $DB->get_record('user', array('id'=>$order->userid))) {
00275         print_error('nousers', '', "$CFG->wwwroot/enrol/authorize/index.php");
00276     }
00277 
00278     $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00279     if ($USER->id != $order->userid) { // Current user viewing someone else's order
00280         require_capability('enrol/authorize:managepayments', $coursecontext);
00281     }
00282 
00283     $settled = AuthorizeNet::settled($order);
00284     $statusandactions = authorize_get_status_action($order);
00285     $color = authorize_get_status_color($statusandactions->status);
00286 
00287     $buttons = '';
00288     if (empty($do))
00289     {
00290         if (empty($statusandactions->actions)) {
00291             if ((AN_METHOD_ECHECK == $order->paymentmethod) && has_capability('enrol/authorize:uploadcsv', get_context_instance(CONTEXT_USER, $USER->id))) {
00292                 $buttons .= "<form method='get' action='uploadcsv.php'><div><input type='submit' value='".get_string('uploadcsv', 'enrol_authorize')."' /></div></form>";
00293             }
00294         }
00295         else {
00296             foreach ($statusandactions->actions as $val) {
00297                 $buttons .= authorize_print_action_button($orderid, $val);
00298             }
00299         }
00300     }
00301 
00302     if (SITEID != $course->id) {
00303         $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
00304         $PAGE->navbar->add($shortname, new moodle_url('/course/view.php', array('id'=>$course->id)));
00305     }
00306     $PAGE->navbar->add($authstrs->paymentmanagement, 'index.php?course='.$course->id);
00307     $PAGE->navbar->add($authstrs->orderid . ': ' . $orderid, 'index.php');
00308     $PAGE->set_course($course);
00309     $PAGE->set_title("$course->shortname: $authstrs->paymentmanagement");
00310     $PAGE->set_heading($authstrs->orderdetails);
00311     $PAGE->set_cacheable(false);
00312     $PAGE->set_button($buttons);
00313     echo $OUTPUT->header();
00314 
00315     $table = new html_table();
00316     $table->width = '100%';
00317     $table->size = array('30%', '70%');
00318     $table->align = array('right', 'left');
00319 
00320     if (AN_METHOD_CC == $order->paymentmethod) {
00321         $table->data[] = array("<b>$authstrs->paymentmethod:</b>", $authstrs->methodcc);
00322         $table->data[] = array("<b>$authstrs->nameoncard:</b>", $order->ccname . ' (<b><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'">'.fullname($user).'</a></b>)');
00323         $table->data[] = array("<b>$authstrs->cclastfour:</b>", $order->refundinfo);
00324     }
00325     else {
00326         $table->data[] = array("<b>$authstrs->paymentmethod:</b>", $authstrs->methodecheck);
00327         $table->data[] = array("<b>$authstrs->echeckfirslasttname:</b>", $order->ccname . ' (<b><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'">'.fullname($user).'</a></b>)');
00328         $table->data[] = array("<b>$authstrs->isbusinesschecking:</b>", ($order->refundinfo == 1) ? $strs->yes : $strs->no);
00329     }
00330 
00331     $table->data[] = array("<b>$authstrs->amount:</b>", "$order->currency $order->amount");
00332     $table->data[] = array("<b>$authstrs->transid:</b>", $order->transid);
00333     $table->data[] = array("<b>$strs->time:</b>", userdate($order->timecreated));
00334     $table->data[] = array("<b>$authstrs->settlementdate:</b>", $settled ? userdate($order->settletime) : $authstrs->notsettled);
00335     $table->data[] = array("<b>$strs->status:</b>", "<b><font style='color:$color'>" . $authstrs->{$statusandactions->status} . "</font></b>");
00336 
00337     if (ORDER_CAPTURE == $do && in_array(ORDER_CAPTURE, $statusandactions->actions)) {
00338         if ($confirm && confirm_sesskey()) {
00339             $message = '';
00340             $extra = NULL;
00341             if (AN_APPROVED == AuthorizeNet::process($order, $message, $extra, AN_ACTION_PRIOR_AUTH_CAPTURE)) {
00342                 if (empty($an_test)) {
00343                     if (enrol_into_course($course, $user, 'authorize')) {
00344                         if ($plugin->get_config('enrol_mailstudents')) {
00345                             send_welcome_messages($orderid);
00346                         }
00347                         redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid");
00348                     }
00349                     else {
00350                         $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
00351                         redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", "Error while trying to enrol ".fullname($user)." in '" . $shortname . "'", 20);
00352                     }
00353                 }
00354                 else {
00355                     redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", get_string('testwarning', 'enrol_authorize'), 10);
00356                 }
00357             }
00358             else {
00359                 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", $message, 20);
00360             }
00361         }
00362         $table->data[] = array("<b>$strs->confirm:</b>", get_string('captureyes', 'enrol_authorize') . '<br />' .
00363                                authorize_print_action_button($orderid, ORDER_CAPTURE, 0, true, false, $strs->no));
00364         echo html_writer::table($table);
00365     }
00366     elseif (ORDER_REFUND == $do && in_array(ORDER_REFUND, $statusandactions->actions)) {
00367         $refunded = 0.0;
00368         $sql = "SELECT SUM(amount) AS refunded
00369                   FROM {enrol_authorize_refunds}
00370                  WHERE (orderid = ?)
00371                    AND (status = ?)";
00372 
00373         if (($refundval = $DB->get_field_sql($sql, array($orderid, AN_STATUS_CREDIT)))) {
00374             $refunded = floatval($refundval);
00375         }
00376         $upto = round($order->amount - $refunded, 2);
00377         if ($upto <= 0) {
00378             print_error('refoundtoorigi', '',
00379                     "$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", $order->amount);
00380         }
00381         $amount = round(optional_param('amount', $upto, PARAM_RAW), 2);
00382         if ($amount > $upto) {
00383             print_error('refoundto', '',
00384                     "$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", $upto);
00385         }
00386         if ($confirm && confirm_sesskey()) {
00387             $extra = new stdClass;
00388             $extra->orderid = $orderid;
00389             $extra->amount = $amount;
00390             $message = '';
00391             $success = AuthorizeNet::process($order, $message, $extra, AN_ACTION_CREDIT);
00392             if (AN_APPROVED == $success || AN_REVIEW == $success) {
00393                 if (empty($an_test)) {
00394                     if (empty($extra->id)) {
00395                         redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", "insert record error", 20);
00396                     }
00397                     else {
00398                         if (!empty($unenrol)) {
00399                             $pinstance = $DB->get_record('enrol', array('id'=>$order->instanceid));
00400                             $plugin->unenrol_user($pinstance, $order->userid);
00401                             //role_unassign_all(array('userid'=>$order->userid, 'contextid'=>$coursecontext->id, 'component'=>'enrol_authorize'), true, true);
00402                         }
00403                         redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid");
00404                     }
00405                 }
00406                 else {
00407                     redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", get_string('testwarning', 'enrol_authorize'), 10);
00408                 }
00409             }
00410             else {
00411                 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", $message, 20);
00412             }
00413         }
00414         $a = new stdClass;
00415         $a->upto = $upto;
00416         $extrahtml = get_string('howmuch', 'enrol_authorize') .
00417                      ' <input type="text" size="5" name="amount" value="'.$amount.'" /> ' .
00418                      get_string('canbecredit', 'enrol_authorize', $a) . '<br />';
00419         $table->data[] = array("<b>$strs->confirm:</b>",
00420                                authorize_print_action_button($orderid, ORDER_REFUND, 0, true, $authstrs->unenrolstudent, $strs->no, $extrahtml));
00421         echo html_writer::table($table);
00422     }
00423     elseif (ORDER_DELETE == $do && in_array(ORDER_DELETE, $statusandactions->actions)) {
00424         if ($confirm && confirm_sesskey()) {
00425             if (!empty($unenrol)) {
00426                 $pinstance = $DB->get_record('enrol', array('id'=>$order->instanceid));
00427                 $plugin->unenrol_user($pinstance, $order->userid);
00428                 //role_unassign_all(array('userid'=>$order->userid, 'contextid'=>$coursecontext->id, 'component'=>'enrol_authorize'), true, true);
00429             }
00430             $DB->delete_records('enrol_authorize', array('id'=>$orderid));
00431             redirect("$CFG->wwwroot/enrol/authorize/index.php");
00432         }
00433         $table->data[] = array("<b>$strs->confirm:</b>",
00434                                authorize_print_action_button($orderid, ORDER_DELETE, 0, true, $authstrs->unenrolstudent,$strs->no));
00435         echo html_writer::table($table);
00436     }
00437     elseif (ORDER_VOID == $do) { // special case: cancel original or refunded transaction?
00438         $suborderid = optional_param('suborder', 0, PARAM_INT);
00439         if (empty($suborderid) && in_array(ORDER_VOID, $statusandactions->actions)) { // cancel original
00440             if ($confirm && confirm_sesskey()) {
00441                 $extra = NULL;
00442                 $message = '';
00443                 if (AN_APPROVED == AuthorizeNet::process($order, $message, $extra, AN_ACTION_VOID)) {
00444                     if (empty($an_test)) {
00445                         redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid");
00446                     }
00447                     else {
00448                         redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", get_string('testwarning', 'enrol_authorize'), 10);
00449                     }
00450                 }
00451                 else {
00452                     redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", $message, 20);
00453                 }
00454             }
00455             $table->data[] = array("<b>$strs->confirm:</b>", get_string('voidyes', 'enrol_authorize') . '<br />' .
00456                                    authorize_print_action_button($orderid, ORDER_VOID, 0, true, false, $strs->no));
00457             echo html_writer::table($table);
00458         }
00459         elseif (!empty($suborderid)) { // cancel refunded
00460             $sql = "SELECT r.*, e.courseid, e.paymentmethod
00461                       FROM {enrol_authorize_refunds} r
00462                 INNER JOIN {enrol_authorize} e
00463                         ON r.orderid = e.id
00464                      WHERE r.id = ?
00465                        AND r.orderid = ?
00466                        AND r.status = ?";
00467 
00468             $suborder = $DB->get_record_sql($sql, array($suborderid, $orderid, AN_STATUS_CREDIT));
00469             if (!$suborder) { // not found
00470                 print_error('transactionvoid', '', "$CFG->wwwroot/enrol/authorize/index.php?order=$orderid");
00471             }
00472             $refundedstatus = authorize_get_status_action($suborder);
00473             unset($suborder->courseid);
00474             if (in_array(ORDER_VOID, $refundedstatus->actions)) {
00475                 if ($confirm && confirm_sesskey()) {
00476                     $message = '';
00477                     $extra = NULL;
00478                     if (AN_APPROVED == AuthorizeNet::process($suborder, $message, $extra, AN_ACTION_VOID)) {
00479                         if (empty($an_test)) {
00480                             if (!empty($unenrol)) {
00481                                 $pinstance = $DB->get_record('enrol', array('id'=>$order->instanceid));
00482                                 $plugin->unenrol_user($pinstance, $order->userid);
00483                                 //role_unassign_all(array('userid'=>$order->userid, 'contextid'=>$coursecontext->id, 'component'=>'enrol_authorize'), true, true);
00484                             }
00485                             redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid");
00486                         }
00487                         else {
00488                             redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", get_string('testwarning', 'enrol_authorize'), 10);
00489                         }
00490                     }
00491                     else {
00492                         redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", $message, 20);
00493                     }
00494                 }
00495                 $a = new stdClass;
00496                 $a->transid = $suborder->transid;
00497                 $a->amount = $suborder->amount;
00498                 $table->data[] = array("<b>$strs->confirm:</b>", get_string('subvoidyes', 'enrol_authorize', $a) . '<br />' .
00499                                        authorize_print_action_button($orderid, ORDER_VOID, $suborderid, true, $authstrs->unenrolstudent, $strs->no));
00500                 echo html_writer::table($table);
00501             }
00502         }
00503     }
00504     else {
00505         echo html_writer::table($table);
00506 
00507         if ($settled) { // show refunds.
00508             $t2 = new html_table();
00509             $t2->size = array('45%', '15%', '20%', '10%', '10%');
00510             $t2->align = array('right', 'right', 'right', 'right', 'right');
00511             $t2->head = array($authstrs->settlementdate, $authstrs->transid, $strs->status, $strs->action, $authstrs->amount);
00512 
00513             $sql = "SELECT r.*, e.courseid, e.paymentmethod
00514                       FROM {enrol_authorize_refunds} r
00515                 INNER JOIN {enrol_authorize} e
00516                         ON r.orderid = e.id
00517                      WHERE r.orderid = ?";
00518 
00519             if (($refunds = $DB->get_records_sql($sql, array($orderid)))) {
00520                 $sumrefund = floatval(0.0);
00521                 foreach ($refunds as $rf) {
00522                     $subactions = '';
00523                     $substatus = authorize_get_status_action($rf);
00524                     if (empty($substatus->actions)) {
00525                         $subactions .= $strs->none;
00526                     }
00527                     else {
00528                         foreach ($substatus->actions as $vl) {
00529                             $subactions .= authorize_print_action_button($orderid, $vl, $rf->id);
00530                         }
00531                     }
00532                     $sign = '';
00533                     $color = authorize_get_status_color($substatus->status);
00534                     if ($substatus->status == 'refunded' or $substatus->status == 'settled') {
00535                         $sign = '-';
00536                         $sumrefund += floatval($rf->amount);
00537                     }
00538                     $t2->data[] = array(
00539                         userdate($rf->settletime),
00540                         $rf->transid,
00541                         "<b><font style='color:$color'>" .$authstrs->{$substatus->status} . "</font></b>",
00542                         $subactions,
00543                         format_float($sign . $rf->amount, 2)
00544                     );
00545                 }
00546                 $t2->data[] = array('','',get_string('total'),$order->currency,format_float('-'.$sumrefund, 2));
00547             }
00548             else {
00549                 $t2->data[] = array('','',get_string('noreturns', 'enrol_authorize'),'','');
00550             }
00551             echo "<h4>" . get_string('returns', 'enrol_authorize') . "</h4>\n";
00552             echo html_writer::table($t2);
00553         }
00554     }
00555 
00556     echo $OUTPUT->footer();
00557 }
00558 
00565 function authorize_get_status_action($order)
00566 {
00567     global $CFG;
00568     static $newordertime = 0;
00569 
00570     if (0 == $newordertime) {
00571         $newordertime = time() - 120; // -2 minutes. Order may be still in process.
00572     }
00573 
00574     $ret = new stdClass();
00575     $ret->actions = array();
00576 
00577     $canmanage = has_capability('enrol/authorize:managepayments', get_context_instance(CONTEXT_COURSE, $order->courseid));
00578 
00579     if (floatval($order->transid) == 0) { // test transaction or new order
00580         if ($order->timecreated < $newordertime) {
00581             if ($canmanage) {
00582                 $ret->actions = array(ORDER_DELETE);
00583             }
00584             $ret->status = 'tested';
00585         }
00586         else {
00587             $ret->status = 'new';
00588         }
00589         return $ret;
00590     }
00591 
00592     switch ($order->status) {
00593         case AN_STATUS_AUTH:
00594             if (AuthorizeNet::expired($order)) {
00595                 if ($canmanage) {
00596                     $ret->actions = array(ORDER_DELETE);
00597                 }
00598                 $ret->status = 'expired';
00599             }
00600             else {
00601                 if ($canmanage) {
00602                     $ret->actions = array(ORDER_CAPTURE, ORDER_VOID);
00603                 }
00604                 $ret->status = 'authorizedpendingcapture';
00605             }
00606             return $ret;
00607 
00608         case AN_STATUS_AUTHCAPTURE:
00609             if (AuthorizeNet::settled($order)) {
00610                 if ($canmanage) {
00611                     if (($order->paymentmethod == AN_METHOD_CC) || ($order->paymentmethod == AN_METHOD_ECHECK && !empty($order->refundinfo))) {
00612                         $ret->actions = array(ORDER_REFUND);
00613                     }
00614                 }
00615                 $ret->status = 'settled';
00616             }
00617             else {
00618                 if ($order->paymentmethod == AN_METHOD_CC && $canmanage) {
00619                     $ret->actions = array(ORDER_VOID);
00620                 }
00621                 $ret->status = 'capturedpendingsettle';
00622             }
00623             return $ret;
00624 
00625         case AN_STATUS_CREDIT:
00626             if (AuthorizeNet::settled($order)) {
00627                 $ret->status = 'settled';
00628             }
00629             else {
00630                 if ($order->paymentmethod == AN_METHOD_CC && $canmanage) {
00631                     $ret->actions = array(ORDER_VOID);
00632                 }
00633                 $ret->status = 'refunded';
00634             }
00635             return $ret;
00636 
00637         case AN_STATUS_VOID:
00638             $ret->status = 'cancelled';
00639             return $ret;
00640 
00641         case AN_STATUS_EXPIRE:
00642             if ($canmanage) {
00643                 $ret->actions = array(ORDER_DELETE);
00644             }
00645             $ret->status = 'expired';
00646             return $ret;
00647 
00648         case AN_STATUS_UNDERREVIEW:
00649             $ret->status = 'underreview';
00650             return $ret;
00651 
00652         case AN_STATUS_APPROVEDREVIEW:
00653             $ret->status = 'approvedreview';
00654             return $ret;
00655 
00656         case AN_STATUS_REVIEWFAILED:
00657             if ($canmanage) {
00658                 $ret->actions = array(ORDER_DELETE);
00659             }
00660             $ret->status = 'reviewfailed';
00661             return $ret;
00662 
00663         default:
00664             return $ret;
00665     }
00666 }
00667 
00668 
00669 function authorize_get_status_color($status)
00670 {
00671     $color = 'black';
00672     switch ($status)
00673     {
00674         case 'settled':
00675         case 'capturedpendingsettle':
00676             $color = '#339900'; // green
00677             break;
00678 
00679         case 'underreview':
00680         case 'approvedreview':
00681         case 'authorizedpendingcapture':
00682             $color = '#FF6600'; // orange
00683             break;
00684 
00685         case 'new':
00686         case 'tested':
00687             $color = '#003366'; // blue
00688             break;
00689 
00690         case 'expired':
00691         case 'cancelled':
00692         case 'refunded';
00693         case 'reviewfailed':
00694             $color = '#FF0033'; // red
00695             break;
00696     }
00697     return $color;
00698 }
00699 
00700 function authorize_print_action_button($orderid, $do, $suborderid=0, $confirm=false, $unenrol=false, $nobutton=false, $extrahtml='')
00701 {
00702     global $CFG, $OUTPUT;
00703     global $authstrs;
00704 
00705     $ret =  '<form action="'.$CFG->wwwroot.'/enrol/authorize/index.php'.'" method="post"><div>' .
00706             '<input type="hidden" name="order" value="'.$orderid.'" />' .
00707             '<input type="hidden" name="do" value="'.$do.'" />' .
00708             '<input type="hidden" name="sesskey" value="'. sesskey() . '" />';
00709     if (!empty($suborderid)) {
00710         $ret .= '<input type="hidden" name="suborder" value="'.$suborderid.'" />';
00711     }
00712     if (!empty($confirm)) {
00713         $ret .= '<input type="hidden" name="confirm" value="1" />';
00714     }
00715     if (!empty($unenrol)) {
00716         $ret .= html_writer::checkbox('unenrol', 1, false, $unenrol) . '<br />';
00717     }
00718     $ret .= $extrahtml;
00719     $ret .= '<input type="submit" value="'.$authstrs->$do.'" />' .
00720             '</div></form>';
00721     if (!empty($nobutton)) {
00722         $ret .= '<form method="get" action="index.php"><div><input type="hidden" name="order" value="'.$orderid.'" /><input type="submit" value="'.$nobutton.'" /></div></form>';
00723     }
00724     return $ret;
00725 }
00726 
 All Data Structures Namespaces Files Functions Variables Enumerations