MDL-52282 tool_lp: Create a scheduled task to sync plans from cohorts
authorIssam Taboubi <issam.taboubi@umontreal.ca>
Fri, 11 Dec 2015 22:23:07 +0000 (17:23 -0500)
committerFrederic Massart <fred@moodle.com>
Mon, 18 Apr 2016 02:58:46 +0000 (10:58 +0800)
admin/tool/lp/classes/task/sync_plans_from_template_cohorts_task.php [new file with mode: 0644]
admin/tool/lp/classes/template_cohort.php
admin/tool/lp/db/tasks.php
admin/tool/lp/lang/en/tool_lp.php
admin/tool/lp/tests/task_test.php [new file with mode: 0644]
admin/tool/lp/version.php

diff --git a/admin/tool/lp/classes/task/sync_plans_from_template_cohorts_task.php b/admin/tool/lp/classes/task/sync_plans_from_template_cohorts_task.php
new file mode 100644 (file)
index 0000000..ce1d566
--- /dev/null
@@ -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/>.
+
+/**
+ * Synchronise plans from template cohorts.
+ *
+ * @package    tool_lp
+ * @copyright  2015 Issam Taboubi <issam.taboubi@umontreal.ca>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace tool_lp\task;
+defined('MOODLE_INTERNAL') || die();
+
+use tool_lp\api;
+use tool_lp\template_cohort;
+
+/**
+ * Synchronise plans from template cohorts.
+ *
+ *
+ * @package    tool_lp
+ * @copyright  2015 Issam Taboubi <issam.taboubi@umontreal.ca>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class sync_plans_from_template_cohorts_task extends \core\task\scheduled_task {
+
+    /**
+     * Get a descriptive name for this task.
+     *
+     * @return string
+     */
+    public function get_name() {
+        return get_string('syncplanscohorts', 'tool_lp');
+    }
+
+    /**
+     * Do the job.
+     */
+    public function execute() {
+
+        $missingplans = template_cohort::get_all_missing_plans(true);
+
+        foreach ($missingplans as $missingplan) {
+            foreach ($missingplan['userids'] as $userid) {
+                api::create_plan_from_template($missingplan['template']->get_id(), $userid);
+            }
+        }
+    }
+}
index 4996c03..d08a87f 100644 (file)
@@ -26,6 +26,7 @@ namespace tool_lp;
 defined('MOODLE_INTERNAL') || die();
 
 use lang_string;
+use tool_lp\template;
 
 /**
  * Template cohort persistent.
@@ -143,4 +144,47 @@ class template_cohort extends persistent {
         return $relation;
     }
 
+    /**
+     * Return an array of templates persistent with their missing userids.
+     * Note that only cohorts associated with visible templates were considered.
+     *
+     * @param bool $unlinkedaremissing When true, unlinked plans are considered as missing.
+     * @return array( array(
+     *                   'template' => \tool_lp\template,
+     *                   'userids' => array
+     *              ))
+     */
+    public static function get_all_missing_plans($unlinkedaremissing = false) {
+        global $DB;
+
+        $skipsql = !$unlinkedaremissing ? '(t.id = p.templateid OR t.id = p.origtemplateid)' : 't.id = p.templateid';
+
+        // TODO MDL-52526 only unexpired template are considered and fix the time()+1 duedate issue.
+        $sql = "SELECT cm.userid, t.*
+                  FROM {cohort_members} cm
+                  JOIN {" . self::TABLE . "} tc ON cm.cohortid = tc.cohortid
+                  JOIN {" . template::TABLE . "} t ON (tc.templateid = t.id AND t.visible = 1)
+             LEFT JOIN {" . plan::TABLE . "} p ON (cm.userid = p.userid AND $skipsql)
+                 WHERE p.id IS NULL
+              ORDER BY t.id";
+
+        $results = $DB->get_records_sql($sql);
+
+        $missingplans = array();
+        foreach ($results as $usertemplate) {
+            $userid = $usertemplate->userid;
+
+            // Check if template already exist in the array.
+            if (isset($missingplans[$usertemplate->id])) {
+                $missingplans[$usertemplate->id]['userids'][] = $userid;
+            } else {
+                unset($usertemplate->userid);
+                $template = new template(0, $usertemplate);
+                $missingplans[$template->get_id()]['template'] = $template;
+                $missingplans[$template->get_id()]['userids'][] = $userid;
+            }
+        }
+        return array_values($missingplans);
+    }
+
 }
