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
defined('MOODLE_INTERNAL') || die();
18
 
19
/**
20
 * assign module data generator class
21
 *
22
 * @package mod_assign
23
 * @category test
24
 * @copyright 2012 Paul Charsley
25
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
class mod_assign_generator extends testing_module_generator {
28
 
29
    /**
30
     * Create a new instance of the assignment activity.
31
     *
32
     * @param array|stdClass|null $record
33
     * @param array|null $options
34
     * @return stdClass
35
     */
36
    public function create_instance($record = null, array $options = null) {
37
        $record = (object)(array)$record;
38
 
39
        $defaultsettings = array(
40
            'alwaysshowdescription'             => 1,
41
            'submissiondrafts'                  => 1,
42
            'requiresubmissionstatement'        => 0,
43
            'sendnotifications'                 => 0,
44
            'sendstudentnotifications'          => 1,
45
            'sendlatenotifications'             => 0,
46
            'duedate'                           => 0,
47
            'allowsubmissionsfromdate'          => 0,
48
            'grade'                             => 100,
49
            'cutoffdate'                        => 0,
50
            'gradingduedate'                    => 0,
51
            'teamsubmission'                    => 0,
52
            'requireallteammemberssubmit'       => 0,
53
            'teamsubmissiongroupingid'          => 0,
54
            'blindmarking'                      => 0,
55
            'attemptreopenmethod'               => 'none',
56
            'maxattempts'                       => -1,
57
            'markingworkflow'                   => 0,
58
            'markingallocation'                 => 0,
59
            'markinganonymous'                  => 0,
60
            'activityformat'                    => 0,
61
            'timelimit'                         => 0,
62
            'submissionattachments'             => 0,
63
        );
64
 
65
        if (property_exists($record, 'teamsubmissiongroupingid')) {
66
            $record->teamsubmissiongroupingid = $this->get_grouping_id($record->teamsubmissiongroupingid);
67
        }
68
 
69
        foreach ($defaultsettings as $name => $value) {
70
            if (!isset($record->{$name})) {
71
                $record->{$name} = $value;
72
            }
73
        }
74
 
75
        return parent::create_instance($record, (array)$options);
76
    }
77
 
78
    /**
79
     * Create an assignment submission.
80
     *
81
     * @param array $data
82
     */
83
    public function create_submission(array $data): void {
84
        global $USER;
85
 
86
        $currentuser = $USER;
87
        $user = \core_user::get_user($data['userid']);
88
        $this->set_user($user);
89
 
90
        $submission = (object) [
91
            'userid' => $user->id,
92
        ];
93
 
94
        [$course, $cm] = get_course_and_cm_from_cmid($data['assignid'], 'assign');
95
        $context = context_module::instance($cm->id);
96
        $assign = new assign($context, $cm, $course);
97
 
98
        foreach ($assign->get_submission_plugins() as $plugin) {
99
            $pluginname = $plugin->get_type();
100
            if (array_key_exists($pluginname, $data)) {
101
                $plugingenerator = $this->datagenerator->get_plugin_generator("assignsubmission_{$pluginname}");
102
                $plugingenerator->add_submission_data($submission, $assign, $data);
103
            }
104
        }
105
 
106
        $assign->save_submission((object) $submission, $notices);
107
 
108
        $this->set_user($currentuser);
109
    }
110
 
111
    /**
112
     * Gets the grouping id from it's idnumber.
113
     *
114
     * @throws Exception
115
     * @param string $idnumber
116
     * @return int
117
     */
118
    protected function get_grouping_id(string $idnumber): int {
119
        global $DB;
120
 
121
        // Do not fetch grouping ID for empty grouping idnumber.
122
        if (empty($idnumber)) {
123
            return null;
124
        }
125
 
126
        if (!$id = $DB->get_field('groupings', 'id', ['idnumber' => $idnumber])) {
127
            if (is_numeric($idnumber)) {
128
                return $idnumber;
129
            }
130
            throw new Exception('The specified grouping with idnumber "' . $idnumber . '" does not exist');
131
        }
132
 
133
        return $id;
134
    }
135
 
136
    /**
137
     * Create an assign override (either user or group).
138
     *
139
     * @param array $data must specify assignid, and one of userid or groupid.
140
     * @throws coding_exception
141
     */
142
    public function create_override(array $data): void {
143
        global $DB;
144
 
145
        if (!isset($data['assignid'])) {
146
            throw new coding_exception('Must specify assignid when creating an assign override.');
147
        }
148
 
149
        if (!isset($data['userid']) && !isset($data['groupid'])) {
150
            throw new coding_exception('Must specify one of userid or groupid when creating an assign override.');
151
        }
152
 
153
        if (isset($data['userid']) && isset($data['groupid'])) {
154
            throw new coding_exception('Cannot specify both userid and groupid when creating an assign override.');
155
        }
156
 
157
        $DB->insert_record('assign_overrides', (object) $data);
158
    }
159
}