MDL-37009 Use new renderers for frontpage course and category listings
authorMarina Glancy <marina@moodle.com>
Wed, 20 Mar 2013 02:04:12 +0000 (13:04 +1100)
committerMarina Glancy <marina@moodle.com>
Mon, 1 Apr 2013 23:58:23 +0000 (10:58 +1100)
course/renderer.php
index.php

index b588bfe..b613e59 100644 (file)
@@ -1750,6 +1750,195 @@ class core_course_renderer extends plugin_renderer_base {
         }
         return '';
     }
+
+    /**
+     * Returns HTML to display one remote course
+     *
+     * @param stdClass $course remote course information, contains properties:
+           id, remoteid, shortname, fullname, hostid, summary, summaryformat, cat_name, hostname
+     * @return string
+     */
+    protected function frontpage_remote_course(stdClass $course) {
+        $url = new moodle_url('/auth/mnet/jump.php', array(
+            'hostid' => $course->hostid,
+            'wantsurl' => '/course/view.php?id='. $course->remoteid
+        ));
+
+        $output = '';
+        $output .= html_writer::start_tag('div', array('class' => 'coursebox remotecoursebox clearfix'));
+        $output .= html_writer::start_tag('div', array('class' => 'info'));
+        $output .= html_writer::start_tag('h3', array('class' => 'name'));
+        $output .= html_writer::link($url, format_string($course->fullname), array('title' => get_string('entercourse')));
+        $output .= html_writer::end_tag('h3'); // .name
+        $output .= html_writer::tag('div', '', array('class' => 'moreinfo'));
+        $output .= html_writer::end_tag('div'); // .info
+        $output .= html_writer::start_tag('div', array('class' => 'content'));
+        $output .= html_writer::start_tag('div', array('class' => 'summary'));
+        $options = new stdClass();
+        $options->noclean = true;
+        $options->para = false;
+        $options->overflowdiv = true;
+        $output .= format_text($course->summary, $course->summaryformat, $options);
+        $output .= html_writer::end_tag('div'); // .summary
+        $addinfo = format_string($course->hostname) . ' : '
+            . format_string($course->cat_name) . ' : '
+            . format_string($course->shortname);
+        $output .= html_writer::tag('div', $addinfo, array('class' => 'remotecourseinfo'));
+        $output .= html_writer::end_tag('div'); // .content
+        $output .= html_writer::end_tag('div'); // .coursebox
+        return $output;
+    }
+
+    /**
+     * Returns HTML to display one remote host
+     *
+     * @param array $host host information, contains properties: name, url, count
+     * @return string
+     */
+    protected function frontpage_remote_host($host) {
+        $output = '';
+        $output .= html_writer::start_tag('div', array('class' => 'coursebox remotehost clearfix'));
+        $output .= html_writer::start_tag('div', array('class' => 'info'));
+        $output .= html_writer::start_tag('h3', array('class' => 'name'));
+        $output .= html_writer::link($host['url'], s($host['name']), array('title' => s($host['name'])));
+        $output .= html_writer::end_tag('h3'); // .name
+        $output .= html_writer::tag('div', '', array('class' => 'moreinfo'));
+        $output .= html_writer::end_tag('div'); // .info
+        $output .= html_writer::start_tag('div', array('class' => 'content'));
+        $output .= html_writer::start_tag('div', array('class' => 'summary'));
+        $output .= $host['count'] . ' ' . get_string('courses');
+        $output .= html_writer::end_tag('div'); // .content
+        $output .= html_writer::end_tag('div'); // .coursebox
+        return $output;
+    }
+
+    /**
+     * Returns HTML to print list of courses user is enrolled to for the frontpage
+     *
+     * Also lists remote courses or remote hosts if MNET authorisation is used
+     *
+     * @return string
+     */
+    public function frontpage_my_courses() {
+        global $USER, $CFG, $DB;
+
+        if (!isloggedin() or isguestuser()) {
+            return '';
+        }
+
+        $output = '';
+        if (!empty($CFG->navsortmycoursessort)) {
+            // sort courses the same as in navigation menu
+            $sortorder = 'visible DESC,'. $CFG->navsortmycoursessort.' ASC';
+        } else {
+            $sortorder = 'visible DESC,sortorder ASC';
+        }
+        $courses  = enrol_get_my_courses('summary, summaryformat', $sortorder);
+        $rhosts   = array();
+        $rcourses = array();
+        if (!empty($CFG->mnet_dispatcher_mode) && $CFG->mnet_dispatcher_mode==='strict') {
+            $rcourses = get_my_remotecourses($USER->id);
+            $rhosts   = get_my_remotehosts();
+        }
+
+        if (!empty($courses) || !empty($rcourses) || !empty($rhosts)) {
+
+            $chelper = new coursecat_helper();
+            $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED)->
+                    set_courses_display_options(array(
+                        'viewmoreurl' => new moodle_url('/course/index.php'),
+                        'viewmoretext' => new lang_string('fulllistofcourses')
+                    ))->
+                    set_attributes(array('class' => 'frontpage-course-list-enrolled'));
+            $totalcount = $DB->count_records('course') - 1;
+            $output .= $this->coursecat_courses($chelper, $courses, $totalcount);
+
+            // MNET
+            if (!empty($rcourses)) {
+                // at the IDP, we know of all the remote courses
+                $output .= html_writer::start_tag('div', array('class' => 'courses'));
+                foreach ($rcourses as $course) {
+                    $output .= $this->frontpage_remote_course($course);
+                }
+                $output .= html_writer::end_tag('div'); // .courses
+            } elseif (!empty($rhosts)) {
+                // non-IDP, we know of all the remote servers, but not courses
+                $output .= html_writer::start_tag('div', array('class' => 'courses'));
+                foreach ($rhosts as $host) {
+                    $output .= $this->frontpage_remote_host($host);
+                }
+                $output .= html_writer::end_tag('div'); // .courses
+            }
+        }
+        return $output;
+    }
+
+    /**
+     * Returns HTML to print list of available courses for the frontpage
+     *
+     * @return string
+     */
+    public function frontpage_available_courses() {
+        global $CFG;
+        require_once($CFG->libdir. '/coursecatlib.php');
+
+        $chelper = new coursecat_helper();
+        $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED)->
+                set_courses_display_options(array(
+                    'recursive' => true,
+                    'limit' => FRONTPAGECOURSELIMIT,
+                    'viewmoreurl' => new moodle_url('/course/index.php'),
+                    'viewmoretext' => new lang_string('fulllistofcourses')));
+
+        $chelper->set_attributes(array('class' => 'frontpage-course-list-all'));
+        $courses = coursecat::get(0)->get_courses($chelper->get_courses_display_options());
+        $totalcount = coursecat::get(0)->get_courses_count($chelper->get_courses_display_options());
+        return $this->coursecat_courses($chelper, $courses, $totalcount);
+    }
+
+    /**
+     * Returns HTML to print tree with course categories and courses for the frontpage
+     *
+     * @return string
+     */
+    public function frontpage_combo_list() {
+        global $CFG;
+        require_once($CFG->libdir. '/coursecatlib.php');
+        $chelper = new coursecat_helper();
+        $chelper->set_subcat_depth($CFG->maxcategorydepth)->
+            set_categories_display_options(array(
+                'limit' => $CFG->coursesperpage,
+                'viewmoreurl' => new moodle_url('/course/category.php',
+                        array('browse' => 'categories', 'page' => 1))
+            ))->
+            set_courses_display_options(array(
+                'limit' => $CFG->coursesperpage,
+                'viewmoreurl' => new moodle_url('/course/category.php',
+                        array('browse' => 'courses', 'page' => 1))
+            ))->
+            set_attributes(array('class' => 'frontpage-category-combo'));
+        return $this->coursecat_tree($chelper, coursecat::get(0));
+    }
+
+    /**
+     * Returns HTML to print tree of course categories (with number of courses) for the frontpage
+     *
+     * @return string
+     */
+    public function frontpage_categories_list() {
+        global $CFG;
+        require_once($CFG->libdir. '/coursecatlib.php');
+        $chelper = new coursecat_helper();
+        $chelper->set_subcat_depth($CFG->maxcategorydepth)->
+                set_show_courses(self::COURSECAT_SHOW_COURSES_COUNT)->
+                set_categories_display_options(array(
+                    'limit' => $CFG->coursesperpage,
+                    'viewmoreurl' => new moodle_url('/course/category.php',
+                            array('browse' => 'categories', 'page' => 1))
+                ))->
+                set_attributes(array('class' => 'frontpage-category-names'));
+        return $this->coursecat_tree($chelper, coursecat::get(0));
+    }
 }
 
 /**
index 23423b3..8ad377b 100644 (file)
--- a/index.php
+++ b/index.php
             case FRONTPAGECOURSELIST:
                 $ncourses = $DB->count_records('course');
                 if (isloggedin() and !$hassiteconfig and !isguestuser() and empty($CFG->disablemycourses)) {
+                    $mycourseshtml = $courserenderer->frontpage_my_courses();
+                    if (empty($mycourseshtml)) {
+                        if ($DB->count_records("course_categories") > 1) {
+                            $mycourseshtml = $courserenderer->frontpage_categories_list();
+                        } else {
+                            $mycourseshtml = $courserenderer->frontpage_available_courses();
+                        }
+                    }
                     echo html_writer::tag('a', get_string('skipa', 'access', textlib::strtolower(get_string('mycourses'))), array('href'=>'#skipmycourses', 'class'=>'skip-block'));
 
                     //wrap frontpage course list in div container
                     echo html_writer::start_tag('div', array('id'=>'frontpage-course-list'));
 
                     echo $OUTPUT->heading(get_string('mycourses'), 2, 'headingblock header');
-                    print_my_moodle();
+                    echo $mycourseshtml;
+                    echo $OUTPUT->box($courserenderer->course_search_form('', 'short'), 'mdl-align');
 
                     //end frontpage course list div container
                     echo html_writer::end_tag('div');
                     echo html_writer::start_tag('div', array('id'=>'frontpage-course-list'));
 
                     echo $OUTPUT->heading(get_string('availablecourses'), 2, 'headingblock header');
-                    print_courses(0);
+                    echo $courserenderer->frontpage_available_courses();
 
                     //end frontpage course list div container
                     echo html_writer::end_tag('div');
                     echo html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipavailablecourses'));
                 } else {
                     echo html_writer::tag('div', get_string('therearecourses', '', $ncourses), array('class' => 'notifyproblem'));
-                    echo $courserenderer->course_search_form('', 'short');
+                    echo $OUTPUT->box($courserenderer->course_search_form('', 'short'), 'mdl-align');
                 }
             break;
 
                 echo html_writer::start_tag('div', array('id'=>'frontpage-category-names'));
 
                 echo $OUTPUT->heading(get_string('categories'), 2, 'headingblock header');
-                echo $OUTPUT->box_start('generalbox categorybox');
-                print_whole_category_list(NULL, NULL, NULL, -1, false);
-                echo $OUTPUT->box_end();
-                echo $courserenderer->course_search_form('', 'short');
+                echo $courserenderer->frontpage_categories_list();
+                echo $OUTPUT->box($courserenderer->course_search_form('', 'short'), 'mdl-align');
 
                 //end frontpage category names div container
                 echo html_writer::end_tag('div');
                 echo html_writer::start_tag('div', array('id'=>'frontpage-category-combo'));
 
                 echo $OUTPUT->heading(get_string('courses'), 2, 'headingblock header');
-                $renderer = $PAGE->get_renderer('core','course');
                 // if there are too many courses, building course category tree could be slow,
                 // users should go to course index page to see the whole list.
                 $coursecount = $DB->count_records('course');
                     $link = new moodle_url('/course/');
                     echo $OUTPUT->notification(get_string('maxnumcoursesincombo', 'moodle', array('link'=>$link->out(), 'maxnumofcourses'=>$CFG->numcoursesincombo, 'numberofcourses'=>$coursecount)));
                 } else {
-                    echo $renderer->course_category_tree(get_course_category_tree());
+                    echo $courserenderer->frontpage_combo_list();
                 }
-                echo $courserenderer->course_search_form('', 'short');
+                echo $OUTPUT->box($courserenderer->course_search_form('', 'short'), 'mdl-align');
 
                 //end frontpage category combo div container
                 echo html_writer::end_tag('div');