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
/**
18
 * This file contains the functions for assign_plugin abstract class
19
 *
20
 *
21
 * @package   mod_assign
22
 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
/**
29
 * Abstract class for assign_plugin (submission/feedback).
30
 *
31
 * @package   mod_assign
32
 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
33
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
abstract class assign_plugin {
36
 
37
    /** @var assign $assignment the assignment record that contains the global
38
     *              settings for this assign instance
39
     */
40
    protected $assignment;
41
    /** @var string $type assignment plugin type */
42
    private $type = '';
43
    /** @var string $error error message */
44
    private $error = '';
45
    /** @var boolean|null $enabledcache Cached lookup of the is_enabled function */
46
    private $enabledcache = null;
47
    /** @var boolean|null $enabledcache Cached lookup of the is_visible function */
48
    private $visiblecache = null;
49
 
50
    /**
51
     * Constructor for the abstract plugin type class
52
     *
53
     * @param assign $assignment
54
     * @param string $type
55
     */
56
    final public function __construct(assign $assignment, $type) {
57
        $this->assignment = $assignment;
58
        $this->type = $type;
59
    }
60
 
61
    /**
62
     * Is this the first plugin in the list?
63
     *
64
     * @return bool
65
     */
66
    final public function is_first() {
67
        $order = get_config($this->get_subtype() . '_' . $this->get_type(), 'sortorder');
68
 
69
        if ($order == 0) {
70
            return true;
71
        }
72
        return false;
73
    }
74
 
75
    /**
76
     * Is this the last plugin in the list?
77
     *
78
     * @return bool
79
     */
80
    final public function is_last() {
81
        $lastindex = count(core_component::get_plugin_list($this->get_subtype()))-1;
82
        $currentindex = get_config($this->get_subtype() . '_' . $this->get_type(), 'sortorder');
83
        if ($lastindex == $currentindex) {
84
            return true;
85
        }
86
 
87
        return false;
88
    }
89
 
90
    /**
91
     * This function should be overridden to provide an array of elements that can be added to a moodle
92
     * form for display in the settings page for the assignment.
93
     * @param MoodleQuickForm $mform The form to add the elements to
94
     * @return $array
95
     */
96
    public function get_settings(MoodleQuickForm $mform) {
97
        return;
98
    }
99
 
100
    /**
101
     * Allows the plugin to update the defaultvalues passed in to
102
     * the settings form (needed to set up draft areas for editor
103
     * and filemanager elements)
104
     * @param array $defaultvalues
105
     */
106
    public function data_preprocessing(&$defaultvalues) {
107
        return;
108
    }
109
 
110
    /**
1441 ariadna 111
     * This method is called when the mod_assign_mod_form is submitted.
112
     *
113
     * It is an opportunity to validate the settings form fields added by {@see get_settings()}.
114
     *
115
     * @param array $data as passed to mod_assign_mod_form::validation().
116
     * @param array $files as passed to mod_assign_mod_form::validation().
117
     * @return array and validation errors that should be displayed.
118
     *      This is array_merged with any other validation errors from the form.
119
     */
120
    public function settings_validation(array $data, array $files): array {
121
        return [];
122
    }
123
 
124
    /**
1 efrain 125
     * The assignment subtype is responsible for saving it's own settings as the database table for the
126
     * standard type cannot be modified.
127
     *
128
     * @param stdClass $formdata - the data submitted from the form
129
     * @return bool - on error the subtype should call set_error and return false.
130
     */
131
    public function save_settings(stdClass $formdata) {
132
        return true;
133
    }
134
 
135
    /**
136
     * Save the error message from the last error
137
     *
138
     * @param string $msg - the error description
139
     */
140
    final protected function set_error($msg) {
141
        $this->error = $msg;
142
    }
143
 
144
    /**
145
     * What was the last error?
146
     *
147
     * @return string
148
     */
149
    final public function get_error() {
150
        return $this->error;
151
    }
152
 
153
    /**
154
     * Should return the name of this plugin type.
155
     *
156
     * @return string - the name
157
     */
158
    abstract public function get_name();
159
 
160
    /**
161
     * Should return the subtype of this plugin.
162
     *
163
     * @return string - either 'assignsubmission' or 'feedback'
164
     */
165
    abstract public function get_subtype();
166
 
167
    /**
168
     * Should return the type of this plugin.
169
     *
170
     * @return string - the type
171
     */
172
    final public function get_type() {
173
        return $this->type;
174
    }
175
 
