Commit a83c69c2 authored by Adrian Greeve's avatar Adrian Greeve
Browse files

Merge branch 'MDL-69549-master-7' of git://github.com/andrewnicols/moodle

parents f720b77a d6c5df42
......@@ -277,6 +277,11 @@ $string['confirmcheckfull'] = 'Are you absolutely sure you want to confirm {$a}
$string['confirmcoursemove'] = 'Are you sure you want to move this course ({$a->course}) into this category ({$a->category})?';
$string['considereddigitalminor'] = 'You are too young to create an account on this site.';
$string['content'] = 'Content';
$string['contentexport_aboutthiscourse'] = 'Course summary';
$string['contentexport_coursesummary'] = 'This file is part of the content downloaded from <a href="{$a->courselink}">{$a->coursename}</a>.';
$string['contentexport_footersummary'] = 'This file is part of the content downloaded from <a href="{$a->courselink}">{$a->coursename}</a> by {$a->userfullname} on {$a->date}';
$string['contentexport_modulesummary'] = 'This page is part of the content downloaded from <a href="{$a->modulelink}">{$a->modulename}</a> on {$a->date}. Note that some content and any files larger than {$a->maxfilesize} are not downloaded.';
$string['contentexport_viewfilename'] = 'View the file {$a}';
$string['contentbank'] = 'Content bank';
$string['continue'] = 'Continue';
$string['continuetocourse'] = 'Click here to enter your course';
......
<?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 API File Area definition.
*
* @package core_files
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use coding_exception;
use context;
use core\content\export\exporters\course_exporter;
use core\content\export\exporters\component_exporter;
use core\content\export\exporters\abstract_mod_exporter;
use core\content\export\zipwriter;
use core_component;
use moodle_url;
use stdClass;
use stored_file;
/**
* The Content API allows all parts of Moodle to determine details about content within a component, or plugintype.
*
* This includes the description of files.
*
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class content {
/**
* Check whether the specified user can export content for the specified context.
*
* @param context $currentcontext
* @param stdClass $user
* @return bool
*/
public static function can_export_context(context $currentcontext, stdClass $user): bool {
return true;
}
/**
* Export content for the specified context.
*
* @param context $requestedcontext The context to be exported
* @param stdClass $user The user being exported
* @param zipwriter $archive The Zip Archive to export to
*/
public static function export_context(context $requestedcontext, stdClass $user, zipwriter $archive): void {
global $USER;
if ($requestedcontext->contextlevel != CONTEXT_COURSE) {
throw new coding_exception('The Content Export API currently only supports the export of courses');
}
if ($USER->id != $user->id) {
throw new coding_exception('The Content Export API currently only supports export of the current user');
}
// Ensure that the zipwriter is aware of the requested context.
$archive->set_root_context($requestedcontext);
// Fetch all child contexts, indexed by path.
$contextlist = [
$requestedcontext->path => $requestedcontext,
];
foreach ($requestedcontext->get_child_contexts() as $context) {
$contextlist[$context->path] = $context;
}
// Reverse the order by key - this ensures that child contexts are processed before their parent.
krsort($contextlist);
// Get the course modinfo.
$modinfo = get_fast_modinfo($requestedcontext->instanceid);
// Filter out any context which cannot be exported.
$contextlist = array_filter($contextlist, function($context) use ($user, $modinfo): bool {
if ($context->contextlevel == CONTEXT_COURSE) {
return self::can_export_context($context, $user);
}
if ($context->contextlevel == CONTEXT_MODULE) {
if (empty($modinfo->cms[$context->instanceid])) {
// Unknown coursemodule in the course.
return false;
}
$cm = $modinfo->cms[$context->instanceid];
if (!$cm->uservisible) {
// This user cannot view the activity.
return false;
}
// Defer to setting checks.
return self::can_export_context($context, $user);
}
// Only course and activities are supported at this time.
return false;
});
// Export each context.
$exportedcontexts = [];
$coursecontroller = new course_exporter($requestedcontext->get_course_context(), $user, $archive);
foreach ($contextlist as $context) {
if ($context->contextlevel === CONTEXT_MODULE) {
$cm = $modinfo->cms[$context->instanceid];
$component = "mod_{$cm->modname}";
// Check for a specific implementation for this module.
// This will export any content specific to this activity.
// For example, in mod_folder it will export the list of folders.
$classname = component_exporter::get_classname_for_component($component);
$exportables = [];
if (class_exists($classname) && is_a($classname, abstract_mod_exporter::class, true)) {
$controller = new $classname($context, $component, $user, $archive);
$exportables = $controller->get_exportables();
}
// Pass the exportable content to the course controller for export.
$coursecontroller->export_mod_content($context, $exportables);
$exportedcontexts[$context->id] = $context;
} else if ($context->contextlevel === CONTEXT_COURSE) {
// Export the course content.
$coursecontroller->export_course($exportedcontexts);
}
}
$archive->finish();
}
}
<?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/>.
/**
* The definition of an item which can be exported.
*
* @package core
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
declare(strict_types=1);
namespace core\content\export;
use context;
use core\content\export\exported_item;
use core\content\export\zipwriter;
/**
* An object used to represent content which can be served.
*
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class exportable_item {
/** @var context The context associated with this exportable item */
protected $context = null;
/** @var string The component being exported */
protected $component = null;
/** @var string The name displayed to the user */
protected $uservisiblename = null;
/**
* Create a new exportable_item instance.
*
* @param context $context The context that this content belongs to
* @param string $component The component that this content relates to
* @param string $uservisiblename The name displayed in the export
*/
public function __construct(context $context, string $component, string $uservisiblename) {
$this->context = $context;
$this->component = $component;
$this->uservisiblename = $uservisiblename;
}
/**
* Get the context that this exportable item is for.
*
* @return context
*/
public function get_context(): context {
return $this->context;
}
/**
* Get the component that this exportable item relates to.
*
* @return string
*/
public function get_component(): string {
return $this->component;
}
/**
* Get the user visible name for the exportable item.
*
* @return string
*/
public function get_user_visible_name(): string {
return $this->uservisiblename;
}
/**
* Add the content to the archive.
*
* @param zipwriter $archive
*/
abstract public function add_to_archive(zipwriter $archive): ?exported_item;
}
<?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/>.
/**
* The definition of a set of files in a filearea to be exported.
*
* @package core
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
declare(strict_types=1);
namespace core\content\export\exportable_items;
use context;
use core\content\export\exportable_item;
use core\content\export\exported_item;
use core\content\export\zipwriter;
use moodle_url;
use stored_file;
/**
* The definition of a set of files in a filearea to be exported.
*
* All files mustbe in a single filearea and itemid combination.
*
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class exportable_filearea extends exportable_item {
/** @var string The destination path of the text content */
protected $folderpath;
/** @var string $filearea The file to be exported */
protected $filearea;
/** @var bool|int The itemid in the Files API */
protected $itemid;
/** @var int The itemid to use in the pluginfile URL */
protected $pluginfileitemid;
/**
* Create a new exportable_item instance.
*
* If no filearea or itemid is specified the no attempt will be made to export files.
*
* @param context $context The context that this content belongs to
* @param string $component
* @param string $uservisiblename The name displayed to the user when filtering
* @param string $filearea The file area in the Files API where these files are located
* @param int $itemid The itemid in the Files API where these files are located
* @param null|int $pluginfileitemid The itemid as used in the Pluginfile URL
* @param string $folderpath Any sub-directory to place files in
*/
public function __construct(
context $context,
string $component,
string $uservisiblename,
string $filearea,
int $itemid,
?int $pluginfileitemid = null,
string $folderpath = ''
) {
parent::__construct($context, $component, $uservisiblename);
$this->filearea = $filearea;
$this->itemid = $itemid;
$this->pluginfileitemid = $pluginfileitemid;
$this->folderpath = $folderpath;
}
/**
* Add the content to the archive.
*
* @param zipwriter $archive
*/
public function add_to_archive(zipwriter $archive): ?exported_item {
$fs = get_file_storage();
$files = $fs->get_area_files($this->context->id, $this->component, $this->filearea, $this->itemid);
$exporteditem = new exported_item();
$exporteditem->set_title($this->get_user_visible_name());
foreach ($files as $file) {
if ($file->is_directory()) {
// Skip folders. The zipwriter cannot handle them.
continue;
}
// Export the content to [contextpath]/[filepath].
$relativefilepath = $this->get_filepath_for_file($file);
$archive->add_file_from_stored_file(
$this->get_context(),
$relativefilepath,
$file
);
if ($archive->is_file_in_archive($this->context, $relativefilepath)) {
// The file was successfully added to the archive.
$exporteditem->add_file($relativefilepath, false);
} else {
// The file was not added. Link to the live version instead.
$exporteditem->add_file(
$relativefilepath,
false,
self::get_pluginfile_url_for_stored_file($file, $this->pluginfileitemid)
);
}
}
return $exporteditem;
}
/**
* Get the filepath for the specified stored_file.
*
* @param stored_file $file The file to get a filepath for
* @return string The generated filepath
*/
protected function get_filepath_for_file(stored_file $file): string {
$folderpath = rtrim($this->folderpath);
if (!empty($folderpath)) {
$folderpath .= '/';
}
return sprintf(
'%s%s%s%s',
$folderpath,
$file->get_filearea(),
$file->get_filepath(),
$file->get_filename()
);
}
/**
* Get the pluginfile URL for a stored file.
*
* Note: The itemid in the pluginfile may be omitted in some URLs, despite an itemid being present in the database.
* Equally, the itemid in the URL may not match the itemid in the files table.
*
* The pluginfileitemid argument provided to this function is the variant in the URL, and not the one in the files
* table.
*
* @param stored_file $file The file whose link will be generated
* @param null|int $pluginfileitemid The itemid of the file in pluginfile URL.
*
*/
protected static function get_pluginfile_url_for_stored_file(stored_file $file, ?int $pluginfileitemid): string {
$link = moodle_url::make_pluginfile_url(
$file->get_contextid(),
$file->get_component(),
$file->get_filearea(),
$pluginfileitemid,
$file->get_filepath(),
$file->get_filename(),
true,
true
);
return $link->out(false);
}
}
<?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/>.
/**
* The definition of an item which can be exported.
*
* @package core
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
declare(strict_types=1);
namespace core\content\export\exportable_items;
use context;
use core\content\export\exportable_item;
use core\content\export\exported_item;
use core\content\export\zipwriter;
use moodle_url;
use stored_file;
/**
* An object used to represent content which can be served.
*
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class exportable_stored_file extends exportable_item {
/** @var string The destination path of the text content */
protected $folderpath;
/** @var stored_file The file to be exported */
protected $file;
/** @var int The itemid to use in the pluginfile URL */
protected $pluginfileitemid;
/**
* Create a new exportable_item instance.
*
* If no filearea or itemid is specified the no attempt will be made to export files.
*
* @param context $context The context that this content belongs to
* @param string $component
* @param string $uservisiblename The name displayed to the user when filtering
* @param stored_file $file
* @param null|int $pluginfileitemid The itemid as used in the pluginfile URL.
* If no itemid is used, then a null value can be provided
* @param string $folderpath Any sub-directory to place files in
*/
public function __construct(
context $context,
string $component,
string $uservisiblename,
stored_file $file,
?int $pluginfileitemid = null,
string $folderpath = ''
) {
parent::__construct($context, $component, $uservisiblename);
$this->file = $file;
$this->folderpath = $folderpath;
$this->pluginfileitemid = $pluginfileitemid;
}
/**
* Create a set of exportable_items from a set of area paramaters as passed to get_areas_files().
*
* If no filearea or itemid is specified the no attempt will be made to export files.
*
* @param context $context The context that this content belongs to
* @param string $component
* @param string $filearea
* @param null|int $itemid
* @param null|int $pluginfileitemid The itemid as used in the pluginfile URL.
* If no itemid is used, then a null value can be provided
* @param string $folderpath Any sub-directory to place files in
* @return array
*/
public static function create_from_area_params(
context $context,
string $component,
string $filearea,
?int $itemid,
?int $pluginfileitemid = null,
string $folderpath = ''
): array {
$fs = get_file_storage();
if ($itemid === null) {
$itemid = false;
}
$exportables = [];
foreach ($fs->get_area_files($context->id, $component, $filearea, $itemid) as $file) {
if ($file->is_directory()) {
// Do not export directories.
// If they contain file contents the directory structure will be created in the zip file.
continue;
}
$filepath = $file->get_filepath() . $file->get_filename();
$exportables[] = new self($context, $component, $filepath, $file, $pluginfileitemid, $folderpath);
}
return $exportables;
}
/**
* Add the content to the archive.
*
* @param zipwriter $archive
*/
public function add_to_archive(zipwriter $archive): ?exported_item {
// Export the content to [contextpath]/[filepath].
$relativefilepath = $this->get_filepath_for_file();
$archive->add_file_from_stored_file(
$this->get_context(),
$relativefilepath,
$this->file
);
$exporteditem = new exported_item();
$exporteditem->set_title($this->get_user_visible_name());
if ($archive->is_file_in_archive($this->context, $relativefilepath)) {
// The file was successfully added to the archive.
$exporteditem->add_file($relativefilepath, false);
} else {
// The file was not added. Link to the live version instead.
$exporteditem->add_file(
$relativefilepath,
false,
self::get_pluginfile_url_for_stored_file($this->file, $this->pluginfileitemid)
);
}
return $exporteditem;
}
/**
* Get the filepath for the specified stored_file.
*
* @return string
*/
protected function get_filepath_for_file(): string {
$folderpath = rtrim($this->folderpath);
if (!empty($folderpath)) {
$folderpath .= '/';
}
return sprintf(
'%s%s%s%s',