mirrored from git://git.moodle.org/moodle.git
/
assignmentplugin.php
605 lines (542 loc) · 20.3 KB
/
assignmentplugin.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
<?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/>.
/**
* This file contains the functions for assign_plugin abstract class
*
*
* @package mod_assign
* @copyright 2012 NetSpot {@link http://www.netspot.com.au}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Abstract class for assign_plugin (submission/feedback).
*
* @package mod_assign
* @copyright 2012 NetSpot {@link http://www.netspot.com.au}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class assign_plugin {
/** @var assign $assignment the assignment record that contains the global settings for this assign instance */
protected $assignment;
/** @var string $type assignment plugin type */
private $type = '';
/** @var string $error error message */
private $error = '';
/**
* Constructor for the abstract plugin type class
*
* @param assign $assignment
* @param string $type
*/
public final function __construct(assign $assignment, $type) {
$this->assignment = $assignment;
$this->type = $type;
}
/**
* Is this the first plugin in the list?
*
* @return bool
*/
public final function is_first() {
$order = get_config($this->get_subtype() . '_' . $this->get_type(), 'sortorder');
if ($order == 0) {
return true;
}
return false;
}
/**
* Is this the last plugin in the list?
*
* @return bool
*/
public final function is_last() {
if ((count(get_plugin_list($this->get_subtype()))-1) == get_config($this->get_subtype() . '_' . $this->get_type(), 'sortorder')) {
return true;
}
return false;
}
/**
* This function should be overridden to provide an array of elements that can be added to a moodle
* form for display in the settings page for the assignment.
* @param MoodleQuickForm $mform The form to add the elements to
* @return $array
*/
public function get_settings(MoodleQuickForm $mform) {
return;
}
/**
* Allows the plugin to update the defaultvalues passed in to
* the settings form (needed to set up draft areas for editor
* and filemanager elements)
* @param array $defaultvalues
*/
public function data_preprocessing(&$defaultvalues) {
return;
}
/**
* The assignment subtype is responsible for saving it's own settings as the database table for the
* standard type cannot be modified.
*
* @param stdClass $formdata - the data submitted from the form
* @return bool - on error the subtype should call set_error and return false.
*/
public function save_settings(stdClass $formdata) {
return true;
}
/**
* Save the error message from the last error
*
* @param string $msg - the error description
*/
protected final function set_error($msg) {
$this->error = $msg;
}
/**
* What was the last error?
*
*
* @return string
*/
public final function get_error() {
return $this->error;
}
/**
* Should return the name of this plugin type.
*
* @return string - the name
*/
public abstract function get_name();
/**
* Should return the subtype of this plugin.
*
* @return string - either 'assignsubmission' or 'feedback'
*/
public abstract function get_subtype();
/**
* Should return the type of this plugin.
*
* @return string - the type
*/
public final function get_type() {
return $this->type;
}
/**
* Get the installed version of this plugin
*
* @return string
*/
public final function get_version() {
$version = get_config($this->get_subtype() . '_' . $this->get_type(), 'version');
if ($version) {
return $version;
} else {
return '';
}
}
/**
* Get the required moodle version for this plugin
*
* @return string
*/
public final function get_requires() {
$requires = get_config($this->get_subtype() . '_' . $this->get_type(), 'requires');
if ($requires) {
return $requires;
} else {
return '';
}
}
/**
* Save any custom data for this form submission
*
* @param stdClass $submissionorgrade - assign_submission or assign_grade
* For submission plugins this is the submission data, for feedback plugins it is the grade data
* @param stdClass $data - the data submitted from the form
* @return bool - on error the subtype should call set_error and return false.
*/
public function save(stdClass $submissionorgrade, stdClass $data) {
return true;
}
/**
* Set this plugin to enabled
*
* @return bool
*/
public final function enable() {
return $this->set_config('enabled', 1);
}
/**
* Set this plugin to disabled
*
* @return bool
*/
public final function disable() {
return $this->set_config('enabled', 0);
}
/**
* Allows hiding this plugin from the submission/feedback screen if it is not enabled.
*
* @return bool - if false - this plugin will not accept submissions / feedback
*/
public function is_enabled() {
return $this->get_config('enabled');
}
/**
* Get any additional fields for the submission/grading form for this assignment.
*
* @param mixed $submissionorgrade submission|grade - For submission plugins this is the submission data,
* for feedback plugins it is the grade data
* @param MoodleQuickForm $mform - This is the form
* @param stdClass $data - This is the form data that can be modified for example by a filemanager element
* @param int $userid - This is the userid for the current submission.
* This is passed separately as there may not yet be a submission or grade.
* @return boolean - true if we added anything to the form
*/
public function get_form_elements_for_user($submissionorgrade, MoodleQuickForm $mform, stdClass $data, $userid) {
return $this->get_form_elements($submissionorgrade, $mform, $data);
}
/**
* Get any additional fields for the submission/grading form for this assignment.
* This function is retained for backwards compatibility - new plugins should override {@link get_form_elements_for_user()}.
*
* @param mixed $submissionorgrade submission|grade - For submission plugins this is the submission data, for feedback plugins it is the grade data
* @param MoodleQuickForm $mform - This is the form
* @param stdClass $data - This is the form data that can be modified for example by a filemanager element
* @return boolean - true if we added anything to the form
*/
public function get_form_elements($submissionorgrade, MoodleQuickForm $mform, stdClass $data) {
return false;
}
/**
* Should not output anything - return the result as a string so it can be consumed by webservices.
*
* @param stdClass $submissionorgrade assign_submission or assign_grade
* For submission plugins this is the submission data, for feedback plugins it is the grade data
* @return string - return a string representation of the submission in full
*/
public function view(stdClass $submissionorgrade) {
return '';
}
/**
* Get the numerical sort order for this plugin
*
* @return int
*/
public final function get_sort_order() {
$order = get_config($this->get_subtype() . '_' . $this->get_type(), 'sortorder');
return $order?$order:0;
}
/**
* Is this plugin enaled?
*
* @return bool
*/
public final function is_visible() {
$disabled = get_config($this->get_subtype() . '_' . $this->get_type(), 'disabled');
return !$disabled;
}
/**
* Has this plugin got a custom settings.php file?
*
* @return bool
*/
public final function has_admin_settings() {
global $CFG;
return file_exists($CFG->dirroot . '/mod/assign/' . substr($this->get_subtype(), strlen('assign')) . '/' . $this->get_type() . '/settings.php');
}
/**
* Set a configuration value for this plugin
*
* @param string $name The config key
* @param string $value The config value
* @return bool
*/
public final function set_config($name, $value) {
global $DB;
$current = $DB->get_record('assign_plugin_config', array('assignment'=>$this->assignment->get_instance()->id, 'subtype'=>$this->get_subtype(), 'plugin'=>$this->get_type(), 'name'=>$name), '*', IGNORE_MISSING);
if ($current) {
$current->value = $value;
return $DB->update_record('assign_plugin_config', $current);
} else {
$setting = new stdClass();
$setting->assignment = $this->assignment->get_instance()->id;
$setting->subtype = $this->get_subtype();
$setting->plugin = $this->get_type();
$setting->name = $name;
$setting->value = $value;
return $DB->insert_record('assign_plugin_config', $setting) > 0;
}
}
/**
* Get a configuration value for this plugin
*
* @param mixed $setting The config key (string) or null
* @return mixed string | false
*/
public final function get_config($setting = null) {
global $DB;
if ($setting) {
if (!$this->assignment->has_instance()) {
return false;
}
$assignment = $this->assignment->get_instance();
if ($assignment) {
$result = $DB->get_record('assign_plugin_config', array('assignment'=>$assignment->id, 'subtype'=>$this->get_subtype(), 'plugin'=>$this->get_type(), 'name'=>$setting), '*', IGNORE_MISSING);
if ($result) {
return $result->value;
}
}
return false;
}
$results = $DB->get_records('assign_plugin_config', array('assignment'=>$this->assignment->get_instance()->id, 'subtype'=>$this->get_subtype(), 'plugin'=>$this->get_type()));
$config = new stdClass();
if (is_array($results)) {
foreach ($results as $setting) {
$name = $setting->name;
$config->$name = $setting->value;
}
}
return $config;
}
/**
* Should not output anything - return the result as a string so it can be consumed by webservices.
*
* @param stdClass $submissionorgrade assign_submission or assign_grade
* For submission plugins this is the submission data, for feedback plugins it is the grade data
* @param bool $showviewlink Modifed to return whether or not to show a link to the full submission/feedback
* @return string - return a string representation of the submission in full
*/
public function view_summary(stdClass $submissionorgrade, & $showviewlink) {
return '';
}
/**
* Given a field name and value should update the text for this field in the plugins submission or grade
*
* @param string $name Name of the field.
* @param string $value Updated text
* @param int $submissionorgradeid The id of the submission or grade
* @return bool - true if the value was updated
*/
public function set_editor_text($name, $value, $submissionorgradeid) {
return false;
}
/**
* Given a field name and value should update the format for this field in the plugins submission or grade
*
* @param string $name Name of the field.
* @param int $format Updated format.
* @param int $submissionorgradeid The id of the submission or grade.
* @return bool - true if the value was updated
*/
public function set_editor_format($name, $format, $submissionorgradeid) {
return false;
}
/**
* Return a list of the fields that can be exported or imported via text.
*
* @return array - The list of field names (strings) and descriptions. ($name => $description)
*/
public function get_editor_fields() {
return array();
}
/**
* Given a field name, should return the text of an editor field that is part of
* this plugin. This is used when exporting to portfolio.
*
* @param string $name Name of the field.
* @param int $submissionorgradeid The id of the submission or grade
* @return string - The text for the editor field
*/
public function get_editor_text($name, $submissionorgradeid) {
return '';
}
/**
* Produce a list of files suitable for export that represent this feedback or submission
*
* @param stdClass $submissionorgrade assign_submission or assign_grade
* For submission plugins this is the submission data, for feedback plugins it is the grade data
* @param stdClass $user The user record for the current submission.
* Needed for url rewriting if this is a group submission.
* @return array - return an array of files indexed by filename
*/
public function get_files(stdClass $submissionorgrade, stdClass $user) {
return array();
}
/**
* Given a field name, should return the format of an editor field that is part of
* this plugin. This is used when exporting to portfolio.
*
* @param string $name Name of the field.
* @param int $submissionid The id of the submission
* @return int - The format for the editor field
*/
public function get_editor_format($name, $submissionid) {
return 0;
}
/**
* Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type
* and version.
*
* @param string $type The old assignment subtype
* @param int $version The old assignment version
* @return bool True if upgrade is possible
*/
public function can_upgrade($type, $version) {
return false;
}
/**
* Upgrade the settings from the old assignment to the new one
*
* @param context $oldcontext The context for the old assignment module
* @param stdClass $oldassignment The data record for the old assignment
* @param string $log Record upgrade messages in the log
* @return bool true or false - false will trigger a rollback
*/
public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
$log = $log . ' ' . get_string('upgradenotimplemented', 'mod_assign', array('type'=>$this->type, 'subtype'=>$this->get_subtype()));
return false;
}
/**
* Upgrade the submission from the old assignment to the new one
*
* @param context $oldcontext The data record for the old context
* @param stdClass $oldassignment The data record for the old assignment
* @param stdClass $oldsubmissionorgrade The data record for the old submission
* @param stdClass $submissionorgrade assign_submission or assign_grade The new submission or grade
* @param string $log Record upgrade messages in the log
* @return boolean true or false - false will trigger a rollback
*/
public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmissionorgrade, stdClass $submissionorgrade, & $log) {
$log = $log . ' ' . get_string('upgradenotimplemented', 'mod_assign', array('type'=>$this->type, 'subtype'=>$this->get_subtype()));
return false;
}
/**
* Formatting for log info
*
* @param stdClass $submissionorgrade assign_submission or assign_grade The new submission or grade
* @return string
*/
public function format_for_log(stdClass $submissionorgrade) {
// format the info for each submission plugin add_to_log
return '';
}
/**
* The assignment has been deleted - remove the plugin specific data
*
* @return bool
*/
public function delete_instance() {
return true;
}
/**
* Run cron for this plugin
*/
public static function cron() {
}
/**
* Is this assignment plugin empty? (ie no submission or feedback)
* @param stdClass $submissionorgrade assign_submission or assign_grade
* @return bool
*/
public function is_empty(stdClass $submissionorgrade) {
return true;
}
/**
* Get file areas returns a list of areas this plugin stores files
* @return array - An array of fileareas (keys) and descriptions (values)
*/
public function get_file_areas() {
return array();
}
/**
* Default implementation of file_get_info for plugins.
* This is used by the filebrowser to browse a plugins file areas.
*
* This implementation should work for most plugins but can be overridden if required.
* @param file_browser $browser
* @param string $filearea
* @param int $itemid
* @param string $filepath
* @param string $filename
* @return file_info_stored
*/
public function get_file_info($browser, $filearea, $itemid, $filepath, $filename) {
global $CFG, $DB, $USER;
$urlbase = $CFG->wwwroot.'/pluginfile.php';
// permission check on the itemid
if ($this->get_subtype() == 'assignsubmission') {
if ($itemid) {
$record = $DB->get_record('assign_submission', array('id'=>$itemid), 'userid', IGNORE_MISSING);
if (!$record) {
return null;
}
if (!$this->assignment->can_view_submission($record->userid)) {
return null;
}
}
} else {
// not supported for feedback plugins
return null;
}
$fs = get_file_storage();
$filepath = is_null($filepath) ? '/' : $filepath;
$filename = is_null($filename) ? '.' : $filename;
if (!($storedfile = $fs->get_file($this->assignment->get_context()->id,
$this->get_subtype() . '_' . $this->get_type(),
$filearea,
$itemid,
$filepath,
$filename))) {
return null;
}
return new file_info_stored($browser,
$this->assignment->get_context(),
$storedfile,
$urlbase,
$filearea,
$itemid,
true,
true,
false);
}
/**
* This allows a plugin to render a page in the context of the assignment
*
* If the plugin creates a link to the assignment view.php page with
* The following required parameters:
* id=coursemoduleid
* plugin=type
* pluginsubtype=assignfeedback|assignsubmission
* pluginaction=customaction
*
* Then this function will be called to display the page with the pluginaction passed as action
* @param string $action The plugin specified action
* @return string
*/
public function view_page($action) {
return '';
}
/**
* If this plugin should not include a column in the grading table or a row on the summary page
* then return false
*
* @return bool
*/
public function has_user_summary() {
return true;
}
}