protected function _MakeItems($TITLE, $TYPE, $RES_ID, $PARENT_ID) { global $APPLICATION; if ($PARENT_ID === 0) { $linkToParentLessonId = CCourse::CourseGetLinkedLesson($this->COURSE_ID); } else { $linkToParentLessonId = (int) $PARENT_ID; } $createUnilesson = false; if ($TYPE == "LES") { $arFields = array('NAME' => $TITLE); $createUnilesson = true; } elseif ($TYPE == "CHA") { $arFields = array('NAME' => $TITLE); $createUnilesson = true; } elseif ($TYPE == "QUE") { $arFields = array("NAME" => $TITLE, "LESSON_ID" => $linkToParentLessonId); $cl = new CLQuestion(); } elseif ($TYPE == "TES") { $arFields = array("NAME" => $TITLE, "COURSE_ID" => $this->COURSE_ID); $cl = new CTest(); } elseif ($TYPE === 'TMK') { $arFields = array(); $cl = new CLTestMark(); } else { return $PARENT_ID; } $r = new CDataXML(); if (!$r->Load($this->package_dir . "/" . strtolower($RES_ID) . ".xml")) { $r = false; } if ($r !== false) { if ($TYPE == "QUE") { if (($data = $r->SelectNodes("/questestinterop/item/presentation/")) && ($resp = $r->SelectNodes("/questestinterop/item/resprocessing/"))) { $arQ = array(); $arData = $data->__toArray(); $arResp = $resp->__toArray(); if (is_set($arData["#"]["material"][0]["#"], "mattext")) { $arQ["NAME"] = $arData["#"]["material"][0]["#"]["mattext"][0]["#"]; } if (is_set($arData["#"]["material"][0]["#"], "matimage")) { $imageDescription = ''; if (is_set($arData["#"]["material"][0]["#"], 'image_description')) { $imageDescription = $arData["#"]["material"][0]["#"]['image_description'][0]['#']; } $arQ["FILE_ID"] = array("MODULE_ID" => "learning", "name" => basename($arData["#"]["material"][0]["#"]["matimage"][0]["@"]["uri"]), "tmp_name" => $this->package_dir . "/" . $arData["#"]["material"][0]["#"]["matimage"][0]["@"]["uri"], "size" => @filesize($this->package_dir . "/" . $arData["#"]["material"][0]["#"]["matimage"][0]["@"]["uri"]), "type" => $arData["#"]["material"][0]["#"]["matimage"][0]["@"]["imagtype"], 'description' => $imageDescription); } if (is_set($arData["#"]["response_lid"][0]["@"], "rcardinality")) { switch ($arData["#"]["response_lid"][0]["@"]["rcardinality"]) { case "Multiple": $arQ["QUESTION_TYPE"] = 'M'; break; case "Text": $arQ["QUESTION_TYPE"] = 'T'; break; case "Sort": $arQ["QUESTION_TYPE"] = 'R'; break; default: $arQ["QUESTION_TYPE"] = 'S'; break; } } if (is_set($arResp["#"]["respcondition"][0]["#"], "setvar")) { $arQ["POINT"] = $arResp["#"]["respcondition"][0]["#"]["setvar"][0]['#']; } //Additional if ($bx = $r->SelectNodes("/questestinterop/item/bitrix/")) { $arQ = array_merge($arQ, $this->_MakeFields($bx->__toArray(), $TYPE)); unset($bx); } $arFields = array_merge($arFields, $arQ); $cl = new CLQuestion(); $ID = $cl->Add($arFields); if ($ID > 0) { $PARENT_ID = $ID; $arCorrect = array(); if (is_set($arResp["#"]["respcondition"][0]["#"], "conditionvar") && is_set($arResp["#"]["respcondition"][0]["#"]["conditionvar"][0]["#"], "varequal")) { foreach ($arResp["#"]["respcondition"][0]["#"]["conditionvar"][0]["#"]["varequal"] as $ar) { $arCorrect[] = $ar["#"]; } } if (is_set($arData["#"]["response_lid"][0]["#"], "render_choice") && is_set($arData["#"]["response_lid"][0]["#"]["render_choice"][0]["#"], "response_label")) { $i = 0; foreach ($arData["#"]["response_lid"][0]["#"]["render_choice"][0]["#"]["response_label"] as $ar) { $i += 10; $cl = new CLAnswer(); $arFields = array("QUESTION_ID" => $PARENT_ID, "SORT" => $i, "CORRECT" => in_array($ar["@"]["ident"], $arCorrect) ? "Y" : "N", "ANSWER" => $ar["#"]["material"][0]["#"]["mattext"][0]["#"]); $AswerID = $cl->Add($arFields); $res = $AswerID > 0; if (!$res) { if ($e = $APPLICATION->GetException()) { $this->arWarnings[$TYPE][] = array("TITLE" => $TITLE, "TEXT" => $e->GetString()); } } } } } else { if ($e = $APPLICATION->GetException()) { $this->arWarnings[$TYPE][] = array("TITLE" => $TITLE, "TEXT" => $e->GetString()); } } unset($cl); unset($data); unset($arQ); unset($resp); unset($arData); unset($arResp); return $PARENT_ID; } } else { if ($data = $r->SelectNodes("/content/")) { $ar = $data->__toArray(); $arFields = array_merge($arFields, $this->_MakeFields($ar, $TYPE)); if ($TYPE === 'TMK') { $arFields['TEST_ID'] = (int) $PARENT_ID; } if (is_set($arFields, "COMPLETED_SCORE") && intval($arFields["COMPLETED_SCORE"]) <= 0) { unset($arFields["COMPLETED_SCORE"]); } if (is_set($arFields, "PREVIOUS_TEST_ID") && intval($arFields["PREVIOUS_TEST_ID"]) <= 0 || !CTest::GetByID($arFields["PREVIOUS_TEST_ID"])->Fetch()) { unset($arFields["PREVIOUS_TEST_ID"], $arFields["PREVIOUS_TEST_SCORE"]); } } } } if ($createUnilesson === false) { $ID = $cl->Add($arFields); unset($cl); } else { $bProhibitPublish = false; // properties (in context of parent) by default $arProperties = array('SORT' => 500); // Lesson's sort order in context of parent if (isset($arFields['EDGE_SORT'])) { $arFields['SORT'] = (int) $arFields['EDGE_SORT']; unset($arFields['EDGE_SORT']); } if (isset($arFields['SORT'])) { $arProperties['SORT'] = (int) $arFields['SORT']; // Lessons doesn't have more SORT field unset($arFields['SORT']); } if (isset($arFields['META_PUBLISH_PROHIBITED'])) { if ($arFields['META_PUBLISH_PROHIBITED'] === 'Y') { $bProhibitPublish = true; } unset($arFields['META_PUBLISH_PROHIBITED']); } // unset fields, that are absent in unilesson $arUnilessonFields = $arFields; $arFieldsNames = array_keys($arUnilessonFields); foreach ($arFieldsNames as $fieldName) { if (!in_array(strtoupper($fieldName), $this->arLessonWritableFields)) { unset($arUnilessonFields[$fieldName]); } } $ID = CLearnLesson::Add($arUnilessonFields, false, $linkToParentLessonId, $arProperties); if ($bProhibitPublish && $ID > 0) { CLearnLesson::PublishProhibitionSetTo($ID, $linkToParentLessonId, $bProhibitPublish); } } if ($ID > 0) { return $ID; } else { if ($e = $APPLICATION->GetException()) { $this->arWarnings[$TYPE][] = array("TITLE" => $TITLE, "TEXT" => $e->GetString()); } } }
require_once($_SERVER["DOCUMENT_ROOT"]."/freetrix/modules/main/classes/general/xml.php"); $bIncorrectFormat = false; $handle = fopen($abs_path, "r"); $size = filesize($abs_path); if ($size > 20) { $contents = fread($handle, 20); if (strtolower(substr($contents, 0, 5)) != "<?xml") $bIncorrectFormat = true; } if (!$bIncorrectFormat) { $objXML = new CDataXML(); $objXML->Load($abs_path); $arTree = $objXML->GetTree(); $arTracks = Array(); $bIncorrectFormat = true; $ch = $arTree->children; if (count($ch) > 0 && strtolower($ch[0]->name) == 'playlist') { $pl = $ch[0]; $tls = $pl->children; for ($i_ = 0, $l_ = count($tls); $i_ < $l_; $i_++) { if (strtolower($tls[$i_]->name) != 'tracklist') continue; $tracks = $tls[$i_]->children; for ($i = 0, $l = count($tracks); $i < $l; $i++)
public static function ImportPropsFromXML($BID, $xmlPath, &$arOldNewID) { if (file_exists($xmlPath) && $BID && CModule::IncludeModule('iblock')) { require_once $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/classes/general/xml.php'; $arExistProps = array(); $rsProp = CIBlockProperty::GetList(array(), array('IBLOCK_ID' => $BID)); while ($arProp = $rsProp->Fetch()) { $arExistProps[$arProp['CODE']] = $arProp; } $arExistEnums = array(); $rsEnum = CIBlockPropertyEnum::GetList(array(), array('IBLOCK_ID' => $BID)); while ($arEnum = $rsEnum->Fetch()) { $arExistEnums[$arEnum['PROPERTY_ID'] . '_' . $arEnum['XML_ID']] = $arEnum; } $arOldNewID = array(); $xml = new CDataXML(); $ep = new CIBlockProperty(); $en = new CIBlockPropertyEnum(); if ($xml->Load($xmlPath)) { if ($node = $xml->SelectNodes('/asd_iblock_props/props/')) { foreach ($node->children() as $child) { $arProp = array_pop($child->__toArray()); $arFields = array('IBLOCK_ID' => $BID); foreach ($arProp as $code => $v) { $arFields[strtoupper($code)] = is_array($v[0]['#']['cdata-section']) ? $v[0]['#']['cdata-section'][0]['#'] : $v[0]['#']; } if (isset($arExistProps[$arFields['CODE']])) { $arOldNewID[$arFields['OLD_ID']] = $arExistProps[$arFields['CODE']]['ID']; $ep->Update($arExistProps[$arFields['CODE']]['ID'], $arFields); } else { $arOldNewID[$arFields['OLD_ID']] = $arFields['ID'] = $ep->Add($arFields); $arExistProps[$arFields['CODE']] = $arFields; } } } if ($node = $xml->SelectNodes('/asd_iblock_props/enums/')) { foreach ($node->children() as $child) { $arProp = array_pop($child->__toArray()); $arFields = array('IBLOCK_ID' => $BID); foreach ($arProp as $code => $v) { $arFields[strtoupper($code)] = is_array($v[0]['#']['cdata-section']) ? $v[0]['#']['cdata-section'][0]['#'] : $v[0]['#']; } $arFields['PROPERTY_ID'] = $arExistProps[$arFields['PROPERTY_CODE']]['ID']; if (isset($arExistEnums[$arFields['PROPERTY_ID'] . '_' . $arFields['XML_ID']])) { $en->Update($arExistEnums[$arFields['PROPERTY_ID'] . '_' . $arFields['XML_ID']]['ID'], $arFields); } else { $en->Add($arFields); } } } } } }