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
(Created page with "== 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 ass...")
(No difference)

Revision as of 05:43, 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.

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 [/dev/version.php] for more information.

$plugin->version   = 2012011600;
$plugin->requires  = 2011110200;
$plugin->component = 'submission_file';

settings.php

The settings file allows us to add custom settings to the system wide configuration page for our plugin. The file plugin 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 ("submission_file") in this case. The strings are specified in this plugins language file.

if (isset($CFG->maxbytes)) {
    $settings->add(new admin_setting_configselect('submission_file_maxbytes', 
                        get_string('maximumsubmissionsize', 'submission_file'),
                        get_string('configmaxbytes', 'submission_file'), 1048576,       
                        get_max_upload_sizes($CFG->maxbytes)));
}

lang/en/submission_file.php

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

$string['allowfilesubmissions'] = 'Enabled';
$string['configmaxbytes'] = 'Maximum file size';
$string['file'] = 'File Submissions';
$string['maxbytes'] = 'Maximum file size';
$string['maxfilessubmission'] = 'Maximum number of uploaded files';
$string['maximumsubmissionsize'] = 'Maximum submission size';
$string['pluginname'] = 'File Submissions';
$string['submissionfilearea'] = 'Uploaded submission files';

db/access.php

This is where any additional capabilities are defined (None for this plugin).

$capabilities = array(

);

db/upgrade.php

This is where any upgrade code is defined (None for this plugin).

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

    $dbman = $DB->get_manager();

    // do the upgrades
    return true;
}

db/install.xml

This is where any database tables required to save this plugins data are defined. In this case we 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="20090420" 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="assign_submission_file" COMMENT="Info about submitted assignments">
      <FIELDS>
        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="assignment"/>
        <FIELD NAME="assignment" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="submission"/>
        <FIELD NAME="submission" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="assignment" NEXT="numfiles"/>
        <FIELD NAME="numfiles" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="submission" />
      </FIELDS>
      <KEYS>
        <KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="assignment"/>
        <KEY NAME="assignment" TYPE="foreign" FIELDS="assignment" REFTABLE="assign" REFFIELDS="id" PREVIOUS="primary" NEXT="submission"/>
        <KEY NAME="submission" TYPE="foreign" FIELDS="submission" REFTABLE="assign_submission" REFFIELDS="id" PREVIOUS="assignment"/>
      </KEYS>
    </TABLE>
  </TABLES>
</XMLDB>

db/install.php

This example is actually from the submission_comments plugin - not the submission_file plugin - but 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');
    }
        
    // do the upgrades
    return true;
}

lib.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 submission_file extends submission_plugin {

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


    public function get_name() {
        return get_string('file', 'submission_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(&$mform) {
        global $CFG, $COURSE, $DB;

        $default_maxfilesubmissions = $this->get_config('maxfilesubmissions');
        $default_maxsubmissionsizebytes = $this->get_config('maxsubmissionsizebytes');

        $settings = array();
        $options = array();
        for($i = 1; $i <= ASSIGN_MAX_SUBMISSION_FILES; $i++) {
            $options[$i] = $i;
        }

        $mform->addElement('select', 'submission_file_maxfiles', get_string('maxfilessubmission', 'submission_file'), $options);
        $mform->setDefault('submission_file_maxfiles', $default_maxfilesubmissions);

        $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
        $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
        $settings[] = array('type' => 'select',
                            'name' => 'maxsubmissionsizebytes',
                            'description' => get_string('maximumsubmissionsize', 'submission_file'),
                            'options'=>$choices,
                            'default'=>$default_maxsubmissionsizebytes);

        $mform->addElement('select', 'submission_file_maxsizebytes', get_string('maximumsubmissionsize', 'submission_file'), $choices);
        $mform->setDefault('submission_file_maxsizebytes', $default_maxsubmissionsizebytes);


    }

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($mform) {
        $this->set_config('maxfilesubmissions', $mform->submission_file_maxfiles);
        $this->set_config('maxsubmissionsizebytes', $mform->submission_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 assignment_plugin::set_config function shown here to save key/value pairs against this assignment instance for this plugin.

    public function get_form_elements($submission, & $mform, & $data) {

        $elements = array();

        if ($this->get_config('maxfilesubmissions') <= 0) {
            return $elements;
        }

        $fileoptions = $this->get_file_options();
        $submissionid = $submission ? $submission->id : 0;


        $data = file_prepare_standard_filemanager($data, 'files', $fileoptions, $this->assignment->get_context(), 'mod_assign', ASSIGN_FILEAREA_SUBMISSION_FILES, $submissionid);
        $mform->addElement('filemanager', 'files_filemanager', '', 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 function 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($submission, $data) {

        global $USER, $DB;

        $fileoptions = $this->get_file_options();


        $data = file_postupdate_standard_filemanager($data, 'files', $fileoptions, $this->assignment->get_context(), 'mod_assign', ASSIGN_FILEAREA_SUBMISSION_FILES, $submission->id);


        $file_submission = $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, 'mod_assign', ASSIGN_FILEAREA_SUBMISSION_FILES, $submission->id, "id", false);

        // send files to event system
        // Let 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 ($files) {
            $eventdata->files = $files;
        }
        events_trigger('assessable_file_uploaded', $eventdata);


        if ($file_submission) {
            $file_submission->numfiles = $this->count_files($submission->id);
            return $DB->update_record('assign_submission_file', $file_submission);
        } else {
            $file_submission = new stdClass();
            $file_submission->numfiles = $this->count_files($submission->id);
            $file_submission->submission = $submission->id;
            $file_submission->assignment = $this->assignment->get_instance()->id;
            return $DB->insert_record('assign_submission_file', $file_submission) > 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 "assign_submission_file" table.

    public function get_files($submission) {
        global $DB;
        $result = array();
        $fs = get_file_storage();

        $files = $fs->get_area_files($this->assignment->get_context()->id, 'mod_assign', ASSIGN_FILEAREA_SUBMISSION_FILES, $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.

    public function view_summary($submission) {
        $count = $this->count_files($submission->id);
        if ($count <= ASSIGN_SUBMISSION_FILE_MAX_SUMMARY_FILES) {
            return $this->assignment->render_area_files(ASSIGN_FILEAREA_SUBMISSION_FILES, $submission->id);
        } else {
            return get_string('countfiles', 'submission_file', $count);
        }
    }

The view_summary function is called to display a summary of the submission to both markers and students. It counts the numbre of files submitted and if it is more that a set number (5) it only displays a count of how many files have been updated - 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(ASSIGN_FILEAREA_SUBMISSION_FILES, $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 show_view_link($submission) {
        $count = $this->count_files($submission->id);
        return $count > ASSIGN_SUBMISSION_FILE_MAX_SUMMARY_FILES;
    }

The show_view_link function allows the plugin to control whether the assignment module renders a link to show the full submission or not. This is because some submission types can display the entire submission in the summary and some cannot.

    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 subtypes that can be upgraded by this plugin. This plugin supports upgrades from the old "upload" and "uploadsingle" assignment subtypes.

    
    public function upgrade_settings($oldassignment, & $log) {
        // TODO: get the maxbytes and maxfiles settings from the old assignment and set them for this plugin
        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 should be setting the maxbytes and maxfiles configuration settings (but it isn't yet!).


    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).

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

        return '';
    }

This example is from submission_onlinetext - not submission_file. 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.

    /**
     * get the content format for the editor 
     * @param string $name
     * @param int $submissionid
     * @return bool
     */
    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;
    }

This example is from submission_onlinetext - not submission_file. 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.