Skip to content

Commit

Permalink
MDL-57273 core: Added a performance helper to use with exporters
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic Massart authored and Damyon Wiese committed Jan 20, 2017
1 parent 6e28143 commit 476bf96
Show file tree
Hide file tree
Showing 14 changed files with 371 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use renderer_base;
use moodle_url;
use core_competency\external\competency_exporter;
use core_competency\external\performance_helper;

/**
* Class for exporting a course competency statistics summary.
Expand Down Expand Up @@ -84,12 +85,9 @@ protected function get_other_values(renderer_base $output) {
$proficientcompetencypercentageformatted = format_float($proficientcompetencypercentage);
}
$competencies = array();
$contextcache = array();
$helper = new performance_helper();
foreach ($this->data->leastproficientcompetencies as $competency) {
if (!isset($contextcache[$competency->get_competencyframeworkid()])) {
$contextcache[$competency->get_competencyframeworkid()] = $competency->get_context();
}
$context = $contextcache[$competency->get_competencyframeworkid()];
$context = $helper->get_context_from_competency($competency);
$exporter = new competency_exporter($competency, array('context' => $context));
$competencies[] = $exporter->export($output);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use renderer_base;
use moodle_url;
use core_competency\external\competency_exporter;
use core_competency\external\performance_helper;

/**
* Class for exporting a cohort summary from an stdClass.
Expand Down Expand Up @@ -118,12 +119,9 @@ protected function get_other_values(renderer_base $output) {
$proficientusercompetencyplanpercentageformatted = format_float($proficientusercompetencyplanpercentage);
}
$competencies = array();
$contextcache = array();
$helper = new performance_helper();
foreach ($this->data->leastproficientcompetencies as $competency) {
if (!isset($contextcache[$competency->get_competencyframeworkid()])) {
$contextcache[$competency->get_competencyframeworkid()] = $competency->get_context();
}
$context = $contextcache[$competency->get_competencyframeworkid()];
$context = $helper->get_context_from_competency($competency);
$exporter = new competency_exporter($competency, array('context' => $context));
$competencies[] = $exporter->export($output);
}
Expand Down
24 changes: 6 additions & 18 deletions admin/tool/lp/classes/external/user_evidence_summary_exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use moodle_url;
use renderer_base;
use core_competency\external\stored_file_exporter;
use core_competency\external\performance_helper;

/**
* Class for exporting user evidence with all competencies.
Expand Down Expand Up @@ -91,32 +92,19 @@ protected function get_other_values(renderer_base $output) {
}

$userevidencecompetencies = array();
$frameworks = array();
$scales = array();
$usercompetencies = $this->persistent->get_user_competencies();
$helper = new performance_helper();
foreach ($usercompetencies as $usercompetency) {
$competency = $usercompetency->get_competency();

// Get the framework.
if (!isset($frameworks[$competency->get_competencyframeworkid()])) {
$frameworks[$competency->get_competencyframeworkid()] = $competency->get_framework();
}
$framework = $frameworks[$competency->get_competencyframeworkid()];

// Get the scale.
$scaleid = $competency->get_scaleid();
if ($scaleid === null) {
$scaleid = $framework->get_scaleid();
}
if (!isset($scales[$framework->get_scaleid()])) {
$scales[$framework->get_scaleid()] = $framework->get_scale();
}
$scale = $scales[$framework->get_scaleid()];
$context = $helper->get_context_from_competency($competency);
$framework = $helper->get_framework_from_competency($competency);
$scale = $helper->get_scale_from_competency($competency);

$related = array('competency' => $competency,
'usercompetency' => $usercompetency,
'scale' => $scale,
'context' => $framework->get_context());
'context' => $context);

$userevidencecompetencysummaryexporter = new user_evidence_competency_summary_exporter(null, $related);

Expand Down
11 changes: 4 additions & 7 deletions admin/tool/lp/classes/output/competency_plan_navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
use renderer_base;
use templatable;
use context_course;
use \core_competency\external\competency_exporter;
use core_competency\external\competency_exporter;
use core_competency\external\performance_helper;
use stdClass;

/**
Expand Down Expand Up @@ -82,13 +83,9 @@ public function export_for_template(renderer_base $output) {

$plancompetencies = \core_competency\api::list_plan_competencies($data->planid);
$data->competencies = array();
$contextcache = array();
$helper = new performance_helper();
foreach ($plancompetencies as $plancompetency) {
$frameworkid = $plancompetency->competency->get_competencyframeworkid();
if (!isset($contextcache[$frameworkid])) {
$contextcache[$frameworkid] = $plancompetency->competency->get_context();
}
$context = $contextcache[$frameworkid];
$context = $helper->get_context_from_competency($plancompetency->competency);
$exporter = new competency_exporter($plancompetency->competency, array('context' => $context));
$competency = $exporter->export($output);
if ($competency->id == $this->competencyid) {
Expand Down
12 changes: 5 additions & 7 deletions admin/tool/lp/classes/output/course_competencies_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use tool_lp\course_competency_statistics;
use core_competency\competency;
use core_competency\course_competency;
use core_competency\external\performance_helper;
use core_competency\external\competency_exporter;
use core_competency\external\course_competency_exporter;
use core_competency\external\course_competency_settings_exporter;
Expand Down Expand Up @@ -112,7 +113,6 @@ public function export_for_template(renderer_base $output) {
$data->courseid = $this->courseid;
$data->pagecontextid = $this->context->id;
$data->competencies = array();
$contextcache = array();

$gradable = is_enrolled($this->context, $USER, 'moodle/competency:coursecompetencygradable');
if ($gradable) {
Expand All @@ -126,13 +126,11 @@ public function export_for_template(renderer_base $output) {
$ruleoutcomeoptions[$value] = array('value' => $value, 'text' => (string) $text, 'selected' => false);
}

$helper = new performance_helper();
foreach ($this->coursecompetencylist as $coursecompetencyelement) {
$coursecompetency = $coursecompetencyelement['coursecompetency'];
$competency = $coursecompetencyelement['competency'];
if (!isset($contextcache[$competency->get_competencyframeworkid()])) {
$contextcache[$competency->get_competencyframeworkid()] = $competency->get_context();
}
$context = $contextcache[$competency->get_competencyframeworkid()];
$context = $helper->get_context_from_competency($competency);

$compexporter = new competency_exporter($competency, array('context' => $context));
$ccexporter = new course_competency_exporter($coursecompetency, array('context' => $context));
Expand All @@ -152,7 +150,7 @@ public function export_for_template(renderer_base $output) {
// Competency path.
$pathexporter = new competency_path_exporter([
'ancestors' => $competency->get_ancestors(),
'framework' => $competency->get_framework(),
'framework' => $helper->get_framework_from_competency($competency),
'context' => $context
]);

Expand All @@ -172,7 +170,7 @@ public function export_for_template(renderer_base $output) {
}
if ($foundusercompetencycourse) {
$related = array(
'scale' => $competency->get_scale()
'scale' => $helper->get_scale_from_competency($competency)
);
$exporter = new user_competency_course_exporter($foundusercompetencycourse, $related);
$onerow['usercompetencycourse'] = $exporter->export($output);
Expand Down
29 changes: 7 additions & 22 deletions admin/tool/lp/classes/output/plan_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use stdClass;
use moodle_url;
use core_competency\api;
use core_competency\external\performance_helper;
use core_competency\plan;
use core_competency\external\competency_exporter;
use core_competency\external\plan_exporter;
Expand Down Expand Up @@ -62,9 +63,6 @@ public function __construct($plan) {
* @return stdClass
*/
public function export_for_template(\renderer_base $output) {
$frameworks = array();
$scales = array();

$planexporter = new plan_exporter($this->plan, array('template' => $this->plan->get_template()));

$data = new stdClass();
Expand All @@ -81,40 +79,27 @@ public function export_for_template(\renderer_base $output) {
$ucexporter = 'core_competency\\external\\user_competency_exporter';
}

$helper = new performance_helper();
$pclist = api::list_plan_competencies($this->plan);
$proficientcount = 0;
foreach ($pclist as $pc) {
$comp = $pc->competency;
$usercomp = $pc->$ucproperty;

// Get the framework.
if (!isset($frameworks[$comp->get_competencyframeworkid()])) {
$frameworks[$comp->get_competencyframeworkid()] = $comp->get_framework();
}
$framework = $frameworks[$comp->get_competencyframeworkid()];

// Get the scale.
$scaleid = $comp->get_scaleid();
$compscale = $comp->get_scale();
if ($scaleid === null) {
$scaleid = $framework->get_scaleid();
$compscale = $framework->get_scale();
}
if (!isset($scales[$scaleid])) {
$scales[$scaleid] = $compscale;
}
$scale = $scales[$scaleid];
$compcontext = $helper->get_context_from_competency($comp);
$framework = $helper->get_framework_from_competency($comp);
$scale = $helper->get_scale_from_competency($comp);

// Prepare the data.
$record = new stdClass();
$exporter = new competency_exporter($comp, array('context' => $framework->get_context()));
$exporter = new competency_exporter($comp, array('context' => $compcontext));
$record->competency = $exporter->export($output);

// Competency path.
$exporter = new competency_path_exporter([
'ancestors' => $comp->get_ancestors(),
'framework' => $framework,
'context' => $framework->get_context()
'context' => $compcontext
]);
$record->comppath = $exporter->export($output);

Expand Down
14 changes: 4 additions & 10 deletions admin/tool/lp/classes/output/template_competencies_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use core_competency\external\template_exporter;
use core_competency\template;
use core_competency\api;
use core_competency\external\performance_helper;
use tool_lp\external\competency_summary_exporter;
use tool_lp\external\template_statistics_exporter;
use tool_lp\template_statistics;
Expand Down Expand Up @@ -95,17 +96,10 @@ public function export_for_template(renderer_base $output) {
$data->template = (new template_exporter($this->template))->export($output);
$data->pagecontextid = $this->pagecontext->id;
$data->competencies = array();
$contextcache = array();
$frameworkcache = array();
$helper = new performance_helper();
foreach ($this->competencies as $competency) {
if (!isset($contextcache[$competency->get_competencyframeworkid()])) {
$contextcache[$competency->get_competencyframeworkid()] = $competency->get_context();
}
$context = $contextcache[$competency->get_competencyframeworkid()];
if (!isset($frameworkcache[$competency->get_competencyframeworkid()])) {
$frameworkcache[$competency->get_competencyframeworkid()] = $competency->get_framework();
}
$framework = $frameworkcache[$competency->get_competencyframeworkid()];
$context = $helper->get_context_from_competency($competency);
$framework = $helper->get_framework_from_competency($competency);

$courses = api::list_courses_using_competency($competency->get_id());
$relatedcompetencies = api::list_related_competencies($competency->get_id());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
use renderer_base;
use templatable;
use context_course;
use \core_competency\external\competency_exporter;
use \core_competency\external\user_summary_exporter;
use core_competency\external\competency_exporter;
use core_competency\external\user_summary_exporter;
use core_competency\external\performance_helper;
use stdClass;

/**
Expand Down Expand Up @@ -118,14 +119,9 @@ public function export_for_template(renderer_base $output) {

$coursecompetencies = \core_competency\api::list_course_competencies($this->courseid);
$data->competencies = array();
$contextcache = array();
$helper = new performance_helper();
foreach ($coursecompetencies as $coursecompetency) {
$frameworkid = $coursecompetency['competency']->get_competencyframeworkid();
if (!isset($contextcache[$frameworkid])) {
$contextcache[$frameworkid] = $coursecompetency['competency']->get_context();
}
$context = $contextcache[$frameworkid];
$coursecompetencycontext = $context;
$coursecompetencycontext = $helper->get_context_from_competency($coursecompetency['competency']);
$exporter = new competency_exporter($coursecompetency['competency'], array('context' => $coursecompetencycontext));
$competency = $exporter->export($output);
if ($competency->id == $this->competencyid) {
Expand Down
31 changes: 9 additions & 22 deletions competency/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use core_competency\external\course_competency_exporter;
use core_competency\external\course_competency_settings_exporter;
use core_competency\external\evidence_exporter;
use core_competency\external\performance_helper;
use core_competency\external\plan_exporter;
use core_competency\external\template_exporter;
use core_competency\external\user_competency_exporter;
Expand Down Expand Up @@ -1338,14 +1339,12 @@ public static function list_course_competencies($courseid) {
$result = array();

$contextcache = array();
$helper = new performance_helper();
foreach ($competencies as $competency) {
if (!isset($contextcache[$competency['competency']->get_competencyframeworkid()])) {
$contextcache[$competency['competency']->get_competencyframeworkid()] = $competency['competency']->get_context();
}
$context = $contextcache[$competency['competency']->get_competencyframeworkid()];
$context = $helper->get_context_from_competency($competency['competency']);
$exporter = new competency_exporter($competency['competency'], array('context' => $context));
$competencyrecord = $exporter->export($output);
$exporter = new course_competency_exporter($competency['coursecompetency'], array('context' => $context));
$exporter = new course_competency_exporter($competency['coursecompetency']);
$coursecompetencyrecord = $exporter->export($output);

$result[] = array(
Expand Down Expand Up @@ -2149,13 +2148,10 @@ public static function list_competencies_in_template($templateid) {

$competencies = api::list_competencies_in_template($params['id']);
$results = array();
$contextcache = array();

$helper = new performance_helper();
foreach ($competencies as $competency) {
if (!isset($contextcache[$competency->get_competencyframeworkid()])) {
$contextcache[$competency->get_competencyframeworkid()] = $competency->get_context();
}
$context = $contextcache[$competency->get_competencyframeworkid()];
$context = $helper->get_context_from_competency($competency);
$exporter = new competency_exporter($competency, array('context' => $context));
$record = $exporter->export($output);
array_push($results, $record);
Expand Down Expand Up @@ -3145,19 +3141,10 @@ public static function list_plan_competencies($id) {
$ucproperty = 'usercompetency';
}

$contextcache = array();
$scalecache = array();

$helper = new performance_helper();
foreach ($result as $key => $r) {
if (!isset($scalecache[$r->competency->get_competencyframeworkid()])) {
$scalecache[$r->competency->get_competencyframeworkid()] = $r->competency->get_framework()->get_scale();
}
$scale = $scalecache[$r->competency->get_competencyframeworkid()];

if (!isset($contextcache[$r->competency->get_competencyframeworkid()])) {
$contextcache[$r->competency->get_competencyframeworkid()] = $r->competency->get_context();
}
$context = $contextcache[$r->competency->get_competencyframeworkid()];
$context = $helper->get_context_from_competency($r->competency);
$scale = $helper->get_scale_from_competency($r->competency);

$exporter = new competency_exporter($r->competency, array('context' => $context));
$r->competency = $exporter->export($output);
Expand Down
Loading

0 comments on commit 476bf96

Please sign in to comment.