Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Assign submission plugins: Difference between revisions

From MoodleDocs
Line 614: Line 614:
     }   
     }   
</pre>
</pre>
=== lib.php ===

Revision as of 06:42, 18 April 2013

Introduction

This page gives an overview of assignment submission plugin and then explains to implement a new one to capture a new type of student submission within the assignment module.

Overview of an assignment submission module

An assignment submission module is used to display custom form fields to a student when they are editing their assignment submission. It can has full control over the display the submitted assignment to graders and students. Plugins participate in all assignment features including backup/restore, upgrades from 2.2, offline grading, group assignments and blind marking.


History

Assignment submission modules were added with the assignment module rewrite for Moodle 2.3.

Template

A great example is the "onlinetext" submission plugin included with Moodle core.

File structure

The files for a custom submission plugin sit under "mod/assign/submission/<pluginname>". A plugin should not include any custom files outside of it's own plugin folder.

Note: The plugin name should be no longer than 11 characters - this is because the database tables for a submission plugin must be prefixed with "assignsubmission_" + pluginname (17 chars + X) and the table names can be no longer than 28 chars (thanks oracle). If a plugin requires multiple database tables, the plugin name will need to be shorter to allow different table names to fit under the 28 character limit.

All examples in this document exclude the required copyright and license information from source files for brevity.

version.php

To start with we need to tell Moodle the version information for our new plugin so that it can be installed and upgraded correctly. This information is added to version.php as with any other type of Moodle plugin. The component name must begin with "submission_" to identify this as a submission plugin.

See version.php for more information.

defined('MOODLE_INTERNAL') || die();                                                                                                
                                                                                                                                    
$plugin->version   = 2012112900;                                                                                                    
$plugin->requires  = 2012112900;                                                                                                    
$plugin->component = 'assignsubmission_file';

settings.php

The settings file allows us to add custom settings to the system wide configuration page for our plugin.

All submission settings should be named 'assignsubmission_pluginname/settingname' in order for the setting to be associated with the plugin.

All submission plugins should include one setting named 'default' to indicate if the plugin should be enabled by default when creating a new assignment.

This example from the submission_file plugin also checks to see if there is a maxbytes setting for this moodle installation and if found, it adds a new admin setting to the settings page. The name of the setting should begin with the plugin component name ("assignsubmission_file") in this case. The strings are specified in this plugins language file.

// Note: This is on by default.                                                                                                     
$settings->add(new admin_setting_configcheckbox('assignsubmission_file/default',                                                    
                   new lang_string('default', 'assignsubmission_file'),                                                             
                   new lang_string('default_help', 'assignsubmission_file'), 1));                                                   
                                                                                                                                    
if (isset($CFG->maxbytes)) {                                                                                                        
                                                                                                                                    
    $name = new lang_string('maximumsubmissionsize', 'assignsubmission_file');                                                      
    $description = new lang_string('configmaxbytes', 'assignsubmission_file');                                                      
                                                                                                                                    
    $element = new admin_setting_configselect('assignsubmission_file/maxbytes',                                                     
                                              $name,                                                                                
                                              $description,                                                                         
                                              1048576,                                                                              
                                              get_max_upload_sizes($CFG->maxbytes));                                                
    $settings->add($element);                                                                                                       
}                                                                          

lang/en/submission_pluginname.php

The language file for this plugin must have the same name as the component name (e.g. "submission_file.php"). It should at least define a string for "pluginname". For example:

$string['pluginname'] = 'Awesome submissions';      

db/access.php

This is where any additional capabilities are defined if required. This file can be omitted if there are no capabilities added by the plugin.

See Activity_modules#access.php for more information.

$capabilities = array(
    'submissionplugin/dungeon:master' => array(
        'riskbitmask' => RISK_XSS,
        'captype' => 'write',
        'contextlevel' => CONTEXT_COURSE,
        'archetypes' => array(
            'editingteacher' => CAP_ALLOW,
            'manager' => CAP_ALLOW
        ),
        'clonepermissionsfrom' => 'moodle/course:manageactivities'
    ),
);

db/upgrade.php

This is where any upgrade code is defined.

See Activity_modules#upgrade.php for more infomation.

function xmldb_submission_file_upgrade($oldversion) {
    global $CFG, $DB, $OUTPUT;

    $dbman = $DB->get_manager();
    if ($oldversion < 2012091800) {
        // Put upgrade code here

        // Savepoint reached.
        upgrade_plugin_savepoint(true, 2012091800, 'assignsubmission', 'file');
    }

    return true;
}

