diff --git a/question/type/calculated/edit_calculated_form.php b/question/type/calculated/edit_calculated_form.php index b52f62823a4..74b989feecc 100644 --- a/question/type/calculated/edit_calculated_form.php +++ b/question/type/calculated/edit_calculated_form.php @@ -62,6 +62,9 @@ class question_edit_calculated_form extends question_edit_form { $repeatsatstart = $count + 1; $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', 1, get_string('addmoreanswerblanks', 'qtype_calculated'));*/ //------------------------------------------------------------------------------------------ + $label = get_string("sharedwildcards", "qtype_datasetdependent"); + $html2 = $this->qtypeobj->print_dataset_definitions_category($this->question); + $mform->insertElementBefore($mform->createElement('static','list',$label,$html2),'questiontext'); $mform->addElement('header', 'answerhdr', get_string('answerhdr', 'qtype_calculated')); @@ -73,7 +76,7 @@ class question_edit_calculated_form extends question_edit_form { $mform->addElement('select', 'fraction[0]', get_string('grade'), $gradeoptions); $mform->setDefault('fraction[0]', 0);*/ $mform->addElement('hidden', 'fraction[0]', 1); - $mform->setConstants(array('fraction[0]'=>PARAM_INT)); + // $mform->setConstants(array('fraction[0]'=>PARAM_INT)); $tolgrp = array(); $tolgrp[] =& $mform->createElement('text', 'tolerance[0]', get_string('tolerance', 'qtype_calculated')); @@ -171,7 +174,17 @@ class question_edit_calculated_form extends question_edit_form { //check grades /*$totalfraction = 0; $maxfraction = -1;*/ - + $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']); + $mandatorydatasets = array(); + foreach ($answers as $key => $answer){ + $mandatorydatasets += $this->qtypeobj->find_dataset_names($data['questiontext']); + } + if (count($possibledatasets) == 0 && count($mandatorydatasets )==0){ + $errors['questiontext']=get_string('atleastonewildcard', 'qtype_datasetdependent'); + foreach ($answers as $key => $answer){ + $errors['answers['.$key.']'] = get_string('atleastonewildcard', 'qtype_datasetdependent'); + } + } foreach ($answers as $key => $answer){ //check no of choices $trimmedanswer = trim($answer); diff --git a/question/type/calculated/questiontype.php b/question/type/calculated/questiontype.php index a390e25d953..b0ebea32f71 100644 --- a/question/type/calculated/questiontype.php +++ b/question/type/calculated/questiontype.php @@ -639,18 +639,25 @@ class question_calculated_qtype extends question_dataset_dependent_questiontype '3' => get_string('geometric', 'quiz')); } - function dataset_options($form, $name, $renameabledatasets=false) { + function dataset_options($form, $name, $mandatory=true,$renameabledatasets=false) { // Takes datasets from the parent implementation but // filters options that are currently not accepted by calculated // It also determines a default selection... - list($options, $selected) = parent::dataset_options($form, $name); + //$renameabledatasets not implemented anmywhere + list($options, $selected) = parent::dataset_options($form, $name,'','qtype_calculated'); + // list($options, $selected) = $this->dataset_optionsa($form, $name); + foreach ($options as $key => $whatever) { if (!ereg('^'.LITERAL.'-', $key) && $key != '0') { unset($options[$key]); } } if (!$selected) { + if ($mandatory){ $selected = LITERAL . "-0-$name"; // Default + }else { + $selected = "0"; // Default + } } return array($options, $selected); } @@ -706,6 +713,100 @@ class question_calculated_qtype extends question_dataset_dependent_questiontype } return $str; } + + /** + * This function retrieve the item count of the available category shareable + * wild cards that is added as a comment displayed when a wild card with + * the same name is displayed in datasetdefinitions_form.php + */ + function get_dataset_definitions_category($form) { + global $CFG; + $datasetdefs = array(); + $lnamemax = 30; + if (!empty($form->category)) { + $sql = "SELECT i.*,d.* + FROM {$CFG->prefix}question_datasets d, + {$CFG->prefix}question_dataset_definitions i + WHERE i.id = d.datasetdefinition + AND i.category = '$form->category' + ; + "; + if ($records = get_records_sql($sql)) { + foreach ($records as $r) { + if ( !isset ($datasetdefs["$r->name"])) $datasetdefs["$r->name"] = $r->itemcount; + } + } + } + return $datasetdefs ; + } + + /** + * This function build a table showing the available category shareable + * wild cards, their name, their definition (Min, Max, Decimal) , the item count + * and the name of the question where they are used. + * This table is intended to be add before the question text to help the user use + * these wild cards + */ + + function print_dataset_definitions_category($form) { + global $CFG; + $datasetdefs = array(); + $lnamemax = 22; + $namestr =get_string('name', 'quiz'); + $minstr=get_string('min', 'quiz'); + $maxstr=get_string('max', 'quiz'); + $rangeofvaluestr=get_string('minmax','qtype_datasetdependent'); + $questionusingstr = get_string('usedinquestion','qtype_calculated'); + $wildcardstr = get_string('wildcard', 'qtype_calculated'); + $itemscountstr = get_string('itemscount','qtype_datasetdependent'); + $text =''; + if (!empty($form->category)) { + $sql = "SELECT i.*,d.* + FROM {$CFG->prefix}question_datasets d, + {$CFG->prefix}question_dataset_definitions i + WHERE i.id = d.datasetdefinition + AND i.category = '$form->category'; + " ; + if ($records = get_records_sql($sql)) { + foreach ($records as $r) { + $sql1 = "SELECT q.* + FROM {$CFG->prefix}question q + WHERE q.id = $r->question + "; + if ( !isset ($datasetdefs["$r->type-$r->category-$r->name"])){ + $datasetdefs["$r->type-$r->category-$r->name"]= $r; + } + if ($questionb = get_records_sql($sql1)) { + $datasetdefs["$r->type-$r->category-$r->name"]->questions[$r->question]->name =$questionb[$r->question]->name ; + } + } + } + } + if (!empty ($datasetdefs)){ + + $text =""; + foreach ($datasetdefs as $datasetdef){ + list($distribution, $min, $max,$dec) = explode(':', $datasetdef->options, 4); + $text .=""; + } + $text .="
$namestr$rangeofvaluestr$itemscountstr$questionusingstr
$datasetdef->name $min - $max $datasetdef->itemcount  "; + foreach ($datasetdef->questions as $qu) { + //limit the name length displayed + if (!empty($qu->name)) { + $qu->name = (strlen($qu->name) > $lnamemax) ? + substr($qu->name, 0, $lnamemax).'...' : $qu->name; + } else { + $qu->name = ''; + } + $text .="    $qu->name
"; + } + $text .="
"; + }else{ + $text .=get_string('no shareable wild card', 'qtype_calculated'); //"NO SHAREABLE DATASETS IN THIS CATEGORY"; + } + return $text ; + } + /// BACKUP FUNCTIONS //////////////////////////// diff --git a/question/type/datasetdependent/abstractqtype.php b/question/type/datasetdependent/abstractqtype.php index 4f4a0fbca92..539ee7c508d 100644 --- a/question/type/datasetdependent/abstractqtype.php +++ b/question/type/datasetdependent/abstractqtype.php @@ -596,10 +596,10 @@ class question_dataset_dependent_questiontype extends default_questiontype { return $datasetdefs; } - function dataset_options($form, $name) { + function dataset_options($form, $name,$prefix='',$langfile='quiz') { // First options - it is not a dataset... - $options['0'] = get_string('nodataset', 'quiz'); + $options['0'] = get_string($prefix.'nodataset', $langfile); // Construct question local options global $CFG; @@ -615,9 +615,9 @@ class question_dataset_dependent_questiontype extends default_questiontype { $key = "$type-0-$name"; if ($currentdatasetdef->type == $type and $currentdatasetdef->category == 0) { - $options[$key] = get_string("keptlocal$type", 'quiz'); + $options[$key] = get_string($prefix."keptlocal$type", $langfile); } else { - $options[$key] = get_string("newlocal$type", 'quiz'); + $options[$key] = get_string($prefix."newlocal$type", $langfile); } } @@ -635,12 +635,12 @@ class question_dataset_dependent_questiontype extends default_questiontype { and $categorydef = $categorydatasetdefs[$type]) { if ($currentdatasetdef->type == $type and $currentdatasetdef->id == $categorydef->id) { - $options[$key] = get_string("keptcategory$type", 'quiz'); + $options[$key] = get_string($prefix."keptcategory$type", $langfile); } else { - $options[$key] = get_string("existingcategory$type", 'quiz'); + $options[$key] = get_string($prefix."existingcategory$type", $langfile); } } else { - $options[$key] = get_string("newcategory$type", 'quiz'); + $options[$key] = get_string($prefix."newcategory$type", $langfile); } } diff --git a/question/type/datasetdependent/datasetdefinitions_form.php b/question/type/datasetdependent/datasetdefinitions_form.php index b383d1c35f2..57f0c8d22e8 100644 --- a/question/type/datasetdependent/datasetdefinitions_form.php +++ b/question/type/datasetdependent/datasetdefinitions_form.php @@ -28,7 +28,7 @@ class question_dataset_dependent_definitions_form extends moodleform { 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)){ @@ -42,25 +42,44 @@ class question_dataset_dependent_definitions_form extends moodleform { } $key = 0; + $datadefscat= array(); + $datadefscat = $this->qtypeobj->get_dataset_definitions_category($this->question); $datasetmenus = array(); + $label = "
".get_string('datasetrole', 'qtype_datasetdependent','numerical')."
"; + $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"). " $datasetname ". get_string("substitutedby", "quiz"); + $label = get_string("wildcard", "quiz"). " $datasetname "; $mform->addElement('select', "dataset[$key]", $label, $options); + if (isset($datadefscat[$datasetname])){ + $mform->addElement('static', "there is a category", $labelsharedwildcard." $datasetname ", 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); - $label = get_string("wildcard", "quiz"). " $datasetname ". get_string("substitutedby", "quiz"); + $this->qtypeobj->dataset_options($this->question, $datasetname,false); + $label = get_string("wildcard", "quiz"). " $datasetname "; $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." $datasetname ", get_string('dataitemdefined',"qtype_datasetdependent", $datadefscat[$datasetname])); + } + + // $selected ="0"; $mform->setDefault("dataset[$key]", $selected); $datasetmenus[$datasetname]=''; $key++; @@ -81,6 +100,22 @@ class question_dataset_dependent_definitions_form extends moodleform { $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; + } } ?> \ No newline at end of file diff --git a/question/type/datasetdependent/datasetitems_form.php b/question/type/datasetdependent/datasetitems_form.php index 959a13b5da7..bc48ae07ce0 100644 --- a/question/type/datasetdependent/datasetitems_form.php +++ b/question/type/datasetdependent/datasetitems_form.php @@ -68,7 +68,7 @@ class question_dataset_dependent_items_form extends moodleform { $this->noofitems = 0; } //------------------------------------------------------------------------------------------------------------------------------ - $mform->addElement('submit', 'updatedatasets', get_string('Update the datasets parameters', 'qtype_datasetdependent')); + $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; @@ -202,7 +202,7 @@ class question_dataset_dependent_items_form extends moodleform { function validation($data){ $errors = array(); - if (isset($data['backtoquiz']) && ($this->noofitems===0)){ + if (isset($data['backtoquiz']) && ($this->noofitems==0)){ $errors['addbutton'] = get_string('youmustaddatleastoneitem', 'qtype_datasetdependent'); } return $errors;