From a771854c460d3c7aa099393323287eaa26b034df Mon Sep 17 00:00:00 2001
From: Troy Williams <troyw@waikato.ac.nz>
Date: Tue, 21 Jul 2015 14:21:10 +1200
Subject: [PATCH 1/3] MDL-50890 enrol_flatfile: Replace cron processing with
 scheduled task

Replace enrolment plugin flatfile cron processing with scheduled task
---
 .../classes/task/flatfile_sync_task.php       | 63 +++++++++++++++++++
 enrol/flatfile/cli/sync.php                   | 15 +++++
 enrol/flatfile/db/tasks.php                   | 37 +++++++++++
 enrol/flatfile/lang/en/enrol_flatfile.php     |  1 +
 enrol/flatfile/lib.php                        |  5 --
 enrol/flatfile/tests/flatfile_test.php        | 31 +++++++++
 enrol/flatfile/version.php                    |  3 +-
 7 files changed, 148 insertions(+), 7 deletions(-)
 create mode 100644 enrol/flatfile/classes/task/flatfile_sync_task.php
 create mode 100644 enrol/flatfile/db/tasks.php

diff --git a/enrol/flatfile/classes/task/flatfile_sync_task.php b/enrol/flatfile/classes/task/flatfile_sync_task.php
new file mode 100644
index 00000000000..9c2feda8b28
--- /dev/null
+++ b/enrol/flatfile/classes/task/flatfile_sync_task.php
@@ -0,0 +1,63 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Scheduled task for processing flatfile enrolments.
+ *
+ * @package    enrol_flatfile
+ * @copyright  2014 Troy Williams
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace enrol_flatfile\task;
+
+defined('MOODLE_INTERNAL') || die;
+
+/**
+ * Simple task to run sync enrolments.
+ */
+class flatfile_sync_task extends \core\task\scheduled_task {
+
+    /**
+     * Get a descriptive name for this task (shown to admins).
+     *
+     * @return string
+     */
+    public function get_name() {
+        return get_string('flatfilesync', 'enrol_flatfile');
+    }
+
+    /**
+     * Do the job.
+     * Throw exceptions on errors (the job will be retried).
+     */
+    public function execute() {
+        global $CFG;
+
+        require_once($CFG->dirroot . '/enrol/flatfile/lib.php');
+
+        if (!enrol_is_enabled('flatfile')) {
+            return 2;
+        }
+
+        // Instance of enrol_flatfile_plugin.
+        $plugin = enrol_get_plugin('flatfile');
+        $result = $plugin->sync(new \null_progress_trace());
+        return $result;
+
+    }
+
+}
diff --git a/enrol/flatfile/cli/sync.php b/enrol/flatfile/cli/sync.php
index 59df7e0dec9..7f5313a4f70 100644
--- a/enrol/flatfile/cli/sync.php
+++ b/enrol/flatfile/cli/sync.php
@@ -22,6 +22,21 @@
  *   - you need to change the "www-data" to match the apache user account
  *   - use "su" if "sudo" not available
  *
