first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+212
View File
@@ -0,0 +1,212 @@
<?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/>.
namespace core_contentbank\output;
use core\context\{course, coursecat};
use core\context_helper;
use core_contentbank\content;
use core_contentbank\contentbank;
use renderable;
use templatable;
use renderer_base;
use stdClass;
/**
* Class containing data for bank content
*
* @package core_contentbank
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class bankcontent implements renderable, templatable {
/**
* @var \core_contentbank\content[] Array of content bank contents.
*/
private $contents;
/**
* @var array $toolbar object.
*/
private $toolbar;
/**
* @var \context Given context. Null by default.
*/
private $context;
/**
* @var array Course categories that the user has access to.
*/
private $allowedcategories;
/**
* @var array Courses that the user has access to.
*/
private $allowedcourses;
/**
* Construct this renderable.
*
* @param \core_contentbank\content[] $contents Array of content bank contents.
* @param array $toolbar List of content bank toolbar options.
* @param \context|null $context Optional context to check (default null)
* @param contentbank $cb Contenbank object.
*/
public function __construct(array $contents, array $toolbar, ?\context $context, contentbank $cb) {
$this->contents = $contents;
$this->toolbar = $toolbar;
$this->context = $context;
list($this->allowedcategories, $this->allowedcourses) = $cb->get_contexts_with_capabilities_by_user();
}
/**
* Export the data.
*
* @param renderer_base $output
* @return stdClass
*/
public function export_for_template(renderer_base $output): stdClass {
global $PAGE, $SITE;
$PAGE->requires->js_call_amd('core_contentbank/search', 'init');
$PAGE->requires->js_call_amd('core_contentbank/sort', 'init');
$data = new stdClass();
$contentdata = array();
foreach ($this->contents as $content) {
$file = $content->get_file();
$filesize = $file ? $file->get_filesize() : 0;
$mimetype = $file ? get_mimetype_description($file) : '';
$contenttypeclass = $content->get_content_type().'\\contenttype';
$contenttype = new $contenttypeclass($this->context);
if ($content->get_visibility() == content::VISIBILITY_UNLISTED) {
$name = get_string('visibilitytitleunlisted', 'contentbank', $content->get_name());
} else {
$name = $content->get_name();
}
$author = \core_user::get_user($content->get_content()->usercreated);
$contentdata[] = array(
'name' => $name,
'title' => strtolower($name),
'link' => $contenttype->get_view_url($content),
'icon' => $contenttype->get_icon($content),
'uses' => count($content->get_uses()),
'timemodified' => $content->get_timemodified(),
'bytes' => $filesize,
'size' => display_size($filesize),
'type' => $mimetype,
'author' => fullname($author),
'visibilityunlisted' => $content->get_visibility() == content::VISIBILITY_UNLISTED
);
}
$data->viewlist = get_user_preferences('core_contentbank_view_list');
$data->contents = $contentdata;
// The tools are displayed in the action bar on the index page.
foreach ($this->toolbar as $tool) {
// Customize the output of a tool, like dropdowns.
$method = 'export_tool_'.$tool['action'];
if (method_exists($this, $method)) {
$this->$method($tool);
}
$data->tools[] = $tool;
}
$allowedcontexts = [];
$systemcontext = \context_system::instance();
if (has_capability('moodle/contentbank:access', $systemcontext)) {
$allowedcontexts[$systemcontext->id] = get_string('coresystem');
}
$options = [];
foreach ($this->allowedcategories as $allowedcategory) {
context_helper::preload_from_record(clone $allowedcategory);
$options[$allowedcategory->ctxid] = format_string($allowedcategory->name, true, [
'context' => coursecat::instance($allowedcategory->ctxinstance),
]);
}
if (!empty($options)) {
$allowedcontexts['categories'] = [get_string('coursecategories') => $options];
}
$options = [];
foreach ($this->allowedcourses as $allowedcourse) {
// Don't add the frontpage course to the list.
if ($allowedcourse->id != $SITE->id) {
context_helper::preload_from_record(clone $allowedcourse);
$options[$allowedcourse->ctxid] = format_string($allowedcourse->fullname, true, [
'context' => course::instance($allowedcourse->ctxinstance),
]);
}
}
if (!empty($options)) {
$allowedcontexts['courses'] = [get_string('courses') => $options];
}
if (!empty($allowedcontexts)) {
$strchoosecontext = get_string('choosecontext', 'core_contentbank');
$singleselect = new \single_select(
new \moodle_url('/contentbank/index.php'),
'contextid',
$allowedcontexts,
$this->context->id,
$strchoosecontext
);
$singleselect->set_label($strchoosecontext, ['class' => 'sr-only']);
$data->allowedcontexts = $singleselect->export_for_template($output);
}
return $data;
}
/**
* Adds the content type items to display to the Add dropdown.
*
* Each content type is represented as an object with the properties:
* - name: the name of the content type.
* - baseurl: the base content type editor URL.
* - types: different types of the content type to display as dropdown items.
*
* @param array $tool Data for rendering the Add dropdown, including the editable content types.
*/
private function export_tool_add(array &$tool) {
$editabletypes = $tool['contenttypes'];
$addoptions = [];
foreach ($editabletypes as $class => $type) {
$contentype = new $class($this->context);
// Get the creation options of each content type.
$types = $contentype->get_contenttype_types();
if ($types) {
// Add a text describing the content type as first option. This will be displayed in the drop down to
// separate the options for the different content types.
$contentdesc = new stdClass();
$contentdesc->typename = get_string('description', $contentype->get_contenttype_name());
array_unshift($types, $contentdesc);
// Context data for the template.
$addcontenttype = new stdClass();
// Content type name.
$addcontenttype->name = $type;
// Content type editor base URL.
$tool['link']->param('plugin', $type);
$addcontenttype->baseurl = $tool['link']->out();
// Different types of the content type.
$addcontenttype->types = $types;
$addoptions[] = $addcontenttype;
}
}
$tool['contenttypes'] = $addoptions;
}
}
+204
View File
@@ -0,0 +1,204 @@
<?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/>.
namespace core_contentbank\output;
use context;
use core_contentbank\content;
use core_contentbank\contenttype;
use moodle_url;
use renderable;
use renderer_base;
use stdClass;
use templatable;
/**
* Class containing data for the content view.
*
* @copyright 2020 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class viewcontent implements renderable, templatable {
/**
* @var contenttype Content bank content type.
*/
private $contenttype;
/**
* @var stdClass Record of the contentbank_content table.
*/
private $content;
/**
* Construct this renderable.
*
* @param contenttype $contenttype Content bank content type.
* @param content $content Record of the contentbank_content table.
*/
public function __construct(contenttype $contenttype, content $content) {
$this->contenttype = $contenttype;
$this->content = $content;
}
/**
* Get the content of the "More" dropdown in the tertiary navigation
*
* @return array|null The options to be displayed in a dropdown in the tertiary navigation
* @throws \moodle_exception
*/
protected function get_edit_actions_dropdown(): ?array {
global $PAGE;
$options = [];
if ($this->contenttype->can_manage($this->content)) {
// Add the visibility item to the menu.
switch($this->content->get_visibility()) {
case content::VISIBILITY_UNLISTED:
$visibilitylabel = get_string('visibilitysetpublic', 'core_contentbank');
$newvisibility = content::VISIBILITY_PUBLIC;
break;
case content::VISIBILITY_PUBLIC:
$visibilitylabel = get_string('visibilitysetunlisted', 'core_contentbank');
$newvisibility = content::VISIBILITY_UNLISTED;
break;
default:
$url = new \moodle_url('/contentbank/index.php', ['contextid' => $this->content->get_contextid()]);
throw new moodle_exception('contentvisibilitynotfound', 'error', $url, $this->content->get_visibility());
}
if ($visibilitylabel) {
$options[$visibilitylabel] = [
'data-action' => 'setcontentvisibility',
'data-visibility' => $newvisibility,
'data-contentid' => $this->content->get_id(),
];
}
// Add the rename content item to the menu.
$options[get_string('rename')] = [
'data-action' => 'renamecontent',
'data-contentname' => $this->content->get_name(),
'data-contentid' => $this->content->get_id(),
];
if ($this->contenttype->can_upload()) {
$options[get_string('replacecontent', 'contentbank')] = ['data-action' => 'upload'];
$PAGE->requires->js_call_amd(
'core_contentbank/upload',
'initModal',
[
'[data-action=upload]',
\core_contentbank\form\upload_files::class,
$this->content->get_contextid(),
$this->content->get_id()
]
);
}
}
if ($this->contenttype->can_download($this->content)) {
$url = new moodle_url($this->contenttype->get_download_url($this->content));
$options[get_string('download')] = [
'url' => $url->out()
];
}
if ($this->contenttype->can_copy($this->content)) {
// Add the copy content item to the menu.
$options[get_string('copycontent', 'contentbank')] = [
'data-action' => 'copycontent',
'data-contentname' => get_string('copyof', 'contentbank', $this->content->get_name()),
'data-contentid' => $this->content->get_id(),
];
}
// Add the delete content item to the menu.
if ($this->contenttype->can_delete($this->content)) {
$options[get_string('delete')] = [
'data-action' => 'deletecontent',
'data-contentname' => $this->content->get_name(),
'data-uses' => count($this->content->get_uses()),
'data-contentid' => $this->content->get_id(),
'data-contextid' => $this->content->get_contextid(),
'class' => 'text-danger',
];
}
$dropdown = [];
if ($options) {
foreach ($options as $key => $attribs) {
$url = $attribs['url'] ?? '#';
$extraclasses = $attribs['class'] ?? '';
$dropdown['options'][] = [
'label' => $key,
'url' => $url,
'extraclasses' => $extraclasses,
'attributes' => array_map(function ($key, $value) {
return [
'name' => $key,
'value' => $value
];
}, array_keys($attribs), $attribs)
];
}
}
return $dropdown;
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output
*
* @return stdClass
*/
public function export_for_template(renderer_base $output): stdClass {
$data = new stdClass();
// Get the content type html.
$contenthtml = $this->contenttype->get_view_content($this->content);
$data->contenthtml = $contenthtml;
// Check if the user can edit this content type.
if ($this->contenttype->can_edit($this->content)) {
$data->usercanedit = true;
$urlparams = [
'contextid' => $this->content->get_contextid(),
'plugin' => $this->contenttype->get_plugin_name(),
'id' => $this->content->get_id()
];
$editcontenturl = new moodle_url('/contentbank/edit.php', $urlparams);
$data->editcontenturl = $editcontenturl->out(false);
}
// Close/exit link for those users who can access that context.
$context = context::instance_by_id($this->content->get_contextid());
if (has_capability('moodle/contentbank:access', $context)) {
$closeurl = new moodle_url('/contentbank/index.php', ['contextid' => $context->id]);
$data->closeurl = $closeurl->out(false);
}
$data->actionmenu = $this->get_edit_actions_dropdown();
$data->heading = $this->content->get_name();
if ($this->content->get_visibility() == content::VISIBILITY_UNLISTED) {
$data->heading = get_string('visibilitytitleunlisted', 'contentbank', $data->heading);
}
return $data;
}
}