Skip to content

Commit

Permalink
MDL-70911 core_badges: Add support to backpacks sortorder
Browse files Browse the repository at this point in the history
Before removing $CFG->badges_site_backpack setting, admins should be
able to re-order the existing site-backpacks (because then, the first
one will be treated as the default one).
This patch adds the sort order feature to the backpack list.
  • Loading branch information
sarjona committed Mar 10, 2021
1 parent a306fd7 commit 066e998
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 5 deletions.
11 changes: 11 additions & 0 deletions badges/backpacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@
} else {
$msg = get_string('sitebackpacknotdeleted', 'badges');
}
} else if ($action == 'moveup' || $action == 'movedown') {
// If no backpack has been selected, there isn't anything to move.
if (empty($id)) {
redirect($url);
}

$direction = BACKPACK_MOVE_DOWN;
if ($action == 'moveup') {
$direction = BACKPACK_MOVE_UP;
}
badges_change_sortorder_backpacks($id, $direction);
}

if ($action == 'edit') {
Expand Down
6 changes: 6 additions & 0 deletions badges/classes/output/external_backpacks_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public function export_for_template(\renderer_base $output) {

$PAGE->requires->js_call_amd('core_badges/backpackactions', 'init');

$rownumber = 0;
$rowcount = count($this->backpacks);

$data = new \stdClass();
$data->baseurl = $this->url;
$data->backpacks = array();
Expand All @@ -69,8 +72,11 @@ public function export_for_template(\renderer_base $output) {
$backpack = $exporter->export($output);
$backpack->cantest = ($backpack->apiversion == OPEN_BADGES_V2);
$backpack->iscurrent = ($backpack->id == $CFG->badges_site_backpack);
$backpack->canmoveup = $rownumber > 0;
$backpack->canmovedown = $rownumber < $rowcount - 1;

$data->backpacks[] = $backpack;
$rownumber++;
}

return $data;
Expand Down
25 changes: 23 additions & 2 deletions badges/templates/external_backpacks_page.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
Example context (json):
{
"backpacks": [
{"backpackweburl": "http://localhost/", "sitebackpack": true, "cantest": true}
{
"backpackweburl": "http://localhost/",
"sitebackpack": true,
"cantest": true,
"canmoveup": true,
"canmovedown": false
}
]
}
}}
Expand All @@ -41,6 +47,7 @@
<tr>
<th scope="col">{{#str}}backpackweburl, core_badges{{/str}}</th>
<th scope="col">{{#str}}sitebackpack, core_badges{{/str}}</th>
<th scope="col">{{#str}}order{{/str}}</th>
<th scope="col">{{#str}}actions, core_badges{{/str}}</th>
</tr>
</thead>
Expand All @@ -49,6 +56,20 @@
<tr data-backpackurl="{{{backpackweburl}}}">
<td> {{{backpackweburl}}} </td>
<td> {{#sitebackpack}}Yes{{/sitebackpack}} </td>
<td>
{{#canmoveup}}
<a href="{{baseurl}}?id={{id}}&action=moveup">{{#pix}}t/up, core,{{#str}}moveup{{/str}}{{/pix}}</a>
{{/canmoveup}}
{{^canmoveup}}
{{#pix}}spacer, moodle{{/pix}}
{{/canmoveup}}
{{#canmovedown}}
<a href="{{baseurl}}?id={{id}}&action=movedown">{{#pix}}t/down, core,{{#str}}movedown{{/str}}{{/pix}}</a>
{{/canmovedown}}
{{^canmovedown}}
{{#pix}}spacer, moodle{{/pix}}
{{/canmovedown}}
</td>
<td>
<a href="{{baseurl}}?id={{id}}&action=edit">{{#pix}}t/edit, core,{{#str}}editsettings{{/str}}{{/pix}}</a>
{{^iscurrent}}
Expand All @@ -63,4 +84,4 @@
</tr>
{{/backpacks}}
</tbody>
</table>
</table>
78 changes: 78 additions & 0 deletions badges/tests/badgeslib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,84 @@ public function badges_get_site_primary_backpack_provider() {
];
}

/**
* Test badges_change_sortorder_backpacks().
*
* @dataProvider badges_change_sortorder_backpacks_provider
* @covers ::badges_change_sortorder_backpacks
*
* @param int $backpacktomove Backpack index to move (from 0 to 5).
* @param int $direction Direction to move the backpack.
* @param int|null $expectedsortorder Expected sortorder or null if an exception is expected.
*/
public function test_badges_change_sortorder_backpacks(int $backpacktomove, int $direction, ?int $expectedsortorder): void {
global $DB;

$this->resetAfterTest();
$this->setAdminUser();

// Create 5 more backpacks.
for ($i = 0; $i < 5; $i++) {
$data = new \stdClass();
$data->apiversion = OPEN_BADGES_V2P1;
$data->backpackapiurl = "https://myurl$i.cat/ob/v2p1";
$data->backpackweburl = "https://myurl$i.cat";
badges_create_site_backpack($data);
}

// Check there are 6 backpacks (1 pre-existing + 5 news).
$total = $DB->count_records('badge_external_backpack');
$this->assertEquals(6, $total);
$backpacks = array_values(badges_get_site_backpacks());

if (is_null($expectedsortorder)) {
$this->expectException('moodle_exception');
}

// Move the backpack.
badges_change_sortorder_backpacks($backpacks[$backpacktomove]->id, $direction);

if (!is_null($expectedsortorder)) {
$backpack = badges_get_site_backpack($backpacks[$backpacktomove]->id);
$this->assertEquals($expectedsortorder, $backpack->sortorder);
}
}

/**
* Provider for test_badges_change_sortorder_backpacks.
*
* @return array
*/
public function badges_change_sortorder_backpacks_provider(): array {
return [
"Test up" => [
'backpacktomove' => 1,
'direction' => BACKPACK_MOVE_UP,
'expectedsortorder' => 1,
],
"Test down" => [
'backpacktomove' => 1,
'direction' => BACKPACK_MOVE_DOWN,
'expectedsortorder' => 3,
],
"Test up the very first element" => [
'backpacktomove' => 0,
'direction' => BACKPACK_MOVE_UP,
'expectedsortorder' => 1,
],
"Test down the very last element" => [
'backpacktomove' => 5,
'direction' => BACKPACK_MOVE_DOWN,
'expectedsortorder' => 6,
],
"Test with an invalid direction value" => [
'backpacktomove' => 1,
'direction' => 10,
'expectedsortorder' => null,
],
];
}

/**
* Test the Badgr URL generator function
*
Expand Down
21 changes: 18 additions & 3 deletions badges/tests/behat/backpack.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Feature: Backpack badges

Background:
Given the following "badge external backpacks" exist:
| backpackapiurl | backpackweburl | apiversion |
| https://dc.imsglobal.org/obchost/ims/ob/v2p1 | https://dc.imsglobal.org | 2.1 |
| https://test.com/ | https://test.com/ | 2 |
| backpackapiurl | backpackweburl | apiversion | sortorder |
| https://dc.imsglobal.org/obchost/ims/ob/v2p1 | https://dc.imsglobal.org | 2.1 | 2 |
| https://test.com/ | https://test.com/ | 2 | 3 |
And the following "users" exist:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@example.com |
Expand Down Expand Up @@ -146,6 +146,21 @@ Feature: Backpack badges
And I should not see "https://dc.imsglobal.org"
And "Delete" "button" should not exist

@javascript
Scenario: Move up and down site backpack
Given I am on homepage
And I log in as "admin"
And I navigate to "Badges > Manage backpacks" in site administration
And "Move up" "icon" should exist in the "https://dc.imsglobal.org" "table_row"
And "Move down" "icon" should exist in the "https://dc.imsglobal.org" "table_row"
When I click on "Move up" "link" in the "https://dc.imsglobal.org" "table_row"
Then "Move up" "icon" should not exist in the "https://dc.imsglobal.org" "table_row"
And "Move down" "icon" should exist in the "https://dc.imsglobal.org" "table_row"
And I click on "Move down" "link" in the "https://dc.imsglobal.org" "table_row"
And I click on "Move down" "link" in the "https://dc.imsglobal.org" "table_row"
And "Move up" "icon" should exist in the "https://dc.imsglobal.org" "table_row"
And "Move down" "icon" should not exist in the "https://dc.imsglobal.org" "table_row"

@javascript
Scenario: Add a new site backpack with authentication details checkbox
Given I am on homepage
Expand Down
37 changes: 37 additions & 0 deletions lib/badgeslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@
define('OPEN_BADGES_V2_TYPE_ENDORSEMENT', 'Endorsement');
define('OPEN_BADGES_V2_TYPE_AUTHOR', 'Author');

define('BACKPACK_MOVE_UP', -1);
define('BACKPACK_MOVE_DOWN', 1);

// Global badge class has been moved to the component namespace.
class_alias('\core_badges\badge', 'badge');

Expand Down Expand Up @@ -996,6 +999,40 @@ function badges_get_site_backpacks() {
return $all;
}

/**
* Moves the backpack in the list one position up or down.
*
* @param int $backpackid The backpack identifier to be moved.
* @param int $direction The direction (BACKPACK_MOVE_UP/BACKPACK_MOVE_DOWN) where to move the backpack.
*
* @throws \moodle_exception if attempting to use invalid direction value.
*/
function badges_change_sortorder_backpacks(int $backpackid, int $direction): void {
global $DB;

if ($direction != BACKPACK_MOVE_UP && $direction != BACKPACK_MOVE_DOWN) {
throw new \coding_exception(
'Must use a valid backpack API move direction constant (BACKPACK_MOVE_UP or BACKPACK_MOVE_DOWN)');
}

$backpacks = badges_get_site_backpacks();
$backpacktoupdate = $backpacks[$backpackid];

$currentsortorder = $backpacktoupdate->sortorder;
$targetsortorder = $currentsortorder + $direction;
if ($targetsortorder > 0 && $targetsortorder <= count($backpacks) ) {
foreach ($backpacks as $backpack) {
if ($backpack->sortorder == $targetsortorder) {
$backpack->sortorder = $backpack->sortorder - $direction;
$DB->update_record('badge_external_backpack', $backpack);
break;
}
}
$backpacktoupdate->sortorder = $targetsortorder;
$DB->update_record('badge_external_backpack', $backpacktoupdate);
}
}

/**
* List the supported badges api versions.
*
Expand Down

0 comments on commit 066e998

Please sign in to comment.