From: jamiesensei Date: Thu, 18 Jan 2007 11:21:10 +0000 (+0000) Subject: some more small changes to datasetdependent and calculated question type forms X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=7b41a4a98924230c0f460a7d99ff09bcc2f61d84;p=moodle.git some more small changes to datasetdependent and calculated question type forms --- diff --git a/lib/formslib.php b/lib/formslib.php index ce4891c16f..379b13cbc8 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -466,7 +466,7 @@ class moodleform { for ($i=0; $i<$repeats; $i++) { foreach ($elementobjs as $elementobj){ $elementclone = clone($elementobj); - $name=$elementclone->getName(); + $name = $elementclone->getName(); if (!empty($name)){ $elementclone->setName($name."[$i]"); } @@ -479,6 +479,7 @@ class moodleform { $elementclone->setLabel(str_replace('{no}', ($i+1), $value)); } + $mform->addElement($elementclone); } } diff --git a/question/question2.php b/question/question2.php index 28b8f26480..5bf9cbea53 100644 --- a/question/question2.php +++ b/question/question2.php @@ -72,7 +72,7 @@ if ($wizardnow!==''){ } if ($mform === null) { - print_error('missingimportantcode', 'question', $returnurl, 'question editing form definition'); + print_error('missingimportantcode', 'question', $returnurl, 'question editing form definition for "'.$question->qtype.'"'); } $toform = $question; // send the question object and a few more parameters to the form $toform->returnurl = $returnurl; @@ -110,7 +110,7 @@ if ($mform->is_cancelled()){ redirect($nexturl.'&wizardnow='.$data->wizard.$queryappend, '', 20); } } else { - // Display the question editing form + $streditingquestion = get_string('editingquestion', 'question'); if (isset($SESSION->modform->instance)) { // TODO: remove restriction to quiz @@ -121,12 +121,11 @@ if ($mform->is_cancelled()){ get_string("editquestions", "quiz").' -> '.$streditingquestion; } print_header_simple($streditingquestion, '', $strediting); - if (isset($mform->heading)){ - print $mform->heading; - } else { - print_heading_with_help(get_string("editing".$question->qtype, "quiz"), $question->qtype, "quiz"); - } - $mform->display(); - print_footer($COURSE); + + // Display a heading, question editing form and possibly some extra content needed for + // for this question type. + $QTYPES[$question->qtype]->display_question_editing_page(&$mform, $question, $wizardnow); + + print_footer($course); } ?> diff --git a/question/type/calculated/editquestion.html b/question/type/calculated/editquestion.html deleted file mode 100644 index bbce4dc60d..0000000000 --- a/question/type/calculated/editquestion.html +++ /dev/null @@ -1,116 +0,0 @@ -qtype]->print_question_form_start($question, array(), $course, $usehtmleditor); -?> - -: - - " />   - - - - - : - - " />± - - - -: - - tolerance_types(), - 'tolerancetype[]', $answers[0]->tolerancetype, false); ?> - - - -: - - ' 0 ', - '1' => ' 1 ', '2' => ' 2 ', '3' => ' 3 ', - '4' => ' 4 ', '5' => ' 5 ', '6' => ' 6 ', - '7' => ' 7 ', '8' => ' 8 ', '9' => ' 9 '), - 'correctanswerlength[]', - $answers[0]->correctanswerlength, false); ?> - get_string('decimalformat', 'quiz'), - '2' => get_string('significantfiguresformat', 'quiz')), - 'correctanswerformat[]', - $answers[0]->correctanswerformat, false); ?> - - - - : - - - - - -: - - - " /> - () - - - - -: - - - - - - - : - " size="10" - align="right" name="multiplier[]" - value="multiplier) ?>" - alt="" /> -    : - " - name="unit[]" - size="5" value="unit) ?>" - alt="" /> - - -qtype]->print_replacement_options($question, $course, $contextquiz); -$QTYPES[$question->qtype]->print_question_form_end($question, - 'onclick="return determineMinAndMax();"', - ''); -?> - diff --git a/question/type/calculated/editquestion.php b/question/type/calculated/editquestion.php deleted file mode 100644 index c8fbb93dd8..0000000000 --- a/question/type/calculated/editquestion.php +++ /dev/null @@ -1,93 +0,0 @@ -answer = ""; - $answers[$i]->feedback = ""; - $answers[$i]->fraction = "1.0"; - $answers[$i]->tolerance = "0.01"; - $answers[$i]->tolerancetype = "1"; - $answers[$i]->correctanswerlength = "2"; // Defaults to two ... - $answers[$i]->correctanswerformat = "1"; // ... decimals - } - - if (!empty($question->id)) { - $QTYPES[$question->qtype]->get_question_options($question); - if (!empty($question->options->answers)) { - // Overwrite the default valued answer slots - // with correct values from database - $answersraw = array_values($question->options->answers); - $n = count($answersraw); - for ($i = 0; $i < $n; $i++) { - $answers[$i] = $answersraw[$i]; - } - } - } - - // Units are handled the same way - // as for numerical questions - $units = array(); - for ($i=0 ; $i<6 ; $i++) { - // Make unit slots, default as blank... - $units[$i]->multiplier = ''; - $units[$i]->unit = ''; - } - if (!empty($question->id) and $unitsraw = get_records( - 'question_numerical_units', 'question', $question->id)) { - /// Find default unit and have it put in the zero slot - /// This procedure might be overridden later when - /// the unit is stripped form an answer... - foreach ($unitsraw as $key => $unit) { - if (1.0 == $unit->multiplier) { - /// Default unit found: - $units[0] = $unit; - unset($unitsraw[$key]); - break; - } - } - /// Fill remaining answer slots with whatsever left - if (!empty($unitsraw)) { - $i = 1; // The zero slot got the default unit... - foreach ($unitsraw as $unit) { - $units[$i] = $unit; - $i++; - } - } - } else { - $units[0]->multiplier = 1.0; - } - - // Strip trailing zeros from multipliers - foreach ($units as $i => $unit) { - if (ereg('^(.*\\..(.*[^0])?)0+$', $unit->multiplier, $regs1)) { - if (ereg('^(.+)\\.0$', $regs1[1], $regs2)) { - $units[$i]->multiplier = $regs2[1]; - } else { - $units[$i]->multiplier = $regs1[1]; - } - } - } - print_heading_with_help(get_string("editingcalculated", "quiz"), "calculated", "quiz"); - require("$CFG->dirroot/question/type/calculated/editquestion.html"); -} else { // $form is not empty - /*********************************************************/ - /***** Any subsequent page of the question wizard **/ - /*********************************************************/ - $qtypeobj->print_next_wizard_page($question, $form, $course); -} -?> diff --git a/question/type/datasetdependent/abstractqtype.php b/question/type/datasetdependent/abstractqtype.php index bbcc1bfba0..d957df8c76 100644 --- a/question/type/datasetdependent/abstractqtype.php +++ b/question/type/datasetdependent/abstractqtype.php @@ -185,14 +185,11 @@ class question_dataset_dependent_questiontype extends default_questiontype { case 'datasetdefinitions': require("$CFG->dirroot/question/type/datasetdependent/datasetdefinitions_form.php"); $mform =& new question_dataset_dependent_definitions_form("$submiturl?wizardnow=datasetdefinitions", $question); - $mform->heading = print_heading_with_help(get_string("choosedatasetproperties", "quiz"), "questiondatasets", "quiz", '', true); break; case 'datasetitems': require("$CFG->dirroot/question/type/datasetdependent/datasetitems_form.php"); $regenerate = optional_param('forceregeneration', 0, PARAM_BOOL); $mform =& new question_dataset_dependent_items_form("$submiturl?wizardnow=datasetitems", $question, $regenerate); - $streditdatasets = get_string("editdatasets", "quiz"); - $mform->heading = print_heading_with_help($streditdatasets, 'questiondatasets', "quiz", '', true); break; default: error('Incorrect or no wizard page specified!'); @@ -201,6 +198,35 @@ class question_dataset_dependent_questiontype extends default_questiontype { return $mform; } + + /** + * This method should be overriden if you want to include a special heading or some other + * html on a question editing page besides the question editing form. + * + * @param question_edit_form $mform a child of question_edit_form + * @param object $question + * @param string $wizardnow is '' for first page. + */ + function display_question_editing_page(&$mform, $question, $wizardnow){ + switch ($wizardnow){ + case '': + //on first page default display is fine + parent::display_question_editing_page($mform, $question, $wizardnow); + return; + break; + case 'datasetdefinitions': + print_heading_with_help(get_string("choosedatasetproperties", "quiz"), "questiondatasets", "quiz"); + break; + case 'datasetitems': + print_heading_with_help(get_string("editdatasets", "quiz"), 'questiondatasets', "quiz"); + break; + } + + + $mform->display(); + + } + function save_question($question, $form, $course) { // For dataset dependent questions a wizard is used for editing // questions. Therefore saving the question is delayed until diff --git a/question/type/datasetdependent/datasetdefinitions.php b/question/type/datasetdependent/datasetdefinitions.php deleted file mode 100644 index cdb5814c47..0000000000 --- a/question/type/datasetdependent/datasetdefinitions.php +++ /dev/null @@ -1,18 +0,0 @@ -find_dataset_names($form->questiontext); -$mandatorydatasets = array(); -foreach ($form->answers as $answer) { - $mandatorydatasets += $this - ->find_dataset_names($answer); -} -$datasets = $this->construct_dataset_menus( - $form, $mandatorydatasets, $possibledatasets); - -// Print the page -print_heading_with_help(get_string("choosedatasetproperties", "quiz"), "questiondatasets", "quiz"); -require("$CFG->dirroot/question/type/datasetdependent/questiondatasets.html"); -?> diff --git a/question/type/datasetdependent/datasetitems.php b/question/type/datasetdependent/datasetitems.php deleted file mode 100644 index 76f31d23a1..0000000000 --- a/question/type/datasetdependent/datasetitems.php +++ /dev/null @@ -1,294 +0,0 @@ -id)) { - $datasetdefs = $this->get_dataset_definitions( - $SESSION->datasetdependent->definitionform); - } else { - if (empty($question->options)) { - $this->get_question_options($question); - } - $datasetdefs = $this->get_dataset_definitions($form); - } - - // Handle generator options... - $olddatasetdefs = fullclone($datasetdefs); // Completely deep copy the array of objects - $datasetdefs = $this->update_dataset_options($olddatasetdefs, $form); - $maxnumber = -1; - $datasets = empty($form->dataset) ? $form->definition : $form->dataset; - foreach ($datasetdefs as $defid => $datasetdef) { - if (isset($datasetdef->id) - && $datasetdef->options != $olddatasetdefs[$defid]->options) { - // Save the new value for options - update_record('question_dataset_definitions', $datasetdef); - } - - // Get maxnumber - if ($maxnumber == -1 || $datasetdef->itemcount < $maxnumber) { - $maxnumber = $datasetdef->itemcount; - } - } - -// Handle adding and removing of dataset items - // This twisted condition should effectively stop resubmits caused by reloads - if (isset($form->addbutton) && $maxnumber + 1 == $form->numbertoadd) { - $addeditem->itemnumber = $form->numbertoadd; - foreach ($form->definition as $key => $defid) { - $addeditem->definition = $datasetdefs[$defid]->id; - $addeditem->value = $form->value[$key]; - if ($form->itemid[$key]) { - // Reuse an previously used record - $addeditem->id = $form->itemid[$key]; - if (!update_record('question_dataset_items', $addeditem)) { - error("Error: Unable to update dataset item"); - } - } else { - unset($addeditem->id); - if (!insert_record('question_dataset_items', $addeditem)) { - error("Error: Unable to insert dataset item"); - } - } - - foreach ($datasetdefs as $key => $newdef) { - if (isset($newdef->id) && $newdef->itemcount <= $maxnumber) { - $newdef->itemcount = $maxnumber+1; - // Save the new value for options - update_record('question_dataset_definitions', $newdef); - } - } - } - // else Success: - $maxnumber = $addeditem->itemnumber; - - } else if (isset($form->deletebutton) - && $maxnumber == $form->numbertodelete) { - // Simply decrease itemcount where == $maxnumber - foreach ($datasetdefs as $datasetdef) { - if ($datasetdef->itemcount == $maxnumber) { - $datasetdef->itemcount--; - if (!update_record('question_dataset_definitions', - $datasetdef)) { - error("Error: Unable to update itemcount"); - } - } - } - --$maxnumber; - } - - make_upload_directory("$course->id"); // Just in case - $grosscoursefiles = get_directory_list("$CFG->dataroot/$course->id", - "$CFG->moddata"); - -// Have $coursefiles indexed by file paths: - $coursefiles = array(); - foreach ($grosscoursefiles as $coursefile) { - $coursefiles[$coursefile] = $coursefile; - } - -// Generate a new dataset item (or reuse an old one) - foreach ($datasetdefs as $defid => $datasetdef) { - if (isset($datasetdef->id)) { - $datasetdefs[$defid]->items = get_records_sql( // Use number as key!! - " SELECT itemnumber, definition, id, value - FROM {$CFG->prefix}question_dataset_items - WHERE definition = $datasetdef->id "); - } - - if (isset($datasetdefs[$defid]->items[$maxnumber + 1]) && ( - empty($form->forceregeneration) && - isset($form->regenerateddefid) && - $form->regenerateddefid != $defid || - isset($form->deletebutton))) { - // Reuse existing datasets - } else { - $datasetitem = new stdClass; - $datasetitem->id = - isset($datasetdefs[$defid]->items[$maxnumber + 1]->id) - ? $datasetdefs[$defid]->items[$maxnumber + 1]->id : ''; - $datasetitem->itemnumber = $maxnumber + 1; - if ($this->supports_dataset_item_generation()) { - if (!empty($form->addbutton) || // If we added an item - !isset($form->value) || // If we don't have a value - $form->regenerateddefid == $defid) { // If we explicitly ask for regeneration - $datasetitem->value = - $this->generate_dataset_item($datasetdef->options); - } else { - $definition = array_flip($form->definition); - $datasetitem->value = $form->value[$definition[$defid]]; - } - } else { - $datasetitem->value = ''; - } - $datasetdefs[$defid]->items[$maxnumber + 1] = clone($datasetitem); - - } - } - -// Get question header if any - $strquestionheader = $this->comment_header($question); - -// Set up the table with the controls for creating new dataset items - $table->head = $addtable->head = array($straction, $strdatasetnumber); - $addtable->align = array("center", "center"); - - $forceregeneration = ''; - if ($this->supports_dataset_item_generation()) { - $force = $reuse = ''; - empty($form->forceregeneration) - ? $reuse = ' checked="checked" ' - : $force = ' checked="checked" '; - $forceregeneration = '
' . $strreuseifpossible - . '
' . $strforceregeneration; - } - - $addline[] = ''.$forceregeneration; - $addline[] = $maxnumber+1; - - - foreach ($datasetdefs as $defid => $datasetdef) { - if ($datasetdef->name) { - $table->head[] = $datasetdef->name; - $addtable->head[] = $datasetdef->name - . ($this->supports_dataset_item_generation() - ? '
' . $this->custom_generator_tools($datasetdef) - : ''); - //$table->align[] = "center"; - - if (isset($datasetdef->items[$maxnumber + 1]->id)) { - $itemid = $datasetdef->items[$maxnumber + 1]->id; - } else { - $itemid = ''; - } - - $addline[] = - '' - . "" - . ( 2 != $datasetdef->type - ? '' - : choose_from_menu($coursefiles, 'value[]', - $datasetdef->items[$maxnumber + 1]->value, - '', '', '', true)); - $data[$datasetdef->name] = $datasetdef->items[$maxnumber + 1]->value; - } - } - - if ($strquestionheader) { - $table->head[] = $strquestionheader; - $addtable->head[] = $strquestionheader; - if (empty($question->id) && - isset($SESSION->datasetdependent->questionform)) { - $tmp = &$SESSION->datasetdependent->questionform; - $answers = array(); - foreach ($tmp->answers as $key => $val) { - $answers[$key]->answer = $tmp->answers[$key]; - $answers[$key]->tolerance = $tmp->tolerance[$key]; - $answers[$key]->tolerancetype = $tmp->tolerancetype[$key]; - $answers[$key]->correctanswerlength = $tmp->correctanswerlength[$key]; - $answers[$key]->correctanswerformat = $tmp->correctanswerformat[$key]; - } - $question->options->answers = $answers; - } - $addline[] = $this->comment_on_datasetitems($question, $data, $maxnumber + 1); - } - -// Set up the table showing existing datasets - $table->data = array(); - $table->align = $addtable->align; - for ($number = $maxnumber ; $number > 0 ; --$number) { - $columns = array(); - if ($maxnumber == $number) { - $columns[] = - " - "; - } else { - $columns[] = ''; - } - $columns[] = $number; - foreach ($datasetdefs as $defid => $datasetdef) { - $columns[] = - '' - . " - " - . // Set $data: - ($data[$datasetdef->name] = $datasetdef->items[$number]->value) ; - } - if ($strquestionheader) { - $columns[] = $this->comment_on_datasetitems($question, $data, $number); - } - $table->data[] = $columns; - } - - -/// Print heading - - print_heading_with_help($streditdatasets, 'questiondatasets', "quiz"); - -// Print the new-dataset table - $addtable->data = array($addline); - echo "
-
- - id\"/> - category\"/> - qtype\"/> - - -
"; - print_table($addtable); - echo '
'; - - // - -// Print the existing-datasets table - if (!empty($table->data)) { - echo "
-
- id\"/> - category\"/> - qtype\"/> - - -
"; - print_table($table); - echo '
'; - - echo "


-
-
- id\"/> - category\"/> - qtype\"/> - - - -
-
\n"; - } else { - notify( $strdataitemneed ); - } - -?> diff --git a/question/type/datasetdependent/datasetitems_form.php b/question/type/datasetdependent/datasetitems_form.php index c043140cc8..b25bad7546 100644 --- a/question/type/datasetdependent/datasetitems_form.php +++ b/question/type/datasetdependent/datasetitems_form.php @@ -206,7 +206,7 @@ class question_dataset_dependent_items_form extends moodleform { function validation($data){ $errors = array(); - if (isset($data['backtoquiz'])){ + if (isset($data['backtoquiz']) && ($this->noofitems===0)){ $errors['addbutton'] = get_string('youmustaddatleastoneitem', 'qtype_datasetdependent'); } return $errors; diff --git a/question/type/datasetdependent/questiondatasets.html b/question/type/datasetdependent/questiondatasets.html deleted file mode 100644 index e8d31ee673..0000000000 --- a/question/type/datasetdependent/questiondatasets.html +++ /dev/null @@ -1,35 +0,0 @@ -
-
- - - - - - - - - $menu) { ?> - - - - - - - - -
{ $tmp){break;} p($name) ?>} -
- {} - -
- -
- - - - - -" /> -
-
- diff --git a/question/type/multichoice/edit_multichoice_form.php b/question/type/multichoice/edit_multichoice_form.php index 8b22f0dbdf..1701ff53c4 100644 --- a/question/type/multichoice/edit_multichoice_form.php +++ b/question/type/multichoice/edit_multichoice_form.php @@ -45,7 +45,7 @@ class question_edit_multichoice_form extends question_edit_form { $repeatsatstart = (QUESTION_NUMANS_START > ($countanswers + QUESTION_NUMANS_ADD))? QUESTION_NUMANS_START : ($countanswers + QUESTION_NUMANS_ADD); $repeatedoptions = array(); - $repeatedoptions['answer']['type'] = PARAM_TEXT; + $repeatedoptions['answer']['type'] = PARAM_NOTAGS;//text with no multilang support $repeatedoptions['fraction']['default'] = 0; $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmorechoiceblanks', 'qtype_multichoice')); diff --git a/question/type/multichoice/questiontype.php b/question/type/multichoice/questiontype.php index 916afc8e66..86a3742864 100644 --- a/question/type/multichoice/questiontype.php +++ b/question/type/multichoice/questiontype.php @@ -99,7 +99,7 @@ class question_multichoice_qtype extends default_questiontype { $update = false; $options = new stdClass; $options->question = $question->id; - + } $options->answers = implode(",",$answers); $options->single = $question->single; @@ -284,7 +284,7 @@ class question_multichoice_qtype extends default_questiontype { $qnumchar = chr(ord('a') + $key); $checked = ''; $chosen = false; - + if ($question->options->single) { $type = 'type="radio"'; $name = "name=\"{$question->name_prefix}\""; @@ -327,21 +327,21 @@ class question_multichoice_qtype extends default_questiontype { $anss[] = clone($a); } - + $feedback = ''; if ($options->feedback) { if ($state->raw_grade >= $question->maxgrade/1.01) { - $feedback = $question->options->correctfeedback; + $feedback = $question->options->correctfeedback; } else if ($state->raw_grade > 0) { - $feedback = $question->options->partiallycorrectfeedback; + $feedback = $question->options->partiallycorrectfeedback; } else { - $feedback = $question->options->incorrectfeedback; + $feedback = $question->options->incorrectfeedback; } $feedback = format_text($feedback, $question->questiontextformat, $formatoptions, $cmoptions->course); } - + include("$CFG->dirroot/question/type/multichoice/display.html"); } @@ -385,7 +385,7 @@ class question_multichoice_qtype extends default_questiontype { } return $responses; } - + /// BACKUP FUNCTIONS //////////////////////////// /* diff --git a/question/type/questiontype.php b/question/type/questiontype.php index 5a8abf9ac4..2e7d7cb404 100644 --- a/question/type/questiontype.php +++ b/question/type/questiontype.php @@ -95,6 +95,22 @@ class default_questiontype { return new $classname($submiturl, $question); } + /** + * This method should be overriden if you want to include a special heading or some other + * html on a question editing page besides the question editing form. + * + * @param question_edit_form $mform a child of question_edit_form + * @param object $question + * @param string $wizardnow is '' for first page. + */ + function display_question_editing_page(&$mform, $question, $wizardnow){ + + print_heading_with_help(get_string("editing".$question->qtype, "quiz"), $question->qtype, "quiz"); + + $mform->display(); + + } + /** * *