Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Daily cron seperated from main cron for performance.

To do:
Performance for welcome message sending to students. Group by student and courses. Like this:
Dear student, welcome to these courses:
1- Coursename 1
2- Coursename 2
3- Coursename 3
  • Loading branch information...
commit 27b1d9d58112e6be52d3d00187b5dfd2ffecc55e 1 parent cdc03b2
ethem authored
Showing with 102 additions and 65 deletions.
  1. +102 −65 enrol/authorize/enrol.php
View
167 enrol/authorize/enrol.php
@@ -683,84 +683,27 @@ function check_openssl_loaded() {
*/
function cron()
{
- global $CFG, $SITE;
+ global $CFG;
require_once($CFG->dirroot.'/enrol/authorize/authorizenetlib.php');
$oneday = 86400;
$timenow = time();
$settlementtime = getsettletime($timenow);
$timediff30 = $settlementtime - (30 * $oneday);
+
$mconfig = get_config('enrol/authorize');
set_config('an_lastcron', $timenow, 'enrol/authorize');
if (intval($mconfig->an_dailysettlement) < $settlementtime) {
set_config('an_dailysettlement', $settlementtime, 'enrol/authorize');
- // Some clean-up and update
- $select = "(status='".AN_STATUS_NONE."') AND (timecreated<'$timediff30')";
- delete_records_select('enrol_authorize', $select);
- $select = "(status='".AN_STATUS_AUTH."') AND (timecreated<'$timediff30')";
- execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET status='".AN_STATUS_EXPIRE."' WHERE $select", false);
- $timediff60 = $settlementtime - (60 * $oneday);
- $select = "(status='".AN_STATUS_EXPIRE."') AND (timecreated<'$timediff60')";
- delete_records_select('enrol_authorize', $select);
- // Daily warning email for expiring pending orders.
- if (!empty($CFG->an_emailexpired)) {
- $timediffem = $settlementtime - ((30 - intval($CFG->an_emailexpired)) * $oneday);
- $select = "(status='". AN_STATUS_AUTH ."') AND (timecreated<'$timediffem') AND (timecreated>'$timediff30')";
- if ($count = count_records_select('enrol_authorize', $select)) {
- $a = new stdClass;
- $a->pending = $count;
- $a->days = $CFG->an_emailexpired;
- $a->course = $SITE->shortname;
- $subject = get_string('pendingorderssubject', 'enrol_authorize', $a);
- $a = new stdClass;
- $a->pending = $count;
- $a->days = $CFG->an_emailexpired;
- $a->course = $SITE->fullname;
- $a->enrolurl = "$CFG->wwwroot/$CFG->admin/users.php";
- $a->url = $CFG->wwwroot."/enrol/authorize/index.php?status=".AN_STATUS_AUTH;
- $message = get_string('pendingordersemail', 'enrol_authorize', $a);
- $adminuser = get_admin();
- email_to_user($adminuser, $adminuser, $subject, $message);
- if (!empty($CFG->an_teachermanagepay) and !empty($CFG->an_emailexpiredteacher)) {
- $sorttype = empty($CFG->an_sorttype) ? 'ttl' : $CFG->an_sorttype;
- $sql = "SELECT E.courseid, E.currency, C.fullname, C.shortname, " .
- "COUNT(E.courseid) AS cnt, SUM(E.amount) as ttl " .
- "FROM {$CFG->prefix}enrol_authorize E " .
- "INNER JOIN {$CFG->prefix}course C ON C.id = E.courseid " .
- "WHERE $select GROUP BY E.courseid ORDER BY $sorttype DESC";
- $message = ''; $subject = ''; $lastcourse = 0;
- $coursesandcounts = get_records_sql($sql);
- foreach($coursesandcounts as $courseandcount) {
- $lastcourse = $courseandcount->courseid;
- if ($teachers = get_course_teachers($lastcourse)) {
- $a = new stdClass;
- $a->course = $courseandcount->shortname;
- $a->pending = $courseandcount->cnt;
- $a->days = $CFG->an_emailexpired;
- $subject = get_string('pendingorderssubject', 'enrol_authorize', $a);
- $a = new stdClass;
- $a->course = $courseandcount->fullname;
- $a->pending = $courseandcount->cnt;
- $a->currency = $courseandcount->currency;
- $a->sumcost = $courseandcount->ttl;
- $a->days = $CFG->an_emailexpired;
- $a->url = $CFG->wwwroot.'/enrol/authorize/index.php?course='.
- $lastcourse.'&amp;status='.AN_STATUS_AUTH;
- $message = get_string('pendingordersemailteacher', 'enrol_authorize', $a);
- foreach ($teachers as $teacher) {
- email_to_user($teacher, $adminuser, $subject, $message);
- }
- }
- }
- }
- }
- }
+ $this->cron_daily();
}
- if (empty($CFG->an_review) || (!empty($CFG->an_test)) ||
- (intval($CFG->an_capture_day) < 1) || (!$this->check_openssl_loaded())) {
- return;
+ if (empty($CFG->an_review) or
+ (!empty($CFG->an_test)) or
+ (intval($CFG->an_capture_day) < 1) or
+ (!$this->check_openssl_loaded())) {
+ return; // order review disabled or test mode or manual capture or openssl wasn't loaded.
}
$timediffcnf = $settlementtime - (intval($CFG->an_capture_day) * $oneday);
@@ -866,5 +809,99 @@ function cron()
get_string('welcometocoursetext', '', $a));
}
}
+
+ /**
+ * Daily cron. It executes at settlement time (default is 00:05).
+ *
+ * @access private
+ */
+ function cron_daily()
+ {
+ global $CFG, $SITE;
+ require_once($CFG->dirroot.'/enrol/authorize/authorizenetlib.php');
+
+ $oneday = 86400;
+ $timenow = time();
+ $settlementtime = getsettletime($timenow);
+ $timediff30 = $settlementtime - (30 * $oneday);
+
+ // Delete orders that no transaction was made.
+ $select = "(status='".AN_STATUS_NONE."') AND (timecreated<'$timediff30')";
+ delete_records_select('enrol_authorize', $select);
+
+ // Pending orders are expired with in 30 days.
+ $select = "(status='".AN_STATUS_AUTH."') AND (timecreated<'$timediff30')";
+ execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET status='".AN_STATUS_EXPIRE."' WHERE $select", false);
+
+ // Delete expired orders 60 days later.
+ $timediff60 = $settlementtime - (60 * $oneday);
+ $select = "(status='".AN_STATUS_EXPIRE."') AND (timecreated<'$timediff60')";
+ delete_records_select('enrol_authorize', $select);
+
+ // Daily warning email for pending orders expiring.
+ if (empty($CFG->an_emailexpired)) {
+ return; // not enabled
+ }
+
+ // Pending orders count will be expired.
+ $timediffem = $settlementtime - ((30 - intval($CFG->an_emailexpired)) * $oneday);
+ $select = "(status='". AN_STATUS_AUTH ."') AND (timecreated<'$timediffem') AND (timecreated>'$timediff30')";
+ $count = count_records_select('enrol_authorize', $select);
+ if (!$count) {
+ return;
+ }
+
+ // Email to admin
+ $a = new stdClass;
+ $a->pending = $count;
+ $a->days = $CFG->an_emailexpired;
+ $a->course = $SITE->shortname;
+ $subject = get_string('pendingorderssubject', 'enrol_authorize', $a);
+ $a = new stdClass;
+ $a->pending = $count;
+ $a->days = $CFG->an_emailexpired;
+ $a->course = $SITE->fullname;
+ $a->enrolurl = "$CFG->wwwroot/$CFG->admin/users.php";
+ $a->url = $CFG->wwwroot.'/enrol/authorize/index.php?status='.AN_STATUS_AUTH;
+ $message = get_string('pendingordersemail', 'enrol_authorize', $a);
+ $adminuser = get_admin();
+ email_to_user($adminuser, $adminuser, $subject, $message);
+
+ // Email to teachers
+ if (empty($CFG->an_teachermanagepay) or empty($CFG->an_emailexpiredteacher)) {
+ return; // teachers can't manage payments or email feature disabled for teachers.
+ }
+
+ $sorttype = empty($CFG->an_sorttype) ? 'ttl' : $CFG->an_sorttype;
+ $sql = "SELECT E.courseid, E.currency, C.fullname, C.shortname, " .
+ "COUNT(E.courseid) AS cnt, SUM(E.amount) as ttl " .
+ "FROM {$CFG->prefix}enrol_authorize E " .
+ "INNER JOIN {$CFG->prefix}course C ON C.id = E.courseid " .
+ "WHERE $select GROUP BY E.courseid ORDER BY $sorttype DESC";
+
+ $courseinfos = get_records_sql($sql);
+ foreach($courseinfos as $courseinfo) {
+ $lastcourse = $courseinfo->courseid;
+ if ($teachers = get_course_teachers($lastcourse)) {
+ $a = new stdClass;
+ $a->course = $courseinfo->shortname;
+ $a->pending = $courseinfo->cnt;
+ $a->days = $CFG->an_emailexpired;
+ $subject = get_string('pendingorderssubject', 'enrol_authorize', $a);
+ $a = new stdClass;
+ $a->course = $courseinfo->fullname;
+ $a->pending = $courseinfo->cnt;
+ $a->currency = $courseinfo->currency;
+ $a->sumcost = $courseinfo->ttl;
+ $a->days = $CFG->an_emailexpired;
+ $a->url = $CFG->wwwroot.'/enrol/authorize/index.php?course='.
+ $lastcourse.'&amp;status='.AN_STATUS_AUTH;
+ $message = get_string('pendingordersemailteacher', 'enrol_authorize', $a);
+ foreach ($teachers as $teacher) {
+ email_to_user($teacher, $adminuser, $subject, $message);
+ }
+ }
+ }
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.