Commit c1e15d20 authored by Marina Glancy's avatar Marina Glancy
Browse files

MDL-57898 core_course: display course custom fields in course listings

This commit is part of work on Custom fields API,
to minimize commit history in moodle core the work of a team of developers was split
into several commits with different authors but the authorship of individual
lines of code may be different from the commit author.
parent 5af9aa63
......@@ -903,6 +903,18 @@ class core_course_category implements renderable, cacheable_object, IteratorAggr
$cache->set_many($values);
}
/**
* Preloads the custom fields values in bulk
*
* @param array $records
*/
public static function preload_custom_fields(array &$records) {
$customfields = \core_course\customfield\course_handler::create()->get_instances_data(array_keys($records));
foreach ($customfields as $courseid => $data) {
$records[$courseid]->customfields = $data;
}
}
/**
* Verify user enrollments for multiple course-user combinations
*
......@@ -1009,6 +1021,10 @@ class core_course_category implements renderable, cacheable_object, IteratorAggr
if (!empty($options['coursecontacts'])) {
self::preload_course_contacts($list);
}
// Preload custom fields if necessary - saves DB queries later to do it for each course separately.
if (!empty($options['customfields'])) {
self::preload_custom_fields($list);
}
return $list;
}
......@@ -1404,6 +1420,10 @@ class core_course_category implements renderable, cacheable_object, IteratorAggr
if (!empty($options['coursecontacts'])) {
self::preload_course_contacts($records);
}
// Preload custom fields if necessary - saves DB queries later to do it for each course separately.
if (!empty($options['customfields'])) {
self::preload_custom_fields($records);
}
// If option 'idonly' is specified no further action is needed, just return list of ids.
if (!empty($options['idonly'])) {
return array_keys($records);
......@@ -1493,6 +1513,10 @@ class core_course_category implements renderable, cacheable_object, IteratorAggr
if (!empty($preloadcoursecontacts)) {
self::preload_course_contacts($records);
}
// Preload custom fields if necessary - saves DB queries later to do it for each course separately.
if (!empty($options['customfields'])) {
self::preload_custom_fields($records);
}
// If option 'idonly' is specified no further action is needed, just return list of ids.
if (!empty($options['idonly'])) {
return array_keys($records);
......@@ -1606,6 +1630,10 @@ class core_course_category implements renderable, cacheable_object, IteratorAggr
if (!empty($options['idonly'])) {
return array_keys($records);
}
// Preload custom fields if necessary - saves DB queries later to do it for each course separately.
if (!empty($options['customfields'])) {
self::preload_custom_fields($records);
}
// Prepare the list of core_course_list_element objects.
foreach ($ids as $id) {
$courses[$id] = new core_course_list_element($records[$id]);
......@@ -1645,6 +1673,10 @@ class core_course_category implements renderable, cacheable_object, IteratorAggr
if (!empty($options['coursecontacts'])) {
self::preload_course_contacts($list);
}
// Preload custom fields if necessary - saves DB queries later to do it for each course separately.
if (!empty($options['customfields'])) {
self::preload_custom_fields($list);
}
// If option 'idonly' is specified no further action is needed, just return list of ids.
if (!empty($options['idonly'])) {
return array_keys($list);
......
......@@ -200,6 +200,28 @@ class core_course_list_element implements IteratorAggregate {
return $this->coursecontacts;
}
/**
* Returns custom fields data for this course
*
* @return \core_customfield\data_controller[]
*/
public function get_custom_fields(): array {
if (!isset($this->record->customfields)) {
$this->record->customfields = \core_course\customfield\course_handler::create()->get_instance_data($this->id);
}
return $this->record->customfields;
}
/**
* Does this course have custom fields
*
* @return bool
*/
public function has_custom_fields(): bool {
$customfields = $this->get_custom_fields();
return !empty($customfields);
}
/**
* Checks if course has any associated overview files
*
......
......@@ -1116,7 +1116,8 @@ class core_course_renderer extends plugin_renderer_base {
// 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()) {
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));
......@@ -1221,6 +1222,13 @@ class core_course_renderer extends plugin_renderer_base {
}
}
// Display custom fields.
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;
}
......@@ -2497,6 +2505,7 @@ class coursecat_helper {
// and core_course_category::search_courses().
$this->coursesdisplayoptions['summary'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_AUTO;
$this->coursesdisplayoptions['coursecontacts'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_EXPANDED;
$this->coursesdisplayoptions['customfields'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_COLLAPSED;
return $this;
}
......@@ -2543,6 +2552,7 @@ class coursecat_helper {
* this may be a huge list!
* - summary - preloads fields 'summary' and 'summaryformat'
* - coursecontacts - preloads course contacts
* - customfields - preloads custom fields data
* - isenrolled - preloads indication whether this user is enrolled in the course
* - sort - list of fields to sort. Example
* array('idnumber' => 1, 'shortname' => 1, 'id' => -1)
......
......@@ -618,14 +618,16 @@ span.editinstructions {
.coursebox > .info > .coursename,
.coursebox .content .teachers,
.coursebox .content .courseimage,
.coursebox .content .coursefile {
.coursebox .content .coursefile,
.coursebox .content .customfields-container {
float: left;
clear: left;
}
.coursebox .content .teachers,
.coursebox .content .courseimage,
.coursebox .content .coursefile {
.coursebox .content .coursefile,
.coursebox .content .customfields-container {
width: 40%;
}
......@@ -683,7 +685,8 @@ span.editinstructions {
.coursebox .content .courseimage,
.coursebox .content .coursefile,
.coursebox .content .teachers,
.coursebox.remotecoursebox .remotecourseinfo {
.coursebox.remotecoursebox .remotecourseinfo,
.coursebox .content .customfields-container {
margin: 15px 5px 5px;
padding: 0;
}
......
......@@ -12090,13 +12090,15 @@ span.editinstructions {
.coursebox > .info > .coursename,
.coursebox .content .teachers,
.coursebox .content .courseimage,
.coursebox .content .coursefile {
.coursebox .content .coursefile,
.coursebox .content .customfields-container {
float: left;
clear: left; }
.coursebox .content .teachers,
.coursebox .content .courseimage,
.coursebox .content .coursefile {
.coursebox .content .coursefile,
.coursebox .content .customfields-container {
width: 40%; }
.coursebox > .info > h3.coursename {
......@@ -12143,7 +12145,8 @@ span.editinstructions {
.coursebox .content .courseimage,
.coursebox .content .coursefile,
.coursebox .content .teachers,
.coursebox.remotecoursebox .remotecourseinfo {
.coursebox.remotecoursebox .remotecourseinfo,
.coursebox .content .customfields-container {
margin: 15px 5px 5px;
padding: 0; }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment