Skip to content

Commit

Permalink
Merge branch 'MDL-67999-master' of git://github.com/sarjona/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Apr 14, 2021
2 parents 9ee8005 + e5872a3 commit 41d9dc4
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 208 deletions.
2 changes: 2 additions & 0 deletions contentbank/amd/build/upload.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contentbank/amd/build/upload.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions contentbank/amd/src/upload.js
@@ -0,0 +1,52 @@
// 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/>.

/**
* Module to handle AJAX interactions with content bank upload files.
*
* @module core_contentbank/upload
* @copyright 2021 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
import ModalForm from 'core_form/modalform';
import {get_string as getString} from 'core/str';

/**
* Initialize upload files to the content bank form as Modal form.
*
* @param {String} elementSelector
* @param {String} formClass
* @param {Integer} contextId
* @param {Integer} contentId
*/
export const initModal = (elementSelector, formClass, contextId, contentId) => {
const element = document.querySelector(elementSelector);
element.addEventListener('click', function(e) {
e.preventDefault();
const form = new ModalForm({
formClass,
args: {
contextid: contextId,
id: contentId,
},
modalConfig: {title: getString('upload', 'contentbank')},
returnFocus: e.target,
});
form.addEventListener(form.events.FORM_SUBMITTED, (event) => {
document.location = event.detail.returnurl;
});
form.show();
});
};
209 changes: 209 additions & 0 deletions contentbank/classes/form/upload_files.php
@@ -0,0 +1,209 @@
<?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\form;

/**
* Upload files to content bank form
*
* @package core_contentbank
* @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class upload_files extends \core_form\dynamic_form {

/**
* Add elements to this form.
*/
public function definition() {
$mform = $this->_form;

$mform->addElement('hidden', 'contextid');
$mform->setType('contextid', PARAM_INT);

$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);

$mform->addElement('filepicker', 'file', get_string('file', 'core_contentbank'), null, $this->get_options());
$mform->addHelpButton('file', 'file', 'core_contentbank');
$mform->addRule('file', null, 'required');
}

/**
* Validate incoming data.
*
* @param array $data
* @param array $files
* @return array
*/
public function validation($data, $files) {
$errors = array();
$draftitemid = $data['file'];
$options = $this->get_options();
if (file_is_draft_area_limit_reached($draftitemid, $options['areamaxbytes'])) {
$errors['file'] = get_string('userquotalimit', 'error');
}
return $errors;
}

/**
* Check if current user has access to this form, otherwise throw exception
*
* Sometimes permission check may depend on the action and/or id of the entity.
* If necessary, form data is available in $this->_ajaxformdata or
* by calling $this->optional_param()
*/
protected function check_access_for_dynamic_submission(): void {
require_capability('moodle/contentbank:upload', $this->get_context_for_dynamic_submission());

// Check the context used by the content bank is allowed.
$cb = new \core_contentbank\contentbank();
if (!$cb->is_context_allowed($this->get_context_for_dynamic_submission())) {
throw new \moodle_exception('contextnotallowed', 'core_contentbank');
}

// If $id is defined, the file content will be replaced (instead of uploading a new one).
// Check that the user has the right permissions to replace this content file.
$id = $this->optional_param('id', null, PARAM_INT);
if ($id) {
$content = $cb->get_content_from_id($id);
$contenttype = $content->get_content_type_instance();
if (!$contenttype->can_manage($content) || !$contenttype->can_upload()) {
throw new \moodle_exception('nopermissions', 'error', '', null, get_string('replacecontent', 'contentbank'));
}
}
}

/**
* Returns form context
*
* If context depends on the form data, it is available in $this->_ajaxformdata or
* by calling $this->optional_param()
*
* @return \context
*/
protected function get_context_for_dynamic_submission(): \context {
$contextid = $this->optional_param('contextid', null, PARAM_INT);
return \context::instance_by_id($contextid, MUST_EXIST);
}

/**
* File upload options
*
* @return array
* @throws \coding_exception
*/
protected function get_options(): array {
global $CFG;

$maxbytes = $CFG->userquota;
$maxareabytes = $CFG->userquota;
if (has_capability('moodle/user:ignoreuserquota', $this->get_context_for_dynamic_submission())) {
$maxbytes = USER_CAN_IGNORE_FILE_SIZE_LIMITS;
$maxareabytes = FILE_AREA_MAX_BYTES_UNLIMITED;
}

$cb = new \core_contentbank\contentbank();
$id = $this->optional_param('id', null, PARAM_INT);
if ($id) {
$content = $cb->get_content_from_id($id);
$contenttype = $content->get_content_type_instance();
$extensions = $contenttype->get_manageable_extensions();
$acceptedtypes = implode(',', $extensions);
} else {
$acceptedtypes = $cb->get_supported_extensions_as_string($this->get_context_for_dynamic_submission());
}

return ['subdirs' => 1, 'maxbytes' => $maxbytes, 'maxfiles' => -1, 'accepted_types' => $acceptedtypes,
'areamaxbytes' => $maxareabytes];
}

