Proyectos de Subversion Moodle

Rev

| 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
            ),
59
            'label' => array(
60
                'type' => PARAM_NOTAGS,
61
                'description' => 'The item label.',
62
            ),
63
            'presentation' => array(
64
                'type' => PARAM_RAW,
65
                'description' => 'The text describing the item or the available possible answers.',
66
            ),
67
            'typ' => array(
68
                'type' => PARAM_ALPHA,
69
                'description' => 'The type of the item.',
70
            ),
71
            'hasvalue' => array(
72
                'type' => PARAM_INT,
73
                'description' => 'Whether it has a value or not.',
74
                'default' => 0,
75
            ),
76
            'position' => array(
77
                'type' => PARAM_INT,
78
                'description' => 'The position in the list of questions.',
79
                'default' => 0,
80
            ),
81
            'required' => array(
82
                'type' => PARAM_BOOL,
83
                'description' => 'Whether is a item (question) required or not.',
84
                'default' => 0,
85
            ),
86
            'dependitem' => array(
87
                'type' => PARAM_INT,
88
                'description' => 'The item id this item depend on.',
89
                'default' => 0,
90
            ),
91
            'dependvalue' => array(
92
                'type' => PARAM_RAW,
93
                'description' => 'The depend value.',
94
            ),
95
            'options' => array(
96
                'type' => PARAM_ALPHA,
97
                'description' => 'Different additional settings for the item (question).',
98
            ),
99
        );
100
    }
101
 
102
    protected static function define_related() {
103
        return array(
104
            'context' => 'context',
105
            'itemnumber' => 'int?'
106
        );
107
    }
108
 
109
    protected static function define_other_properties() {
110
        return array(
111
            'itemfiles' => array(
112
                'type' => stored_file_exporter::read_properties_definition(),
113
                'multiple' => true
114
            ),
115
            'itemnumber' => array(
116
                'type' => PARAM_INT,
117
                'description' => 'The item position number',
118
                'null' => NULL_ALLOWED
119
            ),
120
            'otherdata' => array(
121
                'type' => PARAM_RAW,
122
                'description' => 'Additional data that may be required by external functions',
123
                'null' => NULL_ALLOWED
124
            ),
125
        );
126
    }
127
 
128
    protected function get_other_values(renderer_base $output) {
129
        $context = $this->related['context'];
130
 
131
        $itemobj = feedback_get_item_class($this->data->typ);
132
        $values = array(
133
            'itemfiles' => array(),
134
            'itemnumber' => $this->related['itemnumber'],
135
            'otherdata' => $itemobj->get_data_for_external($this->data),
136
        );
137
 
138
        $fs = get_file_storage();
139
        $files = array();
140
        $itemfiles = $fs->get_area_files($context->id, 'mod_feedback', 'item', $this->data->id, 'filename', false);
141
        if (!empty($itemfiles)) {
142
            foreach ($itemfiles as $storedfile) {
143
                $fileexporter = new stored_file_exporter($storedfile, array('context' => $context));
144
                $files[] = $fileexporter->export($output);
145
            }
146
            $values['itemfiles'] = $files;
147
        }
148
 
149
        return $values;
150
    }
151
 
152
    /**
153
     * Get the formatting parameters for the name.
154
     *
155
     * @return array
156
     */
157
    protected function get_format_parameters_for_name() {
158
        return [
159
            'component' => 'mod_feedback',
160
            'filearea' => 'item',
161
            'itemid' => $this->data->id
162
        ];
163
    }
164
 
165
    /**
166
     * Get the formatting parameters for the presentation.
167
     *
168
     * @return array
169
     */
170
    protected function get_format_parameters_for_presentation() {
171
        return [
172
            'component' => 'mod_feedback',
173
            'filearea' => 'item',
174
            'itemid' => $this->data->id
175
        ];
176
    }
177
}