numerical and random question type creation form migrated to formslib

This commit is contained in:
jamiesensei 2007-01-07 16:10:42 +00:00
parent 375ed78a93
commit 6e9b6ba207
5 changed files with 210 additions and 120 deletions

View file

@ -0,0 +1,126 @@
<?php
/**
* Defines the editing form for the numerical question type.
*
* @copyright &copy; 2007 Jamie Pratt
* @author Jamie Pratt me@jamiep.org
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package questions
*/
/**
* numerical editing form definition.
*/
class question_edit_numerical_form extends question_edit_form {
/**
* Add question-type specific form fields.
*
* @param MoodleQuickForm $mform the form being built.
*/
function definition_inner(&$mform) {
//------------------------------------------------------------------------------------------
$creategrades = get_grade_options();
$gradeoptions = $creategrades->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';
}
}
?>

View file

@ -1,90 +0,0 @@
<?php
$QTYPES[$question->qtype]->print_question_form_start($question, array(), $course, $usehtmleditor);
// Answers.
for ($i=1; $i<=count($answers); $i++) {
$answer = $answers[$i-1];
?>
<tr valign="top">
<td align="right"><b><?php echo get_string("answer", "quiz")." $i"; ?>:</b></td>
<td align="left">
<?php
if ($answer->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;
}
?>
<input type="text" name="answer[]" size="25" value="<?php p($answervalue); ?>" />&nbsp;&nbsp;
<?php echo get_string("acceptederror", "quiz"); ?>&nbsp;<input type="text" name="tolerance[]" size="15" value="<?php p($answertolerance) ?>" />&plusmn;&nbsp;&nbsp;
<?php print_string("grade");
echo ":&nbsp;";
choose_from_menu($gradeoptions, "fraction[]", $fractionval,""); ?>
<br />
</td>
</tr>
<tr valign="top">
<td align="right"><b><?php print_string("feedback", "quiz") ?>:</b></td>
<td align="left">
<textarea name="feedback[]" rows="2" cols="50"><?php p($feedbacktext) ?></textarea>
</td>
</tr>
<tr valign="top">
<td colspan="2">&nbsp;</td>
</tr>
<?php
}
// Units.
?>
<tr valign="top">
<td align="right"><b><?php print_string("unit", "quiz") ?>:</b></td>
<td align="left"><?php
if (isset($units[0]) && 1.0 === (float)$units[0]->multiplier) {
$unit = $units[0]->unit;
} else {
$unit = '';
}
$multiplier = '1.0';
?>
<input type="hidden" name="multiplier[]" value="<?php p($multiplier) ?>" />
<input align="left" type="text" id="defaultunit" name="unit[]" size="10" value="<?php p($unit) ?>" alt="<?php print_string("unit", "quiz") ?>" />
<b>(<?php print_string("optional", "quiz") ?>)</b>
</td>
</tr>
<tr valign="top">
<td></td>
<td align="left"><b><?php print_string("alternativeunits", "quiz") ?>:</b></td>
<td></td>
</tr>
<?php
for ($i = 1; $i < 6; $i++) {
if (isset($units[$i])) {
$multiplier = (float)$units[$i]->multiplier;
$unit = $units[$i]->unit;
} else {
$multiplier = '';
$unit = '';
}
?>
<tr valign="top">
<td></td>
<td align="left">
<b><?php print_string("multiplier", "quiz") ?>:</b>
<input type="text" size="10" name="multiplier[]" value="<?php p($multiplier) ?>"
alt="<?php print_string("multiplier", "quiz") ?>" />
<b>&nbsp;&nbsp;&nbsp;<?php print_string("unit", "quiz") ?>:</b>
<input type="text" name="unit[]" size="5" value="<?php p($unit) ?>" />
</td>
</tr>
<?php
} /// END for
$QTYPES[$question->qtype]->print_replacement_options($question, $course, $contextquiz);
$QTYPES[$question->qtype]->print_question_form_end($question);
?>

View file

@ -1,30 +0,0 @@
<?php // $Id$
// Get all the extra information if we're editing
if (!empty($question->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");
?>

View file

@ -0,0 +1,77 @@
<?php
/**
* Defines the editing form for the random question type.
*
* @copyright &copy; 2007 Jamie Pratt
* @author Jamie Pratt me@jamiep.org
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package questions
*/
/**
* random editing form definition.
*/
class question_edit_random_form extends question_edit_form {
/**
* Build the form definition.
*
* This adds all the form files that the default question type supports.
* If your question type does not support all these fields, then you can
* override this method and remove the ones you don't want with $mform->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';
}
}
?>