db/install.xml

This is where any database tables required to save this plugins data are defined. File submissions define a table that links to submission and contains a column to record the number of files.

<?xml version="1.0" encoding="UTF-8" ?>                                                                                             
<XMLDB PATH="mod/assign/submission/file/db" VERSION="20120423" COMMENT="XMLDB file for Moodle mod/assign/submission/file"           
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                                                                           
    xsi:noNamespaceSchemaLocation="../../../../../lib/xmldb/xmldb.xsd"                                                              
>                                                                                                                                   
  <TABLES>                                                                                                                          
    <TABLE NAME="assignsubmission_file" COMMENT="Info about file submissions for assignments">                                      
      <FIELDS>                                                                                                                      
        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>                                                    
        <FIELD NAME="assignment" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>                               
        <FIELD NAME="submission" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>                               
        <FIELD NAME="numfiles" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="The number of files the student submitted."/>
      </FIELDS>                                                                                                                     
      <KEYS>                                                                                                                        
        <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="The unique id for this submission info."/>                          
        <KEY NAME="assignment" TYPE="foreign" FIELDS="assignment" REFTABLE="assign" REFFIELDS="id" COMMENT="The assignment instance this submission relates to"/>
        <KEY NAME="submission" TYPE="foreign" FIELDS="submission" REFTABLE="assign_submission" REFFIELDS="id" COMMENT="The submission this file submission relates to."/>
      </KEYS>                                                                                                                       
    </TABLE>                                                                                                                        
  </TABLES>                                                                                                                         
</XMLDB>                           

db/install.php

This example is from the submission_comments plugin. It shows how to run custom code on installation of the plugin. In this case it makes the comments plugin the last of the three submission plugins installed by default.

/**
 * Code run after the module database tables have been created.
 */
function xmldb_submission_comments_install() {
    global $CFG, $DB, $OUTPUT;

    // do the install

    require_once($CFG->dirroot . '/mod/assign/locallib.php');
    // set the correct initial order for the plugins
    $assignment = new assignment();
    $plugin = $assignment->get_submission_plugin_by_type('comments');
    if ($plugin) {
        $plugin->move('down');
        $plugin->move('down');
    }
        
    return true;
}

locallib.php

This is where all the functionality for this plugin is defined. We will step through this file and describe each part as we go.


class assign_submission_file extends assign_submission_plugin {

All submission plugins MUST define a class with the component name of the plugin that extends assign_submission_plugin.


    public function get_name() {
        return get_string('file', 'assignsubmission_file');
    }

Get name is abstract in submission_plugin and must be defined in your new plugin. Use the language strings to make your plugin translatable.

    public function get_settings(MoodleQuickForm $mform) {
       global $CFG, $COURSE;                                                                                                       
                                                                                                                                    
        $defaultmaxfilesubmissions = $this->get_config('maxfilesubmissions');                                                       
        $defaultmaxsubmissionsizebytes = $this->get_config('maxsubmissionsizebytes');                                               
                                                                                                                                    
        $settings = array();                                                                                                        
        $options = array();                                                                                                         
        for ($i = 1; $i <= ASSIGNSUBMISSION_FILE_MAXFILES; $i++) {                                                                  
            $options[$i] = $i;                                                                                                      
        }                                                                                                                           
                                                                                                                                    
        $name = get_string('maxfilessubmission', 'assignsubmission_file');                                                          
        $mform->addElement('select', 'assignsubmission_file_maxfiles', $name, $options);                                            
        $mform->addHelpButton('assignsubmission_file_maxfiles',                                                                     
                              'maxfilessubmission',                                                                                 
                              'assignsubmission_file');                                                                             
        $mform->setDefault('assignsubmission_file_maxfiles', $defaultmaxfilesubmissions);                                           
        $mform->disabledIf('assignsubmission_file_maxfiles', 'assignsubmission_file_enabled', 'notchecked');                        
                                                                                                                                    
        $choices = get_max_upload_sizes($CFG->maxbytes,                                                                             
                                        $COURSE->maxbytes,                                                                          
                                        get_config('assignsubmission_file', 'maxbytes'));                                           
                                                                                                                                    
        $settings[] = array('type' => 'select',                                                                                     
                            'name' => 'maxsubmissionsizebytes',                                                                     
                            'description' => get_string('maximumsubmissionsize', 'assignsubmission_file'),                          
                            'options'=> $choices,                                                                                   
                            'default'=> $defaultmaxsubmissionsizebytes);                                                            
                                                                                                                                    
        $name = get_string('maximumsubmissionsize', 'assignsubmission_file');                                                       
        $mform->addElement('select', 'assignsubmission_file_maxsizebytes', $name, $choices);                                        
        $mform->addHelpButton('assignsubmission_file_maxsizebytes',                                                                 
                              'maximumsubmissionsize',                                                                              
                              'assignsubmission_file');                                                                             
        $mform->setDefault('assignsubmission_file_maxsizebytes', $defaultmaxsubmissionsizebytes);                                   
        $mform->disabledIf('assignsubmission_file_maxsizebytes',                                                                    
                           'assignsubmission_file_enabled',                                                                         
                           'notchecked');                             
    }

The "get_settings" function is called when building the settings page for the assignment. It allows this plugin to add a list of settings to the form. Notice that the settings are prefixed by the plugin name which is good practice to avoid conflicts with other plugins.

