From 8a9030c18ec2dbc67b25cf5672b8eedd5ce7bffb Mon Sep 17 00:00:00 2001 From: Serge Gauthier Date: Fri, 30 Oct 2015 11:43:22 -0400 Subject: [PATCH] MDL-51862 tool_lp: Create a persistent model user_competency_plan --- .../tool/lp/classes/user_competency_plan.php | 212 ++++++++++++++++++ admin/tool/lp/db/install.xml | 21 ++ admin/tool/lp/db/upgrade.php | 37 +++ admin/tool/lp/lang/en/tool_lp.php | 1 + admin/tool/lp/tests/generator/lib.php | 61 +++++ admin/tool/lp/tests/generator_test.php | 37 +++ admin/tool/lp/version.php | 2 +- 7 files changed, 370 insertions(+), 1 deletion(-) create mode 100644 admin/tool/lp/classes/user_competency_plan.php diff --git a/admin/tool/lp/classes/user_competency_plan.php b/admin/tool/lp/classes/user_competency_plan.php new file mode 100644 index 00000000000..aa7b04c5873 --- /dev/null +++ b/admin/tool/lp/classes/user_competency_plan.php @@ -0,0 +1,212 @@ +. + +/** + * Class for user_competency_plan persistence. + * + * @package tool_lp + * @copyright 2015 Serge Gauthier + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace tool_lp; + +use lang_string; + +/** + * Class for loading/storing user_competency_plan from the DB. + * + * @copyright 2015 Serge Gauthier + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class user_competency_plan extends persistent { + + /** Table name for user_competency_plan persistency */ + const TABLE = 'tool_lp_user_competency_plan'; + + /** Idle status */ + const STATUS_IDLE = 0; + + /** Waiting for review status */ + const STATUS_WAITING_FOR_REVIEW = 1; + + /** In review status */ + const STATUS_IN_REVIEW = 2; + + /** + * Return the definition of the properties of this model. + * + * @return array + */ + protected static function define_properties() { + return array( + 'userid' => array( + 'type' => PARAM_INT, + ), + 'competencyid' => array( + 'type' => PARAM_INT, + ), + 'status' => array( + 'choices' => array( + self::STATUS_IDLE, + self::STATUS_WAITING_FOR_REVIEW, + self::STATUS_IN_REVIEW, + ), + 'type' => PARAM_INT, + 'default' => self::STATUS_IDLE, + ), + 'reviewerid' => array( + 'type' => PARAM_INT, + 'default' => null, + 'null' => NULL_ALLOWED, + ), + 'proficiency' => array( + 'type' => PARAM_BOOL, + 'default' => null, + 'null' => NULL_ALLOWED, + ), + 'grade' => array( + 'type' => PARAM_INT, + 'default' => null, + 'null' => NULL_ALLOWED, + ), + 'planid' => array( + 'type' => PARAM_INT, + ), + ); + } + + /** + * Human readable status name. + * + * @param int $status The status code. + * @return lang_string + */ + public static function get_status_name($status) { + + switch ($status) { + case self::STATUS_IDLE: + $strname = 'idle'; + break; + case self::STATUS_WAITING_FOR_REVIEW: + $strname = 'waitingforreview'; + break; + case self::STATUS_IN_REVIEW: + $strname = 'inreview'; + break; + default: + throw new \moodle_exception('errorcomptencystatus', 'tool_lp', '', $status); + break; + } + + return new lang_string('usercompetencystatus_' . $strname, 'tool_lp'); + } + + /** + * Get list of competency status. + * + * @return array + */ + public static function get_status_list() { + + static $list = null; + + if ($list === null) { + $list = array( + self::STATUS_IDLE => self::get_status_name(self::STATUS_IDLE), + self::STATUS_WAITING_FOR_REVIEW => self::get_status_name(self::STATUS_WAITING_FOR_REVIEW), + self::STATUS_IN_REVIEW => self::get_status_name(self::STATUS_IN_REVIEW)); + } + + return $list; + } + + /** + * Validate the user ID. + * + * @param int $value The value. + * @return true|lang_string + */ + protected function validate_userid($value) { + global $DB; + + if (!$DB->record_exists('user', array('id' => $value))) { + return new lang_string('invaliduserid', 'error'); + } + + return true; + } + + /** + * Validate the competency ID. + * + * @param int $value The value. + * @return true|lang_string + */ + protected function validate_competencyid($value) { + if (!competency::record_exists($value)) { + return new lang_string('errornocompetency', 'tool_lp', $value); + } + + return true; + } + + /** + * Validate the reviewer ID. + * + * @param int $value The value. + * @return true|lang_string + */ + protected function validate_reviewerid($value) { + global $DB; + + if ($value !== null && !$DB->record_exists('user', array('id' => $value))) { + return new lang_string('invaliduserid', 'error'); + } + + return true; + } + + /** + * Validate the grade. + * + * @param int $value The value. + * @return true|lang_string + */ + protected function validate_grade($value) { + if ($value !== null && $value <= 0) { + return new lang_string('invalidgrade', 'tool_lp'); + } + + return true; + } + + /** + * Validate the plan ID. + * + * @param int $value The value. + * @return true|lang_string + */ + protected function validate_planid($value) { + global $DB; + + if (!plan::record_exists($value) ) { + return new lang_string('invalidplan', 'tool_lp'); + } + + return true; + } + +} diff --git a/admin/tool/lp/db/install.xml b/admin/tool/lp/db/install.xml index f3e4c90159a..dbd33b2c94f 100644 --- a/admin/tool/lp/db/install.xml +++ b/admin/tool/lp/db/install.xml @@ -154,5 +154,26 @@ + + + + + + + + + + + + + + + + + + + + +
\ No newline at end of file diff --git a/admin/tool/lp/db/upgrade.php b/admin/tool/lp/db/upgrade.php index 3b5ee595dbc..337e49c229b 100644 --- a/admin/tool/lp/db/upgrade.php +++ b/admin/tool/lp/db/upgrade.php @@ -218,5 +218,42 @@ function xmldb_tool_lp_upgrade($oldversion) { upgrade_plugin_savepoint(true, 2015052420, 'tool', 'lp'); } + if ($oldversion < 2015052423) { + + // Define table tool_lp_user_competency_plan to be created. + $table = new xmldb_table('tool_lp_user_competency_plan'); + + // Adding fields to table tool_lp_user_competency_plan. + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + $table->add_field('planid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + $table->add_field('status', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0'); + $table->add_field('reviewerid', XMLDB_TYPE_INTEGER, '10', null, null, null, null); + $table->add_field('proficiency', XMLDB_TYPE_INTEGER, '2', null, null, null, '0'); + $table->add_field('grade', XMLDB_TYPE_INTEGER, '10', null, null, null, null); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null); + $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + + // Adding keys to table tool_lp_user_competency_plan. + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + + $index = new xmldb_index('usercompetencyplan', XMLDB_INDEX_UNIQUE, array('userid', 'competencyid', 'planid')); + + // Conditionally launch create table for tool_lp_user_competency_plan. + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + // Conditionally launch add index useridcompetency. + if (!$dbman->index_exists($table, $index)) { + $dbman->add_index($table, $index); + } + + // Lp savepoint reached. + upgrade_plugin_savepoint(true, 2015052423, 'tool', 'lp'); + } + return true; } diff --git a/admin/tool/lp/lang/en/tool_lp.php b/admin/tool/lp/lang/en/tool_lp.php index c9d772f090c..45b7524c6b5 100644 --- a/admin/tool/lp/lang/en/tool_lp.php +++ b/admin/tool/lp/lang/en/tool_lp.php @@ -71,6 +71,7 @@ $string['hiddenhint'] = '(hidden)'; $string['idnumber'] = 'Id number'; $string['invalidgrade'] = 'Invalid grade'; $string['invalidpersistent'] = 'Invalid persistent'; +$string['invalidplan'] = 'Invalid learning plan'; $string['invalidtaxonomy'] = 'Invalid taxonomy: {$a}'; $string['itemstoadd'] = 'Items to add'; $string['learningplans'] = 'Learning plans'; diff --git a/admin/tool/lp/tests/generator/lib.php b/admin/tool/lp/tests/generator/lib.php index dc76e3c943d..73d5ea8f608 100644 --- a/admin/tool/lp/tests/generator/lib.php +++ b/admin/tool/lp/tests/generator/lib.php @@ -26,8 +26,10 @@ use tool_lp\competency; use tool_lp\competency_framework; use tool_lp\external; +use tool_lp\plan; use tool_lp\related_competency; use tool_lp\user_competency; +use tool_lp\user_competency_plan; defined('MOODLE_INTERNAL') || die(); @@ -47,6 +49,9 @@ class tool_lp_generator extends component_generator_base { /** @var int Number of created frameworks. */ protected $frameworkcount = 0; + /** @var int Number of created plans. */ + protected $plancount = 0; + /** @var stdClass Scale that we might need. */ protected $scale; @@ -205,5 +210,61 @@ class tool_lp_generator extends component_generator_base { return $usercompetency; } + /** + * Create a new plan. + * + * @param array|stdClass $record + * @return plan + */ + public function create_plan($record = null) { + $this->plancount++; + $i = $this->plancount; + $record = (object) $record; + + if (!isset($record->name)) { + $record->name = "Plan shortname $i"; + } + if (!isset($record->description)) { + $record->description = "Plan $i description"; + } + if (!isset($record->descriptionformat)) { + $record->descriptionformat = FORMAT_HTML; + } + if (!isset($record->userid)) { + throw new coding_exception('The userid value is required.'); + } + + $plan = new plan(0, $record); + $plan->create(); + + return $plan; + } + + /** + * Create a new user competency plan. + * + * @param array|stdClass $record + * @return user_competency_plan + */ + public function create_user_competency_plan($record = null) { + $record = (object) $record; + + if (!isset($record->userid)) { + throw new coding_exception('The userid value is required.'); + } + if (!isset($record->competencyid)) { + throw new coding_exception('The competencyid value is required.'); + } + + if (!isset($record->planid)) { + throw new coding_exception('The planid value is required.'); + } + + $usercompetencyplan = new user_competency_plan(0, $record); + $usercompetencyplan->create(); + + return $usercompetencyplan; + } + } diff --git a/admin/tool/lp/tests/generator_test.php b/admin/tool/lp/tests/generator_test.php index d1cef213e7f..cb9aa93f8c4 100644 --- a/admin/tool/lp/tests/generator_test.php +++ b/admin/tool/lp/tests/generator_test.php @@ -25,8 +25,10 @@ use tool_lp\competency; use tool_lp\competency_framework; +use tool_lp\plan; use tool_lp\related_competency; use tool_lp\user_competency; +use tool_lp\user_competency_plan; defined('MOODLE_INTERNAL') || die(); @@ -78,6 +80,17 @@ class tool_lp_generator_testcase extends advanced_testcase { $this->assertInstanceOf('\tool_lp\related_competency', $rc); } + public function test_create_plan() { + $this->resetAfterTest(true); + + $user = $this->getDataGenerator()->create_user(); + $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp'); + $this->assertEquals(0, plan::count_records()); + $plan = $lpg->create_plan(array('userid' => $user->id)); + $this->assertEquals(1, plan::count_records()); + $this->assertInstanceOf('\tool_lp\plan', $plan); + } + public function test_create_user_competency() { $this->resetAfterTest(true); @@ -94,5 +107,29 @@ class tool_lp_generator_testcase extends advanced_testcase { $this->assertInstanceOf('\tool_lp\user_competency', $rc); } + public function test_create_user_competency_plan() { + $this->resetAfterTest(true); + + $user = $this->getDataGenerator()->create_user(); + $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp'); + $framework = $lpg->create_framework(); + $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); + $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); + $plan = $lpg->create_plan(array('userid' => $user->id)); + $this->assertEquals(0, user_competency_plan::count_records()); + $ucp = $lpg->create_user_competency_plan(array( + 'userid' => $user->id, + 'competencyid' => $c1->get_id(), + 'planid' => $plan->get_id() + )); + $ucp = $lpg->create_user_competency_plan(array( + 'userid' => $user->id, + 'competencyid' => $c2->get_id(), + 'planid' => $plan->get_id() + )); + $this->assertEquals(2, user_competency_plan::count_records()); + $this->assertInstanceOf('\tool_lp\user_competency_plan', $ucp); + } + } diff --git a/admin/tool/lp/version.php b/admin/tool/lp/version.php index 6f6d8b0205b..752f1dd51bb 100644 --- a/admin/tool/lp/version.php +++ b/admin/tool/lp/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2015052422; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2015052423; // 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).