How to append a parameter to Moodle 4's course-index activity links ?

How to append a parameter to Moodle 4's course-index activity links ?

by Przemek Kaszubski -
Number of replies: 6
Particularly helpful Moodlers இன் படம் Testers இன் படம்

Dear Moodle Coders புன்முறுவல்

Further to this frustrating discovery of mine, I'd like to be able to modify links to activities in the Moodle 4's course-index (just in the non-edit mode if possible). Namely, I'd like to append the "&forceview=1" parameter to these links (possibly only to File resource links) to make sure that the resource description page always loads after clicking.

I've looked once at some parts of the code, but  failed to find a simple way to achieve what I want.

Any advice..?

Thanks,

Przemek

Average of ratings: -
In reply to Przemek Kaszubski

Re: How to append a parameter to Moodle 4's course-index activity links ?

by Ferran Recio Calderó -
Core developers இன் படம் Moodle HQ இன் படம் Peer reviewers இன் படம் Plugin developers இன் படம் Testers இன் படம்
Hi Przemek,

Any format plugin can override the full course index content by overriding the renderer course_index_drawer method. However, there's another option if you don't want to implement your own drawer.

The data to render the course index is generated using the state data (a data object stored in the front end that contains the full course structure). If a format plugin wants to alter that data it can be done by providing an alternative class to the core_courseformat\output\local\state\cm one. If you want to do that your format plugin must have a course/format/PLUGINAME/classes/output/courseformat/state/cm.php.

You can find more documentation about overriding format output classes here: https://moodledev.io/docs/apis/plugintypes/format#override-output-classes

If none of those options works for you, you can always create an improvement issue in the tracker and make a proposal.

I hope this could help,

Ferran.
In reply to Ferran Recio Calderó

Re: How to append a parameter to Moodle 4's course-index activity links ?

by Przemek Kaszubski -
Particularly helpful Moodlers இன் படம் Testers இன் படம்
Hi Ferran,
Thanks a lot ! I will into these options - to the best of my ability புன்முறுவல் .
 I think a tracker issue is likely regardless..
Have a grand weekend புன்முறுவல்
Przemek
In reply to Przemek Kaszubski

Re: How to append a parameter to Moodle 4's course-index activity links ?

by Przemek Kaszubski -
Particularly helpful Moodlers இன் படம் Testers இன் படம்
I think I may have been able to achieve what I want (though I might attempt one day to develop a more nuanced adjustment for file activities only).

In the core file:

/course/format/classes/output/local/state/cm.php

I modified line 100 by adding the desired parameter. Below is my edit in bold:

        // Add url if the activity is compatible.
        $url = $cm->url;
        $url = $url;
        if ($url) {
            $data->url = $url->out() . "&forceview=1";
        }

After that change i was able to use the course-index , click on a pdf or PowerPoint File activity node and load the activity's description page at first - as illustrated by the screenshot:



I have not been testing this widely, but these first impressions from the Topics format look hopeful.

HTH. Regards, and many thanks again.
In reply to Przemek Kaszubski

Re: How to append a parameter to Moodle 4's course-index activity links ?

by Przemek Kaszubski -
Particularly helpful Moodlers இன் படம் Testers இன் படம்
PS. One consequence of using "forceview=1" with file resources is that the conditional activity completion criterion "Student must view resource to complete the activity" gets checked the moment the students opens the description page, NOT the actual file.

This behaviour of File resources is not new, but might need changing - at least for non-media cases, and for those media cases where filters responsible for rendering embedded players and images are disabled.

Meanwhile. if anyone decides to rely on the presented hard-coded solution (or on the next & previous navigation links where the "forceview" parameter always appears), it's probably advisable not to use activity completion criteria at all, or else to utilise manual marking of completion for file resources.
In reply to Przemek Kaszubski

Re: How to append a parameter to Moodle 4's course-index activity links ?

by Przemek Kaszubski -
Particularly helpful Moodlers இன் படம் Testers இன் படம்
I'm still on Moodle 4.0.* and PHP 7.3.

The following lines allow me to add the forceview parameter only to the File resource course-index links:

// Add url if the activity is compatible.
$url = $cm->url;
if ($url) {
if (strpos($url, '/resource/view.php') !== false) {
$data->url = $url->out() . "&forceview=1";
}
else {
$data->url = $url->out();
}
}
In reply to Ferran Recio Calderó

Re: How to append a parameter to Moodle 4's course-index activity links ?

by Mark Sharp -
Core developers இன் படம் Particularly helpful Moodlers இன் படம் Plugin developers இன் படம்

Before Moodle 4 it was possible to override course format renderers in a theme - this is useful if you're using someone else's course format but want to further modify that. In the example you give, I've not yet found a way (perhaps I'm putting class files in the wrong place) of overriding classes this way. 

For example. in my theme I want to override:

course/format/FORMATPLUGINNAME/classes/output/courseformat/state/cm.php

in my theme 

/theme/MYTHEME/classes/output/FORMATPLUGINNAME/state/cm.php

Is this possible now? Or can it only be done in a course format plugin?