|
Moodle
2.2.1
http://www.collinsharper.com
|
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 00030 abstract class base_optigroup extends base_nested_element { 00031 00033 private $multiple; 00034 00042 public function __construct($name, $elements = null, $multiple = false) { 00043 parent::__construct($name); 00044 $this->multiple = $multiple; 00045 if (!empty($elements)) { 00046 $this->add_children($elements); 00047 } 00048 } 00049 00050 // Public API starts here 00051 00057 public function get_level() { 00058 return $this->get_parent() == null ? 1 : $this->get_parent()->get_level(); 00059 } 00060 00061 public function to_string($showvalue = false) { 00062 $indent = str_repeat(' ', $this->get_level()); // Indent output based in level (4cc) 00063 $output = $indent . '!' . $this->get_name() . ' (level: ' . $this->get_level() . ')'; 00064 $children = $this->get_children(); 00065 if (!empty($children)) { 00066 foreach ($this->get_children() as $child) { 00067 $output .= PHP_EOL . $child->to_string($showvalue); 00068 } 00069 } 00070 return $output; 00071 } 00072 00073 // Forbidden API starts here 00074 00078 public function add_attributes($attributes) { 00079 throw new base_element_struct_exception('optigroup_not_attributes'); 00080 } 00081 00085 protected function get_new_attribute($name) { 00086 throw new base_element_struct_exception('optigroup_not_attributes'); 00087 } 00088 00092 public function add_final_elements($attributes) { 00093 throw new base_element_struct_exception('optigroup_not_final_elements'); 00094 } 00095 00099 protected function get_new_final_element($name) { 00100 throw new base_element_struct_exception('optigroup_not_final_elements'); 00101 } 00102 00103 // Protected API starts here 00104 00105 protected function add_children($elements) { 00106 if ($elements instanceof base_nested_element) { // Accept 1 element, object 00107 $elements = array($elements); 00108 } 00109 if (is_array($elements)) { 00110 foreach ($elements as $element) { 00111 $this->add_child($element); 00112 } 00113 } else { 00114 throw new base_optigroup_exception('optigroup_elements_incorrect'); 00115 } 00116 } 00117 00122 protected function set_parent($element) { 00123 parent::set_parent($element); 00124 // Force condition param calculation in all children 00125 foreach ($this->get_children() as $child) { 00126 $child->set_condition($child->get_condition_param(), $child->get_condition_value()); 00127 } 00128 } 00129 00134 protected function add_used($element) { 00135 $newused = array(); 00136 // Iterate over all the element useds, filling $newused and 00137 // observing the multiple setting 00138 foreach ($element->get_used() as $used) { 00139 if (!in_array($used, $this->get_used())) { // it's a new one, add to $newused array 00140 $newused[] = $used; 00141 $this->set_used(array_merge($this->get_used(), array($used))); // add to the optigroup used array 00142 } else { // it's an existing one, exception on multiple optigroups 00143 if ($this->multiple) { 00144 throw new base_optigroup_exception('multiple_optigroup_duplicate_element', $used); 00145 } 00146 } 00147 } 00148 // Finally, inform about newused to the next grand(parent/optigroupelement) 00149 if ($newused && $this->get_parent()) { 00150 $element->set_used($newused); // Only about the newused 00151 $grandparent = $this->get_grandoptigroupelement_or_grandparent(); 00152 $grandparent->check_and_set_used($element); 00153 } 00154 } 00155 00156 protected function is_multiple() { 00157 return $this->multiple; 00158 } 00159 } 00160 00167 class base_optigroup_exception extends base_atom_exception { 00168 00176 public function __construct($errorcode, $a = null, $debuginfo = null) { 00177 parent::__construct($errorcode, $a, $debuginfo); 00178 } 00179 }