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 with front-end (editing form) functionality.
19
 *
20
 * This is a base class of a class implemented by each component, and also has
21
 * static methods.
22
 *
23
 * @package core_availability
24
 * @copyright 2014 The Open University
25
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
 
28
namespace core_availability;
29
 
30
defined('MOODLE_INTERNAL') || die();
31
 
32
/**
33
 * Class with front-end (editing form) functionality.
34
 *
35
 * This is a base class of a class implemented by each component, and also has
36
 * static methods.
37
 *
38
 * @package core_availability
39
 * @copyright 2014 The Open University
40
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
42
abstract class frontend {
43
    /**
44
     * Decides whether this plugin should be available in a given course. The
45
     * plugin can do this depending on course or system settings.
46
     *
47
     * Default returns true.
48
     *
49
     * @param \stdClass $course Course object
50
     * @param \cm_info $cm Course-module currently being edited (null if none)
51
     * @param \section_info $section Section currently being edited (null if none)
52
     */
1441 ariadna 53
    protected function allow_add($course, ?\cm_info $cm = null,
54
            ?\section_info $section = null) {
1 efrain 55
        return true;
56
    }
57
 
58
    /**
59
     * Gets a list of string identifiers (in the plugin's language file) that
60
     * are required in JavaScript for this plugin. The default returns nothing.
61
     *
62
     * You do not need to include the 'title' string (which is used by core) as
63
     * this is automatically added.
64
     *
65
     * @return array Array of required string identifiers
66
     */
67
    protected function get_javascript_strings() {
68
        return array();
69
    }
70
 
71
    /**
72
     * Gets additional parameters for the plugin's initInner function.
73
     *
74
     * Default returns no parameters.
75
     *
76
     * @param \stdClass $course Course object
77
     * @param \cm_info $cm Course-module currently being edited (null if none)
78
     * @param \section_info $section Section currently being edited (null if none)
79
     * @return array Array of parameters for the JavaScript function
80
     */
1441 ariadna 81
    protected function get_javascript_init_params($course, ?\cm_info $cm = null,
82
            ?\section_info $section = null) {
1 efrain 83
        return array();
84
    }
85
 
86
    /**
87
     * Gets the Frankenstyle component name for this plugin.
88
     *
89
     * @return string The component name for this plugin
90
     */
91
    protected function get_component() {
92
        return preg_replace('~^(availability_.*?)\\\\frontend$~', '$1', get_class($this));
93
    }
94
 
95
    /**
96
     * Includes JavaScript for the main system and all plugins.
97
     *
98
     * @param \stdClass $course Course object
99
     * @param \cm_info $cm Course-module currently being edited (null if none)
100
     * @param \section_info $section Section currently being edited (null if none)
101
     */
1441 ariadna 102
    public static function include_all_javascript($course, ?\cm_info $cm = null,
103
            ?\section_info $section = null) {
1 efrain 104
        global $PAGE;
105
 
106
        // Prepare array of required YUI modules. It is bad for performance to
107
        // make multiple yui_module calls, so we group all the plugin modules
108
        // into a single call (the main init function will call init for each
109
        // plugin).
110
        $modules = array('moodle-core_availability-form', 'base', 'node',
111
                'panel', 'moodle-core-notification-dialogue', 'json');
112
 
113
        // Work out JS to include for all components.
114
        $pluginmanager = \core_plugin_manager::instance();
115
        $enabled = $pluginmanager->get_enabled_plugins('availability');
116
        $componentparams = new \stdClass();
117
        foreach ($enabled as $plugin => $info) {
118
            // Create plugin front-end object.
119
            $class = '\availability_' . $plugin . '\frontend';
1441 ariadna 120
            if (!class_exists($class)) {
121
                continue;
122
            }
123
 
124
            /** @var \core_availability\frontend $frontend */
1 efrain 125
            $frontend = new $class();
126
 
127
            // Add to array of required YUI modules.
128
            $component = $frontend->get_component();
129
            $modules[] = 'moodle-' . $component . '-form';
130
 
131
            // Get parameters for this plugin.
1441 ariadna 132
            $componentparams->{$plugin} = [
133
                $component,
134
                $frontend->allow_add($course, $cm, $section),
135
                $frontend->get_javascript_init_params($course, $cm, $section),
136
                get_config('availability_' . $plugin, 'defaultdisplaymode'),
137
            ];
1 efrain 138
 
139
            // Include strings for this plugin.
140
            $identifiers = $frontend->get_javascript_strings();
141
            $identifiers[] = 'title';
142
            $identifiers[] = 'description';
143
            $PAGE->requires->strings_for_js($identifiers, $component);
144
        }
145
 
146
        // Include all JS (in one call). The init function runs on DOM ready.
147
        $PAGE->requires->yui_module($modules,
148
                'M.core_availability.form.init', array($componentparams), null, true);
149
 
150
        // Include main strings.
151
        $PAGE->requires->strings_for_js(array('none', 'cancel', 'delete', 'choosedots'),
152
                'moodle');
153
        $PAGE->requires->strings_for_js(array('addrestriction', 'invalid',
154
                'listheader_sign_before', 'listheader_sign_pos',
155
                'listheader_sign_neg', 'listheader_single',
156
                'listheader_multi_after', 'listheader_multi_before',
157
                'listheader_multi_or', 'listheader_multi_and',
158
                'unknowncondition', 'hide_verb', 'hidden_individual',
159
                'show_verb', 'shown_individual', 'hidden_all', 'shown_all',
160
                'condition_group', 'condition_group_info', 'and', 'or',
161
                'label_multi', 'label_sign', 'setheading', 'itemheading',
162
                'missingplugin', 'disabled_verb'),
163
                'availability');
164
    }
165
 
166
    /**
167
     * For use within forms, reports any validation errors from the availability
168
     * field.
169
     *
170
     * @param array $data Form data fields
171
     * @param array $errors Error array
172
     */
173
    public static function report_validation_errors(array $data, array &$errors) {
174
        // Empty value is allowed!
175
        if ($data['availabilityconditionsjson'] === '') {
176
            return;
177
        }
178
 
179
        // Decode value.
180
        $decoded = json_decode($data['availabilityconditionsjson']);
181
        if (!$decoded) {
182
            // This shouldn't be possible.
183
            throw new \coding_exception('Invalid JSON from availabilityconditionsjson field');
184
        }
185
        if (!empty($decoded->errors)) {
186
            $error = '';
187
            foreach ($decoded->errors as $stringinfo) {
188
                list ($component, $stringname) = explode(':', $stringinfo);
189
                if ($error !== '') {
190
                    $error .= ' ';
191
                }
192
                $error .= get_string($stringname, $component);
193
            }
194
            $errors['availabilityconditionsjson'] = $error;
195
        }
196
    }
197
 
198
    /**
199
     * Converts an associative array into an array of objects with two fields.
200
     *
201
     * This is necessary because JavaScript associative arrays/objects are not
202
     * ordered (at least officially according to the language specification).
203
     *
204
     * @param array $inarray Associative array key => value
205
     * @param string $keyname Name to use for key in resulting array objects
206
     * @param string $valuename Name to use for value in resulting array objects
207
     * @return array Non-associative (numeric) array
208
     */
209
    protected static function convert_associative_array_for_js(array $inarray,
210
            $keyname, $valuename) {
211
        $result = array();
212
        foreach ($inarray as $key => $value) {
213
            $result[] = (object)array($keyname => $key, $valuename => $value);
214
        }
215
        return $result;
216
    }
217
}