Courses and course formats

renderer for format_base::course_header function

 
Picture of Cathal O'Riordan
renderer for format_base::course_header function
Core developers

Hi,

Has anyone got some sample code for building a renderer for format_base::course_header? I'm finding the short guide included in the functions comments (course/format/lib.php) a little confusing. Any help would be appreciated.

regards,

Cathal.

 
Average of ratings: -
It's only an avatar...
Re: renderer for format_base::course_header function
Core developersDocumentation writersParticularly helpful MoodlersPlugin developersTesters

Yes...and no...

The instructions for this relate to Course Formats, so if you don't know how to make these then it is highly  unlikely you would be able to add a renderer.

That said I will move this to Course Formats Forum and hopefully Gareth can help you.

Cheers

Mary

 
Average of ratings: -
Gareth J Barnard
Re: renderer for format_base::course_header function
Core developersParticularly helpful MoodlersPlugin developers

Dear Cathal,

What are you trying to achieve and where?  This is because I have only ever known course formats to extend format_base and not themes (which I think you are trying to do given where the post was originally posted).  I know themes can override the renderers but I'm not sure about the format_base class hierarchy.

Cheers,

Gareth

 
Average of ratings: -
Picture of Cathal O'Riordan
Re: renderer for format_base::course_header function
Core developers

Hi Gareth,

Thanks for replying. I'm trying to use course_header() to output some course related content; a title image for each course added through the course summary files setting. I've created my own course format but I'm unsure how to override (if that's the correct term) course_header() for my purpose.

Cathal.

 
Average of ratings: -
Gareth J Barnard
Re: renderer for format_base::course_header function
Core developersParticularly helpful MoodlersPlugin developers

Hi Cathal,

I see.  At first glance this should be easy by returning some HTML that would be used when the method is called, but then looking at it seems to need a 'renderable'.  I have never done that before so needs investigating.

Gareth

 
Average of ratings: -
Picture of Cathal O'Riordan
Re: renderer for format_base::course_header function
Core developers
Here is the default function definition in class format_base. However, even with the instructions on its use included within the comments, I still can't figure out how to use it.

/**
     * Course-specific information to be output on any course page (usually above navigation bar)
     *
     * Example of usage:
     * define
     * class format_FORMATNAME_XXX implements renderable {}
     *
     * create format renderer in course/format/FORMATNAME/renderer.php, define rendering function:
     * class format_FORMATNAME_renderer extends plugin_renderer_base {
     *     protected function render_format_FORMATNAME_XXX(format_FORMATNAME_XXX $xxx) {
     *         return html_writer::tag('div', 'This is my header/footer');
     *     }
     * }
     *
     * Return instance of format_FORMATNAME_XXX in this function, the appropriate method from
     * plugin renderer will be called
     *
     * @return null|renderable null for no output or object with data for plugin renderer
     */
    public function course_header() {
        return null;
    }
 
Average of ratings: -
Gareth J Barnard
Re: renderer for format_base::course_header function
Core developersParticularly helpful MoodlersPlugin developers

Hi Cathal,

I've been looking at that too and think I've figured it out, need to test though, cannot quite put into words yet, but look at the methods 'course_header' and 'render' in 'lib/outputrenderers.php' and the interface 'renderable' in 'lib/outputcomponents.php'.  They helped me to understand what is going on.

Seems an odd way of doing it though as I would have thought that the interface 'renderable' would have a method that the empty class in the example implemented then the render for the format would not have to have a method and it gets called with an instance of an empty class that this then gets discarded.  Seems inefficient.

Cheers,

Gareth

 
Average of ratings: Useful (1)
Picture of Cathal O'Riordan
Re: renderer for format_base::course_header function
Core developers

Thanks Gareth. I was totally stumped by it.

If you figure it out, would you mind posting some sample code to illustrate how to use it?

Cathal.



 
Average of ratings: -
Gareth J Barnard
Re: renderer for format_base::course_header function
 
Average of ratings: Useful (1)
Picture of Cathal O'Riordan
Re: renderer for format_base::course_header function
Core developers

Hi Gareth, 

Thanks for that. I really appreciate it.

Cathal.

 
Average of ratings: -