Skip to content

Commit

Permalink
MDL-51034 tool_lp: List competencies in a user's plan
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic Massart committed Apr 18, 2016
1 parent 5840500 commit 1d8f0a6
Show file tree
Hide file tree
Showing 13 changed files with 489 additions and 7 deletions.
46 changes: 46 additions & 0 deletions admin/tool/lp/classes/api.php
Expand Up @@ -1306,6 +1306,52 @@ public static function delete_plan($id) {
return $plan->delete();
}

/**
* List the competencies in a user plan.
*
* @param int $planorid The plan, or its ID.
* @return array((object) array('competency' => competency, 'usercompetency' => user_competency))
*/
public static function list_plan_competencies($planorid) {
$plan = $planorid;
if (!is_object($planorid)) {
$plan = new plan($planorid);
}

if (!$plan->can_read()) {
$context = context_user::instance($plan->get_userid());
throw new required_capability_exception($context, 'tool/lp:planview', 'nopermissions', '');
}

$result = array();
$competencies = $plan->get_competencies();
$usercompetencies = user_competency::get_multiple($plan->get_userid(), $competencies);

// Build the return values.
foreach ($competencies as $key => $competency) {
$found = false;

foreach ($usercompetencies as $uckey => $uc) {
if ($uc->get_competencyid() == $competency->get_id()) {
$found = true;
unset($usercompetencies[$uckey]);
break;
}
}

if (!$found) {
$uc = user_competency::create_relation($plan->get_userid(), $competency->get_id());
}

$result[] = (object) array(
'competency' => $competency,
'usercompetency' => $uc,
);
}

return $result;
}

/**
* List all the related competencies.
*
Expand Down
13 changes: 13 additions & 0 deletions admin/tool/lp/classes/competency_framework.php
Expand Up @@ -27,6 +27,8 @@
use lang_string;
use stdClass;

require_once($CFG->libdir . '/grade/grade_scale.php');

/**
* Class for loading/storing competency frameworks from the DB.
*
Expand Down Expand Up @@ -111,6 +113,17 @@ protected static function define_properties() {
);
}

/**
* Return the scale.
*
* @return \grade_scale
*/
public function get_scale() {
$scale = \grade_scale::fetch(array('id' => $this->get_scaleid()));
$scale->load_items();
return $scale;
}

/**
* Get the constant name for a level.
*
Expand Down
107 changes: 107 additions & 0 deletions admin/tool/lp/classes/external.php
Expand Up @@ -865,6 +865,69 @@ protected static function get_competency_external_structure($includerelated = fa
return new external_single_structure($returns);
}

/**
* Returns the external structure of a full user_competency record.
*
* @return \external_single_structure
*/
protected static function get_user_competency_external_structure() {
$id = new external_value(
PARAM_INT,
'Database record id'
);
$userid = new external_value(
PARAM_INT,
'User to whom this record belongs to'
);
$competencyid = new external_value(
PARAM_INT,
'The competency associated with this record'
);
$status = new external_value(
PARAM_INT,
'The status of the user competency'
);
$reviewerid = new external_value(
PARAM_INT,
'The reviewer ID'
);
$proficiency = new external_value(
PARAM_BOOL,
'Whether or not the user is proficient'
);
$grade = new external_value(
PARAM_INT,
'The scale grade'
);
$timecreated = new external_value(
PARAM_INT,
'Timestamp this record was created'
);
$timemodified = new external_value(
PARAM_INT,
'Timestamp this record was modified'
);
$usermodified = new external_value(
PARAM_INT,
'User who modified this record last'
);

$returns = array(
'id' => $id,
'userid' => $userid,
'competencyid' => $competencyid,
'status' => $status,
'reviewerid' => $reviewerid,
'proficiency' => $proficiency,
'grade' => $grade,
'timecreated' => $timecreated,
'timemodified' => $timemodified,
'usermodified' => $usermodified,
);

return new external_single_structure($returns);
}

/**
* Returns description of create_competency() parameters.
*
Expand Down Expand Up @@ -3356,6 +3419,50 @@ public static function data_for_plans_page_returns() {
));
}

/**
* External function parameters structure.
*
* @return \external_description
*/
public static function list_plan_competencies_parameters() {
return new external_single_structure(array(
'id' => new external_value(PARAM_INT, 'The plan ID.')
));
}

/**
* List plan competencies.
* @param int $id The plan ID.
* @return array
*/
public static function list_plan_competencies($id) {
$params = self::validate_parameters(self::list_plan_competencies_parameters(), array('id' => $id));
$id = $params['id'];
$plan = api::read_plan($id);
$result = api::list_plan_competencies($plan);
foreach ($result as $key => $r) {
$r->competency = $r->competency->to_record();
$r->competency->descriptionformatted = format_text($r->competency->description,
$r->competency->descriptionformat, array('context' => $plan->get_context()));
$r->usercompetency = $r->usercompetency->to_record();
}
return $result;
}

