Skip to content

Commit ceac2f7

Browse files
committed
[docs] Course overview integration
1 parent 3959d47 commit ceac2f7

File tree

1 file changed

+232
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)