/**
  * Renders a course listing.
  *
  * @param coursecat $category The currently selected category. This is what the listing is focused on.
  * @param course_in_list $course The currently selected course.
  * @param int $page The page being displayed.
  * @param int $perpage The number of courses to display per page.
  * @return string
  */
 public function course_listing(coursecat $category = null, course_in_list $course = null, $page = 0, $perpage = 20)
 {
     if ($category === null) {
         $html = html_writer::start_div('select-a-category');
         $html .= html_writer::tag('h3', get_string('courses'));
         $html .= $this->output->notification(get_string('selectacategory'), 'notifymessage');
         $html .= html_writer::end_div();
         return $html;
     }
     $page = max($page, 0);
     $perpage = max($perpage, 2);
     $totalcourses = $category->coursecount;
     $totalpages = ceil($totalcourses / $perpage);
     if ($page > $totalpages - 1) {
         $page = $totalpages - 1;
     }
     $options = array('offset' => $page * $perpage, 'limit' => $perpage);
     $courseid = isset($course) ? $course->id : null;
     $class = '';
     if ($page === 0) {
         $class .= ' firstpage';
     }
     if ($page + 1 === (int) $totalpages) {
         $class .= ' lastpage';
     }
     $html = html_writer::start_div('course-listing' . $class, array('data-category' => $category->id, 'data-page' => $page, 'data-totalpages' => $totalpages, 'data-totalcourses' => $totalcourses, 'data-canmoveoutof' => $category->can_move_courses_out_of() && $category->can_move_courses_into()));
     $html .= html_writer::tag('h3', $category->get_formatted_name());
     $html .= $this->course_listing_actions($category, $course, $perpage);
     $html .= $this->listing_pagination($category, $page, $perpage);
     $html .= html_writer::start_tag('ul', array('class' => 'ml'));
     foreach ($category->get_courses($options) as $listitem) {
         $html .= $this->course_listitem($category, $listitem, $courseid);
     }
     $html .= html_writer::end_tag('ul');
     $html .= $this->listing_pagination($category, $page, $perpage, true);
     $html .= $this->course_bulk_actions($category);
     $html .= html_writer::end_div();
     return $html;
 }