Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 42 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_form_elements_xhtml_Treebox | |
0.00% |
0 / 42 |
|
0.00% |
0 / 5 |
156 | |
0.00% |
0 / 1 |
| feed | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getOptions | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| setValue | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| render | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
6 | |||
| getEvaluatedValue | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * |
| 23 | * Copyright (c) 2015 (original work) Open Assessment Technologies SA; |
| 24 | * |
| 25 | * @author Mikhail Kamarouski, <kamarouski@1pt.com> |
| 26 | * @package tao |
| 27 | */ |
| 28 | |
| 29 | use oat\tao\helpers\form\elements\xhtml\XhtmlRenderingTrait; |
| 30 | |
| 31 | /** |
| 32 | * Treebox widget |
| 33 | */ |
| 34 | class tao_helpers_form_elements_xhtml_Treebox extends tao_helpers_form_elements_Treebox |
| 35 | { |
| 36 | use XhtmlRenderingTrait; |
| 37 | |
| 38 | /** |
| 39 | * |
| 40 | * @var core_kernel_classes_Class |
| 41 | */ |
| 42 | protected $range; |
| 43 | |
| 44 | /** |
| 45 | * |
| 46 | * @return void |
| 47 | */ |
| 48 | public function feed() |
| 49 | { |
| 50 | $expression = "/^" . preg_quote($this->name, "/") . "(.)*[0-9]+$/"; |
| 51 | $this->setValues([]); |
| 52 | foreach ($_POST as $key => $value) { |
| 53 | if (preg_match($expression, $key)) { |
| 54 | $this->addValue(tao_helpers_Uri::decode($value)); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * |
| 61 | * @param string $format |
| 62 | * |
| 63 | * @return array |
| 64 | */ |
| 65 | public function getOptions($format = 'flat') |
| 66 | { |
| 67 | switch ($format) { |
| 68 | case 'structured': |
| 69 | $returnValue = parent::getOptions(); |
| 70 | break; |
| 71 | case 'flat': |
| 72 | default: |
| 73 | $returnValue = tao_helpers_form_GenerisFormFactory::extractTreeData(parent::getOptions()); |
| 74 | break; |
| 75 | } |
| 76 | |
| 77 | return $returnValue; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * |
| 82 | * @param string $value |
| 83 | * |
| 84 | * @return void |
| 85 | */ |
| 86 | public function setValue($value) |
| 87 | { |
| 88 | $this->addValue($value); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * |
| 93 | * @return string |
| 94 | */ |
| 95 | public function render() |
| 96 | { |
| 97 | $widgetTreeName = $this->name . '-TreeBox'; |
| 98 | $widgetValueName = $this->name . '-TreeValues'; |
| 99 | $returnValue = $this->renderLabel(); |
| 100 | |
| 101 | $returnValue .= "<div class='form-elt-container' style='min-height:50px; overflow-y:auto;'>"; |
| 102 | |
| 103 | $returnValue .= "<div id='{$widgetValueName}'>"; |
| 104 | $i = 0; |
| 105 | |
| 106 | foreach ($this->values as $value) { |
| 107 | $returnValue .= '<input type=\'hidden\' name=\'' . $this->name . '_' . $i++ . '\' value=\'' . $value |
| 108 | . '\' />'; |
| 109 | } |
| 110 | $returnValue .= "</div>"; |
| 111 | $returnValue .= "<div id='{$widgetTreeName}' class='tree-widget'></div>"; |
| 112 | |
| 113 | $returnValue .= "<script type=\"text/javascript\"> |
| 114 | $(function(){ |
| 115 | require(['require', 'jquery', 'generis.tree.select'], function(req, $) { |
| 116 | $(\"div[id='" . $widgetTreeName . '\']").tree({ |
| 117 | data: { |
| 118 | type : "json", |
| 119 | async: false, |
| 120 | opts : {static : '; |
| 121 | $returnValue .= json_encode($this->getOptions('structured')); |
| 122 | $returnValue .= '} |
| 123 | }, |
| 124 | callback:{ |
| 125 | onload: function(TREE_OBJ) { |
| 126 | checkedElements = ' . json_encode($this->values) . '; |
| 127 | var tree = $("#' . $widgetTreeName . '"); |
| 128 | $.each(checkedElements, function(i, elt){ |
| 129 | NODE = $("li[id=\'"+elt+"\']", tree); |
| 130 | if(NODE.length > 0){ |
| 131 | parent = TREE_OBJ.parent(NODE); |
| 132 | TREE_OBJ.open_branch(parent); |
| 133 | while(parent != -1){ |
| 134 | parent = TREE_OBJ.parent(parent); |
| 135 | TREE_OBJ.open_branch(parent); |
| 136 | } |
| 137 | $.tree.plugins.checkbox.check(NODE); |
| 138 | } |
| 139 | }); |
| 140 | }, |
| 141 | onchange: function(NODE, TREE_OBJ){ |
| 142 | var valueContainer = $("div[id=\'' . $widgetValueName . '\']"); |
| 143 | valueContainer.empty(); |
| 144 | $.each($.tree.plugins.checkbox.get_checked(TREE_OBJ), function(i, myNODE){ |
| 145 | valueContainer.append("<input type=\'hidden\' name=\'' . $this->name |
| 146 | . '_"+i+"\' value=\'"+$(myNODE).attr("id")+"\' />"); |
| 147 | }); |
| 148 | } |
| 149 | }, |
| 150 | types: { |
| 151 | "default" : { |
| 152 | renameable : false, |
| 153 | deletable : false, |
| 154 | creatable : false, |
| 155 | draggable : false |
| 156 | } |
| 157 | }, |
| 158 | ui: { theme_name : "checkbox" }, |
| 159 | plugins : { checkbox : { three_state : false} } |
| 160 | }); |
| 161 | }); |
| 162 | }); |
| 163 | </script>'; |
| 164 | $returnValue .= "</div><br />"; |
| 165 | |
| 166 | return (string) $returnValue; |
| 167 | |
| 168 | return $returnValue; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * |
| 173 | * @access public |
| 174 | * @return mixed |
| 175 | */ |
| 176 | public function getEvaluatedValue() |
| 177 | { |
| 178 | $values = array_map("tao_helpers_Uri::decode", $this->getValues()); |
| 179 | if (count($values) === 1) { |
| 180 | return $values[0]; |
| 181 | } else { |
| 182 | return $values; |
| 183 | } |
| 184 | } |
| 185 | } |