From: jamiesensei Date: Sun, 7 Jan 2007 16:10:42 +0000 (+0000) Subject: numerical and random question type creation form migrated to formslib X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=6e9b6ba20789b9991d1d38d1f37faa1889ab617c;p=moodle.git numerical and random question type creation form migrated to formslib --- diff --git a/lang/en_utf8/qtype_numerical.php b/lang/en_utf8/qtype_numerical.php new file mode 100644 index 0000000000..baa2f2ef32 --- /dev/null +++ b/lang/en_utf8/qtype_numerical.php @@ -0,0 +1,7 @@ + \ No newline at end of file diff --git a/question/type/numerical/edit_numerical_form.php b/question/type/numerical/edit_numerical_form.php new file mode 100644 index 0000000000..25c8196ed6 --- /dev/null +++ b/question/type/numerical/edit_numerical_form.php @@ -0,0 +1,126 @@ +gradeoptions; + $repeated = array(); + $repeatedoptions = array(); + $repeated[] =& $mform->createElement('header', 'answerhdr', get_string('answerno', 'qtype_numerical', '{no}')); + + $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz')); + $repeatedoptions['answer']['type'] = PARAM_NUMBER; + + $repeated[] =& $mform->createElement('text', 'tolerance', get_string('acceptederror', 'quiz')); + $repeatedoptions['tolerance']['type'] = PARAM_NUMBER; + + $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions); + $repeatedoptions['fraction']['default'] = 0; + + $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz')); + $repeatedoptions['feedback']['type'] = PARAM_RAW; + + + if (isset($this->question->options)){ + $countanswers = count($this->question->options->answers); + } else { + $countanswers = 0; + } + $repeatsatstart = (QUESTION_NUMANS_START > ($countanswers + 1))? + QUESTION_NUMANS_START : ($countanswers + 1); + + $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', 2, get_string('addmoreanswerblanks', 'qtype_numerical')); + +//------------------------------------------------------------------------------------------ + $repeated = array(); + $repeatedoptions = array(); + $repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}')); + + $repeated[] =& $mform->createElement('text', 'unit', get_string('unit', 'quiz')); + $repeatedoptions['unit']['type'] = PARAM_NOTAGS; + + $repeated[] =& $mform->createElement('text', 'multiplier', get_string('multiplier', 'quiz')); + $repeatedoptions['multiplier']['type'] = PARAM_NUMBER; + + if (isset($this->question->options)){ + $countunits = count($this->question->options->units); + } else { + $countunits = 0; + } + $repeatsatstart = $countunits + 2; + $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'nounits', 'addunits', 2, get_string('addmoreunitblanks', 'qtype_numerical')); + + $firstunit = $mform->getElement('multiplier[0]'); + $firstunit->freeze(); + $firstunit->setValue('1.0'); + $firstunit->setPersistantFreeze(true); + } + + function set_defaults($question) { + if (isset($question->options)){ + $answers = $question->options->answers; + if (count($answers)) { + $key = 0; + foreach ($answers as $answer){ + $default_values['answer['.$key.']'] = $answer->answer; + $default_values['fraction['.$key.']'] = $answer->fraction; + $default_values['tolerance['.$key.']'] = $answer->tolerance; + $default_values['feedback['.$key.']'] = $answer->feedback; + $key++; + } + } + $units = array_values($question->options->units); + // make sure the default unit is at index 0 + usort($units, create_function('$a, $b', // make sure the default unit is at index 0 + 'if (1.0 === (float)$a->multiplier) { return -1; } else '. + 'if (1.0 === (float)$b->multiplier) { return 1; } else { return 0; }')); + if (count($units)) { + $key = 0; + foreach ($units as $unit){ + $default_values['unit['.$key.']'] = $unit->unit; + $default_values['multiplier['.$key.']'] = $unit->multiplier; + $key++; + } + } + $question = (object)((array)$question + $default_values); + } + parent::set_defaults($question); + } + function validation($data){ + $errors = array(); + $answers = $data['answer']; + $answercount = 0; + foreach ($answers as $answer){ + $trimmedanswer = trim($answer); + if (!empty($trimmedanswer)){ + $answercount++; + } + } + if ($answercount==0){ + $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_numerical'); + } + return $errors; + } + function qtype() { + return 'numerical'; + } +} +?> \ No newline at end of file diff --git a/question/type/numerical/editquestion.html b/question/type/numerical/editquestion.html deleted file mode 100644 index ce48db9ec0..0000000000 --- a/question/type/numerical/editquestion.html +++ /dev/null @@ -1,90 +0,0 @@ -qtype]->print_question_form_start($question, array(), $course, $usehtmleditor); - -// Answers. - for ($i=1; $i<=count($answers); $i++) { - $answer = $answers[$i-1]; -?> - - : - - answer === '' && !isset($answer->tolerance) && !isset($answer->fraction)) { - $answervalue = ''; - $answertolerance = ''; - $fractionval = 0; - $feedbacktext = ''; - } else { - $answervalue = $answer->answer; - $answertolerance = $answer->tolerance; - $fractionval = $answer->fraction; - $feedbacktext = $answer->feedback; - } - ?> -    -  ±   - -
- - - - : - - - - - -   - - - -: - multiplier) { - $unit = $units[0]->unit; - } else { - $unit = ''; - } - $multiplier = '1.0'; - ?> - - " /> - () - - - - -: - - -multiplier; - $unit = $units[$i]->unit; - } else { - $multiplier = ''; - $unit = ''; - } -?> - - - - : - " /> -    : - - - -qtype]->print_replacement_options($question, $course, $contextquiz); -$QTYPES[$question->qtype]->print_question_form_end($question); -?> diff --git a/question/type/numerical/editquestion.php b/question/type/numerical/editquestion.php deleted file mode 100644 index 7b5c877308..0000000000 --- a/question/type/numerical/editquestion.php +++ /dev/null @@ -1,30 +0,0 @@ -id) && isset($question->qtype) && - $QTYPES[$question->qtype]->get_question_options($question)) { - - $answers = array_values($question->options->answers); - $units = array_values($question->options->units); - usort($units, create_function('$a, $b', // make sure the default unit is at index 0 - 'if (1.0 === (float)$a->multiplier) { return -1; } else '. - 'if (1.0 === (float)$b->multiplier) { return 1; } else { return 0; }')); - } else { - $answers = array(); - $units = array(); - } - - // Add blank answers to make the number up to QUESTION_NUMANS - // or one more than current, if there are already lots. - $emptyanswer = new stdClass; - $emptyanswer->answer = ''; - $i = count($answers); - $limit = QUESTION_NUMANS; - $limit = $limit <= $i ? $i+1 : $limit; - for (; $i < $limit; $i++) { - $answers[] = $emptyanswer; - } - - print_heading_with_help(get_string("editingnumerical", "quiz"), "numerical", "quiz"); - require("$CFG->dirroot/question/type/numerical/editquestion.html"); -?> diff --git a/question/type/random/edit_random_form.php b/question/type/random/edit_random_form.php new file mode 100644 index 0000000000..39dadd7485 --- /dev/null +++ b/question/type/random/edit_random_form.php @@ -0,0 +1,77 @@ +removeElement(). + */ + function definition() { + global $COURSE, $CFG; + + $qtype = $this->qtype(); + $langfile = "qtype_$qtype"; + + $mform =& $this->_form; + + // Standard fields at the start of the form. + $mform->addElement('header', 'generalheader', get_string("general", 'form')); + + $mform->addElement('questioncategory', 'category', get_string('category', 'quiz'), + array('courseid' => $COURSE->id, 'published' => true, 'only_editable' => true)); + + $mform->addElement('text', 'name', get_string('questionname', 'quiz'), + array('size' => 50)); + $mform->setType('name', PARAM_TEXT); + $mform->addRule('name', null, 'required', null, 'client'); + + $mform->addElement('advcheckbox', 'questiontext', get_string("recurse", "quiz"), null, null, array(0, 1)); + + // Standard fields at the end of the form. + $mform->addElement('hidden', 'id'); + $mform->setType('id', PARAM_INT); + + $mform->addElement('hidden', 'qtype'); + $mform->setType('qtype', PARAM_ALPHA); + + $mform->addElement('hidden', 'inpopup'); + $mform->setType('inpopup', PARAM_INT); + + $mform->addElement('hidden', 'versioning'); + $mform->setType('versioning', PARAM_BOOL); + + $buttonarray = array(); + $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges')); + if (!empty($this->question->id)) { + $buttonarray[] = &$mform->createElement('submit', 'makecopy', get_string('makecopy', 'quiz')); + } + $buttonarray[] = &$mform->createElement('cancel'); + $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); + $mform->closeHeaderBefore('buttonar'); + } + + function set_defaults($question) { + if (empty($question->name)) { + $question->name = get_string("random", "quiz"); + } + parent::set_defaults($question); + } + + function qtype() { + return 'random'; + } +} +?> \ No newline at end of file