/**
  * Return information about the answer
  * @param string $userAnswer the text of the answer of the question
  * @param bool   $isStudentAnswer true if it's a student answer false the empty question model
  *
  * @return array of information about the answer
  */
 public static function getAnswerInfo($userAnswer = "", $isStudentAnswer = false)
 {
     $listAnswerResults = array();
     $listAnswerResults['text'] = "";
     $listAnswerResults['wordsCount'] = 0;
     $listAnswerResults['tabwordsbracket'] = array();
     $listAnswerResults['tabwords'] = array();
     $listAnswerResults['tabweighting'] = array();
     $listAnswerResults['tabinputsize'] = array();
     $listAnswerResults['switchable'] = "";
     $listAnswerResults['studentanswer'] = array();
     $listAnswerResults['studentscore'] = array();
     $listAnswerResults['blankseparatornumber'] = 0;
     $listDoubleColon = array();
     api_preg_match("/(.*)::(.*)\$/s", $userAnswer, $listResult);
     if (count($listResult) < 2) {
         $listDoubleColon[] = '';
         $listDoubleColon[] = '';
     } else {
         $listDoubleColon[] = $listResult[1];
         $listDoubleColon[] = $listResult[2];
     }
     $listAnswerResults['systemstring'] = $listDoubleColon[1];
     // make sure we only take the last bit to find special marks
     $listArobaseSplit = explode('@', $listDoubleColon[1]);
     if (count($listArobaseSplit) < 2) {
         $listArobaseSplit[1] = "";
     }
     // take the complete string except after the last '::'
     $listDetails = explode(":", $listArobaseSplit[0]);
     // < number of item after the ::[score]:[size]:[separator_id]@ , here there are 3
     if (count($listDetails) < 3) {
         $listWeightings = explode(',', $listDetails[0]);
         $listSizeOfInput = array();
         for ($i = 0; $i < count($listWeightings); $i++) {
             $listSizeOfInput[] = 200;
         }
         $blankSeparatorNumber = 0;
         // 0 is [...]
     } else {
         $listWeightings = explode(',', $listDetails[0]);
         $listSizeOfInput = explode(',', $listDetails[1]);
         $blankSeparatorNumber = $listDetails[2];
     }
     $listAnswerResults['text'] = $listDoubleColon[0];
     $listAnswerResults['tabweighting'] = $listWeightings;
     $listAnswerResults['tabinputsize'] = $listSizeOfInput;
     $listAnswerResults['switchable'] = $listArobaseSplit[1];
     $listAnswerResults['blankseparatorstart'] = self::getStartSeparator($blankSeparatorNumber);
     $listAnswerResults['blankseparatorend'] = self::getEndSeparator($blankSeparatorNumber);
     $listAnswerResults['blankseparatornumber'] = $blankSeparatorNumber;
     $blankCharStart = self::getStartSeparator($blankSeparatorNumber);
     $blankCharEnd = self::getEndSeparator($blankSeparatorNumber);
     $blankCharStartForRegexp = self::escapeForRegexp($blankCharStart);
     $blankCharEndForRegexp = self::escapeForRegexp($blankCharEnd);
     // get all blanks words
     $listAnswerResults['wordsCount'] = api_preg_match_all('/' . $blankCharStartForRegexp . '[^' . $blankCharEndForRegexp . ']*' . $blankCharEndForRegexp . '/', $listDoubleColon[0], $listWords);
     if ($listAnswerResults['wordsCount'] > 0) {
         $listAnswerResults['tabwordsbracket'] = $listWords[0];
         // remove [ and ] in string
         array_walk($listWords[0], function (&$value, $key, $tabBlankChar) {
             $trimChars = "";
             for ($i = 0; $i < count($tabBlankChar); $i++) {
                 $trimChars .= $tabBlankChar[$i];
             }
             $value = trim($value, $trimChars);
         }, array($blankCharStart, $blankCharEnd));
         $listAnswerResults['tabwords'] = $listWords[0];
     }
     // get all common words
     $commonWords = api_preg_replace('/' . $blankCharStartForRegexp . '[^' . $blankCharEndForRegexp . ']*' . $blankCharEndForRegexp . '/', "::", $listDoubleColon[0]);
     // if student answer, the second [] is the student answer,
     // the third is if student scored or not
     $listBrackets = array();
     $listWords = array();
     if ($isStudentAnswer) {
         for ($i = 0; $i < count($listAnswerResults['tabwords']); $i++) {
             $listBrackets[] = $listAnswerResults['tabwordsbracket'][$i];
             $listWords[] = $listAnswerResults['tabwords'][$i];
             if ($i + 1 < count($listAnswerResults['tabwords'])) {
                 // should always be
                 $i++;
             }
             $listAnswerResults['studentanswer'][] = $listAnswerResults['tabwords'][$i];
             if ($i + 1 < count($listAnswerResults['tabwords'])) {
                 // should always be
                 $i++;
             }
             $listAnswerResults['studentscore'][] = $listAnswerResults['tabwords'][$i];
         }
         $listAnswerResults['tabwords'] = $listWords;
         $listAnswerResults['tabwordsbracket'] = $listBrackets;
         // if we are in student view, we've got 3 times :::::: for common words
         $commonWords = api_preg_replace("/::::::/", "::", $commonWords);
     }
     $listAnswerResults['commonwords'] = explode("::", $commonWords);
     return $listAnswerResults;
 }
 /**
  * Escape MySQL wildchars _ and % in LIKE search
  * @param string $text            The string to escape
  *
  * @return string           The escaped string
  */
 public static function escape_sql_wildcards($text)
 {
     $text = api_preg_replace("/_/", "\\_", $text);
     $text = api_preg_replace("/%/", "\\%", $text);
     return $text;
 }
 /**
  * @param string $file
  * @param bool $updatesession options:
  * true: if the session exists it will be updated
  * false: if session exists a new session will be created adding a counter session1, session2, etc
  * @param int $user_id
  * @param $logger
  * @param array convert a file row to an extra field. Example in CSV file there's a SessionID then it will
  * converted to extra_external_session_id if you set this: array('SessionId' => 'extra_external_session_id')
  * @param array extra fields
  * @param string extra field id
  * @param int $daysCoachAccessBeforeBeginning
  * @param int $daysCoachAccessAfterBeginning
  * @param int $sessionVisibility
  * @return array
  */
 static function importCSV($file, $updatesession, $defaultUserId = null, $logger = null, $extraFields = array(), $extraFieldId = null, $daysCoachAccessBeforeBeginning = null, $daysCoachAccessAfterBeginning = null, $sessionVisibility = 1, $fieldsToAvoidUpdate = array())
 {
     $content = file($file);
     $error_message = null;
     $session_counter = 0;
     if (empty($defaultUserId)) {
         $defaultUserId = api_get_user_id();
     }
     $eol = PHP_EOL;
     if (PHP_SAPI != 'cli') {
         $eol = '<br />';
     }
     $debug = false;
     if (isset($logger)) {
         $debug = true;
     }
     $extraParameters = null;
     if (!empty($daysCoachAccessBeforeBeginning) && !empty($daysCoachAccessAfterBeginning)) {
         $extraParameters .= ' , nb_days_access_before_beginning = ' . intval($daysCoachAccessBeforeBeginning);
         $extraParameters .= ' , nb_days_access_after_end = ' . intval($daysCoachAccessAfterBeginning);
     }
     $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
     $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
     $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
     $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
     $sessions = array();
     if (!api_strstr($content[0], ';')) {
         $error_message = get_lang('NotCSV');
     } else {
         $tag_names = array();
         foreach ($content as $key => $enreg) {
             $enreg = explode(';', trim($enreg));
             if ($key) {
                 foreach ($tag_names as $tag_key => $tag_name) {
                     $sessions[$key - 1][$tag_name] = $enreg[$tag_key];
                 }
             } else {
                 foreach ($enreg as $tag_name) {
                     $tag_names[] = api_preg_replace('/[^a-zA-Z0-9_\\-]/', '', $tag_name);
                 }
                 if (!in_array('SessionName', $tag_names) || !in_array('DateStart', $tag_names) || !in_array('DateEnd', $tag_names)) {
                     $error_message = get_lang('NoNeededData');
                     break;
                 }
             }
         }
         // Looping the sessions.
         foreach ($sessions as $enreg) {
             $user_counter = 0;
             $course_counter = 0;
             if (isset($extraFields) && !empty($extraFields)) {
                 foreach ($extraFields as $original => $to) {
                     $enreg[$to] = $enreg[$original];
                 }
             }
             $session_name = Database::escape_string($enreg['SessionName']);
             $date_start = $enreg['DateStart'];
             $date_end = $enreg['DateEnd'];
             $visibility = isset($enreg['Visibility']) ? $enreg['Visibility'] : $sessionVisibility;
             $session_category_id = isset($enreg['SessionCategory']) ? $enreg['SessionCategory'] : null;
             // Searching a general coach.
             if (!empty($enreg['Coach'])) {
                 $coach_id = UserManager::get_user_id_from_username($enreg['Coach']);
                 if ($coach_id === false) {
                     // If the coach-user does not exist - I'm the coach.
                     $coach_id = $defaultUserId;
                 }
             } else {
                 $coach_id = $defaultUserId;
             }
             if (!$updatesession) {
                 // Always create a session.
                 $unique_name = false;
                 // This MUST be initializead.
                 $i = 0;
                 // Change session name, verify that session doesn't exist.
                 $suffix = null;
                 while (!$unique_name) {
                     if ($i > 1) {
                         $suffix = ' - ' . $i;
                     }
                     $sql = 'SELECT 1 FROM ' . $tbl_session . ' WHERE name="' . $session_name . $suffix . '"';
                     $rs = Database::query($sql);
                     if (Database::result($rs, 0, 0)) {
                         $i++;
                     } else {
                         $unique_name = true;
                         $session_name .= $suffix;
                     }
                 }
                 // Creating the session.
                 /*$sql_session = "INSERT IGNORE INTO $tbl_session SET
                           name = '".$session_name."',
                           id_coach = '$coach_id',
                           date_start = '$date_start',
                           date_end = '$date_end',
                           visibility = '$visibility',
                           session_category_id = '$session_category_id',
                           session_admin_id=".intval($defaultUserId).$extraParameters;
                   Database::query($sql_session);*/
                 $params = array('id_coach' => $coach_id, 'visibility' => $visibility, 'name' => $session_name, 'access_start_date' => $date_start, 'access_end_date' => $date_end, 'session_category_id' => $session_category_id, 'session_admin_id' => $defaultUserId);
                 $session_id = SessionManager::add($params);
                 if ($debug) {
                     if ($session_id) {
                         foreach ($enreg as $key => $value) {
                             if (substr($key, 0, 6) == 'extra_') {
                                 //an extra field
                                 self::update_session_extra_field_value($session_id, substr($key, 6), $value);
                             }
                         }
                         $logger->addInfo("Sessions - Session created: #{$session_id} - {$session_name}");
                     } else {
                         $logger->addError("Sessions - Session NOT created: {$session_name}");
                     }
                 }
                 $session_counter++;
             } else {
                 $sessionId = null;
                 if (isset($extraFields) && !empty($extraFields)) {
                     $sessionId = self::get_session_id_from_original_id($enreg['extra_' . $extraFieldId], $extraFieldId);
                     if (empty($sessionId)) {
                         $my_session_result = false;
                     } else {
                         $my_session_result = true;
                     }
                 } else {
                     $my_session_result = self::get_session_by_name($enreg['SessionName']);
                 }
                 if ($my_session_result === false) {
                     // Creating a session.
                     /*$sql_session = "INSERT IGNORE INTO $tbl_session SET
                       name = '$session_name',
                       id_coach = '$coach_id',
                       date_start = '$date_start',
                       date_end = '$date_end',
                       visibility = '$visibility',
                       session_category_id = '$session_category_id' ".$extraParameters;*/
                     $params = array('id_coach' => $coach_id, 'visibility' => $visibility, 'name' => $session_name, 'access_start_date' => $date_start, 'access_end_date' => $date_end, 'session_category_id' => $session_category_id, 'session_admin_id' => $defaultUserId);
                     $session_id = SessionManager::add($params);
                     // We get the last insert id.
                     /*$my_session_result = SessionManager::get_session_by_name($enreg['SessionName']);
                       $session_id = $my_session_result['id'];*/
                     if ($debug) {
                         if ($session_id) {
                             foreach ($enreg as $key => $value) {
                                 if (substr($key, 0, 6) == 'extra_') {
                                     //an extra field
                                     self::update_session_extra_field_value($session_id, substr($key, 6), $value);
                                 }
                             }
                             $logger->addInfo("Sessions - #{$session_id} created: {$session_name}");
                         } else {
                             $logger->addError("Sessions - Session NOT created: {$session_name}");
                         }
                     }
                 } else {
                     $params = array('id_coach' => $coach_id, 'date_start' => $date_start, 'date_end' => $date_end, 'visibility' => $visibility, 'session_category_id' => $session_category_id);
                     if (!empty($fieldsToAvoidUpdate)) {
                         foreach ($fieldsToAvoidUpdate as $field) {
                             unset($params[$field]);
                         }
                     }
                     if (isset($sessionId) && !empty($sessionId)) {
                         // The session already exists, update it then.
                         Database::update($tbl_session, $params, array('id = ?' => $sessionId));
                         $session_id = $sessionId;
                     } else {
                         Database::update($tbl_session, $params, array("name = '?' " => $enreg['SessionName']));
                         $row = Database::query("SELECT id FROM {$tbl_session} WHERE name = '{$session_name}'");
                         list($session_id) = Database::fetch_array($row);
                     }
                     foreach ($enreg as $key => $value) {
                         if (substr($key, 0, 6) == 'extra_') {
                             //an extra field
                             self::update_session_extra_field_value($session_id, substr($key, 6), $value);
                         }
                     }
                     Database::query("DELETE FROM {$tbl_session_user} WHERE id_session='{$session_id}'");
                     Database::query("DELETE FROM {$tbl_session_course} WHERE id_session='{$session_id}'");
                     Database::query("DELETE FROM {$tbl_session_course_user} WHERE id_session='{$session_id}'");
                 }
                 $session_counter++;
             }
             $users = explode('|', $enreg['Users']);
             // Adding the relationship "Session - User".
             if (is_array($users)) {
                 foreach ($users as $user) {
                     $user_id = UserManager::get_user_id_from_username($user);
                     if ($user_id !== false) {
                         // Insert new users.
                         $sql = "INSERT IGNORE INTO {$tbl_session_user} SET\n                                    id_user = '******',\n                                    id_session = '{$session_id}'";
                         Database::query($sql);
                         if ($debug) {
                             $logger->addInfo("Sessions - Adding User #{$user_id} ({$user}) to session #{$session_id}");
                         }
                         $user_counter++;
                     }
                 }
             }
             $courses = explode('|', $enreg['Courses']);
             foreach ($courses as $course) {
                 $course_code = api_strtoupper(api_substr($course, 0, api_strpos($course, '[')));
                 if (CourseManager::course_exists($course_code)) {
                     $courseInfo = api_get_course_info($course_code);
                     $courseId = $courseInfo['real_id'];
                     // Adding the course to a session.
                     $sql_course = "INSERT IGNORE INTO {$tbl_session_course}\n                                       SET c_id = '" . $courseId . "', id_session = '{$session_id}'";
                     Database::query($sql_course);
                     if ($debug) {
                         $logger->addInfo("Sessions - Adding course '{$course_code}' to session #{$session_id}");
                     }
                     $course_counter++;
                     $pattern = "/\\[(.*?)\\]/";
                     preg_match_all($pattern, $course, $matches);
                     if (isset($matches[1])) {
                         $course_coaches = $matches[1][0];
                         $course_users = $matches[1][1];
                     }
                     $course_users = explode(',', $course_users);
                     $course_coaches = explode(',', $course_coaches);
                     // Adding coaches to session course user
                     if (!empty($course_coaches)) {
                         foreach ($course_coaches as $course_coach) {
                             $coach_id = UserManager::get_user_id_from_username($course_coach);
                             if ($coach_id !== false) {
                                 $sql = "INSERT IGNORE INTO {$tbl_session_course_user} SET\n                                            id_user='******',\n                                            c_id ='{$courseId}',\n                                            id_session = '{$session_id}',\n                                            status = 2 ";
                                 Database::query($sql);
                                 if ($debug) {
                                     $logger->addInfo("Sessions - Adding course coach: user #{$coach_id} ({$course_coach}) to course: '{$course_code}' and session #{$session_id}");
                                 }
                             } else {
                                 $error_message .= get_lang('UserDoesNotExist') . ' : ' . $course_coach . $eol;
                             }
                         }
                     }
                     $users_in_course_counter = 0;
                     // Adding the relationship "Session - Course - User".
                     foreach ($course_users as $user) {
                         $user_id = UserManager::get_user_id_from_username($user);
                         if ($user_id !== false) {
                             $sql = "INSERT IGNORE INTO {$tbl_session_course_user} SET\n                                        id_user='******',\n                                        c_id = '{$courseId}',\n                                        id_session = '{$session_id}'";
                             Database::query($sql);
                             if ($debug) {
                                 $logger->addInfo("Sessions - Adding student: user #{$user_id} ({$user}) to course: '{$course_code}' and session #{$session_id}");
                             }
                             $users_in_course_counter++;
                         } else {
                             $error_message .= get_lang('UserDoesNotExist') . ': ' . $user . $eol;
                         }
                     }
                     $sql = "UPDATE {$tbl_session_course} SET nbr_users='{$users_in_course_counter}' WHERE c_id ='{$courseId}'";
                     Database::query($sql);
                     $course_info = CourseManager::get_course_information($course_code);
                     $inserted_in_course[$course_code] = $course_info['title'];
                 }
             }
             $access_url_id = api_get_current_access_url_id();
             UrlManager::add_session_to_url($session_id, $access_url_id);
             $sql_update_users = "UPDATE {$tbl_session} SET nbr_users ='{$user_counter}', nbr_courses='{$course_counter}' WHERE id='{$session_id}'";
             Database::query($sql_update_users);
         }
     }
     return array('error_message' => $error_message, 'session_counter' => $session_counter);
 }
 /**
  * Updates the session synchronize with the csv file.
  * @param string $file
  */
 private function importSessionsStatic($file)
 {
     $content = file($file);
     $sessions = array();
     if (!api_strstr($content[0], ';')) {
         $error_message = get_lang('NotCSV');
     } else {
         $tag_names = array();
         foreach ($content as $key => $enreg) {
             $enreg = explode(';', trim($enreg));
             if ($key) {
                 foreach ($tag_names as $tag_key => $tag_name) {
                     $sessions[$key - 1][$tag_name] = $enreg[$tag_key];
                 }
             } else {
                 foreach ($enreg as $tag_name) {
                     $tag_names[] = api_preg_replace('/[^a-zA-Z0-9_\\-]/', '', $tag_name);
                 }
                 if (!in_array('SessionName', $tag_names) || !in_array('DateStart', $tag_names) || !in_array('DateEnd', $tag_names)) {
                     $error_message = get_lang('NoNeededData');
                     break;
                 }
             }
         }
     }
     if (!empty($sessions)) {
         // Looping the sessions.
         foreach ($sessions as $session) {
             if (!empty($session['SessionID'])) {
                 $sessionId = SessionManager::get_session_id_from_original_id($session['SessionID'], $this->extraFieldIdNameList['session']);
                 $coachUserName = isset($session['Coach']) ? $session['Coach'] : null;
                 $categoryId = isset($session['category_id']) ? $session['category_id'] : null;
                 // 2014-06-30
                 $dateStart = explode('/', $session['DateStart']);
                 $dateEnd = explode('/', $session['DateEnd']);
                 $visibility = $this->defaultSessionVisibility;
                 $coachId = null;
                 if (!empty($coachUserName)) {
                     $coachInfo = api_get_user_info_from_username($coachUserName);
                     $coachId = $coachInfo['user_id'];
                 }
                 if (empty($sessionId)) {
                     $result = SessionManager::create_session($session['SessionName'], $dateStart[0], $dateStart[1], $dateStart[2], $dateEnd[0], $dateEnd[1], $dateEnd[2], $this->daysCoachAccessBeforeBeginning, $this->daysCoachAccessAfterBeginning, null, $coachUserName, $categoryId, $visibility, 1);
                     if (is_numeric($result)) {
                         $sessionId = $result;
                         SessionManager::update_session_extra_field_value($sessionId, $this->extraFieldIdNameList['session'], $session['SessionID']);
                     }
                 } else {
                     $sessionInfo = api_get_session_info($sessionId);
                     $accessBefore = null;
                     $accessAfter = null;
                     if (empty($sessionInfo['nb_days_access_before_beginning']) || !empty($sessionInfo['nb_days_access_before_beginning']) && $sessionInfo['nb_days_access_before_beginning'] < $this->daysCoachAccessBeforeBeginning) {
                         $accessBefore = intval($this->daysCoachAccessBeforeBeginning);
                     }
                     $accessAfter = null;
                     if (empty($sessionInfo['nb_days_access_after_end']) || !empty($sessionInfo['nb_days_access_after_end']) && $sessionInfo['nb_days_access_after_end'] < $this->daysCoachAccessAfterBeginning) {
                         $accessAfter = intval($this->daysCoachAccessAfterBeginning);
                     }
                     $showDescription = isset($sessionInfo['show_description']) ? $sessionInfo['show_description'] : 1;
                     $result = SessionManager::edit_session($sessionId, $session['SessionName'], $dateStart[0], $dateStart[1], $dateStart[2], $dateEnd[0], $dateEnd[1], $dateEnd[2], $accessBefore, $accessAfter, null, $coachId, $categoryId, $visibility, true, true, null, $showDescription);
                     if (is_numeric($result)) {
                         $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
                         $params = array('description' => $session['SessionDescription']);
                         Database::update($tbl_session, $params, array('id = ?' => $sessionId));
                     }
                 }
                 // Courses
                 $courses = explode('|', $session['Courses']);
                 $courseList = array();
                 foreach ($courses as $course) {
                     $courseArray = bracketsToArray($course);
                     $courseCode = $courseArray[0];
                     if (CourseManager::course_exists($courseCode)) {
                         $courseList[] = $courseCode;
                     }
                 }
                 SessionManager::add_courses_to_session($sessionId, $courseList, true);
                 if (!empty($sessionId)) {
                     $courses = explode('|', $session['Courses']);
                     foreach ($courses as $course) {
                         $courseArray = bracketsToArray($course);
                         $courseCode = $courseArray[0];
                         if (CourseManager::course_exists($courseCode)) {
                             // Coaches
                             $courseCoaches = isset($courseArray[1]) ? $courseArray[1] : null;
                             $courseCoaches = explode(',', $courseCoaches);
                             if (!empty($courseCoaches)) {
                                 $coachList = array();
                                 foreach ($courseCoaches as $courseCoach) {
                                     $courseCoachId = UserManager::get_user_id_from_username($courseCoach);
                                     if ($courseCoachId !== false) {
                                         // Just insert new coaches
                                         $coachList[] = $courseCoachId;
                                     }
                                 }
                                 SessionManager::updateCoaches($sessionId, $courseCode, $coachList, true);
                             }
                             // Students
                             $courseUsers = isset($courseArray[2]) ? $courseArray[2] : null;
                             $courseUsers = explode(',', $courseUsers);
                             if (!empty($courseUsers)) {
                                 $userList = array();
                                 foreach ($courseUsers as $username) {
                                     $userInfo = api_get_user_info_from_username(trim($username));
                                     if (!empty($userInfo)) {
                                         $userList[] = $userInfo['user_id'];
                                     }
                                 }
                                 SessionManager::subscribe_users_to_session_course($userList, $sessionId, $courseCode, SESSION_VISIBLE_READ_ONLY, true);
                             } else {
                                 $this->logger->addInfo("No users to register.");
                             }
                         } else {
                             $this->logger->addInfo("Course does not exists {$courseCode}");
                         }
                     }
                 } else {
                     $this->logger->addInfo('SessionID not found in system.');
                 }
             } else {
                 $this->logger->addInfo('SessionID does not exists');
             }
         }
     } else {
         $this->logger->addInfo($error_message);
     }
 }
    /**
     * Shows a question
     *
     * @param int    $questionId question id
     * @param bool   $only_questions if true only show the questions, no exercise title
     * @param bool   $origin  i.e = learnpath
     * @param string $current_item current item from the list of questions
     * @param bool   $show_title
     * @param bool   $freeze
     * @param array  $user_choice
     * @param bool   $show_comment
     * @param bool   $exercise_feedback
     * @param bool   $show_answers
     * */
    public static function showQuestion($questionId, $only_questions = false, $origin = false, $current_item = '', $show_title = true, $freeze = false, $user_choice = array(), $show_comment = false, $exercise_feedback = null, $show_answers = false)
    {
        $course_id = api_get_course_int_id();
        // Change false to true in the following line to enable answer hinting
        $debug_mark_answer = $show_answers;
        // Reads question information
        if (!($objQuestionTmp = Question::read($questionId))) {
            // Question not found
            return false;
        }
        if ($exercise_feedback != EXERCISE_FEEDBACK_TYPE_END) {
            $show_comment = false;
        }
        $answerType = $objQuestionTmp->selectType();
        $pictureName = $objQuestionTmp->selectPicture();
        $s = '';
        if ($answerType != HOT_SPOT && $answerType != HOT_SPOT_DELINEATION) {
            // Question is not a hotspot
            if (!$only_questions) {
                $questionDescription = $objQuestionTmp->selectDescription();
                if ($show_title) {
                    TestCategory::displayCategoryAndTitle($objQuestionTmp->id);
                    echo Display::div($current_item . '. ' . $objQuestionTmp->selectTitle(), array('class' => 'question_title'));
                }
                if (!empty($questionDescription)) {
                    echo Display::div($questionDescription, array('class' => 'question_description'));
                }
            }
            if (in_array($answerType, array(FREE_ANSWER, ORAL_EXPRESSION)) && $freeze) {
                return '';
            }
            echo '<div class="question_options row">';
            // construction of the Answer object (also gets all answers details)
            $objAnswerTmp = new Answer($questionId);
            $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
            $quiz_question_options = Question::readQuestionOption($questionId, $course_id);
            // For "matching" type here, we need something a little bit special
            // because the match between the suggestions and the answers cannot be
            // done easily (suggestions and answers are in the same table), so we
            // have to go through answers first (elems with "correct" value to 0).
            $select_items = array();
            //This will contain the number of answers on the left side. We call them
            // suggestions here, for the sake of comprehensions, while the ones
            // on the right side are called answers
            $num_suggestions = 0;
            if (in_array($answerType, [MATCHING, DRAGGABLE, MATCHING_DRAGGABLE])) {
                if ($answerType == DRAGGABLE) {
                    $s .= '<div class="col-md-12 ui-widget ui-helper-clearfix">
                        <div class="clearfix">
                        <ul class="exercise-draggable-answer ui-helper-reset ui-helper-clearfix">';
                } else {
                    $s .= <<<HTML
                        <div id="drag{$questionId}_question" class="drag_question">
                            <table class="data_table">
HTML;
                }
                // Iterate through answers
                $x = 1;
                //mark letters for each answer
                $letter = 'A';
                $answer_matching = array();
                $cpt1 = array();
                for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
                    $answerCorrect = $objAnswerTmp->isCorrect($answerId);
                    $numAnswer = $objAnswerTmp->selectAutoId($answerId);
                    if ($answerCorrect == 0) {
                        // options (A, B, C, ...) that will be put into the list-box
                        // have the "correct" field set to 0 because they are answer
                        $cpt1[$x] = $letter;
                        $answer_matching[$x] = $objAnswerTmp->selectAnswerByAutoId($numAnswer);
                        $x++;
                        $letter++;
                    }
                }
                $i = 1;
                $select_items[0]['id'] = 0;
                $select_items[0]['letter'] = '--';
                $select_items[0]['answer'] = '';
                foreach ($answer_matching as $id => $value) {
                    $select_items[$i]['id'] = $value['id'];
                    $select_items[$i]['letter'] = $cpt1[$id];
                    $select_items[$i]['answer'] = $value['answer'];
                    $i++;
                }
                $user_choice_array_position = array();
                if (!empty($user_choice)) {
                    foreach ($user_choice as $item) {
                        $user_choice_array_position[$item['position']] = $item['answer'];
                    }
                }
                $num_suggestions = $nbrAnswers - $x + 1;
            } elseif ($answerType == FREE_ANSWER) {
                $fck_content = isset($user_choice[0]) && !empty($user_choice[0]['answer']) ? $user_choice[0]['answer'] : null;
                $form = new FormValidator('free_choice_' . $questionId);
                $config = array('ToolbarSet' => 'TestFreeAnswer');
                $form->addHtmlEditor("choice[" . $questionId . "]", null, false, false, $config);
                $form->setDefaults(array("choice[" . $questionId . "]" => $fck_content));
                $s .= $form->returnForm();
            } elseif ($answerType == ORAL_EXPRESSION) {
                // Add nanog
                if (api_get_setting('enable_nanogong') == 'true') {
                    //@todo pass this as a parameter
                    global $exercise_stat_info, $exerciseId, $exe_id;
                    if (!empty($exercise_stat_info)) {
                        $params = array('exercise_id' => $exercise_stat_info['exe_exo_id'], 'exe_id' => $exercise_stat_info['exe_id'], 'question_id' => $questionId);
                    } else {
                        $params = array('exercise_id' => $exerciseId, 'exe_id' => 'temp_exe', 'question_id' => $questionId);
                    }
                    $nano = new Nanogong($params);
                    echo $nano->show_button();
                }
                $form = new FormValidator('free_choice_' . $questionId);
                $config = array('ToolbarSet' => 'TestFreeAnswer');
                $form->addHtmlEditor("choice[" . $questionId . "]", null, false, false, $config);
                //$form->setDefaults(array("choice[" . $questionId . "]" => $fck_content));
                $s .= $form->return_form();
            }
            // Now navigate through the possible answers, using the max number of
            // answers for the question as a limiter
            $lines_count = 1;
            // a counter for matching-type answers
            if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE || $answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
                $header = Display::tag('th', get_lang('Options'));
                foreach ($objQuestionTmp->options as $item) {
                    if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
                        if (in_array($item, $objQuestionTmp->options)) {
                            $header .= Display::tag('th', get_lang($item));
                        } else {
                            $header .= Display::tag('th', $item);
                        }
                    } else {
                        $header .= Display::tag('th', $item);
                    }
                }
                if ($show_comment) {
                    $header .= Display::tag('th', get_lang('Feedback'));
                }
                $s .= '<table class="table table-hover table-striped">';
                $s .= Display::tag('tr', $header, array('style' => 'text-align:left;'));
            }
            if ($show_comment) {
                if (in_array($answerType, array(MULTIPLE_ANSWER, MULTIPLE_ANSWER_COMBINATION, UNIQUE_ANSWER, UNIQUE_ANSWER_IMAGE, UNIQUE_ANSWER_NO_OPTION, GLOBAL_MULTIPLE_ANSWER))) {
                    $header = Display::tag('th', get_lang('Options'));
                    if ($exercise_feedback == EXERCISE_FEEDBACK_TYPE_END) {
                        $header .= Display::tag('th', get_lang('Feedback'));
                    }
                    $s .= '<table class="table table-hover table-striped">';
                    $s .= Display::tag('tr', $header, array('style' => 'text-align:left;'));
                }
            }
            $matching_correct_answer = 0;
            $user_choice_array = array();
            if (!empty($user_choice)) {
                foreach ($user_choice as $item) {
                    $user_choice_array[] = $item['answer'];
                }
            }
            for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
                $answer = $objAnswerTmp->selectAnswer($answerId);
                $answerCorrect = $objAnswerTmp->isCorrect($answerId);
                $numAnswer = $objAnswerTmp->selectAutoId($answerId);
                $comment = $objAnswerTmp->selectComment($answerId);
                $attributes = array();
                // Unique answer
                if (in_array($answerType, [UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION, UNIQUE_ANSWER_IMAGE])) {
                    $input_id = 'choice-' . $questionId . '-' . $answerId;
                    if (isset($user_choice[0]['answer']) && $user_choice[0]['answer'] == $numAnswer) {
                        $attributes = array('id' => $input_id, 'checked' => 1, 'selected' => 1);
                    } else {
                        $attributes = array('id' => $input_id);
                    }
                    if ($debug_mark_answer) {
                        if ($answerCorrect) {
                            $attributes['checked'] = 1;
                            $attributes['selected'] = 1;
                        }
                    }
                    if ($show_comment) {
                        $s .= '<tr><td>';
                    }
                    if ($answerType == UNIQUE_ANSWER_IMAGE) {
                        if ($show_comment) {
                            if (empty($comment)) {
                                $s .= '<div id="answer' . $questionId . $numAnswer . '" ' . 'class="exercise-unique-answer-image" style="text-align: center">';
                            } else {
                                $s .= '<div id="answer' . $questionId . $numAnswer . '" ' . 'class="exercise-unique-answer-image col-xs-6 col-sm-12" style="text-align: center">';
                            }
                        } else {
                            $s .= '<div id="answer' . $questionId . $numAnswer . '" ' . 'class="exercise-unique-answer-image col-xs-6 col-md-3" style="text-align: center">';
                        }
                    }
                    $answer = Security::remove_XSS($answer, STUDENT);
                    $s .= Display::input('hidden', 'choice2[' . $questionId . ']', '0');
                    $answer_input = null;
                    if ($answerType == UNIQUE_ANSWER_IMAGE) {
                        $attributes['style'] = 'display: none;';
                        $answer = '<div class="thumbnail">' . $answer . '</div>';
                    }
                    $answer_input .= '<label class="radio">';
                    $answer_input .= Display::input('radio', 'choice[' . $questionId . ']', $numAnswer, $attributes);
                    $answer_input .= $answer;
                    $answer_input .= '</label>';
                    if ($answerType == UNIQUE_ANSWER_IMAGE) {
                        $answer_input .= "</div>";
                    }
                    if ($show_comment) {
                        $s .= $answer_input;
                        $s .= '</td>';
                        $s .= '<td>';
                        $s .= $comment;
                        $s .= '</td>';
                        $s .= '</tr>';
                    } else {
                        $s .= $answer_input;
                    }
                } elseif ($answerType == MULTIPLE_ANSWER || $answerType == MULTIPLE_ANSWER_TRUE_FALSE || $answerType == GLOBAL_MULTIPLE_ANSWER) {
                    $input_id = 'choice-' . $questionId . '-' . $answerId;
                    $answer = Security::remove_XSS($answer, STUDENT);
                    if (in_array($numAnswer, $user_choice_array)) {
                        $attributes = array('id' => $input_id, 'checked' => 1, 'selected' => 1);
                    } else {
                        $attributes = array('id' => $input_id);
                    }
                    if ($debug_mark_answer) {
                        if ($answerCorrect) {
                            $attributes['checked'] = 1;
                            $attributes['selected'] = 1;
                        }
                    }
                    if ($answerType == MULTIPLE_ANSWER || $answerType == GLOBAL_MULTIPLE_ANSWER) {
                        $s .= '<input type="hidden" name="choice2[' . $questionId . ']" value="0" />';
                        $answer_input = '<label class="checkbox">';
                        $answer_input .= Display::input('checkbox', 'choice[' . $questionId . '][' . $numAnswer . ']', $numAnswer, $attributes);
                        $answer_input .= $answer;
                        $answer_input .= '</label>';
                        if ($show_comment) {
                            $s .= '<tr><td>';
                            $s .= $answer_input;
                            $s .= '</td>';
                            $s .= '<td>';
                            $s .= $comment;
                            $s .= '</td>';
                            $s .= '</tr>';
                        } else {
                            $s .= $answer_input;
                        }
                    } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
                        $my_choice = array();
                        if (!empty($user_choice_array)) {
                            foreach ($user_choice_array as $item) {
                                $item = explode(':', $item);
                                $my_choice[$item[0]] = $item[1];
                            }
                        }
                        $s .= '<tr>';
                        $s .= Display::tag('td', $answer);
                        if (!empty($quiz_question_options)) {
                            foreach ($quiz_question_options as $id => $item) {
                                if (isset($my_choice[$numAnswer]) && $id == $my_choice[$numAnswer]) {
                                    $attributes = array('checked' => 1, 'selected' => 1);
                                } else {
                                    $attributes = array();
                                }
                                if ($debug_mark_answer) {
                                    if ($id == $answerCorrect) {
                                        $attributes['checked'] = 1;
                                        $attributes['selected'] = 1;
                                    }
                                }
                                $s .= Display::tag('td', Display::input('radio', 'choice[' . $questionId . '][' . $numAnswer . ']', $id, $attributes), array('style' => ''));
                            }
                        }
                        if ($show_comment) {
                            $s .= '<td>';
                            $s .= $comment;
                            $s .= '</td>';
                        }
                        $s .= '</tr>';
                    }
                } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION) {
                    // multiple answers
                    $input_id = 'choice-' . $questionId . '-' . $answerId;
                    if (in_array($numAnswer, $user_choice_array)) {
                        $attributes = array('id' => $input_id, 'checked' => 1, 'selected' => 1);
                    } else {
                        $attributes = array('id' => $input_id);
                    }
                    if ($debug_mark_answer) {
                        if ($answerCorrect) {
                            $attributes['checked'] = 1;
                            $attributes['selected'] = 1;
                        }
                    }
                    $answer = Security::remove_XSS($answer, STUDENT);
                    $answer_input = '<input type="hidden" name="choice2[' . $questionId . ']" value="0" />';
                    $answer_input .= '<label class="checkbox">';
                    $answer_input .= Display::input('checkbox', 'choice[' . $questionId . '][' . $numAnswer . ']', 1, $attributes);
                    $answer_input .= $answer;
                    $answer_input .= '</label>';
                    if ($show_comment) {
                        $s .= '<tr>';
                        $s .= '<td>';
                        $s .= $answer_input;
                        $s .= '</td>';
                        $s .= '<td>';
                        $s .= $comment;
                        $s .= '</td>';
                        $s .= '</tr>';
                    } else {
                        $s .= $answer_input;
                    }
                } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
                    $s .= '<input type="hidden" name="choice2[' . $questionId . ']" value="0" />';
                    $my_choice = array();
                    if (!empty($user_choice_array)) {
                        foreach ($user_choice_array as $item) {
                            $item = explode(':', $item);
                            if (isset($item[1]) && isset($item[0])) {
                                $my_choice[$item[0]] = $item[1];
                            }
                        }
                    }
                    $answer = Security::remove_XSS($answer, STUDENT);
                    $s .= '<tr>';
                    $s .= Display::tag('td', $answer);
                    foreach ($objQuestionTmp->options as $key => $item) {
                        if (isset($my_choice[$numAnswer]) && $key == $my_choice[$numAnswer]) {
                            $attributes = array('checked' => 1, 'selected' => 1);
                        } else {
                            $attributes = array();
                        }
                        if ($debug_mark_answer) {
                            if ($key == $answerCorrect) {
                                $attributes['checked'] = 1;
                                $attributes['selected'] = 1;
                            }
                        }
                        $s .= Display::tag('td', Display::input('radio', 'choice[' . $questionId . '][' . $numAnswer . ']', $key, $attributes));
                    }
                    if ($show_comment) {
                        $s .= '<td>';
                        $s .= $comment;
                        $s .= '</td>';
                    }
                    $s .= '</tr>';
                } elseif ($answerType == FILL_IN_BLANKS) {
                    // display the question, with field empty, for student to fill it,
                    // or filled to display the answer in the Question preview of the exercice/admin.php page
                    $displayForStudent = true;
                    $listAnswerInformations = FillBlanks::getAnswerInfo($answer);
                    $separatorStartRegexp = FillBlanks::escapeForRegexp($listAnswerInformations['blankseparatorstart']);
                    $separatorEndRegexp = FillBlanks::escapeForRegexp($listAnswerInformations['blankseparatorend']);
                    list($answer) = explode('::', $answer);
                    //Correct answers
                    $correctAnswerList = $listAnswerInformations['tabwords'];
                    //Student's answer
                    $studentAnswerList = array();
                    if (isset($user_choice[0]['answer'])) {
                        $arrayStudentAnswer = FillBlanks::getAnswerInfo($user_choice[0]['answer'], true);
                        $studentAnswerList = $arrayStudentAnswer['studentanswer'];
                    }
                    // If the question must be shown with the answer (in page exercice/admin.php) for teacher preview
                    // set the student-answer to the correct answer
                    if ($debug_mark_answer) {
                        $studentAnswerList = $correctAnswerList;
                        $displayForStudent = false;
                    }
                    if (!empty($correctAnswerList) && !empty($studentAnswerList)) {
                        $answer = "";
                        for ($i = 0; $i < count($listAnswerInformations["commonwords"]) - 1; $i++) {
                            // display the common word
                            $answer .= $listAnswerInformations["commonwords"][$i];
                            // display the blank word
                            $correctItem = $listAnswerInformations["tabwords"][$i];
                            $correctItemRegexp = $correctItem;
                            // replace / with \/ to allow the preg_replace bellow and all the regexp char
                            $correctItemRegexp = FillBlanks::getRegexpProtected($correctItemRegexp);
                            if (isset($studentAnswerList[$i])) {
                                // If student already started this test and answered this question,
                                // fill the blank with his previous answers
                                // may be "" if student viewed the question, but did not fill the blanks
                                $correctItem = $studentAnswerList[$i];
                            }
                            $attributes["style"] = "width:" . $listAnswerInformations["tabinputsize"][$i] . "px";
                            $answer .= FillBlanks::getFillTheBlankHtml($separatorStartRegexp, $separatorEndRegexp, $correctItemRegexp, $questionId, $correctItem, $attributes, $answer, $listAnswerInformations, $displayForStudent, $i);
                        }
                        // display the last common word
                        $answer .= $listAnswerInformations["commonwords"][$i];
                    } else {
                        // display empty [input] with the right width for student to fill it
                        $separatorStartRegexp = FillBlanks::escapeForRegexp($listAnswerInformations['blankseparatorstart']);
                        $separatorEndRegexp = FillBlanks::escapeForRegexp($listAnswerInformations['blankseparatorend']);
                        $answer = "";
                        for ($i = 0; $i < count($listAnswerInformations["commonwords"]) - 1; $i++) {
                            // display the common words
                            $answer .= $listAnswerInformations["commonwords"][$i];
                            // display the blank word
                            $attributes["style"] = "width:" . $listAnswerInformations["tabinputsize"][$i] . "px";
                            $correctItem = $listAnswerInformations["tabwords"][$i];
                            $correctItemRegexp = $correctItem;
                            // replace / with \/ to allow the preg_replace bellow and all the regexp char
                            $correctItemRegexp = FillBlanks::getRegexpProtected($correctItemRegexp);
                            $answer .= FillBlanks::getFillTheBlankHtml($separatorStartRegexp, $separatorEndRegexp, $correctItemRegexp, $questionId, '', $attributes, $answer, $listAnswerInformations, $displayForStudent, $i);
                        }
                        // display the last common word
                        $answer .= $listAnswerInformations["commonwords"][$i];
                    }
                    $s .= $answer;
                } elseif ($answerType == CALCULATED_ANSWER) {
                    /*
                     * In the CALCULATED_ANSWER test
                     * you mustn't have [ and ] in the textarea
                     * you mustn't have @@ in the textarea
                     * the text to find mustn't be empty or contains only spaces
                     * the text to find mustn't contains HTML tags
                     * the text to find mustn't contains char "
                     */
                    if ($origin !== null) {
                        global $exe_id;
                        $trackAttempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
                        $sqlTrackAttempt = 'SELECT answer FROM ' . $trackAttempts . '
                                            WHERE exe_id=' . $exe_id . ' AND question_id=' . $questionId;
                        $rsLastAttempt = Database::query($sqlTrackAttempt);
                        $rowLastAttempt = Database::fetch_array($rsLastAttempt);
                        $answer = $rowLastAttempt['answer'];
                        if (empty($answer)) {
                            $_SESSION['calculatedAnswerId'][$questionId] = mt_rand(1, $nbrAnswers);
                            $answer = $objAnswerTmp->selectAnswer($_SESSION['calculatedAnswerId'][$questionId]);
                        }
                    }
                    list($answer) = explode('@@', $answer);
                    // $correctAnswerList array of array with correct anwsers 0=> [0=>[\p] 1=>[plop]]
                    api_preg_match_all('/\\[[^]]+\\]/', $answer, $correctAnswerList);
                    // get student answer to display it if student go back to previous calculated answer question in a test
                    if (isset($user_choice[0]['answer'])) {
                        api_preg_match_all('/\\[[^]]+\\]/', $answer, $studentAnswerList);
                        $studentAnswerListTobecleaned = $studentAnswerList[0];
                        $studentAnswerList = array();
                        for ($i = 0; $i < count($studentAnswerListTobecleaned); $i++) {
                            $answerCorrected = $studentAnswerListTobecleaned[$i];
                            $answerCorrected = api_preg_replace('| / <font color="green"><b>.*$|', '', $answerCorrected);
                            $answerCorrected = api_preg_replace('/^\\[/', '', $answerCorrected);
                            $answerCorrected = api_preg_replace('|^<font color="red"><s>|', '', $answerCorrected);
                            $answerCorrected = api_preg_replace('|</s></font>$|', '', $answerCorrected);
                            $answerCorrected = '[' . $answerCorrected . ']';
                            $studentAnswerList[] = $answerCorrected;
                        }
                    }
                    // If display preview of answer in test view for exemple, set the student answer to the correct answers
                    if ($debug_mark_answer) {
                        // contain the rights answers surronded with brackets
                        $studentAnswerList = $correctAnswerList[0];
                    }
                    /*
                    Split the response by bracket
                    tabComments is an array with text surrounding the text to find
                    we add a space before and after the answerQuestion to be sure to
                    have a block of text before and after [xxx] patterns
                    so we have n text to find ([xxx]) and n+1 block of texts before,
                    between and after the text to find
                    */
                    $tabComments = api_preg_split('/\\[[^]]+\\]/', ' ' . $answer . ' ');
                    if (!empty($correctAnswerList) && !empty($studentAnswerList)) {
                        $answer = "";
                        $i = 0;
                        foreach ($studentAnswerList as $studentItem) {
                            // remove surronding brackets
                            $studentResponse = api_substr($studentItem, 1, api_strlen($studentItem) - 2);
                            $size = strlen($studentItem);
                            $attributes['class'] = self::detectInputAppropriateClass($size);
                            $answer .= $tabComments[$i] . Display::input('text', "choice[{$questionId}][]", $studentResponse, $attributes);
                            $i++;
                        }
                        $answer .= $tabComments[$i];
                    } else {
                        // display exercise with empty input fields
                        // every [xxx] are replaced with an empty input field
                        foreach ($correctAnswerList[0] as $item) {
                            $size = strlen($item);
                            $attributes['class'] = self::detectInputAppropriateClass($size);
                            $answer = str_replace($item, Display::input('text', "choice[{$questionId}][]", '', $attributes), $answer);
                        }
                    }
                    if ($origin !== null) {
                        $s = $answer;
                        break;
                    } else {
                        $s .= $answer;
                    }
                } elseif ($answerType == MATCHING) {
                    // matching type, showing suggestions and answers
                    // TODO: replace $answerId by $numAnswer
                    if ($answerCorrect != 0) {
                        // only show elements to be answered (not the contents of
                        // the select boxes, who are corrrect = 0)
                        $s .= '<tr><td width="45%" valign="top">';
                        $parsed_answer = $answer;
                        //left part questions
                        $s .= '<p class="indent">' . $lines_count . '.&nbsp;' . $parsed_answer . '</p></td>';
                        //middle part (matches selects)
                        $s .= '<td width="10%" valign="top" align="center" >
                                <div class="select-matching">
                                <select name="choice[' . $questionId . '][' . $numAnswer . ']">';
                        // fills the list-box
                        foreach ($select_items as $key => $val) {
                            // set $debug_mark_answer to true at function start to
                            // show the correct answer with a suffix '-x'
                            $selected = '';
                            if ($debug_mark_answer) {
                                if ($val['id'] == $answerCorrect) {
                                    $selected = 'selected="selected"';
                                }
                            }
                            //$user_choice_array_position
                            if (isset($user_choice_array_position[$numAnswer]) && $val['id'] == $user_choice_array_position[$numAnswer]) {
                                $selected = 'selected="selected"';
                            }
                            $s .= '<option value="' . $val['id'] . '" ' . $selected . '>' . $val['letter'] . '</option>';
                        }
                        // end foreach()
                        $s .= '</select></div></td><td width="5%" class="separate">&nbsp;</td>';
                        $s .= '<td width="40%" valign="top" >';
                        if (isset($select_items[$lines_count])) {
                            $s .= '<div class="text-right"><p class="indent">' . $select_items[$lines_count]['letter'] . '.&nbsp; ' . $select_items[$lines_count]['answer'] . '</p></div>';
                        } else {
                            $s .= '&nbsp;';
                        }
                        $s .= '</td>';
                        $s .= '</tr>';
                        $lines_count++;
                        //if the left side of the "matching" has been completely
                        // shown but the right side still has values to show...
                        if ($lines_count - 1 == $num_suggestions) {
                            // if it remains answers to shown at the right side
                            while (isset($select_items[$lines_count])) {
                                $s .= '<tr>
                                      <td colspan="2"></td>
                                      <td valign="top">';
                                $s .= '<b>' . $select_items[$lines_count]['letter'] . '.</b> ' . $select_items[$lines_count]['answer'];
                                $s .= "</td>\n                                </tr>";
                                $lines_count++;
                            }
                            // end while()
                        }
                        // end if()
                        $matching_correct_answer++;
                    }
                } elseif ($answerType == DRAGGABLE) {
                    if ($answerCorrect != 0) {
                        $parsed_answer = $answer;
                        /*$lines_count = '';
                          $data = $objAnswerTmp->getAnswerByAutoId($numAnswer);
                          $data = $objAnswerTmp->getAnswerByAutoId($data['correct']);
                          $lines_count = $data['answer'];*/
                        $windowId = $questionId . '_' . $lines_count;
                        $s .= '<li class="touch-items" id="' . $windowId . '">';
                        $s .= Display::div($parsed_answer, ['id' => "window_{$windowId}", 'class' => "window{$questionId}_question_draggable exercise-draggable-answer-option"]);
                        $selectedValue = 0;
                        $draggableSelectOptions = [];
                        foreach ($select_items as $key => $val) {
                            if ($debug_mark_answer) {
                                if ($val['id'] == $answerCorrect) {
                                    $selectedValue = $val['id'];
                                }
                            }
                            if (isset($user_choice[$matching_correct_answer]) && $val['id'] == $user_choice[$matching_correct_answer]['answer']) {
                                $selectedValue = $val['id'];
                            }
                            $draggableSelectOptions[$val['id']] = $val['letter'];
                        }
                        $s .= Display::select("choice[{$questionId}][{$numAnswer}]", $draggableSelectOptions, $selectedValue, ['id' => "window_{$windowId}_select", 'class' => 'select_option', 'style' => 'display: none;'], false);
                        if (!empty($answerCorrect) && !empty($selectedValue)) {
                            $s .= <<<JAVASCRIPT
                                <script>
                                    \$(function() {
                                        DraggableAnswer.deleteItem(
                                            \$('#{$questionId}_{$selectedValue}'),
                                            \$('#drop_{$windowId}')
                                        );
                                    });
                                </script>
JAVASCRIPT;
                        }
                        if (isset($select_items[$lines_count])) {
                            $s .= Display::div(Display::tag('b', $select_items[$lines_count]['letter']) . $select_items[$lines_count]['answer'], ['id' => "window_{$windowId}_answer", 'style' => 'display: none;']);
                        } else {
                            $s .= '&nbsp;';
                        }
                        $lines_count++;
                        if ($lines_count - 1 == $num_suggestions) {
                            while (isset($select_items[$lines_count])) {
                                $s .= Display::tag('b', $select_items[$lines_count]['letter']);
                                $s .= $select_items[$lines_count]['answer'];
                                $lines_count++;
                            }
                        }
                        $matching_correct_answer++;
                        $s .= '</li>';
                    }
                } elseif ($answerType == MATCHING_DRAGGABLE) {
                    if ($answerId == 1) {
                        echo $objAnswerTmp->getJs();
                    }
                    if ($answerCorrect != 0) {
                        $parsed_answer = $answer;
                        $windowId = "{$questionId}_{$lines_count}";
                        $s .= <<<HTML
                            <tr>
                                <td widht="45%">
                                    <div id="window_{$windowId}" class="window window_left_question window{$questionId}_question">
                                        <strong>{$lines_count}.</strong> {$parsed_answer}
                                    </div>
                                </td>
                                <td width="10%">
HTML;
                        $selectedValue = 0;
                        $questionOptions = [];
                        foreach ($select_items as $key => $val) {
                            if ($debug_mark_answer) {
                                if ($val['id'] == $answerCorrect) {
                                    $selectedValue = $val['id'];
                                }
                            }
                            if (isset($user_choice[$matching_correct_answer]) && $val['id'] == $user_choice[$matching_correct_answer]['answer']) {
                                $selectedValue = $val['id'];
                            }
                            $questionOptions[$val['id']] = $val['letter'];
                        }
                        $s .= Display::select("choice[{$questionId}][{$numAnswer}]", $questionOptions, $selectedValue, ['id' => "window_{$windowId}_select", 'class' => 'hidden'], false);
                        if (!empty($answerCorrect) && !empty($selectedValue)) {
                            // Show connect if is not freeze (question preview)
                            if (!$freeze) {
                                $s .= <<<JAVASCRIPT
                                <script>
                                    jsPlumb.ready(function() {
                                        jsPlumb.connect({
                                            source: 'window_{$windowId}',
                                            target: 'window_{$questionId}_{$selectedValue}_answer',
                                            endpoint: ['Blank', {radius: 15}],
                                            anchors: ['RightMiddle', 'LeftMiddle'],
                                            paintStyle: {strokeStyle: '#8A8888', lineWidth: 8},
                                            connector: [
                                                MatchingDraggable.connectorType,
                                                {curvines: MatchingDraggable.curviness}
                                            ]
                                        });
                                    });
                                </script>
JAVASCRIPT;
                            }
                        }
                        $s .= <<<HTML
                            </td>
                            <td width="45%">
HTML;
                        if (isset($select_items[$lines_count])) {
                            $s .= <<<HTML
                                <div id="window_{$windowId}_answer" class="window window_right_question">
                                    <strong>{$select_items[$lines_count]['letter']}.</strong> {$select_items[$lines_count]['answer']}
                                </div>
HTML;
                        } else {
                            $s .= '&nbsp;';
                        }
                        $s .= '</td></tr>';
                        $lines_count++;
                        if ($lines_count - 1 == $num_suggestions) {
                            while (isset($select_items[$lines_count])) {
                                $s .= <<<HTML
                                    <tr>
                                        <td colspan="2"></td>
                                        <td>
                                            <strong>{$select_items[$lines_count]['letter']}</strong>
                                            {$select_items[$lines_count]['answer']}
                                        </td>
                                    </tr>
HTML;
                                $lines_count++;
                            }
                        }
                        $matching_correct_answer++;
                    }
                }
            }
            // end for()
            if ($show_comment) {
                $s .= '</table>';
            } elseif (in_array($answerType, [MATCHING, MATCHING_DRAGGABLE, UNIQUE_ANSWER_NO_OPTION, MULTIPLE_ANSWER_TRUE_FALSE, MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE])) {
                $s .= '</table>';
            }
            if ($answerType == DRAGGABLE) {
                $s .= "</ul></div>";
                $counterAnswer = 1;
                $s .= '<div class="col-md-12"><div class="row">';
                for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
                    $answerCorrect = $objAnswerTmp->isCorrect($answerId);
                    $windowId = $questionId . '_' . $counterAnswer;
                    if ($answerCorrect) {
                        $s .= Display::div($counterAnswer, ['id' => "drop_{$windowId}", 'class' => 'droppable col-md-2']);
                        $counterAnswer++;
                    }
                }
                $s .= '</div></div>';
            }
            if (in_array($answerType, [MATCHING, MATCHING_DRAGGABLE])) {
                $s .= '</div>';
            }
            $s .= '</div>';
            // destruction of the Answer object
            unset($objAnswerTmp);
            // destruction of the Question object
            unset($objQuestionTmp);
            if ($origin != 'export') {
                echo $s;
            } else {
                return $s;
            }
        } elseif ($answerType == HOT_SPOT || $answerType == HOT_SPOT_DELINEATION) {
            // Question is a HOT_SPOT
            //checking document/images visibility
            if (api_is_platform_admin() || api_is_course_admin()) {
                $course = api_get_course_info();
                $doc_id = DocumentManager::get_document_id($course, '/images/' . $pictureName);
                if (is_numeric($doc_id)) {
                    $images_folder_visibility = api_get_item_visibility($course, 'document', $doc_id, api_get_session_id());
                    if (!$images_folder_visibility) {
                        //This message is shown only to the course/platform admin if the image is set to visibility = false
                        Display::display_warning_message(get_lang('ChangeTheVisibilityOfTheCurrentImage'));
                    }
                }
            }
            $questionName = $objQuestionTmp->selectTitle();
            $questionDescription = $objQuestionTmp->selectDescription();
            if ($freeze) {
                echo Display::img($objQuestionTmp->selectPicturePath());
                return;
            }
            // Get the answers, make a list
            $objAnswerTmp = new Answer($questionId);
            $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
            // get answers of hotpost
            $answers_hotspot = array();
            for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
                $answers = $objAnswerTmp->selectAnswerByAutoId($objAnswerTmp->selectAutoId($answerId));
                $answers_hotspot[$answers['id']] = $objAnswerTmp->selectAnswer($answerId);
            }
            // display answers of hotpost order by id
            $answer_list = '<div style="padding: 10px; margin-left: 0px; border: 1px solid #A4A4A4; height: 408px; width: 200px;"><b>' . get_lang('HotspotZones') . '</b><dl>';
            if (!empty($answers_hotspot)) {
                ksort($answers_hotspot);
                foreach ($answers_hotspot as $key => $value) {
                    $answer_list .= '<dt>' . $key . '.- ' . $value . '</dt><br />';
                }
            }
            $answer_list .= '</dl></div>';
            if ($answerType == HOT_SPOT_DELINEATION) {
                $answer_list = '';
                $swf_file = 'hotspot_delineation_user';
                $swf_height = 405;
            } else {
                $swf_file = 'hotspot_user';
                $swf_height = 436;
            }
            if (!$only_questions) {
                if ($show_title) {
                    TestCategory::displayCategoryAndTitle($objQuestionTmp->id);
                    echo '<div class="question_title">' . $current_item . '. ' . $questionName . '</div>';
                }
                //@todo I need to the get the feedback type
                echo '<input type="hidden" name="hidden_hotspot_id" value="' . $questionId . '" />';
                echo '<table class="exercise_questions" >
                      <tr>
                        <td valign="top" colspan="2">';
                echo $questionDescription;
                echo '</td></tr>';
            }
            $canClick = isset($_GET['editQuestion']) ? '0' : (isset($_GET['modifyAnswers']) ? '0' : '1');
            $s .= '<script type="text/javascript" src="../plugin/hotspot/JavaScriptFlashGateway.js"></script>
                            <script src="../plugin/hotspot/hotspot.js" type="text/javascript" ></script>
                            <script type="text/javascript">
                            <!--
                            // Globals
                            // Major version of Flash required
                            var requiredMajorVersion = 7;
                            // Minor version of Flash required
                            var requiredMinorVersion = 0;
                            // Minor version of Flash required
                            var requiredRevision = 0;
                            // the version of javascript supported
                            var jsVersion = 1.0;
                            // -->
                            </script>
                            <script language="VBScript" type="text/vbscript">
                            <!-- // Visual basic helper required to detect Flash Player ActiveX control version information
                            Function VBGetSwfVer(i)
                              on error resume next
                              Dim swControl, swVersion
                              swVersion = 0

                              set swControl = CreateObject("ShockwaveFlash.ShockwaveFlash." + CStr(i))
                              if (IsObject(swControl)) then
                                swVersion = swControl.GetVariable("$version")
                              end if
                              VBGetSwfVer = swVersion
                            End Function
                            // -->
                            </script>

                            <script language="JavaScript1.1" type="text/javascript">
                            <!-- // Detect Client Browser type
                            var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
                            var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
                            var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
                            jsVersion = 1.1;
                            // JavaScript helper required to detect Flash Player PlugIn version information
                            function JSGetSwfVer(i) {
                                // NS/Opera version >= 3 check for Flash plugin in plugin array
                                if (navigator.plugins != null && navigator.plugins.length > 0) {
                                    if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
                                        var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
                                        var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
                                        descArray = flashDescription.split(" ");
                                        tempArrayMajor = descArray[2].split(".");
                                        versionMajor = tempArrayMajor[0];
                                        versionMinor = tempArrayMajor[1];
                                        if ( descArray[3] != "" ) {
                                            tempArrayMinor = descArray[3].split("r");
                                        } else {
                                            tempArrayMinor = descArray[4].split("r");
                                        }
                                        versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
                                        flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
                                    } else {
                                        flashVer = -1;
                                    }
                                }
                                // MSN/WebTV 2.6 supports Flash 4
                                else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
                                // WebTV 2.5 supports Flash 3
                                else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
                                // older WebTV supports Flash 2
                                else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
                                // Can\'t detect in all other cases
                                else
                                {
                                    flashVer = -1;
                                }
                                return flashVer;
                            }
                            // When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available

                            function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) {
                                reqVer = parseFloat(reqMajorVer + "." + reqRevision);
                                // loop backwards through the versions until we find the newest version
                                for (i=25;i>0;i--) {
                                    if (isIE && isWin && !isOpera) {
                                        versionStr = VBGetSwfVer(i);
                                    } else {
                                        versionStr = JSGetSwfVer(i);
                                    }
                                    if (versionStr == -1 ) {
                                        return false;
                                    } else if (versionStr != 0) {
                                        if(isIE && isWin && !isOpera) {
                                            tempArray         = versionStr.split(" ");
                                            tempString        = tempArray[1];
                                            versionArray      = tempString .split(",");
                                        } else {
                                            versionArray      = versionStr.split(".");
                                        }
                                        versionMajor      = versionArray[0];
                                        versionMinor      = versionArray[1];
                                        versionRevision   = versionArray[2];

                                        versionString     = versionMajor + "." + versionRevision;   // 7.0r24 == 7.24
                                        versionNum        = parseFloat(versionString);
                                        // is the major.revision >= requested major.revision AND the minor version >= requested minor
                                        if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
                                            return true;
                                        } else {
                                            return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
                                        }
                                    }
                                }
                            }
                            // -->
                            </script>';
            $s .= '<tr><td valign="top" colspan="2" width="520"><table><tr><td width="520">
                        <script>
                            <!--
                            // Version check based upon the values entered above in "Globals"
                            var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

                            // Check to see if the version meets the requirements for playback
                            if (hasReqestedVersion) {  // if we\'ve detected an acceptable version
                                var oeTags = \'<object type="application/x-shockwave-flash" data="../plugin/hotspot/' . $swf_file . '.swf?modifyAnswers=' . $questionId . '&canClick:' . $canClick . '" width="600" height="' . $swf_height . '">\'
                                            + \'<param name="wmode" value="transparent">\'
                                            + \'<param name="movie" value="../plugin/hotspot/' . $swf_file . '.swf?modifyAnswers=' . $questionId . '&canClick:' . $canClick . '" />\'
                                            + \'<\\/object>\';
                                document.write(oeTags);   // embed the Flash Content SWF when all tests are passed
                            } else {  // flash is too old or we can\'t detect the plugin
                                var alternateContent = "Error<br \\/>"
                                    + "Hotspots requires Macromedia Flash 7.<br \\/>"
                                    + "<a href=\\"http://www.macromedia.com/go/getflash/\\">Get Flash<\\/a>";
                                document.write(alternateContent);  // insert non-flash content
                            }
                            // -->
                        </script>
                        </td>
                        <td valign="top" align="left">' . $answer_list . '</td></tr>
                        </table>
            </td></tr>';
            echo $s;
            echo '</table>';
        }
        return $nbrAnswers;
    }
    /**
     * Shows a question
     *
     * @param int    $questionId question id
     * @param bool   $only_questions if true only show the questions, no exercise title
     * @param bool   $origin  i.e = learnpath
     * @param string $current_item current item from the list of questions
     * @param bool   $show_title
     * @param bool   $freeze
     * @param array  $user_choice
     * @param bool   $show_comment
     * @param bool   $exercise_feedback
     * @param bool   $show_answers
     * */
    public static function showQuestion($questionId, $only_questions = false, $origin = false, $current_item = '', $show_title = true, $freeze = false, $user_choice = array(), $show_comment = false, $exercise_feedback = null, $show_answers = false)
    {
        $course_id = api_get_course_int_id();
        // Change false to true in the following line to enable answer hinting
        $debug_mark_answer = $show_answers;
        // Reads question information
        if (!($objQuestionTmp = Question::read($questionId))) {
            // Question not found
            return false;
        }
        if ($exercise_feedback != EXERCISE_FEEDBACK_TYPE_END) {
            $show_comment = false;
        }
        $answerType = $objQuestionTmp->selectType();
        $pictureName = $objQuestionTmp->selectPicture();
        $s = '';
        if ($answerType != HOT_SPOT && $answerType != HOT_SPOT_DELINEATION) {
            // Question is not a hotspot
            if (!$only_questions) {
                $questionDescription = $objQuestionTmp->selectDescription();
                if ($show_title) {
                    TestCategory::displayCategoryAndTitle($objQuestionTmp->id);
                    echo Display::div($current_item . '. ' . $objQuestionTmp->selectTitle(), array('class' => 'question_title'));
                }
                if (!empty($questionDescription)) {
                    echo Display::div($questionDescription, array('class' => 'question_description'));
                }
            }
            if (in_array($answerType, array(FREE_ANSWER, ORAL_EXPRESSION)) && $freeze) {
                return '';
            }
            echo '<div class="question_options row">';
            // construction of the Answer object (also gets all answers details)
            $objAnswerTmp = new Answer($questionId);
            $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
            $quiz_question_options = Question::readQuestionOption($questionId, $course_id);
            // For "matching" type here, we need something a little bit special
            // because the match between the suggestions and the answers cannot be
            // done easily (suggestions and answers are in the same table), so we
            // have to go through answers first (elems with "correct" value to 0).
            $select_items = array();
            //This will contain the number of answers on the left side. We call them
            // suggestions here, for the sake of comprehensions, while the ones
            // on the right side are called answers
            $num_suggestions = 0;
            if (in_array($answerType, [MATCHING, DRAGGABLE, MATCHING_DRAGGABLE])) {
                if ($answerType == DRAGGABLE) {
                    $s .= '<div class="col-md-12 ui-widget ui-helper-clearfix">
                        <div class="clearfix">
                        <ul class="exercise-draggable-answer ui-helper-reset ui-helper-clearfix">';
                } else {
                    $s .= '<div id="drag' . $questionId . '_question" class="drag_question">
                           <table class="data_table">';
                }
                // Iterate through answers
                $x = 1;
                //mark letters for each answer
                $letter = 'A';
                $answer_matching = array();
                $cpt1 = array();
                for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
                    $answerCorrect = $objAnswerTmp->isCorrect($answerId);
                    $numAnswer = $objAnswerTmp->selectAutoId($answerId);
                    if ($answerCorrect == 0) {
                        // options (A, B, C, ...) that will be put into the list-box
                        // have the "correct" field set to 0 because they are answer
                        $cpt1[$x] = $letter;
                        $answer_matching[$x] = $objAnswerTmp->selectAnswerByAutoId($numAnswer);
                        $x++;
                        $letter++;
                    }
                }
                $i = 1;
                $select_items[0]['id'] = 0;
                $select_items[0]['letter'] = '--';
                $select_items[0]['answer'] = '';
                foreach ($answer_matching as $id => $value) {
                    $select_items[$i]['id'] = $value['id_auto'];
                    $select_items[$i]['letter'] = $cpt1[$id];
                    $select_items[$i]['answer'] = $value['answer'];
                    $i++;
                }
                $user_choice_array_position = array();
                if (!empty($user_choice)) {
                    foreach ($user_choice as $item) {
                        $user_choice_array_position[$item['position']] = $item['answer'];
                    }
                }
                $num_suggestions = $nbrAnswers - $x + 1;
            } elseif ($answerType == FREE_ANSWER) {
                $fck_content = isset($user_choice[0]) && !empty($user_choice[0]['answer']) ? $user_choice[0]['answer'] : null;
                $form = new FormValidator('free_choice_' . $questionId);
                $config = array('ToolbarSet' => 'TestFreeAnswer');
                $form->addHtmlEditor("choice[" . $questionId . "]", null, false, false, $config);
                $form->setDefaults(array("choice[" . $questionId . "]" => $fck_content));
                $s .= $form->returnForm();
            } elseif ($answerType == ORAL_EXPRESSION) {
                // Add nanog
                if (api_get_setting('document.enable_nanogong') == 'true') {
                    //@todo pass this as a parameter
                    global $exercise_stat_info, $exerciseId, $exe_id;
                    if (!empty($exercise_stat_info)) {
                        $params = array('exercise_id' => $exercise_stat_info['exe_exo_id'], 'exe_id' => $exercise_stat_info['exe_id'], 'question_id' => $questionId);
                    } else {
                        $params = array('exercise_id' => $exerciseId, 'exe_id' => 'temp_exe', 'question_id' => $questionId);
                    }
                    $nano = new Nanogong($params);
                    echo $nano->show_button();
                }
                $form = new FormValidator('free_choice_' . $questionId);
                $config = array('ToolbarSet' => 'TestFreeAnswer');
                $form->addHtmlEditor("choice[" . $questionId . "]", null, false, false, $config);
                //$form->setDefaults(array("choice[" . $questionId . "]" => $fck_content));
                $s .= $form->returnForm();
            }
            // Now navigate through the possible answers, using the max number of
            // answers for the question as a limiter
            $lines_count = 1;
            // a counter for matching-type answers
            if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE || $answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
                $header = Display::tag('th', get_lang('Options'));
                foreach ($objQuestionTmp->options as $item) {
                    if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
                        if (in_array($item, $objQuestionTmp->options)) {
                            $header .= Display::tag('th', get_lang($item));
                        } else {
                            $header .= Display::tag('th', $item);
                        }
                    } else {
                        $header .= Display::tag('th', $item);
                    }
                }
                if ($show_comment) {
                    $header .= Display::tag('th', get_lang('Feedback'));
                }
                $s .= '<table class="table table-hover table-striped">';
                $s .= Display::tag('tr', $header, array('style' => 'text-align:left;'));
            }
            if ($show_comment) {
                if (in_array($answerType, array(MULTIPLE_ANSWER, MULTIPLE_ANSWER_COMBINATION, UNIQUE_ANSWER, UNIQUE_ANSWER_IMAGE, UNIQUE_ANSWER_NO_OPTION, GLOBAL_MULTIPLE_ANSWER))) {
                    $header = Display::tag('th', get_lang('Options'));
                    if ($exercise_feedback == EXERCISE_FEEDBACK_TYPE_END) {
                        $header .= Display::tag('th', get_lang('Feedback'));
                    }
                    $s .= '<table class="table table-hover table-striped">';
                    $s .= Display::tag('tr', $header, array('style' => 'text-align:left;'));
                }
            }
            $matching_correct_answer = 0;
            $user_choice_array = array();
            if (!empty($user_choice)) {
                foreach ($user_choice as $item) {
                    $user_choice_array[] = $item['answer'];
                }
            }
            for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
                $answer = $objAnswerTmp->selectAnswer($answerId);
                $answerCorrect = $objAnswerTmp->isCorrect($answerId);
                $numAnswer = $objAnswerTmp->selectAutoId($answerId);
                $comment = $objAnswerTmp->selectComment($answerId);
                $attributes = array();
                // Unique answer
                if (in_array($answerType, [UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION, UNIQUE_ANSWER_IMAGE])) {
                    $input_id = 'choice-' . $questionId . '-' . $answerId;
                    if (isset($user_choice[0]['answer']) && $user_choice[0]['answer'] == $numAnswer) {
                        $attributes = array('id' => $input_id, 'checked' => 1, 'selected' => 1);
                    } else {
                        $attributes = array('id' => $input_id);
                    }
                    if ($debug_mark_answer) {
                        if ($answerCorrect) {
                            $attributes['checked'] = 1;
                            $attributes['selected'] = 1;
                        }
                    }
                    if ($show_comment) {
                        $s .= '<tr><td>';
                    }
                    if ($answerType == UNIQUE_ANSWER_IMAGE) {
                        if ($show_comment) {
                            if (empty($comment)) {
                                $s .= '<div id="answer' . $questionId . $numAnswer . '" ' . 'class="exercise-unique-answer-image" style="text-align: center">';
                            } else {
                                $s .= '<div id="answer' . $questionId . $numAnswer . '" ' . 'class="exercise-unique-answer-image col-xs-6 col-sm-12" style="text-align: center">';
                            }
                        } else {
                            $s .= '<div id="answer' . $questionId . $numAnswer . '" ' . 'class="exercise-unique-answer-image col-xs-6 col-md-3" style="text-align: center">';
                        }
                    }
                    $answer = Security::remove_XSS($answer, STUDENT);
                    $s .= Display::input('hidden', 'choice2[' . $questionId . ']', '0');
                    $answer_input = null;
                    if ($answerType == UNIQUE_ANSWER_IMAGE) {
                        $attributes['style'] = 'display: none;';
                        $answer = '<div class="thumbnail">' . $answer . '</div>';
                    }
                    $answer_input .= '<label class="radio">';
                    $answer_input .= Display::input('radio', 'choice[' . $questionId . ']', $numAnswer, $attributes);
                    $answer_input .= $answer;
                    $answer_input .= '</label>';
                    if ($answerType == UNIQUE_ANSWER_IMAGE) {
                        $answer_input .= "</div>";
                    }
                    if ($show_comment) {
                        $s .= $answer_input;
                        $s .= '</td>';
                        $s .= '<td>';
                        $s .= $comment;
                        $s .= '</td>';
                        $s .= '</tr>';
                    } else {
                        $s .= $answer_input;
                    }
                } elseif ($answerType == MULTIPLE_ANSWER || $answerType == MULTIPLE_ANSWER_TRUE_FALSE || $answerType == GLOBAL_MULTIPLE_ANSWER) {
                    $input_id = 'choice-' . $questionId . '-' . $answerId;
                    $answer = Security::remove_XSS($answer, STUDENT);
                    if (in_array($numAnswer, $user_choice_array)) {
                        $attributes = array('id' => $input_id, 'checked' => 1, 'selected' => 1);
                    } else {
                        $attributes = array('id' => $input_id);
                    }
                    if ($debug_mark_answer) {
                        if ($answerCorrect) {
                            $attributes['checked'] = 1;
                            $attributes['selected'] = 1;
                        }
                    }
                    if ($answerType == MULTIPLE_ANSWER || $answerType == GLOBAL_MULTIPLE_ANSWER) {
                        $s .= '<input type="hidden" name="choice2[' . $questionId . ']" value="0" />';
                        $answer_input = '<label class="checkbox">';
                        $answer_input .= Display::input('checkbox', 'choice[' . $questionId . '][' . $numAnswer . ']', $numAnswer, $attributes);
                        $answer_input .= $answer;
                        $answer_input .= '</label>';
                        if ($show_comment) {
                            $s .= '<tr><td>';
                            $s .= $answer_input;
                            $s .= '</td>';
                            $s .= '<td>';
                            $s .= $comment;
                            $s .= '</td>';
                            $s .= '</tr>';
                        } else {
                            $s .= $answer_input;
                        }
                    } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
                        $my_choice = array();
                        if (!empty($user_choice_array)) {
                            foreach ($user_choice_array as $item) {
                                $item = explode(':', $item);
                                $my_choice[$item[0]] = $item[1];
                            }
                        }
                        $s .= '<tr>';
                        $s .= Display::tag('td', $answer);
                        if (!empty($quiz_question_options)) {
                            foreach ($quiz_question_options as $id => $item) {
                                if (isset($my_choice[$numAnswer]) && $id == $my_choice[$numAnswer]) {
                                    $attributes = array('checked' => 1, 'selected' => 1);
                                } else {
                                    $attributes = array();
                                }
                                if ($debug_mark_answer) {
                                    if ($id == $answerCorrect) {
                                        $attributes['checked'] = 1;
                                        $attributes['selected'] = 1;
                                    }
                                }
                                $s .= Display::tag('td', Display::input('radio', 'choice[' . $questionId . '][' . $numAnswer . ']', $id, $attributes), array('style' => ''));
                            }
                        }
                        if ($show_comment) {
                            $s .= '<td>';
                            $s .= $comment;
                            $s .= '</td>';
                        }
                        $s .= '</tr>';
                    }
                } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION) {
                    // multiple answers
                    $input_id = 'choice-' . $questionId . '-' . $answerId;
                    if (in_array($numAnswer, $user_choice_array)) {
                        $attributes = array('id' => $input_id, 'checked' => 1, 'selected' => 1);
                    } else {
                        $attributes = array('id' => $input_id);
                    }
                    if ($debug_mark_answer) {
                        if ($answerCorrect) {
                            $attributes['checked'] = 1;
                            $attributes['selected'] = 1;
                        }
                    }
                    $answer = Security::remove_XSS($answer, STUDENT);
                    $answer_input = '<input type="hidden" name="choice2[' . $questionId . ']" value="0" />';
                    $answer_input .= '<label class="checkbox">';
                    $answer_input .= Display::input('checkbox', 'choice[' . $questionId . '][' . $numAnswer . ']', 1, $attributes);
                    $answer_input .= $answer;
                    $answer_input .= '</label>';
                    if ($show_comment) {
                        $s .= '<tr>';
                        $s .= '<td>';
                        $s .= $answer_input;
                        $s .= '</td>';
                        $s .= '<td>';
                        $s .= $comment;
                        $s .= '</td>';
                        $s .= '</tr>';
                    } else {
                        $s .= $answer_input;
                    }
                } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
                    $s .= '<input type="hidden" name="choice2[' . $questionId . ']" value="0" />';
                    $my_choice = array();
                    if (!empty($user_choice_array)) {
                        foreach ($user_choice_array as $item) {
                            $item = explode(':', $item);
                            if (isset($item[1]) && isset($item[0])) {
                                $my_choice[$item[0]] = $item[1];
                            }
                        }
                    }
                    $answer = Security::remove_XSS($answer, STUDENT);
                    $s .= '<tr>';
                    $s .= Display::tag('td', $answer);
                    foreach ($objQuestionTmp->options as $key => $item) {
                        if (isset($my_choice[$numAnswer]) && $key == $my_choice[$numAnswer]) {
                            $attributes = array('checked' => 1, 'selected' => 1);
                        } else {
                            $attributes = array();
                        }
                        if ($debug_mark_answer) {
                            if ($key == $answerCorrect) {
                                $attributes['checked'] = 1;
                                $attributes['selected'] = 1;
                            }
                        }
                        $s .= Display::tag('td', Display::input('radio', 'choice[' . $questionId . '][' . $numAnswer . ']', $key, $attributes));
                    }
                    if ($show_comment) {
                        $s .= '<td>';
                        $s .= $comment;
                        $s .= '</td>';
                    }
                    $s .= '</tr>';
                } elseif ($answerType == FILL_IN_BLANKS) {
                    // display the question, with field empty, for student to fill it,
                    // or filled to display the answer in the Question preview of the exercice/admin.php page
                    $displayForStudent = true;
                    $listAnswerInformations = FillBlanks::getAnswerInfo($answer);
                    $separatorStartRegexp = FillBlanks::escapeForRegexp($listAnswerInformations['blankseparatorstart']);
                    $separatorEndRegexp = FillBlanks::escapeForRegexp($listAnswerInformations['blankseparatorend']);
                    list($answer) = explode('::', $answer);
                    //Correct answers
                    $correctAnswerList = $listAnswerInformations['tabwords'];
                    //Student's answer
                    $studentAnswerList = array();
                    if (isset($user_choice[0]['answer'])) {
                        $arrayStudentAnswer = FillBlanks::getAnswerInfo($user_choice[0]['answer'], true);
                        $studentAnswerList = $arrayStudentAnswer['studentanswer'];
                    }
                    // If the question must be shown with the answer (in page exercice/admin.php) for teacher preview
                    // set the student-answer to the correct answer
                    if ($debug_mark_answer) {
                        $studentAnswerList = $correctAnswerList;
                        $displayForStudent = false;
                    }
                    if (!empty($correctAnswerList) && !empty($studentAnswerList)) {
                        $answer = "";
                        for ($i = 0; $i < count($listAnswerInformations["commonwords"]) - 1; $i++) {
                            // display the common word
                            $answer .= $listAnswerInformations["commonwords"][$i];
                            // display the blank word
                            $correctItem = $listAnswerInformations["tabwords"][$i];
                            $correctItemRegexp = $correctItem;
                            // replace / with \/ to allow the preg_replace bellow and all the regexp char
                            $correctItemRegexp = FillBlanks::getRegexpProtected($correctItemRegexp);
                            if (isset($studentAnswerList[$i])) {
                                // If student already started this test and answered this question,
                                // fill the blank with his previous answers
                                // may be "" if student viewed the question, but did not fill the blanks
                                $correctItem = $studentAnswerList[$i];
                            }
                            $attributes["style"] = "width:" . $listAnswerInformations["tabinputsize"][$i] . "px";
                            $answer .= FillBlanks::getFillTheBlankHtml($separatorStartRegexp, $separatorEndRegexp, $correctItemRegexp, $questionId, $correctItem, $attributes, $answer, $listAnswerInformations, $displayForStudent, $i);
                        }
                        // display the last common word
                        $answer .= $listAnswerInformations["commonwords"][$i];
                    } else {
                        // display empty [input] with the right width for student to fill it
                        $separatorStartRegexp = FillBlanks::escapeForRegexp($listAnswerInformations['blankseparatorstart']);
                        $separatorEndRegexp = FillBlanks::escapeForRegexp($listAnswerInformations['blankseparatorend']);
                        $answer = "";
                        for ($i = 0; $i < count($listAnswerInformations["commonwords"]) - 1; $i++) {
                            // display the common words
                            $answer .= $listAnswerInformations["commonwords"][$i];
                            // display the blank word
                            $attributes["style"] = "width:" . $listAnswerInformations["tabinputsize"][$i] . "px";
                            $correctItem = $listAnswerInformations["tabwords"][$i];
                            $correctItemRegexp = $correctItem;
                            // replace / with \/ to allow the preg_replace bellow and all the regexp char
                            $correctItemRegexp = FillBlanks::getRegexpProtected($correctItemRegexp);
                            $answer .= FillBlanks::getFillTheBlankHtml($separatorStartRegexp, $separatorEndRegexp, $correctItemRegexp, $questionId, '', $attributes, $answer, $listAnswerInformations, $displayForStudent, $i);
                        }
                        // display the last common word
                        $answer .= $listAnswerInformations["commonwords"][$i];
                    }
                    $s .= $answer;
                } elseif ($answerType == CALCULATED_ANSWER) {
                    /*
                     * In the CALCULATED_ANSWER test
                     * you mustn't have [ and ] in the textarea
                     * you mustn't have @@ in the textarea
                     * the text to find mustn't be empty or contains only spaces
                     * the text to find mustn't contains HTML tags
                     * the text to find mustn't contains char "
                     */
                    if ($origin !== null) {
                        global $exe_id;
                        $trackAttempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
                        $sql = 'SELECT answer
                                FROM ' . $trackAttempts . '
                                WHERE exe_id=' . $exe_id . ' AND question_id=' . $questionId;
                        $rsLastAttempt = Database::query($sql);
                        $rowLastAttempt = Database::fetch_array($rsLastAttempt);
                        $answer = $rowLastAttempt['answer'];
                        if (empty($answer)) {
                            $calculatedAnswerId = [];
                            $calculatedAnswerId[$questionId] = mt_rand(1, $nbrAnswers);
                            $answer = $objAnswerTmp->selectAnswer($calculatedAnswerId[$questionId]);
                            Session::write('calculatedAnswerId', $calculatedAnswerId);
                        }
                    }
                    list($answer) = explode('@@', $answer);
                    // $correctAnswerList array of array with correct anwsers 0=> [0=>[\p] 1=>[plop]]
                    api_preg_match_all('/\\[[^]]+\\]/', $answer, $correctAnswerList);
                    // get student answer to display it if student go back to previous calculated answer question in a test
                    if (isset($user_choice[0]['answer'])) {
                        api_preg_match_all('/\\[[^]]+\\]/', $answer, $studentAnswerList);
                        $studentAnswerListTobecleaned = $studentAnswerList[0];
                        $studentAnswerList = array();
                        for ($i = 0; $i < count($studentAnswerListTobecleaned); $i++) {
                            $answerCorrected = $studentAnswerListTobecleaned[$i];
                            $answerCorrected = api_preg_replace('| / <font color="green"><b>.*$|', '', $answerCorrected);
                            $answerCorrected = api_preg_replace('/^\\[/', '', $answerCorrected);
                            $answerCorrected = api_preg_replace('|^<font color="red"><s>|', '', $answerCorrected);
                            $answerCorrected = api_preg_replace('|</s></font>$|', '', $answerCorrected);
                            $answerCorrected = '[' . $answerCorrected . ']';
                            $studentAnswerList[] = $answerCorrected;
                        }
                    }
                    // If display preview of answer in test view for exemple, set the student answer to the correct answers
                    if ($debug_mark_answer) {
                        // contain the rights answers surronded with brackets
                        $studentAnswerList = $correctAnswerList[0];
                    }
                    /*
                    Split the response by bracket
                    tabComments is an array with text surrounding the text to find
                    we add a space before and after the answerQuestion to be sure to
                    have a block of text before and after [xxx] patterns
                    so we have n text to find ([xxx]) and n+1 block of texts before,
                    between and after the text to find
                    */
                    $tabComments = api_preg_split('/\\[[^]]+\\]/', ' ' . $answer . ' ');
                    if (!empty($correctAnswerList) && !empty($studentAnswerList)) {
                        $answer = "";
                        $i = 0;
                        foreach ($studentAnswerList as $studentItem) {
                            // remove surronding brackets
                            $studentResponse = api_substr($studentItem, 1, api_strlen($studentItem) - 2);
                            $size = strlen($studentItem);
                            $attributes['class'] = self::detectInputAppropriateClass($size);
                            $answer .= $tabComments[$i] . Display::input('text', "choice[{$questionId}][]", $studentResponse, $attributes);
                            $i++;
                        }
                        $answer .= $tabComments[$i];
                    } else {
                        // display exercise with empty input fields
                        // every [xxx] are replaced with an empty input field
                        foreach ($correctAnswerList[0] as $item) {
                            $size = strlen($item);
                            $attributes['class'] = self::detectInputAppropriateClass($size);
                            $answer = str_replace($item, Display::input('text', "choice[{$questionId}][]", '', $attributes), $answer);
                        }
                    }
                    if ($origin !== null) {
                        $s = $answer;
                        break;
                    } else {
                        $s .= $answer;
                    }
                } elseif ($answerType == MATCHING) {
                    // matching type, showing suggestions and answers
                    // TODO: replace $answerId by $numAnswer
                    if ($answerCorrect != 0) {
                        // only show elements to be answered (not the contents of
                        // the select boxes, who are corrrect = 0)
                        $s .= '<tr><td width="45%" valign="top">';
                        $parsed_answer = $answer;
                        //left part questions
                        $s .= '<p class="indent">' . $lines_count . '.&nbsp;' . $parsed_answer . '</p></td>';
                        //middle part (matches selects)
                        $s .= '<td width="10%" valign="top" align="center" >
                                <div class="select-matching">
                                <select name="choice[' . $questionId . '][' . $numAnswer . ']">';
                        // fills the list-box
                        foreach ($select_items as $key => $val) {
                            // set $debug_mark_answer to true at function start to
                            // show the correct answer with a suffix '-x'
                            $selected = '';
                            if ($debug_mark_answer) {
                                if ($val['id'] == $answerCorrect) {
                                    $selected = 'selected="selected"';
                                }
                            }
                            //$user_choice_array_position
                            if (isset($user_choice_array_position[$numAnswer]) && $val['id'] == $user_choice_array_position[$numAnswer]) {
                                $selected = 'selected="selected"';
                            }
                            $s .= '<option value="' . $val['id'] . '" ' . $selected . '>' . $val['letter'] . '</option>';
                        }
                        // end foreach()
                        $s .= '</select></div></td><td width="5%" class="separate">&nbsp;</td>';
                        $s .= '<td width="40%" valign="top" >';
                        if (isset($select_items[$lines_count])) {
                            $s .= '<div class="text-right"><p class="indent">' . $select_items[$lines_count]['letter'] . '.&nbsp; ' . $select_items[$lines_count]['answer'] . '</p></div>';
                        } else {
                            $s .= '&nbsp;';
                        }
                        $s .= '</td>';
                        $s .= '</tr>';
                        $lines_count++;
                        //if the left side of the "matching" has been completely
                        // shown but the right side still has values to show...
                        if ($lines_count - 1 == $num_suggestions) {
                            // if it remains answers to shown at the right side
                            while (isset($select_items[$lines_count])) {
                                $s .= '<tr>
                                      <td colspan="2"></td>
                                      <td valign="top">';
                                $s .= '<b>' . $select_items[$lines_count]['letter'] . '.</b> ' . $select_items[$lines_count]['answer'];
                                $s .= "</td>\n                                </tr>";
                                $lines_count++;
                            }
                            // end while()
                        }
                        // end if()
                        $matching_correct_answer++;
                    }
                } elseif ($answerType == DRAGGABLE) {
                    if ($answerCorrect != 0) {
                        $parsed_answer = $answer;
                        /*$lines_count = '';
                          $data = $objAnswerTmp->getAnswerByAutoId($numAnswer);
                          $data = $objAnswerTmp->getAnswerByAutoId($data['correct']);
                          $lines_count = $data['answer'];*/
                        $windowId = $questionId . '_' . $lines_count;
                        $s .= '<li class="touch-items" id="' . $windowId . '">';
                        $s .= Display::div($parsed_answer, ['id' => "window_{$windowId}", 'class' => "window{$questionId}_question_draggable exercise-draggable-answer-option"]);
                        $selectedValue = 0;
                        $draggableSelectOptions = [];
                        foreach ($select_items as $key => $val) {
                            if ($debug_mark_answer) {
                                if ($val['id'] == $answerCorrect) {
                                    $selectedValue = $val['id'];
                                }
                            }
                            if (isset($user_choice[$matching_correct_answer]) && $val['id'] == $user_choice[$matching_correct_answer]['answer']) {
                                $selectedValue = $val['id'];
                            }
                            $draggableSelectOptions[$val['id']] = $val['letter'];
                        }
                        $s .= Display::select("choice[{$questionId}][{$numAnswer}]", $draggableSelectOptions, $selectedValue, ['id' => "window_{$windowId}_select", 'class' => 'select_option', 'style' => 'display: none;'], false);
                        if (!empty($answerCorrect) && !empty($selectedValue)) {
                            $s .= <<<JAVASCRIPT
                                <script>
                                    \$(function() {
                                        DraggableAnswer.deleteItem(
                                            \$('#{$questionId}_{$selectedValue}'),
                                            \$('#drop_{$windowId}')
                                        );
                                    });
                                </script>
JAVASCRIPT;
                        }
                        if (isset($select_items[$lines_count])) {
                            $s .= Display::div(Display::tag('b', $select_items[$lines_count]['letter']) . $select_items[$lines_count]['answer'], ['id' => "window_{$windowId}_answer", 'style' => 'display: none;']);
                        } else {
                            $s .= '&nbsp;';
                        }
                        $lines_count++;
                        if ($lines_count - 1 == $num_suggestions) {
                            while (isset($select_items[$lines_count])) {
                                $s .= Display::tag('b', $select_items[$lines_count]['letter']);
                                $s .= $select_items[$lines_count]['answer'];
                                $lines_count++;
                            }
                        }
                        $matching_correct_answer++;
                        $s .= '</li>';
                    }
                } elseif ($answerType == MATCHING_DRAGGABLE) {
                    if ($answerId == 1) {
                        echo $objAnswerTmp->getJs();
                    }
                    if ($answerCorrect != 0) {
                        $parsed_answer = $answer;
                        $windowId = "{$questionId}_{$lines_count}";
                        $s .= <<<HTML
                            <tr>
                                <td widht="45%">
                                    <div id="window_{$windowId}" class="window window_left_question window{$questionId}_question">
                                        <strong>{$lines_count}.</strong> {$parsed_answer}
                                    </div>
                                </td>
                                <td width="10%">
HTML;
                        $selectedValue = 0;
                        $selectedPosition = 0;
                        $questionOptions = [];
                        $iTempt = 0;
                        foreach ($select_items as $key => $val) {
                            if ($debug_mark_answer) {
                                if ($val['id'] == $answerCorrect) {
                                    $selectedValue = $val['id'];
                                    $selectedPosition = $iTempt;
                                }
                            }
                            if (isset($user_choice[$matching_correct_answer]) && $val['id'] == $user_choice[$matching_correct_answer]['answer']) {
                                $selectedValue = $val['id'];
                                $selectedPosition = $iTempt;
                            }
                            $questionOptions[$val['id']] = $val['letter'];
                            $iTempt++;
                        }
                        $s .= Display::select("choice[{$questionId}][{$numAnswer}]", $questionOptions, $selectedValue, ['id' => "window_{$windowId}_select", 'class' => 'hidden'], false);
                        if (!empty($answerCorrect) && !empty($selectedValue)) {
                            // Show connect if is not freeze (question preview)
                            if (!$freeze) {
                                $s .= <<<JAVASCRIPT
                                <script>
                                    \$(document).on('ready', function () {
                                        jsPlumb.ready(function() {
                                            jsPlumb.connect({
                                                source: 'window_{$windowId}',
                                                target: 'window_{$questionId}_{$selectedPosition}_answer',
                                                endpoint: ['Blank', {radius: 15}],
                                                anchors: ['RightMiddle', 'LeftMiddle'],
                                                paintStyle: {strokeStyle: '#8A8888', lineWidth: 8},
                                                connector: [
                                                    MatchingDraggable.connectorType,
                                                    {curvines: MatchingDraggable.curviness}
                                                ]
                                            });
                                        });
                                    });
                                </script>
JAVASCRIPT;
                            }
                        }
                        $s .= <<<HTML
                            </td>
                            <td width="45%">
HTML;
                        if (isset($select_items[$lines_count])) {
                            $s .= <<<HTML
                                <div id="window_{$windowId}_answer" class="window window_right_question">
                                    <strong>{$select_items[$lines_count]['letter']}.</strong> {$select_items[$lines_count]['answer']}
                                </div>
HTML;
                        } else {
                            $s .= '&nbsp;';
                        }
                        $s .= '</td></tr>';
                        $lines_count++;
                        if ($lines_count - 1 == $num_suggestions) {
                            while (isset($select_items[$lines_count])) {
                                $s .= <<<HTML
                                    <tr>
                                        <td colspan="2"></td>
                                        <td>
                                            <strong>{$select_items[$lines_count]['letter']}</strong>
                                            {$select_items[$lines_count]['answer']}
                                        </td>
                                    </tr>
HTML;
                                $lines_count++;
                            }
                        }
                        $matching_correct_answer++;
                    }
                }
            }
            // end for()
            if ($show_comment) {
                $s .= '</table>';
            } elseif (in_array($answerType, [MATCHING, MATCHING_DRAGGABLE, UNIQUE_ANSWER_NO_OPTION, MULTIPLE_ANSWER_TRUE_FALSE, MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE])) {
                $s .= '</table>';
            }
            if ($answerType == DRAGGABLE) {
                $s .= "</ul>";
                $s .= "</div>";
                //clearfix
                $counterAnswer = 1;
                $s .= '<div class="col-md-12"><div class="row">';
                for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
                    $answerCorrect = $objAnswerTmp->isCorrect($answerId);
                    $windowId = $questionId . '_' . $counterAnswer;
                    if ($answerCorrect) {
                        $s .= Display::div($counterAnswer, ['id' => "drop_{$windowId}", 'class' => 'droppable col-md-2']);
                        $counterAnswer++;
                    }
                }
                $s .= '</div>';
                // row
                $s .= '</div>';
                // col-md-12
                $s .= '</div>';
                // col-md-12 ui-widget ui-helper-clearfix
            }
            if (in_array($answerType, [MATCHING, MATCHING_DRAGGABLE])) {
                $s .= '</div>';
                //drag_question
            }
            $s .= '</div>';
            //question_options row
            // destruction of the Answer object
            unset($objAnswerTmp);
            // destruction of the Question object
            unset($objQuestionTmp);
            if ($origin != 'export') {
                echo $s;
            } else {
                return $s;
            }
        } elseif ($answerType == HOT_SPOT || $answerType == HOT_SPOT_DELINEATION) {
            global $exerciseId, $exe_id;
            // Question is a HOT_SPOT
            //checking document/images visibility
            if (api_is_platform_admin() || api_is_course_admin()) {
                $course = api_get_course_info();
                $doc_id = DocumentManager::get_document_id($course, '/images/' . $pictureName);
                if (is_numeric($doc_id)) {
                    $images_folder_visibility = api_get_item_visibility($course, 'document', $doc_id, api_get_session_id());
                    if (!$images_folder_visibility) {
                        //This message is shown only to the course/platform admin if the image is set to visibility = false
                        Display::display_warning_message(get_lang('ChangeTheVisibilityOfTheCurrentImage'));
                    }
                }
            }
            $questionName = $objQuestionTmp->selectTitle();
            $questionDescription = $objQuestionTmp->selectDescription();
            if ($freeze) {
                echo "\n                    <script>\n                        \$(document).on('ready', function () {\n                            new " . ($answerType == HOT_SPOT ? "HotspotQuestion" : "DelineationQuestion") . "({\n                                questionId: {$questionId},\n                                exerciseId: {$exerciseId},\n                                selector: '#hotspot-preview-{$questionId}',\n                                for: 'preview'\n                            });\n                        });\n                    </script>\n                    <div id=\"hotspot-preview-{$questionId}\"></div>\n                ";
                return;
            }
            // Get the answers, make a list
            $objAnswerTmp = new Answer($questionId);
            $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
            // get answers of hotpost
            $answers_hotspot = array();
            for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
                $answers = $objAnswerTmp->selectAnswerByAutoId($objAnswerTmp->selectAutoId($answerId));
                $answers_hotspot[$answers['id']] = $objAnswerTmp->selectAnswer($answerId);
            }
            $answerList = '';
            if ($answerType != HOT_SPOT_DELINEATION) {
                $answerList = '
                    <div class="well well-sm">
                        <h5 class="page-header">' . get_lang('HotspotZones') . '</h5>
                        <ol>
                ';
                if (!empty($answers_hotspot)) {
                    ksort($answers_hotspot);
                    $countAnswers = 1;
                    foreach ($answers_hotspot as $value) {
                        $answerList .= "<li><p>{$countAnswers} - {$value}</p></li>";
                        $countAnswers++;
                    }
                }
                $answerList .= '
                        </ol>
                    </div>
                ';
            }
            if (!$only_questions) {
                if ($show_title) {
                    TestCategory::displayCategoryAndTitle($objQuestionTmp->id);
                    echo '<div class="question_title">' . $current_item . '. ' . $questionName . '</div>';
                }
                //@todo I need to the get the feedback type
                echo <<<HOTSPOT
                    <input type="hidden" name="hidden_hotspot_id" value="{$questionId}" />
                    <div class="exercise_questions">
                        {$questionDescription}
                        <div class="row">
HOTSPOT;
            }
            $canClick = isset($_GET['editQuestion']) ? '0' : (isset($_GET['modifyAnswers']) ? '0' : '1');
            $s .= "\n                            <div class=\"col-sm-8 col-md-9\">\n                                <div class=\"hotspot-image\"></div>\n                                <script>\n                                    \$(document).on('ready', function () {\n                                        new " . ($answerType == HOT_SPOT_DELINEATION ? 'DelineationQuestion' : 'HotspotQuestion') . "({\n                                            questionId: {$questionId},\n                                            exerciseId: {$exe_id},\n                                            selector: '#question_div_' + {$questionId} + ' .hotspot-image',\n                                            for: 'user'\n                                        });\n                                    });\n                                </script>\n                            </div>\n                            <div class=\"col-sm-4 col-md-3\">\n                                {$answerList}\n                            </div>\n            ";
            echo <<<HOTSPOT
                            {$s}
                        </div>
                    </div>
