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
 * Contains the course_content_item_exporter class.
19
 *
20
 * @package    core
21
 * @subpackage course
22
 * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
namespace core_course\local\exporters;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
use core\external\exporter;
30
use core_course\local\entity\content_item;
31
use core_course\local\service\content_item_service;
32
 
33
/**
34
 * The course_content_item_exporter class.
35
 *
36
 * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class course_content_item_exporter extends exporter {
40
 
41
    /** @var content_item $contentitem the content_item to export. */
42
    private $contentitem;
43
 
44
    /**
45
     * The course_content_item_exporter constructor.
46
     *
47
     * @param content_item $contentitem the content item to export.
48
     * @param array $related the array of related objects used during export.
49
     */
50
    public function __construct(content_item $contentitem, array $related = []) {
51
        $this->contentitem = $contentitem;
52
 
53
        return parent::__construct([], $related);
54
    }
55
 
56
    /**
57
     * Definition of all properties originating in the export target, \core_course\local\entity\content_item.
58
     *
59
     * @return array The array of property values, indexed by name.
60
     */
61
    protected static function define_properties() {
62
        return [
63
            'id' => ['type' => PARAM_INT, 'description' => 'The id of the content item'],
64
            'name' => ['type' => PARAM_TEXT, 'description' => 'Name of the content item'],
65
            'title' => ['type' => PARAM_TEXT, 'description' => 'The string title of the content item, human readable'],
66
            'link' => ['type' => PARAM_URL, 'description' => 'The link to the content item creation page'],
67
            'icon' => ['type' => PARAM_RAW, 'description' => 'Html containing the icon for the content item'],
68
            'help' => ['type' => PARAM_RAW, 'description' => 'Html description / help for the content item'],
69
            'archetype' => ['type' => PARAM_RAW, 'description' => 'The archetype of the module exposing the content item'],
70
            'componentname' => ['type' => PARAM_TEXT, 'description' => 'The name of the component exposing the content item'],
71
            'purpose' => ['type' => PARAM_TEXT, 'description' => 'The purpose of the component exposing the content item'],
72
            'branded' => ['type' => PARAM_BOOL, 'description' => ' Whether this content item is branded or not'],
73
        ];
74
    }
75
 
76
    /**
77
     * Definition of all properties which are either calculated or originate in a related domain object.
78
     *
79
     * @return array The array of property values, indexed by name.
80
     */
81
    protected static function define_other_properties() {
82
        // This will hold user-dependant properties such as whether the item is starred or recommended.
83
        return [
84
            'favourite' => ['type' => PARAM_BOOL, 'description' => 'Has the user favourited the content item'],
85
            'legacyitem' => [
86
                'type' => PARAM_BOOL,
87
                'description' => 'If this item was pulled from the old callback and has no item id.'
88
            ],
89
            'recommended' => ['type' => PARAM_BOOL, 'description' => 'Has this item been recommended'],
90
        ];
91
    }
92
 
93
    /**
94
     * Get ALL properties for the content_item DTO being exported.
95
     *
96
     * These properties are a mix of:
97
     * - readonly properties of the primary object (content_item) being exported.
98
     * - calculated values
99
     * - properties originating from the related domain objects.
100
     *
101
     * Normally, those properties defined in get_properties() are added to the export automatically as part of the superclass code,
102
     * provided they are public properties on the export target. In this case, the export target is content_item, which doesn't
103
     * provide public access to its properties, so those are fetched via their respective getters here.
104
     *
105
     * @param \renderer_base $output
106
     * @return array The array of property values, indexed by name.
107
     */
108
    protected function get_other_values(\renderer_base $output) {
109
 
110
        $favourite = false;
111
        $itemtype = 'contentitem_' . $this->contentitem->get_component_name();
112
        if (isset($this->related['favouriteitems'])) {
113
            foreach ($this->related['favouriteitems'] as $favobj) {
114
                if ($favobj->itemtype === $itemtype && in_array($this->contentitem->get_id(), $favobj->ids)) {
115
                    $favourite = true;
116
                }
117
            }
118
        }
119
 
120
        $recommended = false;
121
        $itemtype = content_item_service::RECOMMENDATION_PREFIX . $this->contentitem->get_component_name();
122
        if (isset($this->related['recommended'])) {
123
            foreach ($this->related['recommended'] as $favobj) {
124
                if ($favobj->itemtype === $itemtype && in_array($this->contentitem->get_id(), $favobj->ids)) {
125
                    $recommended = true;
126
                }
127
            }
128
        }
129
 
130
        $properties = [
131
            'id' => $this->contentitem->get_id(),
132
            'name' => $this->contentitem->get_name(),
133
            'title' => $this->contentitem->get_title()->get_value(),
134
            'link' => $this->contentitem->get_link()->out(false),
135
            'icon' => $this->contentitem->get_icon(),
136
            'help' => format_text($this->contentitem->get_help(), FORMAT_MARKDOWN),
137
            'archetype' => $this->contentitem->get_archetype(),
138
            'componentname' => $this->contentitem->get_component_name(),
139
            'favourite' => $favourite,
140
            'legacyitem' => ($this->contentitem->get_id() == -1),
141
            'recommended' => $recommended,
142
            'purpose' => $this->contentitem->get_purpose(),
143
            'branded' => $this->contentitem->is_branded(),
144
        ];
145
 
146
        return $properties;
147
    }
148
 
149
    /**
150
     * Define the list of related objects, used by this exporter.
151
     *
152
     * @return array the list of related objects.
153
     */
154
    protected static function define_related(): array {
155
        return [
156
            'context' => '\context',
157
            'favouriteitems' => '\stdClass[]?',
158
            'recommended' => '\stdClass[]?'
159
        ];
160
    }
161
}