2 Revize f65f0a81a5 ... fa8449143b

Autor SHA1 Zpráva Datum
  Bernardo fa8449143b Add edit mode for gradeimporter: před 2 roky
  Bernardo 8c2d529a42 Added lib function gradeimporter_reset_gradebook před 2 roky

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+.vscode/
+old_files/
+tests/

+ 76 - 0
forms/submission/delete.php

@@ -0,0 +1,76 @@
+<?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('submission_functions.php');
+
+// Get Params to correctly load page
+$cmid = required_param('cmid', PARAM_INT);
+$subid = required_param('subid', PARAM_INT);
+$gradeimporterid = optional_param('id', 0, PARAM_INT);
+
+// Get course module and $course to check capability
+$cm = get_coursemodule_from_id('gradeimporter', $cmid, 0, false, MUST_EXIST);
+$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
+
+// Gets context to check capability
+$context = context_module::instance($cm->id);
+
+// Sets Page URL
+$url = new moodle_url('/mod/gradeimporter/forms/submission/delete.php', array('cmid' => $cm->id, 'subid' => $subid));
+if (!empty($gradeimporterid)) {
+  $url->param('id', $gradeimporterid);
+}
+$PAGE->set_url($url);
+
+// Check if its teacher opening the page
+require_login ($course, false, $cm);
+require_capability('mod/gradeimporter:edit', $context);
+
+// Get globals
+global $DB;
+
+// Get submission
+$submission = $DB->get_record('gradeimporter_submission', array('id' => $subid, 'gradeimporterid' => $gradeimporterid));
+// If submission doesn't exist, throw error
+if (!$submission) {
+  throw new moodle_exception(get_string('submissionnotfound', 'gradeimporter', $subid));
+}
+
+// Get feedbacks associated with it
+$feedbacks = $DB->get_records('gradeimporter_feedback', array('submissionid' => $subid));
+foreach ($feedbacks as $feedback) {
+  // For each feedback, deletes its files and then delete it
+  delete_feedback_files($feedback);
+  $DB->delete_records('gradeimporter_feedback', array('id' => $feedback->id));
+}
+// Delete submission
+$DB->delete_records('gradeimporter_submission', array('id' => $submission->id));
+
+// Print page and redirects to submission page after 5 seconds
+$PAGE->set_title($submission->name);
+$PAGE->set_heading($course->fullname);
+$PAGE->set_context($context);
+
+$viewlink = new moodle_url('CFG->dirroot/mod/gradeimporter/view.php', array('id' => $cmid, 'edit' => 1));
+
+echo $OUTPUT->header("refresh:5;url=../../view.php?id=$cmid&edit=1");
+
+echo "<p>".get_string('deletesubmissiondata', 'gradeimporter', $submission->name)."</p>";
+echo "<p>".get_string('deletefeedbacksdata', 'gradeimporter', count($feedbacks))."</p>";
+echo "<p>".get_string('redirect5s', 'gradeimporter')."<a href=$viewlink>".get_string('here', 'gradeimporter')."</p>";
+
+echo $OUTPUT->footer();

+ 1 - 1
forms/submission/submission.php

@@ -19,7 +19,7 @@ require_once('../../locallib.php');
 require_once('../../lib.php');
 
 require_once('submission_form.php'); // Requires Form class File
-require_once('submission_form_functions.php'); // Require functions file for submission form
+require_once('submission_functions.php'); // Require functions file for submission form
 
 $cmid = required_param('cmid', PARAM_INT);      // Course Module ID.
 $id = optional_param('id', 0, PARAM_INT);       // Gradeimporter id.

+ 1 - 1
forms/submission/submission_form.php

@@ -18,7 +18,7 @@ defined('MOODLE_INTERNAL') || die();
 
 require_once("$CFG->dirroot/course/moodleform_mod.php");
 require_once("$CFG->libdir/formslib.php");
