MDL-76242 core_contentbank: Hide/show unlisted content

This commit is contained in:
Daniel Neis Araujo 2022-11-08 17:18:14 -03:00
parent 554a790bf0
commit 92e10322d4
10 changed files with 120 additions and 6 deletions

View File

@ -0,0 +1,3 @@
define("core_contentbank/displayunlisted",["exports","core/ajax"],(function(_exports,_ajax){Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.update=void 0;_exports.update=elementSelector=>{const element=document.querySelector(elementSelector);element.addEventListener("click",(function(){const args={userid:this.userId,preferences:[{type:"core_contentbank_displayunlisted",value:!!element.checked}]};(0,_ajax.call)([{methodname:"core_user_update_user_preferences",args:args}])[0].done((function(){document.location.reload()}))}))}}));
//# sourceMappingURL=displayunlisted.min.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"displayunlisted.min.js","sources":["../src/displayunlisted.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Module to handle AJAX interactions with content bank upload files.\n *\n * @module core_contentbank/displayunlisted\n * @copyright 2023 Daniel Neis Araujo\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nimport {call as fetchMany} from 'core/ajax';\n\n/**\n * Initialize upload files to the content bank form as Modal form.\n *\n * @param {String} elementSelector\n */\nexport const update = (elementSelector) => {\n const element = document.querySelector(elementSelector);\n element.addEventListener('click', function() {\n const args = {\n userid: this.userId,\n preferences: [\n {\n type: 'core_contentbank_displayunlisted',\n value: !!element.checked,\n }\n ],\n };\n fetchMany([{\n methodname: 'core_user_update_user_preferences',\n args: args,\n }])[0].done(function() {\n document.location.reload();\n });\n });\n};\n"],"names":["elementSelector","element","document","querySelector","addEventListener","args","userid","this","userId","preferences","type","value","checked","methodname","done","location","reload"],"mappings":"2LA6BuBA,wBACbC,QAAUC,SAASC,cAAcH,iBACvCC,QAAQG,iBAAiB,SAAS,iBACxBC,KAAO,CACTC,OAAQC,KAAKC,OACbC,YAAa,CACT,CACIC,KAAM,mCACNC,QAASV,QAAQW,0BAInB,CAAC,CACPC,WAAY,oCACZR,KAAMA,QACN,GAAGS,MAAK,WACRZ,SAASa,SAASC"}

View File

@ -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,
}
],
};
fetchMany([{
methodname: 'core_user_update_user_preferences',
args: args,
}])[0].done(function() {
document.location.reload();
});
});
};

View File

@ -393,6 +393,12 @@ abstract class content {
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);

View File

@ -122,6 +122,8 @@ class bankcontent implements renderable, templatable {
$method = 'export_tool_'.$tool['action'];
if (method_exists($this, $method)) {
$this->$method($tool);
} else {
$this->export_tool_default($tool);
}
$data->tools[] = $tool;
}
@ -207,4 +209,17 @@ class bankcontent implements renderable, templatable {
$tool['contenttypes'] = $addoptions;
}
/**
* This is the default output of a tool.
* It will be displayed as a button by default.
*
* @param array $tool Data for rendering the Add dropdown, including the editable content types.
* @return void
*/
private function export_tool_default(array &$tool) {
if (empty($tool['checkbox']) && empty($tool['dropdown'])) {
$tool['button'] = true;
}
}
}

View File

@ -75,11 +75,33 @@ foreach ($enabledcontenttypes as $contenttypename) {
}
}
$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.
@ -129,6 +151,8 @@ if (!empty($errormsg) && get_string_manager()->string_exists($errormsg, 'core_co
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);

View File

@ -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'],
],
];
}

View File

@ -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}}"

View File

@ -42,6 +42,7 @@ $string['contentupdated'] = 'Content updated.';
$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';

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2024071900.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2024071900.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.5dev (Build: 20240719)'; // Human-friendly version name