new table in database - having problems

new table in database - having problems

by Süreç Özcan -
Number of replies: 13
For my new questiontype I need some new fields in the database. Therefore I have written an "install.xml"-file (and also a mysql.php-, postgres7.php- and an upgrade.php-file similar to e.g. match question type) in the modules db-folder.
When I go to the Admin->notification it doesn't tell me that the table is inserted correctly.
When I go to the XMLDB editor through Admin->Miscellaneous->XMLDB editor I can find the new table.
In phpMyAdmin I can't see the table.
Am I missing something? Maybe I need to implement something in questiontype.php?
Average of ratings: -
In reply to Süreç Özcan

Re: new table in database - having problems

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Have you created a version.php file for your question type?

And you don't need mysql.php- or postgres7.php files. Those are only needed for things that existed before Moodle 1.7.
In reply to Tim Hunt

Re: new table in database - having problems

by Süreç Özcan -
yes, I have a version.php file for my question type. I had changed the version date though. Do you think that is where the problem is?

<?PHP // $Id: version.php,v 1.1 2006/08/25 21:35:43 tjhunt Exp $

$plugin->version = 2008031400; // TODO?
$plugin->requires = 2006032200;
?>

Oh good that you tell me about the other two files - I will remove them then.
In reply to Süreç Özcan

Re: new table in database - having problems

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
If you already had a version.php file then Moodle will think that the question type is already installed, and so will not look at install.xml. It will only look at upgrade.php when the version number changes.

Probalby the easiest thing to do is to delete the row with name qtype_yourquestiontype from the mdl_config table. Then Moodle will try to install your question type again, which means that it will read install.xml.
In reply to Tim Hunt

Re: new table in database - having problems

by Süreç Özcan -
was on vacation... but I just tried deleting the row you mentioned above. In the admin notification I could see creating the table and in phpMyAdmin I can see my table with its fields. smile

I have created a new question of my new question type. Wanting to edit it again I receive an error:
"Error: Missing question options for junit question 452!"

Searching in the code for this output I find it in the questiontpyes questiontype.php in the function "get_question_options":

function get_question_options(&$question) {
if (!$question->options = get_record('question_so_junit', 'question', $question->id)) {
notify('Error: Missing question options for junit question '. $question->id. '!');
return false;
}
if (!$question->options->answers = get_records('question_answers', 'question',
$question->id, 'id ASC')) {
notify('Error: Missing question answers for junit question'. $question->id. '!');
return false;
}
return true;
}

I have still a few "old" questions of that question type listed in the question bank. I had created those before I have deleted the row you mention above. Maybe I need to delete them before it works properly?
Does anybody has any idea?
In reply to Süreç Özcan

Re: new table in database - having problems

by Süreç Özcan -
I have tried to
  1. add in the edit_so_junit_form.php-file in the set_data-function the $default_values for all the new fields of my new question type, like this as similar in edit_multichoice_form.php:
    1. if (isset($question->options)){
      $default_values['givencode'] = $question->options->givencode;
      ...}
  2. delete all the questions of this question type
  3. renamed the db-folder to db1 of my question type so it won't be recognised
  4. deleted my question type from mdl_config
  5. renamed the db-folder to db back again
  6. went to admin notification - got the question type installed properly and can see the new table in phpMyAdmin
Now when I reopen the created question type I still get the same error message:
"Error: Missing question options for junit question 470!"
and now also notices saying the following for each field
"Notice: Trying to get property of non-object in D:\oezcan\workspace\moodleSue\moodle\question\type\so_junit\edit_so_junit_form.php on line 135"

I hope someone has any clue how to get it working properly?!
In reply to Süreç Özcan

Re: new table in database - having problems

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Suppose you create a new question of your new question type, then look at the contents of the question_so_junit table using phpMyAdmin. Can you see the data for your new question there? If the value in the question column for that row consistent with the id of the question you just created.

(By the way, for a new question type, it would be better to call that column questionid instead of question - the coding guidlines (point 6) changed at some point, which is why old tables don't follow that rule.
In reply to Tim Hunt

Re: new table in database - having problems

by Süreç Özcan -
Looking at the contents of the question_so_junit table using phpMyAdmin I don't see any data for my new question or any at all.
Do you know what this could mean?

I have changed question to questionid. It is just a renaming and has no other influence to anything, right?
In reply to Süreç Özcan

Re: new table in database - having problems

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Right, so it looks like the problem is when you question types gets saved when the user submits the question editing form to create a new question. Does your question type have a save_question_options method, and is it doing the right thing?

If you change the column name from question to questionid, then you will need to change any place where your code refers to $question->options->question to use $question->options->questionid instead.
In reply to Tim Hunt

Re: new table in database - having problems

by Süreç Özcan -
oh, that is a very good hint - since I am just trying out my new created db-table in my new question type. I guess before my added db-table I didn't need to change the save_question_options-method. I will have a look at it.

So as far as I understand I will have to set and check there everything I want to save in any (new) database tables belonging to the input of the teacher. Right?

Should I better make a db-schema before I go ahead and do other stuff like implementing the save_question_options-method? Or is it smart to just try out how thinks works with my temporary db-table (which will need to get changes I guess)?
In reply to Süreç Özcan

Re: new table in database - having problems

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Well, really, save_question_options, and the schema for you new table(s) really need to be developed together. They will depend closely on each other.
In reply to Tim Hunt

Re: new table in database - having problems

by Süreç Özcan -
I tried out to set options in the save_question_options-function - according to a trial database-table for my new so_junit-question-type.
Temporarily I had to change some fields from not null to null and now I can see the first entry of one of my new question-type in phpMyAdmin. The only part I didn't understand is why I can't see any "questionid". Because going back again to edit my newly created question on the top I receive again the error:
Error: Missing question options for junit question 479!

My code in save_question_options-method is the following:
// question_so_junit - table **************************************************
$update = true;
$options = get_record("question_so_junit", "question", $question->id);
if (!$options) {
$update = false;
$options = new stdClass;
$options->question = $question->id;
}

//TODO: needs to be deleted and replaced by the correct database tables!
$options->givencode = trim($question->givencode);
$options->studentanswer = trim($question->studentanswer);
$options->testclassname = trim($question->testclassname);
$options->partialscores = $question->partialscores;
$options->displayoption = $question->displayoption;

if ($update) {
if (!update_record("question_so_junit", $options)) {
$result->error = "Could not update quiz so_junit-table options! (id=$options->id)";
return $result;
}
} else {
if (!insert_record("question_so_junit", $options)) {
$result->error = "Could not insert quiz so_junit-table options!";
return $result;
}
}


If I don't have answers of the teacher to compare with the students' answer I won't need to set any fields to the question_answer-table fields, right?
In reply to Süreç Özcan

Re: new table in database - having problems

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Since the column is called questionid, in your code, you need to refer to $options->questionid instead of $options->question, and get_record('question_so_junit', 'questionid', $question->id);

About the question_answer table. You are correct. That table is there for the benefit of those question types that wish to use it, like multiplechoice, shortanswer and numerical. But for your question type you don't need to use it.