Merge branch 'MDL-62079-master' of git://github.com/andrewnicols/moodle
[moodle.git] / question / classes / external / question_summary_exporter.php
CommitLineData
81f43bc6
RW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Class for exporting a question summary from an stdClass.
19 *
20 * @package core_question
21 * @copyright 2018 Ryan Wyllie <ryan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace core_question\external;
25defined('MOODLE_INTERNAL') || die();
26
27use \renderer_base;
28
29/**
30 * Class for exporting a question summary from an stdClass.
31 *
32 * @copyright 2018 Ryan Wyllie <ryan@moodle.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class question_summary_exporter extends \core\external\exporter {
36
37 /**
38 * @var \stdClass $question
39 */
40 protected $question;
41
42 /**
43 * Constructor.
44 *
45 * @param \stdClass $question
46 * @param array $related The related data.
47 */
48 public function __construct(\stdClass $question, $related = []) {
49 $this->question = $question;
50 return parent::__construct($question, $related);
51 }
52
53 /**
54 * Set the moodle context as a required related object.
55 *
56 * @return array Required related objects.
57 */
58 protected static function define_related() {
59 return ['context' => '\\context'];
60 }
61
62 /**
63 * The list of mandatory properties required on the question object to
64 * export.
65 *
66 * @return string[] List of properties.
67 */
68 public static function get_mandatory_properties() {
69 $properties = self::define_properties();
70 $mandatoryproperties = array_filter($properties, function($property) {
71 return empty($property['optional']);
72 });
73 return array_keys($mandatoryproperties);
74 }
75
76 /**
77 * The list of static properties returned.
78 *
79 * @return array List of properties.
80 */
81 public static function define_properties() {
82 return [
83 'id' => [
84 'type' => PARAM_INT,
85 ],
86 'category' => [
87 'type' => PARAM_INT,
88 ],
89 'parent' => [
90 'type' => PARAM_INT,
91 ],
92 'name' => [
93 'type' => PARAM_TEXT,
94 ],
95 'qtype' => [
96 'type' => PARAM_COMPONENT,
97 ]
98 ];
99 }
100
101 /**
102 * Define the list of calculated properties.
103 *
104 * @return array The list of properties.
105 */
106 protected static function define_other_properties() {
107 return [
108 'icon' => [
109 'type' => question_icon_exporter::read_properties_definition(),
110 ]
111 ];
112 }
113
114 /**
115 * Calculate the values for the properties defined in the define_other_properties
116 * function.
117 *
118 * @param renderer_base $output A renderer.
119 * @return array The list of properties.
120 */
121 protected function get_other_values(\renderer_base $output) {
122 $iconexporter = new question_icon_exporter($this->question, $this->related);
123
124 return [
125 'icon' => $iconexporter->export($output),
126 ];
127 }
128}