Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace mod_assign\external;
18
use core_external\external_function_parameters;
19
use core_external\external_single_structure;
20
use core_external\external_value;
21
use core_external\external_warnings;
22
 
23
/**
24
 * External function to remove an assignment submission.
25
 *
26
 * @package     mod_assign
27
 * @category    external
28
 *
29
 * @copyright   2024 Daniel Ureña <durenadev@gmail.com>
30
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 * @since       Moodle 4.5
32
 */
33
class remove_submission extends external_api {
34
    /**
35
     * Describes the parameters for remove submission.
36
     *
37
     * @return external_function_parameters
38
     */
39
    public static function execute_parameters(): external_function_parameters {
40
        return new external_function_parameters([
41
                'userid' => new external_value(PARAM_INT, 'User id'),
42
                'assignid' => new external_value(PARAM_INT, 'Assignment instance id'),
43
            ]);
44
    }
45
 
46
    /**
47
     * Call to remove submission.
48
     *
49
     * @param int $userid User id to remove submission
50
     * @param int $assignid The id of the assignment
51
     * @return array
52
     */
53
    public static function execute(int $userid, int $assignid): array {
54
        // Initialize return variables.
55
        $warnings = [];
56
        $result   = [];
57
        $status   = false;
58
 
59
        [
60
            'userid' => $userid,
61
            'assignid'  => $assignid
62
        ] = self::validate_parameters(self::execute_parameters(), [
63
            'userid' => $userid,
64
            'assignid'  => $assignid,
65
        ]);
66
 
67
        // Validate and get the assign.
68
        [$assign, $course, $cm, $context] = self::validate_assign($assignid);
69
 
70
        // Get submission.
71
        $submission = $assign->get_user_submission($userid, false);
72
        if (
73
            !$submission ||
74
            $submission->status == ASSIGN_SUBMISSION_STATUS_NEW ||
75
            $submission->status == ASSIGN_SUBMISSION_STATUS_REOPENED
76
        ) {
77
            // No submission to remove.
78
            $warnings[] = self::generate_warning($assignid, 'submissionnotfoundtoremove', 'assign');
79
            return [
80
                'status'    => $status,
81
                'warnings' => $warnings,
82
            ];
83
        }
84
 
85
        if (!$status = $assign->remove_submission($userid)) {
86
            $errors = $assign->get_error_messages();
87
            foreach ($errors as $errormsg) {
88
                $warnings[] = self::generate_warning($assignid, 'couldnotremovesubmission', $errormsg);
89
            }
90
        }
91
        $result['status']   = $status;
92
        $result['warnings'] = $warnings;
93
        return $result;
94
    }
95
 
96
    /**
97
     * Describes the remove submissions return value.
98
     *
99
     * @return external_single_structure
100
     */
101
    public static function execute_returns(): external_single_structure {
102
        return new external_single_structure([
103
            'status' => new external_value(PARAM_BOOL, 'True if the submission was successfully removed and false if was not.'),
104
            'warnings' => new external_warnings(),
105
        ]);
106
    }
107
}