浏览代码

Submission forms registering comments to feedback table.

Bernardo 2 年之前
父节点
当前提交
b280f797c2
共有 7 个文件被更改,包括 527 次插入8 次删除
  1. 60 0
      forms/forms.md
  2. 77 0
      forms/submission_form.php
  3. 137 0
      forms/submission_form_functions.php
  4. 139 0
      forms/submissiontype.php
  5. 59 0
      forms/submissiontype_form.php
  6. 26 0
      lang/en/forms_lang.php
  7. 29 8
      submission.php

+ 60 - 0
forms/forms.md

@@ -0,0 +1,60 @@
+# Describes forms and their fields
+
+## [mod_form](../mod_form.php)
+
+- Creates and modifies instances of gradeimporter module
+
+### General Information
+
+#### Header
+
+- Adds General Information section header
+- Element Header - General
+
+#### Name
+
+- Mod instance Name
+- Element - Text
+- Type - Text
+- Rule - Must be String
+- Parameter - name
+
+<!-- ### Visibility
+
+- Selects if submission will appear to students
+- Element - selectyesno
+  - selectyesno already has type and rules defined
+- Parameter - visibility
+ -->
+### Standard elements
+
+- Receives a features array to define what will show on the form
+  - Disponibility, access restrictions, tags, competencies, etc...
+
+### Hidden Fields
+
+- Used to pass predefined parameters to the database
+  - user id, who and when instance was created or modified
+
+## [submission_form](submission_form.php)
+
+- Creates and modifies submissions that will appear (or not) to the students
+
+### General Information - sub
+
+#### Header - sub
+
+- Adds General Information section header
+- Element Header - General
+
+#### Submission Name
+
+- Mod instance Name
+- Element - Text
+- Type - Text
+- Rule - Must be String
+- Parameter - name
+
+#### Submission Description
+
+#### Submission type

+ 77 - 0
forms/submission_form.php

@@ -0,0 +1,77 @@
+<?php
+// This file is part of
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+defined('MOODLE_INTERNAL') || die();
+
+require_once($CFG->dirroot.'/course/moodleform_mod.php');
+require_once("$CFG->libdir/formslib.php");
+
+class mod_gradeimporter_submission_form extends moodleform {
+
+    public function definition() {
+        global $CFG, $DB;
+
+        $mform = $this->_form;
+
+        // Custom data
+        $submission = $this->_customdata['submission']; // Load fields data
+        $filemanageroptions = $this->_customdata['filemanageroptions'];
+        // ----------------------------------------------------------------
+        // General information
+
+        // Header
+        $mform->addElement('header',  'newsub',  get_string('newsubmission', 'gradeimporter'));
+
+        // Submission name field
+        $mform->addElement('text', 'name', get_string('submissionname', 'gradeimporter'));
+        $mform->setType('name', PARAM_TEXT);
+        $mform->addRule('name', null, 'required', null, 'client');
+
+        $mform->addElement('editor', 'description', get_string('description'));
+        $mform->setType('description', PARAM_TEXT);
+
+        $mform->addElement('filemanager', 'submissionfiles_filemanager', get_string('submissionfiles', 'gradeimporter'),
+                            null, $filemanageroptions);
+        $mform->addHelpButton('submissionfiles_filemanager', 'submissionfiles', 'gradeimporter');
+
+        $selectvalues = array('red', 'green', 'blue');
+        $select = $mform->addElement('select', 'type', get_string('submissiontype', 'gradeimporter'), $selectvalues);
+
+        $mform->addElement('selectyesno', 'visibility',  get_string('visibility', 'gradeimporter'));
+        $mform->setDefault('visibility', array('value' => 1));
+        $mform->addHelpButton('visibility', 'visibility', 'gradeimporter');
+
+        $mform->addElement('selectyesno', 'gradebook',  get_string('gradebook', 'gradeimporter'));
+        $mform->setDefault('gradebook', array('value' => 0));
+        $mform->addHelpButton('gradebook', 'gradebook', 'gradeimporter');
+
+        // -----------------------------
+        // Hidden fields
+        $mform->addElement('hidden', 'id');
+        $mform->setType('id', PARAM_INT);
+        $mform->addElement('hidden', 'cmid');
+        $mform->setType('cmid', PARAM_INT);
+
+        // ------------------------------
+        // Save and cancel buttons
+        $this->add_action_buttons();
+
+        // ------------------------------
+        // Sets preloaded data to fields
+        $this->set_data($submission);
+    }
+
+}

+ 137 - 0
forms/submission_form_functions.php

