MDL-51506 tool_lp: Migrating competency_framework to new model API

This commit is contained in:
Frederic Massart
2016-04-18 10:58:34 +08:00
parent c27113d93e
commit b60702643c
3 changed files with 74 additions and 233 deletions
+2 -7
View File
@@ -363,9 +363,6 @@ class api {
$framework = new competency_framework($record->id);
// Check the permissions before update.
require_capability('tool/lp:competencymanage', $framework->get_context());
if (isset($record->contextid) && $record->contextid != $framework->get_contextid()) {
throw new coding_exception('Changing the context of an existing framework is forbidden.');
}
$framework->from_record($record);
return $framework->update();
}
@@ -415,9 +412,8 @@ class api {
}
// OK - all set.
$framework = new competency_framework();
list($insql, $inparams) = $DB->get_in_or_equal(array_keys($contexts), SQL_PARAMS_NAMED);
return $framework->get_records_select("contextid $insql", $inparams, $sort, '*', $skip, $limit);
return competency_framework::get_records_select("contextid $insql", $inparams, $sort, '*', $skip, $limit);
}
/**
@@ -445,9 +441,8 @@ class api {
}
// OK - all set.
$framework = new competency_framework();
list($insql, $inparams) = $DB->get_in_or_equal(array_keys($contexts), SQL_PARAMS_NAMED);
return $framework->count_records_select("contextid $insql", $inparams);
return competency_framework::count_records_select("contextid $insql", $inparams);
}
/**
+61 -224
View File
@@ -23,8 +23,9 @@
*/
namespace tool_lp;
use stdClass;
use context;
use lang_string;
use stdClass;
/**
* Class for loading/storing competency frameworks from the DB.
@@ -34,127 +35,7 @@ use context;
*/
class competency_framework extends persistent {
/** @var string $shortname Short name for this framework */
private $shortname = '';
/** @var string $idnumber Unique idnumber for this framework - must be unique if it is non-empty */
private $idnumber = '';
/** @var string $description Description for this framework */
private $description = '';
/** @var int $descriptionformat Format for the description */
private $descriptionformat = 0;
/** @var bool $visible Used to show/hide this framework */
private $visible = true;
/** @var int $scaleid The scale ID for this framework */
private $scaleid = 0;
/** @var string $scaleconfiguration scale information relevant to this framework*/
private $scaleconfiguration = '';
/** @var int $contextid The context ID in which the framework is set. */
private $contextid = null;
/**
* Method that provides the table name matching this class.
*
* @return string
*/
public function get_table_name() {
return 'tool_lp_competency_framework';
}
/**
* Get the short name.
*
* @return string The short name
*/
public function get_shortname() {
return $this->shortname;
}
/**
* Set the short name.
*
* @param string $shortname The short name
*/
public function set_shortname($shortname) {
$this->shortname = $shortname;
}
/**
* Get the description format.
*
* @return int The description format
*/
public function get_descriptionformat() {
return $this->descriptionformat;
}
/**
* Set the description format
*
* @param int $descriptionformat The description format
*/
public function set_descriptionformat($descriptionformat) {
$this->descriptionformat = $descriptionformat;
}
/**
* Get the id number.
*
* @return string The id number
*/
public function get_idnumber() {
return $this->idnumber;
}
/**
* Set the id number.
*
* @param string $idnumber The id number
*/
public function set_idnumber($idnumber) {
$this->idnumber = $idnumber;
}
/**
* Get the description.
*
* @return string The description
*/
public function get_description() {
return $this->description;
}
/**
* Set the description.
*
* @param string $description The description
*/
public function set_description($description) {
$this->description = $description;
}
/**
* Get the visible flag.
*
* @return string The visible flag
*/
public function get_visible() {
return $this->visible;
}
/**
* Set the visible flag.
*
* @param string $visible The visible flag
*/
public function set_visible($visible) {
$this->visible = $visible;
}
const TABLE = 'tool_lp_competency_framework';
/**
* Get the context.
@@ -162,122 +43,78 @@ class competency_framework extends persistent {
* @return context The context
*/
public function get_context() {
return context::instance_by_id($this->contextid);
return context::instance_by_id($this->get_contextid());
}
/**
* Get the contextid.
* Return the definition of the properties of this model.
*
* @return string The contextid
* @return array
*/
public function get_contextid() {
return $this->contextid;
protected static function define_properties() {
return array(
'shortname' => array(
'type' => PARAM_TEXT
),
'idnumber' => array(
'type' => PARAM_TEXT
),
'description' => array(
'type' => PARAM_TEXT,
'default' => ''
),
'descriptionformat' => array(
'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
'type' => PARAM_INT,
'default' => FORMAT_HTML
),
'visible' => array(
'type' => PARAM_BOOL,
'default' => 1
),
// TODO MDL-51442 make this mandatory.
'scaleid' => array(
'type' => PARAM_INT,
'default' => 0
),
// TODO MDL-51442 make this mandatory.
'scaleconfiguration' => array(
'type' => PARAM_RAW,
'default' => ''
),
'contextid' => array(
'type' => PARAM_INT
),
);
}
/**
* Get the scale ID.
* Validate the context ID.
*
* @return int The scale ID
* @param int $value The context ID.
* @return bool|lang_string
*/
public function get_scaleid() {
return $this->scaleid;
}
public function validate_contextid($value) {
global $DB;
/**
* Set the scale ID.
*
* @param int $scaleid The scale ID
*/
public function set_scaleid($scaleid) {
$this->scaleid = $scaleid;
}
$context = context::instance_by_id($value, IGNORE_MISSING);
if (!$context) {
return new lang_string('invalidcontext', 'error');
} else if ($context->contextlevel != CONTEXT_SYSTEM && $context->contextlevel != CONTEXT_COURSECAT) {
return new lang_string('invalidcontext', 'error');
}
/**
* Get the scale configuration.
*
* @return string The scale configuration
*/
public function get_scaleconfiguration() {
return $this->scaleconfiguration;
}
// During update.
if ($this->get_id()) {
/**
* Set the scale configuration.
*
* @param string $scaleconfiguration The scale configuration (JSON string)
*/
public function set_scaleconfiguration($scaleconfiguration) {
$this->scaleconfiguration = $scaleconfiguration;
}
// The context must never change.
$oldcontextid = $DB->get_field(self::TABLE, 'contextid', array('id' => $this->get_id()), MUST_EXIST);
if ($this->get_contextid() != $oldcontextid) {
return new lang_string('invalidcontext', 'error');
}
}
/**
* Populate this class with data from a DB record.
*
* @param stdClass $record A DB record.
* @return \tool_lp\competency_framework
*/
public function from_record($record) {
if (isset($record->id)) {
$this->set_id($record->id);
}
if (isset($record->shortname)) {
$this->set_shortname($record->shortname);
}
if (isset($record->idnumber)) {
$this->set_idnumber($record->idnumber);
}
if (isset($record->description)) {
$this->set_description($record->description);
}
if (isset($record->descriptionformat)) {
$this->set_descriptionformat($record->descriptionformat);
}
if (isset($record->scaleid)) {
$this->set_scaleid($record->scaleid);
}
if (isset($record->scaleconfiguration)) {
$this->set_scaleconfiguration($record->scaleconfiguration);
}
if (isset($record->visible)) {
$this->set_visible($record->visible);
}
if (isset($record->timecreated)) {
$this->set_timecreated($record->timecreated);
}
if (isset($record->timemodified)) {
$this->set_timemodified($record->timemodified);
}
if (isset($record->usermodified)) {
$this->set_usermodified($record->usermodified);
}
if (isset($record->contextid)) {
$this->contextid = $record->contextid;
}
return $this;
}
/**
* Create a DB record from this class.
*
* @return stdClass
*/
public function to_record() {
$record = new stdClass();
$record->id = $this->get_id();
$record->shortname = $this->get_shortname();
$record->idnumber = $this->get_idnumber();
$record->description = $this->get_description();
$record->descriptionformat = $this->get_descriptionformat();
$record->descriptionformatted = format_text($this->get_description(), $this->get_descriptionformat());
$record->scaleid = $this->get_scaleid();
$record->scaleconfiguration = $this->get_scaleconfiguration();
$record->visible = $this->get_visible();
$record->timecreated = $this->get_timecreated();
$record->timemodified = $this->get_timemodified();
$record->usermodified = $this->get_usermodified();
$record->contextid = $this->get_contextid();
return $record;
return true;
}
}
+11 -2
View File
@@ -27,6 +27,7 @@ require_once("$CFG->libdir/externallib.php");
require_once("$CFG->libdir/grade/grade_scale.php");
use context;
use context_system;
use external_api;
use external_function_parameters;
use external_value;
@@ -286,7 +287,10 @@ class external extends external_api {
$params = (object) $params;
$result = api::create_framework($params);
return $result->to_record();
$record = $result->to_record();
$record->descriptionformatted = format_text($record->description, $record->descriptionformat,
array('context' => context_system::instance()));
return $record;
}
/**
@@ -337,7 +341,10 @@ class external extends external_api {
));
$result = api::read_framework($params['id']);
return $result->to_record();
$record = $result->to_record();
$record->descriptionformatted = format_text($record->description, $record->descriptionformat,
array('context' => context_system::instance()));
return $record;
}
/**
@@ -593,6 +600,8 @@ class external extends external_api {
$records = array();
foreach ($results as $result) {
$record = $result->to_record();
$record->descriptionformatted = format_text($record->description, $record->descriptionformat,
array('context' => context_system::instance()));
array_push($records, $record);
}
return $records;