diff -Naur old/check.js new/check.js --- old/check.js 1970-01-01 05:30:00.000000000 +0530 +++ new/check.js 2012-04-24 13:52:50.000000000 +0530 @@ -0,0 +1,32 @@ + +var checkCount1=0 +var numarr=new Array(); +var array=new Array(); +i=0 +cnt=0 +function Checks1(obj,maxChecks1){ + if(maxChecks1!=cnt) + i=0 + while(i!=1) { + numarr[maxChecks1]=0 + i = 1 + } + cnt=maxChecks1 + if(obj.checked) { + numarr[maxChecks1]=numarr[maxChecks1]+1 + } else { + numarr[maxChecks1]=numarr[maxChecks1]-1 + } + + if(numarr[maxChecks1]>maxChecks1) { + obj.checked=false + numarr[maxChecks1]=numarr[maxChecks1]-1 + save = numarr[maxChecks1] + alert('you may only choose up to '+maxChecks1+' options only') + numarr[maxChecks1] = save + } + +} + + + diff -Naur old/edit_multichoice_form.php new/edit_multichoice_form.php --- old/edit_multichoice_form.php 2012-02-13 15:34:35.000000000 +0530 +++ new/edit_multichoice_form.php 2012-04-25 11:56:04.462918907 +0530 @@ -59,10 +59,21 @@ $mform->closeHeaderBefore('answersinstruct'); */ $creategrades = get_grade_options(); - $this->add_per_answer_fields($mform, get_string('choiceno', 'qtype_multichoice', '{no}'), + $var = $this->add_per_answer_fields($mform, get_string('choiceno', 'qtype_multichoice', '{no}'), $creategrades->gradeoptionsfull, max(5, QUESTION_NUMANS_START)); - $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'qtype_multichoice')); + +$no = array(); +$no[0] = 0; +for($i = 2;$i<=$var;$i++) +{ + $no[$i]=$i; +} + + $mform->addElement('select','newsingle', 'choose number',$no); + $mform->disabledIf('newsingle', 'single', 'eq', 1); + + $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'qtype_multichoice')); foreach (array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback') as $feedbackname) { $mform->addElement('editor', $feedbackname, get_string($feedbackname, 'qtype_multichoice'), @@ -91,6 +102,7 @@ } } $default_values['single'] = $question->options->single; + $default_values['newsingle'] = $question->options->newsingle; $default_values['answernumbering'] = $question->options->answernumbering; $default_values['shuffleanswers'] = $question->options->shuffleanswers; @@ -156,20 +168,26 @@ $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2); } + elseif ($answercount < $data['newsingle']){ + $errors['newsingle'] = get_string('notenoughanswers', 'qtype_multichoice', $data['newsingle']); + } /// Perform sanity checks on fractional grades - if ($data['single']) { + if ($data['single']==1) { if ($maxfraction != 1) { $maxfraction = $maxfraction * 100; $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction); - } - } else { + } + } + else { $totalfraction = round($totalfraction,2); if ($totalfraction != 1) { $totalfraction = $totalfraction * 100; $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction); } - } + } + + return $errors; } } diff -Naur old/edit_question_form.php new/edit_question_form.php --- old/edit_question_form.php 2012-02-13 15:34:35.000000000 +0530 +++ new/edit_question_form.php 2012-04-25 11:13:00.000000000 +0530 @@ -310,7 +310,8 @@ $repeatsatstart = $countanswers; } - $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', $addoptions, get_string('addmorechoiceblanks', 'qtype_multichoice')); + $var = $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', $addoptions, get_string('addmorechoiceblanks', 'qtype_multichoice')); + return $var; } function set_data($question) { diff -Naur old/questiontype.php new/questiontype.php --- old/questiontype.php 2012-02-13 15:34:35.000000000 +0530 +++ new/questiontype.php 2012-04-25 11:54:52.642918916 +0530 @@ -47,12 +47,17 @@ $answercount++; } } - if ($answercount < 2) { // check there are at lest 2 answers for multiple choice + if ($answercount < 2 && $data['single']==0) { // check there are at lest 2 answers for multiple choice $result->notice = get_string('notenoughanswers', 'qtype_multichoice', '2'); return $result; } - // Insert all the new answers + if ($answercount < $data['single']) { // check there are at lest 3 answers for multiple choice + $result->notice = get_string('notenoughanswers', 'qtype_multichoice', $data['single']); + return $result; + } + + // Insert all the new answers $totalfraction = 0; $maxfraction = -1; $answers = array(); @@ -116,6 +121,7 @@ $options->answers = implode(',', $answers); $options->single = $question->single; + $options->newsingle = $question->newsingle; if (isset($question->layout)) { $options->layout = $question->layout; } @@ -134,12 +140,13 @@ $DB->update_record('question_multichoice', $options); /// Perform sanity checks on fractional grades - if ($options->single) { + if ($options->single==1) { if ($maxfraction != 1) { $result->noticeyesno = get_string('fractionsnomax', 'qtype_multichoice', $maxfraction * 100); return $result; } - } else { + } + else { $totalfraction = round($totalfraction, 2); if ($totalfraction != 1) { $result->noticeyesno = get_string('fractionsaddwrong', 'qtype_multichoice', $totalfraction * 100); @@ -272,39 +279,66 @@ // Print formulation $questiontext = format_text($question->questiontext, $question->questiontextformat, $formatoptions, $cmoptions->course); - $answerprompt = ($question->options->single) ? get_string('singleanswer', 'quiz') : - get_string('multipleanswers', 'quiz'); - + + if($question->options->single==1) + { + $answerprompt = get_string('singleanswer', 'quiz'); + } + else + { + $answerprompt = get_string('multipleanswers', 'quiz'); + } // Print each answer in a separate row foreach ($state->options->order as $key => $aid) { $answer = &$answers[$aid]; $checked = ''; $chosen = false; - if ($question->options->single) { + $a = new stdClass; + $a->id = $question->name_prefix . $aid; + $a->class = ''; + $a->feedbackimg = ''; + + if ($question->options->single==1) { $type = 'type="radio"'; $name = "name=\"{$question->name_prefix}\""; if (isset($state->responses['']) and $aid == $state->responses['']) { $checked = 'checked="checked"'; $chosen = true; } - } else { + + $a->control = "id\" $name $checked $type value=\"$aid\" />"; + } + + + global $PAGE; + $PAGE->requires->js('/question/type/multichoice/check.js'); + + if($question->options->single==0) { $type = ' type="checkbox" '; $name = "name=\"{$question->name_prefix}{$aid}\""; if (isset($state->responses[$aid])) { $checked = 'checked="checked"'; $chosen = true; } - } - - $a = new stdClass; - $a->id = $question->name_prefix . $aid; - $a->class = ''; - $a->feedbackimg = ''; - - // Print the control - $a->control = "id\" $name $checked $type value=\"$aid\" />"; +?> +requires->js('/question/type/multichoice/check.js'); + $val = $question->options->newsingle; + + if($val == 0) { + $a->control = "id\" $name $checked $type value=\"$aid\" />"; + } + else { + $a->control = "id\" $name $checked $type onclick=\"Checks1(this, $val)\" value=\"$aid\" />"; + } + +} +?> +correct_responses && $answer->fraction > 0) { $a->class = question_get_feedback_class(1); } @@ -372,12 +406,14 @@ function grade_responses(&$question, &$state, $cmoptions) { $state->raw_grade = 0; - if($question->options->single) { + if($question->options->single==1) { $response = reset($state->responses); if ($response) { $state->raw_grade = $question->options->answers[$response]->fraction; } - } else { + } + + if($question->options->single==0) { foreach ($state->responses as $response) { if ($response) { $state->raw_grade += $question->options->answers[$response]->fraction; @@ -385,6 +421,7 @@ } } + // Make sure we don't assign negative or too high marks $state->raw_grade = min(max((float) $state->raw_grade, 0.0), 1.0) * $question->maxgrade;