@@ -0,0 +1,137 @@
+<?php
+// This file is part of
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+function create_submission($data, $gradeimporterid, $userid) {
+    global $DB;
+    $timenow = time();
+    if (empty($data->id)) {
+        // If is new db entry;
+        $data->gradeimporterid = $gradeimporterid;
+        $data->usermodified = $userid;
+        $data->timecreated = $timenow;
+
+        $isnewentry = true;
+    } else {
+        $isnewentry = false;
+    }
+
+    $data->descriptionformat = $data->description["format"];
+    $data->description = $data->description["text"];
+    $data->timemodified = $timenow;
+    $data->position = -1; // Do later
+
+    if ($isnewentry) {
+        // If is new entry insert data into DB and gets id
+        $data->id = $DB->insert_record('gradeimporter_submission', $data);
+    }
+
+    // If not new entry updates information
+    // If new entry inserts id into DB
+    $DB->update_record('gradeimporter_submission', $data);
+
+    return $data;
+}
+
+
+
+function store_files($context, $cm, $data) {
+    $fs = get_file_storage();
+    $files = $fs->get_area_files($context->id, 'mod_gradeimporter',
+                                'submissionfiles', $data->id
+                            );
+    foreach ($files as $file) {
+        $extension = explode(".", $file->get_filename())[1];
+        if ($extension == 'csv') {
+            read_csv($file->get_content(), $context, $data);
+            return;
+        }
+    }
+}
+
+function read_csv($content, $context, $data) {
+    $csv = prepare_csv($content);
+    foreach ($csv as $feedback) {
+        if ($feedback['file'] != "") {
+            // If feedback has associated file, insert it into pluginfile
+            $feedbackfilepath = store_feedback_file($feedback, $context, $data, $filepath);
+        }
+        echo "Before store_feedback</br>";
+        store_feedback($feedback, $context->id, $data->id);
+        echo "After store_feedback</br>";
+    }
+}
+
+function prepare_csv($content) {
+    $csvlines = explode(PHP_EOL, $content);
+    $csv = array();
+    foreach ($csvlines as $line) {
+        $csv[] = str_getcsv($line);
+    }
+    $header = array_shift($csv);
+
+    $outputcsv = array();
+    $outputcsv = array_map(
+                            function($v)use($header){
+                                return array_combine($header, $v);
+                            },
+                            $csv
+                        );
+
+    return $outputcsv;
+}
+
+function store_feedback($feedback, $contextid, $submissionid) {
+    global $DB;
+    // Prepare data to submit to gradeimporter_feedback table
+    $entry = new stdClass();
+    $entry->id = null;
+
+    $entry->timecreated = time();
+    $entry->timemodified = time();
+
+    $entry->submissionid = $submissionid;
+    $entry->studentid = $feedback['id'];
+    $entry->grade = $feedback['grade'];
+    $entry->comment = $feedback['comment'];
+    $entry->name = $feedback['file'];
+    $entry->fileid = 0;
+    $entry->usermodified = 1;
+    $entry->contextid = $contextid;
+
+    // Insert data into gradeimporter_feedback table and gets ID
+    $entry->id = $DB->insert_record('gradeimporter_feedback', $entry);
+
+}
+
+function store_feedback_file($feedback, $context, $data, $filepath) {
+    // Prepare file
+    $fs = get_file_storage();
+    $fileinfo = array(
+        'contextid' => $context->id,
+        'component' => 'mod_gradeimporter',
+        'filearea' => 'submissionfiles',
+        'itemid' => 0,
+        'filepath' => "/$data->id/{$feedback[id]}/",
+        'filename' => $feedback['file'],
+        'timecreated' => time(),
+        'timemodified' => time()
+    );
+
+    // Move file from temp to pluginfile
+    $fs->create_file_from_pathname($fileinfo, $filepath);
+
+    return $fileinfo['filepath'];
+}

+ 139 - 0
forms/submissiontype.php

