forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exsubmission.php
239 lines (213 loc) · 10.9 KB
/
exsubmission.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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/>.
/**
* View, create or edit single example submission
*
* @package mod
* @subpackage workshop
* @copyright 2009 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once(dirname(__FILE__).'/locallib.php');
$cmid = required_param('cmid', PARAM_INT); // course module id
$id = required_param('id', PARAM_INT); // example submission id, 0 for the new one
$edit = optional_param('edit', false, PARAM_BOOL); // open for editing?
$delete = optional_param('delete', false, PARAM_BOOL); // example removal requested
$confirm = optional_param('confirm', false, PARAM_BOOL); // example removal request confirmed
$assess = optional_param('assess', false, PARAM_BOOL); // assessment required
$cm = get_coursemodule_from_id('workshop', $cmid, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
require_login($course, false, $cm);
if (isguestuser()) {
print_error('guestsarenotallowed');
}
$workshop = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
$workshop = new workshop($workshop, $cm, $course);
$PAGE->set_url($workshop->exsubmission_url($id), array('edit' => $edit));
$PAGE->set_title($workshop->name);
$PAGE->set_heading($course->fullname);
if ($edit) {
$PAGE->navbar->add(get_string('exampleediting', 'workshop'));
} else {
$PAGE->navbar->add(get_string('example', 'workshop'));
}
$output = $PAGE->get_renderer('mod_workshop');
if ($id) { // example is specified
$example = $workshop->get_example_by_id($id);
$workshop->log('view example', $workshop->exsubmission_url($example->id), $example->id);
} else { // no example specified - create new one
require_capability('mod/workshop:manageexamples', $workshop->context);
$example = new stdclass();
$example->id = null;
$example->authorid = $USER->id;
$example->example = 1;
}
$canmanage = has_capability('mod/workshop:manageexamples', $workshop->context);
$canassess = has_capability('mod/workshop:peerassess', $workshop->context);
$refasid = $DB->get_field('workshop_assessments', 'id', array('submissionid' => $example->id, 'weight' => 1));
if ($example->id and ($canmanage or ($workshop->assessing_examples_allowed() and $canassess))) {
// ok you can go
} elseif (is_null($example->id) and $canmanage) {
// ok you can go
} else {
print_error('nopermissions', 'error', $workshop->view_url(), 'view or manage example submission');
}
if ($id and $delete and $confirm and $canmanage) {
require_sesskey();
$workshop->delete_submission($example);
redirect($workshop->view_url());
}
if ($id and $assess and $canmanage) {
// reference assessment of an example is the assessment with the weight = 1. There should be just one
// such assessment
require_sesskey();
if (!$refasid) {
$refasid = $workshop->add_allocation($example, $USER->id, 1);
}
redirect($workshop->exassess_url($refasid));
}
if ($id and $assess and $canassess) {
// training assessment of an example is the assessment with the weight = 0
require_sesskey();
$asid = $DB->get_field('workshop_assessments', 'id',
array('submissionid' => $example->id, 'weight' => 0, 'reviewerid' => $USER->id));
if (!$asid) {
$asid = $workshop->add_allocation($example, $USER->id, 0);
}
if ($asid == workshop::ALLOCATION_EXISTS) {
// the training assessment of the example was not found but the allocation already
// exists. this probably means that the user is the author of the reference assessment.
echo $output->header();
echo $output->box(get_string('assessmentreferenceconflict', 'workshop'));
echo $output->continue_button($workshop->view_url());
echo $output->footer();
die();
}
redirect($workshop->exassess_url($asid));
}
if ($edit and $canmanage) {
require_once(dirname(__FILE__).'/submission_form.php');
$maxfiles = $workshop->nattachments;
$maxbytes = $workshop->maxbytes;
$contentopts = array(
'trusttext' => true,
'subdirs' => false,
'maxfiles' => $maxfiles,
'maxbytes' => $maxbytes,
'context' => $workshop->context
);
$attachmentopts = array('subdirs' => true, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
$example = file_prepare_standard_editor($example, 'content', $contentopts, $workshop->context,
'mod_workshop', 'submission_content', $example->id);
$example = file_prepare_standard_filemanager($example, 'attachment', $attachmentopts, $workshop->context,
'mod_workshop', 'submission_attachment', $example->id);
$mform = new workshop_submission_form($PAGE->url, array('current' => $example, 'workshop' => $workshop,
'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts));
if ($mform->is_cancelled()) {
redirect($workshop->view_url());
} elseif ($canmanage and $formdata = $mform->get_data()) {
if ($formdata->example == 1) {
// this was used just for validation, it must be set to one when dealing with example submissions
unset($formdata->example);
} else {
throw new coding_exception('Invalid submission form data value: example');
}
$timenow = time();
if (is_null($example->id)) {
$formdata->workshopid = $workshop->id;
$formdata->example = 1;
$formdata->authorid = $USER->id;
$formdata->timecreated = $timenow;
$formdata->feedbackauthorformat = editors_get_preferred_format();
}
$formdata->timemodified = $timenow;
$formdata->title = trim($formdata->title);
$formdata->content = ''; // updated later
$formdata->contentformat = FORMAT_HTML; // updated later
$formdata->contenttrust = 0; // updated later
if (is_null($example->id)) {
$example->id = $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
$workshop->log('add example', $workshop->exsubmission_url($example->id), $example->id);
} else {
$workshop->log('update example', $workshop->exsubmission_url($example->id), $example->id);
if (empty($formdata->id) or empty($example->id) or ($formdata->id != $example->id)) {
throw new moodle_exception('err_examplesubmissionid', 'workshop');
}
}
// save and relink embedded images and save attachments
$formdata = file_postupdate_standard_editor($formdata, 'content', $contentopts, $workshop->context,
'mod_workshop', 'submission_content', $example->id);
$formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $attachmentopts, $workshop->context,
'mod_workshop', 'submission_attachment', $example->id);
if (empty($formdata->attachment)) {
// explicit cast to zero integer
$formdata->attachment = 0;
}
// store the updated values or re-save the new example (re-saving needed because URLs are now rewritten)
$DB->update_record('workshop_submissions', $formdata);
redirect($workshop->exsubmission_url($formdata->id));
}
}
// Output starts here
echo $output->header();
echo $output->heading(format_string($workshop->name), 2);
// show instructions for submitting as they may contain some list of questions and we need to know them
// while reading the submitted answer
if (trim($workshop->instructauthors)) {
$instructions = file_rewrite_pluginfile_urls($workshop->instructauthors, 'pluginfile.php', $PAGE->context->id,
'mod_workshop', 'instructauthors', 0, workshop::instruction_editors_options($PAGE->context));
print_collapsible_region_start('', 'workshop-viewlet-instructauthors', get_string('instructauthors', 'workshop'));
echo $output->box(format_text($instructions, $workshop->instructauthorsformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
print_collapsible_region_end();
}
// if in edit mode, display the form to edit the example
if ($edit and $canmanage) {
$mform->display();
echo $output->footer();
die();
}
// else display the example...
if ($example->id) {
if ($canmanage and $delete) {
echo $output->confirm(get_string('exampledeleteconfirm', 'workshop'),
new moodle_url($PAGE->url, array('delete' => 1, 'confirm' => 1)), $workshop->view_url());
}
if ($canmanage and !$delete and !$DB->record_exists_select('workshop_assessments',
'grade IS NOT NULL AND weight=1 AND submissionid = ?', array($example->id))) {
echo $output->confirm(get_string('assessmentreferenceneeded', 'workshop'),
new moodle_url($PAGE->url, array('assess' => 1)), $workshop->view_url());
}
echo $output->render($workshop->prepare_example_submission($example));
}
// ...with an option to edit or remove it
echo $output->container_start('buttonsbar');
if ($canmanage) {
if (empty($edit) and empty($delete)) {
$aurl = new moodle_url($workshop->exsubmission_url($example->id), array('edit' => 'on'));
echo $output->single_button($aurl, get_string('exampleedit', 'workshop'), 'get');
$aurl = new moodle_url($workshop->exsubmission_url($example->id), array('delete' => 'on'));
echo $output->single_button($aurl, get_string('exampledelete', 'workshop'), 'get');
}
}
// ...and optionally assess it
if ($canassess or ($canmanage and empty($edit) and empty($delete))) {
$aurl = new moodle_url($workshop->exsubmission_url($example->id), array('assess' => 'on', 'sesskey' => sesskey()));
echo $output->single_button($aurl, get_string('exampleassess', 'workshop'), 'get');
}
echo $output->container_end(); // buttonsbar
// and possibly display the example's review(s) - todo
echo $output->footer();