Skip to content

Commit 0dddae0

Browse files
committed
[docs] Course overview integration
1 parent 90cd6f5 commit 0dddae0

File tree

2 files changed

+242
-0
lines changed

2 files changed

+242
-0
lines changed
+234
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
---
2+
title: Course overview integration
3+
tags:
4+
- MUA Project
5+
- API
6+
- Module
7+
- Activity
8+
- mod
9+
- Course
10+
---
11+
12+
## Summary
13+
14+
<Since version="5.0" issueNumber="MDL-83872" />
15+
16+
The course overview table displays a summary of all activities in a course, along with relevant information such as due dates, submission state, completion status, etc. Both students and teachers can access the course overview by clicking the "Activities" tab in the course navigation. The new page replaces the old `mod/PLUGINNAME/index.php` page provided by the activity plugins.
17+
18+
To customize the information displayed in the course overview table, activity plugins must implement their own integration class.
19+
20+
## Overview Class Structure
21+
22+
The integration class must be located in `mod/PLUGINNAME/classes/course/overview.php` using the namespace `mod_PLUGINNAME\course\overview`. The `overview` class should extend the `core_course\activityoverviewbase` class and implement the necessary methods to provide the required overview data.
23+
24+
### Class Definition
25+
26+
This is an example of the class definition that do not add any extra overview items. Simply by extending the `activityoverviewbase` class, the course overview will display the activity name, the section and, if the user has completion, the completion status of the activity.
27+
28+
```php
29+
namespace mod_PLUGINNAME\course;
30+
31+
use core_course\activityoverviewbase;
32+
33+
class overview extends activityoverviewbase {
34+
// Implement methods here.
35+
}
36+
```
37+
38+
### Extra Overview Items
39+
40+
To provide extra overview items, the plugin can override the `get_extra_overview_items` method. This method should return an array of `\core_course\local\overview\overviewitem` objects indexed by item shortname.
41+
42+
This is an example of a plugin adding an extra overview item to show if the activity has been submitted:
43+
44+
```php
45+
namespace mod_PLUGINNAME\course;
46+
47+
use core_course\activityoverviewbase;
48+
use core_course\local\overview\overviewitem;
49+
use core\output\pix_icon;
50+
51+
class overview extends activityoverviewbase {
52+
#[\Override]
53+
public function get_extra_overview_items(): array {
54+
return [
55+
'submitted' => $this->get_extra_submitted_overview(),
56+
];
57+
}
58+
59+
/**
60+
* Get the extra overview item to show if the activity has been submitted.
61+
*
62+
* @return overviewitem|null
63+
*/
64+
private function get_extra_submitted_overview(): ?overviewitem {
65+
// Validate if the user needs this overview information. Return null otherwise.
66+
if (!has_capability('mod/PLUGINNAME:complete', $this->context)) {
67+
return null;
68+
}
69+
70+
// All items should have a value (for filtering purposes)-
71+
$value = false;
72+
73+
// The overview content can be a string.
74+
$content = '-';
75+
76+
// Logic to determine if the activity is already submitted
77+
if (PLUGIN_LOGIC_TO_DETECT_IF_THE_USER_HAS_SUBMITTED_ANYTHING($this->cm)) {
78+
$value = true;
79+
// The overview content can also be a renderable object.
80+
$content = new \pix_icon(
81+
'i/checkedcircle',
82+
alt: get_string('already_submitted', 'mod_PLUGINNAME'),
83+
attributes: ['class' => 'text-success'],
84+
);
85+
}
86+
87+
// Return the overview item. It is mandatory to provide also a name to display.
88+
// The name will be used as the table header in the course overview, but it could
89+
// also be used in other placements in the future.
90+
return new overviewitem(
91+
name: get_string('responded', 'mod_PLUGINNAME'),
92+
value: $value,
93+
content: $content,
94+
);
95+
}
96+
}
97+
```
98+
99+
## Fixed Overview Items
100+
101+
Some activity information is especial and they will be added automatically to the course overview table without declaring them in the `get_extra_overview_items` method.
102+
103+
The following are the fixed overview items:
104+
105+
- **Activity Name**: The name of the activity as a link, the section name and the visibility badge.
106+
- **Completion Status**: The completion status of the activity, if enabled and apply to the current user.
107+
108+
However, there are other fixed overview items that are considered empty unless the plugin overrides the corresponding method:
109+
110+
- **Due Date**: The due date of the activity. Provided by the `get_due_date_overview` method.
111+
- **Main action**: The main action of the activity. Provided by the `get_actions_overview` method.
112+
113+
### Due Date Overview
114+
115+
To provide a due date overview, override the `get_due_date_overview` method. This method must return an `overviewitem` object or null if there is no due date.
116+
117+
```php
118+
namespace mod_PLUGINNAME\course;
119+
120+
use core_course\activityoverviewbase;
121+
use core_course\local\overview\overviewitem;
122+
123+
class overview extends activityoverviewbase {
124+
#[\Override]
125+
public function get_due_date_overview(): ?overviewitem {
126+
// Implement here how to get the due date of the activity.
127+
$duedate = DO_SOMETHING_TO_GET_YOUR_PLUGIN_DUE_DATE();
128+
129+
if (empty($duedate)) {
130+
// If all overview items return null, the due date column will not be displayed.
131+
return new overviewitem(
132+
name: get_string('duedate'),
133+
value: null,
134+
content: '-',
135+
);
136+
}
137+
return new overviewitem(
138+
name: get_string('duedate'),
139+
value: $this->cm->name,
140+
content: userdate($duedate),
141+
);
142+
}
143+
}
144+
```
145+
146+
### Main Action Overview
147+
148+
The main action is always displayed as the last column in the course overview table. However, by default the column is empty and it is not rendered in the table. However, the plugin can define their own action cell to provide useful links to the user. For example, the main action could be the submissions count with a link to the submissions page.
149+
150+
For now, the main action overview is only displayed in the course overview table. However, in the future, it could be used in other places like the course page or the activity page.
151+
152+
To provide a main action overview, override the `get_actions_overview` method.
153+
154+
```php
155+
namespace mod_PLUGINNAME\course;
156+
157+
use core_course\activityoverviewbase;
158+
use core_course\local\overview\overviewitem;
159+
use core\output\action_link;
160+
use core\output\local\properties\button;
161+
use core\output\local\properties\text_align;
162+
163+
class overview extends activityoverviewbase {
164+
#[\Override]
165+
public function get_actions_overview(): ?overviewitem {
166+
// Validate if the user can do the action. Return null otherwise.
167+
if (!has_capability('mod/PLUGINNAME:viewreports', $this->context)) {
168+
return null;
169+
}
170+
171+
$submissions = CALCULATE_YOUR_PLUGIN_SUBMISSIONS_COUNT();
172+
$total = CALCULATE_YOUR_PLUGIN_TOTAL_SUBMISSIONS();
173+
174+
if (!$total) {
175+
return new overviewitem(
176+
name: get_string('responses', 'mod_feedback'),
177+
value: 0,
178+
content: '-',
179+
textalign: text_align::CENTER,
180+
);
181+
}
182+
183+
// For the action content, it is recommended to style it using the
184+
// button enum css helper, it will make the final result consistent
185+
// with the rest of plugins.
186+
$content = new action_link(
187+
url: new url('/mod/feedback/show_entries.php', ['id' => $this->cm->id]),
188+
text: get_string(
189+
'count_of_total',
190+
'core',
191+
['count' => $submissions, 'total' => $total]
192+
),
193+
attributes: ['class' => button::SECONDARY_OUTLINE->classes()],
194+
);
195+
196+
return new overviewitem(
197+
name: get_string('responses', 'mod_feedback'),
198+
value: $submissions,
199+
content: $content,
200+
);
201+
}
202+
}
203+
```
204+
205+
## Dependency Injection
206+
207+
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.
208+
209+
This is an example of a overview integration that needs access to the database and a clock interface for timestamps.
210+
211+
```php
212+
namespace mod_PLUGINNAME\course;
213+
214+
use core_course\activityoverviewbase;
215+
216+
class overview extends activityoverviewbase {
217+
public function __construct(
218+
/** @var cm_info $cm the activity course module. */
219+
cm_info $cm,
220+
/** @var \moodle_database $db the database acces. */
221+
protected readonly \moodle_database $db,
222+
/** @var \core\clock $clock the clock interface */
223+
protected readonly \core\clock $clock
224+
) {
225+
parent::__construct($cm);
226+
}
227+
228+
// The rest of your code goes here. All methods must access the
229+
// $this->db property to interact with the database instead of
230+
// using the global $DB.
231+
232+
// Also, to handle timestamps, use $this->clock->time() instead.
233+
}
234+
```

docs/devupdate.md

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ tags:
99

1010
This page highlights the important changes that are coming in Moodle 5.0 for developers.
1111

12+
## Activity overview page integration
13+
14+
<Since version="5.0" issueNumber="MDL-83872" />
15+
16+
The course navigation has been updated to include a new link to the activity overview page. This page will replace the current `index.php` page in the activity modules. By default, the overview page displays a table that includes the name of each activity, its completion status, and the grade (if applicable). Additionally, plugins can enhance this information by implementing a `mod_PLUGINNAME\course\overview` class.
17+
18+
See the [Activity overview page integration](./apis/plugintypes/mod/courseoverview) documentation for more information.
19+
1220
## Course formats
1321

1422
<Since version="5.0" issueNumber="MDL-83527" />

0 commit comments

Comments
 (0)