    public function save_settings(stdClass $data) {                                                                                 
        $this->set_config('maxfilesubmissions', $data->assignsubmission_file_maxfiles);                                             
        $this->set_config('maxsubmissionsizebytes', $data->assignsubmission_file_maxsizebytes);                                     
        return true;                                                                                                                
    }   

The "save_settings" function is called when the assignment settings page is submitted, either for a new assignment or when editing an existing one. For settings specific to a single instance of the assignment you can use the assign_plugin::set_config function shown here to save key/value pairs against this assignment instance for this plugin.

    public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {                                        
                                                                                                                                    
        if ($this->get_config('maxfilesubmissions') <= 0) {                                                                         
            return false;                                                                                                           
        }                                                                                                                           
                                                                                                                                    
        $fileoptions = $this->get_file_options();                                                                                   
        $submissionid = $submission ? $submission->id : 0;                                                                          
                                                                                                                                    
        $data = file_prepare_standard_filemanager($data,                                                                            
                                                  'files',                                                                          
                                                  $fileoptions,                                                                     
                                                  $this->assignment->get_context(),                                                 
                                                  'assignsubmission_file',                                                          
                                                  ASSIGNSUBMISSION_FILE_FILEAREA,                                                   
                                                  $submissionid);                                                                   
        $mform->addElement('filemanager', 'files_filemanager', html_writer::tag('span', $this->get_name(),                          
            array('class' => 'accesshide')), null, $fileoptions);                                                                   
                                                                                                                                    
        return true;                                                                                                                
    }                                    

The get_form_elements function is called when building the submission form. It functions identically to the get_settings function except that the submission object is available (if there is a submission) to associate the settings with a single submission. This example also shows how to use a filemanager within a submission plugin. The function must return true if it has modified the form otherwise the assignment will not include a header for this plugin.

   public function save(stdClass $submission, stdClass $data) {                                                                    
        global $USER, $DB;                                                                                                          
                                                                                                                                    
        $fileoptions = $this->get_file_options();                                                                                   
                                                                                                                                    
        $data = file_postupdate_standard_filemanager($data,                                                                         
                                                     'files',                                                                       
                                                     $fileoptions,                                                                  
                                                     $this->assignment->get_context(),                                              
                                                     'assignsubmission_file',                                                       
                                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                
                                                     $submission->id);                                                              
                                                                                                                                    
        $filesubmission = $this->get_file_submission($submission->id);                                                              
                                                                                                                                    
        // Plagiarism code event trigger when files are uploaded.                                                                   
                                                                                                                                    
        $fs = get_file_storage();                                                                                                   
        $files = $fs->get_area_files($this->assignment->get_context()->id,                                                          
                                     'assignsubmission_file',                                                                       
                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                                
                                     $submission->id,                                                                               
                                     'id',                                                                                          
                                     false);                                                                                        
                                                                                                                                    
        $count = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);                                               
                                                                                                                                    
