/** * The UI for doing a quiz. * * @return tempcode The result of execution. */ function do_quiz() { $id = get_param_integer('id'); $quizzes = $GLOBALS['SITE_DB']->query_select('quizzes', array('*'), array('id' => $id), '', 1); if (!array_key_exists(0, $quizzes)) { warn_exit(do_lang_tempcode('MISSING_RESOURCE')); } $quiz = $quizzes[0]; $this->enforcement_checks($quiz); if (addon_installed('awards')) { require_code('awards'); $awards = find_awards_for('quiz', strval($id)); } else { $awards = array(); } $quiz_name = get_translated_text($quiz['q_name']); $title_to_use = do_lang_tempcode('THIS_WITH', do_lang_tempcode($quiz['q_type']), make_string_tempcode(escape_html($quiz_name))); $title_to_use_2 = do_lang('THIS_WITH_SIMPLE', do_lang($quiz['q_type']), $quiz_name); $title = get_page_title($title_to_use, false, NULL, NULL, $awards); seo_meta_load_for('quiz', strval($id), $title_to_use_2); $last_visit_time = $GLOBALS['SITE_DB']->query_value_null_ok('quiz_member_last_visit', 'v_time', array('v_quiz_id' => $id, 'v_member_id' => get_member()), 'ORDER BY v_time DESC'); if (!is_null($last_visit_time)) { $timer_offset = time() - $last_visit_time; if (is_null($quiz['q_timeout']) || $timer_offset >= $quiz['q_timeout'] * 60) { $GLOBALS['SITE_DB']->query_delete('quiz_member_last_visit', array('v_member_id' => get_member(), 'v_quiz_id' => $id)); $GLOBALS['SITE_DB']->query_insert('quiz_member_last_visit', array('v_quiz_id' => $id, 'v_time' => time(), 'v_member_id' => get_member())); $timer_offset = 0; } } else { $GLOBALS['SITE_DB']->query_insert('quiz_member_last_visit', array('v_quiz_id' => $id, 'v_time' => time(), 'v_member_id' => get_member())); $timer_offset = 0; } $questions = $GLOBALS['SITE_DB']->query_select('quiz_questions', array('*'), array('q_quiz' => $id), 'ORDER BY q_order'); // If a test/quiz, randomly order questions // if ($quiz['q_type']!='SURVEY') shuffle($questions); No, could cause problems foreach ($questions as $i => $question) { $answers = $GLOBALS['SITE_DB']->query_select('quiz_question_answers', array('*'), array('q_question' => $question['id']), 'ORDER BY q_order'); // If a test/quiz, randomly order answers if ($quiz['q_type'] != 'SURVEY') { shuffle($answers); } $questions[$i]['answers'] = $answers; } require_code('quiz'); $fields = render_quiz($questions); breadcrumb_set_self(make_string_tempcode(escape_html(get_translated_text($quiz['q_name'])))); // Validation if ($quiz['q_validated'] == 0) { if (!has_specific_permission(get_member(), 'jump_to_unvalidated')) { access_denied('SPECIFIC_PERMISSION', 'jump_to_unvalidated'); } $warning_details = do_template('WARNING_TABLE', array('WARNING' => do_lang_tempcode(get_param_integer('redirected', 0) == 1 ? 'UNVALIDATED_TEXT_NON_DIRECT' : 'UNVALIDATED_TEXT'))); } else { $warning_details = new ocp_tempcode(); } $type = 'Quiz'; switch ($quiz['q_type']) { case 'COMPETITION': $type = 'Competition'; break; case 'SURVEY': $type = 'Survey'; break; case 'TEST': $type = 'Test'; break; } $GLOBALS['META_DATA'] += array('created' => date('Y-m-d', $quiz['q_add_date']), 'creator' => $GLOBALS['FORUM_DRIVER']->get_username($quiz['q_submitter']), 'publisher' => '', 'modified' => '', 'type' => $type, 'title' => get_translated_text($quiz['q_name']), 'identifier' => '_SEARCH:quiz:do:' . strval($id), 'description' => get_translated_text($quiz['q_start_text']), 'image' => find_theme_image('bigicons/quiz')); $edit_url = new ocp_tempcode(); if (has_actual_page_access(NULL, 'cms_quiz', NULL, NULL) && has_edit_permission('mid', get_member(), $quiz['q_submitter'], 'cms_quiz', array('quiz', $id))) { $edit_url = build_url(array('page' => 'cms_quiz', 'type' => '_ed', 'id' => $id), get_module_zone('cms_quiz')); } // Display UI: start text, questions. Including timeout $start_text = get_translated_tempcode($quiz['q_start_text']); $post_url = build_url(array('page' => '_SELF', 'type' => '_do', 'id' => $id), '_SELF'); return do_template('QUIZ_SCREEN', array('_GUID' => 'f390877672938ba62f79f9528bef742f', 'EDIT_URL' => $edit_url, 'TAGS' => get_loaded_tags('quiz'), 'ID' => strval($id), 'WARNING_DETAILS' => $warning_details, 'URL' => $post_url, 'TITLE' => $title, 'START_TEXT' => $start_text, 'FIELDS' => $fields, 'TIMEOUT' => is_null($quiz['q_timeout']) ? '' : strval($quiz['q_timeout'] * 60 - $timer_offset))); }
/** * Standard modular run function for preview hooks. * * @return array A pair: The preview, the updated post Comcode */ function run() { require_code('quiz'); $questions = array(); $text = post_param('text'); $type = post_param('type'); $_qs = explode(chr(10) . chr(10), $text); $qs = array(); foreach ($_qs as $q) { $q = trim($q); if ($q != '') { $qs[] = $q; } } $num_q = 0; $qs2 = array(); foreach ($qs as $i => $q) { $_as = explode(chr(10), $q); $as = array(); foreach ($_as as $a) { if ($a != '') { $as[] = $a; } } $q = array_shift($as); $matches = array(); //if (preg_match('#^(\d+)\)?(.*)#',$q,$matches)===false) continue; if (preg_match('#^(.*)#', $q, $matches) === false) { continue; } if (count($matches) == 0) { continue; } $implicit_question_number = $i; //$matches[1]; $qs2[$implicit_question_number] = $q . chr(10) . implode(chr(10), $as); } ksort($qs2); foreach (array_values($qs2) as $i => $q) { $_as = explode(chr(10), $q); $as = array(); foreach ($_as as $a) { if ($a != '') { $as[] = $a; } } $q = array_shift($as); $matches = array(); //if (preg_match('#^(\d+)\)?(.*)#',$q,$matches)===false) continue; if (preg_match('#^(.*)#', $q, $matches) === false) { continue; } if (count($matches) == 0) { continue; } $question = trim($matches[count($matches) - 1]); $long_input_field = strpos($question, ' [LONG]') !== false ? 1 : 0; $question = str_replace(' [LONG]', '', $question); $num_choosable_answers = strpos($question, ' [*]') !== false ? count($as) : (count($as) > 0 ? 1 : 0); $question = str_replace(' [*]', '', $question); $required = strpos($question, ' [REQUIRED]') !== false ? 1 : 0; $question = str_replace(' [REQUIRED]', '', $question); // Now we add the answers $answers = array(); foreach ($as as $x => $a) { $is_correct = $x == 0 && strpos($qs2[$i], ' [*]') === false && $type != 'SURVEY' || strpos($a, ' [*]') !== false ? 1 : 0; $a = str_replace(' [*]', '', $a); if (substr($a, 0, 1) == ':') { continue; } $answers[] = array('id' => $x, 'q_answer_text' => $a, 'q_is_correct' => $is_correct); } $questions[] = array('id' => $i, 'q_long_input_field' => $long_input_field, 'q_num_choosable_answers' => $num_choosable_answers, 'q_question_text' => $question, 'answers' => $answers, 'q_required' => $required); $num_q++; } $preview = render_quiz($questions); return array(do_template('FORM', array('SUBMIT_NAME' => '', 'TEXT' => '', 'URL' => '', 'HIDDEN' => '', 'FIELDS' => $preview)), NULL); }