Skip to content

Commit

Permalink
MDL-51644 tool_lp: Create persistent model for user_competency
Browse files Browse the repository at this point in the history
  • Loading branch information
gauts authored and Frederic Massart committed Apr 18, 2016
1 parent 67e0b72 commit 6d57a98
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 2 deletions.
193 changes: 193 additions & 0 deletions admin/tool/lp/classes/user_competency.php
@@ -0,0 +1,193 @@
<?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 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;
}

}
22 changes: 21 additions & 1 deletion admin/tool/lp/db/install.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="admin/tool/lp/db" VERSION="20151022" COMMENT="XMLDB file for Moodle admin/tool/lp"
<XMLDB PATH="admin/tool/lp/db" VERSION="20151027" COMMENT="XMLDB file for Moodle admin/tool/lp"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -134,5 +134,25 @@
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="tool_lp_user_competency" COMMENT="User competencies">
<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="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="useridcompetency" UNIQUE="true" FIELDS="userid, competencyid"/>
</INDEXES>
</TABLE>
</TABLES>
</XMLDB>
36 changes: 36 additions & 0 deletions admin/tool/lp/db/upgrade.php
Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions admin/tool/lp/lang/en/tool_lp.php
Expand Up @@ -39,6 +39,9 @@
$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';
Expand Down Expand Up @@ -68,6 +71,7 @@
$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';
Expand Down
24 changes: 24 additions & 0 deletions admin/tool/lp/tests/generator/lib.php
Expand Up @@ -27,6 +27,8 @@
use tool_lp\competency_framework;
use tool_lp\external;
use tool_lp\related_competency;
use tool_lp\user_competency;

defined('MOODLE_INTERNAL') || die();

/**
Expand Down Expand Up @@ -181,5 +183,27 @@ public function create_related_competency($record = null) {
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;
}

}

18 changes: 18 additions & 0 deletions admin/tool/lp/tests/generator_test.php
Expand Up @@ -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();

/**
Expand Down Expand Up @@ -76,5 +78,21 @@ public function test_create_related_competency() {
$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);
}

}

2 changes: 1 addition & 1 deletion admin/tool/lp/version.php
Expand Up @@ -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).

0 comments on commit 6d57a98

Please sign in to comment.