mirrored from git://git.moodle.org/moodle.git
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UI for editing questions changed to support sharing questions in mult…
…iple contexts. Also changed import / export and category editing form to use formslib.
- Loading branch information
jamiesensei
committed
May 20, 2007
1 parent
d8720ee
commit cd3f0da
Showing
2 changed files
with
357 additions
and
0 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
question/type/datasetdependent/datasetdefinitions_form.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,135 @@ | |||
<?php | |||
/** | |||
* @package questionbank | |||
* @subpackage questiontypes | |||
*/ | |||
|
|||
class question_dataset_dependent_definitions_form extends moodleform { | |||
/** | |||
* Question object with options and answers already loaded by get_question_options | |||
* Be careful how you use this it is needed sometimes to set up the structure of the | |||
* form in definition_inner but data is always loaded into the form with set_defaults. | |||
* | |||
* @var object | |||
*/ | |||
var $question; | |||
/** | |||
* Reference to question type object | |||
* | |||
* @var question_dataset_dependent_questiontype | |||
*/ | |||
var $qtypeobj; | |||
/** | |||
* Add question-type specific form fields. | |||
* | |||
* @param MoodleQuickForm $mform the form being built. | |||
*/ | |||
function question_dataset_dependent_definitions_form($submiturl, $question){ | |||
global $QTYPES; | |||
$this->question = $question; | |||
$this->qtypeobj =& $QTYPES[$this->question->qtype]; | |||
parent::moodleform($submiturl); | |||
} | |||
function definition() { | |||
global $SESSION; | |||
$mform =& $this->_form; | |||
$stringfile = 'qtype_'.$this->question->qtype ; | |||
$possibledatasets = $this->qtypeobj->find_dataset_names($this->question->questiontext); | |||
$mandatorydatasets = array(); | |||
if (isset($this->question->options->answers)){ | |||
foreach ($this->question->options->answers as $answer) { | |||
$mandatorydatasets += $this->qtypeobj->find_dataset_names($answer->answer); | |||
} | |||
}else{ | |||
foreach ($SESSION->datasetdependent->questionform->answers as $answer){ | |||
$mandatorydatasets += $this->qtypeobj->find_dataset_names($answer); | |||
} | |||
} | |||
|
|||
$key = 0; | |||
$datadefscat= array(); | |||
$datadefscat = $this->qtypeobj->get_dataset_definitions_category($this->question); | |||
$datasetmenus = array(); | |||
$label = "<div align=\"center\">".get_string('datasetrole', 'qtype_datasetdependent','numerical')."</div>"; | |||
$mform->addElement('html', $label);// explaining the role of datasets so other strings can be shortened | |||
$mform->addElement('header', 'mandatoryhdr', get_string('mandatoryhdr', $stringfile)); | |||
$labelsharedwildcard = get_string("sharedwildcard", "qtype_datasetdependent"); | |||
|
|||
foreach ($mandatorydatasets as $datasetname) { | |||
if (!isset($datasetmenus[$datasetname])) { | |||
list($options, $selected) = | |||
$this->qtypeobj->dataset_options($this->question, $datasetname); | |||
unset($options['0']); // Mandatory... | |||
$label = get_string("wildcard", "quiz"). " <strong>$datasetname</strong> "; | |||
$mform->addElement('select', "dataset[$key]", $label, $options); | |||
if (isset($datadefscat[$datasetname])){ | |||
$mform->addElement('static', "there is a category", $labelsharedwildcard." <strong>$datasetname </strong>", get_string('dataitemdefined',"qtype_datasetdependent", $datadefscat[$datasetname])); | |||
} | |||
$mform->setDefault("dataset[$key]", $selected); | |||
$datasetmenus[$datasetname]=''; | |||
$key++; | |||
} | |||
} | |||
$mform->addElement('header', 'possiblehdr', get_string('possiblehdr', $stringfile)); | |||
|
|||
|
|||
foreach ($possibledatasets as $datasetname) { | |||
if (!isset($datasetmenus[$datasetname])) { | |||
list($options, $selected) = | |||
$this->qtypeobj->dataset_options($this->question, $datasetname,false); | |||
$label = get_string("wildcard", "quiz"). " <strong>$datasetname</strong> "; | |||
$mform->addElement('select', "dataset[$key]", $label, $options); | |||
// $mform->addRule("dataset[$key]", null, 'required', null, 'client'); | |||
if (isset($datadefscat[$datasetname])){ | |||
$mform->addElement('static', "there is a category", $labelsharedwildcard." <strong>$datasetname </strong>", get_string('dataitemdefined',"qtype_datasetdependent", $datadefscat[$datasetname])); | |||
} | |||
|
|||
// $selected ="0"; | |||
$mform->setDefault("dataset[$key]", $selected); | |||
$datasetmenus[$datasetname]=''; | |||
$key++; | |||
} | |||
} | |||
$this->add_action_buttons(false, get_string('nextpage', 'qtype_calculated')); | |||
|
|||
|
|||
//hidden elements | |||
$mform->addElement('hidden', 'returnurl'); | |||
$mform->setType('returnurl', PARAM_URL); | |||
$mform->addElement('hidden', 'qtype'); | |||
$mform->setType('qtype', PARAM_ALPHA); | |||
$mform->addElement('hidden', 'category'); | |||
$mform->setType('category', PARAM_INT); | |||
$mform->addElement('hidden', 'id'); | |||
|
|||
$mform->addElement('hidden', 'courseid'); | |||
$mform->setType('courseid', PARAM_INT); | |||
$mform->setDefault('courseid', 0); | |||
|
|||
$mform->addElement('hidden', 'cmid'); | |||
$mform->setType('cmid', PARAM_INT); | |||
$mform->setDefault('cmid', 0); | |||
|
|||
$mform->setType('id', PARAM_INT); | |||
$mform->addElement('hidden', 'wizard', 'datasetitems'); | |||
$mform->setType('wizard', PARAM_ALPHA); | |||
} | |||
function validation($data){ | |||
$errors = array(); | |||
$datasets = $data['dataset']; | |||
$countvalid = 0 ; | |||
foreach ($datasets as $key => $dataset){ | |||
if ($dataset !="0") { | |||
$countvalid++; | |||
} | |||
} | |||
if (!$countvalid){ | |||
foreach ($datasets as $key => $dataset){ | |||
$errors['dataset['.$key.']'] = get_string('atleastonerealdataset', 'qtype_datasetdependent'); | |||
} | |||
} | |||
return $errors; | |||
} | |||
|
|||
} | |||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,222 @@ | |||
<?php | |||
class question_dataset_dependent_items_form extends moodleform { | |||
/** | |||
* Question object with options and answers already loaded by get_question_options | |||
* Be careful how you use this it is needed sometimes to set up the structure of the | |||
* form in definition_inner but data is always loaded into the form with set_defaults. | |||
* | |||
* @var object | |||
*/ | |||
var $question; | |||
/** | |||
* Reference to question type object | |||
* | |||
* @var question_dataset_dependent_questiontype | |||
*/ | |||
var $qtypeobj; | |||
|
|||
var $datasetdefs; | |||
|
|||
var $maxnumber = -1; | |||
|
|||
var $regenerate; | |||
|
|||
var $noofitems; | |||
/** | |||
* Add question-type specific form fields. | |||
* | |||
* @param MoodleQuickForm $mform the form being built. | |||
*/ | |||
function question_dataset_dependent_items_form($submiturl, $question, $regenerate){ | |||
global $QTYPES, $SESSION, $CFG; | |||
$this->regenerate = $regenerate; | |||
$this->question = $question; | |||
$this->qtypeobj =& $QTYPES[$this->question->qtype]; | |||
//get the dataset defintions for this question | |||
if (empty($question->id)) { | |||
$this->datasetdefs = $this->qtypeobj->get_dataset_definitions($question->id, $SESSION->datasetdependent->definitionform->dataset); | |||
} else { | |||
if (empty($question->options)) { | |||
$this->get_question_options($question); | |||
} | |||
$this->datasetdefs = $this->qtypeobj->get_dataset_definitions($question->id, array()); | |||
} | |||
|
|||
foreach ($this->datasetdefs as $datasetdef) { | |||
|
|||
// Get maxnumber | |||
if ($this->maxnumber == -1 || $datasetdef->itemcount < $this->maxnumber) { | |||
$this->maxnumber = $datasetdef->itemcount; | |||
} | |||
} | |||
foreach ($this->datasetdefs as $defid => $datasetdef) { | |||
if (isset($datasetdef->id)) { | |||
$this->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 "); | |||
} | |||
} | |||
parent::moodleform($submiturl); | |||
} | |||
function definition() { | |||
$mform =& $this->_form; | |||
$strquestionlabel = $this->qtypeobj->comment_header($this->question); | |||
if ($this->maxnumber != -1){ | |||
$this->noofitems = $this->maxnumber; | |||
} else { | |||
$this->noofitems = 0; | |||
} | |||
//------------------------------------------------------------------------------------------------------------------------------ | |||
$mform->addElement('submit', 'updatedatasets', get_string('updatedatasetparam', 'qtype_datasetdependent')); | |||
$mform->addElement('header', 'additemhdr', get_string('itemtoadd', 'qtype_datasetdependent')); | |||
$idx = 1; | |||
$j = (($this->noofitems) * count($this->datasetdefs))+1; | |||
foreach ($this->datasetdefs as $defkey => $datasetdef){ | |||
$mform->addElement('text', "number[$j]", get_string('param', 'qtype_datasetdependent', $datasetdef->name)); | |||
$this->qtypeobj->custom_generator_tools_part(&$mform, $idx, $j); | |||
$idx++; | |||
$mform->addElement('hidden', "definition[$j]"); | |||
$mform->addElement('hidden', "itemid[$j]"); | |||
$mform->addElement('static', "divider[$j]", '', '<hr />'); | |||
$j++; | |||
} | |||
|
|||
if ('' != $strquestionlabel){ | |||
$mform->addElement('static', 'answercomment['.($this->noofitems+1).']', $strquestionlabel); | |||
} | |||
$mform->closeHeaderBefore('addbutton'); | |||
$mform->addElement('submit', 'addbutton', get_string('additem', 'qtype_datasetdependent')); | |||
|
|||
if ($this->qtypeobj->supports_dataset_item_generation()){ | |||
$radiogrp = array(); | |||
$radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', null, get_string('reuseifpossible', 'qtype_datasetdependent'), 0); | |||
$radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', null, get_string('forceregeneration', 'qtype_datasetdependent'), 1); | |||
$mform->addGroup($radiogrp, 'forceregenerationgrp', get_string('nextitemtoadd', 'qtype_calculated'), null, false); | |||
} | |||
|
|||
$mform->addElement('submit', 'getnextbutton', get_string('getnextnow', 'qtype_datasetdependent')); | |||
|
|||
//------------------------------------------------------------------------------------------------------------------------------ | |||
$j = $this->noofitems * count($this->datasetdefs); | |||
for ($i = $this->noofitems; $i >= 1 ; $i--){ | |||
$mform->addElement('header', '', get_string('itemno', 'qtype_datasetdependent', $i)); | |||
foreach ($this->datasetdefs as $defkey => $datasetdef){ | |||
$mform->addElement('text', "number[$j]", get_string('param', 'qtype_datasetdependent', $datasetdef->name)); | |||
|
|||
$mform->addElement('hidden', "itemid[$j]"); | |||
|
|||
$mform->addElement('hidden', "definition[$j]"); | |||
|
|||
$j--; | |||
} | |||
if ('' != $strquestionlabel){ | |||
$repeated[] =& $mform->addElement('static', "answercomment[$i]", $strquestionlabel); | |||
} | |||
if ($i == $this->noofitems) {//last item | |||
$mform->addElement('submit', 'deletebutton', get_string('deletelastitem', 'qtype_datasetdependent')); | |||
} | |||
} | |||
$mform->setType('number', PARAM_NUMBER); | |||
$mform->setType('itemid', PARAM_INT); | |||
$mform->setType('definition', PARAM_NOTAGS); | |||
|
|||
|
|||
//------------------------------------------------------------------------------------------------------------------------------ | |||
//non standard name for button element needed so not using add_action_buttons | |||
$mform->addElement('submit', 'backtoquiz', get_string('savechanges')); | |||
$mform->closeHeaderBefore('backtoquiz'); | |||
|
|||
//hidden elements | |||
$mform->addElement('hidden', 'returnurl'); | |||
$mform->setType('returnurl', PARAM_URL); | |||
$mform->addElement('hidden', 'qtype'); | |||
$mform->setType('qtype', PARAM_ALPHA); | |||
$mform->addElement('hidden', 'category'); | |||
$mform->setType('category', PARAM_INT); | |||
|
|||
$mform->addElement('hidden', 'courseid'); | |||
$mform->setType('courseid', PARAM_INT); | |||
$mform->setDefault('courseid', 0); | |||
|
|||
$mform->addElement('hidden', 'cmid'); | |||
$mform->setType('cmid', PARAM_INT); | |||
$mform->setDefault('cmid', 0); | |||
|
|||
$mform->addElement('hidden', 'id'); | |||
$mform->setType('id', PARAM_INT); | |||
$mform->addElement('hidden', 'wizard', 'datasetitems'); | |||
$mform->setType('wizard', PARAM_ALPHA); | |||
} | |||
|
|||
function set_data($question){ | |||
$formdata = array(); | |||
|
|||
//fill out all data sets and also the fields for the next item to add. | |||
$j = $this->noofitems * count($this->datasetdefs); | |||
for ($itemnumber = $this->noofitems; $itemnumber >= 1; $itemnumber--){ | |||
$data = array(); | |||
foreach ($this->datasetdefs as $defid => $datasetdef){ | |||
if (isset($datasetdef->items[$itemnumber])){ | |||
$formdata["number[$j]"] = $datasetdef->items[$itemnumber]->value; | |||
$formdata["definition[$j]"] = $defid; | |||
$formdata["itemid[$j]"] = $datasetdef->items[$itemnumber]->id; | |||
$data[$datasetdef->name] = $datasetdef->items[$itemnumber]->value; | |||
} | |||
$j--; | |||
} | |||
$formdata['answercomment['.$itemnumber.']'] = $this->qtypeobj->comment_on_datasetitems($this->question, $data, $itemnumber); | |||
} | |||
|
|||
$formdata['nextpageparam[forceregeneration]'] = $this->regenerate; | |||
|
|||
$j = $this->noofitems * count($this->datasetdefs)+1; | |||
$data = array(); // data for comment_on_datasetitems later | |||
//dataset generation dafaults | |||
if ($this->qtypeobj->supports_dataset_item_generation()) { | |||
$itemnumber = $this->noofitems+1; | |||
foreach ($this->datasetdefs as $defid => $datasetdef){ | |||
$formdata["number[$j]"] = $this->qtypeobj->generate_dataset_item($datasetdef->options); | |||
$formdata["definition[$j]"] = $defid; | |||
$formdata["itemid[$j]"] = | |||
isset($datasetdef->items[$itemnumber])?$datasetdef->items[$itemnumber]->id:0; | |||
$data[$datasetdef->name] = $formdata["number[$j]"]; | |||
$j++; | |||
} | |||
} | |||
|
|||
//existing records override generated data depending on radio element | |||
$j = $this->noofitems * count($this->datasetdefs)+1; | |||
if (!$this->regenerate){ | |||
$idx = 1; | |||
$itemnumber = $this->noofitems+1; | |||
foreach ($this->datasetdefs as $defid => $datasetdef){ | |||
if (isset($datasetdef->items[$itemnumber])){ | |||
$formdata["number[$j]"] = $datasetdef->items[$itemnumber]->value; | |||
$formdata["definition[$j]"] = $defid; | |||
$formdata["itemid[$j]"] = $datasetdef->items[$itemnumber]->id; | |||
$data[$datasetdef->name] = $datasetdef->items[$itemnumber]->value; | |||
} | |||
$j++; | |||
} | |||
|
|||
} | |||
//default answercomment will get ignored if answer element is not in the form. | |||
$formdata['answercomment['.($this->noofitems+1).']'] = $this->qtypeobj->comment_on_datasetitems($this->question, $data, ($this->noofitems+1)); | |||
|
|||
$formdata = $this->qtypeobj->custom_generator_set_data($this->datasetdefs, $formdata); | |||
|
|||
parent::set_data((object)($formdata + (array)$question)); | |||
} | |||
|
|||
function validation($data){ | |||
$errors = array(); | |||
if (isset($data['backtoquiz']) && ($this->noofitems==0)){ | |||
$errors['addbutton'] = get_string('youmustaddatleastoneitem', 'qtype_datasetdependent'); | |||
} | |||
return $errors; | |||
} | |||
|
|||
|
|||
} | |||
?> |