From ebd08abce8a9d4a22413270c7aa5247bcf0b2503 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mikel=20Mart=C3=ADn?= Date: Tue, 28 Apr 2020 10:38:14 +0200 Subject: [PATCH] MDL-68177 course: split course_info_box into different methods --- course/renderer.php | 244 ++++++++++++++++++++++++++++---------------- course/upgrade.txt | 9 ++ 2 files changed, 167 insertions(+), 86 deletions(-) diff --git a/course/renderer.php b/course/renderer.php index 784d6ee2a34..f7332349389 100644 --- a/course/renderer.php +++ b/course/renderer.php @@ -1122,6 +1122,58 @@ class core_course_renderer extends plugin_renderer_base { return $content; } + /** + * Returns HTML to display course name. + * + * @param coursecat_helper $chelper + * @param core_course_list_element $course + * @return string + */ + protected function course_name(coursecat_helper $chelper, core_course_list_element $course): string { + $content = ''; + if ($chelper->get_show_courses() >= self::COURSECAT_SHOW_COURSES_EXPANDED) { + $nametag = 'h3'; + } else { + $nametag = 'div'; + } + $coursename = $chelper->get_course_formatted_name($course); + $coursenamelink = html_writer::link(new moodle_url('/course/view.php', ['id' => $course->id]), + $coursename, ['class' => $course->visible ? '' : 'dimmed']); + $content .= html_writer::tag($nametag, $coursenamelink, ['class' => 'coursename']); + // If we display course in collapsed form but the course has summary or course contacts, display the link to the info page. + $content .= html_writer::start_tag('div', ['class' => 'moreinfo']); + if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) { + if ($course->has_summary() || $course->has_course_contacts() || $course->has_course_overviewfiles() + || $course->has_custom_fields()) { + $url = new moodle_url('/course/info.php', ['id' => $course->id]); + $image = $this->output->pix_icon('i/info', $this->strings->summary); + $content .= html_writer::link($url, $image, ['title' => $this->strings->summary]); + // Make sure JS file to expand course content is included. + $this->coursecat_include_js(); + } + } + $content .= html_writer::end_tag('div'); + return $content; + } + + /** + * Returns HTML to display course enrolment icons. + * + * @param core_course_list_element $course + * @return string + */ + protected function course_enrolment_icons(core_course_list_element $course): string { + $content = ''; + if ($icons = enrol_get_course_info_icons($course)) { + $content .= html_writer::start_tag('div', ['class' => 'enrolmenticons']); + foreach ($icons as $icon) { + $content .= $this->render($icon); + } + $content .= html_writer::end_tag('div'); + } + return $content; + } + /** * Displays one course in the list of courses. * @@ -1146,11 +1198,8 @@ class core_course_renderer extends plugin_renderer_base { } $content = ''; $classes = trim('coursebox clearfix '. $additionalclasses); - if ($chelper->get_show_courses() >= self::COURSECAT_SHOW_COURSES_EXPANDED) { - $nametag = 'h3'; - } else { + if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) { $classes .= ' collapsed'; - $nametag = 'div'; } // .coursebox @@ -1161,128 +1210,151 @@ class core_course_renderer extends plugin_renderer_base { )); $content .= html_writer::start_tag('div', array('class' => 'info')); - - // course name - $coursename = $chelper->get_course_formatted_name($course); - $coursenamelink = html_writer::link(new moodle_url('/course/view.php', array('id' => $course->id)), - $coursename, array('class' => $course->visible ? '' : 'dimmed')); - $content .= html_writer::tag($nametag, $coursenamelink, array('class' => 'coursename')); - // If we display course in collapsed form but the course has summary or course contacts, display the link to the info page. - $content .= html_writer::start_tag('div', array('class' => 'moreinfo')); - if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) { - if ($course->has_summary() || $course->has_course_contacts() || $course->has_course_overviewfiles() - || $course->has_custom_fields()) { - $url = new moodle_url('/course/info.php', array('id' => $course->id)); - $image = $this->output->pix_icon('i/info', $this->strings->summary); - $content .= html_writer::link($url, $image, array('title' => $this->strings->summary)); - // Make sure JS file to expand course content is included. - $this->coursecat_include_js(); - } - } - $content .= html_writer::end_tag('div'); // .moreinfo - - // print enrolmenticons - if ($icons = enrol_get_course_info_icons($course)) { - $content .= html_writer::start_tag('div', array('class' => 'enrolmenticons')); - foreach ($icons as $pix_icon) { - $content .= $this->render($pix_icon); - } - $content .= html_writer::end_tag('div'); // .enrolmenticons - } - - $content .= html_writer::end_tag('div'); // .info + $content .= $this->course_name($chelper, $course); + $content .= $this->course_enrolment_icons($course); + $content .= html_writer::end_tag('div'); $content .= html_writer::start_tag('div', array('class' => 'content')); $content .= $this->coursecat_coursebox_content($chelper, $course); - $content .= html_writer::end_tag('div'); // .content + $content .= html_writer::end_tag('div'); $content .= html_writer::end_tag('div'); // .coursebox return $content; } /** - * Returns HTML to display course content (summary, course contacts and optionally category name) - * - * This method is called from coursecat_coursebox() and may be re-used in AJAX + * Returns HTML to display course summary. * - * @param coursecat_helper $chelper various display options - * @param stdClass|core_course_list_element $course + * @param coursecat_helper $chelper + * @param core_course_list_element $course * @return string */ - protected function coursecat_coursebox_content(coursecat_helper $chelper, $course) { - global $CFG; - if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) { - return ''; - } - if ($course instanceof stdClass) { - $course = new core_course_list_element($course); - } + protected function course_summary(coursecat_helper $chelper, core_course_list_element $course): string { $content = ''; - - // display course summary if ($course->has_summary()) { - $content .= html_writer::start_tag('div', array('class' => 'summary')); + $content .= html_writer::start_tag('div', ['class' => 'summary']); $content .= $chelper->get_course_formatted_summary($course, - array('overflowdiv' => true, 'noclean' => true, 'para' => false)); - $content .= html_writer::end_tag('div'); // .summary + array('overflowdiv' => true, 'noclean' => true, 'para' => false)); + $content .= html_writer::end_tag('div'); } + return $content; + } + + /** + * Returns HTML to display course contacts. + * + * @param core_course_list_element $course + * @return string + */ + protected function course_contacts(core_course_list_element $course) { + $content = ''; + if ($course->has_course_contacts()) { + $content .= html_writer::start_tag('ul', ['class' => 'teachers']); + foreach ($course->get_course_contacts() as $coursecontact) { + $rolenames = array_map(function ($role) { + return $role->displayname; + }, $coursecontact['roles']); + $name = implode(", ", $rolenames).': '. + html_writer::link(new moodle_url('/user/view.php', + ['id' => $coursecontact['user']->id, 'course' => SITEID]), + $coursecontact['username']); + $content .= html_writer::tag('li', $name); + } + $content .= html_writer::end_tag('ul'); + } + return $content; + } + + /** + * Returns HTML to display course overview files. + * + * @param core_course_list_element $course + * @return string + */ + protected function course_overview_files(core_course_list_element $course): string { + global $CFG; - // display course overview files $contentimages = $contentfiles = ''; foreach ($course->get_course_overviewfiles() as $file) { $isimage = $file->is_valid_image(); - $url = file_encode_url("$CFG->wwwroot/pluginfile.php", - '/'. $file->get_contextid(). '/'. $file->get_component(). '/'. - $file->get_filearea(). $file->get_filepath(). $file->get_filename(), !$isimage); + $url = moodle_url::make_file_url("$CFG->wwwroot/pluginfile.php", + '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . + $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage); if ($isimage) { $contentimages .= html_writer::tag('div', - html_writer::empty_tag('img', array('src' => $url)), - array('class' => 'courseimage')); + html_writer::empty_tag('img', ['src' => $url]), + ['class' => 'courseimage']); } else { $image = $this->output->pix_icon(file_file_icon($file, 24), $file->get_filename(), 'moodle'); - $filename = html_writer::tag('span', $image, array('class' => 'fp-icon')). - html_writer::tag('span', $file->get_filename(), array('class' => 'fp-filename')); + $filename = html_writer::tag('span', $image, ['class' => 'fp-icon']). + html_writer::tag('span', $file->get_filename(), ['class' => 'fp-filename']); $contentfiles .= html_writer::tag('span', - html_writer::link($url, $filename), - array('class' => 'coursefile fp-filename-icon')); + html_writer::link($url, $filename), + ['class' => 'coursefile fp-filename-icon']); } } - $content .= $contentimages. $contentfiles; - - // Display course contacts. See core_course_list_element::get_course_contacts(). - if ($course->has_course_contacts()) { - $content .= html_writer::start_tag('ul', array('class' => 'teachers')); - foreach ($course->get_course_contacts() as $coursecontact) { - $rolenames = array_map(function ($role) { - return $role->displayname; - }, $coursecontact['roles']); - $name = implode(", ", $rolenames).': '. - html_writer::link(new moodle_url('/user/view.php', - array('id' => $coursecontact['user']->id, 'course' => SITEID)), - $coursecontact['username']); - $content .= html_writer::tag('li', $name); - } - $content .= html_writer::end_tag('ul'); // .teachers - } + return $contentimages . $contentfiles; + } - // display course category if necessary (for example in search results) + /** + * Returns HTML to display course category name. + * + * @param coursecat_helper $chelper + * @param core_course_list_element $course + * @return string + */ + protected function course_category_name(coursecat_helper $chelper, core_course_list_element $course): string { + $content = ''; + // Display course category if necessary (for example in search results). if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT) { if ($cat = core_course_category::get($course->category, IGNORE_MISSING)) { - $content .= html_writer::start_tag('div', array('class' => 'coursecat')); + $content .= html_writer::start_tag('div', ['class' => 'coursecat']); $content .= get_string('category').': '. - html_writer::link(new moodle_url('/course/index.php', array('categoryid' => $cat->id)), - $cat->get_formatted_name(), array('class' => $cat->visible ? '' : 'dimmed')); - $content .= html_writer::end_tag('div'); // .coursecat + html_writer::link(new moodle_url('/course/index.php', ['categoryid' => $cat->id]), + $cat->get_formatted_name(), ['class' => $cat->visible ? '' : 'dimmed']); + $content .= html_writer::end_tag('div'); } } + return $content; + } - // Display custom fields. + /** + * Returns HTML to display course custom fields. + * + * @param core_course_list_element $course + * @return string + */ + protected function course_custom_fields(core_course_list_element $course): string { + $content = ''; if ($course->has_custom_fields()) { $handler = core_course\customfield\course_handler::create(); $customfields = $handler->display_custom_fields_data($course->get_custom_fields()); $content .= \html_writer::tag('div', $customfields, ['class' => 'customfields-container']); } + return $content; + } + /** + * Returns HTML to display course content (summary, course contacts and optionally category name) + * + * This method is called from coursecat_coursebox() and may be re-used in AJAX + * + * @param coursecat_helper $chelper various display options + * @param stdClass|core_course_list_element $course + * @return string + */ + protected function coursecat_coursebox_content(coursecat_helper $chelper, $course) { + if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) { + return ''; + } + if ($course instanceof stdClass) { + $course = new core_course_list_element($course); + } + $content = $this->course_summary($chelper, $course); + $content .= $this->course_overview_files($course); + $content .= $this->course_contacts($course); + $content .= $this->course_category_name($chelper, $course); + $content .= $this->course_custom_fields($course); return $content; } diff --git a/course/upgrade.txt b/course/upgrade.txt index 195a6ebf7fc..45dc3a15c63 100644 --- a/course/upgrade.txt +++ b/course/upgrade.txt @@ -5,6 +5,15 @@ information provided here is intended especially for developers. * The function get_module_metadata is now deprecated. Please use \core_course\local\service\content_item_service instead. * Activity module names are now PARAM_ALPHANUM instead of PARAM_ALPHA so integers can be used in activity module names +* The following functions have been added to core_course_renderer class to have more granularity. They can be overriden in + extending classes: + - course_name + - course_enrolment_icons + - course_summary + - course_contacts + - course_overview_files + - course_category_name + - course_custom_fields === 3.8 === -- 2.43.0