HOTSPOT;
        }
        return $nbrAnswers;
    }
Exemple #7
0
/**
 * Return a string where " are replaced with 2 '
 * It is useful when you pass a PHP variable in a Javascript browser dialog
 * e.g. : alert("<?php get_lang('Message') ?>");
 * and message contains character "
 *
 * @param string $in_text
 * @return string
 */
function convert_double_quote_to_single($in_text)
{
    return api_preg_replace('/"/', "''", $in_text);
}
/**
 * Prepares a string for displaying by highlighting the search results inside, if any.
 * @param string $input    The input string.
 * @return string          The same string with highlighted hits inside.
 *
 * @author Patrick Cool <*****@*****.**>, Ghent University, February 2006 - the initial version.
 * @author Ivan Tcholakov, March 2011 - adaptation for Chamilo LMS.
 */
function prepare4display($input)
{
    static $highlightcolors = array('yellow', '#33CC33', '#3399CC', '#9999FF', '#33CC33');
    static $search;
    if (!isset($search)) {
        if (isset($_POST['search_term'])) {
            $search = $_POST['search_term'];
            // No html at all.
        } elseif (isset($_GET['search'])) {
            $search = $_GET['search'];
        } else {
            $search = '';
        }
    }
    if (!empty($search)) {
        if (strstr($search, '+')) {
            $search_terms = explode('+', $search);
        } else {
            $search_terms[] = trim($search);
        }
        $counter = 0;
        foreach ($search_terms as $key => $search_term) {
            $input = api_preg_replace('/' . preg_quote(trim($search_term), '/') . '/i', '<span style="background-color: ' . $highlightcolors[$counter] . '">$0</span>', $input);
            $counter++;
        }
    }
    // TODO: Security should be implemented outside this function.
    // Change this to COURSEMANAGERLOWSECURITY or COURSEMANAGER to lower filtering and allow more styles (see comments of Security::remove_XSS() method to learn about other levels).
    return Security::remove_XSS($input, STUDENT, true);
}
/**
 * Shows a question
 *
 * @param int    $questionId question id
 * @param bool   $only_questions if true only show the questions, no exercise title
 * @param bool   $origin  i.e = learnpath
 * @param string $current_item current item from the list of questions
 * @param bool   $show_title
 * @param bool   $freeze
 * @param array  $user_choice
 * @param bool   $show_comment
 * @param bool   $exercise_feedback
 * @param bool   $show_answers
 * */
