Skip to content

Commit

Permalink
MDL-50328 tool_lp: Unit test failure and little clean-up of plans page
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic Massart committed Apr 18, 2016
1 parent 554b369 commit 8ff9ae8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 52 deletions.
45 changes: 20 additions & 25 deletions admin/tool/lp/classes/api.php
Expand Up @@ -520,7 +520,9 @@ public static function count_frameworks($context, $includes) {
/**
* Fetches all the relevant contexts.
*
* Note: This currently only supports system and category contexts.
* Note: This currently only supports system, category and user contexts. However user contexts
* behave a bit differently and will fallback on the system context. This is what makes the most
* sense because a user context does not have descendants, and only has system as a parent.
*
* @param context $context The context to start from.
* @param string $includes Defines what other contexts to find.
Expand All @@ -537,9 +539,10 @@ public static function get_related_contexts($context, $includes, array $hasanyca
if (!in_array($includes, array('children', 'parents', 'self'))) {
throw new coding_exception('Invalid parameter value for \'includes\'.');
}

// If context user swap it for the context_system.
if ($context->contextlevel == CONTEXT_USER) {
$context = $systemcontext = context_system::instance();
$context = context_system::instance();
}

$contexts = array($context->id => $context);
Expand Down Expand Up @@ -1364,27 +1367,25 @@ public static function list_plan_competencies($planorid) {
* @return bool
*/
public static function add_competency_to_plan($planid, $competencyid) {
// First we do a permissions check.
$plan = new plan($planid);
$plan->read();

// First we do a permissions check.
if (!$plan->can_manage()) {
$context = context_user::instance($plan->get_userid());
throw new required_capability_exception($context, 'tool/lp:planmanage', 'nopermissions', '');
}
throw new required_capability_exception($plan->get_context(), 'tool/lp:planmanage', 'nopermissions', '');

if ($plan->is_based_on_template()) {
} else if ($plan->is_based_on_template()) {
throw new coding_exception('A competency can not be added to a learning plan based on a template');
}

$record = new stdClass();
$record->planid = $planid;
$record->competencyid = $competencyid;

$exists = plan_competency::get_records(array('planid' => $planid, 'competencyid' => $competencyid));
$exists = plan_competency::get_record(array('planid' => $planid, 'competencyid' => $competencyid));
if (!$exists) {
$record = new stdClass();
$record->planid = $planid;
$record->competencyid = $competencyid;
$plancompetency = new plan_competency(0, $record);
$plancompetency->create();
}

return true;
}

Expand All @@ -1396,22 +1397,17 @@ public static function add_competency_to_plan($planid, $competencyid) {
* @return bool
*/
public static function remove_competency_from_plan($planid, $competencyid) {
// First we do a permissions check.
$plan = new plan($planid);
$plan->read();

// First we do a permissions check.
if (!$plan->can_manage()) {
$context = context_user::instance($plan->get_userid());
throw new required_capability_exception($context, 'tool/lp:planmanage', 'nopermissions', '');
}

if ($plan->is_based_on_template()) {
} else if ($plan->is_based_on_template()) {
throw new coding_exception('A competency can not be removed from a learning plan based on a template');
}

$record = new stdClass();
$record->planid = $planid;
$record->competencyid = $competencyid;

$link = plan_competency::get_record(array('planid' => $planid, 'competencyid' => $competencyid));
if ($link) {
return $link->delete();
Expand All @@ -1430,15 +1426,14 @@ public static function remove_competency_from_plan($planid, $competencyid) {
* @return boolean
*/
public static function reorder_plan_competency($planid, $competencyidfrom, $competencyidto) {
// First we do a permissions check.
$plan = new plan($planid);
$plan->read();

// First we do a permissions check.
if (!$plan->can_manage()) {
$context = context_user::instance($plan->get_userid());
throw new required_capability_exception($context, 'tool/lp:planmanage', 'nopermissions', '');
}

if ($plan->is_based_on_template()) {
} else if ($plan->is_based_on_template()) {
throw new coding_exception('A competency can not be reordered in a learning plan based on a template');
}

Expand Down
48 changes: 24 additions & 24 deletions admin/tool/lp/classes/external.php
Expand Up @@ -868,9 +868,10 @@ protected static function get_competency_external_structure($includerelated = fa
/**
* Returns the external structure of a full user_competency record.
*
* @param int $fordisplay When true, additional fields for display purposes will be added.
* @return \external_single_structure
*/
protected static function get_user_competency_external_structure() {
protected static function get_user_competency_external_structure($fordisplay = false) {
$id = new external_value(
PARAM_INT,
'Database record id'
Expand Down Expand Up @@ -912,20 +913,6 @@ protected static function get_user_competency_external_structure() {
'User who modified this record last'
);

// Extra params.
$gradename = new external_value(
PARAM_TEXT,
'User competency status name'
);
$proficiencyname = new external_value(
PARAM_TEXT,
'User competency proficiency name'
);
$statusname = new external_value(
PARAM_TEXT,
'User competency status name'
);

$returns = array(
'id' => $id,
'userid' => $userid,
Expand All @@ -937,11 +924,26 @@ protected static function get_user_competency_external_structure() {
'timecreated' => $timecreated,
'timemodified' => $timemodified,
'usermodified' => $usermodified,
'gradename' => $gradename,
'proficiencyname' => $proficiencyname,
'statusname' => $statusname,
);

if ($fordisplay) {
$gradename = new external_value(
PARAM_TEXT,
'User competency status name'
);
$proficiencyname = new external_value(
PARAM_TEXT,
'User competency proficiency name'
);
$statusname = new external_value(
PARAM_TEXT,
'User competency status name'
);
$returns['gradename'] = $gradename;
$returns['proficiencyname'] = $proficiencyname;
$returns['statusname'] = $statusname;
}

return new external_single_structure($returns);
}

Expand Down Expand Up @@ -3210,18 +3212,16 @@ public static function data_for_plan_competencies_page($planid) {
* @return \external_description
*/
public static function data_for_plan_competencies_page_returns() {

return new external_single_structure(array (
'planid' => new external_value(PARAM_INT, 'Learning Plan id'),
'canmanage' => new external_value(PARAM_BOOL, 'User can manage learning plan'),
'competencies' => new external_multiple_structure(
new external_single_structure(array(
'competency' => self::get_competency_external_structure(),
'usercompetency' => self::get_user_competency_external_structure(),
))
new external_single_structure(array(
'competency' => self::get_competency_external_structure(),
'usercompetency' => self::get_user_competency_external_structure(true),
))
)
));

}

/**
Expand Down
3 changes: 1 addition & 2 deletions admin/tool/lp/classes/persistent.php
Expand Up @@ -653,8 +653,7 @@ public static function get_records($filters = array(), $sort = '', $order = 'ASC
* Load a single record.
*
* @param array $filters Filters to apply.
*
* @return \tool_lp\persistent
* @return false|\tool_lp\persistent
*/
public static function get_record($filters = array()) {
global $DB;
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/lp/version.php
Expand Up @@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();


$plugin->version = 2015052425; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2015052426; // 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 8ff9ae8

Please sign in to comment.