Custom display of activities and mods on course page

Custom display of activities and mods on course page

by Stephen Hooley -
Number of replies: 5


I'm attempting to alter the way my course format displays certain activities/mods in Moodle 2.7.

Previously in Moodle 2.3 I created a course format that showed certain activity types differently. For example rather than just a link to a SCORM activity the course page appeared with an indicator showing its status. Other bespoke modules showed other information dependent upon the type/state of the activity. I achieved this quite simply by modifying the code in {theme}/format.php and creating my own version of the print_multiple_section_page function. A simple switch statement detected the module type ($mod->modname) and changed the output code for each module as required.

With 2.5+ the method of displaying activities within a course appears to be embedded within several functions in course/format/renderer.php and course/renderer.php and therefore much harder to change. Before I start recreating multiple functions is there a simple way to change how activities appear in the course format or mod code that I've missed? Has anybody else done this?

Thanks in anticipation.

Steve

Average of ratings: Useful (1)
In reply to Stephen Hooley

Re: Custom display of activities and mods on course page

by Stephen Hooley -

Ok.

I've done a little more research and the course format/layout now uses the new Moodle 2 renderers.

I've identified that the function I need to override is course_section_cm() which is defined in the core_course_renderer class.

I've read the documentation on overriding renderer functions described here that talks about overriding for themes but not for individual course formats.

I've tried adding the following to the my bespoke course format renderer.php file:

require_once($CFG->dirroot.'/course/renderer.php');

class format_mycourseformat_course_renderer extends core_course_renderer{

public function course_section_cm([params]){

....custom code....

}

}

Has anyone overridden a renderer from a course format before or any ideas why this isn't working?

In reply to Stephen Hooley

Re: Custom display of activities and mods on course page

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

I've only ever overridden that renderer in a theme and did not think that the course format used it or indeed could.  There is code for themes to have detected overridden renderers but not that I know of for course formats, bar 'format_section_renderer_base' and 'format_base' in lib.php.  Look at: https://docs.moodle.org/dev/Course_Format.

In reply to Stephen Hooley

Re: Custom display of activities and mods on course page

by Stephen Hooley -

Thanks Gareth, with a little more time I've worked out how to do it. I'll try and explain for others who may be interested.

I want to display custom code for some activities on the course page rather than just a link to the activity. This will allow students to see other information about an activity directly on the course page without having to click on it. I have created some custom activities where this is useful, such as one that shows a certificate on the course page when another activity is complete or the course is complete.

Firstly I've made a copy of the Topics course format and renamed all the references to Topic as myformat.

The function that displays a single course activity is 'course_section_cm' a method of the class 'core_course_renderer' which is defined in '/course/renderer.php'. I therefore need to override this function with my own code.

The Topics format already has its own renderer class defined in 'renderer.php'. In my instance this is now called 'format_myformat_renderer' and it extends the 'format_section_renderer_base' class.

A course format may have one custom renderer and it must take be named format_{formatname}_renderer. Because the renderer is already present I can't just create another as I had tried in the post above as Moodle won't recognise it.

I can't just put my override 'course_section_cm' function in this class as it is extending the class 'format_section_renderer_base' instead of 'core_course_renderer' where the original 'course_section_cm' resides. The class cannot extend two different classes so I have to instantiate a new class to extend 'core_course_renderer'. This done by adding a line of code to the constructor class which is already being overridden in the format renderer. I'll call my new class format_myformat_course_renderer although I could call it anything.

public function __construct(moodle_page $page, $target) {

parent::__construct($page, $target);

//this line creates a new class that can extend the 'core_course_renderer'
$this->courserenderer = new format_myformat_course_renderer($page, $target);

// Since format_bish_renderer::section_edit_controls() only displays the 'Set current section' control when editing mode is on
// we need to be sure that the link 'Turn editing mode on' is available for a user who does not have any other managing capability.
$page->set_other_editing_capability('moodle/course:setcurrentsection');
}

Now I have a class that I can use to override the functions in the 'core_course_renderer' and it will load when the course format loads.

Finally in 'renderer.php' I define the new class and the function I want to override. Note. You must also include the file where the renderer is defined.

require_once($CFG->dirroot.'/course/renderer.php');

class format_myformat_course_renderer extends core_course_renderer{

public function course_section_cm($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = array()) {

....my custom code....
}
}

All I need to do now is insert my custom code. I this instance all I'll do is use a switch statement to identify the type of mod using $mod->modname and display content accordingly.

Job done. I hope that makes sense, if you want to know anything else just ask.

Average of ratings: Useful (3)
In reply to Stephen Hooley

Re: Custom display of activities and mods on course page

by Phillip McAbee -

Hi,

I'm trying to do something similar with the Rating Block.

Will this technique work the same for showing  rendering

$block->display_rating($course->id);

inside course listings?

Thanks Phil


In reply to Stephen Hooley

Re: Custom display of activities and mods on course page

by Hector Mongil Montero -

Hello Stephen Hooley

I'm trying to do something like that. I want to show scorm TOC instead of just scorm activity link.

Could you help me?

Thank you.