176
    /**
177
     * Get the installed version of this plugin
178
     *
179
     * @return string
180
     */
181
    final public function get_version() {
182
        $version = get_config($this->get_subtype() . '_' . $this->get_type(), 'version');
183
        if ($version) {
184
            return $version;
185
        } else {
186
            return '';
187
        }
188
    }
189
 
190
    /**
191
     * Get the required moodle version for this plugin
192
     *
193
     * @return string
194
     */
195
    final public function get_requires() {
196
        $requires = get_config($this->get_subtype() . '_' . $this->get_type(), 'requires');
197
        if ($requires) {
198
            return $requires;
199
        } else {
200
            return '';
201
        }
202
    }
203
 
204
    /**
205
     * Save any custom data for this form submission
206
     *
207
     * @param stdClass $submissionorgrade - assign_submission or assign_grade.
208
     *              For submission plugins this is the submission data,
209
     *              for feedback plugins it is the grade data
210
     * @param stdClass $data - the data submitted from the form
211
     * @return bool - on error the subtype should call set_error and return false.
212
     */
213
    public function save(stdClass $submissionorgrade, stdClass $data) {
214
        return true;
215
    }
216
 
217
    /**
218
     * Set this plugin to enabled
219
     *
220
     * @return bool
221
     */
222
    final public function enable() {
223
        $this->enabledcache = true;
224
        return $this->set_config('enabled', 1);
225
    }
226
 
227
    /**
228
     * Set this plugin to disabled
229
     *
230
     * @return bool
231
     */
232
    final public function disable() {
233
        $this->enabledcache = false;
234
        return $this->set_config('enabled', 0);
235
    }
236
 
237
    /**
238
     * Allows hiding this plugin from the submission/feedback screen if it is not enabled.
239
     *
240
     * @return bool - if false - this plugin will not accept submissions / feedback
241
     */
242
    public function is_enabled() {
243
        if ($this->enabledcache === null) {
244
            $this->enabledcache = $this->get_config('enabled');
245
        }
246
        return $this->enabledcache;
247
    }
248
 
249
 
250
    /**
251
     * Get any additional fields for the submission/grading form for this assignment.
252
     *
253
     * @param mixed $submissionorgrade submission|grade - For submission plugins this is the submission data,
254
     *                                                    for feedback plugins it is the grade data
255
     * @param MoodleQuickForm $mform - This is the form
256
     * @param stdClass $data - This is the form data that can be modified for example by a filemanager element
257
     * @param int $userid - This is the userid for the current submission.
258
     *                      This is passed separately as there may not yet be a submission or grade.
259
     * @return boolean - true if we added anything to the form
260
     */
261
    public function get_form_elements_for_user($submissionorgrade, MoodleQuickForm $mform, stdClass $data, $userid) {
262
        return $this->get_form_elements($submissionorgrade, $mform, $data);
263
    }
264
 
265
    /**
266
     * Get any additional fields for the submission/grading form for this assignment.
267
     * This function is retained for backwards compatibility - new plugins should override {@link get_form_elements_for_user()}.
268
     *
269
     * @param mixed $submissionorgrade submission|grade - For submission plugins this is the submission data,
270
     *                                                    for feedback plugins it is the grade data
271
     * @param MoodleQuickForm $mform - This is the form
272
     * @param stdClass $data - This is the form data that can be modified for example by a filemanager element
273
     * @return boolean - true if we added anything to the form
274
     */
275
    public function get_form_elements($submissionorgrade, MoodleQuickForm $mform, stdClass $data) {
276
        return false;
277
    }
278
 
279
    /**
280
     * Should not output anything - return the result as a string so it can be consumed by webservices.
281
     *
282
     * @param stdClass $submissionorgrade assign_submission or assign_grade
283
     *                 For submission plugins this is the submission data,
284
     *                 for feedback plugins it is the grade data
285
     * @return string - return a string representation of the submission in full
286
     */
287
    public function view(stdClass $submissionorgrade) {
288
        return '';
289
    }
290
 
291
    /**
292
     * Get the numerical sort order for this plugin
293
     *
294
     * @return int
295
     */
296
    final public function get_sort_order() {
297
        $order = get_config($this->get_subtype() . '_' . $this->get_type(), 'sortorder');
298
        return $order?$order:0;
299
    }
300
 
301
    /**
302
     * Is this plugin enaled?
303
     *
304
     * @return bool
305
     */
