MDL-37009 Display course search results using new renderer methods
authorMarina Glancy <marina@moodle.com>
Tue, 19 Mar 2013 04:50:02 +0000 (15:50 +1100)
committerMarina Glancy <marina@moodle.com>
Mon, 1 Apr 2013 23:54:49 +0000 (10:54 +1100)
course/renderer.php
course/search.php

index f3783e0..dffbda0 100644 (file)
@@ -1653,6 +1653,73 @@ class core_course_renderer extends plugin_renderer_base {
 
         return $output;
     }
+
+    /**
+     * Renders html to display search result page
+     *
+     * @param array $searchcriteria may contain elements: search, blocklist, modulelist, tagid
+     * @return string
+     */
+    public function search_courses($searchcriteria) {
+        global $CFG;
+        $content = '';
+        if (!empty($searchcriteria)) {
+            // print search results
+
+            $displayoptions = array('sort' => array('displayname' => 1));
+            // take the current page and number of results per page from query
+            $perpage = optional_param('perpage', 0, PARAM_RAW);
+            if ($perpage !== 'all') {
+                $displayoptions['limit'] = ((int)$perpage <= 0) ? $CFG->coursesperpage : (int)$perpage;
+                $page = optional_param('page', 0, PARAM_INT);
+                $displayoptions['offset'] = $displayoptions['limit'] * $page;
+            }
+            // options 'paginationurl' and 'paginationallowall' are only used in method coursecat_courses()
+            $displayoptions['paginationurl'] = new moodle_url('/course/search.php', $searchcriteria);
+            $displayoptions['paginationallowall'] = true; // allow adding link 'View all'
+
+            $class = 'course-search-result';
+            foreach ($searchcriteria as $key => $value) {
+                if (!empty($value)) {
+                    $class .= ' course-search-result-'. $key;
+                }
+            }
+            $chelper = new coursecat_helper();
+            $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT)->
+                    set_courses_display_options($displayoptions)->
+                    set_search_criteria($searchcriteria)->
+                    set_attributes(array('class' => $class));
+
+            $courses = coursecat::search_courses($searchcriteria, $chelper->get_courses_display_options());
+            $totalcount = coursecat::search_courses_count($searchcriteria);
+            $courseslist = $this->coursecat_courses($chelper, $courses, $totalcount);
+
+            if (!$totalcount) {
+                if (!empty($searchcriteria['search'])) {
+                    $content .= $this->heading(get_string('nocoursesfound', '', $searchcriteria['search']));
+                } else {
+                    $content .= $this->heading(get_string('novalidcourses'));
+                }
+            } else {
+                $content .= $this->heading(get_string('searchresults'). ": $totalcount");
+                $content .= $courseslist;
+            }
+
+            if (!empty($searchcriteria['search'])) {
+                // print search form only if there was a search by search string, otherwise it is confusing
+                $content .= $this->box_start('generalbox mdl-align');
+                $content .= $this->course_search_form($searchcriteria['search']);
+                $content .= $this->box_end();
+            }
+        } else {
+            // just print search form
+            $content .= $this->box_start('generalbox mdl-align');
+            $content .= $this->course_search_form();
+            $content .= html_writer::tag('div', get_string("searchhelp"), array('class' => 'searchhelp'));
+            $content .= $this->box_end();
+        }
+        return $content;
+    }
 }
 
 /**
index 3605fc5..d365ade 100644 (file)
@@ -72,109 +72,30 @@ $strsearch = new lang_string("search");
 $strsearchresults = new lang_string("searchresults");
 $strnovalidcourses = new lang_string('novalidcourses');
 
-if (empty($searchcriteria)) {
-    // no search criteria specified, print page with just search form
-    $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
-    $PAGE->navbar->add($strsearch);
-    $PAGE->set_title("$site->fullname : $strsearch");
-    $PAGE->set_heading($site->fullname);
-
-    echo $OUTPUT->header();
-    echo $OUTPUT->box_start();
-    echo "<center>";
-    echo "<br />";
-    echo $courserenderer->course_search_form('', 'plain');
-    echo "<br /><p>";
-    print_string("searchhelp");
-    echo "</p>";
-    echo "</center>";
-    echo $OUTPUT->box_end();
-    echo $OUTPUT->footer();
-    exit;
-}
-
-// get list of courses
-$searchoptions = array('recursive' => true, 'sort' => array('fullname' => 1));
-if ($perpage !== 'all') {
-   $searchoptions['offset'] = $page * $perpage;
-   $searchoptions['limit'] = $perpage;
-}
-$courses = coursecat::get(0)->search_courses($searchcriteria, $searchoptions);
-$totalcount = coursecat::get(0)->search_courses_count($searchcriteria, $searchoptions);
-
-$searchform = '';
-// Turn editing should be visible if user have system or category level capability
-if (!empty($courses) && (can_edit_in_category() || !empty($usercatlist))) {
-    $aurl = new moodle_url('/course/manage.php', $searchcriteria);
-    $searchform = $OUTPUT->single_button($aurl, get_string('managecourses'), 'get');
-} else {
-    $searchform = $courserenderer->course_search_form($search, 'navbar');
-}
-
 $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
 $PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
 if (!empty($search)) {
     $PAGE->navbar->add(s($search));
 }
-$PAGE->set_title("$site->fullname : $strsearchresults");
-$PAGE->set_heading($site->fullname);
-$PAGE->set_button($searchform);
-
-echo $OUTPUT->header();
-
-if ($courses) {
-    echo $OUTPUT->heading("$strsearchresults: $totalcount");
-
-    // add the module/block parameter to the paging bar if they exists
-    $modulelink = "";
-    if (!empty($modulelist) and confirm_sesskey()) {
-        $modulelink = "&amp;modulelist=".$modulelist."&amp;sesskey=".sesskey();
-    } else if (!empty($blocklist) and confirm_sesskey()) {
-        $modulelink = "&amp;blocklist=".$blocklist."&amp;sesskey=".sesskey();
-    }
-
-    print_navigation_bar($totalcount, $page, $perpage, $searchcriteria);
-
-    // Show list of courses
-    echo $courserenderer->courses_list($courses, $search, true);
-
-    print_navigation_bar($totalcount, $page, $perpage, $searchcriteria);
 
+if (empty($searchcriteria)) {
+    // no search criteria specified, print page with just search form
+    $PAGE->set_title("$site->fullname : $strsearch");
 } else {
-    if (!empty($search)) {
-        echo $OUTPUT->heading(get_string("nocoursesfound",'', s($search)));
-    }
-    else {
-        echo $OUTPUT->heading($strnovalidcourses);
+    // this is search results page
+    $PAGE->set_title("$site->fullname : $strsearchresults");
+    // Link to manage search results should be visible if user have system or category level capability
+    if ((can_edit_in_category() || !empty($usercatlist))) {
+        $aurl = new moodle_url('/course/manage.php', $searchcriteria);
+        $searchform = $OUTPUT->single_button($aurl, get_string('managecourses'), 'get');
+    } else {
+        $searchform = $courserenderer->course_search_form($search, 'navbar');
     }
+    $PAGE->set_button($searchform);
 }
 
-echo "<br /><br />";
-
-echo $courserenderer->course_search_form($search);
+$PAGE->set_heading($site->fullname);
 
+echo $OUTPUT->header();
+echo $courserenderer->search_courses($searchcriteria);
 echo $OUTPUT->footer();
-
-/**
- * Print a list navigation bar
- * Display page numbers, and a link for displaying all entries
- * @param int $totalcount number of entry to display
- * @param int $page page number
- * @param int $perpage number of entry per page
- * @param array $search
- */
-function print_navigation_bar($totalcount, $page, $perpage, $search) {
-    global $OUTPUT, $CFG;
-    $url = new moodle_url('/course/search.php', $search);
-    if ($perpage !== 'all' && $totalcount > $perpage) {
-        echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $url->out(false, array('perpage' => $perpage)));
-        echo "<center><p>";
-        echo html_writer::link($url->out(false, array('perpage' => 'all')), get_string("showall", "", $totalcount));
-        echo "</p></center>";
-    } else if ($perpage === 'all') {
-        echo "<center><p>";
-        echo html_writer::link($url->out(false, array('perpage' => $CFG->coursesperpage)),
-                get_string("showperpage", "", $CFG->coursesperpage));
-        echo "</p></center>";
-    }
-}