//add new
     for ($i = 0; $i < 500; $i++) {
         if (strlen(${"ANSWER_n" . $i . "_ANSWER"}) <= 0) {
             continue;
         }
         $arFields = array("SORT" => ${"ANSWER_n" . $i . "_SORT"}, "ANSWER" => ${"ANSWER_n" . $i . "_ANSWER"}, "QUESTION_ID" => $ID);
         switch ($QUESTION_TYPE) {
             case "M":
                 $arFields["CORRECT"] = ${"ANSWER_n" . $i . "_CORRECT"} == "Y" ? "Y" : "N";
                 break;
             case "S":
             default:
                 $arFields["CORRECT"] = "n" . $i == $ANSWER_CORRECT ? "Y" : "N";
         }
         $asw = new CLAnswer();
         $AswerID = $asw->Add($arFields);
         if (intval($AswerID) <= 0) {
             if ($e = $APPLICATION->GetException()) {
                 $message = new CAdminMessage(GetMessage("LEARNING_ERROR"), $e);
             }
             $bVarsFromForm = true;
         }
     }
 } else {
     //Delete answers
     $answers = CLAnswer::GetList(array(), array("QUESTION_ID" => $ID));
     while ($a = $answers->GetNext()) {
         CLAnswer::Delete($a["ID"]);
     }
 }
 //Redirect
Esempio n. 2
0
 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());
         }
     }
 }