MDL-27120 More efficient use of QuickForms in backup UI
authorAshley Holman <ashley@netspot.com.au>
Thu, 7 Apr 2011 06:08:10 +0000 (15:38 +0930)
committerRajesh Taneja <rajesh@moodle.com>
Mon, 21 May 2012 03:02:16 +0000 (11:02 +0800)
backup/util/ui/backup_ui_stage.class.php
backup/util/ui/base_moodleform.class.php

index 2cc2045..fdf48c2 100644 (file)
@@ -124,6 +124,8 @@ class backup_ui_stage_initial extends backup_ui_stage {
             // Store as a variable so we can iterate by reference
             $tasks = $this->ui->get_tasks();
             // Iterate all tasks by reference
+            $add_settings = array();
+            $dependencies = array();
             foreach ($tasks as &$task) {
                 // For the initial stage we are only interested in the root settings
                 if ($task instanceof backup_root_task) {
@@ -134,17 +136,23 @@ class backup_ui_stage_initial extends backup_ui_stage {
                         if ($setting->get_name() == 'filename') {
                             continue;
                         }
-                        $form->add_setting($setting, $task);
+                        $add_settings[] = array($setting, $task);
                     }
                     // Then add all dependencies
                     foreach ($settings as &$setting) {
                         if ($setting->get_name() == 'filename') {
                             continue;
                         }
-                        $form->add_dependencies($setting);
+                        $dependencies[] = $setting;
                     }
                 }
             }
+            // Add all settings at once
+            $form->add_settings($add_settings);
+            // Add dependencies
+            foreach ($dependencies as $depsetting) {
+                $form->add_dependencies($setting);
+            }
             $this->stageform = $form;
         }
         // Return the form
@@ -226,6 +234,8 @@ class backup_ui_stage_schema extends backup_ui_stage {
             $tasks = $this->ui->get_tasks();
             $content = '';
             $courseheading = false;
+            $add_settings = array();
+            $dependencies = array();
             foreach ($tasks as $task) {
                 if (!($task instanceof backup_root_task)) {
                     if (!$courseheading) {
@@ -235,11 +245,11 @@ class backup_ui_stage_schema extends backup_ui_stage {
                     }
                     // First add each setting
                     foreach ($task->get_settings() as $setting) {
-                        $form->add_setting($setting, $task);
+                        $add_settings[] = array($setting, $task);
                     }
                     // The add all the dependencies
                     foreach ($task->get_settings() as $setting) {
-                        $form->add_dependencies($setting);
+                        $dependencies[] = $setting;
                     }
                 } else if ($this->ui->enforce_changed_dependencies()) {
                     // Only show these settings if dependencies changed them.
@@ -254,6 +264,10 @@ class backup_ui_stage_schema extends backup_ui_stage {
                     }
                 }
             }
+            $form->add_settings($add_settings);
+            foreach($dependencies as $depsetting) {
+              $form->add_dependencies($depsetting);
+            }
             $this->stageform = $form;
         }
         return $this->stageform;
index ad99260..652d580 100644 (file)
@@ -156,6 +156,38 @@ abstract class base_moodleform extends moodleform {
         $this->_form->addElement('html', html_writer::end_tag('div'));
         return true;
     }
+    /**
+     * Adds multiple backup_settings as elements to the form
+     * @param array $settingstasks Consists of array($setting, $task) elements
+     * @return bool
+     */
+    function add_settings(array $settingstasks) {
+        global $OUTPUT;
+
+        $defaults = array();
+        foreach ($settingstasks as $st) {
+            list($setting, $task) = $st;
+            // If the setting cant be changed or isn't visible then add it as a fixed setting.
+            if (!$setting->get_ui()->is_changeable() || $setting->get_visibility() != backup_setting::VISIBLE) {
+                $this->add_fixed_setting($setting, $task);
+                continue;
+            }
+
+            // First add the formatting for this setting
+            $this->add_html_formatting($setting);
+
+            // Then call the add method with the get_element_properties array
+            call_user_func_array(array($this->_form, 'addElement'), $setting->get_ui()->get_element_properties($task, $OUTPUT));
+            $defaults[$setting->get_ui_name()] = $setting->get_value();
+            if ($setting->has_help()) {
+                list($identifier, $component) = $setting->get_help();
+                $this->_form->addHelpButton($setting->get_ui_name(), $identifier, $component);
+            }
+            $this->_form->addElement('html', html_writer::end_tag('div'));
+        }
+        $this->_form->setDefaults($defaults);
+        return true;
+    }
     /**
      * Adds a heading to the form
      * @param string $name
@@ -317,4 +349,4 @@ abstract class base_moodleform extends moodleform {
             $this->definition_after_data();
         }
     }
-}
\ No newline at end of file
+}