Commit 066e9984 authored by Sara Arjona's avatar Sara Arjona
Browse files

MDL-70911 core_badges: Add support to backpacks sortorder

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.
parent a306fd7a
......@@ -56,6 +56,17 @@ if ($action == 'delete' && $confirm && confirm_sesskey()) {
} 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') {
......
......@@ -60,6 +60,9 @@ class external_backpacks_page implements \renderable {
$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();
......@@ -69,8 +72,11 @@ class external_backpacks_page implements \renderable {
$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;
......
......@@ -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
}
]
}
}}
......@@ -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>
......@@ -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}}
......@@ -63,4 +84,4 @@
</tr>
{{/backpacks}}
</tbody>
</table>
\ No newline at end of file
</table>
......@@ -1344,6 +1344,84 @@ class core_badges_badgeslib_testcase extends advanced_testcase {
];
}
/**
* 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
*
......
......@@ -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 |
......@@ -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
......
......@@ -127,6 +127,9 @@ define('OPEN_BADGES_V2_TYPE_ISSUER', 'Issuer');
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');
......@@ -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.
*
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment