Proyectos de Subversion Moodle

Rev

| 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.',
58
        ];
59
 
60
        $message = $warningmessages[$warningcode];
61
        if (empty($message)) {
62
            $message = 'Unknown warning type.';
63
        }
64
 
65
        return [
66
            'item' => s($detail),
67
            'itemid' => $assignmentid,
68
            'warningcode' => $warningcode,
69
            'message' => $message,
70
        ];
71
    }
72
 
73
    /**
74
     * Utility function for validating an assign.
75
     *
76
     * @param int $assignid assign instance id
77
     * @return array array containing the assign, course, context and course module objects
78
     * @since  Moodle 3.2
79
     */
80
    protected static function validate_assign(int $assignid): array {
81
        global $DB;
82
 
83
        // Request and permission validation.
84
        $assign = $DB->get_record('assign', ['id' => $assignid], 'id', MUST_EXIST);
85
        list($course, $cm) = get_course_and_cm_from_instance($assign, 'assign');
86
 
87
        $context = \context_module::instance($cm->id);
88
        // Please, note that is not required to check mod/assign:view because is done by validate_context->require_login.
89
        self::validate_context($context);
90
        $assign = new \assign($context, $cm, $course);
91
 
92
        return [$assign, $course, $cm, $context];
93
    }
94
 
95
    /**
96
     * Get a submission from an assignment for a user. Encapsulates checking whether it's a solo or team submission.
97
     *
98
     * @param \assign $assignment Assignment object.
99
     * @param int|null $userid User id.
100
     * @param int $groupid Group id.
101
     * @param bool $create Whether a new submission should be created.
102
     * @param int $attemptnumber Attempt number. Use -1 for last attempt.
103
     * @return bool|\stdClass
104
     */
105
    protected static function get_user_or_group_submission(\assign $assignment, int $userid = null,
106
            int $groupid = 0, bool $create = false, int $attemptnumber = -1) {
107
        if ($assignment->get_instance($userid)->teamsubmission) {
108
            $submission = $assignment->get_group_submission($userid, $groupid, $create, $attemptnumber);
109
        } else {
110
            $submission = $assignment->get_user_submission($userid, $create, $attemptnumber);
111
        }
112
        return $submission;
113
    }
114
}