Calling core_course_renderer::get_category_formatted_description() - Moodle 3.10+

Calling core_course_renderer::get_category_formatted_description() - Moodle 3.10+

by Dave Emsley -
Number of replies: 6

Hi all,

I seem to be having a bit of brain fade trying to use the method core_course_renderer::get_category_formatted_description()

However I get the error - Call to undefined method core_course_renderer::get_category_formatted_description() from the course lib.php

The php code is:   

$output .= core_course_renderer::get_category_formatted_description($cat->id);
The error is:

Error

Exception - Call to undefined method core_course_renderer::get_category_formatted_description()
Debug info:
Error code: generalexceptionmessage
Stack trace:
  • line 142 of /theme/mytheme/lib.php: Error thrown
  • line 49 of /theme/mytheme/layout/frontpage.php: call to theme_horn_getcourses()
  • line 1374 of /lib/outputrenderers.php: call to include()
  • line 1304 of /lib/outputrenderers.php: call to core_renderer->render_page_layout()
  • line 100 of /index.php: call to core_renderer->header()

I'm sure I'm missing something obvious but any help greatfully received.

Thanks

Dave

Average of ratings: -
In reply to Dave Emsley

Re: Calling core_course_renderer::get_category_formatted_description() - Moodle 3.10+

by Dave Emsley -
Addendum - I'm simply trying to display the category information correctly.
In reply to Dave Emsley

Re: Calling core_course_renderer::get_category_formatted_description() - Moodle 3.10+

by lior gil -
Picture of Core developers

You're trying to call for a static function and  the function get_category_formatted_description isn't static.

There's actually a call to the function in the course renderer that you can use as a reference and do something like, it's implemented like this:

$chelper = new coursecat_helper();
$chelper->get_category_formatted_description($cat->id);


Average of ratings: Useful (1)
In reply to lior gil

Re: Calling core_course_renderer::get_category_formatted_description() - Moodle 3.10+

by Dave Emsley -
Thanks for getting back to me. I tried this and, for some reason it doesn't return the description. I guess I'm not calling it correctly?

Cheers

Dave
In reply to Dave Emsley

Re: Calling core_course_renderer::get_category_formatted_description() - Moodle 3.10+

by Paul Holden -
Picture of Core developers Picture of Moodle HQ Picture of Moodle Workplace team Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers
Hi Dave,

As Lior said, you can't call this method statically, it should be called from an instantiated coursecat_helper class
What does your code look like?
Average of ratings: Useful (1)
In reply to Paul Holden

Re: Calling core_course_renderer::get_category_formatted_description() - Moodle 3.10+

by Dave Emsley -
Hi Paul,

Pretty much as per Lior's example at the moment.

mytheme/lib.php contains:
---------------------------------------------------
function theme_horn_getcourses() {
global $DB;
$output = "";
$table = "course_categories";
$params = array('id'=>1);
$sql = "SELECT * FROM {course_categories} WHERE id > :id ORDER BY idnumber ";
$categories = $DB->get_records_sql($sql, $params);
foreach($categories as $cat) {
$output .= '<div class="horn_sep_category"> <div class="horn_category_header"> <h3>'.$cat->name.'</h3><span class="horn_click_to_open"<%gt;i class="horn-toggle-icon fa fa-angle-down fa-3x fa-rotate-180"><i></span> </div>';
$chelper = new \coursecat_helper();
$description = $chelper->get_category_formatted_description($cat->id);
$output .= $description ;
$output .= theme_horn_getcourses_by_category($cat->id);
$output .= ' </div>';
}

return $output;
}
-----------------------------------------------------------------

...which gets the code from the database OK but injects:
<p dir="ltr" style="text-align:left;"> <img src="@  @  PLUGIN FILE @ @/Sealion-Yawns.JPG?time=1634733374402" alt="" class="img-fluid atto_image_button_left" width="476" height="317">sdgf sdsdf uif uishfui haduisfui hsdifu hsdfuihsdfuihsidfuhsduifhsuifhsuifh yu u fu8ygf uyguy guygffrwugrueryyuwegrur yuweruyweruywe.
So I know I need to run it through get_category_formatted_description but can't see how.

Cheers

Dave

In reply to Dave Emsley

Re: Calling core_course_renderer::get_category_formatted_description() - Moodle 3.10+

by Dave Emsley -
I've got it working by looking at the /course/renderer.php file and taking its guts out:

$context = context_coursecat::instance($cat->id);
$output .= file_rewrite_pluginfile_urls($cat->description,
'pluginfile.php', $context->id, 'coursecat', 'description', null);


Would the code provided above by Lior be more efficient? Better?

Cheers

Dave