Skip to content

Commit 5721f3f

Browse files
committed
[docs] overview improvements
1 parent a1ac120 commit 5721f3f

File tree

2 files changed

+205
-4
lines changed

2 files changed

+205
-4
lines changed

docs/apis/plugintypes/mod/courseoverview.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class overview extends activityoverviewbase {
3737

3838
### The `overviewitem` class
3939

40-
The `core_courseformat\local\overview\overviewitem` class represents a specific activity overview item. Currently, items are used only as cells in the course overview table, but they may be used in other places like the course page or the activity page in the future.
40+
The `core_courseformat\local\overview\overviewitem` class represents a specific activity overview item. Currently, items are used only as cells in the course overview table, but they may be used in other places like the course page or the activity page in the future. Also, they could be exported to the mobile APP or other external systems.
4141

4242
The class has the following mandatory properties:
4343

@@ -49,6 +49,8 @@ Also, the class has the following optional properties:
4949

5050
- **`textalign`** (`core\output\local\properties\text_align`): Indicates the preferred text alignment for the parent container. Notice the type hint is not string but `text_align` enum, this limit only to valid text alignment values.
5151
- **`alertcount`** (`integer`) and **`alertlabel`** (`string`): Some items may have an alert count to inform the user of pending actions. This information is not displayed in the table (must be included also in the content) but is added as a data attribute and may be used in other places in the future, such as filtering or mobile app notifications.
52+
- **`extradata`** (`stdClass`): Use this property to include any additional data your plugin needs to provide. While it is not used in the overview page, it will be included in the web service data exported for the mobile app, or for future uses.
53+
- **`key`** (`string`): A unique identifier for the overview item within the displayed context. Typically, the key is assigned automatically based on the context, so you do not need to specify it when instantiating an overview item. For example, the overview table will automatically set the key when retrieving activity overview items.
5254

5355
### Extra Overview Items
5456

@@ -295,3 +297,15 @@ $courseid = required_param('id', PARAM_INT);
295297
```
296298

297299
Once done, the plugin can deprecate any method, output or renderer related to the previous index page.
300+
301+
## Webservice and Moodle APP support
302+
303+
<Since version="5.1" issueNumber="MDL-85509" />
304+
305+
The Moodle Mobile app does not currently support the course overview table, but this feature is planned for future releases. All data from the overview table is already available via the `core_courseformat_get_overview_information` web service.
306+
307+
This web service returns overview items for all activities in a course, including any extra overview items provided by plugins. To ensure your plugin is compatible with the Moodle Mobile app, follow these guidelines:
308+
309+
- If your overview item content is a plain string, use only basic HTML tags such as `<a>`, `<span>`, `<strong>`, and `<em>`. Avoid using special CSS classes, including Bootstrap or other frameworks, as these are not supported by the app.
310+
- If your overview item content is a renderable object, ensure that it also implements the `exportable` interface. This interface provides a stable data structure for use in web services and is required for mobile compatibility.
311+
- If your plugin requires custom visual elements that need more information than the standard output class provides, use the `extradata` property to supply any additional data for the APP. This property is included in the web service response and will be accessible by your custom templates in the Moodle app when the course overview table is implemented.

docs/apis/subsystems/output/index.md

Lines changed: 190 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,10 @@ Some interesting parameters for this function are:
387387
- `classes`: The classes of the paragraph. Note that this parameter is a comma-separated list of classes, not an array.
388388
- `id`: An optional id of the paragraph.
389389

390-
#### sr_text()
390+
#### visually_hidden_text()
391391

392392
```php
393-
function sr_text(string $contents): string
393+
function visually_hidden_text(string $contents): string
394394
```
395395

396396
This function should be used to:
@@ -404,7 +404,7 @@ Some interesting parameters for this function are:
404404
In the standard Boost theme this method will output a span using the [Bootstrap screen reader class](https://getbootstrap.com/docs/4.0/getting-started/accessibility/#visually-hidden-content):
405405

406406
```html
407-
<span class="sr-only">Contents</span>
407+
<span class="visually-hidden">Contents</span>
408408
```
409409

410410
### Other
@@ -505,6 +505,193 @@ $task->initialise_stored_progress(); // Creates a stored progress record, so the
505505

506506
With the stored progress bars, you can update the progress either via iterations, by passing in the total amount expected and then the current iteration, using `->update()`(see: previous example), this will calculate the percentage complete for you. Or you can use `->update_full()` to manually set the percentage complete.
507507

508+
## Reusing Output classes in webservices
509+
510+
<Since version="5.1" issueNumber="MDL-85509" />
511+
512+
The `renderable` interface with its `export_for_template` method is designed to provide data specifically for template rendering. The structure and content of the exported data are tightly coupled to the template's requirements, which may change over time. This makes the data unstable and unsuitable for webservices, where a consistent and predictable data structure is essential for API consumers.
513+
514+
However, many output classes implements internal logics that can be reused in webservices. To achieve this, you can implement the `externable` interface in your output class. This interface is designed to provide a stable data structure for webservices, ensuring that the data remains consistent and reliable across different versions of the API.
515+
516+
When implementing the `externable` interface, you should define an `export_for_external` method that returns a structured object `stdClass` containing the necessary data. This method should not depend on any specific template or rendering logic, but rather focus on providing a stable and predictable data structure that can be consumed by external clients.
517+
518+
<details>
519+
<summary>Here is an example of how to implement the `externable` interface in your output class.</summary>
520+
521+
```php
522+
523+
namespace mod_MYPLUGIN\output\myname;
524+
525+
use cm_info;
526+
use core\output\externable;
527+
use core\output\named_templatable;
528+
use core\output\renderable;
529+
use core\output\renderer_base;
530+
use core_courseformat\base as course_format;
531+
use stdClass;
532+
533+
class myname implements renderable, named_templatable, externable {
534+
public function __construct(
535+
/** @var cm_info The course module. */
536+
protected cm_info $cm,
537+
) {
538+
}
539+
540+
#[\Override]
541+
public function export_for_template(renderer_base $output): stdClass {
542+
// This method is used to prepare data for rendering in a template.
543+
// It is related to the `templatable` interface and could return an object or array.
544+
$cm = $this->cm;
545+
$result = (object) [
546+
'activityname' => \core_external\util::format_string($cm->name, $cm->context, true),
547+
'activityurl' => $cm->url,
548+
'hidden' => empty($cm->visible),
549+
'extraclasses' => 'mystyle fw-bold',
550+
];
551+
if ($cm->is_stealth()) {
552+
$result->extrawarning = get_string('stealth', 'mod_MYPLUGIN');
553+
}
554+
return $result;
555+
}
556+
557+
#[\Override]
558+
public function export_for_external(renderer_base $output): stdClass {
559+
// This method is used to prepare data for webservice clients.
560+
// It is related to the `externable` interface and should return always an stdClass object.
561+
$templatedata = $this->export_for_template($output);
562+
return (object) [
563+
'activityname' => \core_external\util::format_string($cm->name, $cm->context, true),
564+
'activityurl' => ($cm->url) ? $cm->url->out(false) : null,
565+
'hidden' => empty($cm->visible),
566+
'stealth' => $cm->is_stealth(),
567+
];
568+
}
569+
570+
#[\Override]
571+
public function get_template_name(renderer_base $renderer): string {
572+
// This method is used to specify the template name for rendering.
573+
// It is not used for webservice clients, but it is required by the named_templatable interface.
574+
return 'core_courseformat/local/overview/activityname';
575+
}
576+
}
577+
```
578+
579+
</details>
580+
581+
:::important PHPUnitTests are required
582+
583+
To ensure the stability of exported data, any class implementing `externable` must provide a PHPUnit test that verifies both the structure and values of the exported data. It is also recommended that all attributes in the data returned by `export_for_external` have a value, avoiding nulls or missing fields, to guarantee reliability for webservice clients.
584+
585+
:::
586+
587+
### Using `exporters` with `exportable` objects.
588+
589+
When an `externable` class is used in a webservice, it is strongly recommended to also implement an exporter class. The exporters can be used to simplify the webservice code and to ensure that the data returned by the webservice is consistent with the data returned by the `export_for_external` method.
590+
591+
<details>
592+
<summary>Here is an example of how to implement an `exporter` to an `externable` output.</summary>
593+
594+
This is an example of an exporter for the `myname` output class we created above.
595+
596+
```php
597+
namespace mod_MYPLUGIN\external;
598+
599+
use core\external\exporter;
600+
601+
class myname_exporter extends exporter {
602+
#[\Override]
603+
protected static function define_properties(): array {
604+
return [
605+
'activityname' => [
606+
'type' => PARAM_TEXT,
607+
'null' => NULL_NOT_ALLOWED,
608+
'description' => 'The activity name.',
609+
],
610+
'activityurl' => [
611+
'type' => PARAM_URL,
612+
'null' => NULL_ALLOWED,
613+
'description' => 'An optional activity url.',
614+
],
615+
'hidden' => [
616+
'type' => PARAM_BOOL,
617+
'null' => NULL_NOT_ALLOWED,
618+
'description' => 'Wether the activity is hidden or not.',
619+
],
620+
'stealth' => [
621+
'type' => PARAM_BOOL,
622+
'null' => NULL_ALLOWED,
623+
'default' => null,
624+
'description' => 'If the activity is strealth.',
625+
],
626+
];
627+
}
628+
629+
#[\Override]
630+
protected static function define_related() {
631+
// Most exporter need to define the context as related data to parse texts.
632+
return [
633+
'context' => 'context',
634+
];
635+
}
636+
637+
// In case you need to add some extra data to the exported data, you can
638+
// overrride define_other_properties and get_other_values to cook
639+
// the data you need.
640+
}
641+
```
642+
643+
Once the exported is implemented, the webservice can use it to return the data in a consistent.
644+
645+
```php
646+
namespace mod_MYPLUGIN\external;
647+
648+
use core_external\external_api;
649+
use core_external\external_function_parameters;
650+
use core_external\external_single_structure;
651+
use core_external\external_value;
652+
use stdClass;
653+
use core\output\renderer_helper;
654+
655+
class get_my_name extends external_api {
656+
public static function execute_parameters(): external_function_parameters {
657+
return new external_function_parameters([
658+
'cmid' => new external_value(PARAM_INT, 'Course module id', VALUE_REQUIRED),
659+
]);
660+
}
661+
662+
public static function execute(int $cmid): stdClass {
663+
[
664+
'cmid' => $cmid,
665+
] = external_api::validate_parameters(self::execute_parameters(), [
666+
'cmid' => $cmid,
667+
]);
668+
669+
[$course, $cm] = get_course_and_cm_from_cmid($cmid);
670+
671+
$context = \core\context\module::instance($cm->id);
672+
self::validate_context($context);
673+
674+
$page = \core\di::get(renderer_helper::class)->get_core_renderer();
675+
$exportable = new \mod_MYPLUGIN\output\myname($cm);
676+
677+
$exportedcontent = $exportable->export_for_external($renderer);
678+
$exporter = new myname_exporter($exportedcontent, ['context' => $context]);
679+
return $exporter->export($renderer);
680+
}
681+
682+
/**
683+
* Webservice returns.
684+
*
685+
* @return external_single_structure
686+
*/
687+
public static function execute_returns(): external_single_structure {
688+
return myname_exporter::get_read_structure();
689+
}
690+
}
691+
```
692+
693+
</details>
694+
508695
## See also
509696

510697
- [HTML Guidelines](https://docs.moodle.org/dev/HTML_Guidelines)

0 commit comments

Comments
 (0)