Skip to content

Commit

Permalink
MDL-57273 core: Exporters support custom formatting parameters
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 268beda commit 4bc68a4
Show file tree
Hide file tree
Showing 16 changed files with 334 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected static function define_other_properties() {
'type' => path_node_exporter::read_properties_definition()
],
'pluginbaseurl' => [
'type' => PARAM_TEXT
'type' => PARAM_URL
],
'pagecontextid' => [
'type' => PARAM_INT
Expand Down
29 changes: 29 additions & 0 deletions admin/tool/lp/classes/external/path_node_exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace tool_lp\external;
defined('MOODLE_INTERNAL') || die();

use context_system;

/**
* Class for exporting path_node data.
Expand All @@ -33,6 +34,34 @@
*/
class path_node_exporter extends \core\external\exporter {

/**
* Constructor - saves the persistent object, and the related objects.
*
* @param mixed $data The data.
* @param array $related Array of relateds.
*/
public function __construct($data, $related = array()) {
if (!isset($related['context'])) {
// Previous code was automatically using the system context which was not always correct.
// We let developers know that they must fix their code without breaking anything,
// and fallback on the previous behaviour. This should be removed at a later stage: Moodle 3.5.
debugging('Missing related context in path_node_exporter.', DEBUG_DEVELOPER);
$related['context'] = context_system::instance();
}
parent::__construct($data, $related);
}

/**
* Return the list of properties.
*
* @return array
*/
protected static function define_related() {
return [
'context' => 'context'
];
}

/**
* Return the list of properties.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ protected function get_other_values(renderer_base $output) {
'scale' => $scale,
'usercompetency' => ($this->related['usercompetency'] ? $this->related['usercompetency'] : null),
'usercompetencyplan' => ($this->related['usercompetencyplan'] ? $this->related['usercompetencyplan'] : null),
'context' => $evidence->get_context()
);
$related['actionuser'] = !empty($actionuserid) ? $usercache[$actionuserid] : null;
$exporter = new evidence_exporter($evidence, $related);
Expand Down
18 changes: 18 additions & 0 deletions admin/tool/lp/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,24 @@ public function test_data_for_user_competency_summary_in_plan() {
$this->assertEquals('A', $summary->usercompetencysummary->evidence[1]->gradename);
}

public function test_data_for_user_competency_summary() {
$this->setUser($this->creator);

$dg = $this->getDataGenerator();
$lpg = $dg->get_plugin_generator('core_competency');
$f1 = $lpg->create_framework();
$c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));

$evidence = \core_competency\external::grade_competency($this->user->id, $c1->get_id(), 1, true);
$evidence = \core_competency\external::grade_competency($this->user->id, $c1->get_id(), 2, true);

$summary = external::data_for_user_competency_summary($this->user->id, $c1->get_id());
$this->assertTrue($summary->cangrade);
$this->assertEquals('B', $summary->usercompetency->gradename);
$this->assertEquals('B', $summary->evidence[0]->gradename);
$this->assertEquals('A', $summary->evidence[1]->gradename);
}

/**
* Search cohorts.
*/
Expand Down
9 changes: 9 additions & 0 deletions competency/classes/evidence.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ public function get_competency() {
return user_competency::get_competency_by_usercompetencyid($this->get_usercompetencyid());
}

/**
* Return the evidence's context.
*
* @return context
*/
public function get_context() {
return context::instance_by_id($this->get('contextid'));
}

/**
* Convenience method to get the description $a.
*
Expand Down
3 changes: 3 additions & 0 deletions competency/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -3709,6 +3709,7 @@ public static function grade_competency($userid, $competencyid, $grade, $note =
'scale' => $scale,
'usercompetency' => $uc,
'usercompetencyplan' => null,
'context' => $evidence->get_context()
]);
return $exporter->export($output);
}
Expand Down Expand Up @@ -3795,6 +3796,7 @@ public static function grade_competency_in_plan($planid, $competencyid, $grade,
'scale' => $scale,
'usercompetency' => null,
'usercompetencyplan' => null,
'context' => $evidence->get_context()
]);
return $exporter->export($output);
}
Expand Down Expand Up @@ -4107,6 +4109,7 @@ public static function grade_competency_in_course($courseid, $userid, $competenc
'scale' => $scale,
'usercompetency' => null,
'usercompetencyplan' => null,
'context' => $evidence->get_context(),
));
return $exporter->export($output);
}
Expand Down
4 changes: 2 additions & 2 deletions competency/classes/external/competency_framework_exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ protected static function define_other_properties() {
'type' => PARAM_INT
),
'contextname' => array(
'type' => PARAM_TEXT
'type' => PARAM_RAW
),
'contextnamenoprefix' => array(
'type' => PARAM_TEXT
'type' => PARAM_RAW
)
);
}
Expand Down
34 changes: 32 additions & 2 deletions competency/classes/external/evidence_exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace core_competency\external;
defined('MOODLE_INTERNAL') || die();

use context_system;
use renderer_base;
use core_competency\evidence;
use core_competency\user_competency;
Expand All @@ -37,9 +38,27 @@
*/
class evidence_exporter extends \core\external\persistent_exporter {

/**
* Constructor.
*
* @param mixed $data The data.
* @param array $related Array of relateds.
*/
public function __construct($data, $related = array()) {
if (!isset($related['context'])) {
// Previous code was automatically using the system context which was not correct.
// We let developers know that they must fix their code without breaking anything, and
// fallback on the previous behaviour. This should be removed at a later stage: Moodle 3.5.
debugging('Missing related context in evidence_exporter.', DEBUG_DEVELOPER);
$related['context'] = context_system::instance();
}
parent::__construct($data, $related);
}

protected static function define_related() {
return array(
'actionuser' => 'stdClass?',
'context' => 'context',
'scale' => 'grade_scale',
'usercompetency' => 'core_competency\\user_competency?',
'usercompetencyplan' => 'core_competency\\user_competency_plan?',
Expand Down Expand Up @@ -85,20 +104,31 @@ protected function get_other_values(renderer_base $output) {
return $other;
}

/**
* Get the format parameters for gradename.
*
* @return array
*/
protected function get_format_parameters_for_gradename() {
return [
'context' => context_system::instance(), // The system context is cached, so we can get it right away.
];
}

public static function define_other_properties() {
return array(
'actionuser' => array(
'type' => user_summary_exporter::read_properties_definition(),
'optional' => true
),
'description' => array(
'type' => PARAM_TEXT,
'type' => PARAM_TEXT, // The description may contain course names, etc.. which may need filtering.
),
'gradename' => array(
'type' => PARAM_TEXT,
),
'userdate' => array(
'type' => PARAM_TEXT
'type' => PARAM_NOTAGS
),
'candelete' => array(
'type' => PARAM_BOOL
Expand Down
13 changes: 12 additions & 1 deletion competency/classes/external/user_competency_course_exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace core_competency\external;
defined('MOODLE_INTERNAL') || die();

use core_user;
use context_system;
use renderer_base;
use stdClass;

Expand Down Expand Up @@ -65,6 +65,17 @@ protected function get_other_values(renderer_base $output) {
return (array) $result;
}

/**
* Get the format parameters for gradename.
*
* @return array
*/
protected function get_format_parameters_for_gradename() {
return [
'context' => context_system::instance(), // The system context is cached, so we can get it right away.
];
}

protected static function define_other_properties() {
return array(
'gradename' => array(
Expand Down
12 changes: 12 additions & 0 deletions competency/classes/external/user_competency_exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace core_competency\external;
defined('MOODLE_INTERNAL') || die();

use context_system;
use core_user;
use renderer_base;
use stdClass;
Expand Down Expand Up @@ -94,6 +95,17 @@ protected function get_other_values(renderer_base $output) {
return (array) $result;
}

/**
* Get the format parameters for gradename.
*
* @return array
*/
protected function get_format_parameters_for_gradename() {
return [
'context' => context_system::instance(), // The system context is cached, so we can get it right away.
];
}

protected static function define_other_properties() {
return array(
'canrequestreview' => array(
Expand Down
12 changes: 12 additions & 0 deletions competency/classes/external/user_competency_plan_exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace core_competency\external;
defined('MOODLE_INTERNAL') || die();

use context_system;
use renderer_base;
use stdClass;

Expand Down Expand Up @@ -64,6 +65,17 @@ protected function get_other_values(renderer_base $output) {
return (array) $result;
}

/**
* Get the format parameters for gradename.
*
* @return array
*/
protected function get_format_parameters_for_gradename() {
return [
'context' => context_system::instance(), // The system context is cached, so we can get it right away.
];
}

protected static function define_other_properties() {
return array(
'gradename' => array(
Expand Down
47 changes: 47 additions & 0 deletions competency/tests/external_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2681,6 +2681,53 @@ public function test_fix_sortorder_when_moving_competency() {
$this->assertEquals(2, $c2a->get_sortorder());
}

public function test_grade_competency() {
global $CFG;

$this->setUser($this->creator);
$dg = $this->getDataGenerator();
$lpg = $dg->get_plugin_generator('core_competency');

$f1 = $lpg->create_framework();
$c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
$evidence = external::grade_competency($this->user->id, $c1->get_id(), 1, 'Evil note');

$this->assertEquals('The competency rating was manually set.', $evidence->description);
$this->assertEquals('A', $evidence->gradename);
$this->assertEquals('Evil note', $evidence->note);

$this->setUser($this->user);

$this->expectException('required_capability_exception');
$evidence = external::grade_competency($this->user->id, $c1->get_id(), 1);
}

public function test_grade_competency_in_course() {
global $CFG;

$this->setUser($this->creator);
$dg = $this->getDataGenerator();
$lpg = $dg->get_plugin_generator('core_competency');

$course = $dg->create_course(['fullname' => 'Evil course']);
$dg->enrol_user($this->creator->id, $course->id, 'editingteacher');
$dg->enrol_user($this->user->id, $course->id, 'student');
$f1 = $lpg->create_framework();
$c1 = $lpg->create_competency(['competencyframeworkid' => $f1->get_id()]);
$lpg->create_course_competency(['courseid' => $course->id, 'competencyid' => $c1->get_id()]);

$evidence = external::grade_competency_in_course($course->id, $this->user->id, $c1->get_id(), 1, 'Evil note');

$this->assertEquals('The competency rating was manually set in the course \'Course: Evil course\'.', $evidence->description);
$this->assertEquals('A', $evidence->gradename);
$this->assertEquals('Evil note', $evidence->note);

$this->setUser($this->user);

$this->expectException('required_capability_exception');
$evidence = external::grade_competency_in_course($course->id, $this->user->id, $c1->get_id(), 1);
}

public function test_grade_competency_in_plan() {
global $CFG;

Expand Down
Loading

0 comments on commit 4bc68a4

Please sign in to comment.