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
namespace core_contentbank\output;
18
 
11 efrain 19
use core\context\{course, coursecat};
20
use core\context_helper;
1 efrain 21
use core_contentbank\content;
22
use core_contentbank\contentbank;
23
use renderable;
24
use templatable;
25
use renderer_base;
26
use stdClass;
27
 
28
/**
29
 * Class containing data for bank content
30
 *
31
 * @package    core_contentbank
32
 * @copyright  2020 Ferran Recio <ferran@moodle.com>
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class bankcontent implements renderable, templatable {
36
 
37
    /**
38
     * @var \core_contentbank\content[]    Array of content bank contents.
39
     */
40
    private $contents;
41
 
42
    /**
43
     * @var array   $toolbar object.
44
     */
45
    private $toolbar;
46
 
47
    /**
48
     * @var \context    Given context. Null by default.
49
     */
50
    private $context;
51
 
52
    /**
53
     * @var array   Course categories that the user has access to.
54
     */
55
    private $allowedcategories;
56
 
57
    /**
58
     * @var array   Courses that the user has access to.
59
     */
60
    private $allowedcourses;
61
 
62
    /**
63
     * Construct this renderable.
64
     *
65
     * @param \core_contentbank\content[] $contents   Array of content bank contents.
66
     * @param array $toolbar List of content bank toolbar options.
67
     * @param \context|null $context Optional context to check (default null)
68
     * @param contentbank $cb Contenbank object.
69
     */
70
    public function __construct(array $contents, array $toolbar, ?\context $context, contentbank $cb) {
71
        $this->contents = $contents;
72
        $this->toolbar = $toolbar;
73
        $this->context = $context;
74
        list($this->allowedcategories, $this->allowedcourses) = $cb->get_contexts_with_capabilities_by_user();
75
    }
76
 
77
    /**
78
     * Export the data.
79
     *
80
     * @param renderer_base $output
81
     * @return stdClass
82
     */
83
    public function export_for_template(renderer_base $output): stdClass {
84
        global $PAGE, $SITE;
85
 
86
        $PAGE->requires->js_call_amd('core_contentbank/search', 'init');
87
        $PAGE->requires->js_call_amd('core_contentbank/sort', 'init');
88
 
89
        $data = new stdClass();
90
        $contentdata = array();
91
        foreach ($this->contents as $content) {
92
            $file = $content->get_file();
93
            $filesize = $file ? $file->get_filesize() : 0;
94
            $mimetype = $file ? get_mimetype_description($file) : '';
95
            $contenttypeclass = $content->get_content_type().'\\contenttype';
96
            $contenttype = new $contenttypeclass($this->context);
97
            if ($content->get_visibility() == content::VISIBILITY_UNLISTED) {
98
                $name = get_string('visibilitytitleunlisted', 'contentbank', $content->get_name());
99
            } else {
100
                $name = $content->get_name();
101
            }
102
            $author = \core_user::get_user($content->get_content()->usercreated);
103
            $contentdata[] = array(
104
                'name' => $name,
105
                'title' => strtolower($name),
106
                'link' => $contenttype->get_view_url($content),
107
                'icon' => $contenttype->get_icon($content),
108
                'uses' => count($content->get_uses()),
109
                'timemodified' => $content->get_timemodified(),
110
                'bytes' => $filesize,
111
                'size' => display_size($filesize),
112
                'type' => $mimetype,
113
                'author' => fullname($author),
114
                'visibilityunlisted' => $content->get_visibility() == content::VISIBILITY_UNLISTED
115
            );
116
        }
117
        $data->viewlist = get_user_preferences('core_contentbank_view_list');
118
        $data->contents = $contentdata;
119
        // The tools are displayed in the action bar on the index page.
120
        foreach ($this->toolbar as $tool) {
121
            // Customize the output of a tool, like dropdowns.
122
            $method = 'export_tool_'.$tool['action'];
123
            if (method_exists($this, $method)) {
124
                $this->$method($tool);
125
            }
126
            $data->tools[] = $tool;
127
        }
128
 
129
        $allowedcontexts = [];
130
        $systemcontext = \context_system::instance();
131
        if (has_capability('moodle/contentbank:access', $systemcontext)) {
132
            $allowedcontexts[$systemcontext->id] = get_string('coresystem');
133
        }
134
        $options = [];
135
        foreach ($this->allowedcategories as $allowedcategory) {
11 efrain 136
            context_helper::preload_from_record(clone $allowedcategory);
1 efrain 137
            $options[$allowedcategory->ctxid] = format_string($allowedcategory->name, true, [
11 efrain 138
                'context' => coursecat::instance($allowedcategory->ctxinstance),
1 efrain 139
            ]);
140
        }
141
        if (!empty($options)) {
142
            $allowedcontexts['categories'] = [get_string('coursecategories') => $options];
143
        }
144
        $options = [];
145
        foreach ($this->allowedcourses as $allowedcourse) {
146
            // Don't add the frontpage course to the list.
147
            if ($allowedcourse->id != $SITE->id) {
11 efrain 148
                context_helper::preload_from_record(clone $allowedcourse);
1 efrain 149
                $options[$allowedcourse->ctxid] = format_string($allowedcourse->fullname, true, [
11 efrain 150
                    'context' => course::instance($allowedcourse->ctxinstance),
1 efrain 151
                ]);
152
            }
153
        }
154
        if (!empty($options)) {
155
            $allowedcontexts['courses'] = [get_string('courses') => $options];
156
        }
157
        if (!empty($allowedcontexts)) {
158
            $strchoosecontext = get_string('choosecontext', 'core_contentbank');
159
            $singleselect = new \single_select(
160
                new \moodle_url('/contentbank/index.php'),
161
                'contextid',
162
                $allowedcontexts,
163
                $this->context->id,
164
                $strchoosecontext
165
            );
166
            $singleselect->set_label($strchoosecontext, ['class' => 'sr-only']);
167
            $data->allowedcontexts = $singleselect->export_for_template($output);
168
        }
169
 
170
        return $data;
171
    }
172
 
173
    /**
174
     * Adds the content type items to display to the Add dropdown.
175
     *
176
     * Each content type is represented as an object with the properties:
177
     *     - name: the name of the content type.
178
     *     - baseurl: the base content type editor URL.
179
     *     - types: different types of the content type to display as dropdown items.
180
     *
181
     * @param array $tool Data for rendering the Add dropdown, including the editable content types.
182
     */
183
    private function export_tool_add(array &$tool) {
184
        $editabletypes = $tool['contenttypes'];
185
 
186
        $addoptions = [];
187
        foreach ($editabletypes as $class => $type) {
188
            $contentype = new $class($this->context);
189
            // Get the creation options of each content type.
190
            $types = $contentype->get_contenttype_types();
191
            if ($types) {
192
                // Add a text describing the content type as first option. This will be displayed in the drop down to
193
                // separate the options for the different content types.
194
                $contentdesc = new stdClass();
195
                $contentdesc->typename = get_string('description', $contentype->get_contenttype_name());
196
                array_unshift($types, $contentdesc);
197
                // Context data for the template.
198
                $addcontenttype = new stdClass();
199
                // Content type name.
200
                $addcontenttype->name = $type;
201
                // Content type editor base URL.
202
                $tool['link']->param('plugin', $type);
203
                $addcontenttype->baseurl = $tool['link']->out();
204
                // Different types of the content type.
205
                $addcontenttype->types = $types;
206
                $addoptions[] = $addcontenttype;
207
            }
208
        }
209
 
210
        $tool['contenttypes'] = $addoptions;
211
    }
212
}