forked from Kai-Drysdale/ICT302-Team5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocallib.php
898 lines (751 loc) · 34.1 KB
/
locallib.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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
<?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 definition for the library class for onlinetexte submission plugin
*
* This class provides all the functionality for the new assign module.
*
* @package assignsubmission_onlinetexte
* @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();
// File area for online text submission assignment.
define('ASSIGNSUBMISSION_ONLINETEXTE_FILEAREA', 'submissions_onlinetexte');
/**
* library class for onlinetexte submission plugin extending submission plugin base class
*
* @package assignsubmission_onlinetexte
* @copyright 2012 NetSpot {@link http://www.netspot.com.au}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class assign_submission_onlinetexte extends assign_submission_plugin {
/**
* Get the name of the online text submission plugin
* @return string
*/
public function get_name() {
return get_string('onlinetexte', 'assignsubmission_onlinetexte');
}
/**
* Get onlinetexte submission information from the database
*
* @param int $submissionid
* @return mixed
*/
private function get_onlinetexte_submission($submissionid) {
global $DB;
return $DB->get_record('assignsubmission_onlinetexte', array('submission'=>$submissionid));
}
/**
* Remove a submission.
*
* @param stdClass $submission The submission
* @return boolean
*/
public function remove(stdClass $submission) {
global $DB;
$submissionid = $submission ? $submission->id : 0;
if ($submissionid) {
$DB->delete_records('assignsubmission_onlinetexte', array('submission' => $submissionid));
}
return true;
}
/**
* Get the settings for onlinetexte submission plugin
*
* @param MoodleQuickForm $mform The form to add elements to
* @return void
*/
public function get_settings(MoodleQuickForm $mform) {
global $CFG, $COURSE;
$defaultwordlimit = $this->get_config('wordlimit') == 0 ? '' : $this->get_config('wordlimit');
$defaultwordlimitenabled = $this->get_config('wordlimitenabled');
$options = array('size' => '6', 'maxlength' => '6');
$wordLimitName = get_string('wordlimit', 'assignsubmission_onlinetexte');
// Create a text box that can be enabled/disabled for onlinetexte word limit.
$wordlimitgrp = array();
$wordlimitgrp[] = $mform->createElement('text', 'assignsubmission_onlinetexte_wordlimit', '', $options);
$wordlimitgrp[] = $mform->createElement('checkbox', 'assignsubmission_onlinetexte_wordlimit_enabled',
'', get_string('enable'));
$mform->addGroup($wordlimitgrp, 'assignsubmission_onlinetexte_wordlimit_group', $wordLimitName, ' ', false);
$mform->addHelpButton('assignsubmission_onlinetexte_wordlimit_group',
'wordlimit',
'assignsubmission_onlinetexte');
$mform->disabledIf('assignsubmission_onlinetexte_wordlimit',
'assignsubmission_onlinetexte_wordlimit_enabled',
'notchecked');
$mform->hideIf('assignsubmission_onlinetexte_wordlimit',
'assignsubmission_onlinetexte_enabled',
'notchecked');
// Add numeric rule to text field.
$wordlimitgrprules = array();
$wordlimitgrprules['assignsubmission_onlinetexte_wordlimit'][] = array(null, 'numeric', null, 'client');
$mform->addGroupRule('assignsubmission_onlinetexte_wordlimit_group', $wordlimitgrprules);
// Rest of group setup.
$mform->setDefault('assignsubmission_onlinetexte_wordlimit', $defaultwordlimit);
$mform->setDefault('assignsubmission_onlinetexte_wordlimit_enabled', $defaultwordlimitenabled);
$mform->setType('assignsubmission_onlinetexte_wordlimit', PARAM_INT);
$mform->hideIf('assignsubmission_onlinetexte_wordlimit_group',
'assignsubmission_onlinetexte_enabled',
'notchecked');
//Add text area for adding link
if($this->get_config('links')=='0')
$defaultlinks = '';
else
$defaultlinks = $this->get_config('links');
$mform->addElement('textarea', 'assignsubmission_onlinetexte_addlink', 'Add Links', 'rows="2" cols="30"');
$mform->setDefault('assignsubmission_onlinetexte_addlink', $defaultlinks);
$mform->hideIf('assignsubmission_onlinetexte_addlink',
'assignsubmission_onlinetexte_enabled',
'notchecked');
//Add text area for adding link
if($this->get_config('examples')=='0')
$defaultexamples = '';
else
$defaultexamples = $this->get_config('examples');
$mform->addElement('textarea', 'assignsubmission_onlinetexte_addexample', 'Add Examples', 'rows="2" cols="30"');
$mform->setDefault('assignsubmission_onlinetexte_addexample', $defaultexamples);
$mform->hideIf('assignsubmission_onlinetexte_addexample',
'assignsubmission_onlinetexte_enabled',
'notchecked');
//Add tips
}
/**
* Save the settings for onlinetexte submission plugin
*
* @param stdClass $data
* @return bool
*/
public function save_settings(stdClass $data) {
//Save word limit
if (empty($data->assignsubmission_onlinetexte_wordlimit) || empty($data->assignsubmission_onlinetexte_wordlimit_enabled)) {
$wordlimit = 0;
$wordlimitenabled = 0;
} else {
$wordlimit = $data->assignsubmission_onlinetexte_wordlimit;
$wordlimitenabled = 1;
}
$this->set_config('wordlimit', $wordlimit);
$this->set_config('wordlimitenabled', $wordlimitenabled);
//Save links
if(empty($data->assignsubmission_onlinetexte_addlink)){
$links = '';
}
else{
$links = $data->assignsubmission_onlinetexte_addlink;
}
$this->set_config('links', $links);
//Save examples
if(empty($data->assignsubmission_onlinetexte_addexample)){
$examples = '';
}
else{
$examples = $data->assignsubmission_onlinetexte_addexample;
}
$this->set_config('examples', $examples);
return true;
}
/**
* Add form elements for settings
*
* @param mixed $submission can be null
* @param MoodleQuickForm $mform
* @param stdClass $data
* @return true if elements were added to the form
*/
public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {
global $USER;
$elements = array();
$customText = "This is some custom text that will appear above the submission form.";
$editoroptions = $this->get_edit_options();
$submissionid = $submission ? $submission->id : 0;
if (!isset($data->onlinetexte)) {
$data->onlinetexte = '';
}
if (!isset($data->onlinetexteformat)) {
$data->onlinetexteformat = editors_get_preferred_format();
}
if ($submission) {
$onlinetextesubmission = $this->get_onlinetexte_submission($submission->id);
if ($onlinetextesubmission) {
$data->onlinetexte = $onlinetextesubmission->onlinetexte;
$data->onlinetexteformat = $onlinetextesubmission->onlineformat;
}
}
$data = file_prepare_standard_editor($data,
'onlinetexte',
$editoroptions,
$this->assignment->get_context(),
'assignsubmission_onlinetexte',
ASSIGNSUBMISSION_ONLINETEXTE_FILEAREA,
$submissionid);
$mform->addElement('html', '<div id="editor" style="float: left;">');
$mform->addElement('editor', 'onlinetexte_editor', null, null, $editoroptions);
//$mform->addElement('html', '</div> <br /> <br /> <br /> <br /> <br /> <br /> ');
$mform->addElement('html', '<script> var link = document.createElement("link"); link.rel = "stylesheet"; link.type = "text/css"; link.href = "submission/onlinetexte/css/sidebar.css"; document.head.appendChild(link);</script>');
$mform->addElement('html', $this->create_realtime_ui());
$mform->addElement('html', $this->create_sidebar());
//Get saved links
$links = $this->get_config('links');
if ($links != null)
{
$links = $this->format_links($links);
$this->sidebar_extra_links($links);
}
$duedate = $this->assignment->get_instance()->duedate;
$mform->addElement('html', $this->add_realtime_word_count_script($mform, $duedate));
return true;
}
function create_realtime_ui()
{
$realtime_ui = '<div id="word_count_display" style="float: left;">Word Count: 0</div> <br />';
$realtime_ui .= '<div id="timer_display" style="float: left;">Timer: </div> <br />';
$realtime_ui .= '<div style="float: left;">
<a href="#" id="sidebarbutton">Side bar</a>
<script type="text/javascript" src="submission/onlinetexte/js/sidebar.js"></script>
</div>';
return $realtime_ui;
}
function create_sidebar()
{
global $USER;
$assignsubmissionstatus = $this->assignment->get_assign_submission_status_renderable($USER, "");
$gradingcontrollerpreview = $assignsubmissionstatus->gradingcontrollerpreview;
$examples = $this->get_config('examples');
$examples = str_replace("\r\n", "br />", $examples);
$rubric = explode("Rubric options", $gradingcontrollerpreview);
$assignmentinto = $this->assignment->get_instance()->intro;
$sidebar = '<div id="sidebar"><div id="sidebarContent"><br><br><div id="assignment-name">';
$sidebar .= $this->assignment->get_instance()->name;
$sidebar .= '</div><div id="assign-desc">';
$sidebar .= $this->assignment->get_instance()->intro;
$sidebar .= $this->assignment->get_instance()->activity;
if ($examples != null)
{
$sidebar .= 'Examples: <br />';
$sidebar .= $examples . '<br />';
}
$sidebar .= '</div> <div id="extraLinks"></div>' ;
if ($rubric[0] != null)
{
$sidebar .= 'Marking Rubric:';
$sidebar .= $rubric[0];
}
$sidebar .= '</div></div>';
return $sidebar;
}
//Format links for saving
function format_links($links)
{
$links = str_replace(',', ' ', $links);
$links = preg_replace('/\s+/',' ', $links);
return $links;
}
function add_realtime_word_count_script($mform, $duedate)
{
// Enqueue the JavaScript file with a relative path
return $js = <<<EOD
<script type="text/javascript">
var duedate = new Date($duedate * 1000); // Convert Unix timestamp to milliseconds
</script>
<script type="text/javascript" src="submission/onlinetexte/js/real_time.js"></script>
EOD;
}
function sidebar_extra_links($links){
$js = <<<EOD
<script type="text/javascript">
var links = "$links";
</script>
<script type="text/javascript" src="submission/onlinetexte/js/extra_links.js"></script>
EOD;
echo $js;
}
/**
* Editor format options
*
* @return array
*/
private function get_edit_options() {
$editoroptions = array(
'noclean' => false,
'maxfiles' => EDITOR_UNLIMITED_FILES,
'maxbytes' => $this->assignment->get_course()->maxbytes,
'context' => $this->assignment->get_context(),
'return_types' => (FILE_INTERNAL | FILE_EXTERNAL | FILE_CONTROLLED_LINK),
'removeorphaneddrafts' => true // Whether or not to remove any draft files which aren't referenced in the text.
);
return $editoroptions;
}
/**
* Save data to the database and trigger plagiarism plugin,
* if enabled, to scan the uploaded content via events trigger
*
* @param stdClass $submission
* @param stdClass $data
* @return bool
*
*/
public function save(stdClass $submission, stdClass $data) {
global $USER, $DB;
$editoroptions = $this->get_edit_options();
$data = file_postupdate_standard_editor($data,
'onlinetexte',
$editoroptions,
$this->assignment->get_context(),
'assignsubmission_onlinetexte',
ASSIGNSUBMISSION_ONLINETEXTE_FILEAREA,
$submission->id);
$onlinetextesubmission = $this->get_onlinetexte_submission($submission->id);
$fs = get_file_storage();
$files = $fs->get_area_files($this->assignment->get_context()->id,
'assignsubmission_onlinetexte',
ASSIGNSUBMISSION_ONLINETEXTE_FILEAREA,
$submission->id,
'id',
false);
// Check word count before submitting anything.
$exceeded = $this->check_word_count(trim($data->onlinetexte));
if ($exceeded) {
$this->set_error($exceeded);
return false;
}
$params = array(
'context' => context_module::instance($this->assignment->get_course_module()->id),
'courseid' => $this->assignment->get_course()->id,
'objectid' => $submission->id,
'other' => array(
'pathnamehashes' => array_keys($files),
'content' => trim($data->onlinetexte),
'format' => $data->onlinetexte_editor['format']
)
);
if (!empty($submission->userid) && ($submission->userid != $USER->id)) {
$params['relateduserid'] = $submission->userid;
}
if ($this->assignment->is_blind_marking()) {
$params['anonymous'] = 1;
}
$event = \assignsubmission_onlinetexte\event\assessable_uploaded::create($params);
$event->trigger();
$groupname = null;
$groupid = 0;
// Get the group name as other fields are not transcribed in the logs and this information is important.
if (empty($submission->userid) && !empty($submission->groupid)) {
$groupname = $DB->get_field('groups', 'name', array('id' => $submission->groupid), MUST_EXIST);
$groupid = $submission->groupid;
} else {
$params['relateduserid'] = $submission->userid;
}
$count = count_words($data->onlinetexte);
// Unset the objectid and other field from params for use in submission events.
unset($params['objectid']);
unset($params['other']);
$params['other'] = array(
'submissionid' => $submission->id,
'submissionattempt' => $submission->attemptnumber,
'submissionstatus' => $submission->status,
'onlinetextewordcount' => $count,
'groupid' => $groupid,
'groupname' => $groupname
);
if ($onlinetextesubmission) {
$onlinetextesubmission->onlinetexte = $data->onlinetexte;
$onlinetextesubmission->onlineformat = $data->onlinetexte_editor['format'];
$params['objectid'] = $onlinetextesubmission->id;
$updatestatus = $DB->update_record('assignsubmission_onlinetexte', $onlinetextesubmission);
$event = \assignsubmission_onlinetexte\event\submission_updated::create($params);
$event->set_assign($this->assignment);
$event->trigger();
return $updatestatus;
} else {
$onlinetextesubmission = new stdClass();
$onlinetextesubmission->onlinetexte = $data->onlinetexte;
$onlinetextesubmission->onlineformat = $data->onlinetexte_editor['format'];
$onlinetextesubmission->submission = $submission->id;
$onlinetextesubmission->assignment = $this->assignment->get_instance()->id;
$onlinetextesubmission->id = $DB->insert_record('assignsubmission_onlinetexte', $onlinetextesubmission);
$params['objectid'] = $onlinetextesubmission->id;
$event = \assignsubmission_onlinetexte\event\submission_created::create($params);
$event->set_assign($this->assignment);
$event->trigger();
return $onlinetextesubmission->id > 0;
}
}
/**
* Return a list of the text fields that can be imported/exported by this plugin
*
* @return array An array of field names and descriptions. (name=>description, ...)
*/
public function get_editor_fields() {
return array('onlinetexte' => get_string('pluginname', 'assignsubmission_onlinetexte'));
}
/**
* Get the saved text content from the editor
*
* @param string $name
* @param int $submissionid
* @return string
*/
public function get_editor_text($name, $submissionid) {
if ($name == 'onlinetexte') {
$onlinetextesubmission = $this->get_onlinetexte_submission($submissionid);
if ($onlinetextesubmission) {
return $onlinetextesubmission->onlinetexte;
}
}
return '';
}
/**
* Get the content format for the editor
*
* @param string $name
* @param int $submissionid
* @return int
*/
public function get_editor_format($name, $submissionid) {
if ($name == 'onlinetexte') {
$onlinetextesubmission = $this->get_onlinetexte_submission($submissionid);
if ($onlinetextesubmission) {
return $onlinetextesubmission->onlineformat;
}
}
return 0;
}
/**
* Display onlinetexte word count in the submission status table
*
* @param stdClass $submission
* @param bool $showviewlink - If the summary has been truncated set this to true
* @return string
*/
public function view_summary(stdClass $submission, & $showviewlink) {
global $CFG;
$onlinetextesubmission = $this->get_onlinetexte_submission($submission->id);
// Always show the view link.
$showviewlink = true;
if ($onlinetextesubmission) {
// This contains the shortened version of the text plus an optional 'Export to portfolio' button.
$text = $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
$onlinetextesubmission->submission,
$this->get_type(),
'onlinetexte',
'assignsubmission_onlinetexte', true);
// The actual submission text.
$onlinetexte = trim($onlinetextesubmission->onlinetexte);
// The shortened version of the submission text.
$shorttext = shorten_text($onlinetexte, 140);
$plagiarismlinks = '';
if (!empty($CFG->enableplagiarism)) {
require_once($CFG->libdir . '/plagiarismlib.php');
$plagiarismlinks .= plagiarism_get_links(array('userid' => $submission->userid,
'content' => $onlinetexte,
'cmid' => $this->assignment->get_course_module()->id,
'course' => $this->assignment->get_course()->id,
'assignment' => $submission->assignment));
}
// We compare the actual text submission and the shortened version. If they are not equal, we show the word count.
if ($onlinetexte != $shorttext) {
$wordcount = get_string('numwords', 'assignsubmission_onlinetexte', count_words($onlinetexte));
return $plagiarismlinks . $wordcount . $text;
} else {
return $plagiarismlinks . $text;
}
}
return '';
}
/**
* Produce a list of files suitable for export that represent this submission.
*
* @param stdClass $submission - For this is the submission data
* @param stdClass $user - This is the user record for this submission
* @return array - return an array of files indexed by filename
*/
public function get_files(stdClass $submission, stdClass $user) {
global $DB;
$files = array();
$onlinetextesubmission = $this->get_onlinetexte_submission($submission->id);
// Note that this check is the same logic as the result from the is_empty function but we do
// not call it directly because we already have the submission record.
if ($onlinetextesubmission) {
// Do not pass the text through format_text. The result may not be displayed in Moodle and
// may be passed to external services such as document conversion or portfolios.
$formattedtext = $this->assignment->download_rewrite_pluginfile_urls($onlinetextesubmission->onlinetexte, $user, $this);
$head = '<head><meta charset="UTF-8"></head>';
$submissioncontent = '<!DOCTYPE html><html>' . $head . '<body>'. $formattedtext . '</body></html>';
$filename = get_string('onlinetextefilename', 'assignsubmission_onlinetexte');
$files[$filename] = array($submissioncontent);
$fs = get_file_storage();
$fsfiles = $fs->get_area_files($this->assignment->get_context()->id,
'assignsubmission_onlinetexte',
ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
$submission->id,
'timemodified',
false);
foreach ($fsfiles as $file) {
$files[$file->get_filename()] = $file;
}
}
return $files;
}
/**
* Display the saved text content from the editor in the view table
*
* @param stdClass $submission
* @return string
*/
public function view(stdClass $submission) {
global $CFG;
$result = '';
$plagiarismlinks = '';
$onlinetextesubmission = $this->get_onlinetexte_submission($submission->id);
if ($onlinetextesubmission) {
// Render for portfolio API.
$result .= $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
$onlinetextesubmission->submission,
$this->get_type(),
'onlinetexte',
'assignsubmission_onlinetexte');
if (!empty($CFG->enableplagiarism)) {
require_once($CFG->libdir . '/plagiarismlib.php');
$plagiarismlinks .= plagiarism_get_links(array('userid' => $submission->userid,
'content' => trim($onlinetextesubmission->onlinetexte),
'cmid' => $this->assignment->get_course_module()->id,
'course' => $this->assignment->get_course()->id,
'assignment' => $submission->assignment));
}
}
return $plagiarismlinks . $result;
}
/**
* Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type and version.
*
* @param string $type old assignment subtype
* @param int $version old assignment version
* @return bool True if upgrade is possible
*/
public function can_upgrade($type, $version) {
if ($type == 'online' && $version >= 2011112900) {
return true;
}
return false;
}
/**
* Upgrade the settings from the old assignment to the new plugin based one
*
* @param context $oldcontext - the database for the old assignment context
* @param stdClass $oldassignment - the database for the old assignment instance
* @param string $log record log events here
* @return bool Was it a success?
*/
public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
// No settings to upgrade.
return true;
}
/**
* Upgrade the submission from the old assignment to the new one
*
* @param context $oldcontext - the database for the old assignment context
* @param stdClass $oldassignment The data record for the old assignment
* @param stdClass $oldsubmission The data record for the old submission
* @param stdClass $submission The data record for the new submission
* @param string $log Record upgrade messages in the log
* @return bool true or false - false will trigger a rollback
*/
public function upgrade(context $oldcontext,
stdClass $oldassignment,
stdClass $oldsubmission,
stdClass $submission,
& $log) {
global $DB;
$onlinetextesubmission = new stdClass();
$onlinetextesubmission->onlinetexte = $oldsubmission->data1;
$onlinetextesubmission->onlineformat = $oldsubmission->data2;
$onlinetextesubmission->submission = $submission->id;
$onlinetextesubmission->assignment = $this->assignment->get_instance()->id;
if ($onlinetextesubmission->onlinetexte === null) {
$onlinetextesubmission->onlinetexte = '';
}
if ($onlinetextesubmission->onlineformat === null) {
$onlinetextesubmission->onlineformat = editors_get_preferred_format();
}
if (!$DB->insert_record('assignsubmission_onlinetexte', $onlinetextesubmission) > 0) {
$log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
return false;
}
// Now copy the area files.
$this->assignment->copy_area_files_for_upgrade($oldcontext->id,
'mod_assignment',
'submission',
$oldsubmission->id,
$this->assignment->get_context()->id,
'assignsubmission_onlinetexte',
ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
$submission->id);
return true;
}
/**
* Formatting for log info
*
* @param stdClass $submission The new submission
* @return string
*/
public function format_for_log(stdClass $submission) {
// Format the info for each submission plugin (will be logged).
$onlinetextesubmission = $this->get_onlinetexte_submission($submission->id);
$onlinetexteloginfo = '';
$onlinetexteloginfo .= get_string('numwordsforlog',
'assignsubmission_onlinetexte',
count_words($onlinetextesubmission->onlinetexte));
return $onlinetexteloginfo;
}
/**
* The assignment has been deleted - cleanup
*
* @return bool
*/
public function delete_instance() {
global $DB;
$DB->delete_records('assignsubmission_onlinetexte',
array('assignment'=>$this->assignment->get_instance()->id));
return true;
}
/**
* No text is set for this plugin
*
* @param stdClass $submission
* @return bool
*/
public function is_empty(stdClass $submission) {
$onlinetextesubmission = $this->get_onlinetexte_submission($submission->id);
$wordcount = 0;
$hasinsertedresources = false;
if (isset($onlinetextesubmission->onlinetexte)) {
$wordcount = count_words(trim($onlinetextesubmission->onlinetexte));
// Check if the online text submission contains video, audio or image elements
// that can be ignored and stripped by count_words().
$hasinsertedresources = preg_match('/<\s*((video|audio)[^>]*>(.*?)<\s*\/\s*(video|audio)>)|(img[^>]*>(.*?))/',
trim($onlinetextesubmission->onlinetexte));
}
return $wordcount == 0 && !$hasinsertedresources;
}
/**
* Determine if a submission is empty
*
* This is distinct from is_empty in that it is intended to be used to
* determine if a submission made before saving is empty.
*
* @param stdClass $data The submission data
* @return bool
*/
public function submission_is_empty(stdClass $data) {
if (!isset($data->onlinetexte_editor)) {
return true;
}
$wordcount = 0;
$hasinsertedresources = false;
if (isset($data->onlinetexte_editor['text'])) {
$wordcount = count_words(trim((string)$data->onlinetexte_editor['text']));
// Check if the online text submission contains video, audio or image elements
// that can be ignored and stripped by count_words().
$hasinsertedresources = preg_match('/<\s*((video|audio)[^>]*>(.*?)<\s*\/\s*(video|audio)>)|(img[^>]*>(.*?))/',
trim((string)$data->onlinetexte_editor['text']));
}
return $wordcount == 0 && !$hasinsertedresources;
}
/**
* 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(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA=>$this->get_name());
}
/**
* Copy the student's submission from a previous submission. Used when a student opts to base their resubmission
* on the last submission.
* @param stdClass $sourcesubmission
* @param stdClass $destsubmission
*/
public function copy_submission(stdClass $sourcesubmission, stdClass $destsubmission) {
global $DB;
// Copy the files across (attached via the text editor).
$contextid = $this->assignment->get_context()->id;
$fs = get_file_storage();
$files = $fs->get_area_files($contextid, 'assignsubmission_onlinetexte',
ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $sourcesubmission->id, 'id', false);
foreach ($files as $file) {
$fieldupdates = array('itemid' => $destsubmission->id);
$fs->create_file_from_storedfile($fieldupdates, $file);
}
// Copy the assignsubmission_onlinetexte record.
$onlinetextesubmission = $this->get_onlinetexte_submission($sourcesubmission->id);
if ($onlinetextesubmission) {
unset($onlinetextesubmission->id);
$onlinetextesubmission->submission = $destsubmission->id;
$DB->insert_record('assignsubmission_onlinetexte', $onlinetextesubmission);
}
return true;
}
/**
* Return a description of external params suitable for uploading an onlinetexte submission from a webservice.
*
* @return external_description|null
*/
public function get_external_parameters() {
$editorparams = array('text' => new external_value(PARAM_RAW, 'The text for this submission.'),
'format' => new external_value(PARAM_INT, 'The format for this submission'),
'itemid' => new external_value(PARAM_INT, 'The draft area id for files attached to the submission'));
$editorstructure = new external_single_structure($editorparams, 'Editor structure', VALUE_OPTIONAL);
return array('onlinetexte_editor' => $editorstructure);
}
/**
* Compare word count of onlinetexte submission to word limit, and return result.
*
* @param string $submissiontext Onlinetext submission text from editor
* @return string Error message if limit is enabled and exceeded, otherwise null
*/
public function check_word_count($submissiontext) {
global $OUTPUT;
$wordlimitenabled = $this->get_config('wordlimitenabled');
$wordlimit = $this->get_config('wordlimit');
if ($wordlimitenabled == 0) {
return null;
}
// Count words and compare to limit.
$wordcount = count_words($submissiontext);
echo '<script type="text/javascript">';
echo 'var message = "' . $wordcount. '";'; // Embed PHP variable in JavaScript
echo 'alert(message);';
echo '</script>';
if ($wordcount <= $wordlimit) {
return null;
} else {
$errormsg = get_string('wordlimitexceeded', 'assignsubmission_onlinetexte',
array('limit' => $wordlimit, 'count' => $wordcount));
return $OUTPUT->error_text($errormsg);
}
}
/**
* Return the plugin configs for external functions.
*
* @return array the list of settings
* @since Moodle 3.2
*/
public function get_config_for_external() {
return (array) $this->get_config();
}
// Add this method to your class
}