MDL-21198 new simple_button output component, this could finally solve potential...
authorPetr Skoda <skodak@moodle.org>
Sun, 3 Jan 2010 17:08:21 +0000 (17:08 +0000)
committerPetr Skoda <skodak@moodle.org>
Sun, 3 Jan 2010 17:08:21 +0000 (17:08 +0000)
lib/deprecatedlib.php
lib/outputcomponents.php
lib/outputrenderers.php
mod/quiz/accessrules.php
mod/quiz/summary.php

index 744fbc86695512985a4b372ed9c54cb2abf1b39f..73046714623d3a9c86f5c9eb231c4b107f58d5ec 100644 (file)
@@ -2483,11 +2483,9 @@ function button_to_popup_window ($url, $name=null, $linkname=null,
     }
 
     // Create a html_button object
-    $form = new html_form();
-    $form->button->text = $linkname;
+    $form = new single_button($url, $text, 'post');
     $form->button->title = $title;
     $form->button->id = $id;
-    $form->url = $url;
     $form->add_class($class);
 
     // Parse the $options string
@@ -2550,19 +2548,14 @@ function print_single_button($link, $options, $label='OK', $method='get', $notus
 
     // Cast $options to array
     $options = (array) $options;
-    $form = new html_form();
-    $form->url = new moodle_url($link, $options);
-    $form->button->text = $label;
-    $form->button->disabled = $disabled;
-    $form->button->title = $tooltip;
-    $form->method = $method;
-    $form->id = $formid;
+
+    $button = new sibngle_button(new moodle_url($link, $options), $label, $method, array('disabled'=>$disabled, 'title'=>$tooltip, 'id'=>$id));
 
     if ($jsconfirmmessage) {
-        $form->button->add_confirm_action($jsconfirmmessage);
+        $button->button->add_confirm_action($jsconfirmmessage);
     }
 
-    $output = $OUTPUT->single_button($form);
+    $output = $OUTPUT->single_button($button);
 
     if ($return) {
         return $output;
index c1f0d07a486d3631716cec9d8f9cf6b2387667aa..e0404e77015696154f604fefd520e146f269d41b 100644 (file)
@@ -1497,10 +1497,6 @@ class html_form extends html_component {
      * @var mixed $url A moodle_url including params or a string
      */
     public $url;
-    /**
-     * @var array $params Optional array of parameters. Ignored if $url instanceof moodle_url
-     */
-    public $params = array();
     /**
      * @var boolean $showbutton If true, the submit button will always be shown even if JavaScript is available
      */
@@ -1524,7 +1520,7 @@ class html_form extends html_component {
     public function __construct(array $options = null) {
         parent::__construct($options);
         $this->button = new html_button();
-        $this->button->text = get_string('ok');
+        $this->button->text = get_string('go');
     }
 
     /**
@@ -1537,11 +1533,12 @@ class html_form extends html_component {
             throw new coding_exception('A html_form must have a $url value (string or moodle_url).');
         }
 
-        if (!($this->url instanceof moodle_url)) {
-            $this->url = new moodle_url($this->url, $this->params);
+        if (is_string($this->url)) {
+            $this->url = new moodle_url($this->url);
         }
 
         if ($this->method == 'post') {
+            // automatic CSRF protection
             $this->url->param('sesskey', sesskey());
         }
 
@@ -1549,6 +1546,7 @@ class html_form extends html_component {
     }
 
     public static function make_button($url, array $params=null, $label=null, $method='post', array $formoptions=null) {
+        //TODO: to be removed soon, repalced by ew single_button()
         $form = new html_form($formoptions);
         $form->url = new moodle_url($url, $params);
         if ($label !== null) {
@@ -1561,6 +1559,36 @@ class html_form extends html_component {
 }
 
 
+/**
+ * A component representing a simple form with only one button.
+ *
+ * @copyright 2009 Petr Skoda
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since     Moodle 2.0
+ */
+class single_button extends html_form {
+    /**
+     * Constructor
+     * @param string|moodle_url 
+     * @param string $label button text
+     * @param string $method get or post submit method
+     * @param array $options associative array form attributes + {disabled, title}
+     */
+    public function __construct($url, $label, $method='post', array $options=null) {
+        parent::__construct($options);
+        $this->url = $url;
+        $form->method = $method;
+        $this->button->text = $label;
+        if (!empty($options['disabled'])) {
+            $this->button->disabled = true;
+        }
+        if (!empty($options['title'])) {
+            $this->button->title = $options['title'];
+        }
+    }
+}
+
+
 /**
  * Component representing a list.
  *
index 668786e6dc8d596299b24a3a40d2b19894b86397..2a281981ad81767885ecf93053c0f7f48dd1ec05 100644 (file)
@@ -997,27 +997,23 @@ class core_renderer extends renderer_base {
      * Returns a form with single button.
      * If first parameter is html_form instance all other parameters are ignored.
      *
-     * @param string|moodle_url|html_form $url_or_form 
+     * @param string|moodle_url|single_button $singlebutton_or_form 
      * @param string $label button text
      * @param string $method get or post submit method
      * @param array $options associative array {disabled, title}
      * @return string HTML fragment
      */
-    public function single_button($url_or_form, $label=null, $method='post', array $options=null) {
-        if ($url_or_form instanceof html_form) {
-            $form = $url_or_form;
+    public function single_button($singlebutton_or_form, $label=null, $method='post', array $options=null) {
+        if ($singlebutton_or_form instanceof single_button) {
+            $button = $singlebutton_or_form;
             if (func_num_args() > 1) {
                 debugging('html_form instance used as first parameter of $OUTPUT->single_button(), all other parameters are ignored.');
             }
         } else {
-            $form = html_form::make_button($url_or_form, null, $label, $method);
-            $form->button->disabled = !empty($options['disabled']);
-            if (!empty($options['title'])) {
-                $form->button->title = $options['title'];
-            }
+            $button = new single_button($url_or_form, $label, $method, $options);
         }
 
-        return $this->button($form);
+        return $this->button($button);
     }
 
     /**
index 3cc1d45a1367e570e246bd8eb62cd1be7a1330ed..21a9df77cffa2af003b336c1730fb9bac621f0c4 100644 (file)
@@ -220,7 +220,7 @@ class quiz_access_manager {
         if ($this->securewindow_required($canpreview)) {
             $this->_securewindowrule->print_start_attempt_button($buttontext, $strconfirmstartattempt);
         } else {
-            $form = html_form::make_button($this->_quizobj->start_attempt_url(), array('cmid' => $this->_quizobj->get_cmid()), $buttontext);
+            $form = new single_button(new moodle_url($this->_quizobj->start_attempt_url(), array('cmid' => $this->_quizobj->get_cmid())), $buttontext);
             if ($strconfirmstartattempt) {
                 $form->button->add_confirm_action($strconfirmstartattempt);
             }
@@ -745,10 +745,7 @@ class securewindow_access_rule extends quiz_access_rule_base {
      */
     public function make_review_link($linktext, $attemptid) {
         global $OUTPUT;
-        $form = new html_form();
-        $form->button->text = $linktext;
-        $form->button->title = $form->button->text;
-        $form->url = $this->_quizobj->review_url($attemptid);
+        $form = new single_button($this->_quizobj->review_url($attemptid), $linktext);
         $form->button->add_action(new popup_action('click', $form->url, 'quizpopup', $this->windowoptions));
         return $OUTPUT->single_button($form);
     }
index 275da221d3f510239d283c71b94630f755dc4492..4cc204538c13b6252594d0a21540e86ff0b794e0 100644 (file)
@@ -124,7 +124,7 @@ $options = array(
     'sesskey' => sesskey(),
 );
 
-$form = html_form::make_button($attemptobj->processattempt_url(), $options, get_string('finishattempt', 'quiz'));
+$form = new single_button(new moodle_url($attemptobj->processattempt_url(), $options), get_string('finishattempt', 'quiz'));
 $form->id = 'responseform';
 $form->button->add_confirm_action(get_string('confirmclose', 'quiz'));