moodle /lib/classes/external/exporter.php

Language PHP Lines 591
MD5 Hash b3d328d7276e2288310e950cfab41dc0 Estimated Cost $7,498 (why?)
Repository https://bitbucket.org/moodle/moodle.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
<?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/>.

/**
 * Generic exporter to take a stdClass and prepare it for return by webservice.
 *
 * @package    core
 * @copyright  2015 Damyon Wiese
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
namespace core\external;
defined('MOODLE_INTERNAL') || die();

require_once($CFG->libdir . '/externallib.php');

use stdClass;
use renderer_base;
use context;
use context_system;
use coding_exception;
use external_single_structure;
use external_multiple_structure;
use external_value;
use external_format_value;

/**
 * Generic exporter to take a stdClass and prepare it for return by webservice, or as the context for a template.
 *
 * templatable classes implementing export_for_template, should always use a standard exporter if it exists.
 * External functions should always use a standard exporter if it exists.
 *
 * @copyright  2015 Damyon Wiese
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
abstract class exporter {

    /** @var array $related List of related objects used to avoid DB queries. */
    protected $related = array();

    /** @var stdClass|array The data of this exporter. */
    protected $data = null;

    /**
     * Constructor - saves the persistent object, and the related objects.
     *
     * @param mixed $data - Either an stdClass or an array of values.
     * @param array $related - An optional list of pre-loaded objects related to this object.
     */
    public function __construct($data, $related = array()) {
        $this->data = $data;
        // Cache the valid related objects.
        foreach (static::define_related() as $key => $classname) {
            $isarray = false;
            $nullallowed = false;

            // Allow ? to mean null is allowed.
            if (substr($classname, -1) === '?') {
                $classname = substr($classname, 0, -1);
                $nullallowed = true;
            }

            // Allow [] to mean an array of values.
            if (substr($classname, -2) === '[]') {
                $classname = substr($classname, 0, -2);
                $isarray = true;
            }

            $missingdataerr = 'Exporter class is missing required related data: (' . get_called_class() . ') ';

            if ($nullallowed && array_key_exists($key, $related) && $related[$key] === null) {
                $this->related[$key] = $related[$key];

            } else if ($isarray) {
                if (array_key_exists($key, $related) && is_array($related[$key])) {
                    foreach ($related[$key] as $index => $value) {
                        if (!$value instanceof $classname) {
                            throw new coding_exception($missingdataerr . $key . ' => ' . $classname . '[]');
                        }
                    }
                    $this->related[$key] = $related[$key];
                } else {
                    throw new coding_exception($missingdataerr . $key . ' => ' . $classname . '[]');
                }

            } else {
                $scalartypes = ['string', 'int', 'bool', 'float'];
                $scalarcheck = 'is_' . $classname;
                if (array_key_exists($key, $related) &&
                        ((in_array($classname, $scalartypes) && $scalarcheck($related[$key])) ||
                        ($related[$key] instanceof $classname))) {
                    $this->related[$key] = $related[$key];
                } else {
                    throw new coding_exception($missingdataerr . $key . ' => ' . $classname);
                }
            }
        }
    }

    /**
     * Function to export the renderer data in a format that is suitable for a
     * mustache template. This means raw records are generated as in to_record,
     * but all strings are correctly passed through external_format_text (or external_format_string).
     *
     * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
     * @return stdClass
     */
    final public function export(renderer_base $output) {
        $data = new stdClass();
        $properties = self::read_properties_definition();
        $values = (array) $this->data;

        $othervalues = $this->get_other_values($output);
        if (array_intersect_key($values, $othervalues)) {
            // Attempt to replace a standard property.
            throw new coding_exception('Cannot override a standard property value.');
        }
        $values += $othervalues;
        $record = (object) $values;

        foreach ($properties as $property => $definition) {
            if (isset($data->$property)) {
                // This happens when we have already defined the format properties.
                continue;
            } else if (!property_exists($record, $property) && array_key_exists('default', $definition)) {
                // We have a default value for this property.
                $record->$property = $definition['default'];
            } else if (!property_exists($record, $property) && !empty($definition['optional'])) {
                // Fine, this property can be omitted.
                continue;
            } else if (!property_exists($record, $property)) {
                // Whoops, we got something that wasn't defined.
                throw new coding_exception('Unexpected property ' . $property);
            }

            $data->$property = $record->$property;

            // If the field is PARAM_RAW and has a format field.
            if ($propertyformat = self::get_format_field($properties, $property)) {
                if (!property_exists($record, $propertyformat)) {
                    // Whoops, we got something that wasn't defined.
                    throw new coding_exception('Unexpected property ' . $propertyformat);
                }

                $formatparams = $this->get_format_parameters($property);
                $format = $record->$propertyformat;

                list($text, $format) = external_format_text($data->$property, $format, $formatparams['context']->id,
                    $formatparams['component'], $formatparams['filearea'], $formatparams['itemid'], $formatparams['options']);

                $data->$property = $text;
                $data->$propertyformat = $format;

            } else if ($definition['type'] === PARAM_TEXT) {
                $formatparams = $this->get_format_parameters($property);

                if (!empty($definition['multiple'])) {
                    foreach ($data->$property as $key => $value) {
                        $data->{$property}[$key] = external_format_string($value, $formatparams['context']->id,
                            $formatparams['striplinks'], $formatparams['options']);
                    }
                } else {
                    $data->$property = external_format_string($data->$property, $formatparams['context']->id,
                            $formatparams['striplinks'], $formatparams['options']);
                }
            }
        }

        return $data;
    }

    /**
     * Get the format parameters.
     *
     * This method returns the parameters to use with the functions external_format_text(), and
     * external_format_string(). To override the default parameters, you can define a protected method
     * called 'get_format_parameters_for_<propertyName>'. For example, 'get_format_parameters_for_description',
     * if your property is 'description'.
     *
     * Your method must return an array containing any of the following keys:
     * - context: The context to use. Defaults to $this->related['context'] if defined, else throws an exception.
     * - component: The component to use with external_format_text(). Defaults to null.
     * - filearea: The filearea to use with external_format_text(). Defaults to null.
     * - itemid: The itemid to use with external_format_text(). Defaults to null.
     * - options: An array of options accepted by external_format_text() or external_format_string(). Defaults to [].
     * - striplinks: Whether to strip the links with external_format_string(). Defaults to true.
     *
     * @param string $property The property to get the parameters for.
     * @return array
     */
    final protected function get_format_parameters($property) {
        $parameters = [
            'component' => null,
            'filearea' => null,
            'itemid' => null,
            'options' => [],
            'striplinks' => true,
        ];

        $candidate = 'get_format_parameters_for_' . $property;
        if (method_exists($this, $candidate)) {
            $parameters = array_merge($parameters, $this->{$candidate}());
        }

        if (!isset($parameters['context'])) {
            if (!isset($this->related['context']) || !($this->related['context'] instanceof context)) {
                throw new coding_exception("Unknown context to use for formatting the property '$property' in the " .
                    "exporter '" . get_class($this) . "'. You either need to add 'context' to your related objects, " .
                    "or create the method '$candidate' and return the context from there.");
            }
            $parameters['context'] = $this->related['context'];

        } else if (!($parameters['context'] instanceof context)) {
            throw new coding_exception("The context given to format the property '$property' in the exporter '" .
                get_class($this) . "' is invalid.");
        }

        return $parameters;
    }

    /**
     * Get the additional values to inject while exporting.
     *
     * These are additional generated values that are not passed in through $data
     * to the exporter. For a persistent exporter - these are generated values that
     * do not exist in the persistent class. For your convenience the format_text or
     * format_string functions do not need to be applied to PARAM_TEXT fields,
     * it will be done automatically during export.
     *
     * These values are only used when returning data via {@link self::export()},
     * they are not used when generating any of the different external structures.
     *
     * Note: These must be defined in {@link self::define_other_properties()}.
     *
     * @param renderer_base $output The renderer.
     * @return array Keys are the property names, values are their values.
     */
    protected function get_other_values(renderer_base $output) {
        return array();
    }

    /**
     * Get the read properties definition of this exporter. Read properties combines the
     * default properties from the model (persistent or stdClass) with the properties defined
     * by {@link self::define_other_properties()}.
     *
     * @return array Keys are the property names, and value their definition.
     */
    final public static function read_properties_definition() {
        $properties = static::properties_definition();
        $customprops = static::define_other_properties();
        foreach ($customprops as $property => $definition) {
            // Ensures that null is set to its default.
            if (!isset($definition['null'])) {
                $customprops[$property]['null'] = NULL_NOT_ALLOWED;
            }
            if (!isset($definition['description'])) {
                $customprops[$property]['description'] = $property;
            }
        }
        $properties += $customprops;
        return $properties;
    }

    /**
     * Get the properties definition of this exporter used for create, and update structures.
     * The read structures are returned by: {@link self::read_properties_definition()}.
     *
     * @return array Keys are the property names, and value their definition.
     */
    final public static function properties_definition() {
        $properties = static::define_properties();
        foreach ($properties as $property => $definition) {
            // Ensures that null is set to its default.
            if (!isset($definition['null'])) {
                $properties[$property]['null'] = NULL_NOT_ALLOWED;
            }
            if (!isset($definition['description'])) {
                $properties[$property]['description'] = $property;
            }
        }
        return $properties;
    }

    /**
     * Return the list of additional properties used only for display.
     *
     * Additional properties are only ever used for the read structure, and during
     * export of the persistent data.
     *
     * The format of the array returned by this method has to match the structure
     * defined in {@link \core\persistent::define_properties()}. The display properties
     * can however do some more fancy things. They can define 'multiple' => true to wrap
     * values in an external_multiple_structure automatically - or they can define the
     * type as a nested array of more properties in order to generate a nested
     * external_single_structure.
     *
     * You can specify an array of values by including a 'multiple' => true array value. This
     * will result in a nested external_multiple_structure.
     * E.g.
     *
     *       'arrayofbools' => array(
     *           'type' => PARAM_BOOL,
     *           'multiple' => true
     *       ),
     *
     * You can return a nested array in the type field, which will result in a nested external_single_structure.
     * E.g.
     *      'competency' => array(
     *          'type' => competency_exporter::read_properties_definition()
     *       ),
     *
     * Other properties can be specifically marked as optional, in which case they do not need
     * to be included in the export in {@link self::get_other_values()}. This is useful when exporting
     * a substructure which cannot be set as null due to webservices protocol constraints.
     * E.g.
     *      'competency' => array(
     *          'type' => competency_exporter::read_properties_definition(),
     *          'optional' => true
     *       ),
     *
     * @return array
     */
    protected static function define_other_properties() {
        return array();
    }

    /**
     * Return the list of properties.
     *
     * The format of the array returned by this method has to match the structure
     * defined in {@link \core\persistent::define_properties()}. Howewer you can
     * add a new attribute "description" to describe the parameter for documenting the API.
     *
     * Note that the type PARAM_TEXT should ONLY be used for strings which need to
     * go through filters (multilang, etc...) and do not have a FORMAT_* associated
     * to them. Typically strings passed through to format_string().
     *
     * Other filtered strings which use a FORMAT_* constant (hear used with format_text)
     * must be defined as PARAM_RAW.
     *
     * @return array
     */
    protected static function define_properties() {
        return array();
    }

    /**
     * Returns a list of objects that are related to this persistent.
     *
     * Only objects listed here can be cached in this object.
     *
     * The class name can be suffixed:
     * - with [] to indicate an array of values.
     * - with ? to indicate that 'null' is allowed.
     *
     * @return array of 'propertyname' => array('type' => classname, 'required' => true)
     */
    protected static function define_related() {
        return array();
    }

    /**
     * Get the context structure.
     *
     * @return external_single_structure
     */
    final protected static function get_context_structure() {
        return array(
            'contextid' => new external_value(PARAM_INT, 'The context id', VALUE_OPTIONAL),
            'contextlevel' => new external_value(PARAM_ALPHA, 'The context level', VALUE_OPTIONAL),
            'instanceid' => new external_value(PARAM_INT, 'The Instance id', VALUE_OPTIONAL),
        );
    }

    /**
     * Get the format field name.
     *
     * @param  array $definitions List of properties definitions.
     * @param  string $property The name of the property that may have a format field.
     * @return bool|string False, or the name of the format property.
     */
    final protected static function get_format_field($definitions, $property) {
        $formatproperty = $property . 'format';
        if (($definitions[$property]['type'] == PARAM_RAW || $definitions[$property]['type'] == PARAM_CLEANHTML)
                && isset($definitions[$formatproperty])
                && $definitions[$formatproperty]['type'] == PARAM_INT) {
            return $formatproperty;
        }
        return false;
    }

    /**
     * Get the format structure.
     *
     * @param  string $property   The name of the property on which the format applies.
     * @param  array  $definition The definition of the format property.
     * @param  int    $required   Constant VALUE_*.
     * @return external_format_value
     */
    final protected static function get_format_structure($property, $definition, $required = VALUE_REQUIRED) {
        if (array_key_exists('default', $definition)) {
            $required = VALUE_DEFAULT;
        }
        return new external_format_value($property, $required);
    }

    /**
     * Returns the create structure.
     *
     * @return external_single_structure
     */
    final public static function get_create_structure() {
        $properties = self::properties_definition();
        $returns = array();

        foreach ($properties as $property => $definition) {
            if ($property == 'id') {
                // The can not be set on create.
                continue;

            } else if (isset($returns[$property]) && substr($property, -6) === 'format') {
                // We've already treated the format.
                continue;
            }

            $required = VALUE_REQUIRED;
            $default = null;

            // We cannot use isset here because we want to detect nulls.
            if (array_key_exists('default', $definition)) {
                $required = VALUE_DEFAULT;
                $default = $definition['default'];
            }

            // Magically treat the contextid fields.
            if ($property == 'contextid') {
                if (isset($properties['context'])) {
                    throw new coding_exception('There cannot be a context and a contextid column');
                }
                $returns += self::get_context_structure();

            } else {
                $returns[$property] = new external_value($definition['type'], $definition['description'], $required, $default,
                    $definition['null']);

                // Magically treat the format properties.
                if ($formatproperty = self::get_format_field($properties, $property)) {
                    if (isset($returns[$formatproperty])) {
                        throw new coding_exception('The format for \'' . $property . '\' is already defined.');
                    }
                    $returns[$formatproperty] = self::get_format_structure($property,
                        $properties[$formatproperty], VALUE_REQUIRED);
                }
            }
        }

        return new external_single_structure($returns);
    }

    /**
     * Returns the read structure.
     *
     * @return external_single_structure
     */
    final public static function get_read_structure() {
        $properties = self::read_properties_definition();

        return self::get_read_structure_from_properties($properties);
    }

    /**
     * Returns the read structure from a set of properties (recursive).
     *
     * @param array $properties The properties.
     * @param int $required Whether is required.
     * @param mixed $default The default value.
     * @return external_single_structure
     */
    final protected static function get_read_structure_from_properties($properties, $required = VALUE_REQUIRED, $default = null) {
        $returns = array();
        foreach ($properties as $property => $definition) {
            if (isset($returns[$property]) && substr($property, -6) === 'format') {
                // We've already treated the format.
                continue;
            }
            $thisvalue = null;

            $type = $definition['type'];
            $proprequired = VALUE_REQUIRED;
            $propdefault = null;
            if (array_key_exists('default', $definition)) {
                $propdefault = $definition['default'];
            }
            if (array_key_exists('optional', $definition)) {
                // Mark as optional. Note that this should only apply to "reading" "other" properties.
                $proprequired = VALUE_OPTIONAL;
            }

            if (is_array($type)) {
                // This is a nested array of more properties.
                $thisvalue = self::get_read_structure_from_properties($type, $proprequired, $propdefault);
            } else {
                if ($definition['type'] == PARAM_TEXT || $definition['type'] == PARAM_CLEANHTML) {
                    // PARAM_TEXT always becomes PARAM_RAW because filters may be applied.
                    $type = PARAM_RAW;
                }
                $thisvalue = new external_value($type, $definition['description'], $proprequired, $propdefault, $definition['null']);
            }
            if (!empty($definition['multiple'])) {
                $returns[$property] = new external_multiple_structure($thisvalue, $definition['description'], $proprequired,
                    $propdefault);
            } else {
                $returns[$property] = $thisvalue;

                // Magically treat the format properties (not possible for arrays).
                if ($formatproperty = self::get_format_field($properties, $property)) {
                    if (isset($returns[$formatproperty])) {
                        throw new coding_exception('The format for \'' . $property . '\' is already defined.');
                    }
                    $returns[$formatproperty] = self::get_format_structure($property, $properties[$formatproperty]);
                }
            }
        }

        return new external_single_structure($returns, '', $required, $default);
    }

    /**
     * Returns the update structure.
     *
     * This structure can never be included at the top level for an external function signature
     * because it contains optional parameters.
     *
     * @return external_single_structure
     */
    final public static function get_update_structure() {
        $properties = self::properties_definition();
        $returns = array();

        foreach ($properties as $property => $definition) {
            if (isset($returns[$property]) && substr($property, -6) === 'format') {
                // We've already treated the format.
                continue;
            }

            $default = null;
            $required = VALUE_OPTIONAL;
            if ($property == 'id') {
                $required = VALUE_REQUIRED;
            }

            // Magically treat the contextid fields.
            if ($property == 'contextid') {
                if (isset($properties['context'])) {
                    throw new coding_exception('There cannot be a context and a contextid column');
                }
                $returns += self::get_context_structure();

            } else {
                $returns[$property] = new external_value($definition['type'], $definition['description'], $required, $default,
                    $definition['null']);

                // Magically treat the format properties.
                if ($formatproperty = self::get_format_field($properties, $property)) {
                    if (isset($returns[$formatproperty])) {
                        throw new coding_exception('The format for \'' . $property . '\' is already defined.');
                    }
                    $returns[$formatproperty] = self::get_format_structure($property,
                        $properties[$formatproperty], VALUE_OPTIONAL);
                }
            }
        }

        return new external_single_structure($returns);
    }

}
Back to Top