/**
* Process the form submission, used if form was submitted via AJAX
*
* This method can return scalar values or arrays that can be json-encoded, they will be passed to the caller JS.
*
* Submission data can be accessed as: $this->get_data()
*
* @return mixed
*/
public function process_dynamic_submission() {
global $USER;

// Get the file and create the content based on it.
$usercontext = \context_user::instance($USER->id);
$fs = get_file_storage();
$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $this->get_data()->file, 'itemid, filepath,
filename', false);
if (!empty($files)) {
$file = reset($files);
$cb = new \core_contentbank\contentbank();
if ($this->get_data()->id) {
$content = $cb->get_content_from_id($this->get_data()->id);
$contenttype = $content->get_content_type_instance();
$content = $contenttype->replace_content($file, $content);
} else {
$content = $cb->create_content_from_file($this->get_context_for_dynamic_submission(), $USER->id, $file);
}
$params = ['id' => $content->get_id(), 'contextid' => $this->get_context_for_dynamic_submission()->id];
$viewurl = new \moodle_url('/contentbank/view.php', $params);
return ['returnurl' => $viewurl->out(false)];
}

return null;
}

/**
* Load in existing data as form defaults
*
* Can be overridden to retrieve existing values from db by entity id and also
* to preprocess editor and filemanager elements
*
* Example:
* $this->set_data(get_entity($this->_ajaxformdata['id']));
*/
public function set_data_for_dynamic_submission(): void {
$data = (object)[
'contextid' => $this->optional_param('contextid', null, PARAM_INT),
'id' => $this->optional_param('id', null, PARAM_INT),
];
$this->set_data($data);
}

/**
* Returns url to set in $PAGE->set_url() when form is being rendered or submitted via AJAX
*
* This is used in the form elements sensitive to the page url, such as Atto autosave in 'editor'
*
* If the form has arguments (such as 'id' of the element being edited), the URL should
* also have respective argument.
*
* @return \moodle_url
*/
protected function get_page_url_for_dynamic_submission(): \moodle_url {
$params = ['contextid' => $this->get_context_for_dynamic_submission()->id];

$id = $this->optional_param('id', null, PARAM_INT);
if ($id) {
$url = '/contentbank/view.php';
$params['id'] = $id;
} else {
$url = '/contentbank/index.php';
}

return new \moodle_url($url, $params);
}
}
78 changes: 0 additions & 78 deletions contentbank/files_form.php

This file was deleted.

9 changes: 7 additions & 2 deletions contentbank/index.php
Expand Up @@ -88,13 +88,18 @@
// Don' show upload button if there's no plugin to support any file extension.
$accepted = $cb->get_supported_extensions_as_string($context);
if (!empty($accepted)) {
$importurl = new moodle_url('/contentbank/upload.php', ['contextid' => $contextid]);
$importurl = new moodle_url('/contentbank/index.php', ['contextid' => $contextid]);
$toolbar[] = [
'name' => get_string('upload', 'contentbank'),
'link' => $importurl,
'link' => $importurl->out(false),
'icon' => 'i/upload',
'action' => 'upload'
];
$PAGE->requires->js_call_amd(
'core_contentbank/upload',
'initModal',
['[data-action=upload]', \core_contentbank\form\upload_files::class, $contextid]
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion contentbank/templates/bankcontent/toolbar.mustache
Expand Up @@ -59,7 +59,7 @@
{{>core_contentbank/bankcontent/toolbar_dropdown}}
{{/dropdown}}
{{^dropdown}}
<a href="{{{ link }}}" class="icon-no-margin btn btn-secondary" title="{{{ name }}}">
<a href="{{ link }}" class="icon-no-margin btn btn-secondary" title="{{ name }}" data-action="{{ action }}">
{{#pix}} {{{ icon }}} {{/pix}} {{{ name }}}
</a>
{{/dropdown}}
Expand Down
2 changes: 2 additions & 0 deletions contentbank/upgrade.txt
Expand Up @@ -4,3 +4,5 @@ information provided here is intended especially for developers.
=== 3.11 ===
* Added "get_uses()" method to content class to return places where a content is used.
* Added set_visibility()/get_visibility() methods to let users decide if their content should be listed in the content bank.
* The contentbank/upload.php page for displaying the upload files form has been removed. The form for uploading/replacing
files now is displayed in a modal.

0 comments on commit 41d9dc4

Please sign in to comment.