Skip to content

Commit

Permalink
MDL-68796 core_contentbank: Store view preferences
Browse files Browse the repository at this point in the history
includes privacy export, privacy unit test
  • Loading branch information
Bas Brands committed May 27, 2020
1 parent 68fd8d8 commit c393d81
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 8 deletions.
2 changes: 1 addition & 1 deletion contentbank/amd/build/sort.min.js

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

2 changes: 1 addition & 1 deletion contentbank/amd/build/sort.min.js.map

Large diffs are not rendered by default.

35 changes: 34 additions & 1 deletion contentbank/amd/src/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
* @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.
Expand Down Expand Up @@ -59,13 +61,15 @@ const registerListenerEvents = (contentBank) => {
contentBank.classList.add('view-grid');
viewGrid.classList.add('active');
viewList.classList.remove('active');
setViewListPreference(false);
});

viewList.addEventListener('click', () => {
contentBank.classList.remove('view-grid');
contentBank.classList.add('view-list');
viewList.classList.add('active');
viewGrid.classList.remove('active');
setViewListPreference(true);
});

// Sort by file name alphabetical
Expand Down Expand Up @@ -97,6 +101,35 @@ const registerListenerEvents = (contentBank) => {
});
};


/**
* 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.
*
Expand Down
1 change: 1 addition & 0 deletions contentbank/classes/output/bankcontent.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function export_for_template(renderer_base $output): stdClass {
'type' => $mimetype
);
}
$data->viewlist = get_user_preferences('core_contentbank_view_list');
$data->contents = $contentdata;
$data->tools = $this->toolbar;
return $data;
Expand Down
23 changes: 22 additions & 1 deletion contentbank/classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
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.
Expand All @@ -65,6 +66,26 @@ public static function get_metadata(collection $collection): collection {
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.
*
Expand Down
39 changes: 39 additions & 0 deletions contentbank/lib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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'
],
];
}
3 changes: 2 additions & 1 deletion contentbank/templates/bankcontent.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
}

}}
<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}}
Expand Down
4 changes: 2 additions & 2 deletions contentbank/templates/bankcontent/toolbar.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
{{#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}}
Expand Down
28 changes: 28 additions & 0 deletions contentbank/tests/behat/view_preferences.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@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"
47 changes: 47 additions & 0 deletions contentbank/tests/privacy_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;
Expand Down Expand Up @@ -361,4 +362,50 @@ protected function setup_scenario() {

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
);
}
}
1 change: 1 addition & 0 deletions lang/en/contentbank.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
$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';
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ protected static function fill_preferences_cache() {
// 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;
Expand Down

0 comments on commit c393d81

Please sign in to comment.