]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-19756 Improved API of popup_form
authornicolasconnault <nicolasconnault>
Mon, 10 Aug 2009 03:33:41 +0000 (03:33 +0000)
committernicolasconnault <nicolasconnault>
Mon, 10 Aug 2009 03:33:41 +0000 (03:33 +0000)
lib/deprecatedlib.php
lib/outputlib.php

index 5d0ff170fb1a7b61a5ece99b67d4551e72210cf0..d2923f21b584e552a503d82f63d5df3729c684b4 100644 (file)
@@ -3272,20 +3272,25 @@ function popup_form($baseurl, $options, $formid, $selected='', $nothing='choose'
     $targetwindow='self', $selectlabel='', $optionsextra=NULL, $submitvalue='', $disabled=false, $showbutton=false) {
     global $OUTPUT;
 
-    // debugging('popup_form() has been deprecated. Please change your code to use $OUTPUT->select($dateselector).');
+    // debugging('popup_form() has been deprecated. Please change your code to use $OUTPUT->select($select).');
 
     if (empty($options)) {
         return '';
     }
 
-    foreach ($options as $var => $val) {
-        $url = new moodle_url($baseurl . $var);
-        $options[$url->out(false, array(), false)] = $val;
-        unset($options[$var]);
+    // Extract the last param in the baseurl
+    $name = null;
+    if (preg_match('/([a-zA-Z0-9\-_]*)=$/', $baseurl, $matches)) {
+        $name = $matches[1];
     }
 
-    $select = moodle_select::make_popup_form($options, $formid, $selected, $submitvalue);
+    $baseurl = new moodle_url($baseurl);
+    $select = moodle_select::make_popup_form($baseurl, $name, $options, $formid, $selected);
     $select->disabled = $disabled;
+    
+    if (!empty($submitvalue)) {
+        $select->form->button->text = $submitvalue;
+    }
 
     if (!empty($optionsextra)) {
         debugging('The $optionsextra (11th) param to popup_form is not supported, please improve your code.', DEBUG_DEVELOPER);
index 86499c8d7af77a13ca17a945da6aca05a2d0e349..ee939bb2e3e6b1883aeb73eef5f0f05b5431c338 100644 (file)
@@ -3962,16 +3962,38 @@ class moodle_select extends moodle_html_component {
 
     /**
      * This is a shortcut for making a select popup form.
-     * @param string $baseurl The target URL up to the point of the variable that changes
+     * @param mixed $baseurl The target URL, string or moodle_url
+     * @param string $name The variable which this select's options are changing in the URL
      * @param array $options A list of value-label pairs for the popup list
      * @param string $formid id for the control. Must be unique on the page. Used in the HTML.
      * @param string $selected The option that is initially selected
-     * @param string $submitvalue Optional label for the 'Go' button. Defaults to get_string('go').
      * @return moodle_select A menu initialised as a popup form.
      */
-    public function make_popup_form($options, $formid, $selected=null, $submitvalue='') {
+    public function make_popup_form($baseurl, $name, $options, $formid, $selected=null) {
         global $CFG;
-        $select = self::make($options, 'jump', $selected);
+
+        $selectedurl = null;
+
+        if (!($baseurl instanceof moodle_url)) {
+            $baseurl = new moodle_url($baseurl);
+        }
+
+        if (!empty($selected)) {
+            $selectedurl = $baseurl->out(false, array($name => $selected), false);
+        }
+
+        if (!($baseurl instanceof moodle_url)) {
+            $baseurl = new moodle_url($baseurl);
+        }
+
+        // Replace real value by formatted URLs
+        foreach ($options as $value => $label) {
+            $options[$baseurl->out(false, array($name => $value), false)] = $label;
+            unset($options[$value]);
+        }
+
+        $select = self::make($options, 'jump', $selectedurl);
+
         $select->form = new html_form();
         $select->form->id = $formid;
         $select->form->method = 'get';
@@ -3979,10 +4001,6 @@ class moodle_select extends moodle_html_component {
         $select->form->url = new moodle_url($CFG->wwwroot . '/course/jumpto.php', array('sesskey' => sesskey()));
         $select->form->button->text = get_string('go');
 
-        if (!empty($submitvalue)) {
-            $select->form->button->text = $submitvalue;
-        }
-
         $select->id = $formid . '_jump';
 
         $select->add_action('change', 'submit_form_by_id', array('id' => $formid, 'selectid' => $select->id));
@@ -3990,6 +4008,43 @@ class moodle_select extends moodle_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) {
+        global $PAGE;
+
+        $this->initialise_options();
+
+        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) {
+                next($options);
+                $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';
+                }
+            }
+        }
+    }
+
     /**
      * Adds a help icon next to the select menu.
      *