Browse Source

update to moodle phpcs format
2 functions in locallib.php missing because of camel case

Bernardo 3 years ago
parent
commit
930b15bb92
15 changed files with 925 additions and 668 deletions
  1. 32 21
      classes/event/gradeimporter_submission_created.php
  2. 63 46
      db/access.php
  3. 20 5
      db/install.php
  4. 21 6
      index.php
  5. 113 90
      lib.php
  6. 128 106
      locallib.php
  7. 61 48
      mod_form.php
  8. 2 1
      readme.md
  9. 20 5
      renderer.php
  10. 76 46
      submission.php
  11. 128 105
      submission_form.php
  12. 60 33
      submissiontype.php
  13. 57 44
      submissiontype_form.php
  14. 15 2
      version.php
  15. 129 110
      view.php

+ 32 - 21
classes/event/gradeimporter_submission_created.php

@@ -1,67 +1,78 @@
 <?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/>.
 
 namespace mod_gradeimporter\event;
 
-defined('MOODLE_INTERNAL') || die();
-
 class gradeimporter_submission_created extends \core\event\base {
-    
     protected function init() {
         $this->data['crud'] = 'c';
         $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
     }
 
     /**
-    *Returns the description of what happened
-    *
-    *@return string
-    */ 
-    public function get_description(){
-        return "The user with id '$this->userid' has created a submission " . 
-                "on the grade importer with the course module id '$this->contextinstanceid'." ; 
+     * Returns the description of what happened
+     *
+     * @return string
+     */
+    public function get_description () {
+        return "The user with id '$this->userid' has created a submission " .
+                "on the grade importer with the course module id '$this->contextinstanceid'.";
     }
 
     /**
      * Returns localised event name
-     * 
+     *
      * @return string
      */
-    public static function get_name(){
+    public static function get_name () {
         return get_string('eventmodelsubmissioncreated', 'mod_gradeimporter');
     }
 
     /**
      * Get URL related to the action
-     * 
+     *
      * @return \moodle_url
      */
-    public function get_url(){
+    public function get_url () {
         $url = new \moodle_url('/mod/gradeimporter/view.php', array('id' => $this->contextinstanceid));
         return $url;
     }
 
     /**
      * Return the legacy event log data.
-     * 
+     *
      * @return array|null
      */
-    protected function get_legacy_logdata(){
-        //the legacy log table expects a relative path to /mod/model/
+    protected function get_legacy_logdata () {
+        // The legacy log table expects a relative path to /mod/model/ !!
         $logurl = new \moodle_url('/mod/gradeimporter/view.php', array('id' => $this->contextinstanceid));
-        return array($this->courseid, 'gradeimporter', 'editgradeimporter', $logurl, 
+        return array($this->courseid, 'gradeimporter', 'editgradeimporter', $logurl,
                         $this->other['gradeimporterid'], $this->gradeimporterid);
     }
 
     /**
      * Custom validation
-     * 
+     *
      * @throws \coding_exception
      * @return void
      */
-    protected function validate_data(){
+    protected function validate_data () {
         parent::validate_data();
         if ($this->contextlevel != CONTEXT_MODULE) {
             throw new \coding_exception('Context level must be CONTEXT_MODULE');
         }
     }
-}
+}

+ 63 - 46
db/access.php

@@ -1,57 +1,74 @@
 <?php
-    $capabilities = array(
-        'mod/gradeimporter:addinstance' => array(
-            'riskbitmask' => RISK_XSS,
-            'captype' => 'write',
-            'contextlevel' => CONTEXT_COURSE,
-            'archetypes' => array(
-                'editingteacher' => CAP_ALLOW,
-                'manager' => CAP_ALLOW
-            ),
-            'clonepermissionsfrom' => 'moodle/course:manageactivies'
+// 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();
+
+$capabilities = array(
+    'mod/gradeimporter:addinstance' => array(
+        'riskbitmask' => RISK_XSS,
+        'captype' => 'write',
+        'contextlevel' => CONTEXT_COURSE,
+        'archetypes' => array(
+            'editingteacher' => CAP_ALLOW,
+            'manager' => CAP_ALLOW
+        ),
+        'clonepermissionsfrom' => 'moodle/course:manageactivies'
+    ),
+    'mod/gradeimporter:view' => array(
+        'captype' => 'read',
+        'contextlevel' => CONTEXT_MODULE,
+        'archetypes' => array(
+            'guest' => CAP_ALLOW,
+            'student' => CAP_ALLOW,
+            'teacher' => CAP_ALLOW,
+            'editingteacher' => CAP_ALLOW,
+            'manager' => CAP_ALLOW
+            )
         ),
-        'mod/gradeimporter:view' => array(
-            'captype' => 'read',
+    'mod/gradeimporter:addsubmission' => array(
+            'captype' => 'write',
             'contextlevel' => CONTEXT_MODULE,
             'archetypes' => array(
-                'guest' => CAP_ALLOW,
-                'student' => CAP_ALLOW,
-                'teacher' => CAP_ALLOW,
-                'editingteacher' => CAP_ALLOW,
-                'manager' => CAP_ALLOW
-                )
-            ),
-        'mod/gradeimporter:addsubmission' => array(
+                'editingteacher' => CAP_ALLOW
+            )
+        ),
+    'mod/gradeimporter:edit' => array(
                 'captype' => 'write',
                 'contextlevel' => CONTEXT_MODULE,
                 'archetypes' => array(
-                    'editingteacher' => CAP_ALLOW
+                    'editingteacher' => CAP_ALLOW,
+                    'manager' => CAP_ALLOW
                 )
-            ),
-        'mod/gradeimporter:edit' => array(
-                    'captype' => 'write',
-                    'contextlevel' => CONTEXT_MODULE,
-                    'archetypes' => array(
-                        'editingteacher' => CAP_ALLOW,
-                        'manager' => CAP_ALLOW
-                    )
-        ),
+    ),
 
-        /*'mod/gradeimporter:teacherview' => array(
-            'captype' => 'write',
-            'contextlevel' => CONTEXT_MODULE,
-            'archetypes' => array(
-                'editingteacher' => CAP_ALLOW,
-                'manager' => CAP_ALLOW,
-                'teacher' => CAP_ALLOW
-            )
-),*/
-        
-        'mod/gradeimporter:student'=> array(
-            'captype'=> 'read',
-            'contextlevel' =>  CONTEXT_MODULE,
-            'archetypes' => array(
-                'student' => CAP_ALLOW
-            )
+    /*'mod/gradeimporter:teacherview' => array(
+        'captype' => 'write',
+        'contextlevel' => CONTEXT_MODULE,
+        'archetypes' => array(
+            'editingteacher' => CAP_ALLOW,
+            'manager' => CAP_ALLOW,
+            'teacher' => CAP_ALLOW
+        )
+    ),*/
+
+    'mod/gradeimporter:student' => array(
+        'captype' => 'read',
+        'contextlevel' => CONTEXT_MODULE,
+        'archetypes' => array(
+            'student' => CAP_ALLOW
         )
+    )
 );

+ 20 - 5
db/install.php

@@ -1,7 +1,22 @@
 <?php
-    function xmldb_gradeimporter_install(){
-    }
+// 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 xmldb_gradeimporter_install_recovery(){
-        
-    }
+function xmldb_gradeimporter_install() {
+}
+
+function xmldb_gradeimporter_install_recovery() {
+
+}

+ 21 - 6
index.php

@@ -1,9 +1,24 @@
 <?php
-    require_once('../../config.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/>.
 
-    $id = required_param('id', PARAM_INT);           // Course ID
+require_once(__DIR__.'../../config.php');
 
-    // Ensure that the course specified is valid
-    if (!$course = $DB->get_record('course', array('id'=> $id))) {
-        print_error('Course ID is incorrect');
-    }
+$id = required_param('id', PARAM_INT); // Course ID.
+
+// Ensure that the course specified is valid
+if (!$course = $DB->get_record('course', array('id' => $id))) {
+    throw new moodle_exception('Course ID is incorrect');
+}

+ 113 - 90
lib.php

@@ -1,6 +1,20 @@
 <?php
-
-//adds gradeimporter to add new activity page
+// 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/>.
+
+// Adds gradeimporter to add new activity page
 function tool_devcourse_extend_navigation_course($navigation, $course, $coursecontext) {
     $url = new moodle_url('/admin/tool/devcourse/index.php');
     $devcoursenode = navigation_node::create('Development course', $url, navigation_node::TYPE_CUSTOM, 'Dev course', 'devcourse');
@@ -8,21 +22,29 @@ function tool_devcourse_extend_navigation_course($navigation, $course, $courseco
 }
 
 function gradeimporter_supports($feature) {
-    switch($feature) {
-        case FEATURE_MOD_ARCHETYPE:                 return MOD_ARCHETYPE_RESOURCE;
-        case FEATURE_GROUPS:                        return false;
-        case FEATURE_GROUPINGS:                     return false;
-        case FEATURE_MOD_INTRO:                     return true;
-        case FEATURE_COMPLETION_TRACKS_VIEWS:       return true;
-        case FEATURE_GRADE_HAS_GRADE:               return false;
-        case FEATURE_GRADE_OUTCOMES:                return false;
-        case FEATURE_BACKUP_MOODLE2:                return true;
-        
-       default:                                     return null;
-      }
+    switch ($feature) {
+        case FEATURE_MOD_ARCHETYPE:
+            return MOD_ARCHETYPE_RESOURCE;
+        case FEATURE_GROUPS:
+            return false;
+        case FEATURE_GROUPINGS:
+            return false;
+        case FEATURE_MOD_INTRO:
+            return true;
+        case FEATURE_COMPLETION_TRACKS_VIEWS:
+            return true;
+        case FEATURE_GRADE_HAS_GRADE:
+            return false;
+        case FEATURE_GRADE_OUTCOMES:
+            return false;
+        case FEATURE_BACKUP_MOODLE2:
+            return true;
+        default:
+            return null;
     }
+}
 
-function gradeimporter_add_instance ($data, $mform) {
+function gradeimporter_add_instance($data, $mform) {
     /*
         *Given an object containing all the necessary data,
         *(defined by the form in mod_form.php) this function
@@ -34,13 +56,13 @@ function gradeimporter_add_instance ($data, $mform) {
     */
     global $DB;
     $data->timemodified = time();
-    
+
     $data->id = $DB->insert_record("gradeimporter", $data);
-    
+
     return $data->id;
 }
 
-function gradeimporter_update_instance ($data){
+function gradeimporter_update_instance($data) {
     /*
         *given an object containing all the necessary data,
         *(defined by the form in mod_form.php) this function
@@ -57,7 +79,7 @@ function gradeimporter_update_instance ($data){
     return $DB->update_record('gradeimporter', $data);
 }
 
-function gradeimporter_delete_instance($data){
+function gradeimporter_delete_instance($data) {
     /*
     *Given an id of a gradeimporter instance,
     * this function permanently deletes the instance
@@ -69,8 +91,8 @@ function gradeimporter_delete_instance($data){
     */
 
     global $DB;
-    
-    if (!$data = $DB->get_record('gradeimporter', array('id'=>$id) ) ){
+
+    if (!$data = $DB->get_record('gradeimporter', array('id' => $id))) {
         return false;
     }
 
@@ -80,85 +102,86 @@ function gradeimporter_delete_instance($data){
     // Files
     $fs = get_file_storage();
     $fs->delete_area_files($context->id, 'mod_gradeimporter');
-    
-    //delete all files and submissions associated with this instance
+
+    // Delete all files and submissions associated with this instance
     $DB->delete_records('gradeimporter_submission', array('gradeimporterid' => $gradeimporter->id));
     $DB->delete_records('gradeimporter_feedback', array('gradeimporterid' => $gradeimporter->id));
 
-    //delete the instance itself
-    $DB->delete_records('gradeimporter', array('id'=>$id) );
+    // Delete the instance itself
+    $DB->delete_records('gradeimporter', array('id' => $id));
     return true;
 }
 
-function gradeimporter_check_for_zips ($context, $cm, $submission){
-  global $DB;
+function gradeimporter_check_for_zips($context, $cm, $submission) {
+    global $DB;
 
     $fs = get_file_storage();
-    $files = $fs->get_area_files($context->id, 'mod_gradeimporter', 'submission', $submission->id, "itemid, filepath, filename", false);
+    $files = $fs->get_area_files($context->id, 'mod_gradeimporter',
+                                'submission', $submission->id,
+                                "itemid, filepath, filename", false);
+
     foreach ($files as $storedfile) {
-      if ($storedfile->get_mimetype() == 'application/zip') {
-        //unpack zip
-        $packer = get_file_packer('application/zip');
-        $fs->delete_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0);
-        $storedfile->extract_to_storage($packer, $context->id, 'mod_gradeimporter', 'unpacktemp', 0, "item, filepath, filename", false);
-        $tempfiles = $fs->get_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0, "itemid, filepath, filename", false);
-        if (count($tempfiles) > 0) {
-          $storedfile->delete(); // delete the zip
-
-          foreach ($tempfiles as $storedfile){
-            if ($storedfile->get_mimetype() == 'text/csv'){
-
-              $csvData = $storedfile->get_content();
-              $csvLines = explode(PHP_EOL, $csvData);
-              $csv = array();
-              foreach($csvLines as $line){
-                $csv[] = str_getcsv($line);
-              }
-              //array_map('str_getcsv', $storedfile->get_content() );
-              array_walk($csv, function(&$a) use ($csv) { //adds first row as keys
-                $a = array_combine($csv[0], $a);
-              });
-              array_shift($csv); // remove column header
-              for ($i = 0; $i< sizeof($csv); $i++){
+        if ($storedfile->get_mimetype() == 'application/zip') {
+            // Unpack zip
+            $packer = get_file_packer('application/zip');
+            $fs->delete_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0);
+            $storedfile->extract_to_storage($packer, $context->id, 'mod_gradeimporter',
+                                            'unpacktemp', 0, "item, filepath, filename", false
+                                        );
+            $tempfiles = $fs->get_area_files($context->id, 'mod_gradeimporter', 'unpacktemp',
+                                            0, "itemid, filepath, filename", false);
+            if (count($tempfiles) > 0) {
+                $storedfile->delete(); // Delete the zip
+
                 foreach ($tempfiles as $storedfile) {
-                  if ($storedfile->get_filename() == $csv[$i]['file']){                   
-
-                    $dbentry = new stdClass();
-                    $dbentry->id = null;
-                    $dbentry->submissionid  = $submission->id;
-                    $dbentry->studentid     = $csv[$i]['id'];
-                    $dbentry->grade        = $csv[$i]['grade'];
-                    $dbentry->comment      = $csv[$i]['comment'];
-                    $dbentry->fileid      = $submission->id;
-                    $dbentry->usermodified = 0;
-                    $dbentry->timecreated   = time();
-                    $dbentry->timemodified   = time();
-                    $dbentry->contextid   = $context->id;
-                    $dbentry->name    = $csv[$i]['file'];
-
-                    $dbentry->id = $DB->insert_record('gradeimporter_feedback', $dbentry);
-
-                    $fileinfo = array(
-                      'contextid'   =>  $context->id,
-                      'component'   =>  'mod_gradeimporter',
-                      'filearea'    =>  'gradeimporter_feedback',
-                      'itemid'      =>  $dbentry->id,
-                      'filepath'    =>  '/',
-                      'filename'        =>  $csv[$i]['file']
-                    ); 
-
-                    $fs->create_file_from_storedfile($fileinfo, $storedfile);
-                    break;
-                  }
+                    if ($storedfile->get_mimetype() == 'text/csv') {
+                        $csvdata = $storedfile->get_content();
+                        $csvlines = explode(PHP_EOL, $csvdata);
+                        $csv = array();
+                        foreach ($csvlines as $line) {
+                            $csv[] = str_getcsv($line);
+                        }
+                        // array_map('str_getcsv', $storedfile->get_content() );
+                        array_walk($csv, function (&$a) use ($csv) { // Adds first row as keys
+                            $a = array_combine($csv[0], $a);
+                        });
+                        array_shift($csv); // Remove column header
+                        for ($i = 0; $i < count($csv); $i++) {
+                            foreach ($tempfiles as $storedfile) {
+                                if ($storedfile->get_filename() == $csv[$i]['file']) {
+                                    $dbentry = new stdClass();
+                                    $dbentry->id = null;
+                                    $dbentry->submissionid  = $submission->id;
+                                    $dbentry->studentid     = $csv[$i]['id'];
+                                    $dbentry->grade        = $csv[$i]['grade'];
+                                    $dbentry->comment      = $csv[$i]['comment'];
+                                    $dbentry->fileid      = $submission->id;
+                                    $dbentry->usermodified = 0;
+                                    $dbentry->timecreated   = time();
+                                    $dbentry->timemodified   = time();
+                                    $dbentry->contextid   = $context->id;
+                                    $dbentry->name    = $csv[$i]['file'];
+
+                                    $dbentry->id = $DB->insert_record('gradeimporter_feedback', $dbentry);
+
+                                    $fileinfo = array(
+                                        'contextid' => $context->id,
+                                        'component' => 'mod_gradeimporter',
+                                        'filearea' => 'gradeimporter_feedback',
+                                        'itemid' => $dbentry->id,
+                                        'filepath' => '/',
+                                        'filename' => $csv[$i]['file']
+                                    );
+
+                                    $fs->create_file_from_storedfile($fileinfo, $storedfile);
+                                    break;
+                                }
+                            }
+                        }
+                    }
                 }
-                
-              }
             }
-          }
-
-
-          }
-        $fs->delete_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0);
-      }
+            $fs->delete_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0);
+        }
     }
-  }
+}

+ 128 - 106
locallib.php

@@ -1,10 +1,26 @@
 <?php
-
-function get_comments($cmid, $id){
-  global $DB, $USER, $CFG;
-  $tp = $CFG->prefix; // gets moodle tables prefix, not everyone uses mdl_
-
-  $sql = "
+// 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();
+
+function get_comments($cmid, $id) {
+    global $DB, $USER, $CFG;
+    $tp = $CFG->prefix; // gets moodle tables prefix, not everyone uses mdl_
+
+    $sql = "
           SELECT gf.id gf_id,
                 gf.grade,
                 gf.comment gf_comment,
@@ -23,65 +39,66 @@ function get_comments($cmid, $id){
               ON gs.type = gst.id
           WHERE gf.studentid = ?";
 
-  $records = $DB->get_records_sql($sql, array('studentid' => $USER->id));
-
-  $data = array();
-  if (count($records)) {
-    foreach ($records as $value) {
-      if (!array_key_exists($value->gst_name, $data)) $data[$value->gst_name] = array();
+    $records = $DB->get_records_sql($sql, array('studentid' => $USER->id));
 
+    $data = array();
+    if (count($records)) {
+        foreach ($records as $value) {
+            if (!array_key_exists($value->gst_name, $data)) {
+                $data[$value->gst_name] = array();
+            }
 
-      $fileurl = buildurl($cmid, $value->gf_id, $value->gf_name);
+            $fileurl = buildurl($cmid, $value->gf_id, $value->gf_name);
 
-      $data[$value->gst_name][] = array($value->gs_name, $value->grade, $value->gf_comment, $fileurl);
+            $data[$value->gst_name][] = array($value->gs_name, $value->grade, $value->gf_comment, $fileurl);
+        }
     }
-  }
 
-  return $data;
+    return $data;
 }
 
 
-function get_teacher_view($cmid, $gradeimporterid, $enrolledusers){
-  global $DB, $CFG;
-  
-  $tp = $CFG->prefix; // gets moodle tables prefix, not everyone uses mdl_
+function get_teacher_view($cmid, $gradeimporterid, $enrolledusers) {
+    global $DB, $CFG;
 
-  $table = new html_table();
-  $table->align = array('center');
-  $table->attributes = array('class'=>'generaltable mod_index');
+    $tp = $CFG->prefix; // gets moodle tables prefix, not everyone uses mdl_
 
-  //creates table head
-  $subrow = new html_table_row();
-  
-  $cell = new html_table_cell(get_string('nameCol', 'gradeimporter'));
-  $cell->style = "border:1px solid black";
+    $table = new html_table();
+    $table->align = array('center');
+    $table->attributes = array('class' => 'generaltable mod_index');
 
-  $cell->colspan = 1;
-  $subrow->cells[] = $cell;
-  $submissionsSQL = "SELECT id, name 
-                      FROM ".$tp."gradeimporter_submission 
-                      WHERE gradeimporterid = $gradeimporterid
-                      ORDER BY id";
-  $submissions = $DB->get_records_sql($submissionsSQL);
-  foreach ($submissions as $submission){
-    $subname = editSub($submission->name, $gradeimporterid, $cmid, $submission->id);
-    $cell = new html_table_cell($subname);
-    $cell->colspan = 2;
+    // Creates table head
+    $subrow = new html_table_row();
+
+    $cell = new html_table_cell(get_string('nameCol', 'gradeimporter'));
     $cell->style = "border:1px solid black";
+
+    $cell->colspan = 1;
     $subrow->cells[] = $cell;
+    $submissionssql = "SELECT id, name
+                      FROM ".$tp."gradeimporter_submission
+                      WHERE gradeimporterid = $gradeimporterid
+                      ORDER BY id";
+    $submissions = $DB->get_records_sql($submissionssql);
+    foreach ($submissions as $submission) {
+        $subname = editSub($submission->name, $gradeimporterid, $cmid, $submission->id);
+        $cell = new html_table_cell($subname);
+        $cell->colspan = 2;
+        $cell->style = "border:1px solid black";
+        $subrow->cells[] = $cell;
+
+        // Adds 2 align, one for grade and another to the file
+        $table->align[] = 'center';
+        $table->align[] = 'center';
+    }
+    $table->data[] = $subrow;
 
-    //adds 2 align, one for grade and another to the file
-    $table->align[] = 'center';
-    $table->align[] = 'center';
-  }
-  $table->data[] = $subrow;
-  
-  //all submissions names are on the table
-  
-  $sql = " SELECT gs.id gs_id,
+    // All submissions names are on the table
+
+    $sql = " SELECT gs.id gs_id,
                   gs.gradeimporterid gs_gradeimporterid,
                   gs.type gs_type,
-                  gs.name gs_name, 
+                  gs.name gs_name,
                   gst.name  gst_name,
                   gf.id gf_id,
                   gf.studentid  gf_studentid,
@@ -100,43 +117,42 @@ function get_teacher_view($cmid, $gradeimporterid, $enrolledusers){
                       ON gst.id = gs.type
                   WHERE gs.gradeimporterid = $gradeimporterid
                   ORDER BY user.firstname, user.lastname, gs.id, gf.id";
-  $records = $DB->get_recordset_sql($sql);
-  
-  $usersList = array();
-  foreach ($enrolledusers as $user){
-    $usersList[$user->id] = array('name'=>$user->firstname." ".$user->lastname);
-  }
-  //ksort($usersList);
-
-  foreach ($records as $record){
-    $url = buildurl($cmid, $record->gf_id, $record->gf_name);
-    $usersList[$record->gf_studentid][$record->gs_id] = array('grade'=>$record->gf_grade, 'file'=>$url);
-  }
-  
-  foreach($usersList as $user){
-    $row = new html_table_row();
-    $cell = new html_table_cell($user['name']);
-    $cell->style = "border:1px solid black";
-    $row->cells[] = $cell;
-    foreach ($submissions as $submission){
-      if (array_key_exists($submission->id, $user)){
-        $url = $user[$submission->id]['file'];
-        $grade = $user[$submission->id]['grade'];
-      } else{
-        $url = '-';
-        $grade = '-';
-      }
-      $cell = new html_table_cell ($grade);
-      $cell->style = "border:1px solid black; border-right:0";
-      $row->cells[] = $cell;
-
-      $cell = new html_table_cell ($url);
-      $cell->style = "border:1px solid black; border-left:0";
-      $row->cells[] = $cell;
+    $records = $DB->get_recordset_sql($sql);
+
+    $userslist = array();
+    foreach ($enrolledusers as $user) {
+        $userslist[$user->id] = array('name' => $user->firstname." ".$user->lastname);
+    }
+
+    foreach ($records as $record) {
+        $url = buildurl($cmid, $record->gf_id, $record->gf_name);
+        $userslist[$record->gf_studentid][$record->gs_id] = array('grade' => $record->gf_grade, 'file' => $url);
     }
-    $table->data[] = $row;
-  }
-  echo html_writer::table($table);
+
+    foreach ($userslist as $user) {
+        $row = new html_table_row();
+        $cell = new html_table_cell($user['name']);
+        $cell->style = "border:1px solid black";
+        $row->cells[] = $cell;
+        foreach ($submissions as $submission) {
+            if (array_key_exists($submission->id, $user)) {
+                $url = $user[$submission->id]['file'];
+                $grade = $user[$submission->id]['grade'];
+            } else {
+                $url = '-';
+                $grade = '-';
+            }
+            $cell = new html_table_cell($grade);
+            $cell->style = "border:1px solid black; border-right:0";
+            $row->cells[] = $cell;
+
+            $cell = new html_table_cell($url);
+            $cell->style = "border:1px solid black; border-left:0";
+            $row->cells[] = $cell;
+        }
+        $table->data[] = $row;
+    }
+    echo html_writer::table($table);
 }
 
 
@@ -144,32 +160,38 @@ require_once($CFG->libdir . '/filelib.php');
 require_once("$CFG->libdir/csvlib.class.php");
 
 
-function buildurl($cmid, $fileid, $filename){
-
-  $fileurl = new moodle_url("/mod/gradeimporter/view.php", array('id' => $cmid, 'fileid' => $fileid, 'filename' => $filename, 'action' => 1));
-  return "<a href=$fileurl target=_blank> $filename</a>";
+function buildurl($cmid, $fileid, $filename) {
+    $fileurl = new moodle_url("/mod/gradeimporter/view.php", array('id' => $cmid, 'fileid' => $fileid, 'filename' => $filename, 'action' => 1));
+    return "<a href=$fileurl target=_blank> $filename</a>";
 }
 
-function editSub($subname, $gradeimporterid, $cmid, $subid){
-  $url = new moodle_url("/mod/gradeimporter/submission.php", array('id'=>$gradeimporterid, 'cmid'=>$cmid, 'subid'=>$subid, 'update'=>1));
-  return $subname.'<a href='.$url.' target="_blank"><i class="icon fa fa-pencil fa-fw"  title="'.get_string('editSub','gradeimporter').'" aria-label="'.get_string('editSub','gradeimporter').'"></i>';
+function editSub($subname, $gradeimporterid, $cmid, $subid) {
+    $url = new moodle_url("/mod/gradeimporter/submission.php", array('id' => $gradeimporterid,
+                                                                    'cmid' => $cmid,
+                                                                    'subid' => $subid,
+                                                                    'update' => 1
+                                                                  )
+                           );
+    return $subname.'<a href='.$url.' target="_blank"><i class="icon fa fa-pencil fa-fw"  title="'
+                        .get_string('editSub', 'gradeimporter')
+                        .'" aria-label="'
+                        .get_string('editSub', 'gradeimporter').'"></i>';
 }
 
-function exportCSV($context){
-  $enrolledusers = get_enrolled_users($context, 'mod/gradeimporter:student');
-  $header = array('id', 'name', 'email', 'grade', 'comment', 'file');
+function exportCSV($context) {
+    $enrolledusers = get_enrolled_users($context, 'mod/gradeimporter:student');
+    $header = array('id', 'name', 'email', 'grade', 'comment', 'file');
 
-  $csvexport = new csv_export_writer();
-  $csvexport->set_filename('config');
-  $csvexport->add_data($header);
+    $csvexport = new csv_export_writer();
+    $csvexport->set_filename('config');
+    $csvexport->add_data($header);
 
-  foreach ($enrolledusers as $value) {
-    $name = $value->firstname . ' ' . $value->lastname;
-    $studententry = array($value->id, $name, $value->email, '', '', '');
-    $csvexport->add_data($studententry);
-  }
-
-  $csvexport->download_file();
-  //$dlfile = $csvexporter->download_array('config', $data);
+    foreach ($enrolledusers as $value) {
+        $name = $value->firstname . ' ' . $value->lastname;
+        $studententry = array($value->id, $name, $value->email, '', '', '');
+        $csvexport->add_data($studententry);
+    }
 
+    $csvexport->download_file();
+    // $dlfile = $csvexporter->download_array('config', $data);
 }

+ 61 - 48
mod_form.php

@@ -1,50 +1,63 @@
 <?php
-    if (!defined('MOODLE_INTERNAL')) {
-        die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
-    }
-
-    require_once($CFG->dirroot.'/course/moodleform_mod.php');
-    require_once($CFG->dirroot.'/mod/gradeimporter/lib.php');
-
-    class mod_gradeimporter_mod_form extends moodleform_mod {
-
-        function definition() {
-            global $CFG, $COURSE, $USER, $DB, $OUTPUT;
-
-            $mform =& $this->_form;
-            $course_modules_id = optional_param('update', 0, PARAM_INT);
-
-            $mform->addElement('header', 'general', get_string('general', 'gradeimporter'));
-
-            $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');
-            
-            
-            $this->standard_intro_elements(get_string('description', 'gradeimporter'));
-
-            //$ynoptions = array(0 => get_string('visibilityOn', 'gradeimporter'), 1 => get_string('visibilityOff', 'gradeimporter'));
-            //$mform->addElement('')
-
-
-            $features = array('groups' => false, 'groupings' => false, 'groupmembersonly' => false,  'outcomes' => false,'gradecat' => false, 'idnumber' => false);
-            $this->standard_coursemodule_elements($features);
-            
-            //hidden fields
-            $mform->addElement('hidden', 'action');
-            $mform->setType('action', PARAM_TEXT);
-            $mform->addElement('hidden', 'id');
-            $mform->setType('id', PARAM_TEXT);
-            $mform->addElement('hidden', 'author_name');
-            $mform->setType('author_name', PARAM_TEXT);
-            $mform->addElement('hidden', 'author_modified_name');
-            $mform->setType('author_modified_name', PARAM_TEXT);
-            $mform->addElement('hidden', 'timecreated');
-            $mform->setType('timecreated', PARAM_TEXT);
-            $mform->addElement('hidden', 'position');
-            $mform->setType('position', PARAM_TEXT);
-
-            $this->add_action_buttons();
-            }
-
+// 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->dirroot.'/mod/gradeimporter/lib.php');
+
+class mod_gradeimporter_mod_form extends moodleform_mod {
+    public function definition() {
+        global $CFG, $COURSE, $USER, $DB, $OUTPUT;
+
+        $mform =& $this->_form;
+        $coursemodulesid = optional_param('update', 0, PARAM_INT);
+
+        $mform->addElement('header', 'general', get_string('general', 'gradeimporter'));
+
+        $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');
+
+        $this->standard_intro_elements(get_string('description', 'gradeimporter'));
+
+        // $ynoptions = array(0 => get_string('visibilityOn', 'gradeimporter'), 1 => get_string('visibilityOff', 'gradeimporter'));
+        // $mform->addElement('')
+
+        $features = array('groups' => false, 'groupings' => false,
+                        'groupmembersonly' => false,  'outcomes' => false,
+                        'gradecat' => false, 'idnumber' => false
+                    );
+
+        $this->standard_coursemodule_elements($features);
+
+        // Hidden fields
+        $mform->addElement('hidden', 'action');
+        $mform->setType('action', PARAM_TEXT);
+        $mform->addElement('hidden', 'id');
+        $mform->setType('id', PARAM_TEXT);
+        $mform->addElement('hidden', 'author_name');
+        $mform->setType('author_name', PARAM_TEXT);
+        $mform->addElement('hidden', 'author_modified_name');
+        $mform->setType('author_modified_name', PARAM_TEXT);
+        $mform->addElement('hidden', 'timecreated');
+        $mform->setType('timecreated', PARAM_TEXT);
+        $mform->addElement('hidden', 'position');
+        $mform->setType('position', PARAM_TEXT);
+
+        $this->add_action_buttons();
     }
+}

+ 2 - 1
readme.md

@@ -1,4 +1,5 @@
-### Important Links
+# Important Links
+
 - [gradeimporter Repository](http://200.144.254.107/git/bernardo/gradeImporter)
 - [Moodle School](https://moodledev.moodle.school/ )
 - [Repository Plugin - MoodleDocs](https://docs.moodle.org/dev/Repository_plugins)

+ 20 - 5
renderer.php

@@ -1,8 +1,20 @@
 <?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();
-
-class mod_wavefront_renderer extends plugin_renderer_base{
+class mod_wavefront_renderer extends plugin_renderer_base {
     /**
      * Returns html to display the gradeimporter feedbacks
      * @param object $gradeimporter feedback with which is associated to the user
@@ -13,7 +25,10 @@ class mod_wavefront_renderer extends plugin_renderer_base{
         $output = '';
 
         if ($gradeimporter->intro && !editing) {
-            $output .= $this->output->box(format_module_intro('gradeimporter', $gradeimporter, $this->page->cm->id), 'generalbox', 'intro');
+            $output .= $this->output->box(format_module_intro('gradeimporter',
+                                            $gradeimporter, $this->page->cm->id),
+                                            'generalbox', 'intro'
+                                        );
         }
     }
-}
+}

+ 76 - 46
submission.php

@@ -1,4 +1,18 @@
 <?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');
@@ -6,30 +20,30 @@ require_once('lib.php');
 require_once('submission_form.php');
 
 
-$cmid   =   required_param('cmid', PARAM_INT);            // Course Module ID
-$id     =   optional_param('id' , 0, PARAM_INT);           //Gradeimporter id
-$subid  =   optional_param('subid', 1, PARAM_INT);       //submission id
-$update =   optional_param('update', 0, PARAM_INT);      // if 1 the submission is beign updated
+$cmid = required_param('cmid', PARAM_INT);            // Course Module ID.
+$id = optional_param('id', 0, PARAM_INT);           // Gradeimporter id.
+$subid = optional_param('subid', 1, PARAM_INT);       // Submission id.
+$update = optional_param('update', 0, PARAM_INT);      // If 1 the submission is beign updated.
 
 
-if (!$cm = get_coursemodule_from_id('gradeimporter', $cmid)){
-    print_error('invalidcoursemodule');
+if (!$cm = get_coursemodule_from_id('gradeimporter', $cmid)) {
+    throw new moodle_exception('invalidcoursemodule');
 }
 
-if (!$course = $DB->get_record('course', array('id'=>$cm->course))){
-    print_error('coursemisconf');
+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))){
-    print_error('invalidid', 'gradeimporter');
+if (!$gradeimporter = $DB->get_record('gradeimporter', array('id' => $cm->instance))) {
+    throw new moodle_exception(get_string('invalidid', 'gradeimporter'));
 }
 
-$url = new moodle_url('/mod/gradeimporter/submission.php', array('cmid'=>$cm->id));
-if(!empty($id)){
+$url = new moodle_url('/mod/gradeimporter/submission.php', array('cmid' => $cm->id));
+if (!empty($id)) {
     $url->param('id', $id);
 }
 $PAGE->set_url($url);
@@ -37,11 +51,12 @@ $PAGE->set_url($url);
 require_login($course, false, $cm);
 
 
-if(isset($submission)){
-    if (isguestuser()){
-        print_error('guestnoedit', 'gradeimporter', "$CFG->wwwroot/mod/gradeimporter/view.php?id=$cmid");
+if (isset($submission)) {
+    if (isguestuser()) {
+        throw new moodle_exception(get_string('guestnoedit', 'gradeimporter').
+                            " $CFG->wwwroot/mod/gradeimporter/view.php?id=$cmid");
     }
-} else{
+} else {
     $submission = new stdClass();
     $submission->id = null;
 }
@@ -49,30 +64,39 @@ if(isset($submission)){
 $maxfiles = 50;
 $maxbytes = $course->maxbytes;
 
-$descriptionoptions = array('trusttext'=>true, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes, 'context'=>$context, 'subdirs'=>file_area_contains_subdirs($context, 'mod_gradeimporter', 'submission', $submission->id));
-$submissionoptions = array('subdirs'=>false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes);
+$descriptionoptions = array('trusttext' => true, 'maxfiles' => $maxfiles,
+                        'maxbytes' => $maxbytes, 'context' => $context,
+                        'subdirs' => file_area_contains_subdirs($context, 'mod_gradeimporter', 'submission', $submission->id));
 
-$submission = file_prepare_standard_editor($submission, 'description', $descriptionoptions, $context, 'mod_gradeimporter', 'description', $submission->id);
-$submission = file_prepare_standard_filemanager($submission, 'submission', $submissionoptions, $context, 'mod_gradeimporter', 'submission', $submission->id);
+$submissionoptions = array('subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
+
+$submission = file_prepare_standard_editor($submission, 'description', $descriptionoptions, $context,
+                                            'mod_gradeimporter', 'description', $submission->id);
+$submission = file_prepare_standard_filemanager($submission, 'submission', $submissionoptions, $context,
+                                                'mod_gradeimporter', 'submission', $submission->id);
 
 $submission->cmid = $cm->id;
 
-$mform = new mod_gradeimporter_submission_form(null, array('submission'=>$submission, 'cm'=>$cm, 'descriptionoptions'=>$descriptionoptions, 'submissionoptions'=>$submissionoptions, 'gradeimporterid'=>$gradeimporter->id));
+$mform = new mod_gradeimporter_submission_form(null, array('submission' => $submission,
+                                                     'cm' => $cm, 'descriptionoptions' => $descriptionoptions,
+                                                     'submissionoptions' => $submissionoptions,
+                                                     'gradeimporterid' => $gradeimporter->id
+                                                    ));
 
-if ($update==1){
-    $subToUpdate = $DB->get_record('gradeimporter_submission', array('id'=>$subid));
-    $array = (array) $subToUpdate;
+if ($update == 1) {
+    $subtoupdate = $DB->get_record('gradeimporter_submission', array('id' => $subid));
+    $array = (array) $subtoupdate;
     echo 'aaaa'.$array['description'];
-    $mform->set_data($subToUpdate);
+    $mform->set_data($subtoupdate);
 }
 
-if ($mform->is_cancelled()){
-    if ($id){
+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 ($submission = $mform->get_data()){
+} else if ($submission = $mform->get_data()) {
     $timenow = time();
 
     if (empty($submission->id)) {
@@ -84,38 +108,44 @@ if ($mform->is_cancelled()){
     } else {
         $isnewentry                     = false;
     }
-    $submission->description        = '';           //updated later
-    $submission->descriptionformat  = FORMAT_HTML;  //updated later
+    $submission->description        = '';           // Updated later.
+    $submission->descriptionformat  = FORMAT_HTML;  // Updated later.
     $submission->definitiontrust    = 0;
-    $submission->timemodified       = $timenow;     
+    $submission->timemodified       = $timenow;
     $submission->position           = -1;
 
     if ($isnewentry) {
-        //add new entry
+        // Add new entry.
         $submission->id = $DB->insert_record('gradeimporter_submission', $submission);
     } else {
-        //update existing entry
+        // Update existing entry.
         $DB->update_record('gradeimporter', $submission);
     }
 
-    $submission = file_postupdate_standard_editor($submission, 'description', $descriptionoptions, $context, 'mod_gradeimporter', 'description', $submission->id);
-    $submission = file_postupdate_standard_filemanager($submission, 'submission', $submissionoptions, $context, 'mod_gradeimporter', 'submission', $submission->id);
+    $submission = file_postupdate_standard_editor($submission, 'description',
+                                            $descriptionoptions, $context, 'mod_gradeimporter',
+                                            'description', $submission->id);
+
+    $submission = file_postupdate_standard_filemanager($submission, 'submission',
+                                            $submissionoptions, $context, 'mod_gradeimporter',
+                                            'submission', $submission->id);
 
     gradeimporter_check_for_zips($context, $cm, $submission);
 
-    //store updated values
+    // Store updated values.
+    // Error: write to database invalid.
     $DB->update_record('gradeimporter_submission', $submission);
 
-    //refetch complete entry
-    $submission = $DB->get_record('gradeimporter_submission', array('id'=>$submission->id));
+    // Refetch complete entry.
+    $submission = $DB->get_record('gradeimporter_submission', array('id' => $submission->id));
 
-    //trigger event and update completion (if entry was created)
+    // Trigger event and update completion (if entry was created).
     $eventparams = array(
-        'context'=>$context,
-        'objectid'=>$submission->id,
+        'context' => $context,
+        'objectid' => $submission->id,
         );
-    if ($isnewentry){
-        //update completion state
+    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);
@@ -125,7 +155,7 @@ if ($mform->is_cancelled()){
     redirect("view.php?id=$cm->id&edit=1");
 }
 
-if(!empty($id)) {
+if (!empty($id)) {
     $PAGE->navbar->add(get_string('edit'));
 }
 
@@ -133,10 +163,10 @@ $PAGE->set_title($gradeimporter->name);
 $PAGE->set_heading($course->fullname);
 echo $OUTPUT->header();
 echo $OUTPUT->heading(format_string($gradeimporter->name), 2);
-if ($gradeimporter->intro){
+if ($gradeimporter->intro) {
     echo $OUTPUT->box(format_module_intro('gradeimporter', $gradeimporter, $cm->id), 'generalbox', 'intro');
 }
 
 $mform->display();
 
-echo $OUTPUT->footer();
+echo $OUTPUT->footer();

+ 128 - 105
submission_form.php

@@ -1,115 +1,138 @@
 <?php
-    if (!defined('MOODLE_INTERNAL')) {
-        die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
-    }
+// 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.'/lib/formslib.php');
+
+// require_once($CFG->dirroot.'/course/moodleform_mod.php');
+// require_once($CFG->dirroot.'/mod/gradeimporter/lib.php');
+
+class mod_gradeimporter_submission_form extends moodleform {
+
+    public function definition() {
+        global $CFG, $DB;
+
+        $mform =& $this->_form;
+
+        $submission         = $this->_customdata['submission'];
+        $cm                 = $this->_customdata['cm'];
+        $descriptionoptions = $this->_customdata['descriptionoptions'];
+        $submissionoptions  = $this->_customdata['submissionoptions'];
+        $gradeimporterid    = $this->_customdata['gradeimporterid'];
+
+        $context = context_module::instance($cm->id);
+        $fmtoptions = array('context' => $context);
+
+        // ----------------------------------------------------------------------
+        // Form elements start here
+        $mform->addElement('header', 'general', get_string('general', 'form'));
+
+        // Submission 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', 'descriptioneditor',
+                            get_string('submissionDescription', 'gradeimporter'),
+                            null, $descriptionoptions,
+                        );
+
+        $mform->setType('descriptioneditor', PARAM_RAW);
+
+        $mform->addElement('header', 'filehandling', get_string('filehandling', 'gradeimporter'));
+
+        $url = new moodle_url('/mod/gradeimporter/submissiontype.php?id='.$gradeimporterid.'&cmid='.$cm->id.'&page=0');
+        $newsubtypecontent = '<a href='.$url.'>'.get_string('newsubtype', 'gradeimporter').'</a>';
+        /*$newsubtypecontent = '<form action="'. $url . '">'.
+                '<input type="hidden" name="id" value="'. $gradeimporter->id .'" />'.
+                '<input type="hidden" name="cmid" value="'.$cm->id.'" />'.
+                '<input type="hidden" name="page" value="0" />'.
+                '<input type="submit" Value="'.get_string('newsubtype', 'gradeimporter').'" />'.
+                '</form>';*/
+        $mform->addElement('html', $newsubtypecontent);
+
+        // Submission type
+        $submissiontyperecords = $DB->get_records('gradeimporter_submissiontype', null, 'id', 'id, name, description');
+        $submissiontyperecords = array_values($submissiontyperecords);
+        $submissiontype = array();
+
+        foreach (array_keys($submissiontyperecords) as $key) {
+            $submissiontype[$submissiontyperecords[$key]->id] = $submissiontyperecords[$key]->name;
+        }
 
-    require_once($CFG->dirroot.'/lib/formslib.php');
-
-    //require_once($CFG->dirroot.'/course/moodleform_mod.php');
-    //require_once($CFG->dirroot.'/mod/gradeimporter/lib.php');
-
-    class mod_gradeimporter_submission_form extends moodleform{
-        
-        function definition(){
-            global $CFG, $DB;
-
-            $mform =& $this->_form;
-            
-            $submission         = $this->_customdata['submission'];
-            $cm                 = $this->_customdata['cm'];
-            $descriptionoptions = $this->_customdata['descriptionoptions'];
-            $submissionoptions  = $this->_customdata['submissionoptions'];
-            $gradeimporterid    = $this->_customdata['gradeimporterid'];
-
-            $context = context_module::instance($cm->id);
-            $fmtoptions = array('context'=>$context);
-
-//----------------------------------------------------------------------
-//form elements start here
-            $mform->addElement('header', 'general', get_string('general', 'form'));
-
-            //submission 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', 'descriptioneditor', get_string('submissionDescription', 'gradeimporter'), null, $descriptionoptions);
-            $mform->setType('descriptioneditor', PARAM_RAW);
-
-
-            $mform->addElement('header', 'filehandling', get_string('filehandling', 'gradeimporter'));
-
-            $url = new moodle_url('/mod/gradeimporter/submissiontype.php?id='.$gradeimporterid.'&cmid='.$cm->id.'&page=0');
-            $newSubTypeContent = '<a href='.$url.'>'.get_string('newsubtype', 'gradeimporter').'</a>';
-            /*$newSubTypeContent = '<form action="'. $url . '">'.
-                    '<input type="hidden" name="id" value="'. $gradeimporter->id .'" />'.
-                    '<input type="hidden" name="cmid" value="'.$cm->id.'" />'.
-                    '<input type="hidden" name="page" value="0" />'.
-                    '<input type="submit" Value="'.get_string('newsubtype', 'gradeimporter').'" />'.
-                    '</form>';*/
-            $mform->addElement('html', $newSubTypeContent);
-
-            //submission type
-            $submissionTypesRecords = $DB->get_records('gradeimporter_submissiontype', null, 'id', 'id, name, description');
-            $submissionTypesRecords = array_values($submissionTypesRecords);
-            $submissionType = array();
-
-            foreach (array_keys($submissionTypesRecords) as $key){
-                $submissionType[$submissionTypesRecords[$key]->id] = $submissionTypesRecords[$key]->name;
-            }
-
-   
-            $mform->addElement('select', 'type', get_string('submissionType', 'gradeimporter'), $submissionType, array('onChange' => 'config_type(this.value);'));
-            //$mform->setDefault('submissionType', 0); // default submissionType = 3
-            $mform->addHelpButton('type', 'submissionType', 'gradeimporter');
-
-            //receive the files
-            $mform->addElement('filemanager', 'submission_filemanager', get_string('submissionFiles', 'gradeimporter'), null, $descriptionoptions);
-            $mform->addHelpButton('submission_filemanager', 'submissionFiles', 'gradeimporter');
-
-            
-
-            //csv delimiter
-            $csv_delimiter = array();
-            $csv_delimiter[1] = ',';
-            /*$csv_delimiter[2] = ';';
-            $csv_delimiter[3] = ':';
-            $csv_delimiter[4] = '\t';
-            */
-   
-            $mform->addElement('select', 'csv_delimiter', get_string('csvdelimiter', 'gradeimporter'), $csv_delimiter, array('onChange' => 'config_type(this.value);'));
-            $mform->setDefault('csv_delimiter', 1); // default csv_delimiter = 3
-            $mform->addHelpButton('csv_delimiter', 'csvdelimiter', 'gradeimporter');
-
-            
-            //gradebook Y/N Combobox
-            $ynarray = array();
-            $ynarray[0] = get_string('no', 'gradeimporter');
-            $ynarray[1] = get_string('yes', 'gradeimporter');
-            $mform->addElement('select', 'gradebook', get_string('gradebookYN', 'gradeimporter'), $ynarray);
-            //$mform->setDefault('gradebook', 0);
-            $mform->addHelpButton('gradebook', 'gradebookYN', 'gradeimporter');
-
-
-            //visibility Y/N Combobox
-            $mform->addElement('select', 'visibility', get_string('visibilityYN', 'gradeimporter'), $ynarray);
-            //$mform->setDefault('visibility', 0);
-            $mform->addHelpButton('visibility', 'visibilityYN', 'gradeimporter');
-
-        //-----------------------------
-        //hidden fields
+        $mform->addElement('select', 'type',
+                            get_string('submissiontype', 'gradeimporter'),
+                            $submissiontype,
+                            array('onChange' => 'config_type(this.value);')
+                        );
+        // $mform->setDefault('submissiontype', 0); // default submissiontype = 3
+        $mform->addHelpButton('type', 'submissiontype', 'gradeimporter');
+
+        // Receive the files
+        $mform->addElement('filemanager', 'submission_filemanager',
+                            get_string('submissionFiles', 'gradeimporter'),
+                            null, $descriptionoptions
+                        );
+
+        $mform->addHelpButton('submission_filemanager', 'submissionFiles', 'gradeimporter');
+
+        // Csv delimiter
+        $csvdelimiter = array();
+        $csvdelimiter[1] = ',';
+        /*$csvdelimiter[2] = ';';
+        $csvdelimiter[3] = ':';
+        $csvdelimiter[4] = '\t';
+        */
+
+        $mform->addElement('select', 'csvdelimiter',
+                            get_string('csvdelimiter', 'gradeimporter'),
+                            $csvdelimiter,
+                            array('onChange' => 'config_type(this.value);')
+                        );
+        $mform->setDefault('csvdelimiter', 1); // Default csvdelimiter = 3
+        $mform->addHelpButton('csvdelimiter', 'csvdelimiter', 'gradeimporter');
+
+        // Gradebook Y/N Combobox
+        $ynarray = array();
+        $ynarray[0] = get_string('no', 'gradeimporter');
+        $ynarray[1] = get_string('yes', 'gradeimporter');
+        $mform->addElement('select', 'gradebook', get_string('gradebookYN', 'gradeimporter'), $ynarray);
+        // $mform->setDefault('gradebook', 0);
+        $mform->addHelpButton('gradebook', 'gradebookYN', 'gradeimporter');
+
+        // Visibility Y/N Combobox
+        $mform->addElement('select', 'visibility', get_string('visibilityYN', 'gradeimporter'), $ynarray);
+        // $mform->setDefault('visibility', 0);
+        $mform->addHelpButton('visibility', 'visibilityYN', 'gradeimporter');
+
+        // -----------------------------
+        // Hidden fields
         $mform->addElement('hidden', 'id');
         $mform->setType('id', PARAM_INT);
         $mform->addElement('hidden', 'cmid');
         $mform->setType('cmid', PARAM_INT);
-        //------------------------------
+        // ------------------------------
 
-            //--------------------------------------------------------------
-            $this->add_action_buttons();
+        // --------------------------------------------------------------
+        $this->add_action_buttons();
 
-            //--------------------------------------------------------------
-            $this->set_data($submission);
-        }
+        // --------------------------------------------------------------
+        $this->set_data($submission);
     }
+}
 

+ 60 - 33
submissiontype.php

@@ -1,4 +1,18 @@
 <?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');
@@ -7,26 +21,26 @@ require_once('submissiontype_form.php');
 
 
 $cmid = required_param('cmid', PARAM_INT);            // Course Module ID
-$id   = required_param('id' , PARAM_INT);           //Gradeimporter id
+$id   = required_param('id' , PARAM_INT);           // Gradeimporter id
 
-if (!$cm = get_coursemodule_from_id('gradeimporter', $cmid)){
-    print_error('invalidcoursemodule');
+if (!$cm = get_coursemodule_from_id('gradeimporter', $cmid)) {
+    throw new moodle_exception('invalidcoursemodule');
 }
 
-if (!$course = $DB->get_record('course', array('id'=>$cm->course))){
-    print_error('coursemisconf');
+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))){
-    print_error('invalidid', 'gradeimporter');
+if (!$gradeimporter = $DB->get_record('gradeimporter', array('id' => $cm->instance))) {
+    throw new moodle_exception('invalidid', 'gradeimporter');
 }
 
-$url = new moodle_url('/mod/gradeimporter/submissiontype.php', array('cmid'=>$cm->id));
-if (!empty($id)){
+$url = new moodle_url('/mod/gradeimporter/submissiontype.php', array('cmid' => $cm->id));
+if (!empty($id)) {
     $url->param('id', $id);
 }
 $PAGE->set_url($url);
@@ -34,9 +48,9 @@ $PAGE->set_url($url);
 require_login($course, false, $cm);
 
 
-if ($submissiontype){
-    if (isguestuser()){
-        print_error('guestnoedit', 'gradeimporter', "$CFG->wwwroot/mod/gradeimporter/view.php?id=$cmid");
+if ($submissiontype) {
+    if (isguestuser()) {
+        throw new moodle_exception('guestnoedit', 'gradeimporter', "$CFG->wwwroot/mod/gradeimporter/view.php?id=$cmid");
     }
 } else {
 
@@ -47,45 +61,58 @@ if ($submissiontype){
 $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);
+$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 = 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));;
+$mform = new mod_gradeimporter_submissiontype_form(null, array('submissiontype' => $submissiontype,
+                                                                'cm' => $cm,
+                                                                'descriptionoptions' => $descriptionoptions,
+                                                                'submissiontypeoptions' => $submissiontypeoptions
+                                                            )
+                                                    );
 
-if ($mform->is_cancelled()){
-    if ($id){
+if ($mform->is_cancelled()) {
+    if ($id) {
         redirect("view.php?id=$cm->id&mode=entry&hook=$id");
-    } else{
+    } else {
         redirect("view.php?id=$cm->id");
     }
-} else if ($submissiontype = $mform->get_data()){
-    if (empty($submissiontype->id)){
+} else if ($submissiontype = $mform->get_data()) {
+    if (empty($submissiontype->id)) {
         $submissiontype->gradeimporterid    = $gradeimporter->id;
-        
+
         $isnewentry                         = true;
-    } else{
+    } else {
         $isnewentry                         = false;
     }
 
-    $submissiontype->description        ='';
+    $submissiontype->description        = '';
     $submissiontype->descriptionformat  = FORMAT_HTML;
     $submissiontype->definitiontrust    = 0;
-    
-    if ($isnewentry){
+
+    if ($isnewentry) {
         $submissiontype->id = $DB->insert_record('gradeimporter_submissiontype', $submissiontype);
-    } else{
+    } else {
         $DB->update_record('gradeimporter', $submissiontype);
     }
 
-    $submissiontype = file_postupdate_standard_editor($submissiontype, 'description', $descriptionoptions, $context, 'mod_gradeimporter', 'submissiontype', $submissiontype->id);
+    $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
+    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);
@@ -95,7 +122,7 @@ if ($mform->is_cancelled()){
     redirect("submission.php?id=$id&cmid=$cm->id&page=0");
 }
 
-if (!empty($id)){
+if (!empty($id)) {
     $PAGE->navbar->add(get_string('edit'));
 }
 
@@ -103,10 +130,10 @@ $PAGE->set_title($gradeimporter->name);
 $PAGE->set_heading($course->fullname);
 echo $OUTPUT->header();
 echo $OUTPUT->heading(format_string($gradeimporter->name), 2);
-if($gradeimporter->intro){
+if ($gradeimporter->intro) {
     echo $OUTPUT->box(format_module_intro('gradeimporter', $gradeimporter, $cm->id), 'generalbox', 'intro');
 }
 
 $mform->display();
 
-echo $OUTPUT->footer();
+echo $OUTPUT->footer();

+ 57 - 44
submissiontype_form.php

@@ -1,46 +1,59 @@
 <?php
-    if (!defined('MOODLE_INTERNAL')) {
-        die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
+// 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);
     }
-
-    require_once($CFG->dirroot.'/lib/formslib.php');
-
-    class mod_gradeimporter_submissiontype_form extends moodleform{
-
-        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);
-        }
-    }
+}

+ 15 - 2
version.php

@@ -1,10 +1,23 @@
 <?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();
 
-$plugin->version = 20200212;
+$plugin->version = 20200219;
 $plugin->requires = 2014021100;
 $plugin->release = 'v1.0';
 $plugin->component = 'mod_gradeimporter';
 $plugin->maturity = MATURITY_STABLE;
-

+ 129 - 110
view.php

@@ -1,76 +1,89 @@
 <?php
-    require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
-    require_once(dirname(__FILE__).'/lib.php');
-    require_once($CFG->libdir.'/filelib.php');
-    require_once('locallib.php');
-
-
-    global $DB;
-
-
-    $id = optional_param('id', 0, PARAM_INT); // course_module ID, or
-    $g  = optional_param('g', 0, PARAM_INT);  // gradeimporter instance ID, should be named as the first character of the module
-    $action = optional_param('action', 0, PARAM_INT);
-    //$cmid = optional_param('cmid', 0, PARAM_INT);
-    $id       = optional_param('id', 0, PARAM_INT);
-    $fileid     = optional_param('fileid', 0, PARAM_INT);
-    $filename   = optional_param('filename', 'a', PARAM_TEXT);
-
-    
-
-    if ($id) {
-        if (! $cm = get_coursemodule_from_id('gradeimporter', $id)) {
-            error('Course Module ID was incorrect');
-        }
-
-        if (! $course = $DB->get_record('course', array('id'=> $cm->course))) {
-            error('Course is misconfigured');
-        }
-
-        if (! $gradeimporter = $DB->get_record('gradeimporter', array('id'=> $cm->instance) ) ) {
-            error('Course module is incorrect');
-        }
+// 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(dirname(dirname(dirname(__FILE__))).'/config.php');
+require_once(dirname(__FILE__).'/lib.php');
+require_once($CFG->libdir.'/filelib.php');
+require_once('locallib.php');
+
+
+  global $DB;
+
+
+  $id = optional_param('id', 0, PARAM_INT); // Course_module ID.
+  $g  = optional_param('g', 0, PARAM_INT);  // Gradeimporter instance ID, should be named as the first character of the module.
+  $action = optional_param('action', 0, PARAM_INT);
+  // $cmid = optional_param('cmid', 0, PARAM_INT);
+  $id       = optional_param('id', 0, PARAM_INT);
+  $fileid     = optional_param('fileid', 0, PARAM_INT);
+  $filename   = optional_param('filename', 'a', PARAM_TEXT);
+
+
+
+if ($id) {
+    if (! $cm = get_coursemodule_from_id('gradeimporter', $id)) {
+        error('Course Module ID was incorrect');
+    }
 
-    } else if ($g) {
-        if (! $gradeimporter = $DB->get_record('gradeimporter', array('id'=> $g)) ){
-            error('Course module is incorrect');
-        }
-        if (! $course = $DB->get_record('course', array('id'=> $gradeimporter->course)) ) {
-            error('Course is misconfigured');
-        }
-        if (! $cm = get_coursemodule_from_instance('gradeimporter', $gradeimporter->id, $course->id)) {
-            error('Course Module ID was incorrect');
-        }
+    if (! $course = $DB->get_record('course', array('id' => $cm->course))) {
+        error('Course is misconfigured');
+    }
 
-    } else {
-        error('You must specify a course_module ID or an instance ID');
+    if (! $gradeimporter = $DB->get_record('gradeimporter', array('id' => $cm->instance))) {
+        error('Course module is incorrect');
+    }
+} else if ($g) {
+    if (! $gradeimporter = $DB->get_record('gradeimporter', array('id' => $g))) {
+        error('Course module is incorrect');
+    }
+    if (! $course = $DB->get_record('course', array('id' => $gradeimporter->course))) {
+        error('Course is misconfigured');
+    }
+    if (! $cm = get_coursemodule_from_instance('gradeimporter', $gradeimporter->id, $course->id)) {
+        error('Course Module ID was incorrect');
     }
+} else {
+    error('You must specify a course_module ID or an instance ID');
+}
 
-    require_login($course, true, $cm);
+require_login($course, true, $cm);
 
-    $context = context_module::instance($cm->id);
+$context = context_module::instance($cm->id);
 
 
 
-    if ($action==1 && has_capability('mod/gradeimporter:view', $context)){
-      //download feedback file
-      $fs = get_file_storage();
-      $file = $fs->get_file($context->id, 'mod_gradeimporter', 'gradeimporter_feedback', $fileid, '/', $filename);
-      if ($file){
+if ($action == 1 && has_capability('mod/gradeimporter:view', $context)) {
+    // Download feedback file.
+    $fs = get_file_storage();
+    $file = $fs->get_file($context->id, 'mod_gradeimporter', 'gradeimporter_feedback', $fileid, '/', $filename);
+    if ($file) {
         send_stored_file($file, 86400, 0, true);
-      }
-    } else if ($action == 2 && has_capability('mod/gradeimporter:edit', $context)){
-      //download students csv with their id
-      exportCSV($context);
     }
+} else if ($action == 2 && has_capability('mod/gradeimporter:edit', $context)) {
+    // Download students csv with their id.
+    exportCSV($context);
+}
 
 
-    /// Print the page header
-    $PAGE->set_cm($cm);
-    $PAGE->set_url('/mod/gradeimporter/view.php', array('id' => $cm->id));
-    $PAGE->set_title(format_string($gradeimporter->name));
-    $PAGE->set_heading(format_string($course->fullname));
-    $PAGE->set_context($context);
+// Print the page header.
+$PAGE->set_cm($cm);
+$PAGE->set_url('/mod/gradeimporter/view.php', array('id' => $cm->id));
+$PAGE->set_title(format_string($gradeimporter->name));
+$PAGE->set_heading(format_string($course->fullname));
+$PAGE->set_context($context);
 
 $output = $PAGE->get_renderer('mod_folder');
 echo $output->header();
@@ -80,80 +93,86 @@ $heading = get_string('displayingview', 'gradeimporter', $gradeimporter->name);
 echo $output->heading($heading);
 
 
-//button to add new submission
+// Button to add new submission.
 if (has_capability('mod/gradeimporter:edit', $context)) {
-  $url = new moodle_url('/mod/gradeimporter/submission.php');
-  $newbutton = '<form action="'. $url . '">'.
+    $url = new moodle_url('/mod/gradeimporter/submission.php');
+    $newbutton = '<form action="'. $url . '">'.
           '<input type="hidden" name="id" value="'. $gradeimporter->id .'" />'.
           '<input type="hidden" name="cmid" value="'.$cm->id.'" />'.
           '<input type="hidden" name="page" value="0" />'.
           '<input type="submit" Value="'.get_string('newsubmission', 'gradeimporter').'" />'.
           '</form>';
-  echo $newbutton;
+    echo $newbutton;
 
-  $url = new moodle_url("/mod/gradeimporter/view.php", array('id'=>$id, 'cmid'=>$cm->id, 'action'=>2));
-  $newbutton = '<form action="'. $url . '">'.
+    $url = new moodle_url("/mod/gradeimporter/view.php", array('id' => $id, 'cmid' => $cm->id, 'action' => 2));
+    $newbutton = '<form action="'. $url . '">'.
         '<input type="hidden" name="id" value="'. $id .'" />'.
         '<input type="hidden" name="cmid" value="'.$cm->id.'" />'.
         '<input type="hidden" name="action" value="2" />'.
         '<input type="submit" Value="'.get_string('downloadconfigcsv', 'gradeimporter').'" />'.
         '</form>';
-  echo $newbutton;
+    echo $newbutton;
 }
 
-//tabela com as notas vem aqui
+// Tabela com as notas vem aqui.
 
 require_once($CFG->libdir . '/tablelib.php');
 require_once(dirname(__FILE__).'/locallib.php');
 
 
 
-if (has_capability('mod/gradeimporter:edit', $context)){
-  //loads teacher view
-  
-  $enrolledusers = get_enrolled_users ($context, 'mod/gradeimporter:view', 0, 'u.id, u.firstname, u.lastname', 'u.firstname, u.lastname');
+if (has_capability('mod/gradeimporter:edit', $context)) {
+    // Loads teacher view.
+
+    $enrolledusers = get_enrolled_users($context, 'mod/gradeimporter:view', 0, 'u.id, u.firstname, u.lastname', 'u.firstname, u.lastname');
 
-  get_teacher_view($cm->id, $gradeimporter->id, $enrolledusers);
-  
+    get_teacher_view($cm->id, $gradeimporter->id, $enrolledusers);
 } else {
-  $data = get_comments($cm->id, $id);
-
-  //loads student view
-  $table = new html_table();
-  $table->attributes['class'] = 'generaltable mod_index';
-  $table->head  = array (get_string('type', 'gradeimporter'), get_string('submission', 'gradeimporter'), get_string('grade', 'gradeimporter'), get_string('comment', 'gradeimporter'), get_string('file', 'gradeimporter'));
-  $table->align = array ('center', 'center', 'center', 'center', 'center');
-
-  if (count($data)) {
-    $current = "";
-    foreach ($data as $tipo => $cells) {
-      if ($current != "" && $current != $tipo) {
-        $row = new html_table_row();
-        $cell = new html_table_cell();
-        $cell->colspan = 5;
-        $row->cells[] = $cell;
-        $table->data[] = $row;
-      }
-      $current = $tipo;
-
-      $row = new html_table_row();
-      $cell = new html_table_cell($tipo);
-      $cell->rowspan = count($cells)+1;
-      $row->cells[] = $cell;
-      $table->data[] = $row;
-      foreach ($cells as $cell) {
-        $row = new html_table_row();
-        foreach ($cell as $value) {
-          $cell = new html_table_cell($value);
-          $row->cells[] = $cell;
+    $data = get_comments($cm->id, $id);
+
+    // Loads student view.
+    $table = new html_table();
+    $table->attributes['class'] = 'generaltable mod_index';
+    $table->head  = array(get_string('type', 'gradeimporter'),
+        get_string('submission', 'gradeimporter'),
+        get_string('grade', 'gradeimporter'),
+        get_string('comment', 'gradeimporter'),
+        get_string('file', 'gradeimporter'));
+    $table->align = array('center', 'center', 'center', 'center', 'center');
+
+    if (count($data)) {
+        $current = "";
+        foreach ($data as $tipo => $cells) {
+            if ($current != "" && $current != $tipo) {
+                $row = new html_table_row();
+                $cell = new html_table_cell();
+                $cell->colspan = 5;
+                $row->cells[] = $cell;
+                $table->data[] = $row;
+            }
+            $current = $tipo;
+
+            $row = new html_table_row();
+            $cell = new html_table_cell($tipo);
+            $cell->rowspan = count($cells) + 1;
+            $row->cells[] = $cell;
+            $table->data[] = $row;
+            foreach ($cells as $cell) {
+                $row = new html_table_row();
+                foreach ($cell as $value) {
+                    $cell = new html_table_cell($value);
+                    $row->cells[] = $cell;
+                }
+                $table->data[] = $row;
+            }
         }
-        $table->data[] = $row;
-      }
     }
-  }
-  echo html_writer::table($table);
+    echo html_writer::table($table);
 }
 
+$PAGE->requires->js_call_amd('mod_gradeimporter/helloworld', 'helloworld');
+$PAGE->requires->js_call_amd('mod_gradeimporter/hello', 'informal', array('marquinho'));
+
 
-/// Finish the page
-echo $output->footer();
+// Finishes the page!
+echo $output->footer();