        // Send files to event system.                                                                                              
        // This lets Moodle know that an assessable file was uploaded (eg for plagiarism detection).                                
        $eventdata = new stdClass();                                                                                                
        $eventdata->modulename = 'assign';                                                                                          
        $eventdata->cmid = $this->assignment->get_course_module()->id;                                                              
        $eventdata->itemid = $submission->id;                                                                                       
        $eventdata->courseid = $this->assignment->get_course()->id;                                                                 
        $eventdata->userid = $USER->id;                                                                                             
        if ($count > 1) {                                                                                                           
            $eventdata->files = $files;                                                                                             
        }                                                                                                                           
        $eventdata->file = $files;                                    
        $eventdata->pathnamehashes = array_keys($files);                                                                            
        events_trigger('assessable_file_uploaded', $eventdata);                                                                     
                                                                                                                                    
        if ($filesubmission) {                                                                                                      
            $filesubmission->numfiles = $this->count_files($submission->id,                                                         
                                                           ASSIGNSUBMISSION_FILE_FILEAREA);                                         
            return $DB->update_record('assignsubmission_file', $filesubmission);                                                    
        } else {                                                                                                                    
            $filesubmission = new stdClass();                                                                                       
            $filesubmission->numfiles = $this->count_files($submission->id,                                                         
                                                           ASSIGNSUBMISSION_FILE_FILEAREA);                                         
            $filesubmission->submission = $submission->id;                                                                          
            $filesubmission->assignment = $this->assignment->get_instance()->id;                                                    
            return $DB->insert_record('assignsubmission_file', $filesubmission) > 0;                                                
        }                                

The "save" function is called to save a user submission. The parameters are the submission object and the data from the submission form. This example calls file_postupdate_standard_filemanager to copy the files from the draft file area to the filearea for this submission, it then uses the event api to trigger an assessable_file_uploaded event for the plagiarism api. It then records the number of files in the plugin specific "assignsubmission_file" table.

    public function get_files($submission) {
        $result = array();                                                                                                          
        $fs = get_file_storage();                                                                                                   
                                                                                                                                    
        $files = $fs->get_area_files($this->assignment->get_context()->id,                                                          
                                     'assignsubmission_file',                                                                       
                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                                
                                     $submission->id,                                                                               
                                     'timemodified',                                                                                
                                     false);                                                                                        
                                                                                                                                    
        foreach ($files as $file) {                                                                                                 
            $result[$file->get_filename()] = $file;                                                                                 
        }                                                                                                                           
        return $result;
    }

If this submission plugin produces one or more files, it should implement "get_files" so that the portfolio API can export a list of all the files from all of the plugins for this assignment submission. This is also used by the offline grading feature in the assignment.

    public function view_summary(stdClass $submission, & $showviewlink) {                                                           
        $count = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);                                               
                                                                                                                                    
        // Show we show a link to view all files for this plugin?                                                                   
        $showviewlink = $count > ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES;                                                             
        if ($count <= ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES) {                                                                      
            return $this->assignment->render_area_files('assignsubmission_file',                                                    
                                                        ASSIGNSUBMISSION_FILE_FILEAREA,                                             
                                                        $submission->id);                                                           
        } else {                                                                                                                    
            return get_string('countfiles', 'assignsubmission_file', $count);                                                       
        }                                                                                                                           
    }

The view_summary function is called to display a summary of the submission to both markers and students. It counts the number of files submitted and if it is more that a set number, it only displays a count of how many files are in the submission - otherwise it uses a helper function to write the entire list of files. This is because we want to keep the summaries really short so they can be displayed in a table. There will be a link to view the full submission on the submission status page.

    public function view($submission) {
        return $this->assignment->render_area_files('assignsubmission_file',                                                        
                                                    ASSIGNSUBMISSION_FILE_FILEAREA,                                                 
                                                    $submission->id);
    }

The view function is called to display the entire submission to both markers and students. In this case it uses the helper function in the assignment class to write the list of files.

    public function can_upgrade($type, $version) {

        $uploadsingle_type ='uploadsingle';
        $upload_type ='upload';

        if (($type == $uploadsingle_type || $type == $upload_type) && $version >= 2011112900) {
            return true;
        }
        return false;
    }

