From 6d57a987bf43fa73b3ccac846c68a7dab49d326b Mon Sep 17 00:00:00 2001 From: Serge Gauthier Date: Wed, 28 Oct 2015 13:24:27 -0400 Subject: [PATCH] MDL-51644 tool_lp: Create persistent model for user_competency --- admin/tool/lp/classes/user_competency.php | 193 ++++++++++++++++++++++ admin/tool/lp/db/install.xml | 22 ++- admin/tool/lp/db/upgrade.php | 36 ++++ admin/tool/lp/lang/en/tool_lp.php | 4 + admin/tool/lp/tests/generator/lib.php | 24 +++ admin/tool/lp/tests/generator_test.php | 18 ++ admin/tool/lp/version.php | 2 +- 7 files changed, 297 insertions(+), 2 deletions(-) create mode 100644 admin/tool/lp/classes/user_competency.php diff --git a/admin/tool/lp/classes/user_competency.php b/admin/tool/lp/classes/user_competency.php new file mode 100644 index 00000000000..574f79db697 --- /dev/null +++ b/admin/tool/lp/classes/user_competency.php @@ -0,0 +1,193 @@ +. + +/** + * Class for user_competency 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 from the DB. + * + * @copyright 2015 Serge Gauthier + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class user_competency extends persistent { + + /** Table name for user_competency persistency */ + const TABLE = 'tool_lp_user_competency'; + + /** 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, + ), + ); + } + + /** + * 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; + } + +} diff --git a/admin/tool/lp/db/install.xml b/admin/tool/lp/db/install.xml index 916a64bb5c5..f3e4c90159a 100644 --- a/admin/tool/lp/db/install.xml +++ b/admin/tool/lp/db/install.xml @@ -1,5 +1,5 @@ - @@ -134,5 +134,25 @@ + + + + + + + + + + + + + + + + + + + +
\ No newline at end of file diff --git a/admin/tool/lp/db/upgrade.php b/admin/tool/lp/db/upgrade.php index 654a02b995f..3b5ee595dbc 100644 --- a/admin/tool/lp/db/upgrade.php +++ b/admin/tool/lp/db/upgrade.php @@ -182,5 +182,41 @@ function xmldb_tool_lp_upgrade($oldversion) { upgrade_plugin_savepoint(true, 2015052416, 'tool', 'lp'); } + if ($oldversion < 2015052420) { + + // Define table tool_lp_user_competency to be created. + $table = new xmldb_table('tool_lp_user_competency'); + + // Adding fields to table tool_lp_user_competency. + $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('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. + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + + $index = new xmldb_index('useridcompetency', XMLDB_INDEX_UNIQUE, array('userid', 'competencyid')); + + // Conditionally launch create table for tool_lp_user_competency. + 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, 2015052420, '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 e96e0ce9681..79e0f643dbb 100644 --- a/admin/tool/lp/lang/en/tool_lp.php +++ b/admin/tool/lp/lang/en/tool_lp.php @@ -39,6 +39,9 @@ $string['competencyframeworks'] = 'Competency Frameworks'; $string['competencyframeworkupdated'] = 'Competency framework updated.'; $string['competencypicker'] = 'Competency picker'; $string['competencyrelatedcompetencies'] = '{$a} related competencies'; +$string['usercompetencystatus_idle'] = 'Idle'; +$string['usercompetencystatus_inreview'] = 'In review'; +$string['usercompetencystatus_waitingforreview'] = 'Waiting for review'; $string['competencyupdated'] = 'Competency updated'; $string['configurescale'] = 'Configure scales'; $string['coursecompetencies'] = 'Course competencies'; @@ -68,6 +71,7 @@ $string['errorscaleconfiguration'] = 'You must configure the scale by selecting $string['hidden'] = 'Hidden'; $string['hiddenhint'] = '(hidden)'; $string['idnumber'] = 'Id number'; +$string['invalidgrade'] = 'Invalid grade'; $string['invalidpersistent'] = 'Invalid persistent'; $string['invalidtaxonomy'] = 'Invalid taxonomy: {$a}'; $string['itemstoadd'] = 'Items to add'; diff --git a/admin/tool/lp/tests/generator/lib.php b/admin/tool/lp/tests/generator/lib.php index ce7d16a9947..dc76e3c943d 100644 --- a/admin/tool/lp/tests/generator/lib.php +++ b/admin/tool/lp/tests/generator/lib.php @@ -27,6 +27,8 @@ use tool_lp\competency; use tool_lp\competency_framework; use tool_lp\external; use tool_lp\related_competency; +use tool_lp\user_competency; + defined('MOODLE_INTERNAL') || die(); /** @@ -181,5 +183,27 @@ class tool_lp_generator extends component_generator_base { return $relation; } + /** + * Create a new user competency. + * + * @param array|stdClass $record + * @return user_competency + */ + public function create_user_competency($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.'); + } + + $usercompetency = new user_competency(0, $record); + $usercompetency->create(); + + return $usercompetency; + } + } diff --git a/admin/tool/lp/tests/generator_test.php b/admin/tool/lp/tests/generator_test.php index 0cf2ba870b1..d1cef213e7f 100644 --- a/admin/tool/lp/tests/generator_test.php +++ b/admin/tool/lp/tests/generator_test.php @@ -26,6 +26,8 @@ use tool_lp\competency; use tool_lp\competency_framework; use tool_lp\related_competency; +use tool_lp\user_competency; + defined('MOODLE_INTERNAL') || die(); /** @@ -76,5 +78,21 @@ class tool_lp_generator_testcase extends advanced_testcase { $this->assertInstanceOf('\tool_lp\related_competency', $rc); } + public function test_create_user_competency() { + $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())); + $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); + $this->assertEquals(0, user_competency::count_records()); + $rc = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id())); + $rc = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c2->get_id())); + $this->assertEquals(2, user_competency::count_records()); + $this->assertInstanceOf('\tool_lp\user_competency', $rc); + } + } diff --git a/admin/tool/lp/version.php b/admin/tool/lp/version.php index 0d523f71c03..7e18f485a41 100644 --- a/admin/tool/lp/version.php +++ b/admin/tool/lp/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2015052419; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2015052420; // 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).