function showQuestion($questionId, $only_questions = false, $origin = false, $current_item = '', $show_title = true, $freeze = false, $user_choice = array(), $show_comment = false, $exercise_feedback = null, $show_answers = false)
{
    // Text direction for the current language
    $is_ltr_text_direction = api_get_text_direction() != 'rtl';
    // Change false to true in the following line to enable answer hinting
    $debug_mark_answer = $show_answers;
    //api_is_allowed_to_edit() && false;
    // Reads question information
    if (!($objQuestionTmp = Question::read($questionId))) {
        // Question not found
        return false;
    }
    if ($exercise_feedback != EXERCISE_FEEDBACK_TYPE_END) {
        $show_comment = false;
    }
    $answerType = $objQuestionTmp->selectType();
    $pictureName = $objQuestionTmp->selectPicture();
    $s = '';
    if ($answerType != HOT_SPOT && $answerType != HOT_SPOT_DELINEATION) {
        // Question is not a hotspot
        if (!$only_questions) {
            $questionDescription = $objQuestionTmp->selectDescription();
            if ($show_title) {
                Testcategory::displayCategoryAndTitle($objQuestionTmp->id);
                echo Display::div($current_item . '. ' . $objQuestionTmp->selectTitle(), array('class' => 'question_title'));
            }
            if (!empty($questionDescription)) {
                echo Display::div($questionDescription, array('class' => 'question_description'));
            }
        }
        if (in_array($answerType, array(FREE_ANSWER, ORAL_EXPRESSION)) && $freeze) {
            return '';
        }
        echo '<div class="question_options">';
        // construction of the Answer object (also gets all answers details)
        $objAnswerTmp = new Answer($questionId);
        $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
        $course_id = api_get_course_int_id();
        $quiz_question_options = Question::readQuestionOption($questionId, $course_id);
        // For "matching" type here, we need something a little bit special
        // because the match between the suggestions and the answers cannot be
        // done easily (suggestions and answers are in the same table), so we
        // have to go through answers first (elems with "correct" value to 0).
        $select_items = array();
        //This will contain the number of answers on the left side. We call them
        // suggestions here, for the sake of comprehensions, while the ones
        // on the right side are called answers
        $num_suggestions = 0;
        if ($answerType == MATCHING) {
            $s .= '<table class="data_table">';
            // Iterate through answers
            $x = 1;
            //mark letters for each answer
            $letter = 'A';
            $answer_matching = array();
            $cpt1 = array();
            for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
                $answerCorrect = $objAnswerTmp->isCorrect($answerId);
                $numAnswer = $objAnswerTmp->selectAutoId($answerId);
                $answer = $objAnswerTmp->selectAnswer($answerId);
                if ($answerCorrect == 0) {
                    // options (A, B, C, ...) that will be put into the list-box
                    // have the "correct" field set to 0 because they are answer
                    $cpt1[$x] = $letter;
                    $answer_matching[$x] = $objAnswerTmp->selectAnswerByAutoId($numAnswer);
                    $x++;
                    $letter++;
                }
            }
            $i = 1;
            $select_items[0]['id'] = 0;
            $select_items[0]['letter'] = '--';
            $select_items[0]['answer'] = '';
            foreach ($answer_matching as $id => $value) {
                $select_items[$i]['id'] = $value['id'];
                $select_items[$i]['letter'] = $cpt1[$id];
                $select_items[$i]['answer'] = $value['answer'];
                $i++;
            }
            $user_choice_array_position = array();
            if (!empty($user_choice)) {
                foreach ($user_choice as $item) {
                    $user_choice_array_position[$item['position']] = $item['answer'];
                }
            }
            $num_suggestions = $nbrAnswers - $x + 1;
        } elseif ($answerType == FREE_ANSWER) {
            $fck_content = isset($user_choice[0]) && !empty($user_choice[0]['answer']) ? $user_choice[0]['answer'] : null;
            $oFCKeditor = new FCKeditor("choice[" . $questionId . "]");
            $oFCKeditor->ToolbarSet = 'TestFreeAnswer';
            $oFCKeditor->Width = '100%';
            $oFCKeditor->Height = '200';
            $oFCKeditor->Value = $fck_content;
            $s .= $oFCKeditor->CreateHtml();
        } elseif ($answerType == ORAL_EXPRESSION) {
            //Add nanog
            if (api_get_setting('enable_nanogong') == 'true') {
                require_once api_get_path(LIBRARY_PATH) . 'nanogong.lib.php';
                //@todo pass this as a parameter
                global $exercise_stat_info, $exerciseId, $exe_id;
                if (!empty($exercise_stat_info)) {
                    $params = array('exercise_id' => $exercise_stat_info['exe_exo_id'], 'exe_id' => $exercise_stat_info['exe_id'], 'question_id' => $questionId);
                } else {
                    $params = array('exercise_id' => $exerciseId, 'exe_id' => 'temp_exe', 'question_id' => $questionId);
                }
                $nano = new Nanogong($params);
                echo $nano->show_button();
            }
            $oFCKeditor = new FCKeditor("choice[" . $questionId . "]");
            $oFCKeditor->ToolbarSet = 'TestFreeAnswer';
            $oFCKeditor->Width = '100%';
            $oFCKeditor->Height = '150';
            $oFCKeditor->ToolbarStartExpanded = false;
            $oFCKeditor->Value = '';
            $s .= $oFCKeditor->CreateHtml();
        }
        // Now navigate through the possible answers, using the max number of
        // answers for the question as a limiter
        $lines_count = 1;
        // a counter for matching-type answers
        if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE || $answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
            $header = Display::tag('th', get_lang('Options'));
            foreach ($objQuestionTmp->options as $item) {
                if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
                    if (in_array($item, $objQuestionTmp->options)) {
                        $header .= Display::tag('th', get_lang($item));
                    } else {
                        $header .= Display::tag('th', $item);
                    }
                } else {
                    $header .= Display::tag('th', $item);
                }
            }
            if ($show_comment) {
                $header .= Display::tag('th', get_lang('Feedback'));
            }
            $s .= '<table class="data_table">';
            $s .= Display::tag('tr', $header, array('style' => 'text-align:left;'));
        }
        if ($show_comment) {
            if (in_array($answerType, array(MULTIPLE_ANSWER, MULTIPLE_ANSWER_COMBINATION, UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION, GLOBAL_MULTIPLE_ANSWER))) {
                $header = Display::tag('th', get_lang('Options'));
                if ($exercise_feedback == EXERCISE_FEEDBACK_TYPE_END) {
                    $header .= Display::tag('th', get_lang('Feedback'));
                }
                $s .= '<table class="data_table">';
                $s .= Display::tag('tr', $header, array('style' => 'text-align:left;'));
            }
        }
        $matching_correct_answer = 0;
        $user_choice_array = array();
        if (!empty($user_choice)) {
            foreach ($user_choice as $item) {
                $user_choice_array[] = $item['answer'];
            }
        }
        for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
            $answer = $objAnswerTmp->selectAnswer($answerId);
            $answerCorrect = $objAnswerTmp->isCorrect($answerId);
            $numAnswer = $objAnswerTmp->selectAutoId($answerId);
            $comment = $objAnswerTmp->selectComment($answerId);
            $attributes = array();
            // Unique answer
            if ($answerType == UNIQUE_ANSWER || $answerType == UNIQUE_ANSWER_NO_OPTION) {
                $input_id = 'choice-' . $questionId . '-' . $answerId;
                if (isset($user_choice[0]['answer']) && $user_choice[0]['answer'] == $numAnswer) {
                    $attributes = array('id' => $input_id, 'checked' => 1, 'selected' => 1);
                } else {
                    $attributes = array('id' => $input_id);
                }
                if ($debug_mark_answer) {
                    if ($answerCorrect) {
                        $attributes['checked'] = 1;
                        $attributes['selected'] = 1;
                    }
                }
                $answer = Security::remove_XSS($answer, STUDENT);
                $s .= Display::input('hidden', 'choice2[' . $questionId . ']', '0');
                $answer_input = '<label class="radio">';
                $answer_input .= Display::input('radio', 'choice[' . $questionId . ']', $numAnswer, $attributes);
                $answer_input .= $answer;
                $answer_input .= '</label>';
                if ($show_comment) {
                    $s .= '<tr><td>';
                    $s .= $answer_input;
                    $s .= '</td>';
                    $s .= '<td>';
                    $s .= $comment;
                    $s .= '</td>';
                    $s .= '</tr>';
                } else {
                    $s .= $answer_input;
                }
            } elseif ($answerType == MULTIPLE_ANSWER || $answerType == MULTIPLE_ANSWER_TRUE_FALSE || $answerType == GLOBAL_MULTIPLE_ANSWER) {
                $input_id = 'choice-' . $questionId . '-' . $answerId;
                $answer = Security::remove_XSS($answer, STUDENT);
                if (in_array($numAnswer, $user_choice_array)) {
                    $attributes = array('id' => $input_id, 'checked' => 1, 'selected' => 1);
                } else {
                    $attributes = array('id' => $input_id);
                }
                if ($debug_mark_answer) {
                    if ($answerCorrect) {
                        $attributes['checked'] = 1;
                        $attributes['selected'] = 1;
                    }
                }
                if ($answerType == MULTIPLE_ANSWER || $answerType == GLOBAL_MULTIPLE_ANSWER) {
                    $s .= '<input type="hidden" name="choice2[' . $questionId . ']" value="0" />';
                    $answer_input = '<label class="checkbox">';
                    $answer_input .= Display::input('checkbox', 'choice[' . $questionId . '][' . $numAnswer . ']', $numAnswer, $attributes);
                    $answer_input .= $answer;
                    $answer_input .= '</label>';
                    if ($show_comment) {
                        $s .= '<tr><td>';
                        $s .= $answer_input;
                        $s .= '</td>';
                        $s .= '<td>';
                        $s .= $comment;
                        $s .= '</td>';
                        $s .= '</tr>';
                    } else {
                        $s .= $answer_input;
                    }
                } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
                    $my_choice = array();
                    if (!empty($user_choice_array)) {
                        foreach ($user_choice_array as $item) {
                            $item = explode(':', $item);
                            $my_choice[$item[0]] = $item[1];
                        }
                    }
                    $s .= '<tr>';
                    $s .= Display::tag('td', $answer);
                    if (!empty($quiz_question_options)) {
                        foreach ($quiz_question_options as $id => $item) {
                            if (isset($my_choice[$numAnswer]) && $id == $my_choice[$numAnswer]) {
                                $attributes = array('checked' => 1, 'selected' => 1);
                            } else {
                                $attributes = array();
                            }
                            if ($debug_mark_answer) {
                                if ($id == $answerCorrect) {
                                    $attributes['checked'] = 1;
                                    $attributes['selected'] = 1;
                                }
                            }
                            $s .= Display::tag('td', Display::input('radio', 'choice[' . $questionId . '][' . $numAnswer . ']', $id, $attributes), array('style' => ''));
                        }
                    }
                    if ($show_comment) {
                        $s .= '<td>';
                        $s .= $comment;
                        $s .= '</td>';
                    }
                    $s .= '</tr>';
                }
            } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION) {
                // multiple answers
                $input_id = 'choice-' . $questionId . '-' . $answerId;
                if (in_array($numAnswer, $user_choice_array)) {
                    $attributes = array('id' => $input_id, 'checked' => 1, 'selected' => 1);
                } else {
                    $attributes = array('id' => $input_id);
                }
                if ($debug_mark_answer) {
                    if ($answerCorrect) {
                        $attributes['checked'] = 1;
                        $attributes['selected'] = 1;
                    }
                }
                $answer = Security::remove_XSS($answer, STUDENT);
                $answer_input = '<input type="hidden" name="choice2[' . $questionId . ']" value="0" />';
                $answer_input .= '<label class="checkbox">';
                $answer_input .= Display::input('checkbox', 'choice[' . $questionId . '][' . $numAnswer . ']', 1, $attributes);
                $answer_input .= $answer;
                $answer_input .= '</label>';
                if ($show_comment) {
                    $s .= '<tr>';
                    $s .= '<td>';
                    $s .= $answer_input;
                    $s .= '</td>';
                    $s .= '<td>';
                    $s .= $comment;
                    $s .= '</td>';
                    $s .= '</tr>';
                } else {
                    $s .= $answer_input;
                }
            } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
                $s .= '<input type="hidden" name="choice2[' . $questionId . ']" value="0" />';
                $my_choice = array();
                if (!empty($user_choice_array)) {
                    foreach ($user_choice_array as $item) {
                        $item = explode(':', $item);
                        $my_choice[$item[0]] = $item[1];
                    }
                }
                $answer = Security::remove_XSS($answer, STUDENT);
                $s .= '<tr>';
                $s .= Display::tag('td', $answer);
                foreach ($objQuestionTmp->options as $key => $item) {
                    if (isset($my_choice[$numAnswer]) && $key == $my_choice[$numAnswer]) {
                        $attributes = array('checked' => 1, 'selected' => 1);
                    } else {
                        $attributes = array();
                    }
                    if ($debug_mark_answer) {
                        if ($key == $answerCorrect) {
                            $attributes['checked'] = 1;
                            $attributes['selected'] = 1;
                        }
                    }
                    $s .= Display::tag('td', Display::input('radio', 'choice[' . $questionId . '][' . $numAnswer . ']', $key, $attributes));
                }
                if ($show_comment) {
                    $s .= '<td>';
                    $s .= $comment;
                    $s .= '</td>';
                }
                $s .= '</tr>';
            } elseif ($answerType == FILL_IN_BLANKS) {
                /*
                 * In the FILL_IN_BLANKS test
                 * you mustn't have [ and ] in the textarea
                 * you mustn't have :: in the textarea
                 * the text to find mustn't be empty or contains only spaces
                 * the text to find mustn't contains HTML tags
                 * the text to find mustn't contains char "
                 */
                list($answer) = explode('::', $answer);
                // $correct_answer_list array of array with correct anwsers 0=> [0=>[\p] 1=>[plop]]
                api_preg_match_all('/\\[[^]]+\\]/', $answer, $correct_answer_list);
                // get student answer to display it if student go back to previous fillBlank answer question in a test
                if (isset($user_choice[0]['answer'])) {
                    api_preg_match_all('/\\[[^]]+\\]/', $user_choice[0]['answer'], $student_answer_list);
                    $student_answer_list_tobecleaned = $student_answer_list[0];
                    $student_answer_list = array();
                    // here we got the student answer in a test
                    // let's clean up the results
                    /*
                    Array
                    (
                        [0] => Array
                        (
                            [0] => [<font color="red"><s>yer</s></font> / <font color="green"><b>ici</b></font>]
                            [1] => [<font color="red"><s>plop</s></font> / <font color="green"><b>/p</b></font>]
                        )
                    )
                    */
                    for ($i = 0; $i < count($student_answer_list_tobecleaned); $i++) {
                        $answer_corrected = $student_answer_list_tobecleaned[$i];
                        /*
                         * we got if student answer is wrong
                         * [<font color="red"><s>rrr</s></font> / <font color="green"><b>/p</b></font>]
                         * or if student answer is good
                         * [plop / <font color="green"><b>plop</b></font>]
                         * or if student didn't answer []
                         */
                        $answer_corrected = api_preg_replace('| / <font color="green"><b>.*$|', '', $answer_corrected);
                        /*
                         * we got [<font color="red"><s>rrr</s></font> or [plop or [
                         */
                        $answer_corrected = api_preg_replace('/^\\[/', '', $answer_corrected);
                        /*
                         * we got <font color="red"><s>rrr</s></font> or plop
                         * non breakable spaces &nbsp;&nbsp;&nbsp; from /main/exercice/exercise.class.php have been removed l 2391 and l 2370
                         */
                        $answer_corrected = api_preg_replace('|^<font color="red"><s>|', '', $answer_corrected);
                        $answer_corrected = api_preg_replace('|</s></font>$|', '', $answer_corrected);
                        $answer_corrected = '[' . $answer_corrected . ']';
                        /*
                         * we got [rrr] or [plop] or []
                         */
                        $student_answer_list[] = $answer_corrected;
                    }
                }
                // If display preview of answer in test view for exemple, set the student answer to the correct answers
                if ($debug_mark_answer) {
                    // contain the rights answers surronded with brackets
                    $student_answer_list = $correct_answer_list[0];
                }
                /*
                Split the response by bracket
                tab_comments is an array with text surrounding the text to find
                we add a space before and after the answer_question to be sure to
                have a block of text before and after [xxx] patterns
                so we have n text to find ([xxx]) and n+1 block of texts before,
                between and after the text to find
                */
                $tab_comments = api_preg_split('/\\[[^]]+\\]/', ' ' . $answer . ' ');
                if (!empty($correct_answer_list) && !empty($student_answer_list)) {
                    $answer = "";
                    $i = 0;
                    foreach ($student_answer_list as $student_item) {
                        // remove surronding brackets
                        $student_response = api_substr($student_item, 1, api_strlen($student_item) - 2);
                        $size = strlen($student_item);
                        $attributes['class'] = detectInputAppropriateClass($size);
                        $answer .= $tab_comments[$i] . Display::input('text', "choice[{$questionId}][]", $student_response, $attributes);
                        $i++;
                    }
                    $answer .= $tab_comments[$i];
                } else {
                    // display exercise with empty input fields
                    // every [xxx] are replaced with an empty input field
                    foreach ($correct_answer_list[0] as $item) {
                        $size = strlen($item);
                        $attributes['class'] = detectInputAppropriateClass($size);
                        $answer = str_replace($item, Display::input('text', "choice[{$questionId}][]", '', $attributes), $answer);
                    }
                    /*$answer = api_preg_replace(
                      '/\[[^]]+\]/',
                      Display::input(
                          'text',
                          "choice[$questionId][]",
                          '',
                          $attributes
                      ), $answer);*/
                }
                $s .= $answer;
            } elseif ($answerType == CALCULATED_ANSWER) {
                /*
                 * In the CALCULATED_ANSWER test
                 * you mustn't have [ and ] in the textarea
                 * you mustn't have @@ in the textarea
                 * the text to find mustn't be empty or contains only spaces
                 * the text to find mustn't contains HTML tags
                 * the text to find mustn't contains char "
                 */
                if ($origin !== null) {
                    global $exe_id;
                    $trackAttempts = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
                    $sqlTrackAttempt = 'SELECT answer FROM ' . $trackAttempts . ' WHERE exe_id=' . $exe_id . ' AND question_id=' . $questionId;
                    $rsLastAttempt = Database::query($sqlTrackAttempt);
                    $rowLastAttempt = Database::fetch_array($rsLastAttempt);
                    $answer = $rowLastAttempt['answer'];
                    if (empty($answer)) {
                        $_SESSION['calculatedAnswerId'][$questionId] = mt_rand(1, $nbrAnswers);
                        $answer = $objAnswerTmp->selectAnswer($_SESSION['calculatedAnswerId'][$questionId]);
                    }
                }
                list($answer) = explode('@@', $answer);
                // $correctAnswerList array of array with correct anwsers 0=> [0=>[\p] 1=>[plop]]
                api_preg_match_all('/\\[[^]]+\\]/', $answer, $correctAnswerList);
                // get student answer to display it if student go back to previous calculated answer question in a test
                if (isset($user_choice[0]['answer'])) {
                    api_preg_match_all('/\\[[^]]+\\]/', $answer, $studentAnswerList);
                    $studentAnswerListTobecleaned = $studentAnswerList[0];
                    $studentAnswerList = array();
                    for ($i = 0; $i < count($studentAnswerListTobecleaned); $i++) {
                        $answerCorrected = $studentAnswerListTobecleaned[$i];
                        $answerCorrected = api_preg_replace('| / <font color="green"><b>.*$|', '', $answerCorrected);
                        $answerCorrected = api_preg_replace('/^\\[/', '', $answerCorrected);
                        $answerCorrected = api_preg_replace('|^<font color="red"><s>|', '', $answerCorrected);
                        $answerCorrected = api_preg_replace('|</s></font>$|', '', $answerCorrected);
                        $answerCorrected = '[' . $answerCorrected . ']';
                        $studentAnswerList[] = $answerCorrected;
                    }
                }
                // If display preview of answer in test view for exemple, set the student answer to the correct answers
                if ($debug_mark_answer) {
                    // contain the rights answers surronded with brackets
                    $studentAnswerList = $correctAnswerList[0];
                }
                /*
                Split the response by bracket
                tabComments is an array with text surrounding the text to find
                we add a space before and after the answerQuestion to be sure to
                have a block of text before and after [xxx] patterns
                so we have n text to find ([xxx]) and n+1 block of texts before,
                between and after the text to find
                */
                $tabComments = api_preg_split('/\\[[^]]+\\]/', ' ' . $answer . ' ');
                if (!empty($correctAnswerList) && !empty($studentAnswerList)) {
                    $answer = "";
                    $i = 0;
                    foreach ($studentAnswerList as $studentItem) {
                        // remove surronding brackets
                        $studentResponse = api_substr($studentItem, 1, api_strlen($studentItem) - 2);
                        $size = strlen($studentItem);
                        $attributes['class'] = detectInputAppropriateClass($size);
                        $answer .= $tabComments[$i] . Display::input('text', "choice[{$questionId}][]", $studentResponse, $attributes);
                        $i++;
                    }
                    $answer .= $tabComments[$i];
                } else {
                    // display exercise with empty input fields
                    // every [xxx] are replaced with an empty input field
                    foreach ($correctAnswerList[0] as $item) {
                        $size = strlen($item);
                        $attributes['class'] = detectInputAppropriateClass($size);
                        $answer = str_replace($item, Display::input('text', "choice[{$questionId}][]", '', $attributes), $answer);
                    }
                }
                if ($origin !== null) {
                    $s = $answer;
                    break;
                } else {
                    $s .= $answer;
                }
            } elseif ($answerType == MATCHING) {
                // matching type, showing suggestions and answers
                // TODO: replace $answerId by $numAnswer
                if ($answerCorrect != 0) {
                    // only show elements to be answered (not the contents of
                    // the select boxes, who are corrrect = 0)
                    $s .= '<tr><td width="45%" valign="top">';
                    $parsed_answer = $answer;
                    //left part questions
                    $s .= ' <span style="float:left; width:8%;"><b>' . $lines_count . '</b>.&nbsp;</span>
    					 	<span style="float:left; width:92%;">' . $parsed_answer . '</span></td>';
                    //middle part (matches selects)
                    $s .= '<td width="10%" valign="top" align="center">&nbsp;&nbsp;
    			            <select name="choice[' . $questionId . '][' . $numAnswer . ']">';
                    // fills the list-box
                    foreach ($select_items as $key => $val) {
                        // set $debug_mark_answer to true at function start to
                        // show the correct answer with a suffix '-x'
                        $selected = '';
                        if ($debug_mark_answer) {
                            if ($val['id'] == $answerCorrect) {
                                $selected = 'selected="selected"';
                            }
                        }
                        //$user_choice_array_position
                        if (isset($user_choice_array_position[$numAnswer]) && $val['id'] == $user_choice_array_position[$numAnswer]) {
                            $selected = 'selected="selected"';
                        }
                        /*if (isset($user_choice_array[$matching_correct_answer]) && $val['id'] == $user_choice_array[$matching_correct_answer]['answer']) {
                              $selected = 'selected="selected"';
                          }*/
                        $s .= '<option value="' . $val['id'] . '" ' . $selected . '>' . $val['letter'] . '</option>';
                    }
                    // end foreach()
                    $s .= '</select></td>';
                    $s .= '<td width="45%" valign="top" >';
                    if (isset($select_items[$lines_count])) {
                        $s .= '<span style="float:left; width:5%;"><b>' . $select_items[$lines_count]['letter'] . '.</b></span>' . '<span style="float:left; width:95%;">' . $select_items[$lines_count]['answer'] . '</span>';
                    } else {
                        $s .= '&nbsp;';
                    }
                    $s .= '</td>';
                    $s .= '</tr>';
                    $lines_count++;
                    //if the left side of the "matching" has been completely
                    // shown but the right side still has values to show...
                    if ($lines_count - 1 == $num_suggestions) {
                        // if it remains answers to shown at the right side
                        while (isset($select_items[$lines_count])) {
                            $s .= '<tr>
    							  <td colspan="2"></td>
    							  <td valign="top">';
                            $s .= '<b>' . $select_items[$lines_count]['letter'] . '.</b> ' . $select_items[$lines_count]['answer'];
                            $s .= "</td>\n    \t\t\t\t\t\t</tr>";
                            $lines_count++;
                        }
                        // end while()
                    }
                    // end if()
                    $matching_correct_answer++;
                }
            }
        }
        // end for()
        if ($show_comment) {
            $s .= '</table>';
        } else {
            if ($answerType == MATCHING || $answerType == UNIQUE_ANSWER_NO_OPTION || $answerType == MULTIPLE_ANSWER_TRUE_FALSE || $answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
                $s .= '</table>';
            }
        }
        $s .= '</div>';
        // destruction of the Answer object
        unset($objAnswerTmp);
        // destruction of the Question object
        unset($objQuestionTmp);
        if ($origin != 'export') {
            echo $s;
        } else {
            return $s;
        }
    } elseif ($answerType == HOT_SPOT || $answerType == HOT_SPOT_DELINEATION) {
        // Question is a HOT_SPOT
        //checking document/images visibility
        if (api_is_platform_admin() || api_is_course_admin()) {
            require_once api_get_path(LIBRARY_PATH) . 'document.lib.php';
            $course = api_get_course_info();
            $doc_id = DocumentManager::get_document_id($course, '/images/' . $pictureName);
            if (is_numeric($doc_id)) {
                $images_folder_visibility = api_get_item_visibility($course, 'document', $doc_id, api_get_session_id());
                if (!$images_folder_visibility) {
                    //This message is shown only to the course/platform admin if the image is set to visibility = false
                    Display::display_warning_message(get_lang('ChangeTheVisibilityOfTheCurrentImage'));
                }
            }
        }
        $questionName = $objQuestionTmp->selectTitle();
        $questionDescription = $objQuestionTmp->selectDescription();
        if ($freeze) {
            echo Display::img($objQuestionTmp->selectPicturePath());
            return;
        }
        // Get the answers, make a list
        $objAnswerTmp = new Answer($questionId);
        $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
        // get answers of hotpost
        $answers_hotspot = array();
        for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
            $answers = $objAnswerTmp->selectAnswerByAutoId($objAnswerTmp->selectAutoId($answerId));
            $answers_hotspot[$answers['id']] = $objAnswerTmp->selectAnswer($answerId);
        }
        // display answers of hotpost order by id
        $answer_list = '<div style="padding: 10px; margin-left: 0px; border: 1px solid #A4A4A4; height: 408px; width: 200px;"><b>' . get_lang('HotspotZones') . '</b><dl>';
        if (!empty($answers_hotspot)) {
            ksort($answers_hotspot);
            foreach ($answers_hotspot as $key => $value) {
                $answer_list .= '<dt>' . $key . '.- ' . $value . '</dt><br />';
            }
        }
        $answer_list .= '</dl></div>';
        if ($answerType == HOT_SPOT_DELINEATION) {
            $answer_list = '';
            $swf_file = 'hotspot_delineation_user';
            $swf_height = 405;
        } else {
            $swf_file = 'hotspot_user';
            $swf_height = 436;
        }
        if (!$only_questions) {
            if ($show_title) {
                Testcategory::displayCategoryAndTitle($objQuestionTmp->id);
                echo '<div class="question_title">' . $current_item . '. ' . $questionName . '</div>';
            }
            //@todo I need to the get the feedback type
            echo '<input type="hidden" name="hidden_hotspot_id" value="' . $questionId . '" />';
            echo '<table class="exercise_questions" >
    			  <tr>
    		  		<td valign="top" colspan="2">';
            echo $questionDescription;
            echo '</td></tr>';
        }
        $canClick = isset($_GET['editQuestion']) ? '0' : (isset($_GET['modifyAnswers']) ? '0' : '1');
        $s .= '<script type="text/javascript" src="../plugin/hotspot/JavaScriptFlashGateway.js"></script>
    					<script src="../plugin/hotspot/hotspot.js" type="text/javascript" ></script>
    					<script type="text/javascript">
    					<!--
    					// Globals
    					// Major version of Flash required
    					var requiredMajorVersion = 7;
    					// Minor version of Flash required
    					var requiredMinorVersion = 0;
    					// Minor version of Flash required
    					var requiredRevision = 0;
    					// the version of javascript supported
    					var jsVersion = 1.0;
    					// -->
    					</script>
    					<script language="VBScript" type="text/vbscript">
    					<!-- // Visual basic helper required to detect Flash Player ActiveX control version information
    					Function VBGetSwfVer(i)
    					  on error resume next
    					  Dim swControl, swVersion
    					  swVersion = 0

    					  set swControl = CreateObject("ShockwaveFlash.ShockwaveFlash." + CStr(i))
    					  if (IsObject(swControl)) then
    					    swVersion = swControl.GetVariable("$version")
    					  end if
    					  VBGetSwfVer = swVersion
    					End Function
    					// -->
    					</script>

    					<script language="JavaScript1.1" type="text/javascript">
    					<!-- // Detect Client Browser type
    					var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
    					var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
    					var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
    					jsVersion = 1.1;
    					// JavaScript helper required to detect Flash Player PlugIn version information
    					function JSGetSwfVer(i) {
    						// NS/Opera version >= 3 check for Flash plugin in plugin array
    						if (navigator.plugins != null && navigator.plugins.length > 0) {
    							if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
    								var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
    					      		var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
    								descArray = flashDescription.split(" ");
    								tempArrayMajor = descArray[2].split(".");
    								versionMajor = tempArrayMajor[0];
    								versionMinor = tempArrayMajor[1];
    								if ( descArray[3] != "" ) {
    									tempArrayMinor = descArray[3].split("r");
    								} else {
    									tempArrayMinor = descArray[4].split("r");
    								}
    					      		versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
    					            flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
    					      	} else {
    								flashVer = -1;
    							}
    						}
    						// MSN/WebTV 2.6 supports Flash 4
    						else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
    						// WebTV 2.5 supports Flash 3
    						else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
    						// older WebTV supports Flash 2
    						else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
    						// Can\'t detect in all other cases
    						else
    						{
    							flashVer = -1;
    						}
    						return flashVer;
    					}
    					// When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available

    					function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) {
    					 	reqVer = parseFloat(reqMajorVer + "." + reqRevision);
    					   	// loop backwards through the versions until we find the newest version
    						for (i=25;i>0;i--) {
    							if (isIE && isWin && !isOpera) {
    								versionStr = VBGetSwfVer(i);
    							} else {
    								versionStr = JSGetSwfVer(i);
    							}
    							if (versionStr == -1 ) {
    								return false;
    							} else if (versionStr != 0) {
    								if(isIE && isWin && !isOpera) {
    									tempArray         = versionStr.split(" ");
    									tempString        = tempArray[1];
    									versionArray      = tempString .split(",");
    								} else {
    									versionArray      = versionStr.split(".");
    								}
    								versionMajor      = versionArray[0];
    								versionMinor      = versionArray[1];
    								versionRevision   = versionArray[2];

    								versionString     = versionMajor + "." + versionRevision;   // 7.0r24 == 7.24
    								versionNum        = parseFloat(versionString);
    					        	// is the major.revision >= requested major.revision AND the minor version >= requested minor
    								if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
    									return true;
    								} else {
    									return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
    								}
    							}
    						}
    					}
    					// -->
    					</script>';
        $s .= '<tr><td valign="top" colspan="2" width="520"><table><tr><td width="520">
    				<script>
    					<!--
    					// Version check based upon the values entered above in "Globals"
    					var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

    					// Check to see if the version meets the requirements for playback
    					if (hasReqestedVersion) {  // if we\'ve detected an acceptable version
    					    var oeTags = \'<object type="application/x-shockwave-flash" data="../plugin/hotspot/' . $swf_file . '.swf?modifyAnswers=' . $questionId . '&amp;canClick:' . $canClick . '" width="600" height="' . $swf_height . '">\'
    					    			+ \'<param name="wmode" value="transparent">\'
    									+ \'<param name="movie" value="../plugin/hotspot/' . $swf_file . '.swf?modifyAnswers=' . $questionId . '&amp;canClick:' . $canClick . '" />\'
    									+ \'<\\/object>\';
    					    document.write(oeTags);   // embed the Flash Content SWF when all tests are passed
    					} else {  // flash is too old or we can\'t detect the plugin
    						var alternateContent = "Error<br \\/>"
    							+ "Hotspots requires Macromedia Flash 7.<br \\/>"
    							+ "<a href=\\"http://www.macromedia.com/go/getflash/\\">Get Flash<\\/a>";
    						document.write(alternateContent);  // insert non-flash content
    					}
    					// -->
    				</script>
    				</td>
    				<td valign="top" align="left">' . $answer_list . '</td></tr>
    				</table>
    	</td></tr>';
        echo $s;
        echo '</table>';
    }
    return $nbrAnswers;
}
 /**
  * @param string $file
  * @param bool $updateSession options:
  *  true: if the session exists it will be updated.
  *  false: if session exists a new session will be created adding a counter session1, session2, etc
  * @param int $defaultUserId
  * @param mixed $logger
  * @param array $extraFields convert a file row to an extra field. Example in CSV file there's a SessionID then it will
  * converted to extra_external_session_id if you set this: array('SessionId' => 'extra_external_session_id')
  * @param string $extraFieldId
  * @param int $daysCoachAccessBeforeBeginning
  * @param int $daysCoachAccessAfterBeginning
  * @param int $sessionVisibility
  * @param array $fieldsToAvoidUpdate
  * @param bool $deleteUsersNotInList
  * @param bool $updateCourseCoaches
  * @param bool $sessionWithCoursesModifier
  * @param int $showDescription
  * @return array
  */
 static function importCSV($file, $updateSession, $defaultUserId = null, $logger = null, $extraFields = array(), $extraFieldId = null, $daysCoachAccessBeforeBeginning = null, $daysCoachAccessAfterBeginning = null, $sessionVisibility = 1, $fieldsToAvoidUpdate = array(), $deleteUsersNotInList = false, $updateCourseCoaches = false, $sessionWithCoursesModifier = false, $addOriginalCourseTeachersAsCourseSessionCoaches = true, $removeAllTeachersFromCourse = true, $showDescription = null)
 {
     $content = file($file);
     $error_message = null;
     $session_counter = 0;
     if (empty($defaultUserId)) {
         $defaultUserId = api_get_user_id();
     }
     $eol = PHP_EOL;
     if (PHP_SAPI != 'cli') {
         $eol = '<br />';
     }
     $debug = false;
     if (isset($logger)) {
         $debug = true;
     }
     $extraParameters = null;
     if (!empty($daysCoachAccessBeforeBeginning) && !empty($daysCoachAccessAfterBeginning)) {
         $extraParameters .= ' , nb_days_access_before_beginning = ' . intval($daysCoachAccessBeforeBeginning);
         $extraParameters .= ' , nb_days_access_after_end = ' . intval($daysCoachAccessAfterBeginning);
     }
     if (!is_null($showDescription)) {
         $extraParameters .= ' , show_description = ' . intval($showDescription);
     }
     $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
     $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
     $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
     $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
     $sessions = array();
     if (!api_strstr($content[0], ';')) {
         $error_message = get_lang('NotCSV');
     } else {
         $tag_names = array();
         foreach ($content as $key => $enreg) {
             $enreg = explode(';', trim($enreg));
             if ($key) {
                 foreach ($tag_names as $tag_key => $tag_name) {
                     $sessions[$key - 1][$tag_name] = $enreg[$tag_key];
                 }
             } else {
                 foreach ($enreg as $tag_name) {
                     $tag_names[] = api_preg_replace('/[^a-zA-Z0-9_\\-]/', '', $tag_name);
                 }
                 if (!in_array('SessionName', $tag_names) || !in_array('DateStart', $tag_names) || !in_array('DateEnd', $tag_names)) {
                     $error_message = get_lang('NoNeededData');
                     break;
                 }
             }
         }
         $sessionList = array();
         // Looping the sessions.
         foreach ($sessions as $enreg) {
             $user_counter = 0;
             $course_counter = 0;
             if (isset($extraFields) && !empty($extraFields)) {
                 foreach ($extraFields as $original => $to) {
                     $enreg[$to] = isset($enreg[$original]) ? $enreg[$original] : null;
                 }
             }
             $session_name = Database::escape_string($enreg['SessionName']);
             // Default visibility
             $visibilityAfterExpirationPerSession = $sessionVisibility;
             if (isset($enreg['VisibilityAfterExpiration'])) {
                 $visibility = $enreg['VisibilityAfterExpiration'];
                 switch ($visibility) {
                     case 'read_only':
                         $visibilityAfterExpirationPerSession = SESSION_VISIBLE_READ_ONLY;
                         break;
                     case 'accessible':
                         $visibilityAfterExpirationPerSession = SESSION_VISIBLE;
                         break;
                     case 'not_accessible':
                         $visibilityAfterExpirationPerSession = SESSION_INVISIBLE;
                         break;
                 }
             }
             if (empty($session_name)) {
                 continue;
             }
             $date_start = $enreg['DateStart'];
             $date_end = $enreg['DateEnd'];
             $session_category_id = isset($enreg['SessionCategory']) ? $enreg['SessionCategory'] : null;
             $sessionDescription = isset($enreg['SessionDescription']) ? $enreg['SessionDescription'] : null;
             $extraSessionParameters = null;
             if (!empty($sessionDescription)) {
                 $extraSessionParameters = " , description = '" . Database::escape_string($sessionDescription) . "'";
             }
             // Searching a general coach.
             if (!empty($enreg['Coach'])) {
                 $coach_id = UserManager::get_user_id_from_username($enreg['Coach']);
                 if ($coach_id === false) {
                     // If the coach-user does not exist - I'm the coach.
                     $coach_id = $defaultUserId;
                 }
             } else {
                 $coach_id = $defaultUserId;
             }
             if (!$updateSession) {
                 // Always create a session.
                 $unique_name = false;
                 $i = 0;
                 // Change session name, verify that session doesn't exist.
                 $suffix = null;
                 while (!$unique_name) {
                     if ($i > 1) {
                         $suffix = ' - ' . $i;
                     }
                     $sql = 'SELECT 1 FROM ' . $tbl_session . '
                             WHERE name="' . $session_name . $suffix . '"';
                     $rs = Database::query($sql);
                     if (Database::result($rs, 0, 0)) {
                         $i++;
                     } else {
                         $unique_name = true;
                         $session_name .= $suffix;
                     }
                 }
                 $sessionCondition = '';
                 if (!empty($session_category_id)) {
                     $sessionCondition = "session_category_id = '{$session_category_id}',";
                 }
                 // Creating the session.
                 $sql = "INSERT IGNORE INTO {$tbl_session} SET\n                            name = '" . $session_name . "',\n                            id_coach = '{$coach_id}',\n                            access_start_date = '{$date_start}',\n                            access_end_date = '{$date_end}',\n                            visibility = '{$visibilityAfterExpirationPerSession}',\n                            {$sessionCondition}\n                            session_admin_id = " . intval($defaultUserId) . $extraParameters . $extraSessionParameters;
                 Database::query($sql);
                 $session_id = Database::insert_id();
                 if ($debug) {
                     if ($session_id) {
                         foreach ($enreg as $key => $value) {
                             if (substr($key, 0, 6) == 'extra_') {
                                 //an extra field
                                 self::update_session_extra_field_value($session_id, substr($key, 6), $value);
                             }
                         }
                         $logger->addInfo("Sessions - Session created: #{$session_id} - {$session_name}");
                     } else {
                         $logger->addError("Sessions - Session NOT created: {$session_name}");
                     }
                 }
                 $session_counter++;
             } else {
                 $sessionId = null;
                 if (isset($extraFields) && !empty($extraFields) && !empty($enreg['extra_' . $extraFieldId])) {
                     $sessionId = self::getSessionIdFromOriginalId($enreg['extra_' . $extraFieldId], $extraFieldId);
                     if (empty($sessionId)) {
                         $my_session_result = false;
                     } else {
                         $my_session_result = true;
                     }
                 } else {
                     $my_session_result = self::get_session_by_name($enreg['SessionName']);
                 }
                 if ($my_session_result === false) {
                     // Creating a session.
                     $sql = "INSERT IGNORE INTO {$tbl_session} SET\n                                name = '{$session_name}',\n                                id_coach = '{$coach_id}',\n                                access_start_date = '{$date_start}',\n                                access_end_date = '{$date_end}',\n                                visibility = '{$visibilityAfterExpirationPerSession}',\n                                session_category_id = '{$session_category_id}' " . $extraParameters . $extraSessionParameters;
                     Database::query($sql);
                     // We get the last insert id.
                     $my_session_result = SessionManager::get_session_by_name($enreg['SessionName']);
                     $session_id = $my_session_result['id'];
                     if ($session_id) {
                         foreach ($enreg as $key => $value) {
                             if (substr($key, 0, 6) == 'extra_') {
                                 //an extra field
                                 self::update_session_extra_field_value($session_id, substr($key, 6), $value);
                             }
                         }
                         if ($debug) {
                             $logger->addInfo("Sessions - #{$session_id} created: {$session_name}");
                         }
                         // Delete session-user relation only for students
                         $sql = "DELETE FROM {$tbl_session_user}\n                                    WHERE session_id = '{$session_id}' AND relation_type <> " . SESSION_RELATION_TYPE_RRHH;
                         Database::query($sql);
                         $sql = "DELETE FROM {$tbl_session_course} WHERE session_id = '{$session_id}'";
                         Database::query($sql);
                         // Delete session-course-user relationships students and coaches.
                         if ($updateCourseCoaches) {
                             $sql = "DELETE FROM {$tbl_session_course_user}\n                                        WHERE session_id = '{$session_id}' AND status in ('0', '2')";
                             Database::query($sql);
                         } else {
                             // Delete session-course-user relation ships *only* for students.
                             $sql = "DELETE FROM {$tbl_session_course_user}\n                                        WHERE session_id = '{$session_id}' AND status <> 2";
                             Database::query($sql);
                         }
                     }
                 } else {
                     if ($debug) {
                         $logger->addError("Sessions - Session to be updated: {$session_name}");
                     }
                     // Updating the session.
                     $params = array('id_coach' => $coach_id, 'access_start_date' => $date_start, 'access_end_date' => $date_end, 'visibility' => $visibilityAfterExpirationPerSession, 'session_category_id' => $session_category_id);
                     if (!empty($sessionDescription)) {
                         $params['description'] = $sessionDescription;
                     }
                     if (!empty($fieldsToAvoidUpdate)) {
                         foreach ($fieldsToAvoidUpdate as $field) {
                             unset($params[$field]);
                         }
                     }
                     if (isset($sessionId) && !empty($sessionId)) {
                         if (!empty($enreg['SessionName'])) {
                             $params['name'] = $enreg['SessionName'];
                         }
                         $session_id = $sessionId;
                     } else {
                         $row = Database::query("SELECT id FROM {$tbl_session} WHERE name = '{$session_name}'");
                         list($session_id) = Database::fetch_array($row);
                     }
                     if ($session_id) {
                         if ($debug) {
                             $logger->addError("Sessions - Session to be updated #{$session_id}");
                         }
                         $sessionInfo = api_get_session_info($session_id);
                         $params['show_description'] = isset($sessionInfo['show_description']) ? $sessionInfo['show_description'] : intval($showDescription);
                         if (!empty($daysCoachAccessBeforeBeginning) && !empty($daysCoachAccessAfterBeginning)) {
                             if (empty($sessionInfo['nb_days_access_before_beginning']) || !empty($sessionInfo['nb_days_access_before_beginning']) && $sessionInfo['nb_days_access_before_beginning'] < $daysCoachAccessBeforeBeginning) {
                                 $params['nb_days_access_before_beginning'] = intval($daysCoachAccessBeforeBeginning);
                             }
                             if (empty($sessionInfo['nb_days_access_after_end']) || !empty($sessionInfo['nb_days_access_after_end']) && $sessionInfo['nb_days_access_after_end'] < $daysCoachAccessAfterBeginning) {
                                 $params['nb_days_access_after_end'] = intval($daysCoachAccessAfterBeginning);
                             }
                         }
                         Database::update($tbl_session, $params, array('id = ?' => $session_id));
                         foreach ($enreg as $key => $value) {
                             if (substr($key, 0, 6) == 'extra_') {
                                 //an extra field
                                 self::update_session_extra_field_value($session_id, substr($key, 6), $value);
                             }
                         }
                         // Delete session-user relation only for students
                         $sql = "DELETE FROM {$tbl_session_user}\n                                    WHERE session_id = '{$session_id}' AND relation_type <> " . SESSION_RELATION_TYPE_RRHH;
                         Database::query($sql);
                         $sql = "DELETE FROM {$tbl_session_course} WHERE session_id = '{$session_id}'";
                         Database::query($sql);
                         // Delete session-course-user relationships students and coaches.
                         if ($updateCourseCoaches) {
                             $sql = "DELETE FROM {$tbl_session_course_user}\n                                        WHERE session_id = '{$session_id}' AND status in ('0', '2')";
                             Database::query($sql);
                         } else {
                             // Delete session-course-user relation ships *only* for students.
                             $sql = "DELETE FROM {$tbl_session_course_user}\n                                        WHERE session_id = '{$session_id}' AND status <> 2";
                             Database::query($sql);
                         }
                     } else {
                         if ($debug) {
                             $logger->addError("Sessions - Session not found");
                         }
                     }
                 }
                 $session_counter++;
             }
             $sessionList[] = $session_id;
             $users = explode('|', $enreg['Users']);
             // Adding the relationship "Session - User" for students
             $userList = array();
             if (is_array($users)) {
                 foreach ($users as $user) {
                     $user_id = UserManager::get_user_id_from_username($user);
                     if ($user_id !== false) {
                         $userList[] = $user_id;
                         // Insert new users.
                         $sql = "INSERT IGNORE INTO {$tbl_session_user} SET\n                                    user_id = '{$user_id}',\n                                    session_id = '{$session_id}',\n                                    registered_at = '" . api_get_utc_datetime() . "'";
                         Database::query($sql);
                         if ($debug) {
                             $logger->addInfo("Sessions - Adding User #{$user_id} ({$user}) to session #{$session_id}");
                         }
                         $user_counter++;
                     }
                 }
             }
             if ($deleteUsersNotInList) {
                 // Getting user in DB in order to compare to the new list.
                 $usersListInDatabase = self::get_users_by_session($session_id, 0);
                 if (!empty($usersListInDatabase)) {
                     if (empty($userList)) {
                         foreach ($usersListInDatabase as $userInfo) {
                             self::unsubscribe_user_from_session($session_id, $userInfo['user_id']);
                         }
                     } else {
                         foreach ($usersListInDatabase as $userInfo) {
                             if (!in_array($userInfo['user_id'], $userList)) {
                                 self::unsubscribe_user_from_session($session_id, $userInfo['user_id']);
                             }
                         }
                     }
                 }
             }
             $courses = explode('|', $enreg['Courses']);
             // See BT#6449
             $onlyAddFirstCoachOrTeacher = false;
             if ($sessionWithCoursesModifier) {
                 if (count($courses) >= 2) {
                     // Only first teacher in course session;
                     $onlyAddFirstCoachOrTeacher = true;
                     // Remove all teachers from course.
                     $removeAllTeachersFromCourse = false;
                 }
             }
             foreach ($courses as $course) {
                 $courseArray = bracketsToArray($course);
                 $course_code = $courseArray[0];
                 if (CourseManager::course_exists($course_code)) {
                     $courseInfo = api_get_course_info($course_code);
                     $courseId = $courseInfo['real_id'];
                     // Adding the course to a session.
                     $sql = "INSERT IGNORE INTO {$tbl_session_course}\n                                SET c_id = '{$courseId}', session_id='{$session_id}'";
                     Database::query($sql);
                     SessionManager::installCourse($session_id, $courseInfo['real_id']);
                     if ($debug) {
                         $logger->addInfo("Sessions - Adding course '{$course_code}' to session #{$session_id}");
                     }
                     $course_counter++;
                     $course_coaches = isset($courseArray[1]) ? $courseArray[1] : null;
                     $course_users = isset($courseArray[2]) ? $courseArray[2] : null;
                     $course_users = explode(',', $course_users);
                     $course_coaches = explode(',', $course_coaches);
                     // Checking if the flag is set TeachersWillBeAddedAsCoachInAllCourseSessions (course_edit.php)
                     $addTeachersToSession = true;
                     if (array_key_exists('add_teachers_to_sessions_courses', $courseInfo)) {
                         $addTeachersToSession = $courseInfo['add_teachers_to_sessions_courses'];
                     }
                     // If any user provided for a course, use the users array.
                     if (empty($course_users)) {
                         if (!empty($userList)) {
                             SessionManager::subscribe_users_to_session_course($userList, $session_id, $course_code);
                             if ($debug) {
                                 $msg = "Sessions - Adding student list " . implode(', #', $userList) . " to course: '{$course_code}' and session #{$session_id}";
                                 $logger->addInfo($msg);
                             }
                         }
                     }
                     // Adding coaches to session course user.
                     if (!empty($course_coaches)) {
                         $savedCoaches = array();
                         // only edit if add_teachers_to_sessions_courses is set.
                         if ($addTeachersToSession) {
                             if ($addOriginalCourseTeachersAsCourseSessionCoaches) {
                                 // Adding course teachers as course session teachers.
                                 $alreadyAddedTeachers = CourseManager::get_teacher_list_from_course_code($course_code);
                                 if (!empty($alreadyAddedTeachers)) {
                                     $teachersToAdd = array();
                                     foreach ($alreadyAddedTeachers as $user) {
                                         $teachersToAdd[] = $user['username'];
                                     }
                                     $course_coaches = array_merge($course_coaches, $teachersToAdd);
                                 }
                             }
                             foreach ($course_coaches as $course_coach) {
                                 $coach_id = UserManager::get_user_id_from_username($course_coach);
                                 if ($coach_id !== false) {
                                     // Just insert new coaches
                                     SessionManager::updateCoaches($session_id, $courseId, array($coach_id), false);
                                     if ($debug) {
                                         $logger->addInfo("Sessions - Adding course coach: user #{$coach_id} ({$course_coach}) to course: '{$course_code}' and session #{$session_id}");
                                     }
                                     $savedCoaches[] = $coach_id;
                                 } else {
                                     $error_message .= get_lang('UserDoesNotExist') . ' : ' . $course_coach . $eol;
                                 }
                             }
                         }
                         // Custom courses/session coaches
                         $teacherToAdd = null;
                         // Only one coach is added.
                         if ($onlyAddFirstCoachOrTeacher == true) {
                             foreach ($course_coaches as $course_coach) {
                                 $coach_id = UserManager::get_user_id_from_username($course_coach);
                                 if ($coach_id !== false) {
                                     $teacherToAdd = $coach_id;
                                     break;
                                 }
                             }
                             // Un subscribe everyone that's not in the list.
                             $teacherList = CourseManager::get_teacher_list_from_course_code($course_code);
                             if (!empty($teacherList)) {
                                 foreach ($teacherList as $teacher) {
                                     if ($teacherToAdd != $teacher['user_id']) {
                                         CourseManager::unsubscribe_user($teacher['user_id'], $course_code);
                                     }
                                 }
                             }
                             if (!empty($teacherToAdd)) {
                                 SessionManager::updateCoaches($session_id, $courseId, array($teacherToAdd), true);
                                 CourseManager::subscribe_user($teacherToAdd, $course_code, COURSEMANAGER);
                             }
                         }
                         // See BT#6449#note-195
                         // All coaches are added.
                         if ($removeAllTeachersFromCourse) {
                             $teacherToAdd = null;
                             foreach ($course_coaches as $course_coach) {
                                 $coach_id = UserManager::get_user_id_from_username($course_coach);
                                 if ($coach_id !== false) {
                                     $teacherToAdd[] = $coach_id;
                                 }
                             }
                             if (!empty($teacherToAdd)) {
                                 // Deleting all course teachers and adding the only coach as teacher.
                                 $teacherList = CourseManager::get_teacher_list_from_course_code($course_code);
                                 if (!empty($teacherList)) {
                                     foreach ($teacherList as $teacher) {
                                         if (!in_array($teacher['user_id'], $teacherToAdd)) {
                                             CourseManager::unsubscribe_user($teacher['user_id'], $course_code);
                                         }
                                     }
                                 }
                                 foreach ($teacherToAdd as $teacherId) {
                                     CourseManager::subscribe_user($teacherId, $course_code, COURSEMANAGER);
                                 }
                             }
                         }
                         // Continue default behaviour.
                         if ($onlyAddFirstCoachOrTeacher == false) {
                             // Checking one more time see BT#6449#note-149
                             $coaches = SessionManager::getCoachesByCourseSession($session_id, $courseId);
                             // Update coaches if only there's 1 course see BT#6449#note-189
                             if (empty($coaches) || count($courses) == 1) {
                                 foreach ($course_coaches as $course_coach) {
                                     $course_coach = trim($course_coach);
                                     $coach_id = UserManager::get_user_id_from_username($course_coach);
                                     if ($coach_id !== false) {
                                         // Just insert new coaches
                                         SessionManager::updateCoaches($session_id, $courseId, array($coach_id), false);
                                         if ($debug) {
                                             $logger->addInfo("Sessions - Adding course coach: user #{$coach_id} ({$course_coach}) to course: '{$course_code}' and session #{$session_id}");
                                         }
                                         $savedCoaches[] = $coach_id;
                                     } else {
                                         $error_message .= get_lang('UserDoesNotExist') . ' : ' . $course_coach . $eol;
                                     }
                                 }
                             }
                         }
                     }
                     // Adding Students, updating relationship "Session - Course - User".
                     $course_users = array_filter($course_users);
                     if (!empty($course_users)) {
                         foreach ($course_users as $user) {
                             $user_id = UserManager::get_user_id_from_username($user);
                             if ($user_id !== false) {
                                 SessionManager::subscribe_users_to_session_course(array($user_id), $session_id, $course_code);
                                 if ($debug) {
                                     $logger->addInfo("Sessions - Adding student: user #{$user_id} ({$user}) to course: '{$course_code}' and session #{$session_id}");
                                 }
                             } else {
                                 $error_message .= get_lang('UserDoesNotExist') . ': ' . $user . $eol;
                             }
                         }
                     }
                     $inserted_in_course[$course_code] = $courseInfo['title'];
                 }
             }
             $access_url_id = api_get_current_access_url_id();
             UrlManager::add_session_to_url($session_id, $access_url_id);
             $sql = "UPDATE {$tbl_session} SET nbr_users = '{$user_counter}', nbr_courses = '{$course_counter}' WHERE id = '{$session_id}'";
             Database::query($sql);
         }
     }
     return array('error_message' => $error_message, 'session_counter' => $session_counter, 'session_list' => $sessionList);
 }
    /**
     * Shows a question
     * @param Question $objQuestionTmp
     * @param bool $only_questions if true only show the questions, no exercise title
     * @param bool $origin origin i.e = learnpath
     * @param string $current_item current item from the list of questions
     * @param bool $show_title
     * @param bool $freeze
     * @param array $user_choice
     * @param bool $show_comment
     * @param null $exercise_feedback
     * @param bool $show_answers
     * @param null $modelType
     * @param bool $categoryMinusOne
     * @return bool|null|string
     */
    public function showQuestion(Question $objQuestionTmp, $only_questions = false, $origin = false, $current_item = '', $show_title = true, $freeze = false, $user_choice = array(), $show_comment = false, $exercise_feedback = null, $show_answers = false, $modelType = null, $categoryMinusOne = true)
    {
        // Text direction for the current language
        //$is_ltr_text_direction = api_get_text_direction() != 'rtl';
        // Change false to true in the following line to enable answer hinting
        $debug_mark_answer = $show_answers;
        //api_is_allowed_to_edit() && false;
        // Reads question information
        if (!$objQuestionTmp) {
            // Question not found
            return false;
        }
        $html = null;
        $questionId = $objQuestionTmp->id;
        if ($exercise_feedback != EXERCISE_FEEDBACK_TYPE_END) {
            $show_comment = false;
        }
        $answerType = $objQuestionTmp->selectType();
        $pictureName = $objQuestionTmp->selectPicture();
        $s = null;
        $form = new FormValidator('question');
        $renderer = $form->defaultRenderer();
        $form_template = '{content}';
        $renderer->setFormTemplate($form_template);
        if ($answerType != HOT_SPOT && $answerType != HOT_SPOT_DELINEATION) {
            // Question is not a hotspot
            if (!$only_questions) {
                $questionDescription = $objQuestionTmp->selectDescription();
                if ($show_title) {
                    $categoryName = TestCategory::getCategoryNamesForQuestion($objQuestionTmp->id, null, true, $categoryMinusOne);
                    $html .= $categoryName;
                    $html .= Display::div($current_item . '. ' . $objQuestionTmp->selectTitle(), array('class' => 'question_title'));
                    if (!empty($questionDescription)) {
                        $html .= Display::div($questionDescription, array('class' => 'question_description'));
                    }
                } else {
                    $html .= '<div class="media">';
                    $html .= '<div class="pull-left">';
                    $html .= '<div class="media-object">';
                    $html .= Display::div($current_item, array('class' => 'question_no_title'));
                    $html .= '</div>';
                    $html .= '</div>';
                    $html .= '<div class="media-body">';
                    if (!empty($questionDescription)) {
                        $html .= Display::div($questionDescription, array('class' => 'question_description'));
                    }
                    $html .= '</div>';
                    $html .= '</div>';
                }
            }
            if (in_array($answerType, array(FREE_ANSWER, ORAL_EXPRESSION)) && $freeze) {
                return null;
            }
            $html .= '<div class="question_options">';
            // construction of the Answer object (also gets all answers details)
            $objAnswerTmp = new Answer($questionId, null, $this);
            $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
            $course_id = api_get_course_int_id();
            $quiz_question_options = Question::readQuestionOption($questionId, $course_id);
            // For "matching" type here, we need something a little bit special
            // because the match between the suggestions and the answers cannot be
            // done easily (suggestions and answers are in the same table), so we
            // have to go through answers first (elems with "correct" value to 0).
            $select_items = array();
            //This will contain the number of answers on the left side. We call them
            // suggestions here, for the sake of comprehensions, while the ones
            // on the right side are called answers
            $num_suggestions = 0;
            if ($answerType == MATCHING || $answerType == DRAGGABLE) {
                if ($answerType == DRAGGABLE) {
                    $s .= '<div class="ui-widget ui-helper-clearfix">
                            <ul class="drag_question ui-helper-reset ui-helper-clearfix">';
                } else {
                    $s .= '<div id="drag' . $questionId . '_question" class="drag_question">';
                    $s .= '<table class="data_table">';
                }
                $j = 1;
                //iterate through answers
                $letter = 'A';
                //mark letters for each answer
                $answer_matching = array();
                $capital_letter = array();
                //for ($answerId=1; $answerId <= $nbrAnswers; $answerId++) {
                foreach ($objAnswerTmp->answer as $answerId => $answer_item) {
                    $answerCorrect = $objAnswerTmp->isCorrect($answerId);
                    $answer = $objAnswerTmp->selectAnswer($answerId);
                    if ($answerCorrect == 0) {
                        // options (A, B, C, ...) that will be put into the list-box
                        // have the "correct" field set to 0 because they are answer
                        $capital_letter[$j] = $letter;
                        //$answer_matching[$j]=$objAnswerTmp->selectAnswerByAutoId($numAnswer);
                        $answer_matching[$j] = array('id' => $answerId, 'answer' => $answer);
                        $j++;
                        $letter++;
                    }
                }
                $i = 1;
                $select_items[0]['id'] = 0;
                $select_items[0]['letter'] = '--';
                $select_items[0]['answer'] = '';
                foreach ($answer_matching as $id => $value) {
                    $select_items[$i]['id'] = $value['id'];
                    $select_items[$i]['letter'] = $capital_letter[$id];
                    $select_items[$i]['answer'] = $value['answer'];
                    $i++;
                }
                $num_suggestions = $nbrAnswers - $j + 1;
            } elseif ($answerType == FREE_ANSWER) {
                $content = isset($user_choice[0]) && !empty($user_choice[0]['answer']) ? $user_choice[0]['answer'] : null;
                $toolBar = 'TestFreeAnswer';
                if ($modelType == EXERCISE_MODEL_TYPE_COMMITTEE) {
                    $toolBar = 'TestFreeAnswerStrict';
                }
                $form->addElement('html_editor', "choice[" . $questionId . "]", null, array('id' => "choice[" . $questionId . "]"), array('ToolbarSet' => $toolBar));
                $form->setDefaults(array("choice[" . $questionId . "]" => $content));
                $s .= $form->return_form();
            } elseif ($answerType == ORAL_EXPRESSION) {
                // Add nanogong
                if (api_get_setting('document.enable_nanogong') == 'true') {
                    //@todo pass this as a parameter
                    global $exercise_stat_info, $exerciseId;
                    if (!empty($exercise_stat_info)) {
                        $params = array('exercise_id' => $exercise_stat_info['exe_exo_id'], 'exe_id' => $exercise_stat_info['exe_id'], 'question_id' => $questionId);
                    } else {
                        $params = array('exercise_id' => $exerciseId, 'exe_id' => 'temp_exe', 'question_id' => $questionId);
                    }
                    $nano = new Nanogong($params);
                    $s .= $nano->show_button();
                }
                $form->addElement('html_editor', "choice[" . $questionId . "]", null, array('id' => "choice[" . $questionId . "]"), array('ToolbarSet' => 'TestFreeAnswer'));
                //$form->setDefaults(array("choice[".$questionId."]" => $content));
                $s .= $form->return_form();
            }
            // Now navigate through the possible answers, using the max number of
            // answers for the question as a limiter
            $lines_count = 1;
            // a counter for matching-type answers
            if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE || $answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
                $header = Display::tag('th', get_lang('Options'));
                foreach ($objQuestionTmp->options as $item) {
                    $header .= Display::tag('th', $item);
                }
                if ($show_comment) {
                    $header .= Display::tag('th', get_lang('Feedback'));
                }
                $s .= '<table class="data_table">';
                $s .= Display::tag('tr', $header, array('style' => 'text-align:left;'));
            }
            if ($show_comment) {
                if (in_array($answerType, array(MULTIPLE_ANSWER, MULTIPLE_ANSWER_COMBINATION, UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION, GLOBAL_MULTIPLE_ANSWER))) {
                    $header = Display::tag('th', get_lang('Options'));
                    if ($exercise_feedback == EXERCISE_FEEDBACK_TYPE_END) {
                        $header .= Display::tag('th', get_lang('Feedback'));
                    }
                    $s .= '<table class="data_table">';
                    $s .= Display::tag('tr', $header, array('style' => 'text-align:left;'));
                }
            }
            $matching_correct_answer = 0;
            $user_choice_array = array();
            if (!empty($user_choice)) {
                foreach ($user_choice as $item) {
                    $user_choice_array[] = $item['answer'];
                }
            }
            foreach ($objAnswerTmp->answer as $answerId => $answer_item) {
                $answer = $objAnswerTmp->selectAnswer($answerId);
                $answerCorrect = $objAnswerTmp->isCorrect($answerId);
                $comment = $objAnswerTmp->selectComment($answerId);
                //$numAnswer       = $objAnswerTmp->selectAutoId($answerId);
                $numAnswer = $answerId;
                $attributes = array();
                // Unique answer
                if (in_array($answerType, array(UNIQUE_ANSWER, UNIQUE_ANSWER_IMAGE, UNIQUE_ANSWER_NO_OPTION))) {
                    $input_id = 'choice-' . $questionId . '-' . $answerId;
                    if (isset($user_choice[0]['answer']) && $user_choice[0]['answer'] == $numAnswer) {
                        $attributes = array('id' => $input_id, 'checked' => 1, 'selected' => 1);
                    } else {
                        $attributes = array('id' => $input_id);
                    }
                    if ($debug_mark_answer) {
                        if ($answerCorrect) {
                            $attributes['checked'] = 1;
                            $attributes['selected'] = 1;
                        }
                    }
                    $answer = Security::remove_XSS($answer);
                    $s .= Display::input('hidden', 'choice2[' . $questionId . ']', '0');
                    $answer_input = null;
                    if ($answerType == UNIQUE_ANSWER_IMAGE) {
                        $attributes['style'] = 'display:none';
                        $answer_input .= '<div id="answer' . $questionId . $numAnswer . '" style="float:left" class="highlight_image_default highlight_image">';
                    }
                    $answer_input .= '<label class="radio">';
                    $answer_input .= Display::input('radio', 'choice[' . $questionId . ']', $numAnswer, $attributes);
                    $answer_input .= $answer;
                    $answer_input .= '</label>';
                    if ($answerType == UNIQUE_ANSWER_IMAGE) {
                        $answer_input .= "</div>";
                    }
                    if ($show_comment) {
                        $s .= '<tr><td>';
                        $s .= $answer_input;
                        $s .= '</td>';
                        $s .= '<td>';
                        $s .= $comment;
                        $s .= '</td>';
                        $s .= '</tr>';
                    } else {
                        $s .= $answer_input;
                    }
                } elseif (in_array($answerType, array(MULTIPLE_ANSWER, MULTIPLE_ANSWER_TRUE_FALSE, GLOBAL_MULTIPLE_ANSWER))) {
                    $input_id = 'choice-' . $questionId . '-' . $answerId;
                    $answer = Security::remove_XSS($answer);
                    if (in_array($numAnswer, $user_choice_array)) {
                        $attributes = array('id' => $input_id, 'checked' => 1, 'selected' => 1);
                    } else {
                        $attributes = array('id' => $input_id);
                    }
                    if ($debug_mark_answer) {
                        if ($answerCorrect) {
                            $attributes['checked'] = 1;
                            $attributes['selected'] = 1;
                        }
                    }
                    if ($answerType == MULTIPLE_ANSWER || $answerType == GLOBAL_MULTIPLE_ANSWER) {
                        $s .= '<input type="hidden" name="choice2[' . $questionId . ']" value="0" />';
                        $answer_input = '<label class="checkbox">';
                        $answer_input .= Display::input('checkbox', 'choice[' . $questionId . '][' . $numAnswer . ']', $numAnswer, $attributes);
                        $answer_input .= $answer;
                        $answer_input .= '</label>';
                        if ($show_comment) {
                            $s .= '<tr><td>';
                            $s .= $answer_input;
                            $s .= '</td>';
                            $s .= '<td>';
                            $s .= $comment;
                            $s .= '</td>';
                            $s .= '</tr>';
                        } else {
                            $s .= $answer_input;
                        }
                    } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
                        $my_choice = array();
                        if (!empty($user_choice_array)) {
                            foreach ($user_choice_array as $item) {
                                $item = explode(':', $item);
                                $my_choice[$item[0]] = $item[1];
                            }
                        }
                        $s .= '<tr>';
                        $s .= Display::tag('td', $answer);
                        if (!empty($quiz_question_options)) {
                            foreach ($quiz_question_options as $id => $item) {
                                $id = $item['iid'];
                                if (isset($my_choice[$numAnswer]) && $id == $my_choice[$numAnswer]) {
                                    $attributes = array('checked' => 1, 'selected' => 1);
                                } else {
                                    $attributes = array();
                                }
                                if ($debug_mark_answer) {
                                    if ($id == $answerCorrect) {
                                        $attributes['checked'] = 1;
                                        $attributes['selected'] = 1;
                                    }
                                }
                                $s .= Display::tag('td', Display::input('radio', 'choice[' . $questionId . '][' . $numAnswer . ']', $id, $attributes), array('style' => ''));
                            }
                        }
                        if ($show_comment) {
                            $s .= '<td>';
                            $s .= $comment;
                            $s .= '</td>';
                        }
                        $s .= '</tr>';
                    }
                } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION) {
                    // multiple answers
                    $input_id = 'choice-' . $questionId . '-' . $answerId;
                    if (in_array($numAnswer, $user_choice_array)) {
                        $attributes = array('id' => $input_id, 'checked' => 1, 'selected' => 1);
                    } else {
                        $attributes = array('id' => $input_id);
                    }
                    if ($debug_mark_answer) {
                        if ($answerCorrect) {
                            $attributes['checked'] = 1;
                            $attributes['selected'] = 1;
                        }
                    }
                    $answer = Security::remove_XSS($answer);
                    $answer_input = '<input type="hidden" name="choice2[' . $questionId . ']" value="0" />';
                    $answer_input .= '<label class="checkbox">';
                    $answer_input .= Display::input('checkbox', 'choice[' . $questionId . '][' . $numAnswer . ']', 1, $attributes);
                    $answer_input .= $answer;
                    $answer_input .= '</label>';
                    if ($show_comment) {
                        $s .= '<tr>';
                        $s .= '<td>';
                        $s .= $answer_input;
                        $s .= '</td>';
                        $s .= '<td>';
                        $s .= $comment;
                        $s .= '</td>';
                        $s .= '</tr>';
                    } else {
                        $s .= $answer_input;
                    }
                } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
                    $s .= '<input type="hidden" name="choice2[' . $questionId . ']" value="0" />';
                    $my_choice = array();
                    if (!empty($user_choice_array)) {
                        foreach ($user_choice_array as $item) {
                            $item = explode(':', $item);
                            $my_choice[$item[0]] = $item[1];
                        }
                    }
                    $answer = Security::remove_XSS($answer);
                    $s .= '<tr>';
                    $s .= Display::tag('td', $answer);
                    foreach ($objQuestionTmp->options as $key => $item) {
                        if (isset($my_choice[$numAnswer]) && $key == $my_choice[$numAnswer]) {
                            $attributes = array('checked' => 1, 'selected' => 1);
                        } else {
                            $attributes = array();
                        }
                        if ($debug_mark_answer) {
                            if ($key == $answerCorrect) {
                                $attributes['checked'] = 1;
                                $attributes['selected'] = 1;
                            }
                        }
                        $s .= Display::tag('td', Display::input('radio', 'choice[' . $questionId . '][' . $numAnswer . ']', $key, $attributes));
                    }
                    if ($show_comment) {
                        $s .= '<td>';
                        $s .= $comment;
                        $s .= '</td>';
                    }
                    $s .= '</tr>';
                } elseif ($answerType == FILL_IN_BLANKS) {
                    list($answer) = explode('::', $answer);
                    //Correct answer
                    api_preg_match_all('/\\[[^]]+\\]/', $answer, $correct_answer_list);
                    //Student's answezr
                    if (isset($user_choice[0]['answer'])) {
                        api_preg_match_all('/\\[[^]]+\\]/', $user_choice[0]['answer'], $student_answer_list);
                        $student_answer_list = $student_answer_list[0];
                    }
                    //If debug
                    if ($debug_mark_answer) {
                        $student_answer_list = $correct_answer_list[0];
                    }
                    if (!empty($correct_answer_list) && !empty($student_answer_list)) {
                        $correct_answer_list = $correct_answer_list[0];
                        $i = 0;
                        foreach ($correct_answer_list as $correct_item) {
                            $value = null;
                            if (isset($student_answer_list[$i]) && !empty($student_answer_list[$i])) {
                                //Cleaning student answer list
                                $value = strip_tags($student_answer_list[$i]);
                                $value = api_substr($value, 1, api_strlen($value) - 2);
                                $value = explode('/', $value);
                                if (!empty($value[0])) {
                                    $value = str_replace('&nbsp;', '', trim($value[0]));
                                }
                                $correct_item = preg_quote($correct_item);
                                $correct_item = api_preg_replace('|/|', '\\/', $correct_item);
                                // to prevent error if there is a / in the text to find
                                $answer = api_preg_replace('/' . $correct_item . '/', Display::input('text', "choice[{$questionId}][]", $value), $answer, 1);
                            }
                            $i++;
                        }
                    } else {
                        $answer = api_preg_replace('/\\[[^]]+\\]/', Display::input('text', "choice[{$questionId}][]", '', $attributes), $answer);
                    }
                    $s .= $answer;
                } elseif ($answerType == MATCHING) {
                    // matching type, showing suggestions and answers
                    // TODO: replace $answerId by $numAnswer
                    if ($lines_count == 1) {
                        $s .= $objAnswerTmp->getJs();
                    }
                    if ($answerCorrect != 0) {
                        // only show elements to be answered (not the contents of
                        // the select boxes, who are correct = 0)
                        $s .= '<tr><td width="45%">';
                        $parsed_answer = $answer;
                        $windowId = $questionId . '_' . $lines_count;
                        //left part questions
                        $s .= ' <div id="window_' . $windowId . '" class="window window_left_question window' . $questionId . '_question">
                                    <b>' . $lines_count . '</b>.&nbsp' . $parsed_answer . '
                                </div>
                                </td>';
                        // middle part (matches selects)
                        $s .= '<td width="10%" align="center">&nbsp;&nbsp;';
                        $s .= '<div style="display:block">';
                        $s .= '<select id="window_' . $windowId . '_select" name="choice[' . $questionId . '][' . $numAnswer . ']">';
                        $selectedValue = 0;
                        // fills the list-box
                        $item = 0;
                        foreach ($select_items as $val) {
                            // set $debug_mark_answer to true at public static function start to
                            // show the correct answer with a suffix '-x'
                            $selected = '';
                            if ($debug_mark_answer) {
                                if ($val['id'] == $answerCorrect) {
                                    $selected = 'selected="selected"';
                                    $selectedValue = $val['id'];
                                }
                            }
                            if (isset($user_choice[$matching_correct_answer]) && $val['id'] == $user_choice[$matching_correct_answer]['answer']) {
                                $selected = 'selected="selected"';
                                $selectedValue = $val['id'];
                            }
                            //$s .= '<option value="'.$val['id'].'" '.$selected.'>'.$val['letter'].'</option>';
                            $s .= '<option value="' . $item . '" ' . $selected . '>' . $val['letter'] . '</option>';
                            $item++;
                        }
                        if (!empty($answerCorrect) && !empty($selectedValue)) {
                            $s .= '<script>
                                jsPlumb.ready(function() {
                                    jsPlumb.connect({
                                        source: "window_' . $windowId . '",
                                        target: "window_' . $questionId . '_' . $selectedValue . '_answer",
                                        endpoint:["Blank", { radius:15 }],
                                        anchor:["RightMiddle","LeftMiddle"],
                                        paintStyle:{ strokeStyle:"#8a8888" , lineWidth:8 },
                                        connector: [connectorType, { curviness: curvinessValue } ],
                                    })
                                });
                                </script>';
                        }
                        $s .= '</select></div></td>';
                        $s .= '<td width="45%" valign="top" >';
                        if (isset($select_items[$lines_count])) {
                            $s .= '<div id="window_' . $windowId . '_answer" class="window window_right_question">
                                    <b>' . $select_items[$lines_count]['letter'] . '.</b> ' . $select_items[$lines_count]['answer'] . '
                                  </div>';
                        } else {
                            $s .= '&nbsp;';
                        }
                        $s .= '</td>';
                        $s .= '</tr>';
                        $lines_count++;
                        //if the left side of the "matching" has been completely
                        // shown but the right side still has values to show...
                        if ($lines_count - 1 == $num_suggestions) {
                            // if it remains answers to shown at the right side
                            while (isset($select_items[$lines_count])) {
                                $s .= '<tr>
                                      <td colspan="2"></td>
                                      <td valign="top">';
                                $s .= '<b>' . $select_items[$lines_count]['letter'] . '.</b>';
                                $s .= $select_items[$lines_count]['answer'];
                                $s .= "</td>\n                                </tr>";
                                $lines_count++;
                            }
                            // end while()
                        }
                        // end if()
                        $matching_correct_answer++;
                    }
                } elseif ($answerType == DRAGGABLE) {
                    // matching type, showing suggestions and answers
                    // TODO: replace $answerId by $numAnswer
                    if ($answerCorrect != 0) {
                        // only show elements to be answered (not the contents of
                        // the select boxes, who are correct = 0)
                        $s .= '<td>';
                        $parsed_answer = $answer;
                        $windowId = $questionId . '_' . $numAnswer;
                        //67_293 - 67_294
                        //left part questions
                        $s .= '<li class="ui-state-default" id="' . $windowId . '">';
                        $s .= ' <div id="window_' . $windowId . '" class="window' . $questionId . '_question_draggable question_draggable">
                                   ' . $parsed_answer . '
                                </div>';
                        $s .= '<div style="display:none">';
                        $s .= '<select id="window_' . $windowId . '_select" name="choice[' . $questionId . '][' . $numAnswer . ']" class="select_option">';
                        $selectedValue = 0;
                        // fills the list-box
                        $item = 0;
                        foreach ($select_items as $val) {
                            // set $debug_mark_answer to true at function start to
                            // show the correct answer with a suffix '-x'
                            $selected = '';
                            if ($debug_mark_answer) {
                                if ($val['id'] == $answerCorrect) {
                                    $selected = 'selected="selected"';
                                    $selectedValue = $val['id'];
                                }
                            }
                            if (isset($user_choice[$matching_correct_answer]) && $val['id'] == $user_choice[$matching_correct_answer]['answer']) {
                                $selected = 'selected="selected"';
                                $selectedValue = $val['id'];
                            }
                            //$s .= '<option value="'.$val['id'].'" '.$selected.'>'.$val['letter'].'</option>';
                            $s .= '<option value="' . $item . '" ' . $selected . '>' . $val['letter'] . '</option>';
                            $item++;
                        }
                        $s .= '</select>';
                        if (!empty($answerCorrect) && !empty($selectedValue)) {
                            $s .= '<script>
                                $(function() {
                                    deleteItem($("#' . $questionId . '_' . $selectedValue . '"), $("#drop_' . $windowId . '"));
                                });
                                </script>';
                        }
                        if (isset($select_items[$lines_count])) {
                            $s .= '<div id="window_' . $windowId . '_answer" class="">
                                    <b>' . $select_items[$lines_count]['letter'] . '.</b> ' . $select_items[$lines_count]['answer'] . '
                                  </div>';
                        } else {
                            $s .= '&nbsp;';
                        }
                        $lines_count++;
                        //if the left side of the "matching" has been completely
                        // shown but the right side still has values to show...
                        if ($lines_count - 1 == $num_suggestions) {
                            // if it remains answers to shown at the right side
                            while (isset($select_items[$lines_count])) {
                                $s .= '<b>' . $select_items[$lines_count]['letter'] . '.</b>';
                                $s .= $select_items[$lines_count]['answer'];
                                $lines_count++;
                            }
                        }
                        $s .= '</div>';
                        $matching_correct_answer++;
                        $s .= '</li>';
                    }
                }
            }
            // end for()
            if ($show_comment) {
                $s .= '</table>';
            } else {
                if ($answerType == MATCHING || $answerType == UNIQUE_ANSWER_NO_OPTION || $answerType == MULTIPLE_ANSWER_TRUE_FALSE || $answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
                    $s .= '</table>';
                }
            }
            if ($answerType == DRAGGABLE) {
                $s .= '</ul><div class="clear"></div>';
                $counterAnswer = 1;
                foreach ($objAnswerTmp->answer as $answerId => $answer_item) {
                    $answerCorrect = $objAnswerTmp->isCorrect($answerId);
                    $windowId = $questionId . '_' . $counterAnswer;
                    if ($answerCorrect == 0) {
                        $s .= '<div id="drop_' . $windowId . '" class="droppable ui-state-default">' . $counterAnswer . '</div>';
                        $counterAnswer++;
                    }
                }
            }
            if ($answerType == MATCHING) {
                $s .= '</div>';
            }
            $s .= '</div>';
            // destruction of the Answer object
            unset($objAnswerTmp);
            // destruction of the Question object
            unset($objQuestionTmp);
            $html .= $s;
            return $html;
        } elseif ($answerType == HOT_SPOT || $answerType == HOT_SPOT_DELINEATION) {
            // Question is a HOT_SPOT
            //checking document/images visibility
            if (api_is_platform_admin() || api_is_course_admin()) {
                $course = api_get_course_info();
                $doc_id = DocumentManager::get_document_id($course, '/images/' . $pictureName);
                if (is_numeric($doc_id)) {
                    $images_folder_visibility = api_get_item_visibility($course, 'document', $doc_id, api_get_session_id());
                    if (!$images_folder_visibility) {
                        //This message is shown only to the course/platform admin if the image is set to visibility = false
                        Display::display_warning_message(get_lang('ChangeTheVisibilityOfTheCurrentImage'));
                    }
                }
            }
            $questionName = $objQuestionTmp->selectTitle();
            $questionDescription = $objQuestionTmp->selectDescription();
            if ($freeze) {
                $s .= Display::img($objQuestionTmp->selectPicturePath());
                $html .= $s;
                return $html;
            }
            // Get the answers, make a list
            $objAnswerTmp = new Answer($questionId);
            // get answers of hotpost
            $answers_hotspot = array();
            foreach ($objAnswerTmp->answer as $answerId => $answer_item) {
                //$answers = $objAnswerTmp->selectAnswerByAutoId($objAnswerTmp->selectAutoId($answerId));
                $answers_hotspot[$answerId] = $objAnswerTmp->selectAnswer($answerId);
            }
            // display answers of hotpost order by id
            $answer_list = '<div style="padding: 10px; margin-left: 0px; border: 1px solid #A4A4A4; height: 408px; width: 200px;"><b>' . get_lang('HotspotZones') . '</b><dl>';
            if (!empty($answers_hotspot)) {
                ksort($answers_hotspot);
                foreach ($answers_hotspot as $key => $value) {
                    $answer_list .= '<dt>' . $key . '.- ' . $value . '</dt><br />';
                }
            }
            $answer_list .= '</dl></div>';
            if ($answerType == HOT_SPOT_DELINEATION) {
                $answer_list = '';
                $swf_file = 'hotspot_delineation_user';
                $swf_height = 405;
            } else {
                $swf_file = 'hotspot_user';
                $swf_height = 436;
            }
            if (!$only_questions) {
                if ($show_title) {
                    $html .= TestCategory::getCategoryNamesForQuestion($objQuestionTmp->id);
                    $html .= '<div class="question_title">' . $current_item . '. ' . $questionName . '</div>';
                    $html .= $questionDescription;
                } else {
                    $html .= '<div class="media">';
                    $html .= '<div class="pull-left">';
                    $html .= '<div class="media-object">';
                    $html .= Display::div($current_item . '. ', array('class' => 'question_no_title'));
                    $html .= '</div>';
                    $html .= '</div>';
                    $html .= '<div class="media-body">';
                    if (!empty($questionDescription)) {
                        $html .= Display::div($questionDescription, array('class' => 'question_description'));
                    }
                    $html .= '</div>';
                    $html .= '</div>';
                }
                //@todo I need to the get the feedback type
                $html .= '<input type="hidden" name="hidden_hotspot_id" value="' . $questionId . '" />';
                $html .= '<table class="exercise_questions">
                           <tr>
                            <td valign="top" colspan="2">';
                $html .= '</td></tr>';
            }
            $canClick = isset($_GET['editQuestion']) ? '0' : (isset($_GET['modifyAnswers']) ? '0' : '1');
            $s .= ' <script type="text/javascript" src="../plugin/hotspot/JavaScriptFlashGateway.js"></script>
                    <script src="../plugin/hotspot/hotspot.js" type="text/javascript" ></script>
                    <script type="text/javascript">
                    <!--
                    // Globals
                    // Major version of Flash required
                    var requiredMajorVersion = 7;
                    // Minor version of Flash required
                    var requiredMinorVersion = 0;
                    // Minor version of Flash required
                    var requiredRevision = 0;
                    // the version of javascript supported
                    var jsVersion = 1.0;
                    // -->
                    </script>
                    <script language="VBScript" type="text/vbscript">
                    <!-- // Visual basic helper required to detect Flash Player ActiveX control version information
                    Function VBGetSwfVer(i)
                      on error resume next
                      Dim swControl, swVersion
                      swVersion = 0

                      set swControl = CreateObject("ShockwaveFlash.ShockwaveFlash." + CStr(i))
                      if (IsObject(swControl)) then
                        swVersion = swControl.GetVariable("$version")
                      end if
                      VBGetSwfVer = swVersion
                    End Function
                    // -->
                    </script>

                    <script language="JavaScript1.1" type="text/javascript">
                    <!-- // Detect Client Browser type
                    var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
                    var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
                    var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
                    jsVersion = 1.1;
                    // JavaScript helper required to detect Flash Player PlugIn version information
                    function JSGetSwfVer(i) {
                        // NS/Opera version >= 3 check for Flash plugin in plugin array
                        if (navigator.plugins != null && navigator.plugins.length > 0) {
                            if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
                                var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
                                var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
                                descArray = flashDescription.split(" ");
                                tempArrayMajor = descArray[2].split(".");
                                versionMajor = tempArrayMajor[0];
                                versionMinor = tempArrayMajor[1];
                                if ( descArray[3] != "" ) {
                                    tempArrayMinor = descArray[3].split("r");
                                } else {
                                    tempArrayMinor = descArray[4].split("r");
                                }
                                versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
                                flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
                            } else {
                                flashVer = -1;
                            }
                        }
                        // MSN/WebTV 2.6 supports Flash 4
                        else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
                        // WebTV 2.5 supports Flash 3
                        else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
                        // older WebTV supports Flash 2
                        else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
                        // Can\'t detect in all other cases
                        else {
                            flashVer = -1;
                        }
                        return flashVer;
                    }
                    // When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available

                    function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) {
                        reqVer = parseFloat(reqMajorVer + "." + reqRevision);
                        // loop backwards through the versions until we find the newest version
                        for (i=25;i>0;i--) {
                            if (isIE && isWin && !isOpera) {
                                versionStr = VBGetSwfVer(i);
                            } else {
                                versionStr = JSGetSwfVer(i);
                            }
                            if (versionStr == -1 ) {
                                return false;
                            } else if (versionStr != 0) {
                                if(isIE && isWin && !isOpera) {
                                    tempArray         = versionStr.split(" ");
                                    tempString        = tempArray[1];
                                    versionArray      = tempString .split(",");
                                } else {
                                    versionArray      = versionStr.split(".");
                                }
                                versionMajor      = versionArray[0];
                                versionMinor      = versionArray[1];
                                versionRevision   = versionArray[2];

                                versionString     = versionMajor + "." + versionRevision;   // 7.0r24 == 7.24
                                versionNum        = parseFloat(versionString);
                                // is the major.revision >= requested major.revision AND the minor version >= requested minor
                                if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
                                    return true;
                                } else {
                                    return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
                                }
                            }
                        }
                    }
                    // -->
                    </script>';
            $s .= '<tr><td valign="top" colspan="2" width="520"><table><tr><td width="520">
                    <script>
                        // Version check based upon the values entered above in "Globals"
                        var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

                        // Check to see if the version meets the requirements for playback
                        if (hasReqestedVersion) {  // if we\'ve detected an acceptable version
                            var oeTags = \'<object type="application/x-shockwave-flash" data="../plugin/hotspot/' . $swf_file . '.swf?modifyAnswers=' . $questionId . '&amp;canClick:' . $canClick . '" width="600" height="' . $swf_height . '">\'
                                        + \'<param name="wmode" value="transparent">\'
                                        + \'<param name="movie" value="../plugin/hotspot/' . $swf_file . '.swf?modifyAnswers=' . $questionId . '&amp;canClick:' . $canClick . '" />\'
                                        + \'<\\/object>\';
                            document.write(oeTags);   // embed the Flash Content SWF when all tests are passed
                        } else {  // flash is too old or we can\'t detect the plugin
                            var alternateContent = "Error<br \\/>"
                                + "Hotspots requires Macromedia Flash 7.<br \\/>"
                                + "<a href=\\"http://www.macromedia.com/go/getflash/\\">Get Flash<\\/a>";
                            document.write(alternateContent);  // insert non-flash content
                        }
                    </script>
                    </td>
                    <td valign="top" align="left">' . $answer_list . '</td></tr>
                    </table>
            </td></tr>';
            $html .= $s;
            $html .= '</table>';
            return $html;
        }
        return $nbrAnswers;
    }
