Explorar o código

Teacher can edit a full submission
- Clicking on the pencil besides submission name opens submission.php with fields filled with correct info
- File field can't be filled because files are deleted after used
- When updated replaces all feedbacks that are for the same student
- If new feedback has no file associated but old feedback had, old file is deleted
- If both new feedback and old file have files associated, old file is deleted in favor of the new one
- If new feedback has file and old one doesn't, new file is recorded as normal

Bernardo %!s(int64=2) %!d(string=hai) anos
pai
achega
1dc75d2919

+ 51 - 20
forms/submission/submission_form_functions.php

@@ -90,10 +90,7 @@ function read_csv ($content, $context, $data, $zipfiles = null) {
           break;
         }
       }
-      // Already used all files, so delete extra copies
-
     }
-
   }
 }
 
@@ -117,32 +114,43 @@ function prepare_csv ($content) {
 }
 
 function store_feedback ($feedback, $contextid, $submissionid) {
-  global $DB;
+  global $DB, $USER;
   // Prepare data to submit to gradeimporter_feedback table
-  $entry = new stdClass();
-  $entry->id = null;
+  $entry = feedback_exists($feedback, $submissionid);
+  $isnewentry = false;
+  if (!$entry) {
+    $entry = new stdClass();
+    $entry->id = null;
+    $entry->timecreated = time();
+    $entry->submissionid = $submissionid;
+    $entry->studentid = $feedback['id'];
+    $entry->contextid = $contextid;
+    $isnewentry = true;
+  } else {
+    // If already had a feedback
+    // Deletes its files and remove its entry on DB
+    delete_feedback_files($entry);
+    $DB->delete_records('gradeimporter_feedback',
+                          array('submissionid' => $submissionid,
+                                'studentid' => $feedback['id'])
+                        );
+  }
 
-  $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);
+  $entry->fileid = 0; // To be removed fileid is now feedbackid
+  $entry->usermodified = $USER->id;
 
-  return $entry->id;
+  // If is not a new feedback update previous record and return id
+  return $DB->insert_record('gradeimporter_feedback', $entry);
 }
 
 function store_feedback_file ($feedback, $context, $submission, $filetostore, $feedbackid) {
   // Prepare file
   $fs = get_file_storage();
+  // Create file information needed
   $fileinfo = array(
     'contextid' => $context->id,
     'component' => 'mod_gradeimporter',
@@ -153,9 +161,8 @@ function store_feedback_file ($feedback, $context, $submission, $filetostore, $f
     'timecreated' => time(), 'timemodified' => time()
   );
 
-  $fileinfo = $fs->create_file_from_storedfile($fileinfo, $filetostore);
-
-  return $fileinfo;
+  // store files in correct area
+  return $fs->create_file_from_storedfile($fileinfo, $filetostore);
 }
 
 function read_zip ($file, $context, $fs) {
@@ -202,3 +209,27 @@ function validate_formdata($data) {
     throw new moodle_exception(get_string('invalidtype', 'gradeimporter'));
   }
 }
+
+function feedback_exists($feedback, $submissionid) {
+  global $DB;
+  $feedback = $DB->get_record('gradeimporter_feedback',
+                              array('submissionid' => $submissionid,
+                                      'studentid' => $feedback['id']),
+                              );
+  return $feedback;
+}
+
+/**
+ * Searches if a feedback has a file when updating it
+ * If already has file then delete it to open space for a new file
+ * Or if updated feedback doesnt have associated file
+ * @param $feedback - record of already submited feedback
+ * @return void
+ */
+function delete_feedback_files($feedback) {
+  $fs = get_file_storage();
+  $file = $fs->get_file($feedback->contextid, 'mod_gradeimporter', 'submissionfiles', $feedback->id, '/', $feedback->name);
+  if ($file) {
+    $file->delete();
+  }
+}

+ 1 - 0
lang/en/gradeimporter.php

@@ -103,6 +103,7 @@ $string['comment']      = 'Comment';
 $string['grade']        = 'Grade';
 $string['file']         = 'File';
 $string['nameCol']      = 'Name';
+$string['nameColTitle'] = 'Fullname of all students enrolled in this course';
 $string['editSub']      = "Edit this submission";
 
 // Access Errors

+ 27 - 11
libs/teacher_viewlib.php

@@ -23,6 +23,7 @@ class Teacherview {
    */
   private $gradeimporterid;
   private $context;
+  private $cmid;
   private $cellstyle = "border:1px solid black; text-align:center";
 
   /**
@@ -32,17 +33,21 @@ class Teacherview {
    */
   public function __construct (int $cmid, int $gradeimporterid) {
     $this->gradeimporterid = $gradeimporterid;
+    $this->cmid = $cmid;
     $this->context = context_module::instance($cmid);
   }
 
   private function get_gradeimporterid () {
     return $this->gradeimporterid;
   }
+  private function get_cmid () {
+    return $this->cmid;
+  }
   private function get_context () {
     return $this->context;
   }
-  private function get_cellstyle () {
-    return $this->cellstyle;
+  private function get_cellstyle ($extrastyle = "") {
+    return "$this->cellstyle $extrastyle";
   }
 
   /**
@@ -110,22 +115,21 @@ class Teacherview {
     $header = new html_table_row();
 
     // Add name header to header row
-    $header->cells[] = $this->make_cell(get_string('nameCol', 'gradeimporter'), 1);
+    $title = get_string('nameColTitle', 'gradeimporter');
+    $text = get_string('nameCol', 'gradeimporter');
+    $celltext = "<a title = \"$title\"> $text </a>";
+    $header->cells[] = $this->make_cell($celltext, 1);
 
     foreach ($submissions as $submission) {
       // Add a icon to redirect to submission form to edit the sub
-      $header->cells[] = $this->make_cell($submission->name, 2);
+      $celltext = $this->subcelltext($submission);
+      $header->cells[] = $this->make_cell($celltext, 2);
     }
     return $header;
   }
 
   /**
-   * Build a row for a student defined by param $student
-   * @param array $student, array with keys "name" and "id"
-   * @param $submissions, submissions list to find each submission for this student
-   * For each submission at $submissions tries do fetch feedback from DB:
-   * * If submission is found adds grade and link to feedback file to row
-   * * If doesn't have the submission sets grade and link to " - "
+   * Build a row for a student defined by paramcmide and link to " - "
    * @return html_table_row $row with each column being grade and feedbackfile for the submission
    */
   private function get_studentsubmissions ($student, $submissions) {
@@ -169,7 +173,7 @@ class Teacherview {
     $tp = $CFG->prefix;
 
     // Build Query
-    $sql = "SELECT id, name, type
+    $sql = "SELECT id, name, type, description
         FROM {$tp}gradeimporter_submission
         WHERE gradeimporterid ={$this->get_gradeimporterid()}
         ORDER BY type, id";
@@ -218,4 +222,16 @@ class Teacherview {
     $cell->colspan = $colspan;
     return $cell;
   }
+
+  private function subcelltext ($submission) {
+    $url = new moodle_url("/mod/gradeimporter/forms/submission/submission.php", array('id' => $this->get_gradeimporterid(),
+                                                                                      'cmid' => $this->get_cmid(),
+                                                                                      'subid' => $submission->id)
+                                                                                    );
+    $editlinktitle = get_string('editSub', 'gradeimporter');
+    return "<a title=\"$submission->description\">$submission->name</a>
+            <a href=\"$url\" target=\"_blank\"><i class = \"icon fa fa-pencil fa-fw\" title=\"$editlinktitle\"
+              aria-label=\"$editlinktitle\"></i>";
+  }
+
 }