MDL-51034 tool_lp: List competencies in a user's plan

This commit is contained in:
Frederic Massart
2016-04-18 10:58:40 +08:00
parent 58405003f8
commit 1d8f0a6f50
13 changed files with 489 additions and 7 deletions
+46
View File
@@ -1306,6 +1306,52 @@ class api {
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.
*