MDL-52282 tool_lp: Create a scheduled task to sync plans from cohorts

This commit is contained in:
Issam Taboubi
2016-04-18 10:58:46 +08:00
committed by Frederic Massart
parent 52eda87637
commit ed549dbfcb
6 changed files with 206 additions and 1 deletions
@@ -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 <[email protected]>
* @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 <[email protected]>
* @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);
}
}
}
}
+44
View 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);
}
}
+9
View 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' => '*'
),
);
+1
View 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';
+88
View File
@@ -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 <[email protected]>
* @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 <[email protected]>
* @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())));
}
}
+1 -1
View 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).