Skip to content

Commit fa00d10

Browse files
authored
Merge pull request #1274 from ferranrecio/MDL-83888-main
MDL-83888 new overview redirection and renderer helper
2 parents 8831bde + c6b662f commit fa00d10

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

docs/apis/plugintypes/mod/courseoverview.md

+32
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,38 @@ class overview extends activityoverviewbase {
202202
}
203203
```
204204

205+
### Custom grade overview items
206+
207+
If the activity has a grade item, the course overview will display the grade of the activity to the student. If your plugin has one single grade item, you don't need to do anything.
208+
209+
For activities with more than one grade item (for example, mod_workshop), no grade will be shown by default because the system doesn't know the meaning of each grade item. For those cases, the plugin must implement `get_grade_item_names` to provide the generic name for each grade item. The overview report will show only the grade items with a generic name.
210+
211+
This is an example of a plugin with two grade items:
212+
213+
```php
214+
#[\Override]
215+
protected function get_grade_item_names(array $items): array {
216+
// Add some fallback in case some grade item is missing.
217+
if (count($items) != 2) {
218+
return parent::get_grade_item_names($items);
219+
}
220+
$names = [];
221+
foreach ($items as $item) {
222+
// Use the itemnunmber to know which grade item is.
223+
$stridentifier = ($item->itemnumber == 0) ? 'submission_gradenoun' : 'assessment_gradenoun';
224+
// Names must be indexed by the grade item id.
225+
$names[$item->id] = get_string($stridentifier, 'mod_YOURPLUGIN');
226+
}
227+
return $names;
228+
}
229+
```
230+
231+
:::note
232+
233+
It is not recommended to override the `get_grades_overviews` method. The method is used to provide the grade information to the course overview, but it is not intended to be used to provide extra information. If you don't want to show the grade information in your plugin, you can override the `get_grade_item_names` method and return an empty array.
234+
235+
:::
236+
205237
## Dependency Injection
206238

207239
The `overview` class will be loaded using [dependency injection](../../core/di/index.md). The constructor must accept a `cm_info` object to initialize the parent class, however, the constructor can also accept other dependencies that the plugin needs.

docs/apis/subsystems/output/index.md

+36
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,42 @@ echo $output->footer();
106106

107107
This prints the HTML for the bottom of the page. It is very important because it also prints out things that were added to the `page_requirements_manager` and that need to be printed in the footer; things like JavaScript includes, navigation tree setup, closing open containers tags etc. The reason all JavaScripts are added to the footer of the page is for performance. If you add JavaScript includes to the top of the page, or inline with the content, the browser must stop and execute the JavaScript before it can render the page. See https://developers.google.com/speed/docs/insights/BlockingJS for more information.
108108

109+
### Accessing renderers with dependency injection
110+
111+
<Since version="5.0" issueNumber="MDL-83888" />
112+
113+
In the example above, we used `$PAGE->get_renderer('tool_demo')` to get an instance of the renderer. This is the traditional way to get a renderer. However, if you are in a class designed for dependency injection, the use of global variables is discouraged. You can read more about dependency injection in the [Dependency Injection](../../../concepts/dependency-injection/index.md) guide.
114+
115+
Instead, you can use the `core\output\renderer_helper` class to get any renderer instance without using the global. This is an example of how to use the `renderer_helper` class to get a renderer instance:
116+
117+
```php
118+
class my_di_example {
119+
public function __construct(
120+
/** @var \core\output\renderer_helper $rendererhelper the renderer helper */
121+
protected readonly \core\output\renderer_helper $rendererhelper,
122+
) {
123+
}
124+
125+
public function do_something_with_my_renderer() {
126+
/** @var \tool_demo\output\renderer $renderer */
127+
$renderer = $this->rendererhelper->get_renderer('tool_demo');
128+
// Do something with the renderer.
129+
}
130+
131+
public function do_something_with_core_renderer() {
132+
// For convenience, the renderer helper also provides a method to get the core renderer.
133+
$renderer = $this->rendererhelper->get_core_renderer();
134+
// Do something with the core renderer.
135+
}
136+
}
137+
```
138+
139+
:::note
140+
141+
The `core\output\renderer_helper` class serves as a wrapper around the global `$PAGE` object. This is necessary because the `$PAGE` object can change during script execution, making direct injection impossible. Dependency injection relies on singletons, and the `renderer_helper` class provides a workaround for this limitation.
142+
143+
:::
144+
109145
### Renderable
110146

111147
In the code above, we created a renderable. This is a class that you have to add to your plugin. It holds all the data required to display something on the page. Here is the renderable for this example:

0 commit comments

Comments
 (0)