ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPCDataTable.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2008 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 require_once("./Services/COPage/classes/class.ilPageContent.php");
25 require_once("./Services/COPage/classes/class.ilPCTable.php");
26 
38 class ilPCDataTable extends ilPCTable
39 {
40  var $dom;
41  var $tab_node;
42 
46  function init()
47  {
48  $this->setType("dtab");
49  }
50 
51  function setNode(&$a_node)
52  {
53  parent::setNode($a_node); // this is the PageContent node
54  $this->tab_node =& $a_node->first_child(); // this is the Table node
55  }
56 
57  function create(&$a_pg_obj, $a_hier_id, $a_pc_id = "")
58  {
59  $this->node = $this->createPageContentNode();
60  $a_pg_obj->insertContent($this, $a_hier_id, IL_INSERT_AFTER, $a_pc_id);
61  $this->tab_node =& $this->dom->create_element("Table");
62  $this->tab_node =& $this->node->append_child($this->tab_node);
63  $this->tab_node->set_attribute("Language", "");
64  $this->tab_node->set_attribute("DataTable", "y");
65  }
66 
70  function getCellNode($i, $j)
71  {
72  $xpc = xpath_new_context($this->dom);
73  $path = "//PageContent[@HierId='".$this->getHierId()."']".
74  "/Table/TableRow[$i+1]/TableData[$j+1]/PageContent[1]/Paragraph[1]";
75  $res =& xpath_eval($xpc, $path);
76 
77  if (is_object($res->nodeset[0]))
78  {
79  return $res->nodeset[0];
80  }
81  else // no node -> delete all childs and create paragraph
82  {
83  $xpc2 = xpath_new_context($this->dom);
84  $path2 = "//PageContent[@HierId='".$this->getHierId()."']".
85  "/Table/TableRow[".($i+1)."]/TableData[".($j+1)."]";
86  //$path2 = "//PageContent";
87 
88  $res2 =& xpath_eval($xpc2, $path2);
89 
90  $td_node = $res2->nodeset[0];
91 
92  if (is_object($td_node))
93  {
94  // delete children of paragraph node
95  $children = $td_node->child_nodes();
96  for($i=0; $i<count($children); $i++)
97  {
98  $td_node->remove_child($children[$i]);
99  }
100 
101  // create page content and paragraph node here.
102  $pc_node = $this->createPageContentNode(false);
103  $pc_node = $td_node->append_child($pc_node);
104  $par_node = $this->dom->create_element("Paragraph");
105  $par_node = $pc_node->append_child($par_node);
106  $par_node->set_attribute("Characteristic", "TableContent");
107  $par_node->set_attribute("Language",
108  $this->getLanguage());
109 
110  return $par_node;
111  }
112  }
113 
114  return "";
115  }
116 
120  function makeEmptyCell($td_node)
121  {
122  // delete children of paragraph node
123  $children = $td_node->child_nodes();
124  for($i=0; $i<count($children); $i++)
125  {
126  $td_node->remove_child($children[$i]);
127  }
128 
129  // create page content and paragraph node here.
130  $pc_node = $this->createPageContentNode(false);
131  $pc_node = $td_node->append_child($pc_node);
132  $par_node = $this->dom->create_element("Paragraph");
133  $par_node = $pc_node->append_child($par_node);
134  $par_node->set_attribute("Characteristic", "TableContent");
135  $par_node->set_attribute("Language",
136  $this->getLanguage());
137  }
138 
142  function getCellText($i, $j)
143  {
144  $cell_par = $this->getCellNode($i, $j);
145 
146  if (is_object($cell_par))
147  {
148  $content = "";
149  $childs = $cell_par->child_nodes();
150  for($i=0; $i<count($childs); $i++)
151  {
152  $content.= $this->dom->dump_node($childs[$i]);
153  }
154  return $content;
155  }
156  else
157  {
158  return "";
159  }
160  }
161 
165  function setData($a_data)
166  {
167  $ok = true;
168 //var_dump($a_data);
169  if (is_array($a_data))
170  {
171  foreach ($a_data as $i => $row)
172  {
173  if (is_array($row))
174  {
175  foreach ($row as $j => $cell)
176  {
177  $temp_dom = @domxml_open_mem('<?xml version="1.0" encoding="UTF-8"?><Paragraph>'.$cell.'</Paragraph>',
179 
180  $par_node = $this->getCellNode($i, $j);
181 //echo "<br>=".htmlentities($this->dom->dump_node($par_node))."=";
182 //echo "<br>-$i-$j-$cell-";
183  // remove all childs
184  if(empty($error) && is_object($par_node))
185  {
186  // delete children of paragraph node
187  $children = $par_node->child_nodes();
188  for($k=0; $k < count($children); $k++)
189  {
190  $par_node->remove_child($children[$k]);
191  }
192 
193  // copy new content children in paragraph node
194  $xpc = xpath_new_context($temp_dom);
195  $path = "//Paragraph";
196  $res =& xpath_eval($xpc, $path);
197 
198  if (count($res->nodeset) == 1)
199  {
200  $new_par_node =& $res->nodeset[0];
201  $new_childs = $new_par_node->child_nodes();
202  for ($l = 0; $l < count($new_childs); $l++)
203  {
204  $cloned_child = $new_childs[$l]->clone_node(true);
205  $par_node->append_child($cloned_child);
206 //echo "<br>=".htmlentities($this->dom->dump_node($cloned_child))."=";
207  }
208  }
209  }
210  else
211  {
212  if (!empty($error))
213  {
214  return $error;
215  }
216  }
217  }
218  }
219  }
220  }
221 
222  return true;
223  }
224 }
225 
226 ?>