Navigation Menu

Skip to content

Commit

Permalink
MDL-27410 Big cleanup of the coding style in qtype_calculated.
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed May 16, 2011
1 parent 8f9145b commit f184c65
Show file tree
Hide file tree
Showing 12 changed files with 1,370 additions and 1,365 deletions.
Expand Up @@ -71,8 +71,8 @@ protected function define_question_plugin_structure() {
$calculatedoptions = new backup_nested_element('calculated_options');
$calculatedoption = new backup_nested_element('calculated_option', array('id'), array(
'synchronize', 'single', 'shuffleanswers', 'correctfeedback',
'correctfeedbackformat', 'partiallycorrectfeedback', 'partiallycorrectfeedbackformat', 'incorrectfeedback',
'incorrectfeedbackformat', 'answernumbering'));
'correctfeedbackformat', 'partiallycorrectfeedback', 'partiallycorrectfeedbackformat',
'incorrectfeedback', 'incorrectfeedbackformat', 'answernumbering'));

// Now the own qtype tree
$pluginwrapper->add_child($calculatedrecords);
Expand All @@ -82,8 +82,10 @@ protected function define_question_plugin_structure() {
$calculatedoptions->add_child($calculatedoption);

// set source to populate the data
$calculatedrecord->set_source_table('question_calculated', array('question' => backup::VAR_PARENTID));
$calculatedoption->set_source_table('question_calculated_options', array('question' => backup::VAR_PARENTID));
$calculatedrecord->set_source_table('question_calculated',
array('question' => backup::VAR_PARENTID));
$calculatedoption->set_source_table('question_calculated_options',
array('question' => backup::VAR_PARENTID));

// don't need to annotate ids nor files

Expand Down
Expand Up @@ -53,11 +53,11 @@ protected function define_question_plugin_structure() {

// Add own qtype stuff
$elename = 'calculated_record';
$elepath = $this->get_pathfor('/calculated_records/calculated_record'); // we used get_recommended_name() so this works
$elepath = $this->get_pathfor('/calculated_records/calculated_record');
$paths[] = new restore_path_element($elename, $elepath);

$elename = 'calculated_option';
$elepath = $this->get_pathfor('/calculated_options/calculated_option'); // we used get_recommended_name() so this works
$elepath = $this->get_pathfor('/calculated_options/calculated_option');
$paths[] = new restore_path_element($elename, $elepath);

return $paths; // And we return the interesting paths
Expand All @@ -75,19 +75,17 @@ public function process_calculated_record($data) {
// Detect if the question is created or mapped
$oldquestionid = $this->get_old_parentid('question');
$newquestionid = $this->get_new_parentid('question');
$questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false;
$questioncreated = $this->get_mappingid('question_created', $oldquestionid) ?
true : false;

// If the question has been created by restore, we need to create its question_calculated too
// If the question has been created by restore, we need to create its
// question_calculated too
if ($questioncreated) {
// Adjust some columns
$data->question = $newquestionid;
$data->answer = $this->get_mappingid('question_answer', $data->answer);
// Insert record
$newitemid = $DB->insert_record('question_calculated', $data);
// Create mapping (not needed, no files nor childs nor states here)
//$this->set_mapping('question_calculated', $oldid, $newitemid);
} else {
// Nothing to remap if the question already existed
}
}

Expand All @@ -103,18 +101,16 @@ public function process_calculated_option($data) {
// Detect if the question is created or mapped
$oldquestionid = $this->get_old_parentid('question');
$newquestionid = $this->get_new_parentid('question');
$questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false;
$questioncreated = $this->get_mappingid('question_created', $oldquestionid) ?
true : false;

// If the question has been created by restore, we need to create its question_calculated too
// If the question has been created by restore, we need to create its
// question_calculated too
if ($questioncreated) {
// Adjust some columns
$data->question = $newquestionid;
// Insert record
$newitemid = $DB->insert_record('question_calculated_options', $data);
// Create mapping (not needed, no files nor childs nor states here)
// $this->set_mapping('question_calculated_option', $oldid, $newitemid);
} else {
// Nothing to remap if the question already existed
}
}
}
115 changes: 60 additions & 55 deletions question/type/calculated/datasetdefinitions_form.php
Expand Up @@ -41,42 +41,43 @@ class question_dataset_dependent_definitions_form extends moodleform {
*
* @var object
*/
var $question;
protected $question;
/**
* Reference to question type object
*
* @var question_dataset_dependent_questiontype
*/
var $qtypeobj;
protected $qtypeobj;
/**
* Add question-type specific form fields.
*
* @param MoodleQuickForm $mform the form being built.
*/
function question_dataset_dependent_definitions_form($submiturl, $question){
global $QTYPES, $DB;
public function __construct($submiturl, $question) {
global $DB;
$this->question = $question;
$this->qtypeobj =& $QTYPES[$this->question->qtype];
$this->qtypeobj = $QTYPES[$this->question->qtype];
// Validate the question category.
if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
if (!$category = $DB->get_record('question_categories',
array('id' => $question->category))) {
print_error('categorydoesnotexist', 'question', $returnurl);
}
$this->category = $category;
$this->categorycontext = get_context_instance_by_id($category->contextid);
parent::moodleform($submiturl);
parent::__construct($submiturl);
}

protected function definition() {
global $SESSION;
$mform =& $this->_form;
$mform = $this->_form;
$possibledatasets = $this->qtypeobj->find_dataset_names($this->question->questiontext);
$mandatorydatasets = array();
if (isset($this->question->options->answers)){
if (isset($this->question->options->answers)) {
foreach ($this->question->options->answers as $answer) {
$mandatorydatasets += $this->qtypeobj->find_dataset_names($answer->answer);
}
}else{
foreach ($SESSION->calculated->questionform->answers as $answer){
} else {
foreach ($SESSION->calculated->questionform->answers as $answer) {
$mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
}
}
Expand All @@ -86,61 +87,71 @@ protected function definition() {
$datadefscat = $this->qtypeobj->get_dataset_definitions_category($this->question);
$datasetmenus = array();
$label = "<div class='mdl-align'>".get_string('datasetrole', 'qtype_calculated')."</div>";
$mform->addElement('html', $label);// explaining the role of datasets so other strings can be shortened
$mform->addElement('header', 'mandatoryhdr', get_string('mandatoryhdr', 'qtype_calculated'));
// explaining the role of datasets so other strings can be shortened
$mform->addElement('html', $label);
$mform->addElement('header', 'mandatoryhdr',
get_string('mandatoryhdr', 'qtype_calculated'));
$labelsharedwildcard = get_string('sharedwildcard', 'qtype_calculated');

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", "qtype_calculated"). " <strong>$datasetname</strong> ";
$label = get_string('wildcard', 'qtype_calculated').
" <strong>$datasetname</strong> ";
$mform->addElement('select', "dataset[$key]", $label, $options);
if (isset($datadefscat[$datasetname])){
$mform->addElement('static', "there is a category", get_string('sharedwildcard', 'qtype_calculated',$datasetname ), get_string('dataitemdefined','qtype_calculated', $datadefscat[$datasetname]));
}
if (isset($datadefscat[$datasetname])) {
$mform->addElement('static', "there is a category",
get_string('sharedwildcard', 'qtype_calculated', $datasetname),
get_string('dataitemdefined', 'qtype_calculated',
$datadefscat[$datasetname]));
}
$mform->setDefault("dataset[$key]", $selected);
$datasetmenus[$datasetname]='';
$datasetmenus[$datasetname] = '';
$key++;
}
}
$mform->addElement('header', 'possiblehdr', get_string('possiblehdr', 'qtype_calculated'));

$mform->addElement('header', 'possiblehdr', get_string('possiblehdr', 'qtype_calculated'));

foreach ($possibledatasets as $datasetname) {
if (!isset($datasetmenus[$datasetname])) {
list($options, $selected) =
$this->qtypeobj->dataset_options($this->question, $datasetname,false);
$label = get_string("wildcard", "qtype_calculated"). " <strong>$datasetname</strong> ";
list($options, $selected) = $this->qtypeobj->dataset_options(
$this->question, $datasetname, false);
$label = get_string('wildcard', 'qtype_calculated') .
" <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", get_string('sharedwildcard', 'qtype_calculated',$datasetname ), get_string('dataitemdefined',"qtype_calculated", $datadefscat[$datasetname]));
}
if (isset($datadefscat[$datasetname])) {
$mform->addElement('static', "there is a category",
get_string('sharedwildcard', 'qtype_calculated', $datasetname),
get_string('dataitemdefined', 'qtype_calculated',
$datadefscat[$datasetname]));
}

// $selected ="0";
$mform->setDefault("dataset[$key]", $selected);
$datasetmenus[$datasetname]='';
$datasetmenus[$datasetname] = '';
$key++;
}
}
// temporary strings
// temporary strings
$mform->addElement('header', 'synchronizehdr', get_string('synchronize', 'qtype_calculated'));
$mform->addElement('radio', 'synchronize', '', get_string('synchronizeno', 'qtype_calculated'),0);
$mform->addElement('radio', 'synchronize', '', get_string('synchronizeyes', 'qtype_calculated'),1);
$mform->addElement('radio', 'synchronize', '', get_string('synchronizeyesdisplay', 'qtype_calculated'),2);
if (isset($this->question->options)&& isset($this->question->options->synchronize) ){
$mform->setDefault("synchronize", $this->question->options->synchronize);
$mform->addElement('header', 'synchronizehdr',
get_string('synchronize', 'qtype_calculated'));
$mform->addElement('radio', 'synchronize', '',
get_string('synchronizeno', 'qtype_calculated'), 0);
$mform->addElement('radio', 'synchronize', '',
get_string('synchronizeyes', 'qtype_calculated'), 1);
$mform->addElement('radio', 'synchronize', '',
get_string('synchronizeyesdisplay', 'qtype_calculated'), 2);
if (isset($this->question->options) &&
isset($this->question->options->synchronize)) {
$mform->setDefault('synchronize', $this->question->options->synchronize);
} else {
$mform->setDefault("synchronize", 0 );
$mform->setDefault('synchronize', 0);
}

$this->add_action_buttons(false, get_string('nextpage', 'qtype_calculated'));


//hidden elements
// Hidden elements
$mform->addElement('hidden', 'returnurl');
$mform->setType('returnurl', PARAM_LOCALURL);
$mform->setDefault('returnurl', 0);
Expand All @@ -163,28 +174,22 @@ protected function definition() {
$mform->addElement('hidden', 'wizard', 'datasetitems');
$mform->setType('wizard', PARAM_ALPHA);
}
/* function set_data($question) {
$formdata = array();
$fromform = new stdClass();
$default_values['synchronize'] = $question->options->synchronize ;
$question = (object)((array)$question + $default_values);
}*/
function validation($data, $files) {

public function validation($data, $files) {
$errors = parent::validation($data, $files);
$datasets = $data['dataset'];
$countvalid = 0 ;
foreach ($datasets as $key => $dataset){
if ($dataset !="0") {
$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_calculated');
if (!$countvalid) {
foreach ($datasets as $key => $dataset) {
$errors['dataset['.$key.']'] =
get_string('atleastonerealdataset', 'qtype_calculated');
}
}
}
return $errors;
}

}

0 comments on commit f184c65

Please sign in to comment.