/**
* External function return structure.
*
* @return \external_description
*/
public static function list_plan_competencies_returns() {
return new external_multiple_structure(
new external_single_structure(array(
'competency' => self::get_competency_external_structure(),
'usercompetency' => self::get_user_competency_external_structure(),
)
));
}

/**
* Returns the description of the get_scale_values() parameters.
*
Expand Down
114 changes: 114 additions & 0 deletions admin/tool/lp/classes/output/plan_page.php
@@ -0,0 +1,114 @@
<?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/>.

/**
* Plan page output.
*
* @package tool_lp
* @copyright 2015 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_lp\output;

use renderable;
use templatable;
use stdClass;
use tool_lp\api;
use tool_lp\user_competency;
use context_user;

/**
* Plan page class.
*
* @package tool_lp
* @copyright 2015 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class plan_page implements renderable, templatable {

/** @var plan */
protected $plan;

/**
* Construct.
*
* @param plan $plan
*/
public function __construct($plan) {
$this->plan = $plan;
}

/**
* Export the data.
*
* @param renderer_base $output
* @return stdClass
*/
public function export_for_template(\renderer_base $output) {
$options = array('context' => $this->plan->get_context());
$frameworks = array();
$scales = array();

$data = new stdClass();
$data->competencies = array();

$pclist = api::list_plan_competencies($this->plan);
foreach ($pclist as $pc) {
$comp = $pc->competency;
$usercomp = $pc->usercompetency;

if (!isset($frameworks[$comp->get_competencyframeworkid()])) {
$frameworks[$comp->get_competencyframeworkid()] = $comp->get_framework();
}
$framework = $frameworks[$comp->get_competencyframeworkid()];
if (!isset($scales[$framework->get_scaleid()])) {
$scales[$framework->get_scaleid()] = $framework->get_scale();
}
$scale = $scales[$framework->get_scaleid()];

// Prepare the data.
$competency = $comp->to_record();
$competency->descriptionformatted = format_text($competency->description, $competency->descriptionformat, $options);
$usercompetency = $usercomp->to_record();
$competency->usercompetency = $usercompetency;

if ($usercompetency->grade === null) {
$gradename = '-';
} else {
$gradename = format_string($scale->scale_items[$usercompetency->grade - 1], null, $options);
}

if ($usercompetency->proficiency === null) {
$proficiencyname = '-';
} else {
$proficiencyname = get_string($usercompetency->proficiency ? 'yes' : 'no');
}

$statusname = '-';
if ($usercompetency->status != user_competency::STATUS_IDLE) {
$statusname = (string) user_competency::get_status_name($usercompetency->status);
}

$usercompetency->gradename = $gradename;
$usercompetency->proficiencyname = $proficiencyname;
$usercompetency->statusname = $statusname;

$data->competencies[] = $competency;
}

return $data;
}
}
11 changes: 11 additions & 0 deletions admin/tool/lp/classes/output/renderer.php
Expand Up @@ -98,6 +98,17 @@ public function render_manage_templates_page(manage_templates_page $page) {
return parent::render_from_template('tool_lp/manage_templates_page', $data);
}

/**
* Defer to template.
*
* @param plan_page $page
* @return bool|string
*/
public function render_plan_page(plan_page $page) {
$data = $page->export_for_template($this);
return parent::render_from_template('tool_lp/plan_page', $data);
}

/**
* Defer to template.
*
Expand Down
4 changes: 2 additions & 2 deletions admin/tool/lp/classes/persistent.php
Expand Up @@ -629,7 +629,7 @@ final public function get_errors() {
* @param int $skip Limitstart.
* @param int $limit Number of rows to return.
*
* @return persistent[]
* @return \tool_lp\persistent[]
*/
public static function get_records($filters = array(), $sort = '', $order = 'ASC', $skip = 0, $limit = 0) {
global $DB;
Expand Down Expand Up @@ -658,7 +658,7 @@ public static function get_records($filters = array(), $sort = '', $order = 'ASC
* @param string $fields
* @param int $limitfrom
* @param int $limitnum
* @return \tool_lp\plan[]
* @return \tool_lp\persistent[]
*/
public static function get_records_select($select, $params = null, $sort = '', $fields = '*', $limitfrom = 0, $limitnum = 0) {
global $DB;
Expand Down
27 changes: 27 additions & 0 deletions admin/tool/lp/classes/plan.php
Expand Up @@ -109,6 +109,33 @@ public function can_read() {
return self::can_read_user($this->get_userid());
}

/**
* Get the competencies in this plan.
*
* @return competency[]
*/
public function get_competencies() {
$competencies = array();
if ($this->get_templateid()) {
// Get the competencies from the template.
$competencies = template_competency::list_competencies($this->get_templateid(), true);
} else {
// TODO MDL-50328.
// Get the competencies in this plan.
// $competencies = plan_competency::list_competencies($this->get_id());
}
return $competencies;
}

/**
* Get the context in which the plan is attached.
*
* @return context_user
*/
public function get_context() {
return context_user::instance($this->get_userid());
}

/**
* Human readable status name.
*
Expand Down

0 comments on commit 1d8f0a6

Please sign in to comment.