-require_once("submission_form_functions.php");
+require_once("submission_functions.php");
 
 class mod_gradeimporter_submission_form extends moodleform {
 

forms/submission/submission_form_functions.php → forms/submission/submission_functions.php


+ 13 - 1
lang/en/gradeimporter.php

@@ -32,6 +32,8 @@ $string['invalidgradeimporterid'] = 'Invalid Grade Importer ID';
 $string['submissionadded'] = 'Submission added to DB';
 $string['newsubmission'] = 'Add new submission';
 
+$string['editionmode'] = 'Turn editing on';
+
 // FormStrings
 
     // Generic
@@ -84,12 +86,15 @@ $string['newsubmission'] = 'Add new submission';
     // Submission form errors
 
 
-// Submission type
+    // Submission type
     $string['newsubtype'] = 'Add new submission type';
 
     // Submission type help
     $string['newsubtype_help'] = 'Goes to new sub type form (sub type examples: Exam, Test, Activity)';
 
+    // Submission on view
+
+
 // Events string
     $string['eventmodelsubmissioncreated'] = 'Submission created';
 
@@ -105,6 +110,13 @@ $string['file']         = 'File';
 $string['nameCol']      = 'Name';
 $string['nameColTitle'] = 'Fullname of all students enrolled in this course';
 $string['editSub']      = 'Edit submission {$a}';
+$string['deletesubmission'] = 'Delete submission {$a}';
+$string['submissionnotfound'] = 'Submission with id {$a} not found';
+$string['deletesubmissiondata'] = 'Succeeded to delete submission {$a}';
+$string['deletefeedbacksdata'] = 'Succeeded to delete {$a} feedbacks';
+$string['redirect5s'] = 'Redirecting in 5 seconds, or click';
+$string['here'] = 'here';
+
 
 // Access Errors
     $string['guestnoedit'] = 'Guests can\'t edit forms';

+ 10 - 0
lib.php

@@ -220,7 +220,17 @@ function gradeimporter_update_grades ($submission, $userid = 0, $nullifnone = tr
 
 function gradeimporter_reset_gradebook ($courseid, $type = '') {
   global $CFG, $DB;
+  $tp = $CFG->prefix;
   // Select all grades on gradebook and reset them
+  $sql = "SELECT gs.*
+          FROM {$tp}gradeimporter_submission as gs
+          JOIN {$tp}gradeimporter as g ON g.id = gs.gradeimporterid
+          WHERE g.course = $courseid";
+
+  $submissions = $DB->get_records_sql($sql);
+  foreach ($submissions as $submission) {
+    gradeimporter_grade_item_update($submission, 'reset');
+  }
 }
 
 function gradeimporter_get_user_grades($submission, $userid) {

+ 26 - 0
libs/locallib.php

@@ -0,0 +1,26 @@
+<?php
+// This file is part of
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+
+function get_editing_button ($id, $edit) {
+  $options = array('id' => $id);
+  if (!$edit) {
+    $options['edit'] = 1;
+  }
+  $string = $edit ? get_string('turneditingoff') : get_string('turneditingon');
+
+  return new single_button(new moodle_url('view.php', $options), $string, 'get');
+}

+ 37 - 11
libs/teacher_viewlib.php

@@ -25,16 +25,18 @@ class Teacherview {
   private $context;
   private $cmid;
   private $cellstyle = "border:1px solid black; text-align:center";
+  private $edit = 0;
 
   /**
    * @param int cmid to get context
    * @param int gradeimporterid to query database
    * constructor
    */
-  public function __construct (int $cmid, int $gradeimporterid) {
+  public function __construct (int $cmid, int $gradeimporterid, int $edit) {
     $this->gradeimporterid = $gradeimporterid;
     $this->cmid = $cmid;
     $this->context = context_module::instance($cmid);
+    $this->edit = $edit;
   }
 
   private function get_gradeimporterid () {
@@ -50,6 +52,10 @@ class Teacherview {
     return "$this->cellstyle $extrastyle";
   }
 
+  private function get_edit () {
+    return $this->edit;
+  }
+
   /**
    * builds teacherview table using php html_table class
    * Gets all submissions from get_submissions()
@@ -224,19 +230,39 @@ class Teacherview {
   }
 
   private function subcelltext ($submission) {
-    $url = new moodle_url("/mod/gradeimporter/forms/submission/submission.php", array('id' => $this->get_gradeimporterid(),
+
+    // Base celltext, shown when editing or not
+    $celltext = "<a title='$submission->intro'>
+                    $submission->name
+                </a>";
+
+    // If is editing submission, add a edit icons
+    if ($this->get_edit()) {
+
+      // Build edit link icon + url
+      $editurl = 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', $submission->name);
-    return "<a title='$submission->intro'>
-              $submission->name
-            </a>
-            <a href=\"$url\" target=\"_blank\">
-              <i class = \"icon fa fa-pencil fa-fw\" title=\"$editlinktitle\"
-              aria-label=\"$editlinktitle\">
-              </i>
-            </a>";
+      $editlinktitle = get_string('editSub', 'gradeimporter', $submission->name);
+      $celltext .= "  <a href=\"$editurl\">
+                        <i class = \"icon fa fa-pencil fa-fw\" title=\"$editlinktitle\" aria-label=\"$editlinktitle\">
+                        </i>
+                      </a>";
+
+      // Build delete link icon + url
+      $deleteurl = new moodle_url("/mod/gradeimporter/forms/submission/delete.php", array('id' => $this->get_gradeimporterid(),
+                                                                                      'cmid' => $this->get_cmid(),
+                                                                                      'subid' => $submission->id)
+                                                                                    );
+      $deletelinktitle = get_string('deletesubmission', 'gradeimporter', $submission->name);
+      $celltext .= "<a href=\"$deleteurl\">
+                      <i class=\"fa fa-trash\" title=\"$deletelinktitle\" aria-label=\"$deletelinktitle\">
+                      </i>
+                    </a>";
+    }
+    // Full celltext, with edit and delete icons if teacher is editing
+    return $celltext;
   }
 
 }

+ 6 - 2
view.php

@@ -20,6 +20,7 @@ require_once($CFG->libdir.'/filelib.php');
 require_once('locallib.php');
 require_once('libs/student_viewlib.php');
 require_once('libs/teacher_viewlib.php');
+require_once('libs/locallib.php');
 
 global $DB;
 $id = optional_param('id', 0, PARAM_INT); // Course_module ID.
@@ -27,6 +28,7 @@ $g  = optional_param('g', 0, PARAM_INT);  // Gradeimporter instance ID, should b
 $action = optional_param('action', 0, PARAM_INT);
 $fileid   = optional_param('fileid', 0, PARAM_INT);
 $filename   = optional_param('filename', 'a', PARAM_TEXT);
+$edit = optional_param('edit', 0, PARAM_INT);
 
 
 
@@ -76,12 +78,13 @@ if ($action == 1 && has_capability('mod/gradeimporter:view', $context)) {
   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));
+$button = $OUTPUT->render(get_editing_button($id, $edit));
+$PAGE->set_button($button);
 $PAGE->set_context($context);
 
 $output = $PAGE->get_renderer('mod_folder');
@@ -110,6 +113,7 @@ if (has_capability('mod/gradeimporter:edit', $context)) {
     '<input type="submit" Value="'.get_string('downloadconfigcsv', 'gradeimporter').'" />'.
     '</form>';
   echo $newbutton;
+
 }
 
 // Tabela com as notas vem aqui.
@@ -120,7 +124,7 @@ require_once(dirname(__FILE__).'/locallib.php');
 if (has_capability('mod/gradeimporter:edit', $context)) {
   // Loads teacher view.
   // get_teacher_view($cm->id, $gradeimporter->id);
-  $teacherview = new Teacherview($cm->id, $gradeimporter->id);
+  $teacherview = new Teacherview($cm->id, $gradeimporter->id, $edit);
   $teachertable = $teacherview->make_table();
   echo $teachertable;
 } else {