306
    final public function is_visible() {
307
        if ($this->visiblecache === null) {
308
            $disabled = get_config($this->get_subtype() . '_' . $this->get_type(), 'disabled');
309
            $this->visiblecache = !$disabled;
310
        }
311
        return $this->visiblecache;
312
    }
313
 
314
 
315
    /**
316
     * Has this plugin got a custom settings.php file?
317
     *
318
     * @return bool
319
     */
320
    final public function has_admin_settings() {
321
        global $CFG;
322
 
323
        $pluginroot = $CFG->dirroot . '/mod/assign/' . substr($this->get_subtype(), strlen('assign')) . '/' . $this->get_type();
324
        $settingsfile = $pluginroot . '/settings.php';
325
        return file_exists($settingsfile);
326
    }
327
 
328
    /**
329
     * Set a configuration value for this plugin
330
     *
331
     * @param string $name The config key
332
     * @param string $value The config value
333
     * @return bool
334
     */
335
    final public function set_config($name, $value) {
336
        global $DB;
337
 
338
        $dbparams = array('assignment'=>$this->assignment->get_instance()->id,
339
                          'subtype'=>$this->get_subtype(),
340
                          'plugin'=>$this->get_type(),
341
                          'name'=>$name);
342
        $current = $DB->get_record('assign_plugin_config', $dbparams, '*', IGNORE_MISSING);
343
 
344
        if ($current) {
345
            $current->value = $value;
346
            return $DB->update_record('assign_plugin_config', $current);
347
        } else {
348
            $setting = new stdClass();
349
            $setting->assignment = $this->assignment->get_instance()->id;
350
            $setting->subtype = $this->get_subtype();
351
            $setting->plugin = $this->get_type();
352
            $setting->name = $name;
353
            $setting->value = $value;
354
 
355
            return $DB->insert_record('assign_plugin_config', $setting) > 0;
356
        }
357
    }
358
 
359
    /**
360
     * Get a configuration value for this plugin
361
     *
362
     * @param mixed $setting The config key (string) or null
363
     * @return mixed string | false
364
     */
365
    final public function get_config($setting = null) {
366
        global $DB;
367
 
368
        if ($setting) {
369
            if (!$this->assignment->has_instance()) {
370
                return false;
371
            }
372
            $assignment = $this->assignment->get_instance();
373
            if ($assignment) {
374
                $dbparams = array('assignment'=>$assignment->id,
375
                                  'subtype'=>$this->get_subtype(),
376
                                  'plugin'=>$this->get_type(),
377
                                  'name'=>$setting);
378
                $result = $DB->get_record('assign_plugin_config', $dbparams, '*', IGNORE_MISSING);
379
                if ($result) {
380
                    return $result->value;
381
                }
382
            }
383
            return false;
384
        }
385
        $dbparams = array('assignment'=>$this->assignment->get_instance()->id,
386
                          'subtype'=>$this->get_subtype(),
387
                           'plugin'=>$this->get_type());
388
        $results = $DB->get_records('assign_plugin_config', $dbparams);
389
 
390
        $config = new stdClass();
391
        if (is_array($results)) {
392
            foreach ($results as $setting) {
393
                $name = $setting->name;
394
                $config->$name = $setting->value;
395
            }
396
        }
397
        return $config;
398
    }
399
 
400
    /**
401
     * Get a list of file areas associated with the plugin configuration.
402
     * This is used for backup/restore.
403
     *
404
     * @return array names of the fileareas, can be an empty array
405
     */
406
    public function get_config_file_areas() {
407
        return array();
408
    }
409
 
410
    /**
411
     * Should not output anything - return the result as a string so it can be consumed by webservices.
412
     *
413
     * @param stdClass $submissionorgrade assign_submission or assign_grade
414
     *                 For submission plugins this is the submission data, for feedback plugins it is the grade data
415
     * @param bool $showviewlink Modifed to return whether or not to show a link to the full submission/feedback
416
     * @return string - return a string representation of the submission in full
417
     */
418
    public function view_summary(stdClass $submissionorgrade, & $showviewlink) {
419
        return '';
420
    }
421
 
422
    /**
423
     * Given a field name and value should update the text for this field in the plugins submission or grade
424
     *
425
     * @param string $name Name of the field.
426
     * @param string $value Updated text
427
     * @param int $submissionorgradeid The id of the submission or grade
428
     * @return bool - true if the value was updated
429
     */
430
    public function set_editor_text($name, $value, $submissionorgradeid) {
431
        return false;
432
    }
433
 