/**
 * Creates the folder (if needed) and uploads the stylesheet in it
 *
 * @param array $values the values of the form
 * @param array $picture the values of the uploaded file
 *
 * @author Patrick Cool <*****@*****.**>, Ghent University, Belgium
 * @version May 2008
 * @since Dokeos 1.8.5
 */
function upload_stylesheet($values, $picture)
{
    $result = false;
    // Valid name for the stylesheet folder.
    $style_name = api_preg_replace('/[^A-Za-z0-9]/', '', $values['name_stylesheet']);
    $cssToUpload = CSS_UPLOAD_PATH;
    // Create the folder if needed.
    if (!is_dir($cssToUpload . $style_name . '/')) {
        mkdir($cssToUpload . $style_name . '/', api_get_permissions_for_new_directories());
    }
    $info = pathinfo($picture['name']);
    if ($info['extension'] == 'zip') {
        // Try to open the file and extract it in the theme.
        $zip = new ZipArchive();
        if ($zip->open($picture['tmp_name'])) {
            // Make sure all files inside the zip are images or css.
            $num_files = $zip->numFiles;
            $valid = true;
            $single_directory = true;
            $invalid_files = array();
            $allowedFiles = array('jpg', 'jpeg', 'png', 'gif', 'css', 'ico', 'psd', 'woff', 'woff2');
            for ($i = 0; $i < $num_files; $i++) {
                $file = $zip->statIndex($i);
                if (substr($file['name'], -1) != '/') {
                    $path_parts = pathinfo($file['name']);
                    if (!in_array($path_parts['extension'], $allowedFiles)) {
                        $valid = false;
                        $invalid_files[] = $file['name'];
                    }
                }
                if (strpos($file['name'], '/') === false) {
                    $single_directory = false;
                }
            }
            if (!$valid) {
                $error_string = '<ul>';
                foreach ($invalid_files as $invalid_file) {
                    $error_string .= '<li>' . $invalid_file . '</li>';
                }
                $error_string .= '</ul>';
                Display::display_error_message(get_lang('ErrorStylesheetFilesExtensionsInsideZip') . $error_string, false);
            } else {
                // If the zip does not contain a single directory, extract it.
                if (!$single_directory) {
                    // Extract zip file.
                    $zip->extractTo($cssToUpload . $style_name . '/');
                    $result = true;
                } else {
                    $extraction_path = $cssToUpload . $style_name . '/';
                    for ($i = 0; $i < $num_files; $i++) {
                        $entry = $zip->getNameIndex($i);
                        if (substr($entry, -1) == '/') {
                            continue;
                        }
                        $pos_slash = strpos($entry, '/');
                        $entry_without_first_dir = substr($entry, $pos_slash + 1);
                        // If there is still a slash, we need to make sure the directories are created.
                        if (strpos($entry_without_first_dir, '/') !== false) {
                            if (!is_dir($extraction_path . dirname($entry_without_first_dir))) {
                                // Create it.
                                @mkdir($extraction_path . dirname($entry_without_first_dir), $mode = 0777, true);
                            }
                        }
                        $fp = $zip->getStream($entry);
                        $ofp = fopen($extraction_path . dirname($entry_without_first_dir) . '/' . basename($entry), 'w');
                        while (!feof($fp)) {
                            fwrite($ofp, fread($fp, 8192));
                        }
                        fclose($fp);
                        fclose($ofp);
                    }
                    $result = true;
                }
            }
            $zip->close();
        } else {
            Display::display_error_message(get_lang('ErrorReadingZip') . $info['extension'], false);
        }
    } else {
        // Simply move the file.
        move_uploaded_file($picture['tmp_name'], $cssToUpload . $style_name . '/' . $picture['name']);
        $result = true;
    }
    if ($result) {
        $fs = new Filesystem();
        $fs->mirror($cssToUpload, api_get_path(SYS_PATH) . 'web/css/themes/');
    }
    return $result;
}
 /**
  * Escape MySQL wildchars _ and % in LIKE search
  * @param string            The string to escape
  * @return string           The escaped string
  */
 public static function escape_sql_wildcards($in_txt)
 {
     $out_txt = api_preg_replace("/_/", "\\_", $in_txt);
     $out_txt = api_preg_replace("/%/", "\\%", $out_txt);
     return $out_txt;
 }