Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 feedback item (question).
19
 *
20
 * @package    mod_feedback
21
 * @copyright  2017 Juan Leyva <juan@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
namespace mod_feedback\external;
25
defined('MOODLE_INTERNAL') || die();
26
 
27
use core\external\exporter;
28
use renderer_base;
29
use core_files\external\stored_file_exporter;
30
 
31
/**
32
 * Class for exporting a feedback item (question).
33
 *
34
 * @copyright  2017 Juan Leyva <juan@moodle.com>
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class feedback_item_exporter extends exporter {
38
 
39
    protected static function define_properties() {
40
        return array(
41
            'id' => array(
42
                'type' => PARAM_INT,
43
                'description' => 'The record id.',
44
            ),
45
            'feedback' => array(
46
                'type' => PARAM_INT,
47
                'description' => 'The feedback instance id this records belongs to.',
48
                'default' => 0,
49
            ),
50
            'template' => array(
51
                'type' => PARAM_INT,
52
                'description' => 'If it belogns to a template, the template id.',
53
                'default' => 0,
54
            ),
55
            'name' => array(
56
                'type' => PARAM_RAW,
57
                'description' => 'The item name.',
58
            ),
1441 ariadna 59
            'nameformat' => [
60
                'choices' => [FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN],
61
                'type' => PARAM_INT,
62
                'description' => 'The format of the item name.',
63
                'default' => FORMAT_HTML,
64
            ],
1 efrain 65
            'label' => array(
66
                'type' => PARAM_NOTAGS,
67
                'description' => 'The item label.',
68
            ),
69
            'presentation' => array(
70
                'type' => PARAM_RAW,
71
                'description' => 'The text describing the item or the available possible answers.',
72
            ),
1441 ariadna 73
            'presentationformat' => [
74
                'choices' => [FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN],
75
                'type' => PARAM_INT,
76
                'description' => 'The format of the text describing the item or the available possible answers.',
77
                'default' => FORMAT_HTML,
78
            ],
1 efrain 79
            'typ' => array(
80
                'type' => PARAM_ALPHA,
81
                'description' => 'The type of the item.',
82
            ),
83
            'hasvalue' => array(
84
                'type' => PARAM_INT,
85
                'description' => 'Whether it has a value or not.',
86
                'default' => 0,
87
            ),
88
            'position' => array(
89
                'type' => PARAM_INT,
90
                'description' => 'The position in the list of questions.',
91
                'default' => 0,
92
            ),
93
            'required' => array(
94
                'type' => PARAM_BOOL,
95
                'description' => 'Whether is a item (question) required or not.',
96
                'default' => 0,
97
            ),
98
            'dependitem' => array(
99
                'type' => PARAM_INT,
100
                'description' => 'The item id this item depend on.',
101
                'default' => 0,
102
            ),
103
            'dependvalue' => array(
104
                'type' => PARAM_RAW,
105
                'description' => 'The depend value.',
106
            ),
107
            'options' => array(
108
                'type' => PARAM_ALPHA,
109
                'description' => 'Different additional settings for the item (question).',
110
            ),
111
        );
112
    }
113
 
114
    protected static function define_related() {
115
        return array(
116
            'context' => 'context',
117
            'itemnumber' => 'int?'
118
        );
119
    }
120
 
121
    protected static function define_other_properties() {
122
        return array(
123
            'itemfiles' => array(
124
                'type' => stored_file_exporter::read_properties_definition(),
125
                'multiple' => true
126
            ),
127
            'itemnumber' => array(
128
                'type' => PARAM_INT,
129
                'description' => 'The item position number',
130
                'null' => NULL_ALLOWED
131
            ),
132
            'otherdata' => array(
133
                'type' => PARAM_RAW,
134
                'description' => 'Additional data that may be required by external functions',
135
                'null' => NULL_ALLOWED
136
            ),
137
        );
138
    }
139
 
140
    protected function get_other_values(renderer_base $output) {
141
        $context = $this->related['context'];
142
 
143
        $itemobj = feedback_get_item_class($this->data->typ);
144
        $values = array(
145
            'itemfiles' => array(),
146
            'itemnumber' => $this->related['itemnumber'],
147
            'otherdata' => $itemobj->get_data_for_external($this->data),
148
        );
149
 
150
        $fs = get_file_storage();
151
        $files = array();
152
        $itemfiles = $fs->get_area_files($context->id, 'mod_feedback', 'item', $this->data->id, 'filename', false);
153
        if (!empty($itemfiles)) {
154
            foreach ($itemfiles as $storedfile) {
155
                $fileexporter = new stored_file_exporter($storedfile, array('context' => $context));
156
                $files[] = $fileexporter->export($output);
157
            }
158
            $values['itemfiles'] = $files;
159
        }
160
 
161
        return $values;
162
    }
163
 
164
    /**
165
     * Get the formatting parameters for the name.
166
     *
167
     * @return array
168
     */
169
    protected function get_format_parameters_for_name() {
170
        return [
171
            'component' => 'mod_feedback',
172
            'filearea' => 'item',
1441 ariadna 173
            'itemid' => $this->data->id,
174
            'options' => ['noclean' => true, 'para' => false],
1 efrain 175
        ];
176
    }
177
 
178
    /**
179
     * Get the formatting parameters for the presentation.
180
     *
181
     * @return array
182
     */
183
    protected function get_format_parameters_for_presentation() {
184
        return [
185
            'component' => 'mod_feedback',
186
            'filearea' => 'item',
1441 ariadna 187
            'itemid' => $this->data->id,
188
            'options' => ['noclean' => true, 'para' => false],
1 efrain 189
        ];
190
    }
191
}