* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-import selectors from 'core_contentbank/selectors';
+import selectors from './selectors';
import {get_string as getString} from 'core/str';
import Prefetch from 'core/prefetch';
+import Ajax from 'core/ajax';
+import Notification from 'core/notification';
/**
* Set up the contentbank views.
contentBank.classList.add('view-grid');
viewGrid.classList.add('active');
viewList.classList.remove('active');
+ setViewListPreference(false);
});
viewList.addEventListener('click', () => {
contentBank.classList.add('view-list');
viewList.classList.add('active');
viewGrid.classList.remove('active');
+ setViewListPreference(true);
});
// Sort by file name alphabetical
});
};
+
+/**
+ * Set the contentbank user preference in list view
+ *
+ * @param {Bool} viewList view ContentBank as list.
+ * @return {Promise} Repository promise.
+ */
+const setViewListPreference = function(viewList) {
+
+ // If the given status is not hidden, the preference has to be deleted with a null value.
+ if (viewList === false) {
+ viewList = null;
+ }
+
+ const request = {
+ methodname: 'core_user_update_user_preferences',
+ args: {
+ preferences: [
+ {
+ type: 'core_contentbank_view_list',
+ value: viewList
+ }
+ ]
+ }
+ };
+
+ return Ajax.call([request])[0].catch(Notification.exception);
+};
+
/**
* Update the sort button view.
*
'type' => $mimetype
);
}
+ $data->viewlist = get_user_preferences('core_contentbank_view_list');
$data->contents = $contentdata;
$data->tools = $this->toolbar;
return $data;
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\core_userlist_provider,
- \core_privacy\local\request\plugin\provider {
+ \core_privacy\local\request\plugin\provider,
+ \core_privacy\local\request\user_preference_provider {
/**
* Returns meta data about this system.
return $collection;
}
+ /**
+ * Export all user preferences for the contentbank
+ *
+ * @param int $userid The userid of the user whose data is to be exported.
+ */
+ public static function export_user_preferences(int $userid) {
+ $preference = get_user_preferences('core_contentbank_view_list', null, $userid);
+ if (isset($preference)) {
+ writer::export_user_preference(
+ 'core_contentbank',
+ 'core_contentbank_view_list',
+ $preference,
+ get_string('privacy:request:preference:set', 'core_contentbank', (object) [
+ 'name' => 'core_contentbank_view_list',
+ 'value' => $preference,
+ ])
+ );
+ }
+ }
+
/**
* Get the list of contexts that contain user information for the specified user.
*
--- /dev/null
+<?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/>.
+
+/**
+ * Library functions for contentbank
+ *
+ * @package core_contentbank
+ * @copyright 2020 Bas Brands
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+/**
+ * Get the current user preferences that are available
+ *
+ * @return Array preferences configuration
+ */
+function core_contentbank_user_preferences() {
+ return [
+ 'core_contentbank_view_list' => [
+ 'choices' => array(0, 1),
+ 'type' => PARAM_INT,
+ 'null' => NULL_NOT_ALLOWED,
+ 'default' => 'none'
+ ],
+ ];
+}
}
}}
-<div class="content-bank-container view-grid" data-region="contentbank">
+<div class="content-bank-container {{#viewlist}}view-list{{/viewlist}} {{^viewlist}}view-grid{{/viewlist}}"
+data-region="contentbank">
<div class="d-flex justify-content-between flex-column flex-sm-row">
<div class="cb-search-container mb-2">
{{>core_contentbank/bankcontent/search}}
{{#pix}} {{{ icon }}} {{/pix}} {{{ name }}}
</a>
{{/tools}}
-<button class="icon-no-margin btn btn-secondary active ml-2"
+<button class="icon-no-margin btn btn-secondary {{^viewlist}}active{{/viewlist}} ml-2"
title="{{#str}} displayicons, contentbank {{/str}}"
data-action="viewgrid">
{{#pix}}a/view_icon_active, core, {{#str}} displayicons, contentbank {{/str}} {{/pix}}
</button>
-<button class="icon-no-margin btn btn-secondary"
+<button class="icon-no-margin btn btn-secondary {{#viewlist}}active{{/viewlist}}"
title="{{#str}} displaydetails, contentbank {{/str}}"
data-action="viewlist">
{{#pix}}t/viewdetails, core, {{#str}} displaydetails, contentbank {{/str}} {{/pix}}
--- /dev/null
+@core @core_contentbank @contentbank_h5p @javascript
+Feature: Store the content bank view preference
+ In order to consistantly view the content bank in icons or details view
+ As an admin
+ I need to be able to store my view preference
+
+ Background:
+ Given the following "contentbank content" exist:
+ | contextlevel | reference | contenttype | user | contentname |
+ | System | | contenttype_h5p | admin | filltheblanks.h5p |
+ | System | | contenttype_h5p | admin | mathsbook.h5p |
+
+ Scenario: Admins can order content in the content bank
+ Given I log in as "admin"
+ And I am on site homepage
+ And I turn editing mode on
+ And I add the "Navigation" block if not present
+ And I expand "Site pages" node
+ And I click on "Content bank" "link"
+ When I click on "Display contentbank with file details" "button"
+ And I should see "Last modified"
+ And I follow "filltheblanks.h5p"
+ And I click on "Content bank" "link"
+ And I should see "Last modified"
+ And I click on "Display contentbank with icons" "button"
+ And I follow "filltheblanks.h5p"
+ And I click on "Content bank" "link"
+ And I should not see "Last modified"
use context_system;
use context_coursecat;
use context_course;
+use context_user;
use core_contentbank\privacy\provider;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\writer;
return $scenario;
}
+
+ /**
+ * Ensure that export_user_preferences returns no data if the user has not visited any content bank.
+ */
+ public function test_export_user_preferences_no_pref() {
+ global $DB;
+
+ $this->resetAfterTest();
+ $user = $this->getDataGenerator()->create_user();
+ $managerroleid = $DB->get_field('role', 'id', ['shortname' => 'manager']);
+ $this->getDataGenerator()->role_assign($managerroleid, $user->id);
+
+ provider::export_user_preferences($user->id);
+ $writer = writer::with_context(context_system::instance());
+ $this->assertFalse($writer->has_any_data());
+ }
+
+ /**
+ * Test for provider::test_export_user_preferences().
+ */
+ public function test_export_user_preferences() {
+ global $DB;
+
+ // Test setup.
+ $this->resetAfterTest(true);
+ $user = $this->getDataGenerator()->create_user();
+ $this->setUser($user);
+
+ set_user_preference('core_contentbank_view_list', 1);
+ // Test the user preferences export contains 1 user preference record for the User.
+ provider::export_user_preferences($user->id);
+ $contextuser = context_user::instance($user->id);
+ $writer = writer::with_context($contextuser);
+ $this->assertTrue($writer->has_any_data());
+
+ $prefs = $writer->get_user_preferences('core_contentbank');
+ $this->assertCount(1, (array) $prefs);
+ $this->assertEquals(1, $prefs->core_contentbank_view_list->value);
+ $this->assertEquals(
+ get_string('privacy:request:preference:set', 'core_contentbank', (object) [
+ 'name' => 'core_contentbank_view_list',
+ 'value' => $prefs->core_contentbank_view_list->value,
+ ]),
+ $prefs->core_contentbank_view_list->description
+ );
+ }
}
$string['privacy:metadata:content:usermodified'] = 'The last user who modified the content.';
$string['privacy:metadata:contentbankcontent'] = 'Stores the content of the content bank.';
$string['privacy:metadata:userid'] = 'The ID of the user creating or modifying content bank content.';
+$string['privacy:request:preference:set'] = 'The value of the setting \'{$a->name}\' was \'{$a->value}\'';
$string['rename'] = 'Rename';
$string['renamecontent'] = 'Rename content';
$string['searchcontentbankbyname'] = 'Search for content by name';
// Core components that may want to define their preferences.
// List of core components implementing callback is hardcoded here for performance reasons.
// TODO MDL-58184 cache list of core components implementing a function.
- $corecomponents = ['core_message', 'core_calendar'];
+ $corecomponents = ['core_message', 'core_calendar', 'core_contentbank'];
foreach ($corecomponents as $component) {
if (($pluginpreferences = component_callback($component, 'user_preferences')) && is_array($pluginpreferences)) {
$preferences += $pluginpreferences;