The can_upgrade function is used to identify old "Assignment 2.2" subtypes that can be upgraded by this plugin. This plugin supports upgrades from the old "upload" and "uploadsingle" assignment subtypes.

    
    public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {                                        
        global $DB;                                                                                                                 
                                                                                                                                    
        if ($oldassignment->assignmenttype == 'uploadsingle') {                                                                     
            $this->set_config('maxfilesubmissions', 1);                                                                             
            $this->set_config('maxsubmissionsizebytes', $oldassignment->maxbytes);                                                  
            return true;                                                                                                            
        } else if ($oldassignment->assignmenttype == 'upload') {                                                                    
            $this->set_config('maxfilesubmissions', $oldassignment->var1);                                                          
            $this->set_config('maxsubmissionsizebytes', $oldassignment->maxbytes);                                                  
                                                                                                                                    
            // Advanced file upload uses a different setting to do the same thing.                                                  
            $DB->set_field('assign',                                                                                                
                           'submissiondrafts',                                                                                      
                           $oldassignment->var4,                                                                                    
                           array('id'=>$this->assignment->get_instance()->id));                                                     
                                                                                                                                    
            // Convert advanced file upload "hide description before due date" setting.                                             
            $alwaysshow = 0;                                                                                                        
            if (!$oldassignment->var3) {                                                                                            
                $alwaysshow = 1;                                                                                                    
            }                                                                                                                       
            $DB->set_field('assign',                                                                                                
                           'alwaysshowdescription',                                                                                 
                           $alwaysshow,                                                                                             
                           array('id'=>$this->assignment->get_instance()->id));                                                     
            return true;                                                                                                            
        }                                                                                                                           
    }                     

This function is called once per assignment instance to upgrade the settings from the old assignment to the new mod_assign. In this case it sets the maxbytes, maxfiles and alwaysshowdescription configuration settings.


    public function upgrade($oldcontext,$oldassignment, $oldsubmission, $submission, & $log) {
        global $DB;

        $file_submission = new stdClass();



        $file_submission->numfiles = $oldsubmission->numfiles;
        $file_submission->submission = $submission->id;
        $file_submission->assignment = $this->assignment->get_instance()->id;

        if (!$DB->insert_record('assign_submission_file', $file_submission) > 0) {
            $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
            return false;
        }




        // now copy the area files
        $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
                                                        'mod_assignment',
                                                        'submission',
                                                        $oldsubmission->id,
                                                        // New file area
                                                        $this->assignment->get_context()->id,
                                                        'mod_assign',
                                                        ASSIGN_FILEAREA_SUBMISSION_FILES,
                                                        $submission->id);





        return true;
    }

The "upgrade" function upgrades a single submission from the old assignment type to the new one. In this case it involves copying all the files from the old filearea to the new one. There is a helper function available in the assignment class for this (Note: the copy will be fast as it is just adding rows to the files table). If this function returns false, the upgrade will be aborted and rolled back.

                                                             
    public function upgrade(context $oldcontext,                                                                                    
                            stdClass $oldassignment,                                                                                
                            stdClass $oldsubmission,                                                                                
                            stdClass $submission,                                                                                   
                            & $log) {                                                                                               
        global $DB;                                                                                                                 
                                                                                                                                    
        $filesubmission = new stdClass();                                                                                           
                                                                                                                                    
        $filesubmission->numfiles = $oldsubmission->numfiles;                                                                       
        $filesubmission->submission = $submission->id;                                                                              
        $filesubmission->assignment = $this->assignment->get_instance()->id;                                                        
                                                                                                                                    
        if (!$DB->insert_record('assignsubmission_file', $filesubmission) > 0) {                                                    
            $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);                                     
            return false;                                                                                                           
        }                                                                                                                           
                                                                                                                                    
        // Now copy the area files.                                                                                                 
        $this->assignment->copy_area_files_for_upgrade($oldcontext->id,                                                             
                                                        'mod_assignment',                                                           
                                                        'submission',                                                               
                                                        $oldsubmission->id,                                                         
                                                        $this->assignment->get_context()->id,                                       
                                                        'assignsubmission_file',                                                    
                                                        ASSIGNSUBMISSION_FILE_FILEAREA,                                             
                                                        $submission->id);                                                           
                                                                                                                                    
        return true;                                                                                                                
    }                              

This example is from assignsubmission_onlinetext. If the plugin uses a text-editor it is ideal if the plugin implements "get_editor_fields". This allows the portfolio to retrieve the text from the plugin when exporting the list of files for a submission. This is required because the text is stored in the plugin specific table that is only known to the plugin itself. If a plugin supports multiple text areas it can return the name of each of them here.

    public function get_editor_fields() {                                                                                           
        return array('onlinetext' => get_string('pluginname', 'assignsubmission_comments'));                                        
    }       

