diff --git a/enrol/authorize/index.php b/enrol/authorize/index.php index c9aee0145b9..e30e9d47244 100644 --- a/enrol/authorize/index.php +++ b/enrol/authorize/index.php @@ -1,570 +1,37 @@ -dirroot.'/enrol/authorize/const.php'; -require_once $CFG->dirroot.'/enrol/authorize/action.php'; + require_once '../../config.php'; + require_once 'lib.php'; -define('ORDER_CAPTURE', 'capture'); -define('ORDER_DELETE', 'delete'); -define('ORDER_REFUND', 'refund'); -define('ORDER_VOID', 'void'); - -if (!($site = get_site())) { - error("Could not find a site!"); -} - -require_login(); - -if (isguest()) { - error("Guests cannot use this page."); -} - -$orderid = optional_param('order', 0, PARAM_INT); - -$strs = get_strings(array('user','status','action','delete','time','course','confirm','yes','no','none','error')); -$authstrs = get_strings(array('paymentmanagement','orderid','void','capture','refund','delete', - 'authcaptured','authorizedpendingcapture','capturedpendingsettle','capturedsettled', - 'settled','refunded','cancelled','expired','tested', - 'transid','settlementdate','notsettled','amount', - 'howmuch','captureyes','unenrolstudent'), 'enrol_authorize'); - -print_header("$site->shortname: $authstrs->paymentmanagement", - "$site->fullname", - "$authstrs->paymentmanagement", ""); - -if (!empty($orderid)) { - authorize_order_details($orderid); -} -else { - authorize_orders(); -} -print_footer(); - - -function authorize_orders() -{ - global $CFG, $USER; - global $strs, $authstrs; - require_once $CFG->libdir.'/tablelib.php'; - - $perpage = 10; - $userid = optional_param('user', 0, PARAM_INT); - $courseid = optional_param('course', SITEID, PARAM_INT); - $status = optional_param('status', AN_STATUS_NONE, PARAM_INT); - - if (!isteacher($courseid)) { - $userid = $USER->id; + if (! $site = get_site()) { + error("Could not find a site!"); } - $baseurl = $CFG->wwwroot."/enrol/authorize/index.php?user=$userid"; - $statusmenu = array(AN_STATUS_NONE => get_string('all'), - AN_STATUS_AUTH => $authstrs->authorizedpendingcapture, - AN_STATUS_AUTHCAPTURE => $authstrs->authcaptured, - AN_STATUS_CREDIT => $authstrs->refunded, - AN_STATUS_VOID => $authstrs->cancelled, - AN_STATUS_EXPIRE => $authstrs->expired - ); + require_login(); - print_simple_box_start('center'); - echo "$strs->status: "; - echo popup_form($baseurl.'&course='.$courseid.'&status=', $statusmenu, 'statusmenu', $status, '', '', '', true); - if ($courses = get_courses('all', 'c.sortorder ASC', 'c.id,c.fullname,c.enrol')) { - $popupcrs = array(); - foreach ($courses as $crs) { - if ($crs->enrol == 'authorize' || (empty($crs->enrol) && $CFG->enrol == 'authorize')) { - $popupcrs[(int)$crs->id] = $crs->fullname; - } - } - echo " $strs->course: "; - echo popup_form($baseurl.'&status='.$status.'&course=', $popupcrs, 'coursesmenu', $courseid, '', '', '', true); + if (isguest()) { + error("Guests cannot use this page."); } - print_simple_box_end(); - $table = new flexible_table('enrol-authorize'); - $table->set_attribute('width', '100%'); - $table->set_attribute('cellspacing', '0'); - $table->set_attribute('cellpadding', '3'); - $table->set_attribute('id', 'orders'); - $table->set_attribute('class', 'generaltable generalbox'); + $strs = get_strings(array('user','status','action','delete','time','course','confirm','yes','no','none','error')); + $authstrs = get_strings(array('paymentmanagement','orderid','void','capture','refund','delete', + 'authcaptured','authorizedpendingcapture','capturedpendingsettle','capturedsettled', + 'settled','refunded','cancelled','expired','tested', + 'transid','settlementdate','notsettled','amount', + 'howmuch','captureyes','unenrolstudent'), 'enrol_authorize'); - $table->define_columns(array('id', 'timecreated', 'userid', 'status', '')); - $table->define_headers(array($authstrs->orderid, $strs->time, $strs->user, $strs->status, $strs->action)); - $table->define_baseurl($baseurl."&status=$status"); + print_header("$site->shortname: $authstrs->paymentmanagement", + "$site->fullname", + "$authstrs->paymentmanagement", ""); - $table->sortable(true); - $table->pageable(true); - $table->setup(); + $orderid = optional_param('order', 0, PARAM_INT); - $select = "SELECT E.id, E.transid, E.courseid, E.userid, E.status, E.ccname, E.timecreated, E.settletime"; - $from = " FROM {$CFG->prefix}enrol_authorize E "; - - if ($status > AN_STATUS_NONE) { - if ($status == AN_STATUS_CREDIT) { - $from .= "INNER JOIN {$CFG->prefix}enrol_authorize_refunds R ON E.id = R.orderid "; - $where = "WHERE (E.status = '" . AN_STATUS_AUTHCAPTURE . "') "; - } - else { - $where = "WHERE (E.status = '$status') "; - } + if (!empty($orderid)) { + print_authorize_order_details($orderid); } else { - if (empty($CFG->an_test)) { - $where = "WHERE (E.status != '" . AN_STATUS_NONE . "') "; - } - else { - $where = "WHERE (1=1) "; - } + print_authorize_orders(); } - if ($userid > 0) { - $where .= "AND (userid = '" . $userid . "') "; - } - if ($courseid != SITEID) { - $where .= "AND (courseid = '" . $courseid . "') "; - } - - if ($sort = $table->get_sql_sort()) { - $sort = ' ORDER BY ' . $sort; - } - else { - $sort = ' ORDER BY id DESC '; - } - - $totalcount = count_records_sql('SELECT COUNT(*) ' . $from . $where); - $table->initialbars($totalcount > $perpage); - $table->pagesize($perpage, $totalcount); - if ($table->get_page_start() !== '' && $table->get_page_size() !== '') { - $limit = ' ' . sql_paging_limit($table->get_page_start(), $table->get_page_size()); - } - else { - $limit = ''; - } - - if ($records = get_records_sql($select . $from . $where . $sort . $limit)) { - foreach ($records as $record) { - $actionstatus = get_order_status_desc($record); - $actions = ''; - - if (empty($actionstatus->actions)) { - $actions .= $strs->none; - } - else { - foreach ($actionstatus->actions as $value) { - $actions .= " {$authstrs->$value} "; - } - } - - $table->add_data(array( - "$record->id", - userdate($record->timecreated), - $record->ccname, - $authstrs->{$actionstatus->status}, - $actions - )); - } - } - - $table->print_html(); -} - - -function authorize_order_details($orderno) { - global $CFG, $USER; - global $strs, $authstrs; - - $unenrol = optional_param('unenrol', ''); - $cmdconfirm = optional_param('confirm', '', PARAM_ALPHA); - - $cmdcapture = optional_param('capture', '', PARAM_ALPHA); - $cmddelete = optional_param('delete', '', PARAM_ALPHA); - $cmdrefund = optional_param('refund', '', PARAM_ALPHA); - $cmdvoid = optional_param('void', '', PARAM_ALPHA); - - $table->width = '100%'; - $table->size = array('30%', '70%'); - $table->align = array('right', 'left'); - - $sql = "SELECT E.*, C.shortname, C.enrolperiod " . - "FROM {$CFG->prefix}enrol_authorize E " . - "INNER JOIN {$CFG->prefix}course C ON C.id = E.courseid " . - "WHERE E.id = '$orderno'"; - - $order = get_record_sql($sql); - if (!$order) { - notice("Order $orderno not found.", "index.php"); - return; - } - - if ($USER->id != $order->userid) { // Current user viewing someone else's order - if (!isteacher($order->courseid)) { - error("Students can view their order."); - } - } - - echo "
'; -} - - -function get_order_status_desc($order) -{ - global $CFG, $USER; - - $ret = new stdClass(); - $ret->actions = array(); - - if (intval($order->transid) == 0) { // test transaction - if (isadmin() || (!empty($CFG->an_teachermanagepay) && isteacher($order->courseid))) { - $ret->actions = array(ORDER_DELETE); - } - $ret->status = 'tested'; - return $ret; - } - - switch ($order->status) { - case AN_STATUS_AUTH: - $timediff30 = getsettletime(time()) - (30 * 3600 * 24); - if (getsettletime($order->timecreated) < $timediff30) { - $order->status = AN_STATUS_EXPIRE; - update_record("enrol_authorize", $order); - if (isadmin() || (!empty($CFG->an_teachermanagepay) && isteacher($order->courseid))) { - $ret->actions = array(ORDER_DELETE); - } - $ret->status = 'expired'; - } - else { - if (isadmin() || (!empty($CFG->an_teachermanagepay) && isteacher($order->courseid))) { - $ret->actions = array(ORDER_CAPTURE, ORDER_VOID); - } - $ret->status = 'authorizedpendingcapture'; - } - return $ret; - - case AN_STATUS_AUTHCAPTURE: - if (settled($order)) { - if (isadmin() || (!empty($CFG->an_teachermanagepay) && isteacher($order->courseid))) { - $ret->actions = array(ORDER_REFUND); - } - $ret->status = 'capturedsettled'; - } - else { - if (isadmin() || (!empty($CFG->an_teachermanagepay) && isteacher($order->courseid))) { - $ret->actions = array(ORDER_VOID); - } - $ret->status = 'capturedpendingsettle'; - } - return $ret; - - case AN_STATUS_CREDIT: - if (settled($order)) { - $ret->actions = array(); - $ret->status = 'settled'; - } - else { - if (isadmin() || (!empty($CFG->an_teachermanagepay) && isteacher($order->courseid))) { - $ret->actions = array(ORDER_VOID); - } - $ret->status = 'refunded'; - } - return $ret; - - case AN_STATUS_VOID: - $ret->actions = array(); - $ret->status = 'cancelled'; - return $ret; - - case AN_STATUS_EXPIRE: - if (isadmin() || (!empty($CFG->an_teachermanagepay) && isteacher($order->courseid))) { - $ret->actions = array(ORDER_DELETE); - } - $ret->status = 'expired'; - return $ret; - - default: - return $ret; - } - -} + print_footer(); ?> diff --git a/enrol/authorize/lib.php b/enrol/authorize/lib.php new file mode 100644 index 00000000000..80751b3e324 --- /dev/null +++ b/enrol/authorize/lib.php @@ -0,0 +1,538 @@ +dirroot.'/enrol/authorize/const.php'; +require_once $CFG->dirroot.'/enrol/authorize/action.php'; + +define('ORDER_CAPTURE', 'capture'); +define('ORDER_DELETE', 'delete'); +define('ORDER_REFUND', 'refund'); +define('ORDER_VOID', 'void'); + +function print_authorize_orders() +{ + global $CFG, $USER; + global $strs, $authstrs; + require_once $CFG->libdir.'/tablelib.php'; + + $perpage = 10; + $userid = optional_param('user', 0, PARAM_INT); + $courseid = optional_param('course', SITEID, PARAM_INT); + $status = optional_param('status', AN_STATUS_NONE, PARAM_INT); + + if (!isteacher($courseid)) { + $userid = $USER->id; + } + + $baseurl = $CFG->wwwroot."/enrol/authorize/index.php?user=$userid"; + $statusmenu = array(AN_STATUS_NONE => get_string('all'), + AN_STATUS_AUTH => $authstrs->authorizedpendingcapture, + AN_STATUS_AUTHCAPTURE => $authstrs->authcaptured, + AN_STATUS_CREDIT => $authstrs->refunded, + AN_STATUS_VOID => $authstrs->cancelled, + AN_STATUS_EXPIRE => $authstrs->expired + ); + + print_simple_box_start('center'); + echo "$strs->status: "; + echo popup_form($baseurl.'&course='.$courseid.'&status=', $statusmenu, 'statusmenu', $status, '', '', '', true); + if ($courses = get_courses('all', 'c.sortorder ASC', 'c.id,c.fullname,c.enrol')) { + $popupcrs = array(); + foreach ($courses as $crs) { + if ($crs->enrol == 'authorize' || (empty($crs->enrol) && $CFG->enrol == 'authorize')) { + $popupcrs[(int)$crs->id] = $crs->fullname; + } + } + echo " $strs->course: "; + echo popup_form($baseurl.'&status='.$status.'&course=', $popupcrs, 'coursesmenu', $courseid, '', '', '', true); + } + print_simple_box_end(); + + $table = new flexible_table('enrol-authorize'); + $table->set_attribute('width', '100%'); + $table->set_attribute('cellspacing', '0'); + $table->set_attribute('cellpadding', '3'); + $table->set_attribute('id', 'orders'); + $table->set_attribute('class', 'generaltable generalbox'); + + $table->define_columns(array('id', 'timecreated', 'userid', 'status', '')); + $table->define_headers(array($authstrs->orderid, $strs->time, $strs->user, $strs->status, $strs->action)); + $table->define_baseurl($baseurl."&status=$status"); + + $table->sortable(true); + $table->pageable(true); + $table->setup(); + + $select = "SELECT E.id, E.transid, E.courseid, E.userid, E.status, E.ccname, E.timecreated, E.settletime"; + $from = " FROM {$CFG->prefix}enrol_authorize E "; + + if ($status > AN_STATUS_NONE) { + if ($status == AN_STATUS_CREDIT) { + $from .= "INNER JOIN {$CFG->prefix}enrol_authorize_refunds R ON E.id = R.orderid "; + $where = "WHERE (E.status = '" . AN_STATUS_AUTHCAPTURE . "') "; + } + else { + $where = "WHERE (E.status = '$status') "; + } + } + else { + if (empty($CFG->an_test)) { + $where = "WHERE (E.status != '" . AN_STATUS_NONE . "') "; + } + else { + $where = "WHERE (1=1) "; + } + } + + if ($userid > 0) { + $where .= "AND (userid = '" . $userid . "') "; + } + if ($courseid != SITEID) { + $where .= "AND (courseid = '" . $courseid . "') "; + } + + if ($sort = $table->get_sql_sort()) { + $sort = ' ORDER BY ' . $sort; + } + else { + $sort = ' ORDER BY id DESC '; + } + + $totalcount = count_records_sql('SELECT COUNT(*) ' . $from . $where); + $table->initialbars($totalcount > $perpage); + $table->pagesize($perpage, $totalcount); + if ($table->get_page_start() !== '' && $table->get_page_size() !== '') { + $limit = ' ' . sql_paging_limit($table->get_page_start(), $table->get_page_size()); + } + else { + $limit = ''; + } + + if ($records = get_records_sql($select . $from . $where . $sort . $limit)) { + foreach ($records as $record) { + $actionstatus = get_order_status_desc($record); + $actions = ''; + + if (empty($actionstatus->actions)) { + $actions .= $strs->none; + } + else { + foreach ($actionstatus->actions as $value) { + $actions .= " {$authstrs->$value} "; + } + } + + $table->add_data(array( + "$record->id", + userdate($record->timecreated), + $record->ccname, + $authstrs->{$actionstatus->status}, + $actions + )); + } + } + + $table->print_html(); +} + + +function print_authorize_order_details($orderno) { + global $CFG, $USER; + global $strs, $authstrs; + + $unenrol = optional_param('unenrol', ''); + $cmdconfirm = optional_param('confirm', '', PARAM_ALPHA); + + $cmdcapture = optional_param('capture', '', PARAM_ALPHA); + $cmddelete = optional_param('delete', '', PARAM_ALPHA); + $cmdrefund = optional_param('refund', '', PARAM_ALPHA); + $cmdvoid = optional_param('void', '', PARAM_ALPHA); + + $table->width = '100%'; + $table->size = array('30%', '70%'); + $table->align = array('right', 'left'); + + $sql = "SELECT E.*, C.shortname, C.enrolperiod " . + "FROM {$CFG->prefix}enrol_authorize E " . + "INNER JOIN {$CFG->prefix}course C ON C.id = E.courseid " . + "WHERE E.id = '$orderno'"; + + $order = get_record_sql($sql); + if (!$order) { + notice("Order $orderno not found.", "index.php"); + return; + } + + if ($USER->id != $order->userid) { // Current user viewing someone else's order + if (!isteacher($order->courseid)) { + error("Students can view their order."); + } + } + + echo "'; +} + + +function get_order_status_desc($order) +{ + global $CFG, $USER; + + $ret = new stdClass(); + $ret->actions = array(); + + if (intval($order->transid) == 0) { // test transaction + if (isadmin() || (!empty($CFG->an_teachermanagepay) && isteacher($order->courseid))) { + $ret->actions = array(ORDER_DELETE); + } + $ret->status = 'tested'; + return $ret; + } + + switch ($order->status) { + case AN_STATUS_AUTH: + $timediff30 = getsettletime(time()) - (30 * 3600 * 24); + if (getsettletime($order->timecreated) < $timediff30) { + $order->status = AN_STATUS_EXPIRE; + update_record("enrol_authorize", $order); + if (isadmin() || (!empty($CFG->an_teachermanagepay) && isteacher($order->courseid))) { + $ret->actions = array(ORDER_DELETE); + } + $ret->status = 'expired'; + } + else { + if (isadmin() || (!empty($CFG->an_teachermanagepay) && isteacher($order->courseid))) { + $ret->actions = array(ORDER_CAPTURE, ORDER_VOID); + } + $ret->status = 'authorizedpendingcapture'; + } + return $ret; + + case AN_STATUS_AUTHCAPTURE: + if (settled($order)) { + if (isadmin() || (!empty($CFG->an_teachermanagepay) && isteacher($order->courseid))) { + $ret->actions = array(ORDER_REFUND); + } + $ret->status = 'capturedsettled'; + } + else { + if (isadmin() || (!empty($CFG->an_teachermanagepay) && isteacher($order->courseid))) { + $ret->actions = array(ORDER_VOID); + } + $ret->status = 'capturedpendingsettle'; + } + return $ret; + + case AN_STATUS_CREDIT: + if (settled($order)) { + $ret->actions = array(); + $ret->status = 'settled'; + } + else { + if (isadmin() || (!empty($CFG->an_teachermanagepay) && isteacher($order->courseid))) { + $ret->actions = array(ORDER_VOID); + } + $ret->status = 'refunded'; + } + return $ret; + + case AN_STATUS_VOID: + $ret->actions = array(); + $ret->status = 'cancelled'; + return $ret; + + case AN_STATUS_EXPIRE: + if (isadmin() || (!empty($CFG->an_teachermanagepay) && isteacher($order->courseid))) { + $ret->actions = array(ORDER_DELETE); + } + $ret->status = 'expired'; + return $ret; + + default: + return $ret; + } + +} +?>