MDL-20204 using url_select
authorPetr Skoda <skodak@moodle.org>
Wed, 10 Feb 2010 10:55:51 +0000 (10:55 +0000)
committerPetr Skoda <skodak@moodle.org>
Wed, 10 Feb 2010 10:55:51 +0000 (10:55 +0000)
grade/lib.php
lib/outputcomponents.php
mod/forum/discuss.php

index 8319ab5..c88ddbb 100644 (file)
@@ -332,7 +332,7 @@ function print_graded_users_selector($course, $actionpage, $userid=0, $groupid=0
  *
  * @return nothing or string if $return true
  */
-function print_grade_plugin_selector($plugin_info, $return=false) {
+function print_grade_plugin_selector($plugin_info, $active_type, $active_plugin, $return=false) {
     global $CFG, $OUTPUT, $PAGE;
 
     $menu = array();
@@ -346,25 +346,33 @@ function print_grade_plugin_selector($plugin_info, $return=false) {
 
         $first_plugin = reset($plugins);
 
-        $menu[$first_plugin->link.'&'] = '--'.$plugin_info['strings'][$plugin_type];
-
-        if (empty($plugins->id)) {
-            foreach ($plugins as $plugin) {
-                $menu[$plugin->link] = $plugin->string;
-                $count++;
+        $sectionname = $plugin_info['strings'][$plugin_type];
+        $section = array();
+        
+        foreach ($plugins as $plugin) {
+            // TODO: this is messy, fix plugin_info instead...
+            $link = str_replace($CFG->wwwroot, '', $plugin->link);
+            $link = str_replace('&amp;', '&', $link);
+            $section[$link] = $plugin->string;
+            $count++;
+            if ($plugin_type === $active_type and $plugin->id === $active_plugin) {
+                $active = $link;
             }
         }
+
+        if ($section) {
+            $menu[] = array($sectionname=>$section);
+        }
     }
 
     // finally print/return the popup form
     if ($count > 1) {
-        $select = html_select::make_popup_form('', '', $menu, 'choosepluginreport', '');
-        $select->override_option_values($menu);
+        $select = new url_select($menu, $active, null, 'choosepluginreport');
 
         if ($return) {
-            return $OUTPUT->select($select);
+            return $OUTPUT->render($select);
         } else {
-            echo $OUTPUT->select($select);
+            echo $OUTPUT->render($select);
         }
     } else {
         // only one option - no plugin selector needed
@@ -865,7 +873,7 @@ function print_grade_page_head($courseid, $active_type, $active_plugin=null,
     }
 
     if ($CFG->grade_navmethod == GRADE_NAVMETHOD_COMBO || $CFG->grade_navmethod == GRADE_NAVMETHOD_DROPDOWN) {
-        $returnval .= print_grade_plugin_selector($plugin_info, $return);
+        $returnval .= print_grade_plugin_selector($plugin_info, $active_type, $active_plugin, $return);
     }
     $returnval .= $OUTPUT->heading($heading);
 
index 60a471d..0dfaecf 100644 (file)
@@ -1498,50 +1498,6 @@ class html_select extends labelled_html_component {
         return $select;
     }
 
-    /**
-     * Override the URLs of the default popup_form, which only supports one base URL
-     * @param array $options value=>label pairs representing select options
-     * @return void
-     */
-    public function override_option_values($options) {
-        // TODO: this is ugly hack because components shoudl never touch global $PAGE with the exception in prepare(),
-        //       in any case this methods needs to be revisited because it does not make much sense to use the same $menu in
-        //       html_select::make_popup_form() and then again in this method!
-        global $PAGE; //TODO: remove
-
-        $originalcount = count($options);
-        $this->initialise_options();
-        $newcount = count($this->options);
-        $first = true;
-
-        reset($options);
-
-        foreach ($this->options as $optkey => $optgroup) {
-            if ($optgroup instanceof html_select_optgroup) {
-                foreach ($optgroup->options as $key => $option) {
-                    next($options);
-                    $this->options[$optkey]->options[$key]->value = key($options);
-
-                    $optionurl = new moodle_url(key($options));
-
-                    if ($optionurl->compare($PAGE->url, URL_MATCH_PARAMS)) {
-                        $this->options[$optkey]->options[$key]->selected = 'selected';
-                    }
-                }
-                next($options);
-            } else if ($optgroup instanceof html_select_option && !($first && $originalcount < $newcount)) {
-                $this->options[$optkey]->value = key($options);
-                $optionurl = new moodle_url(key($options));
-
-                if ($optionurl->compare($PAGE->url, URL_MATCH_PARAMS)) {
-                    $this->options[$optkey]->selected = 'selected';
-                }
-                next($options);
-            }
-            $first = false;
-        }
-    }
-
     /**
      * Adds a help icon next to the select menu.
      *
index ca0ef71..b7c9c30 100644 (file)
             } else {
                 $strsection = get_string("topic");
             }
-            $section = -1;
             $forummenu = array();
             foreach ($modinfo->instances['forum'] as $forumcm) {
                 if (!$forumcm->uservisible || !has_capability('mod/forum:startdiscussion',
                     continue;
                 }
 
-                if (!empty($forumcm->sectionnum) and $section != $forumcm->sectionnum) {
-                    $forummenu[] = "-------------- $strsection $forumcm->sectionnum --------------";
-                }
                 $section = $forumcm->sectionnum;
+                if (empty($forummenu[$section])) {
+                    $forummenu[$section] = array("$strsection $section" => array());
+                }
                 if ($forumcm->instance != $forum->id) {
-                    $url = $CFG->wwwroot . "/mod/forum/discuss.php?d=$discussion->id&move=$forumcm->instance&sesskey=".sesskey();
-                    $forummenu[$url] = format_string($forumcm->name);
+                    $url = "/mod/forum/discuss.php?d=$discussion->id&move=$forumcm->instance&sesskey=".sesskey();
+                    $forummenu[$section]["$strsection $section"][$url] = format_string($forumcm->name);
                 }
             }
             if (!empty($forummenu)) {
 
-                // Check for empty groups... This can occur if there is the forum we are in is
-                // the only forum within its course section.
-                foreach ($forummenu as $key=>$item) {
-                    // If this option is a group and the next option is a group OR it is
-                    // the last item in the array then remove it... or we get an exception
-                    if (strpos($item, '--------------')===0 && (!array_key_exists($key+1, $forummenu) || strpos($forummenu[$key+1], '--------------')===0)) {
-                        // Remember foreach acts on a copy of the array so things
-                        // will not get out of order
-                        unset($forummenu[$key]);
-                    }
-                }
-
                 echo "<div style=\"float:right;\">";
-                $select = html_select::make_popup_form('', '', $forummenu, 'forummenu');
-                $select->nothinglabel = get_string("movethisdiscussionto", "forum");
-                $select->form->button->text = get_string('move');
-
-                $select->override_option_values($forummenu);
-
-                echo $OUTPUT->select($select);
+                $select = new url_select($forummenu, '', array(''=>get_string("movethisdiscussionto", "forum")), 'forummenu');
+                echo $OUTPUT->render($select);
                 echo "</div>";
             }
         }