/** * 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; }