public function communicate(string $name = null, Question $question)
 {
     $this->answersSet->has($name);
     if (null !== $name && $this->answersSet->has($name)) {
         throw new \InvalidArgumentException(sprintf('Question with name "%s" already exists in the QuestionsSet.', $name));
     }
     $default = $this->answersSet->resolve($name, $question->getDefault());
     if ($this->skipOptional && $question->isOptional()) {
         $value = $default;
     } else {
         $value = ($this->communicator)($question->createQuestion($default), $name);
     }
     if (null !== $name) {
         $answer = $value;
         $value = $question->getNormalizer() ? call_user_func($question->getNormalizer(), $value) : $value;
         $this->answersSet->set($name, $answer, $value);
     }
     return $value;
 }
 /**
  * Copy test
  *
  * This function copies a test into the current content tree
  * <br/>Example:
  * <code>
  * $currentContent = new EfrontContentTree(5);           //Initialize content for lesson with id 5
  * $currentContent -> copyTest(3, false);   //Copy the corresponding test into the content tree (at its end)
  * </code>
  *
  * @param int $testId The id of the test to be copied
  * @param mixed $targetUnit The id of the parent unit (or the parent EfrontUnit)in which the new unit will be copied, or false (the unit will be appended at the end)
  * @param boolean $copyQuestions Whether to copy questions as well. Copied questions will be attached to the test itself as parent unit
  * @return EfrontUnit The newly created test unit object
  * @since 3.5.0
  * @access public
  */
 public function copyTest($testId, $targetUnit = false, $copyQuestions = true, $copyFiles = true, $linked_to = false)
 {
     $oldTest = new EfrontTest($testId);
     $oldUnit = $oldTest->getUnit();
     $oldUnit['data'] = $oldTest->test['description'];
     //Hack in order to successfully copy files. It will be removed when we implement the new copy/export framework
     $newUnit = $this->copySimpleUnit($oldUnit, $targetUnit);
     $oldTest->test['description'] = $newUnit['data'];
     //As above
     $newTest = EfrontTest::createTest($newUnit, $oldTest->test);
     $newUnit['data'] = '';
     //As above
     $newUnit->persist();
     //As above
     if ($copyQuestions) {
         $testQuestions = $oldTest->getQuestions(true);
         $newQuestions = array();
         if (sizeof($testQuestions) > 0) {
             $result = eF_getTableData("questions", "*", "id in (" . implode(",", array_keys($testQuestions)) . ")");
             foreach ($result as $value) {
                 $questionData[$value['id']] = $value;
                 unset($questionData[$value['id']]['id']);
             }
         }
         $ids_mapping = array();
         $lesson = new EfrontLesson($newUnit->offsetGet('lessons_ID'));
         $folderId = $lesson->lesson['share_folder'] ? $lesson->lesson['share_folder'] : $lesson->lesson['id'];
         foreach ($testQuestions as $key => $oldQuestion) {
             $questionData[$key]['content_ID'] = $newUnit->offsetGet('id');
             $questionData[$key]['lessons_ID'] = $newUnit->offsetGet('lessons_ID');
             if ($copyFiles) {
                 $questionData[$key]['text'] = replaceQuestionPaths($questionData[$key]['text'], $oldUnit['lessons_ID'], $folderId);
                 $questionData[$key]['explanation'] = replaceQuestionPaths($questionData[$key]['explanation'], $oldUnit['lessons_ID'], $folderId);
             }
             $newQuestion = Question::createQuestion($questionData[$key]);
             $qid = $newQuestion->question['id'];
             if ($linked_to) {
                 $newQuestion->question['linked_to'] = $oldQuestion->question['id'];
                 $newQuestion->persist();
             }
             $newQuestions[$qid] = $oldTest->getAbsoluteQuestionWeight($oldQuestion->question['id']);
             $ids_mapping[$oldQuestion->question['id']] = $qid;
         }
         //code for sorting $newQuestions based on $oldQuestion in order to be copied in same order(#2962)
         $newQuestionsSorted = array();
         foreach ($testQuestions as $key => $oldQuestion) {
             $newQuestionsSorted[$ids_mapping[$key]] = $newQuestions[$ids_mapping[$key]];
         }
         $newQuestions = $newQuestionsSorted;
         $newTest->addQuestions($newQuestions);
     }
     return $newUnit;
 }
         }
     } else {
         $currentQuestion->question = array_merge($currentQuestion->question, $question_values);
         //This way, latter values (new ones) replace former (current ones);
         $currentQuestion->persist();
         //Update the question
         if (isset($_GET['return'])) {
             eF_redirect(basename($_SERVER['PHP_SELF']) . '?' . urldecode($_GET['return']) . "&message=" . rawurlencode(_SUCCESFULLYUPDATEDQUESTION) . "&message_type=success");
         } else {
             eF_redirect("" . ltrim(basename($_SERVER['PHP_SELF']), "/") . "?ctg=tests{$location}&message=" . rawurlencode(_SUCCESFULLYUPDATEDQUESTION) . "&message_type=success&tab=question");
             //&question is used for the tabber to enable the correct tab
         }
     }
 } else {
     //We are inserting a new question
     $newQuestion = Question::createQuestion($question_values);
     $new_question_id = $newQuestion->question['id'];
     // Code to maintain consistent state in questions_to_skills:
     // -- add either question to lesson specific skill if lesson['course_only'] == 0
     // -- or question to course specific skill if lesson['course_only'] == 1
     if (G_VERSIONTYPE == 'educational') {
         #cpp#ifdef EDUCATIONAL
         if ($question_values['lessons_ID']) {
             // then the currentLesson object exists
             if ($currentLesson->lesson['course_only']) {
                 $courses = $currentLesson->getCourses(true);
                 //OPTIMIZE
                 foreach ($courses as $course) {
                     $course_specific_skill = $course->getCourseSkill();
                     eF_insertTableData("questions_to_skills", array("questions_ID" => $new_question_id, "skills_ID" => $course_specific_skill['skill_ID'], "relevance" => 2));
                 }
 public function getModule()
 {
     $smarty = $this->getSmartyVar();
     // Always show the preview of the data
     if ($_POST['preview'] || $_POST['submit']) {
         if ($_POST['questions_format'] == "gift") {
             $questions = $this->scanGIFT($_POST['imported_data']);
         } else {
             $questions = $this->scanAIKEN(str_replace('\\"', '"', str_replace("\\'", "'", $_POST['imported_data'])));
         }
         if (sizeof($questions)) {
             $smarty->assign("T_PREVIEW_DIV", $this->createPreviewHTML($questions));
         }
     }
     // Submit the data the data
     if ($_POST['submit']) {
         if ($_POST['select_unit'] == -1 || $_POST['select_unit'] == -2) {
             $content_ID = 0;
         } else {
             $content_ID = $_POST['select_unit'];
         }
         $currentLesson = $this->getCurrentLesson();
         $lessons_ID = $currentLesson->lesson['id'];
         $count = 0;
         foreach ($questions as $key => $question) {
             if ($question['type'] != "same" && $question['type'] != "error" && $question['type'] != "no_answer_error") {
                 $question['content_ID'] = $content_ID;
                 $question['lessons_ID'] = $lessons_ID;
                 $question['difficulty'] = "medium";
                 if (sizeof($question['options'])) {
                     $question['options'] = serialize($question['options']);
                     //$question['options'] = str_replace("'", "&#39;", $question['options']);
                     //$question['options'] = str_replace("\r", "", $question['options']);
                 }
                 if (sizeof($question['answer'])) {
                     // Different accounting for answers of multiple many type
                     if ($question['type'] == "multiple_many") {
                         $answers = array();
                         foreach ($question['answer'] as $answer) {
                             $answers[$answer] = "1";
                         }
                         $question['answer'] = $answers;
                     }
                     $question['answer'] = serialize($question['answer']);
                     //$question['answer'] = str_replace("'", "&#39;", $question['answer']);
                     //$question['answer'] = str_replace("\r", "", $question['answer']);
                 }
                 //$question['text'] = str_replace("'", "&#39;", $question['text']);
                 if (isset($question['explanation'])) {
                     //$question['explanation'] = str_replace("'", "&#39;", $question['explanation']);
                     //$question['explanation'] = str_replace("\r", "", $question['explanation']);
                 }
                 if (Question::createQuestion($question)) {
                     $count++;
                 }
             }
         }
         if ($count) {
             $this->setMessageVar($count . " " . _GIFTAIKEN_QUESTIONSIMPORTEDSUCCESSFULLY, "success");
         } else {
             $this->setMessageVar(_GIFTAIKEN_NOQUESTIONCOULDBEIMPORTED, "failure");
         }
     }
     $pos = strpos($_SERVER['REQUEST_URI'], "&message");
     if ($pos) {
         $postUrl = substr($_SERVER['REQUEST_URI'], 0, $pos);
     } else {
         $postUrl = $_SERVER['REQUEST_URI'];
     }
     $importForm = new HTML_QuickForm("import_users_form", "post", $postUrl, "", null, true);
     $importForm->registerRule('checkParameter', 'callback', 'eF_checkParameter');
     //Register this rule for checking user input with our function, eF_checkParameter
     $importForm->addElement('radio', 'questions_format', _GIFTAIKEN_GIFT, null, 'gift', 'id="gift_selection"');
     $importForm->addElement('radio', 'questions_format', _GIFTAIKEN_AIKEN, null, 'aiken', 'id="aiken_selection"');
     $currentLesson = $this->getCurrentLesson();
     $currentContent = new EfrontContentTree($currentLesson->lesson['id']);
     $smarty->assign("T_UNITS", $currentContent->toHTMLSelectOptions());
     $importForm->addElement('textarea', 'imported_data', _GIFTAIKEN_QUESTIONDATATOIMPORT, 'class = "inputProjectTextarea" id="imported_data"');
     $importForm->addElement('submit', 'preview', _PREVIEW, 'class=flatButton onclick="$(\'import_users_form\').action += \'&preview=1\'"');
     $importForm->addElement('submit', 'submit', _SUBMIT, 'class=flatButton');
     $renderer = new HTML_QuickForm_Renderer_ArraySmarty($smarty);
     $importForm->setJsWarnings(_BEFOREJAVASCRIPTERROR, _AFTERJAVASCRIPTERROR);
     $importForm->setRequiredNote(_REQUIREDNOTE);
     $importForm->accept($renderer);
     $smarty->assign('T_GIFTAIKENQUESTIONS_FORM', $renderer->toArray());
     return true;
 }