434
    /**
435
     * Given a field name and value should update the format for this field in the plugins submission or grade
436
     *
437
     * @param string $name Name of the field.
438
     * @param int $format Updated format.
439
     * @param int $submissionorgradeid The id of the submission or grade.
440
     * @return bool - true if the value was updated
441
     */
442
    public function set_editor_format($name, $format, $submissionorgradeid) {
443
        return false;
444
    }
445
 
446
    /**
447
     * Return a list of the fields that can be exported or imported via text.
448
     *
449
     * @return array - The list of field names (strings) and descriptions. ($name => $description)
450
     */
451
    public function get_editor_fields() {
452
        return array();
453
    }
454
 
455
    /**
456
     * Given a field name, should return the text of an editor field that is part of
457
     * this plugin. This is used when exporting to portfolio.
458
     *
459
     * @param string $name Name of the field.
460
     * @param int $submissionorgradeid The id of the submission or grade
461
     * @return string - The text for the editor field
462
     */
463
    public function get_editor_text($name, $submissionorgradeid) {
464
        return '';
465
    }
466
 
467
    /**
468
     * Produce a list of files suitable for export that represent this feedback or submission
469
     *
470
     * @param stdClass $submissionorgrade assign_submission or assign_grade
471
     *                 For submission plugins this is the submission data, for feedback plugins it is the grade data
472
     * @param stdClass $user The user record for the current submission.
473
     *                         Needed for url rewriting if this is a group submission.
474
     * @return array - return an array of files indexed by filename
475
     */
476
    public function get_files(stdClass $submissionorgrade, stdClass $user) {
477
        return array();
478
    }
479
 
480
    /**
481
     * Given a field name, should return the format of an editor field that is part of
482
     * this plugin. This is used when exporting to portfolio.
483
     *
484
     * @param string $name Name of the field.
485
     * @param int $submissionid The id of the submission
486
     * @return int - The format for the editor field
487
     */
488
    public function get_editor_format($name, $submissionid) {
489
        return 0;
490
    }
491
 
492
    /**
493
     * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type
494
     * and version.
495
     *
496
     * @param string $type The old assignment subtype
497
     * @param int $version The old assignment version
498
     * @return bool True if upgrade is possible
499
     */
500
    public function can_upgrade($type, $version) {
501
        return false;
502
    }
503
 
504
    /**
505
     * Upgrade the settings from the old assignment to the new one
506
     *
507
     * @param context $oldcontext The context for the old assignment module
508
     * @param stdClass $oldassignment The data record for the old assignment
509
     * @param string $log Record upgrade messages in the log
510
     * @return bool true or false - false will trigger a rollback
511
     */
512
    public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
513
        $params = array('type'=>$this->type, 'subtype'=>$this->get_subtype());
514
        $log .= ' ' . get_string('upgradenotimplemented', 'mod_assign', $params);
515
        return false;
516
    }
517
 
518
    /**
519
     * Upgrade the submission from the old assignment to the new one
520
     *
521
     * @param context $oldcontext The data record for the old context
522
     * @param stdClass $oldassignment The data record for the old assignment
523
     * @param stdClass $oldsubmissionorgrade The data record for the old submission
524
     * @param stdClass $submissionorgrade assign_submission or assign_grade The new submission or grade
525
     * @param string $log Record upgrade messages in the log
526
     * @return boolean true or false - false will trigger a rollback
527
     */
528
    public function upgrade(context $oldcontext,
529
                            stdClass $oldassignment,
530
                            stdClass $oldsubmissionorgrade,
531
                            stdClass $submissionorgrade,
532
                            & $log) {
533
        $params = array('type'=>$this->type, 'subtype'=>$this->get_subtype());
534
        $log = $log . ' ' . get_string('upgradenotimplemented', 'mod_assign', $params);
535
        return false;
536
    }
537
 
538
    /**
539
     * The assignment has been deleted - remove the plugin specific data
540
     *
541
     * @return bool
542
     */
543
    public function delete_instance() {
544
        return true;
545
    }
546
 
547
    /**
548
     * Run cron for this plugin
549
     */
550
    public static function cron() {
551
    }
552
 
553
    /**
554
     * Is this assignment plugin empty? (ie no submission or feedback)
555
     * @param stdClass $submissionorgrade assign_submission or assign_grade
556
     * @return bool
557
     */
558
    public function is_empty(stdClass $submissionorgrade) {
559
        return true;
560
    }
561
 
562
    /**
563
     * Get file areas returns a list of areas this plugin stores files
564
     * @return array - An array of fileareas (keys) and descriptions (values)
565
     */
