本文整理汇总了PHP中Answer::isCorrect方法的典型用法代码示例。如果您正苦于以下问题:PHP Answer::isCorrect方法的具体用法?PHP Answer::isCorrect怎么用?PHP Answer::isCorrect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Answer
的用法示例。
在下文中一共展示了Answer::isCorrect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: successRate
/**
*
* Calculate Question success rate
*/
function successRate($exerciseId = NULL) {
$id = $this->id;
$type = $this->type;
$objAnswerTmp = new Answer($id);
$nbrAnswers = $objAnswerTmp->selectNbrAnswers();
$q_correct_answers_sql = '';
$q_incorrect_answers_sql = '';
$extra_sql = '';
$query_vars = array($id, ATTEMPT_COMPLETED);
if(isset($exerciseId)) {
$extra_sql = " AND b.eid = ?d";
$query_vars[] = $exerciseId;
}
$total_answer_attempts = Database::get()->querySingle("SELECT COUNT(DISTINCT a.eurid) AS count
FROM exercise_answer_record a, exercise_user_record b
WHERE a.eurid = b.eurid AND a.question_id = ?d AND b.attempt_status=?d$extra_sql", $query_vars)->count;
//BUILDING CORRECT ANSWER QUERY BASED ON QUESTION TYPE
if($type == UNIQUE_ANSWER || $type == MULTIPLE_ANSWER || $type == TRUE_FALSE){ //works wrong for MULTIPLE_ANSWER
$i=1;
for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
if ($objAnswerTmp->isCorrect($answerId)) {
$q_correct_answers_sql .= ($i!=1) ? ' OR ' : '';
$q_correct_answers_sql .= 'a.answer_id = '.$objAnswerTmp->selectPosition($answerId);
$q_incorrect_answers_sql .= ($i!=1) ? ' AND ' : '';
$q_incorrect_answers_sql .= 'a.answer_id != '.$objAnswerTmp->selectPosition($answerId);
$i++;
}
}
$q_correct_answers_cnt = $i-1;
} elseif ($type == MATCHING) { // to be done
$i = 1;
for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
//must get answer id ONLY where correct value existS
$answerCorrect = $objAnswerTmp->isCorrect($answerId);
if ($answerCorrect) {
$q_correct_answers_sql .= ($i!=1) ? " OR " : "";
$q_correct_answers_sql .= "(a.answer = $answerId AND a.answer_id = $answerCorrect)";
$q_incorrect_answers_sql .= ($i!=1) ? " OR " : "";
$q_incorrect_answers_sql .= "(a.answer = $answerId AND a.answer_id != $answerCorrect)";
$i++;
}
}
$q_correct_answers_cnt = $i-1;
} elseif ($type == FILL_IN_BLANKS || $type == FILL_IN_BLANKS_TOLERANT) { // Works Great
$answer_field = $objAnswerTmp->selectAnswer($nbrAnswers);
//splits answer string from weighting string
list($answer, $answerWeighting) = explode('::', $answer_field);
//getting all matched strings between [ and ] delimeters
preg_match_all('#(?<=\[)(?!/?m)[^\]]+#', $answer, $match);
$i=1;
$sql_binary_comparison = $type == FILL_IN_BLANKS ? 'BINARY ' : '';
foreach ($match[0] as $answers){
$correct_answers = preg_split('/\s*,\s*/', $answers);
$j=1;
$q_correct_answers_sql .= ($i!=1) ? ' OR ' : '';
$q_incorrect_answers_sql .= ($i!=1) ? ' OR ' : '';
foreach ($correct_answers as $value){
$q_correct_answers_sql .= ($j!=1) ? ' OR ' : '';
$q_correct_answers_sql .= "(a.answer = $sql_binary_comparison'$value' AND a.answer_id = $i)";
$q_incorrect_answers_sql .= ($j!=1) ? ' AND ' : '';
$q_incorrect_answers_sql .= "(a.answer != $sql_binary_comparison'$value' AND a.answer_id = $i)";
$j++;
}
$i++;
}
$q_correct_answers_cnt = $i-1;
}
//FIND CORRECT ANSWER ATTEMPTS
if ($type == FREE_TEXT) {
// This query gets answers which where graded with queston maximum grade
$correct_answer_attempts = Database::get()->querySingle("SELECT COUNT(DISTINCT a.eurid) AS count
FROM exercise_answer_record a, exercise_user_record b, exercise_question c
WHERE a.eurid = b.eurid AND a.question_id = c.id AND a.weight=c.weight AND a.question_id = ?d AND b.attempt_status=?d$extra_sql", $query_vars)->count;
} else {
// One Query to Rule Them All (except free text questions)
// This query groups attempts and counts correct and incorrect answers
// then counts attempts where (correct answers == total anticipated correct attempts)
// and (incorrect answers == 0) (this control is necessary mostly in cases of MULTIPLE ANSWER type)
if ($q_correct_answers_cnt > 0) {
$correct_answer_attempts = Database::get()->querySingle("
SELECT COUNT(*) AS counter FROM(
SELECT a.eurid,
SUM($q_correct_answers_sql) as correct_answer_cnt,
SUM($q_incorrect_answers_sql) as incorrect_answer_cnt
FROM exercise_answer_record a, exercise_user_record b
WHERE a.eurid = b.eurid AND a.question_id = ?d AND b.attempt_status = ?d$extra_sql
GROUP BY(a.eurid) HAVING correct_answer_cnt = $q_correct_answers_cnt AND incorrect_answer_cnt = 0
)sub", $query_vars)->counter;
} else {
$correct_answer_attempts = 0;
}
}
if ($total_answer_attempts>0) {
$successRate = round($correct_answer_attempts/$total_answer_attempts*100, 2);
} else {
//.........这里部分代码省略.........
示例2: manage_answer
//.........这里部分代码省略.........
$exe_info = isset($exe_info[$exeId]) ? $exe_info[$exeId] : null;
$params = array();
$params['course_id'] = $course_id;
$params['session_id'] = api_get_session_id();
$params['user_id'] = isset($exe_info['exe_user_id']) ? $exe_info['exe_user_id'] : api_get_user_id();
$params['exercise_id'] = isset($exe_info['exe_exo_id']) ? $exe_info['exe_exo_id'] : $this->id;
$params['question_id'] = $questionId;
$params['exe_id'] = isset($exe_info['exe_id']) ? $exe_info['exe_id'] : $exeId;
$nano = new Nanogong($params);
//probably this attempt came in an exercise all question by page
if ($feedback_type == 0) {
$nano->replace_with_real_exe($exeId);
}
}
$user_answer = '';
// Get answer list for matching
$sql = "SELECT id_auto, id, answer\n FROM {$table_ans}\n WHERE c_id = {$course_id} AND question_id = {$questionId}";
$res_answer = Database::query($sql);
$answerMatching = array();
while ($real_answer = Database::fetch_array($res_answer)) {
$answerMatching[$real_answer['id_auto']] = $real_answer['answer'];
}
$real_answers = array();
$quiz_question_options = Question::readQuestionOption($questionId, $course_id);
$organs_at_risk_hit = 0;
$questionScore = 0;
if ($debug) {
error_log('Start answer loop ');
}
$answer_correct_array = array();
for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
$answer = $objAnswerTmp->selectAnswer($answerId);
$answerComment = $objAnswerTmp->selectComment($answerId);
$answerCorrect = $objAnswerTmp->isCorrect($answerId);
$answerWeighting = (double) $objAnswerTmp->selectWeighting($answerId);
$answerAutoId = $objAnswerTmp->selectAutoId($answerId);
$answer_correct_array[$answerId] = (bool) $answerCorrect;
if ($debug) {
error_log("answer auto id: {$answerAutoId} ");
error_log("answer correct: {$answerCorrect} ");
}
// Delineation
$delineation_cord = $objAnswerTmp->selectHotspotCoordinates(1);
$answer_delineation_destination = $objAnswerTmp->selectDestination(1);
switch ($answerType) {
// for unique answer
case UNIQUE_ANSWER:
case UNIQUE_ANSWER_IMAGE:
case UNIQUE_ANSWER_NO_OPTION:
if ($from_database) {
$sql = "SELECT answer FROM {$TBL_TRACK_ATTEMPT}\n WHERE\n exe_id = '" . $exeId . "' AND\n question_id= '" . $questionId . "'";
$result = Database::query($sql);
$choice = Database::result($result, 0, "answer");
$studentChoice = $choice == $answerAutoId ? 1 : 0;
if ($studentChoice) {
$questionScore += $answerWeighting;
$totalScore += $answerWeighting;
}
} else {
$studentChoice = $choice == $answerAutoId ? 1 : 0;
if ($studentChoice) {
$questionScore += $answerWeighting;
$totalScore += $answerWeighting;
}
}
break;
示例3: Answer
</tr>";
} else {
echo "<tr class='even'>
<td><b>$langElementList</b></td>
<td><b>$langCorrespondsTo</b></td>
</tr>";
}
}
// construction of the Answer object
$objAnswerTmp = new Answer($questionId);
$nbrAnswers = $objAnswerTmp->selectNbrAnswers();
for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
$answer = $objAnswerTmp->selectAnswer($answerId);
$answerComment = $objAnswerTmp->selectComment($answerId);
$answerCorrect = $objAnswerTmp->isCorrect($answerId);
$answerWeighting = $objAnswerTmp->selectWeighting($answerId);
// support for math symbols
$answer = mathfilter($answer, 12, "$webDir/courses/mathimg/");
$answerComment = mathfilter($answerComment, 12, "$webDir/courses/mathimg/");
switch ($answerType) {
// for unique answer
case UNIQUE_ANSWER : $studentChoice = ($choice == $answerId) ? 1 : 0;
if ($studentChoice) {
$questionScore+=$answerWeighting;
$totalScore+=$answerWeighting;
}
break;
// for multiple answers
case MULTIPLE_ANSWER : $studentChoice = @$choice[$answerId];
示例4: showQuestion
/**
* 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)) {
//.........这里部分代码省略.........
示例5: showQuestion
/**
* 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'];
//.........这里部分代码省略.........
示例6: showQuestion
/**
* 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;
//.........这里部分代码省略.........
示例7: array
$row++;
}
$content = $table->toHtml();
// Format B
$headers = array(get_lang('Question'), get_lang('Answer'), get_lang('Correct'), get_lang('NumberStudentWhoSelectedIt'));
$data = array();
if (!empty($question_list)) {
$id = 0;
foreach ($question_list as $question_id) {
$question_obj = Question::read($question_id);
$exercise_stats = get_student_stats_by_question($question_id, $exercise_id, $courseCode, $sessionId);
$answer = new Answer($question_id);
$answer_count = $answer->selectNbrAnswers();
for ($answer_id = 1; $answer_id <= $answer_count; $answer_id++) {
$answer_info = $answer->selectAnswer($answer_id);
$is_correct = $answer->isCorrect($answer_id);
$correct_answer = $is_correct == 1 ? get_lang('Yes') : get_lang('No');
$real_answer_id = $answer->selectAutoId($answer_id);
// Overwriting values depending of the question
switch ($question_obj->type) {
case FILL_IN_BLANKS:
$answer_info_db = $answer_info;
$answer_info = substr($answer_info, 0, strpos($answer_info, '::'));
$correct_answer = $is_correct;
$answers = $objExercise->fill_in_blank_answer_to_array($answer_info);
$counter = 0;
foreach ($answers as $answer_item) {
if ($counter == 0) {
$data[$id]['name'] = cut($question_obj->question, 100);
} else {
$data[$id]['name'] = '-';
示例8: showQuestion
//.........这里部分代码省略.........
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) {
示例9: prepareQuiz
//.........这里部分代码省略.........
// Attached file, if it exists.
//$attachedFile = $question->selectAttachedFile();
if (!empty($attachedFile)) {
// copy the attached file
if (!claro_copy_file($this->srcDirExercise . '/' . $attachedFile, $this->destDir . '/Exercises')) {
$this->error[] = $GLOBALS['langErrorCopyAttachedFile'] . $attachedFile;
return false;
}
// Ok, if it was an mp3, we need to copy the flash mp3-player too.
$extension = substr(strrchr($attachedFile, '.'), 1);
if ($extension == 'mp3') {
$this->mp3Found = true;
}
$pageBody .= '<tr><td colspan="2">' . display_attached_file($attachedFile) . '</td></tr>' . "\n";
}
/*
* Display the possible answers
*/
$answer = new Answer($questionId);
$answerCount = $answer->selectNbrAnswers();
// Used for matching:
$letterCounter = 'A';
$choiceCounter = 1;
$Select = array();
for ($answerId = 1; $answerId <= $answerCount; $answerId++) {
$answerText = $answer->selectAnswer($answerId);
$answerCorrect = $answer->isCorrect($answerId);
// Unique answer
if ($qtype == UNIQUE_ANSWER || $qtype == TRUE_FALSE) {
// Construct the identifier
$htmlQuestionId = 'unique_' . $questionCount . '_x';
$pageBody .= '<tr><td width="5%" align="center">
<input type="radio" name="' . $htmlQuestionId . '"
id="scorm_' . $idCounter . '"
value="' . $answer->selectWeighting($answerId) . '"></td>
<td width="95%"><label for="scorm_' . $idCounter . '">' . $answerText . '</label>
</td></tr>';
$idCounter++;
}
// Multiple answers
elseif ($qtype == MULTIPLE_ANSWER) {
// Construct the identifier
$htmlQuestionId = 'multiple_' . $questionCount . '_' . $answerId;
// Compute the score modifier if this answer is checked
$raw = $answer->selectWeighting($answerId);
$pageBody .= '<tr><td width="5%" align="center">
<input type="checkbox" name="' . $htmlQuestionId . '"
id="scorm_' . $idCounter . '"
value="' . $raw . '"></td>
<td width="95%"><label for="scorm_' . $idCounter . '">' . $answerText . '</label>
</td></tr>';
$idCounter++;
}
示例10: update_answer_records
/**
* Update user answers
*/
private function update_answer_records($key, $value)
{
// construction of the Question object
$objQuestionTmp = new Question();
// reads question informations
$objQuestionTmp->read($key);
$question_type = $objQuestionTmp->selectType();
$id = $this->id;
$eurid = $_SESSION['exerciseUserRecordID'][$id];
if ($question_type == FREE_TEXT) {
if (!empty($value)) {
Database::get()->query("UPDATE exercise_answer_record SET answer = ?s, answer_id = 1, weight = NULL,\n is_answered = 1 WHERE eurid = ?d AND question_id = ?d", $value, $eurid, $key);
} else {
Database::get()->query("UPDATE exercise_answer_record SET answer = ?s, \n answer_id = 0, weight = 0, is_answered = 1 WHERE eurid = ?d AND question_id = ?d", $value, $eurid, $key);
}
} elseif ($question_type == FILL_IN_BLANKS) {
$objAnswersTmp = new Answer($key);
$answer_field = $objAnswersTmp->selectAnswer(1);
//splits answer string from weighting string
list($answer, $answerWeighting) = explode('::', $answer_field);
// splits weightings that are joined with a comma
$rightAnswerWeighting = explode(',', $answerWeighting);
//getting all matched strings between [ and ] delimeters
preg_match_all('#\\[(.*?)\\]#', $answer, $match);
foreach ($value as $row_key => $row_choice) {
//if user's choice is right assign rightAnswerWeight else 0
$weight = $row_choice == $match[1][$row_key - 1] ? $rightAnswerWeighting[$row_key - 1] : 0;
Database::get()->query("UPDATE exercise_answer_record SET answer = ?s, weight = ?f, is_answered = 1 \n WHERE eurid = ?d AND question_id = ?d AND answer_id = ?d", $row_choice, $weight, $eurid, $key, $row_key);
}
} elseif ($question_type == MULTIPLE_ANSWER) {
if ($value == 0) {
$row_key = 0;
$answer_weight = 0;
Database::get()->query("UPDATE exercise_answer_record SET is_answered= 1 WHERE eurid = ?d AND question_id = ?d", $eurid, $key);
} else {
$objAnswersTmp = new Answer($key);
$i = 1;
// the first time in the loop we should update in order to keep question position in the DB
// and then insert a new record if there are more than one answers
foreach ($value as $row_key => $row_choice) {
$answer_weight = $objAnswersTmp->selectWeighting($row_key);
if ($i == 1) {
Database::get()->query("UPDATE exercise_answer_record SET answer_id = ?d, weight = ?f , is_answered = 1 WHERE eurid = ?d AND question_id = ?d", $row_key, $answer_weight, $eurid, $key);
} else {
Database::get()->query("INSERT INTO exercise_answer_record (eurid, question_id, answer_id, weight, is_answered)\n VALUES (?d, ?d, ?d, ?f, 1)", $eurid, $key, $row_key, $answer_weight);
}
unset($answer_weight);
$i++;
}
unset($objAnswersTmp);
}
} elseif ($question_type == MATCHING) {
$objAnswersTmp = new Answer($key);
foreach ($value as $row_key => $row_choice) {
// In matching questions isCorrect() returns position of left column answers while $row_key returns right column position
$correct_match = $objAnswersTmp->isCorrect($row_key);
if ($correct_match == $row_choice) {
$answer_weight = $objAnswersTmp->selectWeighting($row_key);
} else {
$answer_weight = 0;
}
Database::get()->query("UPDATE exercise_answer_record SET answer_id = ?d, weight = ?f , is_answered = 1\n WHERE eurid = ?d AND question_id = ?d AND answer = ?d", $row_choice, $answer_weight, $eurid, $key, $row_key);
unset($answer_weight);
}
} else {
if ($value != 0) {
$objAnswersTmp = new Answer($key);
$answer_weight = $objAnswersTmp->selectWeighting($value);
} else {
$answer_weight = 0;
}
Database::get()->query("UPDATE exercise_answer_record SET answer_id = ?d, weight = ?f , is_answered = 1\n WHERE eurid = ?d AND question_id = ?d", $value, $answer_weight, $eurid, $key);
}
unset($objQuestionTmp);
}
示例11: createAnswersForm
/**
* function which redefines Question::createAnswersForm
* @param FormValidator $form
*/
public function createAnswersForm($form)
{
$defaults = array();
$nb_matches = $nb_options = 2;
$matches = array();
$answer = null;
$counter = 1;
if (isset($this->id)) {
$answer = new Answer($this->id);
$answer->read();
if (count($answer->nbrAnswers) > 0) {
for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
$correct = $answer->isCorrect($i);
if (empty($correct)) {
$matches[$answer->selectAutoId($i)] = chr(64 + $counter);
$counter++;
}
}
}
}
if ($form->isSubmitted()) {
$nb_matches = $form->getSubmitValue('nb_matches');
$nb_options = $form->getSubmitValue('nb_options');
if (isset($_POST['lessOptions'])) {
$nb_matches--;
$nb_options--;
}
if (isset($_POST['moreOptions'])) {
$nb_matches++;
$nb_options++;
}
} else {
if (!empty($this->id)) {
if (count($answer->nbrAnswers) > 0) {
$nb_matches = $nb_options = 0;
for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
if ($answer->isCorrect($i)) {
$nb_matches++;
$defaults['answer[' . $nb_matches . ']'] = $answer->selectAnswer($i);
$defaults['weighting[' . $nb_matches . ']'] = float_format($answer->selectWeighting($i), 1);
$defaults['matches[' . $nb_matches . ']'] = $answer->correct[$i];
} else {
$nb_options++;
$defaults['option[' . $nb_options . ']'] = $answer->selectAnswer($i);
}
}
}
} else {
$defaults['answer[1]'] = get_lang('DefaultMakeCorrespond1');
$defaults['answer[2]'] = get_lang('DefaultMakeCorrespond2');
$defaults['matches[2]'] = '2';
$defaults['option[1]'] = get_lang('DefaultMatchingOptA');
$defaults['option[2]'] = get_lang('DefaultMatchingOptB');
}
}
if (empty($matches)) {
for ($i = 1; $i <= $nb_options; ++$i) {
// fill the array with A, B, C.....
$matches[$i] = chr(64 + $i);
}
} else {
for ($i = $counter; $i <= $nb_options; ++$i) {
// fill the array with A, B, C.....
$matches[$i] = chr(64 + $i);
}
}
$form->addElement('hidden', 'nb_matches', $nb_matches);
$form->addElement('hidden', 'nb_options', $nb_options);
// DISPLAY MATCHES
$html = '<table class="table table-striped table-hover">
<thead>
<tr>
<th width="5%">' . get_lang('Number') . '</th>
<th width="70%">' . get_lang('Answer') . '</th>
<th width="15%">' . get_lang('MatchesTo') . '</th>
<th width="10%">' . get_lang('Weighting') . '</th>
</tr>
</thead>
<tbody>';
$form->addHeader(get_lang('MakeCorrespond'));
$form->addHtml($html);
if ($nb_matches < 1) {
$nb_matches = 1;
Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
}
for ($i = 1; $i <= $nb_matches; ++$i) {
$renderer =& $form->defaultRenderer();
$renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', "answer[{$i}]");
$renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', "matches[{$i}]");
$renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', "weighting[{$i}]");
$form->addHtml('<tr>');
$form->addHtml("<td>{$i}</td>");
$form->addText("answer[{$i}]", null);
$form->addSelect("matches[{$i}]", null, $matches);
$form->addText("weighting[{$i}]", null, true, ['value' => 10]);
$form->addHtml('</tr>');
//.........这里部分代码省略.........
示例12: showQuestion
function showQuestion(&$objQuestionTmp, $exerciseResult = array())
{
global $tool_content, $picturePath, $langNoAnswer, $langQuestion, $langColumnA, $langColumnB, $langMakeCorrespond, $langInfoGrades, $i, $exerciseType, $nbrQuestions, $langInfoGrade;
$questionId = $objQuestionTmp->id;
$questionWeight = $objQuestionTmp->selectWeighting();
$answerType = $objQuestionTmp->selectType();
$message = $langInfoGrades;
if (intval($questionWeight) == $questionWeight) {
$questionWeight = intval($questionWeight);
}
if ($questionWeight == 1) {
$message = $langInfoGrade;
}
$questionName = $objQuestionTmp->selectTitle();
$questionDescription = $objQuestionTmp->selectDescription();
$questionDescription_temp = $questionDescription;
$questionTypeWord = $objQuestionTmp->selectTypeWord($answerType);
$tool_content .= "\n <div class='panel panel-success'>\n <div class='panel-heading'>\n <h3 class='panel-title'>{$langQuestion} : {$i} ({$questionWeight} {$message})" . ($exerciseType == 2 ? " / " . $nbrQuestions : "") . "</h3>\n </div>\n <div class='panel-body'>\n <h4>{$questionName} <br> \n <small>{$questionTypeWord}</small>\n </h4>\n {$questionDescription_temp}\n <div class='text-center'>\n " . (file_exists($picturePath . '/quiz-' . $questionId) ? "<img src='../../{$picturePath}/quiz-{$questionId}'>" : "") . "\n </div>";
// construction of the Answer object
$objAnswerTmp = new Answer($questionId);
$nbrAnswers = $objAnswerTmp->selectNbrAnswers();
if ($answerType == FREE_TEXT) {
$text = isset($exerciseResult[$questionId]) ? $exerciseResult[$questionId] : '';
$tool_content .= rich_text_editor('choice[' . $questionId . ']', 14, 90, $text);
}
if ($answerType == UNIQUE_ANSWER || $answerType == MULTIPLE_ANSWER || $answerType == TRUE_FALSE) {
$tool_content .= "<input type='hidden' name='choice[{$questionId}]' value='0' />";
}
// only used for the answer type "Matching"
if ($answerType == MATCHING && $nbrAnswers > 0) {
$cpt1 = 'A';
$cpt2 = 1;
$Select = array();
$tool_content .= "\n <table class='table-default'>\n <tr>\n <th>{$langColumnA}</th>\n <th>{$langMakeCorrespond}</th>\n <th>{$langColumnB}</th>\n </tr>";
}
if ($answerType == FILL_IN_BLANKS) {
$tool_content .= "<div class='form-inline'>";
}
for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
$answer = $objAnswerTmp->selectAnswer($answerId);
$answer = mathfilter($answer, 12, '../../courses/mathimg/');
$answerCorrect = $objAnswerTmp->isCorrect($answerId);
if ($answerType == FILL_IN_BLANKS) {
// splits text and weightings that are joined with the character '::'
list($answer) = explode('::', $answer);
// replaces [blank] by an input field
$replace_callback = function () use($questionId, $exerciseResult) {
static $id = 0;
$id++;
$value = isset($exerciseResult[$questionId][$id]) ? 'value = ' . $exerciseResult[$questionId][$id] : '';
return "<input type='text' name='choice[{$questionId}][{$id}]' {$value}>";
};
$answer = preg_replace_callback('/\\[[^]]+\\]/', $replace_callback, standard_text_escape($answer));
}
// unique answer
if ($answerType == UNIQUE_ANSWER) {
$checked = isset($exerciseResult[$questionId]) && $exerciseResult[$questionId] == $answerId ? 'checked="checked"' : '';
$tool_content .= "\n <div class='radio'>\n <label>\n <input type='radio' name='choice[{$questionId}]' value='{$answerId}' {$checked}>\n " . standard_text_escape($answer) . "\n </label>\n </div>";
} elseif ($answerType == MULTIPLE_ANSWER) {
$checked = isset($exerciseResult[$questionId][$answerId]) && $exerciseResult[$questionId][$answerId] == 1 ? 'checked="checked"' : '';
$tool_content .= "\n <div class='checkbox'>\n <label>\n <input type='checkbox' name='choice[{$questionId}][{$answerId}]' value='1' {$checked}>\n " . standard_text_escape($answer) . "\n </label>\n </div>";
} elseif ($answerType == FILL_IN_BLANKS) {
$tool_content .= $answer;
} elseif ($answerType == MATCHING) {
if (!$answerCorrect) {
// options (A, B, C, ...) that will be put into the list-box
$Select[$answerId]['Lettre'] = $cpt1++;
// answers that will be shown at the right side
$Select[$answerId]['Reponse'] = standard_text_escape($answer);
} else {
$tool_content .= "\n\t\t\t\t <tr>\n\t\t\t\t <td><b>{$cpt2}.</b> " . standard_text_escape($answer) . "</td>\n\t\t\t\t <td><div align='left'>\n\t\t\t\t <select name='choice[{$questionId}][{$answerId}]'>\n\t\t\t\t\t <option value='0'>--</option>";
// fills the list-box
foreach ($Select as $key => $val) {
$selected = isset($exerciseResult[$questionId][$answerId]) && $exerciseResult[$questionId][$answerId] == $key ? 'selected="selected"' : '';
$tool_content .= "\n\t\t\t\t\t<option value=\"" . q($key) . "\" {$selected}>{$val['Lettre']}</option>";
}
$tool_content .= "</select></div></td><td width='200'>";
if (isset($Select[$cpt2])) {
$tool_content .= '<b>' . q($Select[$cpt2]['Lettre']) . '.</b> ' . $Select[$cpt2]['Reponse'];
} else {
$tool_content .= ' ';
}
$tool_content .= "</td></tr>";
$cpt2++;
// if the left side of the "matching" has been completely shown
if ($answerId == $nbrAnswers) {
// if it remains answers to shown at the right side
while (isset($Select[$cpt2])) {
$tool_content .= "\n <tr class='even'>\n <td colspan='2'>\n <table width='100%'>\n <tr>\n <td width='200'> </td>\n <td width='100'> </td>\n <td width='200' valign='top'>" . "<b>" . q($Select[$cpt2]['Lettre']) . ".</b> " . q($Select[$cpt2]['Reponse']) . "\n </td>\n </tr>\n </table>\n </td>\n </tr>";
$cpt2++;
}
// end while()
}
// end if()
}
} elseif ($answerType == TRUE_FALSE) {
$checked = isset($exerciseResult[$questionId]) && $exerciseResult[$questionId] == $answerId ? 'checked="checked"' : '';
$tool_content .= "\n <div class='radio'>\n <label>\n <input type='radio' name='choice[{$questionId}]' value='{$answerId}' {$checked}>\n " . standard_text_escape($answer) . "\n </label>\n </div>";
}
}
//.........这里部分代码省略.........
示例13: showQuestion
function showQuestion($questionId, $onlyAnswers = false)
{
global $picturePath, $urlServer;
global $langNoAnswer, $langColumnA, $langColumnB, $langMakeCorrespond;
// construction of the Question object
$objQuestionTmp = new Question();
// reads question informations
if (!$objQuestionTmp->read($questionId)) {
// question not found
return false;
}
$answerType = $objQuestionTmp->selectType();
if (!$onlyAnswers) {
$questionName = $objQuestionTmp->selectTitle();
$questionDescription = $objQuestionTmp->selectDescription();
$questionDescription_temp = standard_text_escape($questionDescription);
echo "<tr class='even'>\n <td colspan='2'><b>" . q($questionName) . "</b><br />\n {$questionDescription_temp}\n </td>\n </tr>";
if (file_exists($picturePath . '/quiz-' . $questionId)) {
echo "<tr class='even'>\n <td class='center' colspan='2'><img src='{$urlServer}/{$picturePath}/quiz-{$questionId}' /></td>\n </tr>";
}
}
// end if(!$onlyAnswers)
// construction of the Answer object
$objAnswerTmp = new Answer($questionId);
$nbrAnswers = $objAnswerTmp->selectNbrAnswers();
// only used for the answer type "Matching"
if ($answerType == MATCHING) {
$cpt1 = 'A';
$cpt2 = 1;
$select = array();
echo "\n <tr class='even'>\n <td colspan='2'>\n <table class='tbl_border' width='100%'>\n <tr>\n <th width='200'>{$langColumnA}</th>\n <th width='130'>{$langMakeCorrespond}</th>\n <th width='200'>{$langColumnB}</th>\n </tr>\n </table>\n </td>\n </tr>";
}
for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
$answer = $objAnswerTmp->selectAnswer($answerId);
$answer = mathfilter($answer, 12, '../../courses/mathimg/');
$answerCorrect = $objAnswerTmp->isCorrect($answerId);
if ($answerType == FILL_IN_BLANKS) {
// splits text and weightings that are joined with the character '::'
list($answer) = explode('::', $answer);
// replaces [blank] by an input field
$answer = preg_replace('/\\[[^]]+\\]/', '<input type="text" name="choice[' . $questionId . '][]" size="10" />', standard_text_escape($answer));
}
// unique answer
if ($answerType == UNIQUE_ANSWER) {
echo "\n <tr class='even'>\n <td class='center' width='1'>\n <input type='radio' name='choice[{$questionId}]' value='{$answerId}' />\n </td>\n <td>" . standard_text_escape($answer) . "</td>\n </tr>";
} elseif ($answerType == MULTIPLE_ANSWER) {
echo "\n <tr class='even'>\n <td width='1' align='center'>\n <input type='checkbox' name='choice[{$questionId}][{$answerId}]' value='1' />\n </td>\n <td>" . standard_text_escape($answer) . "</td>\n </tr>";
} elseif ($answerType == FILL_IN_BLANKS) {
echo "\n <tr class='even'>\n <td colspan='2'>" . $answer . "</td>\n </tr>";
} elseif ($answerType == MATCHING) {
if (!$answerCorrect) {
// options (A, B, C, ...) that will be put into the list-box
$select[$answerId]['Lettre'] = $cpt1++;
// answers that will be shown at the right side
$select[$answerId]['Reponse'] = standard_text_escape($answer);
} else {
echo "<tr class='even'>\n <td colspan='2'>\n <table class='tbl'>\n <tr>\n <td width='200'><b>{$cpt2}.</b> " . standard_text_escape($answer) . "</td>\n <td width='130'><div align='center'>\n <select name='choice[{$questionId}][{$answerId}]'>\n <option value='0'>--</option>";
// fills the list-box
foreach ($select as $key => $val) {
echo "<option value=\"{$key}\">{$val['Lettre']}</option>";
}
echo "</select></div></td>\n <td width='200'>";
if (isset($select[$cpt2])) {
echo '<b>' . $select[$cpt2]['Lettre'] . '.</b> ' . $select[$cpt2]['Reponse'];
} else {
echo ' ';
}
echo "</td></tr></table></td></tr>";
$cpt2++;
// if the left side of the "matching" has been completely shown
if ($answerId == $nbrAnswers) {
// if it remains answers to shown at the right side
while (isset($select[$cpt2])) {
echo "<tr class='even'>\n <td colspan='2'>\n <table>\n <tr>\n <td width='60%' colspan='2'> </td>\n <td width='40%' align='right' valign='top'>" . "<b>" . $select[$cpt2]['Lettre'] . ".</b> " . $select[$cpt2]['Reponse'] . "</td>\n </tr>\n </table>\n </td>\n </tr>";
$cpt2++;
}
// end while()
}
// end if()
}
} elseif ($answerType == TRUE_FALSE) {
echo "<tr class='even'>\n <td width='1' align='center'>\n <input type='radio' name='choice[{$questionId}]' value='{$answerId}' />\n </td><td>{$answer}</td>\n </tr>";
}
}
// end for()
if (!$nbrAnswers) {
echo "<tr><td colspan='2'><div class='alert alert-danger'>{$langNoAnswer}</div></td></tr>";
}
// destruction of the Answer object
unset($objAnswerTmp);
// destruction of the Question object
unset($objQuestionTmp);
return $nbrAnswers;
}
示例14: createAnswersForm
/**
* Function which redefines Question::createAnswersForm
* @param FormValidator $form
*/
public function createAnswersForm($form)
{
$defaults = array();
$nb_matches = $nb_options = 2;
$matches = array();
$answer = null;
if ($form->isSubmitted()) {
$nb_matches = $form->getSubmitValue('nb_matches');
$nb_options = $form->getSubmitValue('nb_options');
if (isset($_POST['lessMatches'])) {
$nb_matches--;
}
if (isset($_POST['moreMatches'])) {
$nb_matches++;
}
if (isset($_POST['lessOptions'])) {
$nb_options--;
}
if (isset($_POST['moreOptions'])) {
$nb_options++;
}
} else {
if (!empty($this->id)) {
$answer = new Answer($this->id);
$answer->read();
if (count($answer->nbrAnswers) > 0) {
$nb_matches = $nb_options = 0;
for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
if ($answer->isCorrect($i)) {
$nb_matches++;
$defaults['answer[' . $nb_matches . ']'] = $answer->selectAnswer($i);
$defaults['weighting[' . $nb_matches . ']'] = float_format($answer->selectWeighting($i), 1);
$answerInfo = $answer->getAnswerByAutoId($answer->correct[$i]);
$defaults['matches[' . $nb_matches . ']'] = isset($answerInfo['answer']) ? $answerInfo['answer'] : '';
} else {
$nb_options++;
$defaults['option[' . $nb_options . ']'] = $answer->selectAnswer($i);
}
}
}
} else {
$defaults['answer[1]'] = get_lang('DefaultMakeCorrespond1');
$defaults['answer[2]'] = get_lang('DefaultMakeCorrespond2');
$defaults['matches[2]'] = '2';
$defaults['option[1]'] = get_lang('DefaultMatchingOptA');
$defaults['option[2]'] = get_lang('DefaultMatchingOptB');
}
}
for ($i = 1; $i <= $nb_matches; ++$i) {
$matches[$i] = $i;
}
$form->addElement('hidden', 'nb_matches', $nb_matches);
$form->addElement('hidden', 'nb_options', $nb_options);
// DISPLAY MATCHES
$html = '<table class="table table-striped table-hover">
<thead>
<tr>
<th width="85%">' . get_lang('Answer') . '</th>
<th width="15%">' . get_lang('MatchesTo') . '</th>
<th width="10">' . get_lang('Weighting') . '</th>
</tr>
</thead>
<tbody>';
$form->addHeader(get_lang('MakeCorrespond'));
$form->addHtml($html);
if ($nb_matches < 1) {
$nb_matches = 1;
Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
}
for ($i = 1; $i <= $nb_matches; ++$i) {
$renderer =& $form->defaultRenderer();
$renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', "answer[{$i}]");
$renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', "matches[{$i}]");
$renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', "weighting[{$i}]");
$form->addHtml('<tr>');
$form->addText("answer[{$i}]", null);
$form->addSelect("matches[{$i}]", null, $matches);
$form->addText("weighting[{$i}]", null, true, ['value' => 10, 'style' => 'width: 60px;']);
$form->addHtml('</tr>');
}
$form->addHtml('</tbody></table>');
$renderer->setElementTemplate('<div class="form-group"><div class="col-sm-offset-2">{element}', 'lessMatches');
$renderer->setElementTemplate('{element}</div></div>', 'moreMatches');
global $text;
$group = [$form->addButtonDelete(get_lang('DelElem'), 'lessMatches', true), $form->addButtonCreate(get_lang('AddElem'), 'moreMatches', true), $form->addButtonSave($text, 'submitQuestion', true)];
$form->addGroup($group);
if (!empty($this->id)) {
$form->setDefaults($defaults);
} else {
if ($this->isContent == 1) {
$form->setDefaults($defaults);
}
}
$form->setConstants(['nb_matches' => $nb_matches, 'nb_options' => $nb_options]);
}
示例15: createAnswersForm
/**
* Function which redefines Question::createAnswersForm
* @param FormValidator instance
*/
public function createAnswersForm($form)
{
$defaults = array();
$nb_matches = $nb_options = 2;
if ($form->isSubmitted()) {
$nb_matches = $form->getSubmitValue('nb_matches');
$nb_options = $form->getSubmitValue('nb_options');
if (isset($_POST['lessMatches'])) {
$nb_matches--;
}
if (isset($_POST['moreMatches'])) {
$nb_matches++;
}
if (isset($_POST['lessOptions'])) {
$nb_options--;
}
if (isset($_POST['moreOptions'])) {
$nb_options++;
}
} else {
if (!empty($this->id)) {
$answer = new Answer($this->id, api_get_course_int_id());
$answer->read();
if (count($answer->nbrAnswers) > 0) {
$nb_matches = $nb_options = 0;
//for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
foreach ($answer->answer as $answerId => $answer_item) {
//$answer_id = $answer->getRealAnswerIdFromList($answerId);
if ($answer->isCorrect($answerId)) {
$nb_matches++;
$defaults['answer[' . $nb_matches . ']'] = $answer->selectAnswer($answerId);
$defaults['weighting[' . $nb_matches . ']'] = Text::float_format($answer->selectWeighting($answerId), 1);
$defaults['matches[' . $nb_matches . ']'] = $answer->correct[$answerId];
//$nb_matches;
} else {
$nb_options++;
$defaults['option[' . $nb_options . ']'] = $nb_options;
}
}
}
} else {
$defaults['answer[1]'] = get_lang('DefaultMakeCorrespond1');
$defaults['answer[2]'] = get_lang('DefaultMakeCorrespond2');
$defaults['matches[2]'] = '2';
$defaults['option[1]'] = get_lang('DefaultMatchingOptA');
$defaults['option[2]'] = get_lang('DefaultMatchingOptB');
}
}
$a_matches = array();
for ($i = 1; $i <= $nb_matches; ++$i) {
$a_matches[$i] = $i;
// fill the array with A, B, C.....
}
$form->addElement('hidden', 'nb_matches', $nb_matches);
$form->addElement('hidden', 'nb_options', $nb_options);
// DISPLAY MATCHES
$html = '<table class="data_table">
<tr>
<th width="40%">
' . get_lang('Answer') . '
</th>
<th width="40%">
' . get_lang('MatchesTo') . '
</th>
<th width="50px">
' . get_lang('Weighting') . '
</th>
</tr>';
$form->addElement('label', get_lang('MakeCorrespond') . '<br /> ' . Display::return_icon('fill_field.png'), $html);
if ($nb_matches < 1) {
$nb_matches = 1;
Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
}
for ($i = 1; $i <= $nb_matches; ++$i) {
$form->addElement('html', '<tr><td>');
$group = array();
$group[] = $form->createElement('text', 'answer[' . $i . ']', null, ' size="60" style="margin-left: 0em;"');
$group[] = $form->createElement('select', 'matches[' . $i . ']', null, $a_matches);
$group[] = $form->createElement('text', 'weighting[' . $i . ']', null, array('class' => 'span1', 'value' => 10));
$form->addGroup($group, null, null, '</td><td>');
$form->addElement('html', '</td></tr>');
$defaults['option[' . $i . ']'] = $i;
}
$form->addElement('html', '</table></div></div>');
$group = array();
$group[] = $form->createElement('style_submit_button', 'moreMatches', get_lang('AddElem'), 'class="btn plus"');
$group[] = $form->createElement('style_submit_button', 'lessMatches', get_lang('DelElem'), 'class="btn minus"');
$group[] = $form->createElement('style_submit_button', 'submitQuestion', $this->submitText, 'class="' . $this->submitClass . '"');
$form->addGroup($group);
$form->addElement('html', '</table></div></div>');
if (!empty($this->id)) {
$form->setDefaults($defaults);
} else {
if ($this->isContent == 1) {
$form->setDefaults($defaults);
}
//.........这里部分代码省略.........