ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilSCORM2004SeqTemplate.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("./Modules/Scorm2004/classes/seq_editor/class.ilSCORM2004Item.php");
25 require_once("./Modules/Scorm2004/classes/class.ilSCORM2004Chapter.php");
26 require_once("./Modules/Scorm2004/classes/class.ilSCORM2004SeqChapter.php");
27 require_once("./Modules/Scorm2004/classes/class.ilSCORM2004Sco.php");
28 
40 {
41 
42  const SEQ_TEMPLATE_DIR = './Modules/Scorm2004/templates/editor/seq_templates';
43  const SEQ_TEMPLATE_XSL = './Modules/Scorm2004/templates/editor/seq_templates/xsl/normalize_seqtemplate.xsl';
44  const SEQ_TEMPLATE_XSD = './Modules/Scorm2004/templates/editor/seq_templates/xsd/seq_template.xsd';
45 
46 
47  private $template;
48  private $template_file;
50  private $tree_node;
51  private $diagnostic = array ();
52  private $parentchapter = true;
53  private $importId;
54 
55  //db fields
56  private $id = null;
57  private $seqNodeId = null;
58  private $sahs_sc13_treeId = null;
59  private $importid;
60 
61  public function ilSCORM2004SeqTemplate($a_identifier)
62  {
63 
65  $this->setNodeName("seqtemplate");
66 
67  if ($a_identifier==null) {return;}
68  $t_file = self::getFileNameForIdentifier($a_identifier);
69 
70  $this->template = new DOMDocument;
71  $this->template->async = false;
72 
73 
74  //look for template in lang_dir, fallback to en
75  $test = self::SEQ_TEMPLATE_DIR."/".$_SESSION["lang"]."/".$t_file;
76  if (file_exists($test))
77  {
78  $this->template_file = $test;
79  } else {
80  $this->template_file = self::SEQ_TEMPLATE_DIR."/en/".$t_file;
81  }
82  if (!@$this->template->load($this->template_file))
83  {
84  $this->diagnostic[] = 'Template not wellformed';
85  $test = $this->template->saveXML();
86  return false;
87  } else {
88  return true;
89  }
90  }
91 
92 
99  public function getMetadataProperties()
100  {
101  $array_metad = array();
102  $metadata = $this->template->getElementsByTagName("metadata");
103  $nodes = $metadata->item(0)->childNodes;
104  for ($i = 0; $i < $nodes->length; $i++ ) {
105  $curNode = $nodes->item($i);
106  $array_metad[$curNode->localName] = $curNode->nodeValue;
107  }
108  return $array_metad;
109  }
110 
111 
112  public static function availableTemplates()
113  {
114  global $ilDB;
115  $arr_templates = array();
116  $query = "SELECT * FROM sahs_sc13_seq_templts ORDER BY identifier";
117  $result = $ilDB->query($query);
118  while($row = $ilDB->fetchAssoc($result))
119  {
120  if($row['identifier']!="pretestpost") { //temporarily deactivated
121  array_push($arr_templates,new ilScorm2004SeqTemplate($row['identifier']));
122  }
123  }
124  return $arr_templates;
125  }
126 
127 
134  public function getIdentifier()
135  {
136  $seqtemplate = $this->template->getElementsByTagName("seqTemplate");
137  return $seqtemplate->item(0)->getAttribute("identifier");;
138  }
139 
140 
141  function insertTemplateForObjectAtParent($a_object,$a_parent,$a_target)
142  {
143  $this->importId = uniqid();
144  return $this->importTemplate($a_target,$a_object,$a_parent, $this->template->getElementsByTagName("item")->item(0));
145  }
146 
147 
148 
155  public function importTemplate($a_target,$a_object,$a_parent,$node) {
156  global $ilDB;
157 
158  switch($node->getAttribute('type'))
159  {
160  case "lesson":
161  if ($this->parentchapter==true)
162  {
163  $chap = new ilSCORM2004SeqChapter($a_object);
164  } else {
165  $chap = new ilSCORM2004Chapter($a_object);
166  }
167  $chap->setTitle($node->getElementsByTagName("title")->item(0)->nodeValue);
168  $chap->setSLMId($a_object->getId());
169  $chap->create();
170  $this->sahs_sc13_treeId = $chap->getId();
171  ilSCORM2004Node::putInTree($chap, $a_parent,$a_target);
172  $a_parent = $this->sahs_sc13_treeId;
173  if ($this->parentchapter==true)
174  {
175  $ilDB->manipulate("INSERT INTO sahs_sc13_seq_assign (identifier, sahs_sc13_tree_node_id) VALUES ".
176  "(".$ilDB->quote($this->getIdentifier(), "text").",".
177  $ilDB->quote($this->sahs_sc13_treeId, "integer").")");
178  $this->parentchapter = false;
179  }
180  $new_id = $chap->getId();
181  break;
182  case "sco" :
183  $sco = new ilSCORM2004Sco($a_object);
184  $sco->setTitle($node->getElementsByTagName("title")->item(0)->nodeValue);
185  $sco->setSLMId($a_object->getId());
186  $sco->create(false,true);
187  $this->sahs_sc13_treeId = $sco->getId();
188  ilSCORM2004Node::putInTree($sco, $a_parent, $target);
189  $new_id = $sco->getId();
190  break;
191  }
192 
193  $seq_node = $node->getElementsByTagName("sequencing")->item(0);
194 
195  $obj_node = $seq_node->getElementsByTagName("objectives")->item(0);
196  //addtitle
197  if ($obj_node) {
198  foreach ($obj_node->childNodes as $objchild ) {
199  if ($objchild->nodeName === "objective" || $objchild->nodeName === "primaryObjective") {
200  $title = $objchild->getAttribute('objectiveID');
201  $objchild->setAttribute("title",$title);
202  $i++;
203  }
204  }
205  }
206 
207  $seq_item = new ilSCORM2004Item();
208  $seq_item->setTreeNodeId($this->sahs_sc13_treeId);
209  $seq_item->setImportid($this->importId);
210  $seq_item->setNocopy($seq_node->getAttribute('nocopy'));
211  $seq_item->setNodelete($seq_node->getAttribute('nodelete'));
212  $seq_item->setNomove($seq_node->getAttribute('nomove'));
213 
214  $seq_doc = new DOMDocument();
215  $toadd = $seq_doc->importNode($seq_node, true);
216  $seq_doc->appendChild($toadd);
217 
218  //generate Unique ObjectiveIDs for context
219 
220  //@targetObjectiveID
221  $xpath_obj = new DOMXPath($seq_doc);
222 
223  $found_nodes = $xpath_obj->query('//@objectiveID | //@referencedObjective | //@targetObjectiveID');
224  for ($i=0; $i<$found_nodes->length; $i++) {
225  $element = null;
226  $val = $found_nodes->item($i)->value;
228  if ($found_nodes->item($i)->name == "targetObjectiveID") {
229  $uid = $this->importId;
230  }
231  $val = strtolower(preg_replace('/ +/','_',$val)."_".$uid);
232  $element = $found_nodes->item($i)->ownerElement;
233  $element->setAttribute($found_nodes->item($i)->name,$val);
234  }
235 
236  $seq_item->setDom($seq_doc);
237 
238  $seq_item->insert();
239 
240  foreach($node->childNodes as $child)
241  { if ($child->nodeName === "item") {
242  $this->importTemplate($a_target,$a_object,$a_parent,$child);
243  }
244  }
245 
246  return $new_id;
247  }
248 
249 
250 
251  private function generateObjIds($a_dom) {
252 
253  }
254 
255  // **********************
256  // Standard DB Operations for Object
257  // **********************
258 
259  function insert($a_insert_node = false)
260  {
261  if ($a_insert_node==true) {$this->setSeqNodeId(parent::insert());}
262  $sql = "INSERT INTO sahs_sc13_seq_templ (seqnodeid,id)".
263  " values(".
264  $this->db->quote($this->seqNodeId, "integer").",".
265  $this->db->quote($this->id, "text").");";
266  $result = $this->db->manipulate($sql);
267  return true;
268  }
269 
270 
271 
272  // **********************
273  // GETTER METHODS
274  // **********************
275 
276  public function getSeqNodeId()
277  {
278  return $this->seqNodeId;
279  }
280 
281  public function getId()
282  {
283  return $this->id;
284  }
285 
286  // **********************
287  // Setter METHODS
288  // **********************
289 
290  public function setSeqNodeId($a_seqnodeid)
291  {
292  $this->seqNodeId = $a_seqnodeid;
293  }
294 
295  public function setId($a_id)
296  {
297  $this->id = $a_id;
298  }
299 
300  //static functions
301 
302  public static function getFileNameForIdentifier($a_identifier)
303  {
304  global $ilDB;
305  $query = "SELECT * FROM sahs_sc13_seq_templts WHERE identifier = ".
306  $ilDB->quote($a_identifier, "text");
307  $obj_set = $ilDB->query($query);
308  $obj_rec = $ilDB->fetchAssoc($obj_set);
309  return $obj_rec["fileName"]; // fixed, switch to all lowercase fields and tables in the future for mdb2 compliance
310  return $obj_rec["filename"];
311  }
312 
313  public static function templateForChapter($a_chapter_id)
314  {
315  global $ilDB;
316  $template = null;
317  $query = "SELECT * FROM sahs_sc13_seq_assign WHERE sahs_sc13_tree_node_id = ".
318  $ilDB->quote($a_chapter_id, "integer");
319  $obj_set = $ilDB->query($query);
320  $obj_rec = $ilDB->fetchAssoc($obj_set);
321  if ($obj_rec['identifier']) {
322  $template = new ilScorm2004SeqTemplate($obj_rec['identifier']);
323  }
324  return $template;
325  }
326 
327 }
328 
329 ?>