MDL-78207 resource: Move file type to activity badge

One of the resource settings, the file type, has been moved to the
activity badge near the activity name.
The rest of the information will be displayed using the afterlink
feature.

However, for now, the activity page still displays all the information
together (that's why resource_get_optional_details() supports now one
new parameter, $showtype, to define whether the file type should be
displayed or not).
This commit is contained in:
Sara Arjona 2023-05-25 17:38:52 +02:00
parent 987ceb1b69
commit af164ce9c7
9 changed files with 120 additions and 18 deletions

View File

@ -129,4 +129,4 @@ Feature: Add customised file types
And I expand all fieldsets
And I set the field "Show type" to "1"
And I press "Save and return to course"
Then I should see "Froggy file"
Then I should see "FROG" in the "div.activitybadge" "css_element"

View File

@ -1110,6 +1110,15 @@ class externallib_test extends externallib_advanced_testcase {
$CFG->forum_allowforcedreadtracking = 1;
list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
// Create a resource with all the appearance options enabled. By default it's a text file and will be added to section 1.
$record = (object) [
'course' => $course->id,
'showsize' => 1,
'showtype' => 1,
'showdate' => 1,
];
$resource = self::getDataGenerator()->create_module('resource', $record);
// We first run the test as admin.
$this->setAdminUser();
$sections = core_course_external::get_course_contents($course->id, array());
@ -1149,6 +1158,12 @@ class externallib_test extends externallib_advanced_testcase {
$this->assertFalse($module['noviewlink']);
$this->assertArrayNotHasKey('description', $module);
$testexecuted = $testexecuted + 1;
} else if ($module['instance'] == $resource->id && $module['modname'] == 'resource') {
// Resources have both, afterlink for the size and the update date and activitybadge for the file type.
$this->assertStringContainsString('32 bytes', $module['afterlink']);
$this->assertEquals('TXT', $module['activitybadge']['badgecontent']);
$this->assertEquals('badge-none', $module['activitybadge']['badgestyle']);
$testexecuted = $testexecuted + 1;
}
}
foreach ($sections[2]['modules'] as $module) {
@ -1161,10 +1176,10 @@ class externallib_test extends externallib_advanced_testcase {
$CFG->forum_allowforcedreadtracking = 0; // Recover original value.
forum_tp_count_forum_unread_posts($forumcm, $course, true); // Reset static cache for further tests.
$this->assertEquals(5, $testexecuted);
$this->assertEquals(6, $testexecuted);
$this->assertEquals(0, $sections[0]['section']);
$this->assertCount(6, $sections[0]['modules']);
$this->assertCount(7, $sections[0]['modules']);
$this->assertCount(1, $sections[1]['modules']);
$this->assertCount(1, $sections[2]['modules']);
$this->assertCount(1, $sections[3]['modules']); // One module for the section with availability restrictions.

View File

@ -31,8 +31,10 @@ Feature: Add a new custom file type
When I add a "File" to section "1" and I fill the form with:
| Name | Test file |
| Select files | files/tests/fixtures/custom_filetype.mdlr |
| Show size | 1 |
| Show type | 1 |
| Display resource description | 1 |
And I am on "Course 1" course homepage
Then I should see "Test file"
And I should see "Moodle rules" in the "span.resourcelinkdetails" "css_element"
And I should see "MDLR" in the "div.activitybadge" "css_element"
And I should not see "MDLR" in the "span.resourcelinkdetails" "css_element"

View File

@ -0,0 +1,35 @@
<?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/>.
namespace mod_resource\output\courseformat;
/**
* Activity badge resource class, used for displaying the file type.
*
* @package mod_resource
* @copyright 2023 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class activitybadge extends \core_courseformat\output\activitybadge {
/**
* This method will be called before exporting the template.
*/
protected function update_content(): void {
$options = (object) ['displayoptions' => $this->cminfo->customdata['displayoptions']];
$this->content = resource_get_optional_filetype($options, $this->cminfo);
}
}

View File

@ -102,10 +102,10 @@ $string['printintroexplain'] = 'Display resource description below content? Some
$string['privacy:metadata'] = 'The File resource plugin does not store any personal data.';
$string['resource:addinstance'] = 'Add a new resource';
$string['resourcecontent'] = 'Files and subfolders';
$string['resourcedetails_sizetype'] = '{$a->size} {$a->type}';
$string['resourcedetails_sizedate'] = '{$a->size} {$a->date}';
$string['resourcedetails_typedate'] = '{$a->type} {$a->date}';
$string['resourcedetails_sizetypedate'] = '{$a->size} {$a->type} {$a->date}';
$string['resourcedetails_sizetype'] = '{$a->size} · {$a->type}';
$string['resourcedetails_sizedate'] = '{$a->size} · {$a->date}';
$string['resourcedetails_typedate'] = '{$a->type} · {$a->date}';
$string['resourcedetails_sizetypedate'] = '{$a->size} · {$a->type} · {$a->date}';
$string['resource:exportresource'] = 'Export resource';
$string['resource:view'] = 'View resource';
$string['search:activity'] = 'File';

View File

@ -271,7 +271,7 @@ function resource_cm_info_view(cm_info $cm) {
require_once($CFG->dirroot . '/mod/resource/locallib.php');
$resource = (object) ['displayoptions' => $cm->customdata['displayoptions']];
$details = resource_get_optional_details($resource, $cm);
$details = resource_get_optional_details($resource, $cm, false);
if ($details) {
$cm->set_after_link(' ' . html_writer::tag('span', $details,
array('class' => 'resourcelinkdetails')));

View File

@ -291,6 +291,7 @@ function resource_get_file_details($resource, $cm) {
if ($mainfile) {
$filedetails['type'] = get_mimetype_description($mainfile);
$filedetails['mimetype'] = $mainfile->get_mimetype();
$filedetails['extension'] = strtoupper(resourcelib_get_extension($mainfile->get_filename()));
// Only show type if it is not unknown.
if ($filedetails['type'] === get_mimetype_description('document/unknown')) {
$filedetails['type'] = '';
@ -328,15 +329,16 @@ function resource_get_file_details($resource, $cm) {
*
* @param object $resource Resource table row (only property 'displayoptions' is used here)
* @param object $cm Course-module table row
* @param bool $showtype Whether the file type should be displayed or not (regardless the display option is enabled).
* @return string Size and type or empty string if show options are not enabled
*/
function resource_get_optional_details($resource, $cm) {
function resource_get_optional_details($resource, $cm, bool $showtype = true) {
global $DB;
$details = '';
$options = empty($resource->displayoptions) ? [] : (array) unserialize_array($resource->displayoptions);
if (!empty($options['showsize']) || !empty($options['showtype']) || !empty($options['showdate'])) {
if (!empty($options['showsize']) || ($showtype && !empty($options['showtype'])) || !empty($options['showdate'])) {
if (!array_key_exists('filedetails', $options)) {
$filedetails = resource_get_file_details($resource, $cm);
} else {
@ -354,9 +356,9 @@ function resource_get_optional_details($resource, $cm) {
$infodisplayed += 1;
}
}
if (!empty($options['showtype'])) {
if ($showtype && !empty($options['showtype'])) {
if (!empty($filedetails['type'])) {
$type = $filedetails['type'];
$type = $filedetails['extension'];
$langstring .= 'type';
$infodisplayed += 1;
}
@ -385,6 +387,34 @@ function resource_get_optional_details($resource, $cm) {
return $details;
}
/**
* Gets optional file type extension for a resource, depending on resource settings.
*
* @param object $resource Resource table row (only property 'displayoptions' is used here)
* @param object $cm Course-module table row
* @return string File extension or null if showtype option is not enabled
*/
function resource_get_optional_filetype($resource, $cm): ?string {
$filetype = null;
$options = empty($resource->displayoptions) ? [] : (array) unserialize_array($resource->displayoptions);
if (empty($options['showtype'])) {
// Show type option is disabled; early return null filetype.
return $filetype;
}
if (!array_key_exists('filedetails', $options)) {
$filedetails = resource_get_file_details($resource, $cm);
} else {
$filedetails = $options['filedetails'];
}
if (!empty($filedetails['type'])) {
$filetype = $filedetails['extension'];
}
return $filetype;
}
/**
* Get resource introduction.
*

View File

@ -44,19 +44,34 @@ Feature: Teacher can specify different display options for the resource
| Show upload/modified date | <showdate> |
And I press "Save and display"
Then I <seesize> see "6 bytes" in the ".resourcedetails" "css_element"
And I <seetype> see "Text file" in the ".resourcedetails" "css_element"
And I <seetype> see "TXT" in the ".resourcedetails" "css_element"
And I <seedate> see "Uploaded" in the ".resourcedetails" "css_element"
And I am on "Course 1" course homepage
And I <seesize> see "6 bytes" in the ".activity.resource .resourcelinkdetails" "css_element"
And I <seetype> see "Text file" in the ".activity.resource .resourcelinkdetails" "css_element"
And I <seetype> see "TXT" in the ".activity.resource .activitybadge" "css_element"
And I <seedate> see "Uploaded" in the ".activity.resource .resourcelinkdetails" "css_element"
Examples:
| showsize | showtype | showdate | seesize | seetype | seedate |
| 1 | 0 | 0 | should | should not | should not |
| 0 | 1 | 0 | should not | should | should not |
| 0 | 0 | 1 | should not | should not | should |
| 1 | 1 | 0 | should | should | should not |
| 1 | 0 | 1 | should | should not | should |
| 0 | 1 | 1 | should not | should | should |
| 1 | 1 | 1 | should | should | should |
Scenario: Specifying only show type for a file resource
When I am on the "Myfile" "resource activity editing" page
And I set the following fields to these values:
| display | 5 |
| Show size | 0 |
| Show type | 1 |
| Show upload/modified date | 0 |
And I press "Save and display"
Then I should see "TXT" in the ".resourcedetails" "css_element"
Then I should not see "6 bytes" in the ".resourcedetails" "css_element"
And I should see "TXT" in the ".resourcedetails" "css_element"
And I should not see "Uploaded" in the ".resourcedetails" "css_element"
And I am on "Course 1" course homepage
And I should see "TXT" in the ".activity.resource .activitybadge" "css_element"
And ".activity.resource .resourcelinkdetails" "css_element" should not exist

View File

@ -1,5 +1,10 @@
This files describes API changes in the quiz code.
This file describes API changes in the resource code.
=== 4.3 ===
* Function resource_get_optional_details() has now one new parameter, $showtype, to decide whether the file type should be
displayed or not (regardless the display option is enabled).
=== 4.0 ===
* Functions resource_print_heading and resource_print_intro have been deprecated in favour for the activity header.
* Functions resource_print_heading and resource_print_intro have been deprecated in favour for the activity header.