Example #1
0
 public function import_XML($xml, $compare = null)
 {
     $this->Sharing_id = 1;
     $xpath = new DOMXPath($xml);
     $elements = $xpath->query("/export");
     foreach ($elements as $element) {
         if (Ini::$version != $element->getAttribute("version")) {
             return -5;
         }
     }
     if ($compare == null) {
         $compare = array("Template" => array(), "Table" => array(), "CustomSection" => array(), "Test" => array(), "QTIAssessmentItem" => array());
     }
     //link templates
     $logged_user = User::get_logged_user();
     $elements = $xpath->query("/export/Template");
     foreach ($elements as $element) {
         $id = $element->getAttribute("id");
         $hash = $element->getAttribute("xml_hash");
         $compare["Template"][$id] = Template::find_xml_hash($hash);
         if ($compare["Template"][$id] == 0) {
             $obj = new Template();
             $obj->Owner_id = $logged_user->id;
             $lid = $obj->import_XML(Template::convert_to_XML_document($element));
             $compare["Template"][$id] = $lid;
         }
     }
     //link QTI assessment items
     $logged_user = User::get_logged_user();
     $elements = $xpath->query("/export/QTIAssessmentItem");
     foreach ($elements as $element) {
         $id = $element->getAttribute("id");
         $hash = $element->getAttribute("xml_hash");
         $compare["QTIAssessmentItem"][$id] = QTIAssessmentItem::find_xml_hash($hash);
         if ($compare["QTIAssessmentItem"][$id] == 0) {
             $obj = new QTIAssessmentItem();
             $obj->Owner_id = $logged_user->id;
             $lid = $obj->import_XML(QTIAssessmentItem::convert_to_XML_document($element));
             $compare["QTIAssessmentItem"][$id] = $lid;
         }
     }
     //link tables
     $elements = $xpath->query("/export/Table");
     foreach ($elements as $element) {
         $id = $element->getAttribute("id");
         $hash = $element->getAttribute("xml_hash");
         $compare["Table"][$id] = Table::find_xml_hash($hash);
         if ($compare["Table"][$id] == 0) {
             $obj = new Table();
             $obj->Owner_id = $logged_user->id;
             $lid = $obj->import_XML(Table::convert_to_XML_document($element));
             $compare["Table"][$id] = $lid;
         }
     }
     //link custom sections
     $elements = $xpath->query("/export/CustomSection");
     foreach ($elements as $element) {
         $id = $element->getAttribute("id");
         $hash = $element->getAttribute("xml_hash");
         $compare["CustomSection"][$id] = CustomSection::find_xml_hash($hash);
         if ($compare["CustomSection"][$id] == 0) {
             $obj = new CustomSection();
             $obj->Owner_id = $logged_user->id;
             $lid = $obj->import_XML(CustomSection::convert_to_XML_document($element));
             $compare["CustomSection"][$id] = $lid;
         }
     }
     //link tests
     $elements = $xpath->query("/export/Test");
     for ($i = 0; $i < $elements->length - 1; $i++) {
         $element = $elements->item($i);
         $id = $element->getAttribute("id");
         $hash = $element->getAttribute("xml_hash");
         if (!isset($compare["Test"][$id])) {
             $compare["Test"][$id] = 0;
         }
         if ($compare["Test"][$id] == 0) {
             $obj = new Test();
             $obj->Owner_id = $logged_user->id;
             $lid = $obj->import_XML(CustomSection::convert_to_XML_document($element), $compare);
             $compare["Test"][$id] = $lid;
         }
     }
     $elements = $xpath->query("/export/Test");
     $element = $elements->item($elements->length - 1);
     $this->xml_hash = $element->getAttribute("xml_hash");
     $element_id = $element->getAttribute("id");
     if (isset($compare["Test"][$element_id]) && $compare["Test"][$element_id] != 0) {
         return $compare["Test"][$element_id];
     }
     $children = $element->childNodes;
     foreach ($children as $child) {
         switch ($child->nodeName) {
             case "name":
                 $this->name = $child->nodeValue;
                 break;
             case "description":
                 $this->description = $child->nodeValue;
                 break;
             case "open":
                 $this->open = $child->nodeValue;
                 break;
             case "loader_Template_id":
                 $this->loader_Template_id = $child->nodeValue == 0 ? 0 : $compare["Template"][$child->nodeValue];
                 break;
         }
     }
     $this->id = $this->mysql_save();
     $post = array();
     $post["sections"] = array();
     $elements = $xpath->query("/export/Test[@id='" . $element_id . "']/TestSections/TestSection");
     foreach ($elements as $element) {
         $test_section = array();
         $test_section["value"] = array();
         $children = $element->childNodes;
         foreach ($children as $child) {
             switch ($child->nodeName) {
                 case "end":
                     $test_section["end"] = $child->nodeValue;
                     break;
                 case "counter":
                     $test_section["counter"] = $child->nodeValue;
                     break;
                 case "TestSectionType_id":
                     $test_section["type"] = $child->nodeValue;
                     break;
                 case "parent_counter":
                     $test_section["parent"] = $child->nodeValue;
                     break;
                 case "TestSectionValues":
                     $ts_child_list = $child->childNodes;
                     foreach ($ts_child_list as $ts_child) {
                         $index = -1;
                         $value = "";
                         $tsv_vars = $ts_child->childNodes;
                         foreach ($tsv_vars as $tsv_child) {
                             switch ($tsv_child->nodeName) {
                                 case "index":
                                     $index = $tsv_child->nodeValue;
                                     break;
                                 case "value":
                                     $value = $tsv_child->nodeValue;
                                     break;
                             }
                         }
                         if ($index != -1) {
                             $test_section["value"]["v" . $index] = $value;
                         }
                     }
                     break;
             }
         }
         switch ($test_section["type"]) {
             case 2:
                 if ($test_section["value"]["v0"] == 0) {
                     break;
                 }
                 $value = 0;
                 if (isset($compare["Template"][$test_section["value"]["v0"]])) {
                     $value = $compare["Template"][$test_section["value"]["v0"]];
                 }
                 $test_section["value"]["v0"] = $value;
                 break;
             case 13:
                 if ($test_section["value"]["v0"] == 0) {
                     break;
                 }
                 $value = 0;
                 if (isset($compare["QTIAssessmentItem"][$test_section["value"]["v0"]])) {
                     $value = $compare["QTIAssessmentItem"][$test_section["value"]["v0"]];
                 }
                 $test_section["value"]["v0"] = $value;
                 break;
             case 11:
                 if ($test_section["value"]["v0"] == 0) {
                     break;
                 }
                 $value = 0;
                 if (isset($compare["Test"][$test_section["value"]["v0"]])) {
                     $value = $compare["Test"][$test_section["value"]["v0"]];
                 }
                 $test_section["value"]["v0"] = $value;
                 break;
             case 9:
                 if ($test_section["value"]["v0"] == 0) {
                     break;
                 }
                 $value = 0;
                 if (isset($compare["CustomSection"][$test_section["value"]["v0"]])) {
                     $value = $compare["CustomSection"][$test_section["value"]["v0"]];
                 }
                 $test_section["value"]["v0"] = $value;
                 break;
             case 8:
                 if ($test_section["value"]["v3"] == 0) {
                     break;
                 }
                 $value = 0;
                 if (isset($compare["Table"][$test_section["value"]["v3"]])) {
                     $value = $compare["Table"][$test_section["value"]["v3"]];
                 }
                 $test_section["value"]["v3"] = $value;
                 break;
             case 5:
                 if ($test_section["value"]["v5"] == 0) {
                     break;
                 }
                 if ($test_section["value"]["v2"] == 0) {
                     $value = 0;
                     if (isset($compare["Table"][$test_section["value"]["v5"]])) {
                         $value = $compare["Table"][$test_section["value"]["v5"]];
                     }
                     $test_section["value"]["v5"] = $value;
                 }
                 break;
         }
         if (count($test_section["value"]) == 0) {
             $test_section['value'] = "{}";
         }
         array_push($post["sections"], $test_section);
     }
     $post['sections'] = json_encode($post['sections']);
     $post["parameters"] = array();
     $elements = $xpath->query("/export/Test[@id='" . $element_id . "']/TestVariables/TestVariable");
     foreach ($elements as $element) {
         $tv = array();
         $tv["Test_id"] = $element_id;
         $children = $element->childNodes;
         $correct = true;
         foreach ($children as $child) {
             switch ($child->nodeName) {
                 case "index":
                     $tv["index"] = $child->nodeValue;
                     break;
                 case "name":
                     $tv["name"] = $child->nodeValue;
                     break;
                 case "description":
                     $tv["description"] = $child->nodeValue;
                     break;
                 case "type":
                     $tv["type"] = $child->nodeValue;
                     if ($tv["type"] != 0) {
                         $correct = false;
                     }
                     break;
             }
         }
         if ($correct) {
             $tv = json_encode($tv);
             array_push($post['parameters'], $tv);
         }
     }
     $post["returns"] = array();
     $elements = $xpath->query("/export/Test[@id='" . $element_id . "']/TestVariables/TestVariable");
     foreach ($elements as $element) {
         $tv = array();
         $tv["Test_id"] = $element_id;
         $children = $element->childNodes;
         $correct = true;
         foreach ($children as $child) {
             switch ($child->nodeName) {
                 case "index":
                     $tv["index"] = $child->nodeValue;
                     break;
                 case "name":
                     $tv["name"] = $child->nodeValue;
                     break;
                 case "description":
                     $tv["description"] = $child->nodeValue;
                     break;
                 case "type":
                     $tv["type"] = $child->nodeValue;
                     if ($tv["type"] != 1) {
                         $correct = false;
                     }
                     break;
             }
         }
         if ($correct) {
             $tv = json_encode($tv);
             array_push($post['returns'], $tv);
         }
     }
     $post["protected"] = array();
     $elements = $xpath->query("/export/Test[@id='" . $element_id . "']/TestProtectedVariables/TestProtectedVariable");
     foreach ($elements as $element) {
         $tpv = array();
         $tpv["Test_id"] = $element_id;
         $children = $element->childNodes;
         $correct = true;
         foreach ($children as $child) {
             switch ($child->nodeName) {
                 case "name":
                     $tpv["name"] = $child->nodeValue;
                     break;
             }
         }
         $tpv = json_encode($tpv);
         array_push($post['protected'], $tpv);
     }
     return $this->mysql_save_from_post($post);
 }