Skip to content

Commit

Permalink
MDL-51041 cbe: Refactor to allow non-persistent exporters
Browse files Browse the repository at this point in the history
  • Loading branch information
Damyon Wiese authored and Frederic Massart committed Apr 18, 2016
1 parent cc8348d commit d04ea16
Show file tree
Hide file tree
Showing 11 changed files with 609 additions and 331 deletions.
3 changes: 2 additions & 1 deletion admin/tool/lp/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use invalid_parameter_exception;
use grade_scale;
use tool_lp\external\competency_framework_exporter;
use tool_lp\external\competency_with_linked_courses_exporter;
use tool_lp\external\user_competency_exporter;
use tool_lp\external\user_competency_plan_exporter;
use tool_lp\external\competency_exporter;
Expand Down Expand Up @@ -2640,7 +2641,7 @@ public static function data_for_template_competencies_page_returns() {
'canmanagecompetencyframeworks' => new external_value(PARAM_BOOL, 'User can manage competency frameworks'),
'canmanagetemplates' => new external_value(PARAM_BOOL, 'User can manage learning plan templates'),
'competencies' => new external_multiple_structure(
competency_exporter::get_read_structure()
competency_with_linked_courses_exporter::get_read_structure()
),
'manageurl' => new external_value(PARAM_LOCALURL, 'Url to the manage competencies page.'),
));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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/>.

/**
* Class for exporting competency data with the set of linked courses.
*
* @package tool_lp
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_lp\external;

use context_course;
use renderer_base;
use stdClass;

/**
* Class for exporting competency data.
*
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class competency_with_linked_courses_exporter extends exporter {

protected static function define_related() {
// We cache the context so it does not need to be retrieved from the framework every time.
return array('context' => '\\context',
'competency' => '\\tool_lp\\competency',
'linkedcourses' => '\\stdClass[]');
}

protected static function define_other_properties() {
return array(
'linkedcourses' => array(
'type' => course_summary_exporter::properties_definition(true),
'multiple' => true
),
'competency' => array(
'type' => competency_exporter::properties_definition(true)
),
'hascourses' => array(
'type' => PARAM_BOOL
)
);
}

protected function get_other_values(renderer_base $output) {
$result = new stdClass();

$courses = $this->related['linkedcourses'];
$linkedcourses = array();
foreach ($courses as $course) {
$context = context_course::instance($course->id);
$exporter = new course_summary_exporter($course, array('context' => $context));
$courseexport = $exporter->export($output);
array_push($linkedcourses, $courseexport);
}
$result->linkedcourses = $linkedcourses;
$result->hascourses = count($linkedcourses) > 0;

$competency = $this->related['competency'];
$context = $this->related['context'];
$exporter = new competency_exporter($competency, array('context' => $context));
$result->competency = $exporter->export($output);

return (array) $result;
}
}
72 changes: 72 additions & 0 deletions admin/tool/lp/classes/external/course_summary_exporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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/>.

/**
* Class for exporting a course summary from an stdClass.
*
* @package tool_lp
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_lp\external;

use renderer_base;
use moodle_url;

/**
* Class for exporting a course summary from an stdClass.
*
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_summary_exporter extends exporter {

protected static function define_related() {
// We cache the context so it does not need to be retrieved from the course.
return array('context' => '\\context');
}

protected function get_other_values(renderer_base $output) {
return array(
'viewurl' => new moodle_url('course/view.php', array('id' => $this->data->id))
);
}

public static function define_properties() {
return array(
'id' => array(
'type' => PARAM_INT,
),
'fullname' => array(
'type' => PARAM_TEXT,
),
'shortname' => array(
'type' => PARAM_TEXT,
),
'idnumber' => array(
'type' => PARAM_TEXT,
)
);
}

public static function define_other_properties() {
return array(
'viewurl' => array(
'type' => PARAM_URL,
)
);
}
}
Loading

0 comments on commit d04ea16

Please sign in to comment.