566
    public function get_file_areas() {
567
        return array();
568
    }
569
 
570
 
571
    /**
572
     * Default implementation of file_get_info for plugins.
573
     * This is used by the filebrowser to browse a plugins file areas.
574
     *
575
     * This implementation should work for most plugins but can be overridden if required.
576
     * @param file_browser $browser
577
     * @param string $filearea
578
     * @param int $itemid
579
     * @param string $filepath
580
     * @param string $filename
581
     * @return file_info_stored
582
     */
583
    public function get_file_info($browser, $filearea, $itemid, $filepath, $filename) {
584
        global $CFG, $DB, $USER;
585
        $urlbase = $CFG->wwwroot.'/pluginfile.php';
586
        $writeaccess = false;
587
        // Permission check on the itemid.
588
        $assignment = $this->assignment;
589
 
590
        if ($this->get_subtype() == 'assignsubmission') {
591
            if ($itemid) {
592
                $record = $DB->get_record('assign_submission', array('id' => $itemid), 'userid,groupid', IGNORE_MISSING);
593
                if (!$record) {
594
                    return null;
595
                }
596
                if (!empty($record->userid)) {
597
                    if (!$assignment->can_view_submission($record->userid)) {
598
                        return null;
599
                    }
600
 
601
                    // We only report write access for teachers.
602
                    $writeaccess = $assignment->can_grade() && $assignment->can_edit_submission($record->userid);
603
                } else {
604
                    // Must be a team submission with a group.
605
                    if (!$assignment->can_view_group_submission($record->groupid)) {
606
                        return null;
607
                    }
608
                    // We only report write access for teachers.
609
                    $writeaccess = $assignment->can_grade() && $assignment->can_edit_group_submission($record->groupid);
610
                }
611
            }
612
        } else {
613
            // Not supported for feedback plugins.
614
            return null;
615
        }
616
 
617
        $fs = get_file_storage();
618
        $filepath = is_null($filepath) ? '/' : $filepath;
619
        $filename = is_null($filename) ? '.' : $filename;
620
        if (!($storedfile = $fs->get_file($assignment->get_context()->id,
621
                                          $this->get_subtype() . '_' . $this->get_type(),
622
                                          $filearea,
623
                                          $itemid,
624
                                          $filepath,
625
                                          $filename))) {
626
            return null;
627
        }
628
 
629
        return new file_info_stored($browser,
630
                                    $assignment->get_context(),
631
                                    $storedfile,
632
                                    $urlbase,
633
                                    $filearea,
634
                                    $itemid,
635
                                    true,
636
                                    $writeaccess,
637
                                    false);
638
    }
639
 
640
    /**
641
     * This allows a plugin to render a page in the context of the assignment
642
     *
643
     * If the plugin creates a link to the assignment view.php page with
644
     * The following required parameters:
645
     *      id=coursemoduleid
646
     *      plugin=type
647
     *      pluginsubtype=assignfeedback|assignsubmission
648
     *      pluginaction=customaction
649
     *
650
     * Then this function will be called to display the page with the pluginaction passed as action
651
     * @param string $action The plugin specified action
652
     * @return string
653
     */
654
    public function view_page($action) {
655
        return '';
656
    }
657
 
658
    /**
659
     * This allows a plugin to render an introductory section which is displayed
660
     * right below the activity's "intro" section on the main assignment page.
661
     *
662
     * @return string
663
     */
664
    public function view_header() {
665
        return '';
666
    }
667
 
668
    /**
669
     * If this plugin should not include a column in the grading table or a row on the summary page
670
     * then return false
671
     *
672
     * @return bool
673
     */
674
    public function has_user_summary() {
675
        return true;
676
    }
677
 
678
    /**
679
     * If this plugin can participate in a webservice (save_submission or save_grade),
680
     * return a list of external_params to be included in the definition of that webservice.
681
     *
682
     * @return external_description|null
683
     */
684
    public function get_external_parameters() {
685
        return null;
686
    }
687
 
688
    /**
689
     * If true, the plugin will appear on the module settings page and can be
690
     * enabled/disabled per assignment instance.
691
     *
692
     * @return bool
693
     */
694
    public function is_configurable() {
695
        return true;
696
    }
697
 
698
    /**
699
     * Return the plugin configs for external functions,
700
     * in some cases the configs will need formatting or be returned only if the current user has some capabilities enabled.
701
     *
702
     * @return array the list of settings
703
     * @since Moodle 3.2
704
     */
705
    public function get_config_for_external() {
706
        return array();
707
    }
708
}