MDL-51862 tool_lp: Create a persistent model user_competency_plan

This commit is contained in:
Serge Gauthier
2016-04-18 10:58:39 +08:00
committed by Frederic Massart
parent 5159d6794b
commit 8a9030c18e
7 changed files with 370 additions and 1 deletions
@@ -0,0 +1,212 @@
<?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/>.
/**
* 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;
}
}
+21
View File
@@ -154,5 +154,26 @@
<INDEX NAME="useridcompetency" UNIQUE="true" FIELDS="userid, competencyid"/>
</INDEXES>
</TABLE>
<TABLE NAME="tool_lp_user_competency_plan" COMMENT="User competencies plans">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="User associated to the competency."/>
<FIELD NAME="competencyid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Competency associated to the user."/>
<FIELD NAME="planid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Plan associated to the user."/>
<FIELD NAME="status" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Competency status."/>
<FIELD NAME="reviewerid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="User that reviewed the competency."/>
<FIELD NAME="proficiency" TYPE="int" LENGTH="2" NOTNULL="false" SEQUENCE="false" COMMENT="Indicate if the competency is proficient not."/>
<FIELD NAME="grade" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="Grade assigned to the competency."/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="usermodified" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="usercompetencyplan" UNIQUE="true" FIELDS="userid, competencyid, planid"/>
</INDEXES>
</TABLE>
</TABLES>
</XMLDB>
+37
View File
@@ -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;
}
+1
View File
@@ -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';
+61
View File
@@ -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;
}
}
+37
View File
@@ -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);
}
}
+1 -1
View File
@@ -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).