Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/ui/backup_ui_setting.class.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00034 class base_setting_ui {
00038     const NAME_PREFIX = 'setting_';
00043     protected $name;
00048     protected $label;
00053     protected $attributes = array();
00058     protected $type;
00063     protected $icon = false;
00068     protected $setting;
00073     public function __construct(base_setting $setting) {
00074         $this->setting = $setting;
00075     }
00076 
00080     public function destroy() {
00081         // No need to destroy anything recursively here, direct reset
00082         $this->setting = null;
00083     }
00084 
00089     public function get_name() {
00090         return self::NAME_PREFIX.$this->name;
00091     }
00096     public function get_label() {
00097         return $this->label;
00098     }
00103     public function get_type() {
00104         return $this->type;
00105     }
00110     public function get_attributes() {
00111         return $this->attributes;
00112     }
00117     public function get_value() {
00118         return $this->setting->get_value();
00119     }
00124     public function get_static_value() {
00125         return $this->setting->get_value();
00126     }
00131     public function set_label($label) {
00132         if (empty($label) || $label !== clean_param($label, PARAM_TEXT)) {
00133             throw new base_setting_ui_exception('setting_invalid_ui_label');
00134         }
00135         $this->label = $label;
00136     }
00140     public function disable() {
00141        $this->attributes['disabled'] = 'disabled';
00142     }
00143 
00149     public function set_icon(pix_icon $icon) {
00150         $this->icon = $icon;
00151     }
00152 
00158     public function get_icon() {
00159         if (!empty($this->icon)) {
00160             return $this->icon;
00161         }
00162         return false;
00163     }
00164 }
00165 
00172 abstract class backup_setting_ui extends base_setting_ui {
00177     protected $options = array();
00178 
00187     public function __construct(backup_setting $setting, $label = null, array $attributes = null, array $options = null) {
00188         parent::__construct($setting);
00189         // Improve the inputs name by appending the level to the name
00190         switch ($setting->get_level()) {
00191             case backup_setting::ROOT_LEVEL :
00192                 $this->name = 'root_'.$setting->get_name();
00193                 break;
00194             case backup_setting::COURSE_LEVEL :
00195                 $this->name = 'course_'.$setting->get_name();
00196                 break;
00197             case backup_setting::SECTION_LEVEL :
00198                 $this->name = 'section_'.$setting->get_name();
00199                 break;
00200             case backup_setting::ACTIVITY_LEVEL :
00201                 $this->name = 'activity_'.$setting->get_name();
00202                 break;
00203         }
00204         $this->label = $label;
00205         if (is_array($attributes)) {
00206             $this->attributes = $attributes;
00207         }
00208         if (is_array($options)) {
00209             $this->options = $options;
00210         }
00211     }
00225     final public static function make(backup_setting $setting, $type, $label, array $attributes = null, array $options=null) {
00226         // Base the decision we make on the type that was sent
00227         switch ($type) {
00228             case backup_setting::UI_HTML_CHECKBOX :
00229                 return new backup_setting_ui_checkbox($setting, $label, null, (array)$attributes, (array)$options);
00230             case backup_setting::UI_HTML_DROPDOWN :
00231                 return new backup_setting_ui_select($setting, $label, null, (array)$attributes, (array)$options);
00232             case backup_setting::UI_HTML_RADIOBUTTON :
00233                 return new backup_setting_ui_radio($setting, $label, null, null, (array)$attributes, (array)$options);
00234             case backup_setting::UI_HTML_TEXTFIELD :
00235                 return new backup_setting_ui_text($setting, $label, $attributes, $options);
00236             default:
00237                 throw new backup_setting_ui_exception('setting_invalid_ui_type');
00238         }
00239     }
00244     abstract public function get_element_properties(base_task $task=null, renderer_base $output=null);
00250     public function apply_options(array $properties) {
00251         if (!empty($this->options['size'])) {
00252             $properties['attributes']['size'] = $this->options['size'];
00253         }
00254         return $properties;
00255     }
00262     public function get_label(base_task $task=null) {
00263         // If a task has been provided and the label is not already set meaniningfully
00264         // we will attempt to improve it.
00265         if (!is_null($task) && $this->label == $this->setting->get_name() && strpos($this->setting->get_name(), '_include')!==false) {
00266             if ($this->setting->get_level() == backup_setting::SECTION_LEVEL) {
00267                 $this->label = get_string('includesection', 'backup', $task->get_name());
00268             } else if ($this->setting->get_level() == backup_setting::ACTIVITY_LEVEL) {
00269                 $this->label = $task->get_name();
00270             }
00271         }
00272         return $this->label;
00273     }
00286     public function is_changeable() {
00287         if ($this->setting->get_status() === backup_setting::NOT_LOCKED) {
00288             // Its not locked so its chanegable
00289             return true;
00290         } else if ($this->setting->get_status() !== backup_setting::LOCKED_BY_HIERARCHY) {
00291             // Its not changeable because its locked by permission or config
00292             return false;
00293         } else if ($this->setting->has_dependencies_on_settings()) {
00294             foreach ($this->setting->get_settings_depended_on() as $dependency) {
00295                 if ($dependency->is_locked() && $dependency->get_setting()->get_level() !== $this->setting->get_level()) {
00296                     // Its not changeable because one or more dependancies arn't
00297                     // changeable.
00298                    return false;
00299                 }
00300             }
00301             // Its changeable because all dependencies are changeable.
00302             return true;
00303         }
00304         // We should never get here but if we do return false to be safe.
00305         // The setting would need to be locked by hierarchy and not have any deps
00306         return false;
00307     }
00308 
00309 }
00310 
00317 class backup_setting_ui_text extends backup_setting_ui {
00321     protected $type = backup_setting::UI_HTML_TEXTFIELD;
00327     public function get_element_properties(base_task $task=null, renderer_base $output=null) {
00328         // name, label, text, attributes
00329         $icon = $this->get_icon();
00330         $label = $this->get_label($task);
00331         if (!empty($icon)) {
00332             $label .= '&nbsp;'.$output->render($icon);
00333         }
00334         // name, label, attributes
00335         return $this->apply_options(array('element'=>'text','name'=>self::NAME_PREFIX.$this->name, 'label'=>$label, 'attributes'=>$this->attributes));
00336     }
00337 
00338 }
00339 
00346 class backup_setting_ui_checkbox extends backup_setting_ui {
00350     protected $type = backup_setting::UI_HTML_CHECKBOX;
00354     protected $changeable = true;
00359     protected $text;
00368     public function __construct(backup_setting $setting, $label = null, $text=null, array $attributes = array(), array $options = array()) {
00369         parent::__construct($setting, $label, $attributes, $options);
00370         $this->text = $text;
00371     }
00377     public function get_element_properties(base_task $task=null, renderer_base $output=null) {
00378         // name, label, text, attributes
00379 
00380         $icon = $this->get_icon();
00381         $label = $this->get_label($task);
00382         if (!empty($icon)) {
00383             $label .= '&nbsp;'.$output->render($icon);
00384         }
00385         return $this->apply_options(array('element'=>'checkbox','name'=>self::NAME_PREFIX.$this->name, 'label'=>$label, 'text'=>$this->text, 'attributes'=>$this->attributes));
00386     }
00391     public function set_text($text) {
00392         $this->text = $text;
00393     }
00399     public function get_static_value() {
00400         global $OUTPUT;
00401         // Checkboxes are always yes or no
00402         if ($this->get_value()) {
00403             return $OUTPUT->pix_icon('i/tick_green_big', get_string('yes'));
00404         } else {
00405             return $OUTPUT->pix_icon('i/cross_red_big', get_string('no'));
00406         }
00407     }
00408 
00413     public function is_changeable() {
00414         if ($this->changeable===false) {
00415             return false;
00416         } else {
00417             return parent::is_changeable();
00418         }
00419     }
00420 
00426     public function set_changeable($newvalue) {
00427         $this->changeable = ($newvalue);
00428     }
00429 }
00430 
00437 class backup_setting_ui_radio extends backup_setting_ui {
00441     protected $type = backup_setting::UI_HTML_RADIOBUTTON;
00446     protected $text;
00451     protected $value;
00461     public function __construct(backup_setting $setting, $label = null, $text=null, $value=null, array $attributes = array(), array $options = array()) {
00462         parent::__construct($setting, $label, $attributes, $options);
00463         $this->text = $text;
00464         $this->value = (string)$value;
00465     }
00471     public function get_element_properties(base_task $task=null, renderer_base $output=null) {
00472         // name, label, text, attributes
00473         $icon = $this->get_icon();
00474         $label = $this->get_label($task);
00475         if (!empty($icon)) {
00476             $label .= '&nbsp;'.$output->render($icon);
00477         }
00478         // name, label, text, value, attributes
00479         return $this->apply_options(array('element'=>'radio','name'=>self::NAME_PREFIX.$this->name, 'label'=>$label, 'text'=>$this->text, 'value'=>$this->value, 'attributes'=>$this->attributes));
00480     }
00485     public function set_text($text) {
00486         $this->text = $text;
00487     }
00492     public function set_value($value) {
00493         $this->value = (string)$value;
00494     }
00498     public function get_static_value() {
00499         return $this->value;
00500     }
00501 }
00502 
00509 class backup_setting_ui_select extends backup_setting_ui {
00513     protected $type = backup_setting::UI_HTML_DROPDOWN;
00518     protected $values;
00527     public function __construct(backup_setting $setting, $label = null, $values=null, array $attributes = array(), array $options = array()) {
00528         parent::__construct($setting, $label, $attributes, $options);
00529         $this->values = $values;
00530     }
00536     public function get_element_properties(base_task $task = null, renderer_base $output=null) {
00537         // name, label, text, attributes
00538         $icon = $this->get_icon();
00539         $label = $this->get_label($task);
00540         if (!empty($icon)) {
00541             $label .= '&nbsp;'.$output->render($icon);
00542         }
00543         // name, label, options, attributes
00544         return $this->apply_options(array('element'=>'select','name'=>self::NAME_PREFIX.$this->name, 'label'=>$label, 'options'=>$this->values, 'attributes'=>$this->attributes));
00545     }
00550     public function set_values(array $values) {
00551         $this->values = $values;
00552     }
00557     public function get_static_value() {
00558         return $this->values[$this->get_value()];
00559     }
00565     public function is_changeable() {
00566         if (count($this->values) == 1) {
00567             return false;
00568         } else {
00569             return parent::is_changeable();
00570         }
00571     }
00572 }
00573 
00574 class backup_setting_ui_dateselector extends backup_setting_ui_text {
00575     public function get_element_properties(base_task $task = null, renderer_base $output=null) {
00576         if (!array_key_exists('optional', $this->attributes)) {
00577             $this->attributes['optional'] = false;
00578         }
00579         $properties = parent::get_element_properties($task, $output);
00580         $properties['element'] = 'date_selector';
00581         return $properties;
00582     }
00583     public function get_static_value() {
00584         $value = $this->get_value();
00585         if (!empty($value)) {
00586             return userdate($value);
00587         }
00588         return parent::get_static_value();
00589     }
00590 }
00591 
00592 class base_setting_ui_exception extends base_setting_exception {}
00593 class backup_setting_ui_exception extends base_setting_ui_exception {};
 All Data Structures Namespaces Files Functions Variables Enumerations