Compoot
Compoot

Reputation: 2377

Moodle plugin development - can't display html heading in index.php file

In a moodle installation, I have the following index.php code in mysitename/local/myfolder/index.php

It is code that basically requires a student to login and then displays all the moodle courses they take.

This works fine, but when I try to add simple HTML (like a red heading at the top which says "My dash", it simply does not display. What am I doing wrong?

Code

    <?php

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

require_login();

$PAGE->set_context(context_system::instance());
$PAGE->set_title('Dashboard');
$PAGE->set_heading('Dashboard');
$PAGE->navbar->add('Dashboard');
echo $OUTPUT->header();

global $USER, $DB;

$courses = enrol_get_users_courses($USER->id);

echo '<!DOCTYPE html>';
echo '<html>';
echo '<head>';
echo '<title>My Dashboard</title>';
echo '<style>';
echo 'h1 { color: red; font-weight: bold; }';
echo '</style>';
echo '</head>';
echo '<body>';
echo '<h1>My Dash</h1>';

if ($courses) {
    echo '<h2>Your courses:</h2>';
    echo '<ul>';
    foreach ($courses as $course) {
        echo '<li><a href="' . $CFG->wwwroot . '/course/view.php?id=' . $course->id . '">' . $course->fullname . '</a></li>';

        // Get quizzes for the current course
        $quizzes = $DB->get_records_sql('SELECT q.id, q.name
                                         FROM {quiz} q
                                         INNER JOIN {course_modules} cm ON q.id = cm.instance
                                         INNER JOIN {modules} m ON m.id = cm.module
                                         WHERE cm.course = ? AND m.name = ?', [$course->id, 'quiz']);

        if ($quizzes) {
            echo '<ul>';
            foreach ($quizzes as $quiz) {
                $attempt = $DB->get_record_sql('SELECT a.*
                                                FROM {quiz_attempts} a
                                                WHERE a.quiz = ? AND a.userid = ?', [$quiz->id, $USER->id]);

                if (!$attempt) {
                    echo '<li>Quiz: <a href="' . $CFG->wwwroot . '/mod/quiz/view.php?id=' . $quiz->id . '">' . $quiz->name . '</a> (not taken)</li>';
                }
            }
            echo '</ul>';
        }

        // Get assignments for the current course
        $assignments = $DB->get_records_sql('SELECT a.id, a.name
                                              FROM {assign} a
                                              INNER JOIN {course_modules} cm ON a.id = cm.instance
                                              INNER JOIN {modules} m ON m.id = cm.module
                                              WHERE cm.course = ? AND m.name = ?', [$course->id, 'assign']);

        if ($assignments) {
            echo '<ul>';
            foreach ($assignments as $assignment) {
                $submission = $DB->get_record_sql('SELECT s.*
                                                    FROM {assign_submission} s
                                                    WHERE s.assignment = ? AND s.userid = ?', [$assignment->id, $USER->id]);

                if (!$submission) {
                    echo '<li>Assignment: <a href="' . $CFG->wwwroot . '/mod/assign/view.php?id=' . $assignment->id . '">' . $assignment->name . '</a> (not submitted)</li>';
                }
            }
            echo '</ul>';
        }
    }
    echo '</ul>';
} else {
    echo '<p>You are not enrolled in any courses</p>';
}

echo '</body>';
echo '</html>';

echo $OUTPUT->footer();

Upvotes: 0

Views: 186

Answers (1)

davosmith
davosmith

Reputation: 6317

As a starting point, you need to remove all the doctype, head and body tags you're outputting - all of that is output already by the line:

echo $OUTPUT->header().

If you want custom styles, then create a styles.css file inside your plugin and then purge your site caches - any styling you add to that file will be included in the combined CSS file served for your entire site (as long as you purge caches or increase your plugin's version number whenever you change the styles.css file).

See https://docs.moodle.org/dev/CSS_Coding_Style for more details about using CSS in Moodle.

Upvotes: 0

Related Questions