Contact Form for Moodle (webmail)

Re: Contact Form for Moodle (webmail)

by Adam Palin -
Number of replies: 1

Hello Michael,

I'm just wondering if I could ask your advice with regards to using this form to save a users input to the database rather than sending it as an email. I have spent many, many hours googling and staring at the moodle form api page not being able to work out where to begin with it all...! I came across your plugin and it looks like it will guide me in how to build the web form, if you could perhaps point me in the direction of a guide or even just what I would need to google to help with what I want to do I would be most appreciative.

Thanks in advance,
Adam

In reply to Adam Palin

Re: Contact Form for Moodle (webmail)

by Michael Milette -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi Adam,

Thanks for reaching out to me and asking your question.

Saving user input to the database is on my TO DO list for features I would like to add to Contact Form for Moodle in the future. It might not be this year as I have other priorities but it is definitely on the roadmap.

If you wanted to create your own right now, I would start by cloning the Contact Form plugin. Then look for the lines that generate the email. Instead of building the email, you would simply write the fields to a database table you created. Take a look at the Moodle data manipulation API for more information on how to do this.

Since the plugin allows any number of fields of any kind to be submitted, I would recommend that each field be added to the table as a new row instead of one row per submission. Naturally you would want to have some identifier to show which rows were part of a specific submission.

On the other hand, if you are only doing this for a single strictly defined set of fields, you could make each submission a separate row. In this case, you would pre-define your fields in the table. Instead of looping through all of the submitted fields, you would require specific fields. With that said, if you are choosing to go down this route, you might want to consider using the Moodle Forms API to collect information and then using the Moodle data manipulation API to write it to the database instead. The only reason Contact doesn't go this route is because it doesn't really lend itself easily to processing forms that can have any number of fields.

One of the strategies I use when creating a new Moodle plugin is to try and find some existing plugin that offers similar type of functionality. For example, in your case, you might want to take a look at how the Moodle Journal activity plugin is made. I haven't looked at it but it sounds like a plugin that has students write a journal entry in a Moodle form and then saves the submitted information into a table in the database.

I haven't really looked into it but have you considered whether the Moodle database activity could be used to achieve what you are trying to do? Feedback could also be an option.

I would need to know more about what you are trying to achieve and your expected outcome in order to narrow down your options.

TIP: Feel free to ask questions in the Moodle Community General Developers forum. There are lots of developers who would also be happy to answer your questions. For best results:

  1. First see if you can solve problems on your own. Don't be afraid to try things out.
  2. When you do post a request for help, use a meaningful and relevant subject line (not something like "Help" or "Question for you").
  3. Explain what you are trying to achieve.
  4. Describe the challenge you are experiencing.
  5. Show what you have tried and either include relevant source code (not the whole plugin) or a link to your project on GitHub and a short description of which file/lines the relevant source code is in.
  6. Explain what you are hoping to achieve.

Take a look at a post called "How to store form data in Moodle database table?" which is a pretty good example. You may also find it relevant to what you are trying to achieve.

Above all, always ask nicely, be patient and show gratitude for any help you get. If the answer was helpful, be sure to rate their post as useful (it is a dropdown at the bottom of their message). Remember, these people are volunteering their time to try and help you.

Thanks again for your great question! You can do this. smile

Best regards,

Michael