Skip to content

Commit

Permalink
UWK - Modify random questions
Browse files Browse the repository at this point in the history
  • Loading branch information
dasistwas committed Oct 23, 2024
1 parent 29e2d14 commit f9ca877
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/filestorage/tgz_extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ protected function process_header($block, $handler) {
// char uid[8];
// char gid[8];
// char size[12];
$filesize = octdec(substr($block, 124, 11));
// Solves problem with backups due to many questions added. Ported from UWK M3.11.
$filesize = octdec(rtrim(substr($block, 124, 12), "\0"));

// char mtime[12];
$mtime = octdec(substr($block, 136, 11));
Expand Down
2 changes: 1 addition & 1 deletion question/type/description/questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function is_real_question_type() {
}

public function is_usable_by_random() {
return false;
return true;
}

public function can_analyse_responses() {
Expand Down
60 changes: 60 additions & 0 deletions question/type/multichoice/question.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,41 @@ public function start_attempt(question_attempt_step $step, $variant) {
if ($this->shuffleanswers) {
shuffle($this->order);
}
// added by gtn / Schwed
if (($this instanceof qtype_multichoice_multi_question || $this instanceof qtype_multichoice_single_question) && isset($GLOBALS['gtn_quizobj'])) { // mod. by G. Schwed
/* @var quiz $quiz */
$quiz = $GLOBALS['gtn_quizobj'];
$maxAnswerCount = $quiz->get_quiz()->maxanswercount;

if ($maxAnswerCount >= 2 && count($this->order) > $maxAnswerCount) {
$random_answers = $this->order;

// always check in random order
shuffle($random_answers);

$final_random_answers = [];

// get first correct answer (so we have at least one correct answer)
foreach ($random_answers as $key => $answerid) {
$answer = $this->answers[$answerid];

if ($answer->fraction > 0) {
$final_random_answers[] = $answerid;
unset($random_answers[$key]);
break;
}
}

$final_random_answers = array_merge($final_random_answers, array_slice($random_answers, 0, $maxAnswerCount - count($final_random_answers)));

shuffle($final_random_answers);
$this->order = $final_random_answers;

$step->set_qt_var('_gtn_version', '2017062700');
}
}
// [/gtn / Schwed]

$step->set_qt_var('_order', implode(',', $this->order));
}

Expand All @@ -87,6 +122,31 @@ public function apply_attempt_state(question_attempt_step $step) {
$this->answers[$ansid] = $this->qtype->make_answer($a);
$this->answers[$ansid]->answerformat = FORMAT_HTML;
}
// [added by gtn / Schwed]
if ($this instanceof qtype_multichoice_multi_question && $step->get_qt_var('_gtn_version') === '2017062700') {
$anzahl_richtig = 0;
foreach ($this->order as $ansid) {
$answer = $this->answers[$ansid];

if ($answer->fraction > 0) {
$anzahl_richtig++;
}
}

// prozentwerte von antworten neu berechnen
foreach ($this->order as $ansid) {
$answer = $this->answers[$ansid];

if ($answer->fraction > 0) {
// richtige antworten bekommen 1 / anzahl prozent. z.b. 2 richtige antworten bekommen jeweils 50%
$answer->fraction = 1 / $anzahl_richtig;
} else {
// falsche antworten bekommen die gleichen prozent aber als abzug
$answer->fraction = -1 / $anzahl_richtig;
}
}
}
// [/gtn / Schwed]
}

public function validate_can_regrade_with_other_version(question_definition $otherversion): ?string {
Expand Down
6 changes: 6 additions & 0 deletions question/type/multichoice/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,16 @@ protected function prompt() {

public function correct_response(question_attempt $qa) {
$question = $qa->get_question();
$order = $question->get_order($qa); // added by GTN / Schwed, 2017-08-03

// Put all correct answers (100% grade) into $right.
$right = array();
foreach ($question->answers as $ansid => $ans) {
// added by GTN / Schwed, 2017-08-03
if (!in_array($ansid, $order)) {
// also check if answer is in random selected answers
continue;
}
if (question_state::graded_state_for_fraction($ans->fraction) ==
question_state::$gradedright) {
$right[] = $question->make_html_inline($question->format_text($ans->answer, $ans->answerformat,
Expand Down

0 comments on commit f9ca877

Please sign in to comment.