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