How to trigger event/method when student submits assignment?

How to trigger event/method when student submits assignment?

by Lloyd D -
Number of replies: 21

I need to find a way where when the student submits their assignment, their assignment (or files) are automatically copied over to a location on the local machine. I need to integrate this into my plugin which is a cloned version of the existing Onlinepoodll plugin. 

Would be very grateful if anyone could help as I'm completely stuck with this sad

Average of ratings: -
In reply to Lloyd D

Re: How to trigger event/method when student submits assignment?

by Olumuyiwa Taiwo -
Picture of Plugin developers
This may be obvious, but have you looked at the functions in lib/filelib.php?

I'm also curious as to why you want to copy the file back to the local machine, considering that the submission file would have been uploaded from the local machine.


In reply to Olumuyiwa Taiwo

Re: How to trigger event/method when student submits assignment?

by Lloyd D -

Thanks for noticing that Olumuylwa, I'm not sure why I said that.. But let me just explain exactly what I'm trying to do.. Basically, when the student submits their programming code as an assignment, their code should then be used for unit tests which are located on the teacher's computer (in their Git repository). Would this be possible to do? 

In reply to Lloyd D

Re: How to trigger event/method when student submits assignment?

by Olumuyiwa Taiwo -
Picture of Plugin developers

Assuming that the teacher's Git repository is always online (i.e. reachable from your Moodle server), this should be possible, but likely to be quite challenging, since you'll either have to ensure that every computer that'll be used by students has a local copy of Git. Alternatively you'll need to include a PHP Git library in your plugin. You'll also need to ensure that the teacher's Git repository is set up with the necessary hooks to run the unit tests.

In reply to Olumuyiwa Taiwo

Re: How to trigger event/method when student submits assignment?

by Lloyd D -

I think to make this simpler for myself.. is there a way I can just retrieve the file submitted by the student? i.e. when the student clicks the submit button, I'll be able to retrieve that file where it is saved in the mdl_files table then use/send that file to elsewhere? 

In reply to Lloyd D

Re: How to trigger event/method when student submits assignment?

by Dan Marsden -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators
Take a look at the plagiarism API.  That's pretty much exactly what it's intended for.


There's a plugin called moorsp in my github account that might work as a template to start with.

Average of ratings: Useful (1)
In reply to Dan Marsden

Re: How to trigger event/method when student submits assignment?

by Lloyd D -

Thanks for that Dan, really appreciate it. I'm unsure on two things though... how can I change it so that my plugin won't be a plagiarism plugin and instead appear under the submission plugins? Also, I can't seem to find where in the code I should change to specify/trigger what happens when the student submits their assignment? 

In reply to Lloyd D

Re: How to trigger event/method when student submits assignment?

by Dan Marsden -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators

why does it need to be a submission plugin? - Plagiarism plugins can add settings to the mod_form page as well as submissions plugins.

if you take a look at the event handling code  you will see that it has direct access to the file record and file - eg:
https://github.com/danmarsden/moodle-plagiarism_moorsp/blob/master/lib.php#L433


Once you have access to the file record you should be able to copy it into a new path.



In reply to Dan Marsden

Re: How to trigger event/method when student submits assignment?

by Dan Marsden -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators

although - it doesn't need to be a submission plugin or a plagiarism plugin really - you could just be listening to the same events as the plagiarism plugins and trigger some code on the file submission events - eg:
https://github.com/danmarsden/moodle-plagiarism_moorsp/blob/master/db/events.php#L30

In reply to Dan Marsden

Re: How to trigger event/method when student submits assignment?

by Lloyd D -

Hi Dan, sorry I'm not quite sure what you mean.. What I'd like to have in the end is the lecturer being able to specify that the submission type of an assignment is a 'Code exercise', like the below for example:

Submission types


Not a massive big deal if I can't get it that way though. But then again this plugin is now (obviously) appearing as a plagiarism plugin.. but the plugin I'm wanting to develop is a submission plugin. What are your thoughts on that?

