/**
  * Analyse completed test
  *
  * This function is used to analyse completed test. Scores are calculated for
  * each unit and subunit, based on the corresponding questions performance.
  * <br/>Example:
  * <code>
  * list($parentScores, $analysisCode) = $completedTest -> analyseTest();
  * </code>
  * The function returns an array with 2 separate elements: The first element is the array
  * of scores per unit, which is needed in order to display the chart. The second element
  * is the content tree, where the scores per unit are depicted.
  *
  * @return array A results array.
  * @since 3.5.2
  * @access public
  */
 public function analyseTest()
 {
     $parentScores = array();
     foreach ($this->questions as $question) {
         $questionIds[$question->question['content_ID']]['score'] += $question->score;
         $questionIds[$question->question['content_ID']]['total']++;
         $question->score > 0 ? $questionIds[$question->question['content_ID']]['correct'] += $question->score / 100 : null;
     }
     $questionsStats = EfrontStats::getQuestionsUnitStatistics($this->questions);
     //Get unit names and ids
     $content = new EfrontContentTree(key($this->getLesson()));
     if (isset($_GET['entity']) && $_GET['entity']) {
         $temp = $content->seekNode($_GET['entity']);
         $tree[0] = new EfrontUnit(array('id' => 0, 'name' => _NOUNIT, 'active' => 1, $temp['id'] => $temp));
         //Add a bogus unit to hold questions which do not belong to a unit
         $iterator = new EfrontNodeFilterIterator(new RecursiveIteratorIterator(new RecursiveArrayIterator($tree), RecursiveIteratorIterator::SELF_FIRST));
     } else {
         $tree = $content->tree;
         $tree[0] = new EfrontUnit(array('id' => 0, 'name' => _NOUNIT, 'active' => 1));
         //Add a bogus unit to hold questions which do not belong to a unit
         $iterator = new EfrontNodeFilterIterator(new RecursiveIteratorIterator(new RecursiveArrayIterator($content->tree), RecursiveIteratorIterator::SELF_FIRST));
     }
     foreach ($iterator as $key => $value) {
         if ($key != 0) {
             foreach ($content->getNodeAncestors($key) as $id => $foo) {
                 $parentScores[$foo['id']]['score'] += $questionIds[$key]['score'];
                 $parentScores[$foo['id']]['total'] += $questionIds[$key]['total'];
                 $parentScores[$foo['id']]['correct'] += $questionIds[$key]['correct'];
             }
             $parentScores[$key]['this_score'] += $questionIds[$key]['score'];
             $parentScores[$key]['this_total'] += $questionIds[$key]['total'];
             $parentScores[$key]['this_correct'] += $questionIds[$key]['correct'];
             $parentScores[$key]['name'] = $value['name'];
             // Check if this chapter is a parent one.
             if (isset($content->tree[$key])) {
                 $parentScores[$key]['top_level'] = 1;
             } else {
                 $parentScores[$key]['top_level'] = 0;
             }
         }
     }
     foreach ($parentScores as $id => $value) {
         if ($value['total']) {
             $parentScores[$id]['percentage'] = round($value['score'] / $value['total'], 2);
         }
         if ($value['this_total']) {
             $parentScores[$id]['this_percentage'] = round($value['this_score'] / $value['this_total'], 2);
         }
         if ($value['total']) {
             $options['custom'][$id] = '
                             <span style = "margin-left:20px;color:gray">' . $parentScores[$id]['percentage'] . '% [' . $value['correct'] . '/' . $value['total'] . ']</span>
                             <img src = "images/16x16/information.png" style = "vertical-align:middle" alt = "" title = "' . _THISLEVEL . ': ' . $parentScores[$id]['this_percentage'] . '% [' . $value['this_correct'] . '/' . $value['this_total'] . ']';
             if ($value['total'] - $value['this_total'] > 0) {
                 $options['custom'][$id] .= '/ ' . _BELOWLEVELS . ': ' . round(($value['score'] - $value['this_score']) / ($value['total'] - $value['this_total']), 2) . '% [' . ($value['correct'] - $value['this_correct']) . '/' . ($value['total'] - $value['this_total']) . '] ';
             }
             $options['custom'][$id] .= '">';
         } else {
             unset($parentScores[$id]);
         }
     }
     $iterator = new analyseTestFilterIterator(new EfrontNodeFilterIterator(new RecursiveIteratorIterator(new RecursiveArrayIterator($content->tree), RecursiveIteratorIterator::SELF_FIRST)), array_keys($parentScores));
     $options['show_hide'] = false;
     $options['noclick'] = true;
     //$options['tree_root'] = array('name' => _BACKTOTOP, 'class' => 'examples', 'onclick' => "$('analysis_frame').src = $('analysis_frame').src.replace(/selected_unit=(\d*)/, 'selected_unit='+Element.extend(this).up().id.replace(/node/, ''));");
     //$options['onclick']   = "re = new RegExp(this.up().id.replace(/node/, ''), 'g');if(treeObj.getNodeOrders().match(re).length > 1) $('analysis_frame').src = $('analysis_frame').src.replace(/selected_unit=(\d*)/, 'selected_unit='+Element.extend(this).up().id.replace(/node/, ''));";
     $options['onclick'] = "showGraph(\$('proto_chart'), 'graph_test_analysis', Element.extend(this).up().id.replace(/node/, ''));";
     return array($parentScores, $content->toHTML($iterator, false, $options));
 }
         //if (EfrontUser::isOptionVisible('tests')) {exit;}
         $iterator = new EfrontTestsFilterIterator(new EfrontVisitableFilterIterator($basicIterator));
     } else {
         if (isset($_GET['type']) && $_GET['type'] == 'theory') {
             $iterator = new EfrontTheoryFilterIterator(new EfrontVisitableFilterIterator($basicIterator));
         } else {
             if (isset($_GET['type']) && $_GET['type'] == 'examples') {
                 $iterator = new EfrontExampleFilterIterator(new EfrontVisitableFilterIterator($basicIterator));
             } else {
                 $iterator = new EfrontVisitableAndEmptyFilterIterator($basicIterator);
             }
         }
     }
     //Find the parents of each of these units, so that we can keep them in the tree
     foreach ($iterator as $key => $value) {
         foreach ($currentContent->getNodeAncestors($key) as $parent) {
             $parents[$parent['id']] = $parent['id'];
         }
     }
     //This iterator keeps the special units (for example, tests or examples) plus their parents
     $iterator = new EfrontInArrayFilterIterator(new EfrontVisitableFilterIterator(new EfrontNodeFilterIterator(new RecursiveIteratorIterator(new RecursiveArrayIterator($currentContent->tree), RecursiveIteratorIterator::SELF_FIRST))), $parents);
     $smarty->assign("T_THEORY_TREE", $currentContent->toHTML($iterator, 'dhtmlContentTree'));
 } else {
     if (G_VERSIONTYPE != 'community') {
         #cpp#ifndef COMMUNITY
         if (G_VERSIONTYPE != 'standard') {
             #cpp#ifndef STANDARD
             /*Check current unit*/
             if (isset($_GET['package_ID']) && $currentContent) {
                 if ($currentUser->coreAccess['content'] == 'hidden') {
                     eF_redirect(basename($_SERVER['PHP_SELF']) . "?ctg=control_panel&message=" . urlencode(_UNAUTHORIZEDACCESS) . "&message_type=failure");
         }
         $currentQuestion = QuestionFactory::factory($_GET['delete_question']);
         $currentQuestion->delete();
     } catch (Exception $e) {
         header("HTTP/1.0 500 ");
         echo $e->getMessage() . ' (' . $e->getCode() . ')';
     }
     exit;
 } elseif (isset($_GET['show_test']) && in_array($_GET['show_test'], $legalValues) && eF_checkParameter($_GET['show_test'], 'id') || isset($_GET['view_unit']) && in_array($_GET['view_unit'], $legalUnits) && eF_checkParameter($_GET['view_unit'], 'id')) {
     if (isset($_GET['view_unit'])) {
         $showTest = new EfrontTest($_GET['view_unit'], true);
         $smarty->assign("T_UNIT", $currentUnit);
         $smarty->assign("T_NEXT_UNIT", $currentContent->getNextNode($currentUnit, $visitableIterator));
         $smarty->assign("T_PREVIOUS_UNIT", $currentContent->getPreviousNode($currentUnit, $visitableIterator));
         //Next and previous units are needed for navigation buttons
         $smarty->assign("T_PARENT_LIST", $currentContent->getNodeAncestors($currentUnit));
         //Parents are needed for printing the title
         $smarty->assign("T_SHOW_TOOLS", true);
         //Tools is the right upper corner table box, that lists tools such as 'upload files', 'copy content' etc
         if (EfrontUser::isOptionVisible('comments')) {
             $smarty->assign("T_COMMENTS", comments::getComments($_SESSION['s_lessons_ID'], false, $currentUnit['id']));
             //Retrieve any comments regarding this unit
         }
         $smarty->assign("T_SHOW_TOOLS", true);
         //Tools is the right upper corner table box, that lists tools such as 'upload files', 'copy content' etc
     } else {
         $showTest = new EfrontTest($_GET['show_test']);
     }
     $smarty->assign("T_CURRENT_TEST", $showTest);
     if (isset($_GET['print'])) {
         $printTest = $showTest;