• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/class.ilSQL2XML.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00024 
00031 class ilSQL2XML
00032 {
00039         var $domxml;
00040 
00047         var $obj_id;
00048 
00055         var $mapping;
00056 
00063         var $hash;
00064 
00071         var $ilias;
00072         
00079         function ilSQL2XML ($a_lm_id, $a_lo_id = "")
00080         {
00081                 global $ilias;
00082                 
00083                 $this->ilias =& $ilias;
00084                 $this->lm_tree = new ilTree ($a_lm_id,$a_lm_id);
00085 
00086                 if (!$a_lm_id)
00087                 {
00088                         $this->ilias->raiseError("No LearningModule ID given",$this->ilias->error_obj->FATAL);
00089                 }
00090         
00091                 $this->lm_id = $a_lm_id;
00092                 
00093                 if (!$a_lo_id)
00094                 {
00095                         // get chapter overview or get first LO in tree?
00096                         // get first LO:
00097                         $node_data = $this->lm_tree->getChilds($this->lm_id);
00098                         
00099                         if (!$node_data[0]["obj_id"])
00100                         {
00101                                 $this->ilias->raiseError("No LearningObjects found",$this->ilias->error_obj->FATAL);
00102                         }
00103 
00104                         $this->lo_id = $node_data[0]["obj_id"];
00105                         
00106                 }
00107                 else
00108                 {
00109                         $this->lo_id = $a_lo_id;
00110                 }
00111                 
00112                 //get parent lo_id
00113                 $this->lo_parent = $this->lm_tree->getParentId($this->lo_id);
00114                 
00115                 /*
00116                 if (empty($_GET["lo_parent"]))
00117                 {
00118                         $this->lo_parent = $this->lm_id;
00119                 }
00120                 else
00121                 {
00122                         $this->lo_parent = $_GET["lo_parent"];
00123                 }
00124                 */
00125                 
00126                 // get level
00127                 $this->level = $this->getAttributeValue($this->lo_id,"General","AggregationLevel");
00128         }
00129 
00139         function getLearningObject ()
00140         {
00141                 global $start;
00142 
00143                 $this->lo_struct = $this->getStructure($this->lo_id);
00144                 
00145                 // build path information for DTD location
00146                 if (strstr(php_uname(), "Windows"))
00147                 {
00148                         $path = "file://";
00149                 }
00150 
00151                 $path .= getcwd();
00152                 
00153                 // create the xml string (workaround for domxml_new_doc) ***
00154 /*
00155                 $xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>". // *** ISO-8859-1
00156                                          "<!DOCTYPE LearningObject SYSTEM \"".$path."/xml/ilias_lo.dtd\">";
00157                                          
00158                 return $xmlHeader.$this->lo_struct;             
00159 */
00160                 
00161                 // create the xml string (workaround for domxml_new_doc) ***
00162                 $xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>". // *** ISO-8859-1
00163                                          "<!DOCTYPE LearningObject SYSTEM \"".$path."/xml/ilias_lo.dtd\">".
00164                                          "<root />"; // dummy node
00165                 
00166                 // create a domxml document object
00167                 $this->doc = domxml_open_mem($xmlHeader); // *** Fehlerabfrage
00168                 
00169                 // delete dummy node 
00170                 $root = $this->doc->document_element();
00171                 $root->unlink_node();
00172 
00173                 // skip document node
00174                 reset($this->lo_struct);
00175                 $start = next($this->lo_struct);
00176                 
00177                 // create root node
00178                 $node = $this->doc->create_element($start["element"]);
00179 
00180                 $root = $this->doc->append_child($node);
00181                 
00182                 $this->hash[$start["node_id"]] = $root;
00183                 
00184                 // build XML document
00185                 $this->buildXML();
00186                         
00188                 // now get the Level-1-LOs
00189                 if (count($subnodes = $this->doc->get_elements_by_tagname("LO")) > 0)
00190                 {
00191                         foreach ($subnodes as $subnode)
00192                         {
00193                                 // fetching lo_id and remove placeholder node
00194                                 $attributes = $subnode->attributes();
00195                                 $lo_id = $attributes[0]->value;
00196 
00197                                 $level = $this->getAttributeValue($lo_id,"General","AggregationLevel");
00198                                 // get next LO
00199                                 //todo: first check if LO is of Level 1 !!!
00200 
00201                                 //$T6 = ilUtil::StopWatch();
00202 
00203                                 if ($level == "1")
00204                                 {
00205                                         $this->hash = array();
00206                                         $this->lo_struct = $this->getStructure($lo_id);
00207 
00208                                         // skip document node
00209                                         reset($this->lo_struct);
00210                                         $start = next($this->lo_struct);
00211 
00212                                         // create first node (LearningObject)
00213                                         $node = $this->doc->create_element($start["element"]);
00214 
00215                                         $parent = $subnode->parent_node();
00216                                         $subnode->unlink_node();
00217 
00218                                         $root = $parent->append_child($node);
00219 
00220                                         $this->hash[$start["node_id"]] = $root;
00221 
00222                                         // build next XML
00223                                         $this->buildXML();
00224 
00225                                         //echo ilUtil::StopWatch($T6)." get_sub LO<br/>";
00226                                 }
00227                                 else
00228                                 {
00229                                         //$obj_data = getObject($lo_id);
00230                                         $loObj =& $this->ilias->obj_factory->getInstanceByObjId($lo_id);
00231                                         $subnode->set_attribute("title", $loObj->getTitle());
00232                                         $subnode->set_attribute("level", $level);
00233                                         unset($loObj);
00234                                 }
00235 
00236                                 //if (($this->level == "3") and ($level == "2" or $level == "3"))
00237 
00238                         }
00239                 }
00240 //*/
00241 
00242                 return $this->doc->dump_mem(true);
00243         }
00244 
00245         function buildXML ()    
00246         {
00247         global $start;
00248 
00249                 //$T4 = ilUtil::StopWatch();
00250 
00251                 foreach ($this->lo_struct as $key => $node_data)
00252                 {
00253                         // exclude root node
00254                         if ($key > $start["node_id"])
00255                         {
00256                                 $insert = false;
00257                                 switch ($node_data["node_type_id"])
00258                                 {
00259                                         case 1:
00260                                                 $node = $this->doc->create_element($node_data["element"]);
00261         
00262                                                 // set attributes
00263                                                 if (is_array($node_data["attr_list"]))
00264                                                 {
00265                                                         foreach ($node_data["attr_list"] as $attr => $value)
00266                                                         {
00267                                                                 $node->set_attribute($attr, $value);
00268                                                         }
00269                                                 }
00270                                                 
00271                                                 $insert = true;
00272                                                 break;
00273 
00274                                         case 3:
00275                                                 $node = $this->doc->create_text_node($node_data["textnode"]);
00276 
00277                                                 $insert = true;
00278                                                 break;
00279                                                 
00280                                 }
00281 
00282                                 if ($insert)
00283                                 {
00284                                         //get parent node
00285                                         $parent = $this->hash[$node_data["parent_node_id"]];
00286                                 
00287                                         //build node
00288                                         $node = $parent->append_child($node);
00289                                         $this->hash[$this->lo_struct[$key]["node_id"]] = $node;
00290                                 }
00291                         }
00292                 }
00293 
00294                 //echo ilUtil::StopWatch($T4)." build_XML<br/>";
00295         }
00296         
00297 
00298         function getStructure($a_lo_id)
00299         {
00300                 //$T1 = ilUtil::StopWatch();
00301                 
00302                 /*
00303                 $q = "SELECT lo.node_id, lo.node_type_id, lo.lo_id, lo.parent_node_id, lo.struct, tx.textnode, el.element ".
00304                          "FROM lo_tree AS lo ".
00305                          "LEFT OUTER JOIN lo_element_idx AS e_idx ON lo.node_id = e_idx.node_id ".
00306                          "LEFT OUTER JOIN lo_element_name AS el ON e_idx.element_id = el.element_id ".
00307                          "LEFT OUTER JOIN lo_text AS tx ON lo.node_id = tx.node_id ".
00308                          "WHERE lo_id='".$a_lo_id."' ".
00309                          "ORDER BY lft ASC";
00310                 */
00311                 
00312                 // Variant: get ALL data including attributes. Very fast! Drawback are multiple rows for each nodes 
00314                 $q = "SELECT lo.node_id, lo.node_type_id, lo.lo_id, lo.parent_node_id, lo.struct, tx.textnode, el.element, a_name.attribute, a_value.value ".
00315                          "FROM lo_tree AS lo ".
00316                          "LEFT OUTER JOIN lo_element_idx AS e_idx ON lo.node_id = e_idx.node_id ".
00317                          "LEFT OUTER JOIN lo_element_name AS el ON e_idx.element_id = el.element_id ".
00318                          "LEFT OUTER JOIN lo_text AS tx ON lo.node_id = tx.node_id ".
00319                          "LEFT OUTER JOIN lo_attribute_idx AS a_idx ON lo.node_id = a_idx.node_id ".
00320                          "LEFT JOIN lo_attribute_name AS a_name ON a_idx.attribute_id=a_name.attribute_id ".
00321                          "LEFT JOIN lo_attribute_value AS a_value ON a_idx.value_id=a_value.value_id ".
00322                          "WHERE lo_id='".$a_lo_id."' ".
00323                          "ORDER BY lft ASC";
00324                          
00325                          //echo $q;exit;
00326 //*/
00327                 
00328                 // 2. variant: I think this is the fastest but you need mysql 4.x in order to use UNION statement
00329 /*
00330                 $q = "SELECT rgt FROM lo_tree ".
00331                          "WHERE lo_id = '".$a_lo_id."' ".
00332                          "AND lft = 1";
00333                 
00334                 $res = $this->ilias->db->query($q);
00335                 
00336                 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
00337                 {
00338                         $rgt_max = $row["rgt"];
00339                 }
00340 
00341                 $q = "(SELECT n.lft AS seq_no_1, 4 AS seq_no_2, 0 AS seq_no_3, 0 AS seq_no_4, x.node_id AS seq_no_5, x.textnode AS parsed_text ".
00342                          "FROM lo_tree n, lo_text x ".
00343                          "WHERE n.lo_id = '".$a_lo_id."' ".
00344                          "AND n.lft >= 1 ".
00345                          "AND n.lft < '".$rgt_max."' ".
00346                          "AND n.node_id = x.node_id) ".
00347                          "UNION ".
00348                          "(SELECT n.lft, 1, 0, 0, 0, t.lft_delimiter ".
00349                          "FROM lo_tree n, lo_node_type t ".
00350                          "WHERE n.lo_id = '".$a_lo_id."' ".
00351                          "AND n.node_type_id = t.node_type_id ".
00352                          "AND n.lft >= 1 ".
00353                          "AND n.lft < '".$rgt_max."' ".
00354                          "AND t.lft_delimiter > '') ".
00355                          "UNION ".
00356                          "(SELECT n.lft, 2, 0, 0, 0, e.element ".
00357                          "FROM lo_element_name e, lo_element_idx e_idx, lo_tree n ".
00358                          "WHERE n.lo_id = '".$a_lo_id."' ".
00359                          "AND n.node_id = e_idx.node_id AND ".
00360                          "e.element_id = e_idx.element_id) ".
00361                          "UNION ".
00362                          "(SELECT n.lft, 3, a.attribute_id, 1, 0, CONCAT(' ', a.attribute, '=\"' ) ".
00363                          "FROM lo_attribute_name a, lo_attribute_idx a_idx, lo_tree n ".
00364                          "WHERE n.lo_id = '".$a_lo_id."' ".
00365                          "AND n.lft >= 1 ".
00366                          "AND n.lft < '".$rgt_max."' ".
00367                          "AND n.node_id = a_idx.node_id ".
00368                          "AND a.attribute_id = a_idx.attribute_id) ".
00369                          "UNION ".
00370                          "(SELECT n.lft, 3, a_idx.attribute_id, 2, n.node_id, CONCAT( a.value, '\"' ) ".
00371                          "FROM lo_attribute_value a, lo_attribute_idx a_idx, lo_tree n ".
00372                          "WHERE n.lo_id = '".$a_lo_id."' ".
00373                          "AND n.lft >= 1 ".
00374                          "AND n.lft < '".$rgt_max."' ".
00375                          "AND n.node_id = a_idx.node_id ".
00376                          "AND a.value_id = a_idx.value_id) ".
00377                          "UNION ".
00378                          "(SELECT n.lft, 9, 0, 0, 0, t.rgt_delimiter ".
00379                          "FROM lo_tree n, lo_node_type t ".
00380                          "WHERE n.lo_id = '".$a_lo_id."' ".
00381                          "AND n.node_type_id = t.node_type_id ".
00382                          "AND t.rgt_delimiter > '' ".
00383                          "AND n.lft >= 1 ".
00384                          "AND n.lft < '".$rgt_max."') ".
00385                          "UNION ".
00386                          "(SELECT n.rgt, 10, 0, 0, 0, CONCAT( t.lft_delimiter, '/' ) ".
00387                          "FROM lo_tree n, lo_node_type t ".
00388                          "WHERE n.lo_id = '".$a_lo_id."' ".
00389                          "AND n.node_type_id = t.node_type_id ".
00390                          "AND n.node_type_id = 1 ".
00391                          "AND t.lft_delimiter > '' ".
00392                          "AND n.lft >= 1 ".
00393                          "AND n.lft < '".$rgt_max."') ".
00394                          "UNION ".
00395                          "(SELECT n.rgt, 10, 0, 3, 0, e.element ".
00396                          "FROM lo_element_name e, lo_element_idx e_idx, lo_tree n ".
00397                          "WHERE n.lo_id = '".$a_lo_id."' ".
00398                          "AND n.node_id = e_idx.node_id ".
00399                          "AND e.element_id = e_idx.element_id) ".
00400                          "UNION ".
00401                          "(SELECT n.rgt, 10, 0, 4, 0, t.rgt_delimiter FROM lo_tree n, lo_node_type t ".
00402                          "WHERE n.lo_id = '".$a_lo_id."' ".
00403                          "AND n.node_type_id = t.node_type_id ".
00404                          "AND t.rgt_delimiter > '' ".
00405                          "AND n.lft >= 1 ".
00406                          "AND n.lft < '".$rgt_max."') ".
00407                          "ORDER BY seq_no_1, seq_no_2, seq_no_3, seq_no_4, seq_no_5";
00408                 
00409                 
00410                 //echo $q;exit;
00411 
00412                 $res = $this->ilias->db->query($q);
00413                 
00414                 if ($res->numRows() == 0)
00415                 {
00416                         $this->ilias->raiseError("no LearningObject ID given",$this->ilias->error_obj->FATAL);
00417                 }
00418 
00419                 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
00420                 {
00421                         $data[] = $row["parsed_text"];
00422                 }
00423                 
00424                 echo ilUtil::StopWatch($T1)." get_structure<br/>";
00425                 
00426                 return implode($data);
00427 */
00428 
00429                 $res = $this->ilias->db->query($q);
00430                 
00431                 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
00432                 {
00433                         $data[$row["node_id"]] = $row;
00434 
00435                         if ($row["struct"] & 1)
00436                         {
00437                                 $tmp[$row["node_id"]][] = array ($row["attribute"] => $row["value"]);
00438                         }
00439                 }
00440 
00441 
00442                 foreach ($tmp as $node_id => $node)
00443                 {
00444                         $attr_list = array();
00445                         
00446                         foreach ($node as $attr)
00447                         {
00448                                 //var_dump($attr);
00449                                 $attr_list[key($attr)] = $attr[key($attr)];
00450                         }
00451 
00452                         $data[$node_id]["attr_list"] = $attr_list;
00453                 }
00454 
00455                 // remove document node
00456                 //array_shift($data);
00457                 
00458                 //echo ilUtil::StopWatch($T1)." get_structure<br/>";
00459                 return $data;
00460         }
00461         
00471         function getTree ($a_lo_id)
00472         {
00473                 return $tree;
00474         }
00475 
00484         function getNode ($a_node_id)
00485         {
00486                 return $node;
00487         }
00488         
00496         function getElementName ($a_node_id)
00497         {
00498                 $q = "SELECT leaf_text FROM lo_element_name_leaf ".
00499                          "WHERE node_id='".$a_node_id."' LIMIT 1";
00500                 //$res = $this->ilias->db->query($q);
00501                 $res = $this->ilias->db->getRow($q, DB_FETCHMODE_ASSOC);
00502 
00503                 return $res["leaf_text"];
00504         }
00505         
00506         function getElementText ($a_node_id)
00507         {
00508                 $q = "SELECT leaf_text FROM lo_text_leaf ".
00509                          "WHERE node_id='".$a_node_id."' LIMIT 1";
00510                 //$res = $this->ilias->db->query($q);
00511                 $res = $this->ilias->db->getRow($q, DB_FETCHMODE_ASSOC);
00512 
00513                 return $res["leaf_text"];
00514         }
00523         function getAttributes ($a_node_id)
00524         {
00525                 $q = "SELECT a_name.attribute,a_value.value ".
00526                          "FROM lo_attribute_idx AS a_idx ".
00527                          "LEFT JOIN lo_attribute_name AS a_name ON a_idx.attribute_id=a_name.attribute_id ".
00528                          "LEFT JOIN lo_attribute_value AS a_value ON a_idx.value_id=a_value.value_id ".
00529                          "WHERE a_idx.node_id = '".$a_node_id."'";
00530                          
00531                  //echo $q;exit;
00532 
00533                 $res = $this->ilias->db->query($q);
00534                 
00535                 if ($res->numRows())
00536                 {
00537                         while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
00538                         {
00539                                 $data[$row["attribute"]] = $row["value"];
00540                         }
00541 
00542                         return $data;
00543                 }
00544 
00545                 return false;
00546         }
00547 
00548         
00557         function getAttributeValue ($a_lo_id,$a_element,$a_attribute)
00558         {
00559                 //$T3 = ilUtil::StopWatch();            
00560 
00561                 $q = "SELECT a_value.value ".
00562                          "FROM lo_tree AS lo ".
00563                          "LEFT JOIN lo_element_idx AS e_idx ON lo.node_id = e_idx.node_id ".
00564                          "LEFT JOIN lo_element_name AS el ON e_idx.element_id = el.element_id ".
00565                          "LEFT JOIN lo_attribute_idx AS a_idx ON lo.node_id = a_idx.node_id ".
00566                          "LEFT JOIN lo_attribute_name AS a_name ON a_idx.attribute_id=a_name.attribute_id ".
00567                          "LEFT JOIN lo_attribute_value AS a_value ON a_idx.value_id=a_value.value_id ".
00568                          "WHERE lo_id = '".$a_lo_id."' ".
00569                          "AND lo.struct > 0 ". // <-- need a bitwise AND against 1
00570                          "AND el.element = '".$a_element."' ".
00571                          "AND a_name.attribute = '".$a_attribute."'";
00572                          
00573                 //echo $q;exit;
00574 
00575                 $res = $this->ilias->db->query($q);
00576                 $row = $res->fetchRow();
00577                 //echo ilUtil::StopWatch($T3)." getAttributeValue<br/>";
00578         
00579                 return $row[0];
00580         }
00581         
00582         function setNavigation()
00583         {
00584                 // chapter up
00585         
00586                 // todo: i need the parent parent at this place!
00587                 if ($this->lo_parent != $this->lm_id)
00588                 {
00589                         $up["child"] = $this->lo_parent;
00590                         $up["parent"] = "missing";
00591                 }
00592                 
00593                 // previous & next page
00594                 $node_data = $this->lm_tree->getChilds($this->lo_parent);
00595                 
00596                 foreach ($node_data as $key => $node)
00597                 {
00598                         if ($this->lo_id == $node["child"])
00599                         {
00600                                 if ($key > 0)
00601                                 {
00602                                         $prev["child"] = $node_data[$key-1]["child"];
00603                                         $prev["parent"] = $node_data[$key-1]["parent"];
00604                                 }
00605                                 
00606                                 if (count($node_data) > $key + 1)
00607                                 {
00608                                         $next["child"] = $node_data[$key+1]["child"];
00609                                         $next["parent"] = $node_data[$key+1]["parent"];
00610                                 }
00611                                 else
00612                                 {
00613                                         // look if there is a subchapter
00614                                         $subnode_data = $this->lm_tree->getChilds($this->lo_id);
00615                                         
00616                                         if (count($subnode_data) > 0)
00617                                         {
00618                                                 // fetch first child
00619                                                 $next["child"] = $subnode_data[0]["child"];
00620                                                 $next["parent"] = $subnode_data[0]["parent"];
00621                                         }
00622                                 }
00623                                 
00624                                 break;
00625                         }
00626                 }
00627                 
00628                 
00629                 $navbar = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
00630                 
00631                 if ($prev)
00632                 {
00633                         $navbar .= "<td>".$this->img_url($prev["child"],$prev["parent"],$this->img_link("arr_left.gif"))."</td>";
00634                 }
00635                 
00636                 if ($up)
00637                 {
00638                         $navbar .= "<td>".$this->img_url($up["child"],$up["parent"],$this->img_link("arr_up.gif"))."</td>";
00639                 }
00640                 
00641                 if ($next)
00642                 {
00643                         $navbar .= "<td>".$this->img_url($next["child"],$next["parent"],$this->img_link("arr_right.gif"))."</td>";
00644                 }
00645                 
00646                 $navbar .= "</tr></table>";
00647                 
00648                 return $navbar;
00649         }
00650         
00651         function img_link($a_img)
00652         {
00653                 return "<img src=\"./images/navigation/".$a_img."\" border=\"0\"/>";
00654         }
00655         
00656         function img_url($a_id, $a_parent, $a_img)
00657         {
00658                 return "<a href=\"./lo_view.php?type=lo&obj_id=".$_GET["obj_id"]."&lm_id=".$_GET["lm_id"]."&lo_id=".$a_id."&lo_parent=".$a_parent."\">".$a_img."</a>";
00659         }       
00660         
00670         function getChildNodes ($a_node_id)
00671         {
00672                 return $nodes;
00673         
00674                 return false;
00675         }
00676 
00686         function getParentNode ($a_node_id)
00687         {
00688                 return $node;
00689         
00690                 return false;
00691         }
00692         
00693         function getFirstChild ($a_node_id)
00694         {
00695                 return $node;
00696         
00697                 return false;
00698         }
00699 
00700         function getprevSibling ($a_node_id)
00701         {
00702                 return $node;
00703         
00704                 return false;
00705         }
00706 
00707         function getNextSibling ($a_node_id)
00708         {
00709                 return $node;
00710         
00711                 return false;
00712         }
00713 } // END class.SQL2XML
00714 ?>
00715 

Generated on Fri Dec 13 2013 09:06:35 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1