And please excuse my confusion! ><

In reply to Lloyd D

Re: How to trigger event/method when student submits assignment?

by Dan Marsden -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators
well - if you write a submission plugin you create your own form/have direct access to the upload anyway... you will want to copy the file submissions plugin type then and do something with the form, and the resulting file.


In reply to Dan Marsden

Re: How to trigger event/method when student submits assignment?

by Lloyd D -

So you're saying that if I just copy the existing file submission plugin or clone one such as Onlinepoodll I'll have direct access to the file? To be honest with you, the plagiarism plugin you recommended seems to fit in exactly to what I want..except that it's classified as a 'plagiarism' plugin though mixed is there anyway I could change this to be a submission plugin without massive complexity?

In reply to Lloyd D

Re: How to trigger event/method when student submits assignment?

by Dan Marsden -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators

you probably need to dig around a bit and look at the code/see how it works/what it does and then decide which best suits your needs.

In reply to Dan Marsden

Re: How to trigger event/method when student submits assignment?

by Lloyd D -

Yes you're right.. I'm struggling to find a submission plugin that allows me to specify in the code what happens immediately on submission.. The plugins I've seen have a save method but that just saves the files to the Moodledata directory.. this means that when the student submits their first submission, my code will try and get the files from storage but there won't be anything there initially... what I mean is that their files won't be in storage until the save method returns true at the end. So to work around this I'd have to click the submit button again for it to check the storage area to find the files.. if that makes sense? Can you recommend any plugins that would be a suitable alternative to that of the plagiarism one? 

Thanks for all your help. Lloyd

In reply to Lloyd D

Re: How to trigger event/method when student submits assignment?

by Lloyd D -

The plagiarism plugin really does suit what I need to do..the tables and just the overall plugin is nearly exactly what I need and won't require massive amounts of changing to what I want it to do. Is there any way I could change the moorsp plugin to be of type submission instead of plagiarism? 

In reply to Dan Marsden

Re: How to trigger event/method when student submits assignment?

by Lloyd D -

Hi Dan, I've been trying to find out if I can change the moorsp plugin from plagiarism to submission type.. do you know if it's possible?

In reply to Dan Marsden

Re: How to trigger event/method when student submits assignment?

by Lloyd D -

On second thoughts, do you know how can I get my plugin to recognise that a submission has been made? just like the moorsp does. Moorsp uses events to detect this?

In reply to Lloyd D

Re: How to trigger event/method when student submits assignment?

by Dan Marsden -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators
Yes - you can copy the event handlers code from the moorsp plugin into any other plugin type. I've probably given as much guidance that I can without going and writing the code (which I can't really do for free sorry!) - Good luck! smile
In reply to Dan Marsden

Re: How to trigger event/method when student submits assignment?

by Lloyd D -

Hi again Dan, I've just spotted a big problem in my plugin to do with the event observer of a file being submitted. Basically, when the student submits their submission on an assignment that doesn't have my plugin enabled, the file uploaded event still triggers and executes my plugin code :/ do you know how I could stop this from happening?

Average of ratings: Useful (1)
In reply to Lloyd D

Re: How to trigger event/method when student submits assignment?

by Lloyd D -

I appear to have found where I should place my code to get triggered upon submission.. am I correct in saying it's in the moorsp_handle_event method?

In reply to Lloyd D

Re: How to trigger event/method when student submits assignment?

by Dan Marsden -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators

yep - that's how that plugin does it.

In reply to Lloyd D

Re: How to trigger event/method when student submits assignment?

by Damilola Olotu -

Hello, 


You could create a local plugin for this. Hook the on assignment submitted event.

$observers = array(

    //assignment submission file upload

    array(

        'eventname'   => 'assignsubmission_file\event\submission_created',

        'callback'    => 'big_observer::observe_assign_submit_file',

        'includefile' => '/local/biglocal/lib.php',

    ),

);



create a class in your lib file and call it big_observer and a function called observe_assign_submit_file.

Define your logic within this function.....