- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
function loadQuestionsInTest() {
$mysqli = connectDB();
$testID = $_POST['testID'];
$testSets = $mysqli -> query('select * from testsets where test_id = '.$testID.';');
if ($testSets->num_rows > 0) {
$query = [];
while ($row = $testSets -> fetch_assoc()) {
$loID = $row['lo_id'];
$questionsCount = $row['count'];
$questionsInLO = $mysqli -> query('select count(*) as num from questions where lo_id = '.$loID.';') -> fetch_assoc()['num'];
if ($questionsCount > $questionsInLO)
$questionsCount = $questionsInLO;
$query[] = '(select id, content, result, lo_id from questions where lo_id = '.$loID.' order by rand() limit '.$questionsCount.')';
}
$query = implode(' union ', $query).' order by rand();';
$questionRes = $mysqli -> query($query);
$questions = array();
while ($row = $questionRes->fetch_assoc()) {
$questionType = json_decode($row['result']) -> type;
if ($questionType == 'check') {
$question = array(
'id' => $row['id'],
'content' => $row['content'],
'loID' => $row['lo_id']
);
array_push($questions, $question);
} else if ($questionType == 'input') {
$questionContent = json_decode($row['result']);
$questionText = $questionContent -> text;
$answers = $questionContent -> answers;
for ($i = count($answers) -1; $i >= 0; $i--)
$questionText = mb_substr_replace($questionText, '(|answer'.$answers[$i] -> id.'|)', $answers[$i] -> posStart, $answers[$i] -> posEnd - $answers[$i] -> posStart);
$content = array( 'type' => 'input', 'text' => $questionText );
$question = array(
'id' => $row['id'],
'content' => json_encode($content, JSON_UNESCAPED_UNICODE),
'loID' => $row['lo_id']
);
array_push($questions, $question);
}
}
$response = json_encode($questions, JSON_UNESCAPED_UNICODE);
echo $response;
$mysqli -> close();
return;
}
echo '[]';
$mysqli -> close();
}
Моя дипломная работа по теме "тестирование студентов". Загрузка вопросов для прохождения теста из базы. Вопросы должны идти в рандомном порядке, варианты ответов тоже.