+ * Update
+ *
+ * This plugin now has a enrolment sync scheduled task. Scheduled tasks were
+ * introduced in Moodle 2.7.  It is possible to override the scheduled tasks
+ * configuration and run a single scheduled task immediately using the
+ * admin/tool/task/cli/schedule_task.php script. This is the recommended
+ * method to use for immediate enrollment synchronisation.
+ *
+ * Usage help:
+ * $ php admin/tool/task/cli/schedule_task.php -h
+ *
+ * Execute task:
+ * $ sudo -u www-data /usr/bin/php admin/tool/task/cli/scheduled_task.php /
+ * --execute=\\enrol_flatfile\\task\\flatfile_sync_task
+ *
  * @package    enrol_flatfile
  * @copyright  2012 Petr Skoda {@link http://skodak.org}
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
diff --git a/enrol/flatfile/db/tasks.php b/enrol/flatfile/db/tasks.php
new file mode 100644
index 00000000000..435bb1bcdf5
--- /dev/null
+++ b/enrol/flatfile/db/tasks.php
@@ -0,0 +1,37 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Definition of flatfile enrolment scheduled tasks.
+ *
+ * @package    enrol_flatfile
+ * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+$tasks = array(
+    array(
+        'classname' => '\enrol_flatfile\task\flatfile_sync_task',
+        'blocking' => 0,
+        'minute' => '*',
+        'hour' => '*',
+        'day' => '*',
+        'dayofweek' => '*',
+        'month' => '*'
+    )
+);
diff --git a/enrol/flatfile/lang/en/enrol_flatfile.php b/enrol/flatfile/lang/en/enrol_flatfile.php
index 7fd19012863..a1c99227d02 100644
--- a/enrol/flatfile/lang/en/enrol_flatfile.php
+++ b/enrol/flatfile/lang/en/enrol_flatfile.php
@@ -29,6 +29,7 @@ $string['filelockedmail'] = 'The text file you are using for file-based enrolmen
 $string['filelockedmailsubject'] = 'Important error: Enrolment file';
 $string['flatfile:manage'] = 'Manage user enrolments manually';
 $string['flatfile:unenrol'] = 'Unenrol users from the course manually';
+$string['flatfilesync'] = 'Flat file enrolment sync';
 $string['location'] = 'File location';
 $string['location_desc'] = 'Specify full path to the enrolment file. The file is automatically deleted after processing.';
 $string['notifyadmin'] = 'Notify administrator';
diff --git a/enrol/flatfile/lib.php b/enrol/flatfile/lib.php
index 41e0577a1a5..24b3cee5945 100644
--- a/enrol/flatfile/lib.php
+++ b/enrol/flatfile/lib.php
@@ -161,11 +161,6 @@ class enrol_flatfile_plugin extends enrol_plugin {
         }
     }
 
-    public function cron() {
-        $trace = new text_progress_trace();
-        $this->sync($trace);
-    }
-
     /**
      * Execute synchronisation.
      * @param progress_trace
diff --git a/enrol/flatfile/tests/flatfile_test.php b/enrol/flatfile/tests/flatfile_test.php
index 93ed0ef7845..0ab21c9fa11 100644
--- a/enrol/flatfile/tests/flatfile_test.php
+++ b/enrol/flatfile/tests/flatfile_test.php
@@ -467,4 +467,35 @@ class enrol_flatfile_testcase extends advanced_testcase {
         $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
         $this->assertEquals(0, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
     }
+
+    /**
+     * Flatfile enrolment sync task test.
+     */
+    public function test_flatfile_sync_task() {
+        global $CFG, $DB;
+        $this->resetAfterTest();
+
+        $flatfileplugin = enrol_get_plugin('flatfile');
+
+        $trace = new null_progress_trace();
+        $this->enable_plugin();
+        $file = "$CFG->dataroot/enrol.txt";
+        $flatfileplugin->set_config('location', $file);
+
+        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
+        $this->assertNotEmpty($studentrole);
+
+        $user1 = $this->getDataGenerator()->create_user(array('idnumber' => 'u1'));
+        $course1 = $this->getDataGenerator()->create_course(array('idnumber' => 'c1'));
+        $context1 = context_course::instance($course1->id);
+
+        $data =
+            "add,student,u1,c1";
+        file_put_contents($file, $data);
+
+        $task = new enrol_flatfile\task\flatfile_sync_task;
+        $task->execute();
+
+        $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid' => $studentrole->id)));
+    }
 }
diff --git a/enrol/flatfile/version.php b/enrol/flatfile/version.php
index efc367cde45..c1c873b32bd 100644
--- a/enrol/flatfile/version.php
+++ b/enrol/flatfile/version.php
@@ -25,7 +25,6 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2015051100;        // The current plugin version (Date: YYYYMMDDXX)
+$plugin->version   = 2015051101;        // The current plugin version (Date: YYYYMMDDXX)
 $plugin->requires  = 2015050500;        // Requires this Moodle version
 $plugin->component = 'enrol_flatfile';  // Full name of the plugin (used for diagnostics)
-$plugin->cron      = 60;

From fd1b39952757ccaa4277f9da573a7d40c2c663b3 Mon Sep 17 00:00:00 2001
From: Jun Pataleta <jun@moodle.com>
Date: Mon, 7 Sep 2015 16:24:02 +0800
Subject: [PATCH 2/3] MDL-50890 enrol_flatfile: Modifications for compliance
 with standards

