Skip to content

Commit

Permalink
MDL-76242 contentbank: Hide/show unlisted content
Browse files Browse the repository at this point in the history
  • Loading branch information
danielneis committed Nov 17, 2023
1 parent b58d1fd commit 5b378e5
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 5 deletions.
3 changes: 3 additions & 0 deletions contentbank/amd/build/displayunlisted.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/displayunlisted.min.js.map

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

49 changes: 49 additions & 0 deletions contentbank/amd/src/displayunlisted.js
@@ -0,0 +1,49 @@
// 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/displayunlisted
* @copyright 2023 Daniel Neis Araujo
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
import {call as fetchMany} from 'core/ajax';

/**
* Initialize upload files to the content bank form as Modal form.
*
* @param {String} elementSelector
*/
export const update = (elementSelector) => {
const element = document.querySelector(elementSelector);
element.addEventListener('click', function() {
const args = {
userid: this.userId,
preferences: [
{
type: 'core_contentbank_displayunlisted',
value: element.checked ? 1 : 0,
}
],
};
fetchMany([{
methodname: 'core_user_update_user_preferences',
args: args,
}])[0].done(function() {
document.location.reload();
});
});
};
6 changes: 6 additions & 0 deletions contentbank/classes/content.php
Expand Up @@ -393,6 +393,12 @@ public function is_view_allowed(): bool {
global $USER;
$context = \context::instance_by_id($this->get_contextid());

$displaypreference = get_user_preferences('core_contentbank_displayunlisted', 1);

if (($this->get_visibility() == self::VISIBILITY_UNLISTED) && !$displaypreference) {
return false;
}

return $USER->id == $this->content->usercreated ||
$this->get_visibility() == self::VISIBILITY_PUBLIC ||
has_capability('moodle/contentbank:viewunlistedcontent', $context);
Expand Down
8 changes: 8 additions & 0 deletions contentbank/classes/output/bankcontent.php
Expand Up @@ -122,6 +122,8 @@ public function export_for_template(renderer_base $output): stdClass {
$method = 'export_tool_'.$tool['action'];
if (method_exists($this, $method)) {
$this->$method($tool);
} else {
$this->export_tool_default($tool);
}
$data->tools[] = $tool;
}
Expand Down Expand Up @@ -207,4 +209,10 @@ private function export_tool_add(array &$tool) {

$tool['contenttypes'] = $addoptions;
}

private function export_tool_default(array &$tool) {
if (empty($tool['checkbox']) && empty($tool['dropdown'])) {
$tool['button'] = true;
}
}
}
28 changes: 26 additions & 2 deletions contentbank/index.php
Expand Up @@ -75,11 +75,33 @@
}
}

$foldercontents = $cb->search_contents($search, $contextid, $contenttypes);

// Get the toolbar ready.
$toolbar = array ();

if (has_capability('moodle/contentbank:viewunlistedcontent', $context)) {
$setdisplay = optional_param('displayunlisted', null, PARAM_INT);
if (is_null($setdisplay)) {
$display = get_user_preferences('core_contentbank_displayunlisted', 1);
} else {
set_user_preference('core_contentbank_displayunlisted', $setdisplay);
$display = $setdisplay;
}
$toolbar[] = [
'name' => 'displayunlisted',
'id' => 'displayunlisted',
'checkbox' => true,
'checked' => $display,
'label' => get_string('displayunlisted', 'contentbank'),
'class' => 'displayunlisted m-2',
'action' => 'displayunlisted',
];
$PAGE->requires->js_call_amd(
'core_contentbank/displayunlisted',
'update',
['[data-action=displayunlisted]']
);
}

// Place the Add button in the toolbar.
if (has_capability('moodle/contentbank:useeditor', $context)) {
// Get the content types for which the user can use an editor.
Expand Down Expand Up @@ -129,6 +151,8 @@
echo $OUTPUT->notification($statusmsg, 'notifysuccess');
}

$foldercontents = $cb->search_contents($search, $contextid, $contenttypes);

// Render the contentbank contents.
$folder = new \core_contentbank\output\bankcontent($foldercontents, $toolbar, $context, $cb);
echo $OUTPUT->render($folder);
Expand Down
9 changes: 8 additions & 1 deletion contentbank/lib.php
Expand Up @@ -44,6 +44,13 @@ function core_contentbank_user_preferences(): array {
'choices' => [content::VISIBILITY_UNLISTED, content::VISIBILITY_PUBLIC],
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED
]
],
'core_contentbank_displayunlisted' => [
'choices' => [0, 1],
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'default' => 0,
'permissioncallback' => [core_user::class, 'is_current_user'],
],
];
}
12 changes: 10 additions & 2 deletions contentbank/templates/bankcontent/toolbar.mustache
Expand Up @@ -58,11 +58,19 @@
{{#dropdown}}
{{>core_contentbank/bankcontent/toolbar_dropdown}}
{{/dropdown}}
{{^dropdown}}
{{#button}}
<a href="{{ link }}" class="icon-no-margin btn btn-secondary text-nowrap" title="{{ name }}" data-action="{{ action }}">
{{#pix}} {{{ icon }}} {{/pix}} {{{ name }}}
</a>
{{/dropdown}}
{{/button}}
{{#checkbox}}
<div class="mr-3 align-middle pt-1 text-nowrap">
<input type="checkbox" id="{{ id }}" class="{{ class }}" name="{{ name }}"
data-action="{{ action }}" {{#checked}}checked="checked"{{/checked}}>

<label for="{{ id }}">{{ label }}</label>
</div>
{{/checkbox}}
{{/tools}}
<button class="icon-no-margin btn btn-secondary {{^viewlist}}active{{/viewlist}} ml-2"
title="{{#str}} displayicons, contentbank {{/str}}"
Expand Down
1 change: 1 addition & 0 deletions lang/en/contentbank.php
Expand Up @@ -42,6 +42,7 @@
$string['contextnotallowed'] = 'You are not allowed to access the content bank in this context.';
$string['copycontent'] = 'Copy content';
$string['copyof'] = 'Copy of {$a}';
$string['displayunlisted'] = 'Show unlisted content';
$string['emptynamenotallowed'] = 'Empty name is not allowed';
$string['eventcontentcreated'] = 'Content created';
$string['eventcontentdeleted'] = 'Content deleted';
Expand Down

0 comments on commit 5b378e5

Please sign in to comment.