MDL-50253 tool_lp: Introduction of data generators to learning plans

This commit is contained in:
Frederic Massart
2016-04-18 10:58:35 +08:00
parent aec5363ec1
commit 72a1e0dede
2 changed files with 265 additions and 0 deletions
+185
View File
@@ -0,0 +1,185 @@
<?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/>.
/**
* Tool LP data generator.
*
* @package tool_lp
* @category test
* @copyright 2015 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use tool_lp\competency;
use tool_lp\competency_framework;
use tool_lp\external;
use tool_lp\related_competency;
defined('MOODLE_INTERNAL') || die();
/**
* Tool LP data generator.
*
* @package tool_lp
* @category test
* @copyright 2015 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_lp_generator extends component_generator_base {
/** @var int Number of created competencies. */
protected $competencycount = 0;
/** @var int Number of created frameworks. */
protected $frameworkcount = 0;
/** @var stdClass Scale that we might need. */
protected $scale;
/**
* Reset process.
*
* Do not call directly.
*
* @return void
*/
public function reset() {
$this->competencycount = 0;
$this->frameworkcount = 0;
$this->scale = null;
}
/**
* Create a new competency.
*
* @param array|stdClass $record
* @return competency
*/
public function create_competency($record = null) {
$this->frameworkcount++;
$i = $this->frameworkcount;
$record = (object) $record;
if (!isset($record->competencyframeworkid)) {
throw new coding_exception('The competencyframeworkid value is required.');
}
if (!isset($record->shortname)) {
$record->shortname = "Competency shortname $i";
}
if (!isset($record->idnumber)) {
$record->idnumber = "cmp{$i}";
}
if (!isset($record->description)) {
$record->description = "Competency $i description ";
}
if (!isset($record->descriptionformat)) {
$record->descriptionformat = FORMAT_HTML;
}
if (!isset($record->visible)) {
$record->visible = 1;
}
$competency = new competency(0, $record);
$competency->create();
return $competency;
}
/**
* Create a new framework.
*
* @param array|stdClass $record
* @return competency_framework
*/
public function create_framework($record = null) {
$generator = phpunit_util::get_data_generator();
$this->frameworkcount++;
$i = $this->frameworkcount;
$record = (object) $record;
if (!isset($record->shortname)) {
$record->shortname = "Framework shortname $i";
}
if (!isset($record->idnumber)) {
$record->idnumber = "frm{$i}";
}
if (!isset($record->description)) {
$record->description = "Framework $i description ";
}
if (!isset($record->descriptionformat)) {
$record->descriptionformat = FORMAT_HTML;
}
if (!isset($record->visible)) {
$record->visible = 1;
}
// TODO MDL-51442 make sure this passes validation.
if (!isset($record->scaleid)) {
if (isset($record->scaleconfiguration)) {
throw new coding_exception('Scale configuration must be provided with a scale.');
}
if (!$this->scale) {
$this->scale = $generator->create_scale();
}
$record->scaleid = $this->scale->id;
}
// TODO MDL-51442 make sure this passes validation.
if (!isset($record->scaleconfiguration)) {
$values = external::get_scale_values($record->scaleid);
$scaleconfig = array(array('scaleid' => $record->scaleid));
$scaleconfig[] = array(
'name' => $values[0]['name'],
'id' => $values[0]['id'],
'scaledefault' => 1,
'proficient' => 1,
);
$record->scaleconfiguration = json_encode($scaleconfig);
}
if (!isset($record->contextid)) {
$record->contextid = context_system::instance()->id;
}
$framework = new competency_framework(0, $record);
$framework->create();
return $framework;
}
/**
* Create a related competency.
*
* @param array|stdClass $record
* @return related_competency
*/
public function create_related_competency($record = null) {
$record = (object) $record;
if (!isset($record->competencyid)) {
throw new coding_exception('Property competencyid is required.');
}
if (!isset($record->relatedcompetencyid)) {
throw new coding_exception('Property relatedcompetencyid is required.');
}
$relation = related_competency::get_relation($record->competencyid, $record->relatedcompetencyid);
if ($relation->get_id()) {
throw new coding_exception('Relation already exists');
}
$relation->create();
return $relation;
}
}
+80
View File
@@ -0,0 +1,80 @@
<?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/>.
/**
* Tool LP data generator tests.
*
* @package tool_lp
* @category test
* @copyright 2015 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use tool_lp\competency;
use tool_lp\competency_framework;
use tool_lp\related_competency;
defined('MOODLE_INTERNAL') || die();
/**
* Tool LP data generator testcase.
*
* @package tool_lp
* @category test
* @copyright 2015 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_lp_generator_testcase extends advanced_testcase {
public function test_create_framework() {
$this->resetAfterTest(true);
$lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
$this->assertEquals(0, competency_framework::count_records());
$framework = $lpg->create_framework();
$framework = $lpg->create_framework();
$this->assertEquals(2, competency_framework::count_records());
$this->assertInstanceOf('\tool_lp\competency_framework', $framework);
}
public function test_create_competency() {
$this->resetAfterTest(true);
$lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
$framework = $lpg->create_framework();
$this->assertEquals(0, competency::count_records());
$competency = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
$competency = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
$this->assertEquals(2, competency::count_records());
$this->assertInstanceOf('\tool_lp\competency', $competency);
}
public function test_create_related_competency() {
$this->resetAfterTest(true);
$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, related_competency::count_records());
$rc = $lpg->create_related_competency(array('competencyid' => $c1->get_id(), 'relatedcompetencyid' => $c2->get_id()));
$rc = $lpg->create_related_competency(array('competencyid' => $c2->get_id(), 'relatedcompetencyid' => $c3->get_id()));
$this->assertEquals(2, related_competency::count_records());
$this->assertInstanceOf('\tool_lp\related_competency', $rc);
}
}