/**
  * 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;
 }
     }
     if ($_GET['ctg'] != 'feedback') {
         $messageString = _SUCCESFULLYMODIFIEDTEST;
     } else {
         $messageString = _SUCCESFULLYMODIFIEDFEEDBACK;
     }
     EfrontCache::getInstance()->deleteCache("content_tree:{$_SESSION['s_lessons_ID']}");
     eF_redirect("" . ltrim(basename($_SERVER['PHP_SELF']), "/") . "?ctg=" . $_GET['ctg'] . "&from_unit=" . $_GET['from_unit'] . "&message=" . urlencode($messageString) . "&message_type=success");
 } else {
     $contentFields = array('data' => '', 'name' => $values['name'], 'lessons_ID' => $currentLesson->lesson['id'], 'ctg_type' => $_GET['ctg'], 'active' => 1, 'timestamp' => time(), 'parent_content_ID' => $values['parent_content']);
     $testFields = array('active' => 1, 'lessons_ID' => isset($currentLesson->lesson['id']) ? $currentLesson->lesson['id'] : 0, 'content_ID' => $test_content_ID, 'description' => applyEditorOffset($values['description']), 'options' => serialize($testOptions), 'name' => $values['name'], 'publish' => $values['publish'], 'keep_best' => $values['keep_best'], 'mastery_score' => $values['mastery_score'] ? $values['mastery_score'] : 0);
     if (!$skillgap_tests) {
         $newUnit = $currentContent->insertNode($contentFields);
         $newTest = EfrontTest::createTest($newUnit, $testFields);
     } else {
         $newTest = EfrontTest::createTest(false, $testFields);
     }
     // If the new test comes from an existing one we should also copy its questions...
     if ($_GET['edit_test']) {
         $testQuestions = $currentTest->getQuestions();
         $newTest->addQuestions($testQuestions);
         // ... and its users if it is a skillgap test
         if ($skillgap_tests) {
             $testUsers = eF_getTableDataFlat("users_to_skillgap_tests", "users_LOGIN", "tests_ID = '" . $_GET['edit_test'] . "'");
             $fields = array();
             foreach ($testUsers as $entry) {
                 $fields[] = array('tests_ID' => $newTest->test['id'], 'users_LOGIN' => $entry['useres_LOGIN']);
             }
             if (sizeof($fields) > 0) {
                 eF_insertTableDataMultiple("users_to_skillgap_tests", $fields);
                 //$insertString = "('" . $newTest->test['id'] . "', '" . implode("'),('" . $newTest -> test['id'] . "', '", $testUsers['users_LOGIN']) . "')";