Also changed the scheduled task to run every hour.
---
 enrol/flatfile/classes/task/flatfile_sync_task.php | 3 +++
 enrol/flatfile/db/tasks.php                        | 2 +-
 enrol/flatfile/version.php                         | 2 +-
 3 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/enrol/flatfile/classes/task/flatfile_sync_task.php b/enrol/flatfile/classes/task/flatfile_sync_task.php
index 9c2feda8b28..7306778449f 100644
--- a/enrol/flatfile/classes/task/flatfile_sync_task.php
+++ b/enrol/flatfile/classes/task/flatfile_sync_task.php
@@ -28,6 +28,9 @@ defined('MOODLE_INTERNAL') || die;
 
 /**
  * Simple task to run sync enrolments.
+ *
+ * @copyright  2014 Troy Williams
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 class flatfile_sync_task extends \core\task\scheduled_task {
 
diff --git a/enrol/flatfile/db/tasks.php b/enrol/flatfile/db/tasks.php
index 435bb1bcdf5..a78fd91bb56 100644
--- a/enrol/flatfile/db/tasks.php
+++ b/enrol/flatfile/db/tasks.php
@@ -28,7 +28,7 @@ $tasks = array(
     array(
         'classname' => '\enrol_flatfile\task\flatfile_sync_task',
         'blocking' => 0,
-        'minute' => '*',
+        'minute' => '15',
         'hour' => '*',
         'day' => '*',
         'dayofweek' => '*',
diff --git a/enrol/flatfile/version.php b/enrol/flatfile/version.php
index c1c873b32bd..e7e8b864fef 100644
--- a/enrol/flatfile/version.php
+++ b/enrol/flatfile/version.php
@@ -25,6 +25,6 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2015051101;        // The current plugin version (Date: YYYYMMDDXX)
+$plugin->version   = 2015090700;        // The current plugin version (Date: YYYYMMDDRR)
 $plugin->requires  = 2015050500;        // Requires this Moodle version
 $plugin->component = 'enrol_flatfile';  // Full name of the plugin (used for diagnostics)

From 6bc490e270c1f2e0a4b92cd35b9b61b883341bff Mon Sep 17 00:00:00 2001
From: David Monllao <davidm@moodle.com>
Date: Wed, 16 Sep 2015 09:07:47 +0800
Subject: [PATCH 3/3] MDL-50890 enrol_flatfile: Correcting typo and return

Would be safer to return nothing, execute methods don't have
a return. I understand this return 2 was inherited from the
previous cron function.
---
 enrol/flatfile/classes/task/flatfile_sync_task.php | 2 +-
 enrol/flatfile/cli/sync.php                        | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/enrol/flatfile/classes/task/flatfile_sync_task.php b/enrol/flatfile/classes/task/flatfile_sync_task.php
index 7306778449f..a34da882a21 100644
--- a/enrol/flatfile/classes/task/flatfile_sync_task.php
+++ b/enrol/flatfile/classes/task/flatfile_sync_task.php
@@ -53,7 +53,7 @@ class flatfile_sync_task extends \core\task\scheduled_task {
         require_once($CFG->dirroot . '/enrol/flatfile/lib.php');
 
         if (!enrol_is_enabled('flatfile')) {
-            return 2;
+            return;
         }
 
         // Instance of enrol_flatfile_plugin.
diff --git a/enrol/flatfile/cli/sync.php b/enrol/flatfile/cli/sync.php
index 7f5313a4f70..2b248c10c3d 100644
--- a/enrol/flatfile/cli/sync.php
+++ b/enrol/flatfile/cli/sync.php
@@ -34,7 +34,7 @@
  * $ php admin/tool/task/cli/schedule_task.php -h
  *
  * Execute task:
- * $ sudo -u www-data /usr/bin/php admin/tool/task/cli/scheduled_task.php /
+ * $ sudo -u www-data /usr/bin/php admin/tool/task/cli/schedule_task.php /
  * --execute=\\enrol_flatfile\\task\\flatfile_sync_task
  *
  * @package    enrol_flatfile