Adding a new assignment type

Adding a new assignment type

by Davo Smith -
Number of replies: 17
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Just looking for a bit of advice here.

I would like to create a plugin to add a new assignment type to Moodle, but I am going to need to add some extra tables to the database for it to work (I notice that assignment already has some generic 'data' and 'value' fields, but they are not going to be enough for my purposes, at least not without some very messy code!). I am also going to need some global configuration options (through the administrator panel).

When I created a stand-alone module (realtime quiz), there was not problem putting in a database 'upgrade.php' file to insert the new tables, but for this one, I am not sure how to approach it.

Options seem to be:
1. (very bad) - overwrite the 'upgrade.php' and version info for the official assigment module with my plugin
2. (probably won't work) put the upgrade.php in a subfolder 'mod/assignment/type/mymod/database' - but I guess that wouldn't be picked up by moodle.
3. (a bit messy) create a separate 'mod/mymod' folder that handles database creation and global configuration and then have my 'mod/assignment/type/mymod/assignment.class.php' file call the functions in 'mod/mymod/lib.php'.

Number 3 seems like the best option, but I am not sure how to stop it showing up in the list of available activities (and only be available under 'assignment').
Average of ratings: -
In reply to Davo Smith

Re: Adding a new assignment type

by Red Morris -

I'm not confident on this, but I believe that upgrade.php is no longer changed, and all database changes should be made to the install.xml so that it can be compliant across all database types.

That said, I've been having trouble adding fields to the assignment table using this method mixed

In reply to Davo Smith

Re: Adding a new assignment type

by Frank Ralf -
Hi David,

Have you already looked here? Development:Installing and upgrading plugin database tables

You might also find some information on creating the install.xml file with Moodle's XMLDB editor in the Introduction to Moodle Programming Course:

http://dev.moodle.org/mod/resource/view.php?id=48
http://dev.moodle.org/mod/resource/view.php?id=55

hth
Frank
In reply to Frank Ralf

Re: Adding a new assignment type

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Thanks to both of you for getting back to me.

The problem is that I know how to update the database if I am creating a whole new plugin (as I mentioned above, I have previously written and released the 'realtime quiz' module that creates several tables for it to operate).

For that I created the following structure:
mod/realtimequiz/version.php (along with all the rest of the code for the module)
mod/realtimequiz/db/install.xml
mod/realtimequiz/db/upgrade.php

Which all worked fine.

However, what I am trying to do this time is to add a new type of assignment to the existing assignment mod. For the moment, this is going to be a 3rd-paty mod, not part of the official moodle code, so I can't mess around with any of these files:
mod/assignment/version.php
mod/assignment/db/*

The only place I can change files is here:
mod/assignment/type/<my new assignment type>

I am assuming that putting a 'version.php' file or a 'db' folder there will not be noticed by the module upgrade process, so I'm trying to work out how to do it.
In reply to Davo Smith

Re: Adding a new assignment type

by Frank Ralf -
Have you tried making a copy of the whole assignment folder, rename it "my_assignment", and make your modifications there? (However, I don't know if and where this will show up in the admin interface...)

The page Development:Assignment types is still empty, but you might find some hints via the following pages: Non-standard assignment types and Development:Assignment development

hth
Frank
In reply to Frank Ralf

Re: Adding a new assignment type

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Thanks for the suggestions.

If I can avoid duplicating the assignment module then that would be better (otherwise I don't benefit from bug fixes / upgrades to the assignment module - plus it just seems messy to have 2 copies of it). It might, however, be the only way to get it working...

I had a look at the links you gave - the first, as you pointed out, was blank. The third had some interesting ideas, but none of them are directly related to what I want to do with the assignment.

The second link was the most useful, but seems to show that there isn't a good solution to my problem - everyone else has either duplicated the assignment module (as you suggested), or came with a note saying 'add this table to your database' (which is a very unfriendly solution).

Davo
In reply to Davo Smith

Re: Adding a new assignment type (FIXED - I think!)

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
After looking through the code, I have finally got my head around what I need to do (and why some of the helpful suggestions I was given didn't seem to work).

To add some database tables along with a new assignment type, you need to do the following:

1. Add a file called 'version.php' to your '/mod/assignment/type/NEW TYPE' folder, with the contents:
<?php
$plugin->version = 2009040900; // YYYYMMDDnn for when the plugin was created
$plugin->requires = 0; // Really should be the id for the required version of the assignment
?>

2. Create a subfolder (under your 'type/NEW TYPE' folder) called 'db'.

3. This step varies, depending on your version of Moodle.

For 1.8 (which I am using) and, presumably below:
Create a mysql.sql file (and, I presume a postgres.sql file as well) in the db folder, with all the SQL instructions for creating the database.
For upgrading the database at a later date, create a mysql.php file (and a postgres.php file) in the db folder, with a function called 'assignment_NEW TYPE_upgrade' to do all the database upgrading.

For 1.9:
Use the install.xml and upgrade.php files (although the old method might still work).

For 2.0: (Not sure about this one 'assignment_upgrade_submodules' no longer exists - I guess it will be the same as above, but just called from somewhere else in the code).
In reply to Davo Smith

Re: Adding a new assignment type (FIXED - I think!)

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Actually, the method you quote for Moodle 1.9 has been the correct way to do it every since Moodle 1.7. The previous system was kept in place for a few releases for backwards compatibility.

The documentation for this install/upgrade system is at Development:Installing_and_upgrading_plugin_database_tables.
In reply to Tim Hunt

Re: Adding a new assignment type (FIXED - I think!)

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I agree that the method I quoted for 1.9 should have been the correct method from 1.7.

But, if you check the source code for the Assignment module, you will find that its handling of sub-types was not updated until 1.9:

Excerpts taken from mod/assignment/lib.php in CVS:

branch MOODLE_18_STABLE:

 assignment_upgrade_submodules 

/* snip */
if (!isset($CFG->$currentversion)) { // First install!
 set_config, >version; 
   ..>dbtype. 
 continue;
 

 upgrade_log_start;
 >debug=true;
  modify_database ..>dbtype. 
 notify;
 
 >debug=false;
 continue;
 
/* snip */
Which looks for the mysql.sql files, etc. and not for the install.xml file.

Branch MOODLE_19_STABLE:

 assignment_upgrade_submodules 

  ;

 upgrade_plugins, , ;

Which does it all correctly.

Sadly, my college is still on 1.8 (as 1.9 arrived too late for this academic year), so I'm going to have to support both versions with my plugin.

Davo
In reply to Davo Smith

Re: Adding a new assignment type (FIXED - I think!)

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I stand corrected. Thank you for taking time to explain.
In reply to Tim Hunt

Re: Adding a new assignment type (FIXED - I think!)

by Red Morris -
Thanks from me too. I think I have a handle on this now. We'll see when my dev server can be turned back on and I can start playing.
In reply to Davo Smith

Re: Adding a new assignment type (FIXED - I think!)

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I've just realised that step 1 should read:

1. Add a file called 'version.php' to your '/mod/assignment/type/NEW TYPE' folder, with the contents:
<?php

/These 2 lines are needed for Moodle 1.9 and above
$plugin->version = 2009040900; // YYYYMMDDnn for when the plugin was created
$plugin->requires = 0; // Really should be the id for the required version of the assignment

// These 2 lines are needed for Moodle 1.8 and below
$submodule->version = 2009040900;
$submodule->requires = 0;

?>

(There shouldn't be any problem with including both '$plugin' and '$submodule')
In reply to Davo Smith

Re: Adding a new assignment type

by Ilya Minkin -
Hi everybody. I want to develop a new assignment type. But Moodle changes from version to version. Will my plugin work in 2.0? How much frequently will I need to update my code to make it work in the newest version of Moodle?
In reply to Ilya Minkin

Re: Adding a new assignment type

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I'm afraid the answer is 'it depends what your plugin does'.

In my experience (writing a couple of plugins), most of the stuff is backwards compatible (at least for a couple of major release versions), even if you get a few 'this is deprecated' warnings when you show developer warnings.

Certainly most of your code will be able to transfer from one version to another entirely unchanged, but there may be occasional little changes to make (such as the function name / parameters for updating a database, or new features, such as the gradebook).

Someone with a bit more experience / in-depth knowledge of the Moodle internals, might be able to give a fuller answer.
In reply to Davo Smith

Re: Adding a new assignment type

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, we are taking the opportunity of the 1.9 -> 2.0 version number jump to make more internal changes. That means more stuff will break, however, most of it is easy to fix, and we are trying to document what you need to do.
In reply to Davo Smith

Re: Adding a new assignment type

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
As I noticed that Frank Ralf had put a link to this discussion into the documentation page for Development:Assignment Types, I thought it worth expanding into a full article.

I have never written a Moodle Docs page before and would appreciate some feedback on it (or even better, edit it with some corrections).


In reply to Davo Smith

Re: Adding a new assignment type

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
David - I really appreciate you taking the time to write this up. Sharing your experience is very helpful as it will help others to avoid some of the hurdles that you were able to clear. I noticed that Tim Hunt went in and made some changes but I would say that it looks like it covers the topic pretty well (i.e. database, language files, etc.). Peace - Anthony
In reply to Davo Smith

Re: Adding a new assignment type

by Dominik Anonymous -
Hi i have another problem related to a new assignment type. i am developing a group assignment mod and set up install.xml and upgrade.php. Everything works fine but making a course backup. My tables specified in install.xml are not backuped. The assignment files are included in the backup zip but are not restored. (i think that happens bacause is altered the strukture there: dataroot\{courseid}\moddata\assignment\{assignmentid}\{groupid}\

Is there a possibility that i can influence the backup/restore process without editing a core file? something like a upgrade.php?

regards