Add New Table to Database

Add New Table to Database

Leigh Dragoon -
回帖数:4

I am currently using Moodle 2.0.  We are creating an installation specifically for a client who needs to use the Create New Account page to gather a LOT of demographic information from each user.  I have managed to add fields to the mdl_user database table and hand-code 90% of these required fields into the signup_form page.  They work fine.

The problem is that there is a chunk of information which I feel strongly should be in its own table, not merely added into the mdl_user table.  I cannot add these fields via the "add new user profile fields" option, which is what I tried first.  The clients want us to both automatically create accounts for about 400 people, AND pre-load each user's demographic information at the same time.  So I have to be able to give them a comma separated template file they can fill out, and then hand back to us, so that our DB guy can then plug the info into the database.  Unfortunately, the way Moodle organizes the table when you create new profile fields makes it impossible for us to load information this way, and  I certainly don't want to enter 400 people's worth of info by hand!

The only other option I can think of is to add 18 additional fields to the mdl_user table.  But, as I mentioned, both the database guy and I are very reluctant to do this.  I think it would just be simpler to create one new table with these 18 fields and connect it to the mdl_user table by userid or something.

Does anyone know if this is possible?  I don't know much about databases.  I don't want to unintentionally break Moodle.  I found this doc:

http://docs.moodle.org/en/Development:Installing_and_upgrading_plugin_database_tables

I don't want to make a plugin though. I just want to add one new table to the database. If anyone can help me I'd really appreciate it. If I look up database tutorials, non-specific to Moodle, will I be able to figure things out, or are there Moodle-specific pitfalls I should be aware of?

回复Leigh Dragoon

Re: Add New Table to Database

Paul Young -

Here is one solution for you to consider: Create a new database using the database activity module and then link the user information entered there to the mdl_user table. You will need to:

1) create the user accounts first by importing them into the mdl_user table

2) export their userid to an Excel file

3)  add the userid field to the comma separated template file your client will fill out by matching the name

4) create a new database using the database activity module and add the 18 additional fields plus the userid field into the database

5 upload the data in the comma separated template file to the newly created database

回复Leigh Dragoon

Re: Add New Table to Database

Tim Hunt -
Core developers的头像 Documentation writers的头像 Particularly helpful Moodlers的头像 Peer reviewers的头像 Plugin developers的头像

I don't think you should touch the mld_user table at all. That is just storing up pain for the future.

Normally, I would suggest you set this up using the 'Custom user profile fields' feature that you can configure in the Admin screens. However, you seem to have a lot of data, so custom coding may be more sensible.

To alter the database in the way you are proposing, the best way is to create a local plugin: local/extendedprofile, or something like that. You can have a db folder in there, and then follow the Development:Installing_and_upgrading_plugin_database_tables instructsions. See also the file local/readme.txt.

回复Tim Hunt

Re: Add New Table to Database

Leigh Dragoon -

Hey Tim,

Thanks so much for the advice. I think you're right, and I'm reading the tutorial you linked to.  Thanks again! 微笑