From ed549dbfcb9715f85a9c0c731c3293827eaa0f9d Mon Sep 17 00:00:00 2001 From: Issam Taboubi Date: Fri, 11 Dec 2015 17:23:07 -0500 Subject: [PATCH] MDL-52282 tool_lp: Create a scheduled task to sync plans from cohorts --- .../sync_plans_from_template_cohorts_task.php | 63 +++++++++++++ admin/tool/lp/classes/template_cohort.php | 44 ++++++++++ admin/tool/lp/db/tasks.php | 9 ++ admin/tool/lp/lang/en/tool_lp.php | 1 + admin/tool/lp/tests/task_test.php | 88 +++++++++++++++++++ admin/tool/lp/version.php | 2 +- 6 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 admin/tool/lp/classes/task/sync_plans_from_template_cohorts_task.php create mode 100644 admin/tool/lp/tests/task_test.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 index 00000000000..ce1d566154d --- /dev/null +++ b/admin/tool/lp/classes/task/sync_plans_from_template_cohorts_task.php @@ -0,0 +1,63 @@ +. + +/** + * Synchronise plans from template cohorts. + * + * @package tool_lp + * @copyright 2015 Issam Taboubi + * @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 + * @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); + } + } + } +} diff --git a/admin/tool/lp/classes/template_cohort.php b/admin/tool/lp/classes/template_cohort.php index 4996c032c7d..d08a87f9129 100644 --- a/admin/tool/lp/classes/template_cohort.php +++ b/admin/tool/lp/classes/template_cohort.php @@ -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); + } + } diff --git a/admin/tool/lp/db/tasks.php b/admin/tool/lp/db/tasks.php index 08b95b1a7e6..8eb2b96526c 100644 --- a/admin/tool/lp/db/tasks.php +++ b/admin/tool/lp/db/tasks.php @@ -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' => '*' + ), ); diff --git a/admin/tool/lp/lang/en/tool_lp.php b/admin/tool/lp/lang/en/tool_lp.php index 5435ed4bba1..710379699ee 100644 --- a/admin/tool/lp/lang/en/tool_lp.php +++ b/admin/tool/lp/lang/en/tool_lp.php @@ -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 index 00000000000..535e849ab80 --- /dev/null +++ b/admin/tool/lp/tests/task_test.php @@ -0,0 +1,88 @@ +. + +/** + * Task tests. + * + * @package tool_lp + * @copyright 2015 Issam Taboubi + * @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 + * @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()))); + } +} diff --git a/admin/tool/lp/version.php b/admin/tool/lp/version.php index e2c16523970..dc03b3443ef 100644 --- a/admin/tool/lp/version.php +++ b/admin/tool/lp/version.php @@ -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).