@@ -0,0 +1,139 @@
+<?php
+// This file is part of
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+require_once('../../config.php');
+require_once('locallib.php');
+require_once('lib.php');
+require_once('submissiontype_form.php');
+
+
+$cmid = required_param('cmid', PARAM_INT);            // Course Module ID
+$id   = required_param('id' , PARAM_INT);           // Gradeimporter id
+
+if (!$cm = get_coursemodule_from_id('gradeimporter', $cmid)) {
+    throw new moodle_exception('invalidcoursemodule');
+}
+
+if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
+    throw new moodle_exception('coursemisconf');
+}
+
+$context = context_module::instance($cm->id);
+
+require_capability('mod/gradeimporter:edit', $context);
+
+if (!$gradeimporter = $DB->get_record('gradeimporter', array('id' => $cm->instance))) {
+    throw new moodle_exception(get_string('invalidid', 'gradeimporter'));
+}
+
+$url = new moodle_url('/mod/gradeimporter/submissiontype.php', array('cmid' => $cm->id));
+if (!empty($id)) {
+    $url->param('id', $id);
+}
+$PAGE->set_url($url);
+
+require_login($course, false, $cm);
+
+
+if ($submissiontype) {
+    if (isguestuser()) {
+        throw new moodle_exception('guestnoedit', 'gradeimporter', "$CFG->wwwroot/mod/gradeimporter/view.php?id=$cmid");
+    }
+} else {
+
+    $submissiontype = new stdClass();
+    $submissiontype->id = null;
+}
+
+$maxfiles = 50;
+$maxbytes = $course->maxbytes;
+
+$descriptionoptions = array('trusttext' => true, 'maxfiles' => $maxfiles,
+                    'maxbytes' => $maxbytes, 'context' => $context,
+                    'subdirs' => file_area_contains_subdirs($context, 'mod_gradeimporter',
+                                                        'submissiontype', $submissiontype->id));
+
+$submissiontypeoptions = array('subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
+
+$submissiontype = file_prepare_standard_editor($submissiontype, 'description', $descriptionoptions,
+                                            $context, 'mod_gradeimporter', 'description', $submissiontype->id);
+
+$submissiontype->cmid = $cm->id;
+
+$mform = new mod_gradeimporter_submissiontype_form(null, array('submissiontype' => $submissiontype,
+                                                                'cm' => $cm,
+                                                                'descriptionoptions' => $descriptionoptions,
+                                                                'submissiontypeoptions' => $submissiontypeoptions
+                                                            )
+                                                    );
+
+if ($mform->is_cancelled()) {
+    if ($id) {
+        redirect("view.php?id=$cm->id&mode=entry&hook=$id");
+    } else {
+        redirect("view.php?id=$cm->id");
+    }
+} else if ($submissiontype = $mform->get_data()) {
+    if (empty($submissiontype->id)) {
+        $submissiontype->gradeimporterid    = $gradeimporter->id;
+
+        $isnewentry                         = true;
+    } else {
+        $isnewentry                         = false;
+    }
+
+    $submissiontype->description        = '';
+    $submissiontype->descriptionformat  = FORMAT_HTML;
+    $submissiontype->definitiontrust    = 0;
+
+    if ($isnewentry) {
+        $submissiontype->id = $DB->insert_record('gradeimporter_submissiontype', $submissiontype);
+    } else {
+        $DB->update_record('gradeimporter', $submissiontype);
+    }
+
+    $submissiontype = file_postupdate_standard_editor($submissiontype, 'description',
+                                                    $descriptionoptions, $context, 'mod_gradeimporter',
+                                                    'submissiontype', $submissiontype->id);
+
+    $DB->update_record('gradeimporter_submissiontype', $submissiontype);
+
+    if ($isnewentry) {
+        // Update completion state
+        $completion = new completion_info($course);
+        if ($completion->is_enabled($cm) == COMPLETION_TRACKING_AUTOMATIC && $gradeimporter->completionentries) {
+            $completion->update_state($cm, COMPLETION_COMPLETE);
+        }
+    }
+
+    redirect("submission.php?id=$id&cmid=$cm->id&page=0");
+}
+
+if (!empty($id)) {
+    $PAGE->navbar->add(get_string('edit'));
+}
+
+$PAGE->set_title($gradeimporter->name);
+$PAGE->set_heading($course->fullname);
+echo $OUTPUT->header();
+echo $OUTPUT->heading(format_string($gradeimporter->name), 2);
+if ($gradeimporter->intro) {
+    echo $OUTPUT->box(format_module_intro('gradeimporter', $gradeimporter, $cm->id), 'generalbox', 'intro');
+}
+
+$mform->display();
+
+echo $OUTPUT->footer();

+ 59 - 0
forms/submissiontype_form.php

@@ -0,0 +1,59 @@
+<?php
+// This file is part of
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+defined('MOODLE_INTERNAL') or die();
+require_once($CFG->dirroot.'/lib/formslib.php');
+
+class mod_gradeimporter_submissiontype_form extends moodleform {
+
+    public function definition() {
+        global $CFG, $DB;
+
+        $mform =& $this->_form;
+
+        $submissiontype = $this->_customdata['submissiontype'];
+        $cm             = $this->_customdata['cm'];
+        $descriptionoptions = $this->_customdata['descriptionoptions'];
+        $submissiontypeoptions  = $this->_customdata['submissiontypeoptions'];
+
+        $context    = context_module::instance($cm->id);
+        $fmtoptions = array('context' => $context);
+
+        // --------------------
+        // Form starts here
+        $mform->addElement('header', 'general', get_string('general', 'form'));
+
+        // Submission type name
+        $mform->addElement('text', 'name', get_string('name', 'gradeimporter'), array('size' => '64'));
+        $mform->setType('name', PARAM_TEXT);
+        $mform->addRule('name', get_string('error_nameField', 'gradeimporter'), 'required', null, 'client');
+
+        // Description
+        $mform->addElement('editor', 'description_editor', get_string('submissionDescription', 'gradeimporter'), null, $descriptionoptions);
+        $mform->setType('description_editor', PARAM_RAW);
+
+        // Hidden fields
+        $mform->addElement('hidden', 'id');
+        $mform->setType('id', PARAM_INT);
+        $mform->addElement('hidden', 'cmid');
+        $mform->setType('cmid', PARAM_INT);
+
+        // Buttons
+        $this->add_action_buttons();
+
+        $this->set_data($submissiontype);
+    }
+}

+ 26 - 0
lang/en/forms_lang.php

@@ -0,0 +1,26 @@
+<?php
+// This file is part of
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+
+// Submission form
+$string['newsubmission'] = 'New Submission';
+$string['submissionname'] = 'Submission name';
+$string['visibility_help'] = 'Not working at the moment, still being built';
+$string['submissiontype'] = 'Submission type';
+$string['gradebook'] = 'Gradebook';
+$string['gradebook_help'] = 'Submission goes to gradebook?';
+$string['submissionfiles'] = 'Submission files';
+$string['submissionfiles_help'] = 'Submit 1 file with all submissions inside it, either a .csv or a .zip with a csv and a folder with the submissions inside it';

+ 29 - 8
submission.php

@@ -49,18 +49,30 @@ require_capability('mod/gradeimporter:edit', $context);
 
 // Prepare data for submission form
 
-if (!isset($submission)) {
+if (!isset($entry)) {
     // If its creating new submission
-    $submission = new stdClass();
-    $submission->id = null;
+    $entry = new stdClass();
+    $entry->id = null;
 }
 
 // Set parameters to sendo to the form
-$submission->cmid = $cm->id;
+$entry->cmid = $cm->id;
 $maxbytes = $course->maxbytes;
 
-$mform = new mod_gradeimporter_submission_form(null, array('submission' => $submission,
-                                                            'maxbytes' => $maxbytes
+// Prepare file manager
+$filemanageroptions = array('subdirs' => 0,
+                                    'maxbytes' => $maxbytes,
+                                    'areamaxbytes' => 10485760,
+                                    'maxfiles' => 1,
+                                    'accepted_types' => array('.csv', '.zip')
+                                );
+
+$entry = file_prepare_standard_filemanager($entry, 'submissionfiles', $filemanageroptions, $context,
+                                                'mod_gradeimporter', 'submissionfiles', $entry->id);
+
+// Create new mform to show to the user
+$mform = new mod_gradeimporter_submission_form(null, array('submission' => $entry,
+                                                            'filemanageroptions' => $filemanageroptions
                                                         )
                                                 );
 
@@ -68,9 +80,18 @@ if ($mform->is_cancelled()) {
     // Handle form cancel operation, if cancel button is present on form
     echo("form is cancelled");
 } else if ($formdata = $mform->get_data()) {
-    create_submission($formdata, $gradeimporter->id, $USER->id);
+
+    $entry = create_submission($formdata, $gradeimporter->id, $USER->id);
+
+    // Gets file manager content
+    $entry = file_postupdate_standard_filemanager($entry, 'submissionfiles',
+                                            $filemanageroptions, $context, 'mod_gradeimporter',
+                                            'submissionfiles', $entry->id);
+
+    store_files($context, $cm, $entry);
     // When complete redirect to view.php
-    redirect("view.php?id=$cm->id&edit=1");
+    // uncomment later
+    // redirect("view.php?id=$cm->id&edit=1");
     // In this case you process validated data. $mform->get_data() returns data posted in form.
 } else {
     // This branch is executed if the form is submitted but the data doesn't validate and the form should be redisplayed