This example is from assignsubmission_onlinetext. If the plugin uses a text-editor it is ideal if the plugin implements "get_editor_text". This allows the portfolio to retrieve the text from the plugin when exporting the list of files for a submission. This is required because the text is stored in the plugin specific table that is only known to the plugin itself. The name is used to distinguish between multiple text areas in the one plugin.

    public function get_editor_text($name, $submissionid) {                                                                         
        if ($name == 'onlinetext') {                                                                                                
            $onlinetextsubmission = $this->get_onlinetext_submission($submissionid);                                                
            if ($onlinetextsubmission) {                                                                                            
                return $onlinetextsubmission->onlinetext;                                                                           
            }                                                                                                                       
        }                                                                                                                           
                                                                                                                                    
        return '';                                                                                                                  
    }             

This example is from assignsubmission_onlinetext. For the same reason as the previous function, if the plugin uses a text editor, it is ideal if the plugin implements "get_editor_format". This allows the portfolio to retrieve the text from the plugin when exporting the list of files for a submission. This is required because the text is stored in the plugin specific table that is only known to the plugin itself. The name is used to distinguish between multiple text areas in the one plugin.

    public function get_editor_format($name, $submissionid) {
        if ($name == 'onlinetext') {
            $onlinetext_submission = $this->get_onlinetext_submission($submissionid);
            if ($onlinetext_submission) {
                return $onlinetext_submission->onlineformat;
            }
        }

        return 0;
    }

If a plugin has no submission data to show - it can return true from the is_empty function. This prevents a table row being added to the submission summary for this plugin. It is also used to check if a student has tried to save an assignment with no data.

    public function is_empty(stdClass $submission) {                                                                                
        return $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA) == 0;                                            
    }   

A plugin should implement get_file_areas if it supports saving of any files to moodle - this allows the file areas to be browsed by the moodle file manager.

   public function get_file_areas() {                                                                                              
        return array(ASSIGNSUBMISSION_FILE_FILEAREA=>$this->get_name());                                                            
    }    

Since Moodle 2.5 - a students submission can be copied to create a new submission attempt. Plugins should implement this function if they store data associated with the submission (most plugins).

    public function copy_submission(stdClass $sourcesubmission, stdClass $destsubmission) {                                         
        global $DB;                                                                                                                 
                                                                                                                                    
        // Copy the files across.                                                                                                   
        $contextid = $this->assignment->get_context()->id;                                                                          
        $fs = get_file_storage();                                                                                                   
        $files = $fs->get_area_files($contextid,                                                                                    
                                     'assignsubmission_file',                                                                       
                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                                
                                     $sourcesubmission->id,                                                                         
                                     'id',                                                                                          
                                     false);                                                                                        
        foreach ($files as $file) {                                                                                                 
            $fieldupdates = array('itemid' => $destsubmission->id);                                                                 
            $fs->create_file_from_storedfile($fieldupdates, $file);                                                                 
        }                                                                                                                           
                                                                                                                                    
        // Copy the assignsubmission_file record.                                                                                   
        if ($filesubmission = $this->get_file_submission($sourcesubmission->id)) {                                                  
            unset($filesubmission->id);                                                                                             
            $filesubmission->submission = $destsubmission->id;                                                                      
            $DB->insert_record('assignsubmission_file', $filesubmission);                                                           
        }                                                                                                                           
        return true;                                                                                                                
    }                                 

The format_for_log function lets a plugin produce a really short summary of a submission suitable for adding to a log message.

   public function format_for_log(stdClass $submission) {                                                                          
        // format the info for each submission plugin add_to_log                                                                    
        $filecount = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);                                           
        $fileloginfo = '';                                                                                                          
        $fileloginfo .= ' the number of file(s) : ' . $filecount . " file(s).<br>";                                                 
                                                                                                                                    
        return $fileloginfo;                                                                                                        
    }                      

The delete_instance function is called when a plugin is deleted. Note only database records need to be cleaned up - files belonging to fileareas for this assignment will be automatically cleaned up.

    public function delete_instance() {                                                                                             
        global $DB;                                                                                                                 
        // will throw exception on failure                                                                                          
        $DB->delete_records('assignsubmission_file', array('assignment'=>$this->assignment->get_instance()->id));                   
                                                                                                                                    
        return true;                                                                                                                
    }   

lib.php