Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
 
23
require_once("$CFG->dirroot/mod/assign/locallib.php");
24
 
25
/**
26
 * Extend the base external_api class with mod_assign utility methods.
27
 *
28
 * @package    mod_assign
29
 * @author     Andrew Madden <andrewmadden@catalyst-au.net>
30
 * @copyright  2021 Catalyst IT
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class external_api extends \core_external\external_api {
34
 
35
    /**
36
     * Generate a warning in a standard structure for a known failure.
37
     *
38
     * @param int $assignmentid - The assignment
39
     * @param string $warningcode - The key for the warning message
40
     * @param string $detail - A description of the error
41
     * @return array - Warning structure containing item, itemid, warningcode, message
42
     */
43
    protected static function generate_warning(int $assignmentid, string $warningcode, string $detail): array {
44
        $warningmessages = [
45
            'couldnotlock' => 'Could not lock the submission for this user.',
46
            'couldnotunlock' => 'Could not unlock the submission for this user.',
47
            'couldnotsubmitforgrading' => 'Could not submit assignment for grading.',
48
            'couldnotrevealidentities' => 'Could not reveal identities.',
49
            'couldnotgrantextensions' => 'Could not grant submission date extensions.',
50
            'couldnotrevert' => 'Could not revert submission to draft.',
51
            'invalidparameters' => 'Invalid parameters.',
52
            'couldnotsavesubmission' => 'Could not save submission.',
53
            'couldnotsavegrade' => 'Could not save grade.',
54
            'couldnotstartsubmission' => 'Could not start submission with time limit.',
55
            'submissionnotopen' => 'This assignment is not open for submissions',
56
            'timelimitnotenabled' => 'Time limit is not enabled for assignment.',
57
            'opensubmissionexists' => 'Open assignment submission already exists.',
1441 ariadna 58
            'couldnotremovesubmission' => 'Could not remove the submission for this user',
59
            'submissionnotfoundtoremove' => 'There is no submission to remove',
1 efrain 60
        ];
61
 
62
        $message = $warningmessages[$warningcode];
63
        if (empty($message)) {
64
            $message = 'Unknown warning type.';
65
        }
66
 
67
        return [
68
            'item' => s($detail),
69
            'itemid' => $assignmentid,
70
            'warningcode' => $warningcode,
71
            'message' => $message,
72
        ];
73
    }
74
 
75
    /**
76
     * Utility function for validating an assign.
77
     *
78
     * @param int $assignid assign instance id
79
     * @return array array containing the assign, course, context and course module objects
80
     * @since  Moodle 3.2
81
     */
82
    protected static function validate_assign(int $assignid): array {
83
        global $DB;
84
 
85
        // Request and permission validation.
86
        $assign = $DB->get_record('assign', ['id' => $assignid], 'id', MUST_EXIST);
87
        list($course, $cm) = get_course_and_cm_from_instance($assign, 'assign');
88
 
89
        $context = \context_module::instance($cm->id);
90
        // Please, note that is not required to check mod/assign:view because is done by validate_context->require_login.
91
        self::validate_context($context);
92
        $assign = new \assign($context, $cm, $course);
93
 
94
        return [$assign, $course, $cm, $context];
95
    }
96
 
97
    /**
98
     * Get a submission from an assignment for a user. Encapsulates checking whether it's a solo or team submission.
99
     *
100
     * @param \assign $assignment Assignment object.
101
     * @param int|null $userid User id.
102
     * @param int $groupid Group id.
103
     * @param bool $create Whether a new submission should be created.
104
     * @param int $attemptnumber Attempt number. Use -1 for last attempt.
105
     * @return bool|\stdClass
106
     */
1441 ariadna 107
    protected static function get_user_or_group_submission(\assign $assignment, ?int $userid = null,
1 efrain 108
            int $groupid = 0, bool $create = false, int $attemptnumber = -1) {
109
        if ($assignment->get_instance($userid)->teamsubmission) {
110
            $submission = $assignment->get_group_submission($userid, $groupid, $create, $attemptnumber);
111
        } else {
112
            $submission = $assignment->get_user_submission($userid, $create, $attemptnumber);
113
        }
114
        return $submission;
115
    }
116
}