Skip to content

Commit

Permalink
refactor to use templating
Browse files Browse the repository at this point in the history
  • Loading branch information
Glutamat42 committed Apr 29, 2024
1 parent 739c1e9 commit 35f7284
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 53 deletions.
57 changes: 4 additions & 53 deletions classes/local/output/pages/index_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace mod_adleradaptivity\local\output\pages;

global $CFG;
global $CFG; // TODO remove and maybe require_once('../../config.php'); also

use bootstrap_renderer;
use coding_exception;
Expand Down Expand Up @@ -40,7 +40,6 @@
class index_page {
private moodle_page $page;
private bootstrap_renderer $output;
private logger $logger;

/**
* Constructs and completely renders the page
Expand Down Expand Up @@ -74,8 +73,6 @@ private function setup_instance_variables(): void {
global $PAGE, $OUTPUT;
$this->page = $PAGE;
$this->output = $OUTPUT;

$this->logger = new logger('mod_adleradaptivity', 'index_page.php');
}

/**
Expand All @@ -84,57 +81,11 @@ private function setup_instance_variables(): void {
* @throws moodle_exception
*/
private function render_page(stdClass $course): void {
echo $this->output->header();
echo $this->output->heading(get_string('modulenameplural', 'adleradaptivity'), 2);

// Get all the appropriate data.
if (!$adleradaptivitys = get_all_instances_in_course('adleradaptivity', $course)) {
notice(get_string('thereareno', 'moodle', get_string('modulenameplural', 'adleradaptivity')), "../../course/view.php?id=$course->id");
die;
}

// Configure table for displaying the list of instances.
$headings = [];
$align = [];

if (course_format_uses_sections($course->format)) {
$headings[] = get_string('sectionname', 'format_' . $course->format);
$align[] = 'left';
}

$headings[] = get_string('name');
$align[] = 'left';

$table = new html_table();
$table->head = $headings;
$table->align = $align;

// Populate the table with the list of instances.
foreach ($adleradaptivitys as $adleradaptivity) {
$row = new html_table_row();

if (course_format_uses_sections($course->format)) {
$row->cells[] = get_section_name($course, $adleradaptivity->section);
}

$row->cells[] = html_writer::link(
new moodle_url('/mod/adleradaptivity/view.php', ['id' => $adleradaptivity->coursemodule]),
format_string($adleradaptivity->name)
);

$table->data[] = $row;
}

// Display the table.
echo html_writer::table($table);
$renderer = $this->page->get_renderer('mod_adleradaptivity', 'index');

echo $this->output->header();
echo $renderer->render_page($course);
echo $this->output->footer();

// $renderer = $this->page->get_renderer('mod_adleradaptivity', 'view');
//
// echo $this->output->header();
// echo $renderer->render_module_view_page($tasks, $quba, $cm, $course);
// echo $this->output->footer();
}

/**
Expand Down
36 changes: 36 additions & 0 deletions classes/output/index_renderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace mod_adleradaptivity\output;

use moodle_url;
use plugin_renderer_base;
use stdClass;

class index_renderer extends plugin_renderer_base {
public function render_page(stdClass $course): string {
// Get all the appropriate data.
if (!$adleradaptivities = get_all_instances_in_course('adleradaptivity', $course)) {
notice(get_string('thereareno', 'moodle', get_string('modulenameplural', 'adleradaptivity')), "../../course/view.php?id=$course->id");
die;
}

// Prepare the data for the Mustache template.
$data = [
'module_name_plural' => get_string('modulenameplural', 'adleradaptivity'),
'course_format_uses_sections' => course_format_uses_sections($course->format),
'column_title_section' => course_format_uses_sections($course->format) ? get_string('sectionname', 'format_' . $course->format) : '',
'column_title_activity_name' => get_string('name'),

'adleradaptivities' => array_map(function($adleradaptivity) use ($course) {
return [
'section' => course_format_uses_sections($course->format) ? format_string(get_section_name($course, $adleradaptivity->section)) : '',
'coursemodule_name' => format_string($adleradaptivity->name),
'coursemodule_url' => new moodle_url('/mod/adleradaptivity/view.php', ['id' => $adleradaptivity->coursemodule]),
];
}, $adleradaptivities)
];

// Render the Mustache template with the data.
return $this->render_from_template('mod_adleradaptivity/index_page', $data);
}
}
26 changes: 26 additions & 0 deletions templates/index_page.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{{! This is the Mustache template for the index page }}

<h2>{{ module_name_plural }}</h2>


<table class="generaltable">
<thead>
<tr>
{{#course_format_uses_sections}}
<th>{{ column_title_section }}</th>
{{/course_format_uses_sections}}
<th>{{ column_title_activity_name }}</th>
</tr>
</thead>
<tbody>
{{#adleradaptivities}}
<tr>
{{#course_format_uses_sections}}
<td>{{ section }}</td>
{{/course_format_uses_sections}}
<td><a href="{{ coursemodule_url }}">{{ coursemodule_name }}</a></td>
</tr>

{{/adleradaptivities}}
</tbody>
</table>

0 comments on commit 35f7284

Please sign in to comment.