index 08b95b1..8eb2b96 100644 (file)
@@ -34,4 +34,13 @@ $tasks = array(
         'dayofweek' => '*',
         'month' => '*'
     ),
+    array(
+        'classname' => 'tool_lp\task\sync_plans_from_template_cohorts_task',
+        'blocking' => 0,
+        'minute' => 'R',
+        'hour' => '*',
+        'day' => '*',
+        'dayofweek' => '*',
+        'month' => '*'
+    ),
 );
index 5435ed4..7103796 100644 (file)
@@ -192,6 +192,7 @@ $string['shortname'] = 'Name';
 $string['state'] = 'State';
 $string['status'] = 'Status';
 $string['stopsyncingcohort'] = 'Stop syncing cohort';
+$string['syncplanscohorts'] = 'Sync plans from template cohorts';
 $string['taxonomies'] = 'Taxonomies';
 $string['taxonomy_add_behaviour'] = 'Add behaviour';
 $string['taxonomy_add_competency'] = 'Add competency';
diff --git a/admin/tool/lp/tests/task_test.php b/admin/tool/lp/tests/task_test.php
new file mode 100644 (file)
index 0000000..535e849
--- /dev/null
@@ -0,0 +1,88 @@
+<?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/>.
+
+/**
+ * Task tests.
+ *
+ * @package    tool_lp
+ * @copyright  2015 Issam Taboubi <issam.taboubi@umontreal.ca>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+use tool_lp\api;
+use tool_lp\plan;
+
+/**
+ * Task tests.
+ *
+ * @package    tool_lp
+ * @copyright  2015 Issam Taboubi <issam.taboubi@umontreal.ca>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class tool_lp_task_testcase extends advanced_testcase {
+
+    public function test_sync_plans_from_cohorts_task() {
+        $this->resetAfterTest(true);
+        $this->setAdminUser();
+        $dg = $this->getDataGenerator();
+        $lpg = $dg->get_plugin_generator('tool_lp');
+
+        $user1 = $dg->create_user();
+        $user2 = $dg->create_user();
+        $user3 = $dg->create_user();
+        $user4 = $dg->create_user();
+
+        $cohort = $dg->create_cohort();
+        $tpl = $lpg->create_template();
+
+        // Add 2 users to the cohort.
+        cohort_add_member($cohort->id, $user1->id);
+        cohort_add_member($cohort->id, $user2->id);
+
+        // Creating plans from template cohort.
+        $templatecohort = api::create_template_cohort($tpl->get_id(), $cohort->id);
+        $created = api::create_plans_from_template_cohort($tpl->get_id(), $cohort->id);
+
+        $this->assertEquals(2, $created);
+
+        $task = \core\task\manager::get_scheduled_task('\\tool_lp\\task\\sync_plans_from_template_cohorts_task');
+        $this->assertInstanceOf('\tool_lp\task\sync_plans_from_template_cohorts_task', $task);
+
+        // Add two more users to the cohort.
+        cohort_add_member($cohort->id, $user3->id);
+        cohort_add_member($cohort->id, $user4->id);
+
+        $task->execute();
+
+        $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
+
+        // Test if remove user from cohort will affect plans.
+        cohort_remove_member($cohort->id, $user3->id);
+        cohort_remove_member($cohort->id, $user4->id);
+
+        $task->execute();
+        $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
+
+        // Adding users to cohort that already exist in plans.
+        cohort_add_member($cohort->id, $user3->id);
+        cohort_add_member($cohort->id, $user4->id);
+
+        $task->execute();
+        $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
+    }
+}
index e2c1652..dc03b34 100644 (file)
@@ -25,6 +25,6 @@
 defined('MOODLE_INTERNAL') || die();
 
 
-$plugin->version   = 2015111029; // The current plugin version (Date: YYYYMMDDXX).
+$plugin->version   = 2015111030; // The current plugin version (Date: YYYYMMDDXX).
 $plugin->requires  = 2014110400; // Requires this Moodle version.
 $plugin->component = 'tool_lp'; // Full name of the plugin (used for diagnostics).