<?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));

// Delete its feedbacks
foreach ($feedbacks as $feedback) {
  // Deletes feedback from gradeimporter_feedback table and removes its associated files
  delete_feedback($feedback);
  // Recalculates gradeimporter instance grade without this feedback
  gradeimporter_update_grades($submission, $feedback->studentid);
}

// 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("/mod/gradeimporter/view.php", array('id' => $cmid, 'edit' => 1));

echo $OUTPUT->header("refresh:5;url=$viewlink");

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();