|
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 00045 class backup_optigroup_element extends backup_nested_element { 00046 00047 private $conditionparam; // Unprocessed param representing on path to look for value 00048 private $procconditionparam; // Processed base_element param to look for value 00049 private $conditionvalue; // Value to compare the the param value with 00050 00059 public function __construct($name, $final_elements = null, $conditionparam = null, $conditionvalue = null) { 00060 parent::__construct($name, null, $final_elements); 00061 $this->set_condition($conditionparam, $conditionvalue); 00062 } 00063 00064 // Public API starts here 00065 00069 public function set_condition($conditionparam, $conditionvalue) { 00070 // We only resolve the condition if the parent of the element (optigroup) already has parent 00071 // else, we'll resolve it once the optigroup parent is defined 00072 if ($this->get_parent() && $this->get_parent()->get_parent() && $conditionparam !== null) { 00073 $this->procconditionparam = $this->find_element($conditionparam); 00074 } 00075 $this->conditionparam = $conditionparam; 00076 $this->conditionvalue = $conditionvalue; 00077 } 00078 00079 public function get_condition_param() { 00080 return $this->conditionparam; 00081 } 00082 00083 public function get_condition_value() { 00084 return $this->conditionvalue; 00085 } 00086 00090 public function condition_matches() { 00091 $match = false; // By default no match 00092 $param = $this->procconditionparam; 00093 if ($param instanceof base_atom && $param->is_set()) { 00094 $match = ($param->get_value() == $this->conditionvalue); // blame $DB for not having === ! 00095 } else { 00096 $match = ($param == $this->conditionvalue); 00097 } 00098 return $match; 00099 } 00100 00106 public function get_level() { 00107 return $this->get_parent() == null ? 1 : $this->get_parent()->get_level(); 00108 } 00109 00117 public function process($processor) { 00118 if (!$processor instanceof base_processor) { // No correct processor, throw exception 00119 throw new base_element_struct_exception('incorrect_processor'); 00120 } 00121 00122 $iterator = $this->get_iterator($processor); // Get the iterator over backup-able data 00123 00124 $itcounter = 0; // To check that the iterator only has 1 ocurrence 00125 foreach ($iterator as $key => $values) { // Process each "ocurrrence" of the nested element (recordset or array) 00126 00127 // Fill the values of the attributes and final elements with the $values from the iterator 00128 $this->fill_values($values); 00129 00130 // Delegate the process of each final_element 00131 foreach ($this->get_final_elements() as $final_element) { 00132 $final_element->process($processor); 00133 } 00134 00135 // Everything processed, clean values before next iteration 00136 $this->clean_values(); 00137 00138 // Increment counters for this element 00139 $this->counter++; 00140 $itcounter++; 00141 00142 // optigroup_element, check we only have 1 element always 00143 if ($itcounter > 1) { 00144 throw new base_element_struct_exception('optigroup_element_only_one_ocurrence', $this->get_name()); 00145 } 00146 } 00147 // Close the iterator (DB recordset / array iterator) 00148 $iterator->close(); 00149 } 00150 00151 // Forbidden API starts here 00152 00156 public function add_add_optigroup($optigroup) { 00157 throw new base_element_struct_exception('optigroup_element_not_optigroup'); 00158 } 00159 00163 public function add_attributes($attributes) { 00164 throw new base_element_struct_exception('optigroup_element_not_attributes'); 00165 } 00166 00170 protected function get_new_attribute($name) { 00171 throw new base_element_struct_exception('optigroup_element_not_attributes'); 00172 } 00173 00174 // Protected API starts here 00175 00180 protected function get_new_final_element($name) { 00181 return new backup_final_element($name); 00182 } 00183 00188 protected function set_parent($element) { 00189 parent::set_parent($element); 00190 // Force condition param calculation 00191 $this->set_condition($this->conditionparam, $this->conditionvalue); 00192 } 00193 00194 }