Adding multiple assignments at once through API

Adding multiple assignments at once through API

by Andrew Nadezhdin -
Number of replies: 4

Hello again!

I have an assignment plugin "Online Judge". I want to be able to add multiple instances of this assignment to the course. I have a text file with all the data needed to create an $assignment object.

Will it be OK if created an array of such objects and then just use 

require_once('assignment.class.php');

foreach ($assignments_array as $assignment) {

   add_instance($assignment);

}

Or is it a bad practice?

Average of ratings: -
In reply to Andrew Nadezhdin

Re: Adding multiple assignments at once through API

by Andrew Nadezhdin -
Can I even use existing "add_instance($assignment)" function without defining a form?
As far as I can see, the assignment object data is defined inside a form.

In reply to Andrew Nadezhdin

Re: Adding multiple assignments at once through API

by Andrew Nadezhdin -
Sorry for bumping, but I really need to know
In reply to Andrew Nadezhdin

Re: Adding multiple assignments at once through API

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

There are several steps involved in creating a new activity in a course. You need to create the activity database entry, like you are doing, and you also need to create a course_modules table entry, to represent it in the cours.  You will also need to add a course_section entry to link it to a particular section of a course, then call rebuild_course_cache to update the displayed copy of the course.

Instead of repeatedly bumping this in the forum, if you looked inside course/modedit.php (the script in the url when you are creating a new activity), you will see it call add_moduleinfo which does all this for you.

Average of ratings: Useful (2)
In reply to Davo Smith

Re: Adding multiple assignments at once through API

by Andrew Nadezhdin -
Thanks a lot. It's really helpful.
And let me apologise again for all the bumping.