How to overrider core_course_renderer in course format?

How to overrider core_course_renderer in course format?

by cw k -
Number of replies: 3

Hello,

I'm creating a new course format, and customize the activityinstance, 

I override core_course_renderer like the way this page shows  : http://tech.solin.eu/doku.php?id=moodle:course_construction.

The activityinstance show what I written when the course page loaded, but if I click the actions like "editing_moveleft", "editing_right", "editing_show" or "editing_hide", then the layout of the activityinstance will be the original version of moodle.

I traced the code, the editing actions called function "editModule" in course/amd/src/actions.js and the function will call "edit_module" in course/externallib.php,  this function get course renderer by $courserenderer = $PAGE->get_renderer('core', 'course');

I created a file called course_renderer.php with codes like below:
class format_FORMATNAME_course_renderer extends core_course_renderer {
//override functions

}

I had tried put my code in classes/course_renderer.php and classes/output/core/course_renderer.php, but does not work.

Can I use my own core_course_renderer without change any code in any codes not in this course format?

Thanks for your help.


My moodle version : 3.8.3+

Average of ratings: -
In reply to cw k

Re: How to overrider core_course_renderer in course format?

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

Tell the format in its own renderer constructor -> https://github.com/gjb2048/moodle-format_topcoll/blob/master/renderer.php#L65.

In reply to Gareth J Barnard

回應: Re: How to overrider core_course_renderer in course format?

by cw k -
Thank you, but I have done this in my course format, but it does not work. Besides, I have tried to override course_section_cm_list_item(The function will be called after click editing_right) in topcoll/classes/course_renderer.php, it does not work, too.

All the codes works perfect when refresh the page, but does not work when I click editing_right/editing_left, etc..

Here my codes:

MYFORMAT/classes/course_renderer.php

defined('MOODLE_INTERNAL') || die();

class format_MYFORMAT_course_renderer extends \core_course_renderer {
public function course_section_cm_list_item($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = array()) {
$output = '';
if ($modulehtml = $this->course_section_cm($course, $completioninfo, $mod, $sectionreturn, $displayoptions)) {
$modclasses = 'activity ' . $mod->modname . ' modtype_' . $mod->modname . ' ' . $mod->extraclasses;
$output .= html_writer::tag('li', $modulehtml, array('class' => $modclasses, 'id' => 'module-' . $mod->id));
}
return $output;
}
public function course_section_cm($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = array()) {
$renderer = $this->page->get_renderer('format_MYFORMAT');

//custom codes for showing activities
}

//other override fucntions

}

MYFORMAT/renderer.php

class format_MYFORMAT_renderer extends format_section_renderer_base {
public function __construct(moodle_page $page, $target) {
parent::__construct($page, $target);
$this->courserenderer = $this->page->get_renderer('format_MYFORMAT', 'course');
$this->courseformat = course_get_format($page->course);
$this->tcsettings = $this->courseformat->get_format_options();

$this->userisediting = $page->user_is_editing();
$this->userallowediting = $page->user_allowed_editing();
}
//other functions
}
In reply to cw k

回應: How to overrider core_course_renderer in course format?

by cw k -
Update :
I modified core code "course/renderer.php" for done this.

 
public function course_section_cm_list_item($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = array()) {
+    global $PAGE;
+    try{
+    		$courserenderer = $PAGE->get_renderer('format_'.$course->format, 'course');
+    }catch (Exception $e){
+    		$courserenderer = $this;
+    }
    
    $output = '';
-    if ($modulehtml = $this->course_section_cm($course, $completioninfo, $mod, $sectionreturn, $displayoptions)) {
+    if ($modulehtml = $courserenderer->course_section_cm($course, $completioninfo, $mod, $sectionreturn, $displayoptions)) {
	$modclasses = 'activity ' . $mod->modname . ' modtype_' . $mod->modname . ' ' . $mod->extraclasses;
	$output .= html_writer::tag('li', $modulehtml, array('class' => $modclasses, 'id' => 'module-' . $mod->id));
     }    
    //rest codes...    
}