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
@@ -0,0 +1,167 @@
<?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/>.
/**
* Utility class for browsing of content bank files.
*
* @package repository_contentbank
* @copyright 2020 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace repository_contentbank\browser;
/**
* Base class for the content bank browsers.
*
* @package repository_contentbank
* @copyright 2020 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class contentbank_browser {
/** @var \context The current context. */
protected $context;
/**
* Get all content nodes in the current context which can be viewed/accessed by the user.
*
* @return array[] The array containing all nodes which can be viewed/accessed by the user in the current context
*/
public function get_content(): array {
return array_merge($this->get_context_folders(), $this->get_contentbank_content());
}
/**
* Generate the full navigation to the current node.
*
* @return array[] The array containing the path to each node in the navigation.
* Each navigation node is an array with keys: name, path.
*/
public function get_navigation(): array {
// Get the current navigation node.
$currentnavigationnode = \repository_contentbank\helper::create_navigation_node($this->context);
$navigationnodes = [$currentnavigationnode];
// Get the parent content bank browser.
$parent = $this->get_parent();
// Prepend parent navigation node in the navigation nodes array until there is no existing parent.
while ($parent !== null) {
$parentnavigationnode = \repository_contentbank\helper::create_navigation_node($parent->context);
array_unshift($navigationnodes, $parentnavigationnode);
$parent = $parent->get_parent();
}
return $navigationnodes;
}
/**
* The required condition to enable the user to view/access the content bank content in this context.
*
* @return bool Whether the user can view/access the content bank content in the context
*/
abstract public function can_access_content(): bool;
/**
* Define the allowed child context levels.
*
* @return int[] The array containing the relevant child context levels
*/
abstract protected function allowed_child_context_levels(): array;
/**
* Get the relevant child contexts.
*
* @return \context[] The array containing the relevant, next-level children contexts
*/
protected function get_child_contexts(): array {
global $DB;
if (empty($allowedcontextlevels = $this->allowed_child_context_levels())) {
// Early return if there aren't any defined child context levels.
return [];
}
list($contextlevelsql, $params) = $DB->get_in_or_equal($allowedcontextlevels, SQL_PARAMS_NAMED);
$pathsql = $DB->sql_like('path', ':path', false, false);
$select = "contextlevel {$contextlevelsql}
AND {$pathsql}
AND depth = :depth";
$params['path'] = "{$this->context->path}/%";
$params['depth'] = $this->context->depth + 1;
$childcontexts = $DB->get_records_select('context', $select, $params);
return array_map(function($childcontext) {
return \context::instance_by_id($childcontext->id);
}, $childcontexts);
}
/**
* Get the content bank browser class of the parent context. Currently used to generate the navigation path.
*
* @return contentbank_browser|null The content bank browser of the parent context
*/
private function get_parent(): ?self {
if ($parentcontext = $this->context->get_parent_context()) {
return \repository_contentbank\helper::get_contentbank_browser($parentcontext);
}
return null;
}
/**
* Generate folder nodes for the relevant child contexts which can be accessed/viewed by the user.
*
* @return array[] The array containing the context folder nodes where each folder node is an array with keys:
* title, datemodified, datecreated, path, thumbnail, children.
*/
private function get_context_folders(): array {
// Get all relevant child contexts.
$children = $this->get_child_contexts();
// Return all child context folder nodes which can be accessed by the user following the defined conditions
// in can_access_content().
return array_reduce($children, function ($list, $child) {
$browser = \repository_contentbank\helper::get_contentbank_browser($child);
if ($browser->can_access_content()) {
$name = $child->get_context_name(false);
$path = base64_encode(json_encode(['contextid' => $child->id]));
$list[] = \repository_contentbank\helper::create_context_folder_node($name, $path);
}
return $list;
}, []);
}
/**
* Generate nodes for the content bank content in the current context which can be accessed/viewed by the user.
*
* @return array[] The array containing the content nodes where each content node is an array with keys:
* shorttitle, title, datemodified, datecreated, author, license, isref, source, icon, thumbnail.
*/
private function get_contentbank_content(): array {
$cb = new \core_contentbank\contentbank();
// Get all content bank files in the current context.
$contents = $cb->search_contents(null, $this->context->id);
// Return all content bank content nodes from the current context which can be accessed by the user following
// the defined conditions in can_access_content().
return array_reduce($contents, function($list, $content) {
if ($this->can_access_content() &&
$contentnode = \repository_contentbank\helper::create_contentbank_content_node($content)) {
$list[] = $contentnode;
}
return $list;
}, []);
}
}
@@ -0,0 +1,68 @@
<?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/>.
/**
* Utility class for browsing of content bank files in the course context.
*
* @package repository_contentbank
* @copyright 2020 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace repository_contentbank\browser;
/**
* Represents the content bank browser in the course context.
*
* @package repository_contentbank
* @copyright 2020 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class contentbank_browser_context_course extends contentbank_browser {
/**
* Constructor.
*
* @param \context_course $context The current context
*/
public function __construct(\context_course $context) {
$this->context = $context;
}
/**
* Define the allowed child context levels.
*
* @return int[] The array containing the relevant child context levels
*/
protected function allowed_child_context_levels(): array {
// The course context is the last relevant context level, therefore child context levels are not being returned.
return [];
}
/**
* The required condition to enable the user to view/access the content bank content in this context.
*
* @return bool Whether the user can view/access the content bank content in the context
*/
public function can_access_content(): bool {
// When the following conditions are met, the user would be able to share the content created in the course
// context level all over the site.
// The content from the course context level should be available to:
// * Every user which has capability to access the content of a given course.
return has_capability('repository/contentbank:accesscoursecontent', $this->context);
}
}
@@ -0,0 +1,84 @@
<?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/>.
/**
* Utility class for browsing of content bank files in the course category context.
*
* @package repository_contentbank
* @copyright 2020 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace repository_contentbank\browser;
/**
* Represents the content bank browser in the course category context.
*
* @package repository_contentbank
* @copyright 2020 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class contentbank_browser_context_coursecat extends contentbank_browser {
/**
* Constructor.
*
* @param \context_coursecat $context The current context
*/
public function __construct(\context_coursecat $context) {
$this->context = $context;
}
/**
* Define the allowed child context levels.
*
* @return int[] The array containing the relevant child context levels
*/
protected function allowed_child_context_levels(): array {
// The expected child contexts in the course category context level are the course category context
// (ex. subcategories) and the course context.
return [\CONTEXT_COURSECAT, \CONTEXT_COURSE];
}
/**
* The required condition to enable the user to view/access the content bank content in this context.
*
* @return bool Whether the user can view/access the content bank content in the context
*/
public function can_access_content(): bool {
// When the following conditions are met, the user would be able to share the content created in the course
// category context level all over the site.
// The content from the course category context level should be available to either:
// * Every user which has a capability to access the 'general' content and has capability to access the
// content of any child course of the given course category.
// * Users that have capability to access content at a course category context level.
if (has_capability('repository/contentbank:accesscoursecategorycontent', $this->context)) {
return true;
}
$canaccesschildcontent = false;
foreach ($this->get_child_contexts() as $childcontext) {
$browser = \repository_contentbank\helper::get_contentbank_browser($childcontext);
if ($canaccesschildcontent = $browser->can_access_content()) {
break;
}
}
return $canaccesschildcontent && has_capability('repository/contentbank:accessgeneralcontent',
$this->context);
}
}
@@ -0,0 +1,68 @@
<?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/>.
/**
* Utility class for browsing of content bank files in the system context.
*
* @package repository_contentbank
* @copyright 2020 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace repository_contentbank\browser;
/**
* Represents the content bank browser in the system context.
*
* @package repository_contentbank
* @copyright 2020 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class contentbank_browser_context_system extends contentbank_browser {
/**
* Constructor.
*
* @param \context_system $context The current context
*/
public function __construct(\context_system $context) {
$this->context = $context;
}
/**
* Define the allowed child context levels.
*
* @return int[] The array containing the relevant child context levels
*/
protected function allowed_child_context_levels(): array {
// The expected child context in the system context level is the course category context.
return [\CONTEXT_COURSECAT];
}
/**
* The required condition to enable the user to view/access the content bank content in this context.
*
* @return bool Whether the user can view/access the content bank content in the context
*/
public function can_access_content(): bool {
// When the following conditions are met, the user would be able to share the content created in the system
// context level all over the site.
// The content from the system context level should be available to:
// * Every user that has a capability to access the 'general' content.
return has_capability('repository/contentbank:accessgeneralcontent', $this->context);
}
}
@@ -0,0 +1,61 @@
<?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/>.
/**
* Utility class for searching of content bank files.
*
* @package repository_contentbank
* @copyright 2020 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace repository_contentbank;
/**
* Represents the content bank search related functionality.
*
* @package repository_contentbank
* @copyright 2020 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class contentbank_search {
/**
* Generate and return content nodes for all content bank files that match the search criteria
* and can be viewed/accessed by the user.
*
* @param string $search The search string
* @return array[] The array containing all content file nodes that match the search criteria. Each content node is
* an array with keys: shorttitle, title, datemodified, datecreated, author, license, isref, source,
* icon, thumbnail.
*/
public static function get_search_contents(string $search): array {
$contentbank = new \core_contentbank\contentbank();
// Return all content bank content that matches the search criteria and can be viewed/accessed by the user.
$contents = $contentbank->search_contents($search);
return array_reduce($contents, function($list, $content) {
$contentcontext = \context::instance_by_id($content->get_content()->contextid);
$browser = \repository_contentbank\helper::get_contentbank_browser($contentcontext);
// If the user can access the content and content node can be created, add the node into the
// search results list.
if ($browser->can_access_content() &&
$contentnode = \repository_contentbank\helper::create_contentbank_content_node($content)) {
$list[] = $contentnode;
}
return $list;
}, []);
}
}
+135
View File
@@ -0,0 +1,135 @@
<?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/>.
/**
* Content bank files repository helpers.
*
* @package repository_contentbank
* @copyright 2020 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace repository_contentbank;
use repository_contentbank\browser\contentbank_browser;
/**
* Helper class for content bank files repository.
*
* @package repository_contentbank
* @copyright 2020 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class helper {
/**
* Get the content bank repository browser for a certain context.
*
* @param \context $context The context
* @return \repository_contentbank\browser\contentbank_browser|null The content bank repository browser
*/
public static function get_contentbank_browser(\context $context): ?contentbank_browser {
switch ($context->contextlevel) {
case CONTEXT_SYSTEM:
return new \repository_contentbank\browser\contentbank_browser_context_system($context);
case CONTEXT_COURSECAT:
return new \repository_contentbank\browser\contentbank_browser_context_coursecat($context);
case CONTEXT_COURSE:
return new \repository_contentbank\browser\contentbank_browser_context_course($context);
}
return null;
}
/**
* Create the context folder node.
*
* @param string $name The name of the context folder node
* @param string $path The path to the context folder node
* @return array The context folder node
*/
public static function create_context_folder_node(string $name, string $path): array {
global $OUTPUT;
return [
'title' => $name,
'datemodified' => '',
'datecreated' => '',
'path' => $path,
'thumbnail' => $OUTPUT->image_url(file_folder_icon())->out(false),
'children' => []
];
}
/**
* Create the content bank content node.
*
* @param \core_contentbank\content $content The content bank content
* @return array|null The content bank content node
*/
public static function create_contentbank_content_node(\core_contentbank\content $content): ?array {
global $OUTPUT;
// Only content files are currently supported, but should be able to create content folder nodes in the future.
// Early return if the content is not a stored file.
if (!$file = $content->get_file()) {
return null;
}
$params = [
'contextid' => $file->get_contextid(),
'component' => $file->get_component(),
'filearea' => $file->get_filearea(),
'itemid' => $file->get_itemid(),
'filepath' => $file->get_filepath(),
'filename' => $file->get_filename()
];
$contenttype = $content->get_content_type_instance();
$encodedpath = base64_encode(json_encode($params));
$node = [
'shorttitle' => $content->get_name(),
'title' => $file->get_filename(),
'datemodified' => $file->get_timemodified(),
'datecreated' => $file->get_timecreated(),
'author' => $file->get_author(),
'license' => $file->get_license(),
'isref' => $file->is_external_file(),
'size' => $file->get_filesize(),
'source' => $encodedpath,
'icon' => $contenttype->get_icon($content),
'thumbnail' => $contenttype->get_icon($content)
];
if ($file->get_status() == 666) {
$node['originalmissing'] = true;
}
return $node;
}
/**
* Generate a navigation node.
*
* @param \context $context The context
* @return array The navigation node
*/
public static function create_navigation_node(\context $context): array {
return [
'path' => base64_encode(json_encode(['contextid' => $context->id])),
'name' => $context->get_context_name(false)
];
}
}
@@ -0,0 +1,44 @@
<?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/>.
/**
* Privacy Subsystem implementation for repository_contentbank.
*
* @package repository_contentbank
* @copyright 2020 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace repository_contentbank\privacy;
/**
* Privacy Subsystem for repository_contentbank implementing null_provider.
*
* @copyright 2020 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}