Skip to content

Commit

Permalink
refactor index page to a "render class"
Browse files Browse the repository at this point in the history
  • Loading branch information
Glutamat42 committed Apr 29, 2024
1 parent 14c21d1 commit 739c1e9
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 67 deletions.
164 changes: 164 additions & 0 deletions classes/local/output/pages/index_page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php

namespace mod_adleradaptivity\local\output\pages;

global $CFG;

use bootstrap_renderer;
use coding_exception;
use context_course;
use context_module;
use core\context;
use core\context\module;
use dml_exception;
use html_table;
use html_table_row;
use html_writer;
use invalid_parameter_exception;
use local_logging\logger;
use mod_adleradaptivity\event\course_module_instance_list_viewed;
use mod_adleradaptivity\local\db\adleradaptivity_repository;
use mod_adleradaptivity\local\db\moodle_core_repository;
use moodle_exception;
use moodle_page;
use moodle_url;
use question_engine;
use question_usage_by_activity;
use require_login_exception;
use required_capability_exception;
use stdClass;

use mod_adleradaptivity\local\db\adleradaptivity_attempt_repository;
use mod_adleradaptivity\local\db\adleradaptivity_question_repository;
use mod_adleradaptivity\local\db\adleradaptivity_task_repository;
use mod_adleradaptivity\local\helpers;

/**
* Handles the rendering of the adleradaptivity module's index page.
* It's responsibilities are the same as index.php in other plugins.
*/
class index_page {
private moodle_page $page;
private bootstrap_renderer $output;
private logger $logger;

/**
* Constructs and completely renders the page
*
* @throws coding_exception
* @throws dml_exception
* @throws require_login_exception
* @throws moodle_exception
*/
public function __construct() {
// setup variables
$this->setup_instance_variables();
list($course) = $this->process_request_parameters();
$course_context = context_course::instance($course->id);

require_login($course);

// Trigger course_module_instance_list_viewed event
$params = [
'context' => $course_context
];
$event = course_module_instance_list_viewed::create($params);
$event->trigger();

// generating the output
$this->define_page_meta_information($course);
index_page::render_page($course);
}

private function setup_instance_variables(): void {
global $PAGE, $OUTPUT;
$this->page = $PAGE;
$this->output = $OUTPUT;

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

/**
* @param stdClass $course The course (DB) object
* @throws coding_exception
* @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);

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();
}

/**
* @return array An array containing the processed attempt ID, course module, course, and module instance objects.
* @throws coding_exception
* @throws dml_exception
*/
private function process_request_parameters(): array {
$course_id = required_param('id', PARAM_INT);

$course = get_course($course_id);
return array($course);
}

/**
* @param stdClass $course
* @return void
* @throws coding_exception
*/
private function define_page_meta_information(stdClass $course): void {
$this->page->set_url('/mod/adleradaptivity/index.php', ['id' => $course->id]);
$this->page->set_pagelayout('incourse');
$this->page->navbar->add(get_string('modulenameplural', 'adleradaptivity'));
$this->page->set_title(get_string('modulenameplural', 'adleradaptivity'));
$this->page->set_heading($course->fullname);
}
}
69 changes: 2 additions & 67 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,8 @@
// One way to reach this view: add sidebar block "Activities" then click on the adler activities link.
// <domain>/mod/adleradaptivity/index.php?id=<course_id>

use mod_adleradaptivity\event\course_module_instance_list_viewed;
use mod_adleradaptivity\local\output\pages\index_page;

require_once('../../config.php');

// The `id` parameter is the course id.
$id = required_param('id', PARAM_INT);

$PAGE->set_url('/mod/adleradaptivity/index.php', ['id' => $id]);
$course = get_course($id);
//$course = $DB->get_record('course', ['id'=> $id], '*', MUST_EXIST);
$coursecontext = context_course::instance($id);
require_login($course);
$PAGE->set_pagelayout('incourse');

$params = [
'context' => $coursecontext
];
$event = course_module_instance_list_viewed::create($params);
$event->trigger();

// Print the header.
$PAGE->navbar->add(get_string('modulenameplural', 'adleradaptivity'));
$PAGE->set_title(get_string('modulenameplural', 'adleradaptivity'));
$PAGE->set_heading($course->fullname);
echo $OUTPUT->header();
echo $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);

// Print the footer.
echo $OUTPUT->footer();
new index_page();

0 comments on commit 739c1e9

Please sign in to comment.