adding some strings
[moodle-linuxchix.git] / enrol / authorize / enrol.php
blobffd536ce3db859a91bdfddde42a3c6e59fa7a7df
1 <?php // $Id$
3 require_once($CFG->dirroot.'/enrol/enrol.class.php');
4 require_once($CFG->dirroot.'/enrol/authorize/const.php');
5 require_once($CFG->dirroot.'/enrol/authorize/localfuncs.php');
7 /**
8 * Authorize.net Payment Gateway plugin
9 */
10 class enrolment_plugin_authorize
13 /**
14 * Cron log.
16 * @var string
17 * @access public
19 var $log;
22 /**
23 * Presents registration forms.
25 * @param object $course Course info
26 * @access public
28 function print_entry($course) {
29 global $CFG, $USER, $form;
31 $zerocost = zero_cost($course);
32 if ($zerocost) {
33 $manual = enrolment_factory::factory('manual');
34 if (!empty($this->errormsg)) {
35 $manual->errormsg = $this->errormsg;
37 $manual->print_entry($course);
38 return;
41 prevent_double_paid($course);
42 httpsrequired();
44 if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 443) { // MDL-9836
45 if (empty($CFG->loginhttps)) {
46 error(get_string('httpsrequired', 'enrol_authorize'));
47 } else {
48 $wwwsroot = str_replace('http:','https:', $CFG->wwwroot);
49 redirect("$wwwsroot/course/enrol.php?id=$course->id");
50 exit;
54 $strcourses = get_string('courses');
55 $strloginto = get_string('loginto', '', $course->shortname);
57 print_header($strloginto,
58 $course->fullname,
59 "<a href=\"$CFG->wwwroot/course/\">$strcourses</a> -> $strloginto");
60 print_course($course, '80%');
62 if ($course->password) {
63 print_heading(get_string('choosemethod', 'enrol_authorize'), 'center');
66 print_simple_box_start('center', '80%');
67 if ($USER->username == 'guest') { // only real guest user, not for users with guest role
68 $curcost = get_course_cost($course);
69 echo '<div align="center">';
70 echo '<p>'.get_string('paymentrequired').'</p>';
71 echo '<p><b>'.get_string('cost').": $curcost[currency] $curcost[cost]".'</b></p>';
72 echo '<p><a href="'.$CFG->httpswwwroot.'/login/">'.get_string('loginsite').'</a></p>';
73 echo '</div>';
75 else {
76 require_once($CFG->dirroot.'/enrol/authorize/enrol_form.php');
77 $frmenrol = new enrol_authorize_form('enrol.php', compact('course'));
78 if ($frmenrol->get_data()) {
79 $authorizeerror = '';
80 switch ($form->paymentmethod) {
81 case AN_METHOD_CC:
82 $authorizeerror = $this->cc_submit($form, $course);
83 break;
85 case AN_METHOD_ECHECK:
86 $authorizeerror = $this->echeck_submit($form, $course);
87 break;
89 if (!empty($authorizeerror)) {
90 error($authorizeerror);
93 $frmenrol->display();
95 print_simple_box_end();
97 if ($course->password) {
98 $password = '';
99 $teacher = get_teacher($course->id);
100 include($CFG->dirroot.'/enrol/manual/enrol.html');
103 print_footer();
108 * Validates registration forms and enrols student to course.
110 * @param object $form Form parameters
111 * @param object $course Course info
112 * @access public
114 function check_entry($form, $course)
116 global $CFG;
118 if (zero_cost($course) || (!empty($course->password) && !empty($form->enrol) && $form->enrol == 'manual')) {
119 $manual = enrolment_factory::factory('manual');
120 $manual->check_entry($form, $course);
121 if (!empty($manual->errormsg)) {
122 $this->errormsg = $manual->errormsg;
130 * The user submitted credit card form.
132 * @param object $form Form parameters
133 * @param object $course Course info
134 * @access private
136 function cc_submit($form, $course)
138 global $CFG, $USER, $SESSION;
139 require_once('authorizenetlib.php');
141 prevent_double_paid($course);
143 $useripno = getremoteaddr();
144 $curcost = get_course_cost($course);
145 $exp_date = sprintf("%02d", $form->ccexpiremm) . $form->ccexpireyyyy;
147 // NEW CC ORDER
148 $timenow = time();
149 $order = new stdClass();
150 $order->paymentmethod = AN_METHOD_CC;
151 $order->refundinfo = substr($form->cc, -4);
152 $order->ccname = $form->firstname . " " . $form->lastname;
153 $order->courseid = $course->id;
154 $order->userid = $USER->id;
155 $order->status = AN_STATUS_NONE; // it will be changed...
156 $order->settletime = 0; // cron changes this.
157 $order->transid = 0; // Transaction Id
158 $order->timecreated = $timenow;
159 $order->amount = $curcost['cost'];
160 $order->currency = $curcost['currency'];
161 $order->id = insert_record("enrol_authorize", $order);
162 if (!$order->id) {
163 email_to_admin("Error while trying to insert new data", $order);
164 return "Insert record error. Admin has been notified!";
167 $extra = new stdClass();
168 $extra->x_card_num = $form->cc;
169 $extra->x_card_code = $form->cvv;
170 $extra->x_exp_date = $exp_date;
171 $extra->x_currency_code = $curcost['currency'];
172 $extra->x_amount = $curcost['cost'];
173 $extra->x_first_name = $form->firstname;
174 $extra->x_last_name = $form->lastname;
175 $extra->x_country = $form->cccountry;
176 $extra->x_address = $form->ccaddress;
177 $extra->x_state = $form->ccstate;
178 $extra->x_city = $form->cccity;
179 $extra->x_zip = $form->cczip;
181 $extra->x_invoice_num = $order->id;
182 $extra->x_description = $course->shortname;
184 $extra->x_cust_id = $USER->id;
185 $extra->x_email = $USER->email;
186 $extra->x_customer_ip = $useripno;
187 $extra->x_email_customer = empty($CFG->enrol_mailstudents) ? 'FALSE' : 'TRUE';
188 $extra->x_phone = '';
189 $extra->x_fax = '';
191 $revieworder = false;
192 $action = AN_ACTION_AUTH_CAPTURE;
194 if (!empty($CFG->an_authcode) && !empty($form->ccauthcode)) {
195 $action = AN_ACTION_CAPTURE_ONLY;
196 $extra->x_auth_code = $form->ccauthcode;
198 elseif (!empty($CFG->an_review)) {
199 $revieworder = true;
200 $action = AN_ACTION_AUTH_ONLY;
203 $message = '';
204 if (AN_APPROVED != authorize_action($order, $message, $extra, $action, $form->cctype)) {
205 email_to_admin($message, $order);
206 return $message;
209 $SESSION->ccpaid = 1; // security check: don't duplicate payment
210 if ($order->transid == 0) { // TEST MODE
211 if ($revieworder) {
212 redirect($CFG->wwwroot, get_string("reviewnotify", "enrol_authorize"), '30');
214 else {
215 enrol_into_course($course, $USER, 'manual');
216 redirect("$CFG->wwwroot/course/view.php?id=$course->id");
218 return;
221 if ($revieworder) { // review enabled, inform payment managers and redirect the user who have paid to main page.
222 $a = new stdClass;
223 $a->url = "$CFG->wwwroot/enrol/authorize/index.php?order=$order->id";
224 $a->orderid = $order->id;
225 $a->transid = $order->transid;
226 $a->amount = "$order->currency $order->amount";
227 $a->expireon = userdate(authorize_getsettletime($timenow + (30 * 3600 * 24)));
228 $a->captureon = userdate(authorize_getsettletime($timenow + (intval($CFG->an_capture_day) * 3600 * 24)));
229 $a->course = $course->fullname;
230 $a->user = fullname($USER);
231 $a->acstatus = ($CFG->an_capture_day > 0) ? get_string('yes') : get_string('no');
232 $emailmessage = get_string('adminneworder', 'enrol_authorize', $a);
233 $a = new stdClass;
234 $a->course = $course->shortname;
235 $a->orderid = $order->id;
236 $emailsubject = get_string('adminnewordersubject', 'enrol_authorize', $a);
237 $context = get_context_instance(CONTEXT_COURSE, $course->id);
238 if ($paymentmanagers = get_users_by_capability($context, 'enrol/authorize:managepayments')) {
239 foreach ($paymentmanagers as $paymentmanager) {
240 email_to_user($paymentmanager, $USER, $emailsubject, $emailmessage);
243 redirect($CFG->wwwroot, get_string("reviewnotify", "enrol_authorize"), '30');
244 return;
247 // Credit card captured, ENROL student now...
248 if (enrol_into_course($course, $USER, 'manual')) {
249 if (!empty($CFG->enrol_mailstudents)) {
250 send_welcome_messages($order->id);
252 if (!empty($CFG->enrol_mailteachers)) {
253 $context = get_context_instance(CONTEXT_COURSE, $course->id);
254 $paymentmanagers = get_users_by_capability($context, 'enrol/authorize:managepayments', '', '', '0', '1');
255 $paymentmanager = array_shift($paymentmanagers);
256 $a = new stdClass;
257 $a->course = "$course->fullname";
258 $a->user = fullname($USER);
259 email_to_user($paymentmanager,
260 $USER,
261 get_string("enrolmentnew", '', format_string($course->shortname)),
262 get_string('enrolmentnewuser', '', $a));
264 if (!empty($CFG->enrol_mailadmins)) {
265 $a = new stdClass;
266 $a->course = "$course->fullname";
267 $a->user = fullname($USER);
268 $admins = get_admins();
269 foreach ($admins as $admin) {
270 email_to_user($admin,
271 $USER,
272 get_string("enrolmentnew", '', format_string($course->shortname)),
273 get_string('enrolmentnewuser', '', $a));
276 } else {
277 email_to_admin("Error while trying to enrol " . fullname($USER) . " in '$course->fullname'", $order);
280 if ($SESSION->wantsurl) {
281 $destination = $SESSION->wantsurl; unset($SESSION->wantsurl);
282 } else {
283 $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
285 load_all_capabilities();
286 redirect($destination, get_string('paymentthanks', 'moodle', $course->fullname), 10);
291 * The user submitted echeck form.
293 * @param object $form Form parameters
294 * @param object $course Course info
295 * @access private
297 function echeck_submit($form, $course)
299 global $CFG, $USER, $SESSION;
300 require_once('authorizenetlib.php');
302 prevent_double_paid($course);
304 $useripno = getremoteaddr();
305 $curcost = get_course_cost($course);
306 $isbusinesschecking = ($form->acctype == 'BUSINESSCHECKING');
308 // NEW ECHECK ORDER
309 $timenow = time();
310 $order = new stdClass();
311 $order->paymentmethod = AN_METHOD_ECHECK;
312 $order->refundinfo = $isbusinesschecking ? 1 : 0;
313 $order->ccname = $form->firstname . ' ' . $form->lastname;
314 $order->courseid = $course->id;
315 $order->userid = $USER->id;
316 $order->status = AN_STATUS_NONE; // it will be changed...
317 $order->settletime = 0; // cron changes this.
318 $order->transid = 0; // Transaction Id
319 $order->timecreated = $timenow;
320 $order->amount = $curcost['cost'];
321 $order->currency = $curcost['currency'];
322 $order->id = insert_record("enrol_authorize", $order);
323 if (!$order->id) {
324 email_to_admin("Error while trying to insert new data", $order);
325 return "Insert record error. Admin has been notified!";
328 $extra = new stdClass();
329 $extra->x_bank_aba_code = $form->abacode;
330 $extra->x_bank_acct_num = $form->accnum;
331 $extra->x_bank_acct_type = $form->acctype;
332 $extra->x_echeck_type = $isbusinesschecking ? 'CCD' : 'WEB';
333 $extra->x_bank_name = $form->bankname;
334 $extra->x_currency_code = $curcost['currency'];
335 $extra->x_amount = $curcost['cost'];
336 $extra->x_first_name = $form->firstname;
337 $extra->x_last_name = $form->lastname;
338 $extra->x_country = $USER->country;
339 $extra->x_address = $USER->address;
340 $extra->x_city = $USER->city;
341 $extra->x_state = '';
342 $extra->x_zip = '';
344 $extra->x_invoice_num = $order->id;
345 $extra->x_description = $course->shortname;
347 $extra->x_cust_id = $USER->id;
348 $extra->x_email = $USER->email;
349 $extra->x_customer_ip = $useripno;
350 $extra->x_email_customer = empty($CFG->enrol_mailstudents) ? 'FALSE' : 'TRUE';
351 $extra->x_phone = '';
352 $extra->x_fax = '';
354 $message = '';
355 if (AN_REVIEW != authorize_action($order, $message, $extra, AN_ACTION_AUTH_CAPTURE)) {
356 email_to_admin($message, $order);
357 return $message;
360 $SESSION->ccpaid = 1; // security check: don't duplicate payment
361 redirect($CFG->wwwroot, get_string("reviewnotify", "enrol_authorize"), '30');
366 * Gets access icons.
368 * @param object $course
369 * @return string
370 * @access public
372 function get_access_icons($course) {
374 $manual = enrolment_factory::factory('manual');
375 $str = $manual->get_access_icons($course);
376 $curcost = get_course_cost($course);
378 if (abs($curcost['cost']) > 0.00) {
379 $strrequirespayment = get_string("requirespayment");
380 $strcost = get_string("cost");
381 $currency = $curcost['currency'];
383 switch ($currency) {
384 case 'USD': $currency = 'US$'; break;
385 case 'CAD': $currency = 'C$'; break;
386 case 'EUR': $currency = '&euro;'; break;
387 case 'GBP': $currency = '&pound;'; break;
388 case 'JPY': $currency = '&yen;'; break;
391 $str .= '<div class="cost" title="'.$strrequirespayment.'">'.$strcost.': ';
392 $str .= $currency . ' ' . $curcost['cost'].'</div>';
395 return $str;
400 * Shows config form & errors
402 * @param object $frm
403 * @access public
405 function config_form($frm)
407 global $CFG;
408 $mconfig = get_config('enrol/authorize');
410 if (! check_openssl_loaded()) {
411 notify('PHP must be compiled with SSL support (--with-openssl)');
414 if (empty($CFG->loginhttps) and substr($CFG->wwwroot, 0, 5) !== 'https') {
415 $a = new stdClass;
416 $a->url = "$CFG->wwwroot/$CFG->admin/settings.php?section=httpsecurity";
417 notify(get_string('adminconfighttps', 'enrol_authorize', $a));
418 return; // notice breaks the form and xhtml later
420 elseif (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 443) { // MDL-9836
421 $wwwsroot = qualified_me();
422 $wwwsroot = str_replace('http:', 'https:', $wwwsroot);
423 $a = new stdClass;
424 $a->url = $wwwsroot;
425 notify(get_string('adminconfighttpsgo', 'enrol_authorize', $a));
426 return; // notice breaks the form and xhtml later
429 if (!empty($frm->an_review)) {
430 $captureday = intval($frm->an_capture_day);
431 $emailexpired = intval($frm->an_emailexpired);
432 if ($captureday > 0 || $emailexpired > 0) {
433 if ((time() - intval($mconfig->an_lastcron) > 3600 * 24)) {
434 notify(get_string('admincronsetup', 'enrol_authorize'));
439 if ($count = count_records('enrol_authorize', 'status', AN_STATUS_AUTH)) {
440 $a = new stdClass;
441 $a->count = $count;
442 $a->url = $CFG->wwwroot."/enrol/authorize/index.php?status=".AN_STATUS_AUTH;
443 notify(get_string('adminpendingorders', 'enrol_authorize', $a));
446 if (data_submitted()) {
447 if (empty($mconfig->an_login)) {
448 notify("an_login required");
450 if (empty($mconfig->an_tran_key) && empty($mconfig->an_password)) {
451 notify("an_tran_key or an_password required");
455 include($CFG->dirroot.'/enrol/authorize/config_form.php');
460 * process_config
462 * @param object $config
463 * @return bool true if it will be saved.
464 * @access public
466 function process_config($config)
468 global $CFG;
469 $mconfig = get_config('enrol/authorize');
471 // site settings
472 if (($cost = optional_param('enrol_cost', 5, PARAM_INT)) > 0) {
473 set_config('enrol_cost', $cost);
475 set_config('enrol_currency', optional_param('enrol_currency', 'USD', PARAM_ALPHA));
476 set_config('enrol_mailstudents', optional_param('enrol_mailstudents', 0, PARAM_BOOL));
477 set_config('enrol_mailteachers', optional_param('enrol_mailteachers', 0, PARAM_BOOL));
478 set_config('enrol_mailadmins', optional_param('enrol_mailadmins', 0, PARAM_BOOL));
480 // optional authorize.net settings
481 set_config('an_avs', optional_param('an_avs', 0, PARAM_BOOL));
482 set_config('an_authcode', optional_param('an_authcode', 0, PARAM_BOOL));
483 set_config('an_test', optional_param('an_test', 0, PARAM_BOOL));
484 set_config('an_referer', optional_param('an_referer', 'http://', PARAM_URL));
486 $acceptmethods = optional_param('acceptmethods', get_list_of_payment_methods(), PARAM_ALPHA);
487 set_config('an_acceptmethods', implode(',', $acceptmethods));
488 $acceptccs = optional_param('acceptccs', array_keys(get_list_of_creditcards()), PARAM_ALPHA);
489 set_config('an_acceptccs', implode(',', $acceptccs));
490 $acceptechecktypes = optional_param('acceptechecktypes', get_list_of_bank_account_types(), PARAM_ALPHA);
491 set_config('an_acceptechecktypes', implode(',', $acceptechecktypes));
493 $cutoff_hour = optional_param('an_cutoff_hour', 0, PARAM_INT);
494 $cutoff_min = optional_param('an_cutoff_min', 5, PARAM_INT);
495 set_config('an_cutoff', $cutoff_hour * 60 + $cutoff_min);
497 // cron depencies
498 $reviewval = optional_param('an_review', 0, PARAM_BOOL);
499 $captureday = optional_param('an_capture_day', 5, PARAM_INT);
500 $emailexpired = optional_param('an_emailexpired', 2, PARAM_INT);
501 $emailexpiredteacher = optional_param('an_emailexpiredteacher', 0, PARAM_BOOL);
502 $sorttype = optional_param('an_sorttype', 'ttl', PARAM_ALPHA);
504 $captureday = ($captureday > 29) ? 29 : (($captureday < 0) ? 0 : $captureday);
505 $emailexpired = ($emailexpired > 5) ? 5 : (($emailexpired < 0) ? 0 : $emailexpired);
507 if (!empty($reviewval) && ($captureday > 0 || $emailexpired > 0)) {
508 if (time() - intval($mconfig->an_lastcron) > 3600 * 24) {
509 return false;
513 set_config('an_review', $reviewval);
514 set_config('an_capture_day', $captureday);
515 set_config('an_emailexpired', $emailexpired);
516 set_config('an_emailexpiredteacher', $emailexpiredteacher);
517 set_config('an_sorttype', $sorttype);
519 // https and openssl library is required
520 if ((substr($CFG->wwwroot, 0, 5) !== 'https' and empty($CFG->loginhttps)) or
521 !check_openssl_loaded()) {
522 return false;
525 // REQUIRED fields;
526 // an_login
527 $loginval = optional_param('an_login', '');
528 if (empty($loginval) && empty($mconfig->an_login)) {
529 return false;
531 $loginval = !empty($loginval) ? rc4encrypt($loginval) : strval($mconfig->an_login);
532 set_config('an_login', $loginval, 'enrol/authorize');
534 // an_tran_key, an_password
535 $tranval = optional_param('an_tran_key', '');
536 $tranval = !empty($tranval) ? rc4encrypt($tranval) : (isset($mconfig->an_tran_key)?$mconfig->an_tran_key:'');
537 $passwordval = optional_param('an_password', '');
538 $passwordval = !empty($passwordval) ? rc4encrypt($passwordval) :(isset($mconfig->an_password)?$mconfig->an_password:'');
539 $deletecurrent = optional_param('delete_current', '0', PARAM_BOOL);
540 if (!empty($deletecurrent) and !empty($tranval)) {
541 unset_config('an_password', 'enrol/authorize');
542 $passwordval = '';
544 elseif (!empty($passwordval)) {
545 set_config('an_password', $passwordval, 'enrol/authorize');
547 if (empty($tranval) and empty($passwordval)) {
548 return false;
550 if (!empty($tranval)) {
551 set_config('an_tran_key', $tranval, 'enrol/authorize');
554 return true;
558 * This function is run by admin/cron.php every time if admin has enabled this plugin.
560 * Everyday at settlement time (default is 00:05), it cleans up some tables
561 * and sends email to admin/teachers about pending orders expiring if manual-capture has enabled.
563 * If admin set up 'Order review' and 'Capture day', it captures credits cards and enrols students.
565 * @access public
567 function cron()
569 global $CFG;
570 require_once($CFG->dirroot.'/enrol/authorize/authorizenetlib.php');
572 $oneday = 86400;
573 $timenow = time();
574 $settlementtime = authorize_getsettletime($timenow);
575 $timediff30 = $settlementtime - (30 * $oneday);
576 $mconfig = get_config('enrol/authorize');
577 set_config('an_lastcron', $timenow, 'enrol/authorize');
579 mtrace("Processing authorize cron...");
581 if (intval($mconfig->an_dailysettlement) < $settlementtime) {
582 set_config('an_dailysettlement', $settlementtime, 'enrol/authorize');
583 mtrace(" daily cron; some cleanups and sending email to admins the count of pending orders expiring", ": ");
584 $this->cron_daily();
585 mtrace("done");
588 mtrace(" scheduled capture", ": ");
589 if (empty($CFG->an_review) or
590 (!empty($CFG->an_test)) or
591 (intval($CFG->an_capture_day) < 1) or
592 (!check_openssl_loaded())) {
593 mtrace("disabled");
594 return; // order review disabled or test mode or manual capture or openssl wasn't loaded.
597 $timediffcnf = $settlementtime - (intval($CFG->an_capture_day) * $oneday);
598 $sql = "SELECT * FROM {$CFG->prefix}enrol_authorize
599 WHERE (status = '" .AN_STATUS_AUTH. "')
600 AND (timecreated < '$timediffcnf')
601 AND (timecreated > '$timediff30')
602 ORDER BY courseid";
604 if (!$orders = get_records_sql($sql)) {
605 mtrace("no pending orders");
606 return;
609 $eachconn = intval($mconfig->an_eachconnsecs);
610 if (empty($eachconn)) $eachconn = 3;
611 elseif ($eachconn > 60) $eachconn = 60;
613 $ordercount = count((array)$orders);
614 if (($ordercount * $eachconn) + intval($mconfig->an_lastcron) > $timenow) {
615 mtrace("blocked");
616 return;
619 mtrace(" $ordercount orders are being processed now", ": ");
621 $faults = '';
622 $sendem = array();
623 $elapsed = time();
624 @set_time_limit(0);
625 $this->log = "AUTHORIZE.NET AUTOCAPTURE CRON: " . userdate($timenow) . "\n";
627 $lastcourseid = 0;
628 foreach ($orders as $order) {
629 $message = '';
630 $extra = NULL;
631 if (AN_APPROVED == authorize_action($order, $message, $extra, AN_ACTION_PRIOR_AUTH_CAPTURE)) {
632 if ($lastcourseid != $order->courseid) {
633 $lastcourseid = $order->courseid;
634 $course = get_record('course', 'id', $lastcourseid);
635 $role = get_default_course_role($course);
636 $context = get_context_instance(CONTEXT_COURSE, $lastcourseid);
638 $timestart = $timeend = 0;
639 if ($course->enrolperiod) {
640 $timestart = $timenow;
641 $timeend = $order->settletime + $course->enrolperiod;
643 $user = get_record('user', 'id', $order->userid);
644 if (role_assign($role->id, $user->id, 0, $context->id, $timestart, $timeend, 0, 'manual')) {
645 $this->log .= "User($user->id) has been enrolled to course($course->id).\n";
646 if (!empty($CFG->enrol_mailstudents)) {
647 $sendem[] = $order->id;
650 else {
651 $faults .= "Error while trying to enrol ".fullname($user)." in '$course->fullname' \n";
652 foreach ($order as $okey => $ovalue) {
653 $faults .= " $okey = $ovalue\n";
657 else {
658 $this->log .= "Error, Order# $order->id: " . $message . "\n";
662 mtrace("processed");
664 $timenow = time();
665 $elapsed = $timenow - $elapsed;
666 $eachconn = ceil($elapsed / $ordercount);
667 set_config('an_eachconnsecs', $eachconn, 'enrol/authorize');
669 $this->log .= "AUTHORIZE.NET CRON FINISHED: " . userdate($timenow);
671 $adminuser = get_admin();
672 if (!empty($faults)) {
673 email_to_user($adminuser, $adminuser, "AUTHORIZE.NET CRON FAULTS", $faults);
675 if (!empty($CFG->enrol_mailadmins)) {
676 email_to_user($adminuser, $adminuser, "AUTHORIZE.NET CRON LOG", $this->log);
679 // Send emails to students about which courses have enrolled.
680 if (!empty($sendem)) {
681 mtrace(" sending welcome messages to students", ": ");
682 send_welcome_messages($sendem);
683 mtrace("sent");
688 * Daily cron. It executes at settlement time (default is 00:05).
690 * @access private
692 function cron_daily()
694 global $CFG, $SITE;
695 require_once($CFG->dirroot.'/enrol/authorize/authorizenetlib.php');
697 $oneday = 86400;
698 $timenow = time();
699 $onepass = $timenow - $oneday;
700 $settlementtime = authorize_getsettletime($timenow);
701 $timediff30 = $settlementtime - (30 * $oneday);
703 // Delete orders that no transaction was made.
704 $select = "(status='".AN_STATUS_NONE."') AND (timecreated<'$timediff30')";
705 delete_records_select('enrol_authorize', $select);
707 // Pending orders are expired with in 30 days.
708 $select = "(status='".AN_STATUS_AUTH."') AND (timecreated<'$timediff30')";
709 execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET status='".AN_STATUS_EXPIRE."' WHERE $select", false);
711 // Delete expired orders 60 days later.
712 $timediff60 = $settlementtime - (60 * $oneday);
713 $select = "(status='".AN_STATUS_EXPIRE."') AND (timecreated<'$timediff60')";
714 delete_records_select('enrol_authorize', $select);
716 // XXX TODO SEND EMAIL to 'enrol/authorize:uploadcsv'
717 // get_users_by_capability() does not handling user level resolving
718 // After user resolving, get_admin() to get_users_by_capability()
719 $adminuser = get_admin();
720 $select = "status IN(".AN_STATUS_UNDERREVIEW.",".AN_STATUS_APPROVEDREVIEW.") " .
721 "AND (timecreated<'$onepass') AND (timecreated>'$timediff60')";
722 $count = count_records_select('enrol_authorize', $select);
723 if ($count) {
724 $a = new stdClass;
725 $a->count = $count;
726 $a->course = $SITE->shortname;
727 $subject = get_string('pendingechecksubject', 'enrol_authorize', $a);
728 $a = new stdClass;
729 $a->count = $count;
730 $a->url = $CFG->wwwroot.'/enrol/authorize/uploadcsv.php';
731 $message = get_string('pendingecheckemail', 'enrol_authorize', $a);
732 @email_to_user($adminuser, $adminuser, $subject, $message);
735 // Daily warning email for pending orders expiring.
736 if (empty($CFG->an_emailexpired)) {
737 return; // not enabled
740 // Pending orders count will be expired.
741 $timediffem = $settlementtime - ((30 - intval($CFG->an_emailexpired)) * $oneday);
742 $select = "(status='". AN_STATUS_AUTH ."') AND (timecreated<'$timediffem') AND (timecreated>'$timediff30')";
743 $count = count_records_select('enrol_authorize', $select);
744 if (!$count) {
745 return;
748 // Email to admin
749 $a = new stdClass;
750 $a->pending = $count;
751 $a->days = $CFG->an_emailexpired;
752 $a->course = $SITE->shortname;
753 $subject = get_string('pendingorderssubject', 'enrol_authorize', $a);
754 $a = new stdClass;
755 $a->pending = $count;
756 $a->days = $CFG->an_emailexpired;
757 $a->course = $SITE->fullname;
758 $a->enrolurl = "$CFG->wwwroot/$CFG->admin/enrol_config.php?enrol=authorize";
759 $a->url = $CFG->wwwroot.'/enrol/authorize/index.php?status='.AN_STATUS_AUTH;
760 $message = get_string('pendingordersemail', 'enrol_authorize', $a);
761 email_to_user($adminuser, $adminuser, $subject, $message);
763 // Email to teachers
764 if (empty($CFG->an_emailexpiredteacher)) {
765 return; // email feature disabled for teachers.
768 $sorttype = empty($CFG->an_sorttype) ? 'ttl' : $CFG->an_sorttype;
769 $sql = "SELECT e.courseid, e.currency, c.fullname, c.shortname,
770 COUNT(e.courseid) AS cnt, SUM(e.amount) as ttl
771 FROM {$CFG->prefix}enrol_authorize e
772 INNER JOIN {$CFG->prefix}course c ON c.id = e.courseid
773 WHERE (e.status = ". AN_STATUS_AUTH .")
774 AND (e.timecreated < $timediffem)
775 AND (e.timecreated > $timediff30)
776 GROUP BY e.courseid
777 ORDER BY $sorttype DESC";
779 $courseinfos = get_records_sql($sql);
780 foreach($courseinfos as $courseinfo) {
781 $lastcourse = $courseinfo->courseid;
782 $context = get_context_instance(CONTEXT_COURSE, $lastcourse);
783 if ($paymentmanagers = get_users_by_capability($context, 'enrol/authorize:managepayments')) {
784 $a = new stdClass;
785 $a->course = $courseinfo->shortname;
786 $a->pending = $courseinfo->cnt;
787 $a->days = $CFG->an_emailexpired;
788 $subject = get_string('pendingorderssubject', 'enrol_authorize', $a);
789 $a = new stdClass;
790 $a->course = $courseinfo->fullname;
791 $a->pending = $courseinfo->cnt;
792 $a->currency = $courseinfo->currency;
793 $a->sumcost = $courseinfo->ttl;
794 $a->days = $CFG->an_emailexpired;
795 $a->url = $CFG->wwwroot.'/enrol/authorize/index.php?course='.$lastcourse.'&amp;status='.AN_STATUS_AUTH;
796 $message = get_string('pendingordersemailteacher', 'enrol_authorize', $a);
797 foreach ($paymentmanagers as $paymentmanager) {
798 email_to_user($paymentmanager, $adminuser, $subject, $message);