-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathexternal.php
executable file
·936 lines (828 loc) · 32 KB
/
external.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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
<?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/>.
defined('MOODLE_INTERNAL') || die();
require_once("$CFG->libdir/externallib.php");
use mod_board\board;
use mod_board\note_form;
/**
* Provides the mod_board external functions.
* @package mod_board
* @author Karen Holland <[email protected]>
* @copyright 2021 Brickfield Education Labs <https://www.brickfield.ie/>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_board_external extends external_api {
/**
* Function board_history_parameters.
* @return external_function_parameters
*/
public static function board_history_parameters(): external_function_parameters {
return new external_function_parameters([
'id' => new external_value(PARAM_INT, 'The board id', VALUE_REQUIRED),
'ownerid' => new external_value(PARAM_INT, 'The board ownerid', VALUE_REQUIRED),
'since' => new external_value(PARAM_INT, 'The last historyid', VALUE_REQUIRED)
]);
}
/**
* Function board_history,
* @param int $id
* @param int $ownerid
* @param int|null $since
* @return array
*/
public static function board_history(int $id, int $ownerid, ?int $since): array {
// Validate recieved parameters.
$params = self::validate_parameters(self::board_history_parameters(), [
'id' => $id,
'ownerid' => $ownerid,
'since' => $since,
]);
// Request and permission validation.
$context = board::context_for_board($params['id']);
self::validate_context($context);
return board::board_history($params['id'], $params['ownerid'], $params['since']);
}
/**
* Function board_history_returns.
* @return external_multiple_structure
*/
public static function board_history_returns(): external_multiple_structure {
return new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'id'),
'boardid' => new external_value(PARAM_INT, 'boardid'),
'action' => new external_value(PARAM_TEXT, 'action'),
'userid' => new external_value(PARAM_INT, 'userid'),
'content' => new external_value(PARAM_RAW, 'content')
)
)
);
}
/**
* Function get_board_parameters.
* @return external_function_parameters
*/
public static function get_board_parameters(): external_function_parameters {
return new external_function_parameters([
'id' => new external_value(PARAM_INT, 'The board id', VALUE_REQUIRED),
'ownerid' => new external_value(PARAM_INT, 'The ownerid', VALUE_DEFAULT, 0)
]);
}
/**
* Function get_board.
* @param int $id
* @param int $ownerid
* @return array
*/
public static function get_board(int $id, int $ownerid = 0): array {
global $USER;
// Validate recieved parameters.
$params = self::validate_parameters(self::get_board_parameters(), [
'id' => $id,
'ownerid' => $ownerid,
]);
// Request and permission validation.
$context = board::context_for_board($params['id']);
self::validate_context($context);
return board::board_get($params['id'], $params['ownerid']);
}
/**
* Function get_board_returns.
* @return external_multiple_structure
*/
public static function get_board_returns(): external_multiple_structure {
return new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'column id'),
'name' => new external_value(PARAM_TEXT, 'column name'),
'locked' => new external_value(PARAM_BOOL, 'column locked'),
'notes' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'post id'),
'userid' => new external_value(PARAM_INT, 'user id'),
'heading' => new external_value(PARAM_TEXT, 'post heading'),
'content' => new external_value(PARAM_RAW, 'post content'),
'type' => new external_value(PARAM_INT, 'type'),
'info' => new external_value(PARAM_TEXT, 'info'),
'url' => new external_value(PARAM_TEXT, 'url'),
'timecreated' => new external_value(PARAM_INT, 'timecreated'),
'rating' => new external_value(PARAM_INT, 'rating'),
'sortorder' => new external_value(PARAM_INT, 'note sort order')
)
)
)
)
)
);
}
/**
* Function add_column_parameters.
* @return external_function_parameters
*/
public static function add_column_parameters(): external_function_parameters {
return new external_function_parameters([
'boardid' => new external_value(PARAM_INT, 'The board id', VALUE_REQUIRED),
'name' => new external_value(PARAM_TEXT, 'The column name', VALUE_REQUIRED)
]);
}
/**
* Function add_column.
* @param int $boardid
* @param string $name
* @return array
*/
public static function add_column(int $boardid, string $name): array {
// Validate recieved parameters.
$params = self::validate_parameters(self::add_column_parameters(), [
'boardid' => $boardid,
'name' => $name,
]);
// Request and permission validation.
$context = board::context_for_board($params['boardid']);
self::validate_context($context);
return board::board_add_column($params['boardid'], $params['name']);
}
/**
* Function add_column_returns.
* @return external_single_structure
*/
public static function add_column_returns(): external_single_structure {
return new external_single_structure([
'id' => new external_value(PARAM_INT, 'The new column id'),
'historyid' => new external_value(PARAM_INT, 'The last history id')
]);
}
/**
* Function update_column_parameters.
* @return external_function_parameters
*/
public static function update_column_parameters(): external_function_parameters {
return new external_function_parameters([
'id' => new external_value(PARAM_INT, 'The column id', VALUE_REQUIRED),
'name' => new external_value(PARAM_TEXT, 'The column name', VALUE_REQUIRED)
]);
}
/**
* Function update_column.
* @param int $id
* @param string $name
* @return array
*/
public static function update_column(int $id, string $name): array {
// Validate recieved parameters.
$params = self::validate_parameters(self::update_column_parameters(), [
'id' => $id,
'name' => $name,
]);
// Request and permission validation.
$column = board::get_column($params['id']);
$context = board::context_for_board($column->boardid);
self::validate_context($context);
return board::board_update_column($params['id'], $params['name']);
}
/**
* Function update_column_returns.
* @return external_single_structure
*/
public static function update_column_returns(): external_single_structure {
return new external_single_structure([
'status' => new external_value(PARAM_BOOL, 'The update status'),
'historyid' => new external_value(PARAM_INT, 'The last history id')
]);
}
/**
* Function delete_column_parameters.
* @return external_function_parameters
*/
public static function delete_column_parameters(): external_function_parameters {
return new external_function_parameters([
'id' => new external_value(PARAM_INT, 'The column id', VALUE_REQUIRED)
]);
}
/**
* Function delete_column.
* @param int $id
* @return array
*/
public static function delete_column(int $id): array {
// Validate recieved parameters.
$params = self::validate_parameters(self::delete_column_parameters(), [
'id' => $id,
]);
// Request and permission validation.
$column = board::get_column($params['id']);
$context = board::context_for_board($column->boardid);
self::validate_context($context);
return board::board_delete_column($params['id']);
}
/**
* Function delete_column_returns.
* @return external_single_structure
*/
public static function delete_column_returns(): external_single_structure {
return new external_single_structure([
'status' => new external_value(PARAM_BOOL, 'The delete status'),
'historyid' => new external_value(PARAM_INT, 'The last history id')
]);
}
/**
* Function lock_column_parameters.
* @return external_function_parameters
*/
public static function lock_column_parameters(): external_function_parameters {
return new external_function_parameters([
'id' => new external_value(PARAM_INT, 'The column id', VALUE_REQUIRED),
'status' => new external_value(PARAM_BOOL, 'True to lock the column', VALUE_REQUIRED),
]);
}
/**
* Function lock_column.
* @param int $id
* @param bool $status
* @return array
*/
public static function lock_column(int $id, bool $status): array {
// Validate recieved parameters.
$params = self::validate_parameters(self::lock_column_parameters(), [
'id' => $id,
'status' => $status,
]);
// Request and permission validation.
$column = board::get_column($params['id']);
$context = board::context_for_board($column->boardid);
self::validate_context($context);
return board::board_lock_column($params['id'], $params['status']);
}
/**
* Function lock_column_returns.
* @return external_single_structure
*/
public static function lock_column_returns(): external_single_structure {
return new external_single_structure([
'status' => new external_value(PARAM_BOOL, 'The lock status'),
'historyid' => new external_value(PARAM_INT, 'The last history id')
]);
}
/**
* Parameters for submit_form.
*
* @return external_function_parameters
*/
public static function submit_form_parameters(): external_function_parameters {
return new external_function_parameters(
array(
'contextid' => new external_value(PARAM_INT, 'The context id for the course'),
'jsonformdata' => new external_value(PARAM_RAW, 'The data from the create group form, encoded as a json array')
)
);
}
/**
* Process the modal form submission.
*
* @param int $contextid
* @param string $jsonformdata
* @return array
* @throws moodle_exception
*/
public static function submit_form($contextid, $jsonformdata): array {
$params = self::validate_parameters(self::submit_form_parameters(),
['contextid' => $contextid, 'jsonformdata' => $jsonformdata]);
// Check the context.
$context = context::instance_by_id($params['contextid'], MUST_EXIST);
self::validate_context($context);
// Extract data out of the form content.
$serialiseddata = json_decode($params['jsonformdata']);
$data = array();
parse_str($serialiseddata, $data);
$data = str_replace(["\r", "\n"], '', $data);
// Make the form with the ajax data to validate.
$form = new note_form(null, null, 'post', '', null, true, $data);
$data = $form->get_data();
if ($data) {
// Check that the passed context, and the context with this note/column match.
$column = board::get_column($data->columnid);
$ccontext = board::context_for_board($column->boardid);
if ($context->id !== $ccontext->id) {
throw new moodle_exception('formcontextmismatch');
}
// Extract the attachment data.
$attachment = [
'type' => $data->mediatype,
'info' => '',
'url' => ''
];
switch ($data->mediatype) {
case 1:
$attachment['info'] = $data->youtubetitle ?? '';
$attachment['url'] = $data->youtubeurl ?? '';
break;
case 2:
if (!empty($data->imagefile)) {
$attachment['info'] = $data->imagetitle ?? '';
$attachment['url'] = $data->filepicker ?? '';
$attachment['draftitemid'] = $data->imagefile;
}
break;
case 3:
$attachment['info'] = $data->linktitle ?? '';
$attachment['url'] = $data->linkurl ?? '';
break;
}
// Check if heading and content and attachment are empty.
if (empty($data->heading) && empty($data->content) && empty($data->imagefile) && empty($attachment['url'])) {
$result = [
'status' => false,
'action' => 'none',
'note' => [
'id' => 0,
'userid' => 0,
'heading' => '',
'content' => '',
'type' => 0,
'info' => '',
'url' => '',
'timecreated' => 0,
'rating' => 0
],
'historyid' => 0
];
return $result;
}
// Process either as an update or insert.
if ($data->noteid) {
$result = board::board_update_note($data->noteid, $data->ownerid, $data->heading, $data->content, $attachment);
$result['action'] = 'update';
} else {
$result = board::board_add_note($data->columnid, $data->ownerid, $data->heading, $data->content, $attachment);
$result['action'] = 'insert';
}
return $result;
} else {
throw new moodle_exception('formsubmissioninvalid');
}
}
/**
* Return definition for submit_form.
*
* @return external_single_structure
*/
public static function submit_form_returns(): external_single_structure {
return new external_single_structure([
'status' => new external_value(PARAM_BOOL, 'The status'),
'action' => new external_value(PARAM_TEXT, 'The action that was performed'),
'note' => new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'post id'),
'userid' => new external_value(PARAM_INT, 'user id'),
'heading' => new external_value(PARAM_RAW, 'post heading'),
'content' => new external_value(PARAM_RAW, 'post content'),
'type' => new external_value(PARAM_INT, 'type'),
'info' => new external_value(PARAM_TEXT, 'info'),
'url' => new external_value(PARAM_TEXT, 'url'),
'timecreated' => new external_value(PARAM_INT, 'timecreated'),
'rating' => new external_value(PARAM_INT, 'rating', VALUE_OPTIONAL)
)
),
'historyid' => new external_value(PARAM_INT, 'The last history id')
]);
}
/**
* Function delete_note_parameters.
* @return external_function_parameters
*/
public static function delete_note_parameters(): external_function_parameters {
return new external_function_parameters([
'id' => new external_value(PARAM_INT, 'The note id', VALUE_REQUIRED)
]);
}
/**
* Function delete_note.
* @param int $id
* @return array
*/
public static function delete_note(int $id): array {
// Validate recieved parameters.
$params = self::validate_parameters(self::delete_note_parameters(), [
'id' => $id,
]);
// Request and permission validation.
$note = board::get_note($params['id']);
$column = board::get_column($note->columnid);
$context = board::context_for_board($column->boardid);
self::validate_context($context);
return board::board_delete_note($params['id']);
}
/**
* Function delete_note_returns.
* @return external_single_structure
*/
public static function delete_note_returns(): external_single_structure {
return new external_single_structure([
'status' => new external_value(PARAM_BOOL, 'The delete status'),
'historyid' => new external_value(PARAM_INT, 'The last history id')
]);
}
/**
* Function move_column_parameters.
* @return external_function_parameters
*/
public static function move_column_parameters(): external_function_parameters {
return new external_function_parameters([
'id' => new external_value(PARAM_INT, 'The column id', VALUE_REQUIRED),
'sortorder' => new external_value(PARAM_INT, 'The new sort order for the column', VALUE_REQUIRED)
]);
}
/**
* Function move_column.
* @param int $id
* @param int $sortorder The order in the column that the note was placed.
* @return array
*/
public static function move_column(int $id, int $sortorder): array {
// Validate recieved parameters.
$params = self::validate_parameters(self::move_column_parameters(), [
'id' => $id,
'sortorder' => $sortorder,
]);
// Request and permission validation.
$column = board::get_column($params['id']);
$context = board::context_for_board($column->boardid);
self::validate_context($context);
return board::board_move_column($params['id'], $params['sortorder']);
}
/**
* Function move_column_returns.
* @return external_single_structure
*/
public static function move_column_returns(): external_single_structure {
return new external_single_structure([
'status' => new external_value(PARAM_BOOL, 'The move status'),
'historyid' => new external_value(PARAM_INT, 'The last history id')
]);
}
/**
* Function move_note_parameters.
* @return external_function_parameters
*/
public static function move_note_parameters(): external_function_parameters {
return new external_function_parameters([
'id' => new external_value(PARAM_INT, 'The note id', VALUE_REQUIRED),
'columnid' => new external_value(PARAM_INT, 'The new column id', VALUE_REQUIRED),
'ownerid' => new external_value(PARAM_INT, 'The owner id', VALUE_REQUIRED),
'sortorder' => new external_value(PARAM_INT, 'The new sort order for the note', VALUE_REQUIRED)
]);
}
/**
* Function move_note.
* @param int $id
* @param int $columnid
* @param int $ownerid
* @param int $sortorder The order in the column that the note was placed.
* @return array
*/
public static function move_note(int $id, int $columnid, int $ownerid, int $sortorder): array {
// Validate recieved parameters.
$params = self::validate_parameters(self::move_note_parameters(), [
'id' => $id,
'columnid' => $columnid,
'ownerid' => $ownerid,
'sortorder' => $sortorder,
]);
// Request and permission validation.
$column = board::get_column($params['columnid']);
$context = board::context_for_board($column->boardid);
self::validate_context($context);
return board::board_move_note($params['id'], $params['ownerid'], $params['columnid'], $params['sortorder']);
}
/**
* Function move_note_returns.
* @return external_single_structure
*/
public static function move_note_returns(): external_single_structure {
return new external_single_structure([
'status' => new external_value(PARAM_BOOL, 'The move status'),
'historyid' => new external_value(PARAM_INT, 'The last history id')
]);
}
/**
* Function can_rate_note_parameters.
* @return external_function_parameters
*/
public static function can_rate_note_parameters(): external_function_parameters {
return new external_function_parameters([
'id' => new external_value(PARAM_INT, 'The note id', VALUE_REQUIRED)
]);
}
/**
* Function can_rate_note.
* @param int $id
* @return bool
*/
public static function can_rate_note(int $id): array {
// Validate recieved parameters.
$params = self::validate_parameters(self::can_rate_note_parameters(), [
'id' => $id,
]);
// Request and permission validation.
$note = board::get_note($params['id']);
$column = board::get_column($note->columnid);
$context = board::context_for_board($column->boardid);
self::validate_context($context);
return board::board_can_rate_note($params['id']);
}
/**
* Function can_rate_note_returns.
* @return external_single_structure
*/
public static function can_rate_note_returns(): external_single_structure {
return new external_single_structure([
'canrate' => new external_value(PARAM_BOOL, 'The user can rate the note'),
'hasrated' => new external_value(PARAM_BOOL, 'The user has rated the note'),
]);
}
/**
* Function rate_note_parameters.
* @return external_function_parameters
*/
public static function rate_note_parameters(): external_function_parameters {
return new external_function_parameters([
'id' => new external_value(PARAM_INT, 'The note id', VALUE_REQUIRED)
]);
}
/**
* Function rate_note.
* @param int $id
* @return array
*/
public static function rate_note(int $id): array {
// Validate recieved parameters.
$params = self::validate_parameters(self::rate_note_parameters(), [
'id' => $id,
]);
// Request and permission validation.
$note = board::get_note($params['id']);
$column = board::get_column($note->columnid);
$context = board::context_for_board($column->boardid);
self::validate_context($context);
return board::board_rate_note($params['id']);
}
/**
* Function rate_note_returns.
* @return external_single_structure
*/
public static function rate_note_returns(): external_single_structure {
return new external_single_structure([
'status' => new external_value(PARAM_BOOL, 'The rate status'),
'rating' => new external_value(PARAM_INT, 'The new rating id'),
'historyid' => new external_value(PARAM_INT, 'The last history id')
]);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 2.9
*/
public static function get_comments_parameters() {
return new external_function_parameters(
[
'noteid' => new external_value(PARAM_INT, 'id of note'),
]
);
}
/**
* Get the comments for this note.
*
* @param int $noteid The id of the note.
* @return array of results
*/
public static function get_comments($noteid) {
global $DB, $USER;
$warnings = [];
$arrayparams = array(
'noteid' => $noteid,
);
$params = self::validate_parameters(self::get_comments_parameters(), $arrayparams);
$comment = new \mod_board\comment($params);
$context = $comment->get_context();
self::validate_context($context);
require_capability('mod/board:view', $context);
$canpost = has_capability('mod/board:postcomment', $context);
$candeleteall = has_capability('mod/board:deleteallcomments', $context);
$notes = $DB->get_records('board_comments', ['noteid' => $params['noteid'], 'deleted' => 0], 'timecreated DESC');
$comments = [];
foreach ($notes as $note) {
$comment = (object)[];
$comment->id = $note->id;
$comment->noteid = $note->noteid;
$comment->content = $note->content;
$comment->candelete = (($canpost && $note->userid === $USER->id) || $candeleteall) ? true : false;
$comment->date = userdate($note->timecreated);
$comments[] = $comment;
}
$results = array(
'noteid' => $params['noteid'],
'commentcount' => count($comments),
'canpost' => $canpost,
'comments' => $comments,
'warnings' => $warnings
);
return $results;
}
/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 2.9
*/
public static function get_comments_returns() {
return new external_single_structure(
[
'canpost' => new external_value(PARAM_BOOL, 'can post comments'),
'noteid' => new external_value(PARAM_INT, 'The note id.'),
'commentcount' => new external_value(PARAM_INT, 'The number of comments.'),
'comments' => new external_multiple_structure(
new external_single_structure(
[
'id' => new external_value(PARAM_INT, 'The comment id.'),
'noteid' => new external_value(PARAM_INT, 'The note id.'),
'candelete' => new external_value(PARAM_BOOL, 'Can delete the comment.'),
'date' => new external_value(PARAM_TEXT, 'The date of the comment.'),
'content' => new external_value(PARAM_RAW, 'The content of the comment.')
]
)
),
'warnings' => new external_warnings()
]
);
}
/**
* Add a note
*
* @return external_function_parameters
* @since Moodle 2.9
*/
public static function add_comment_parameters() {
return new external_function_parameters(
[
'noteid' => new external_value(PARAM_INT, 'id of note'),
'content' => new external_value(PARAM_RAW, 'content of comment'),
]
);
}
/**
* Save a comment.
*
* @param int $noteid The id of the note.
* @param string $content The content of the comment.
*
* @return array of results
*/
public static function add_comment($noteid, $content) {
global $USER, $DB;
$warnings = [];
$arrayparams = [
'noteid' => $noteid,
'content' => $content
];
$params = self::validate_parameters(self::add_comment_parameters(), $arrayparams);
$comment = new \mod_board\comment($params);
$context = $comment->get_context();
self::validate_context($context);
if (!$comment->can_create($context)) {
$results = array(
'count' => '',
'id' => 0,
'warnings' => $warnings
);
return $results;
}
$comment->save();
$results = array(
'id' => $comment->id,
'warnings' => $warnings
);
return $results;
}
/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 2.9
*/
public static function add_comment_returns() {
return new external_single_structure(
[
'id' => new external_value(PARAM_INT, 'The comment id.'),
'warnings' => new external_warnings()
]
);
}
/**
* Delete a comment parameters.
*/
public static function delete_comment_parameters() {
return new external_function_parameters([
'commentid' => new external_value(PARAM_INT, 'The comment id'),
]);
}
/**
* Delete a comment.
*
* @param int $commentid The id of the comment.
*
* @return array of results
*/
public static function delete_comment($commentid) {
global $DB;
$warnings = [];
$arrayparams = [
'commentid' => $commentid,
];
$params = self::validate_parameters(self::delete_comment_parameters(), $arrayparams);
$comment = new \mod_board\comment($params);
$context = $comment->get_context();
self::validate_context($context);
if (!$comment->delete()) {
$warnings[] = [
'item' => $comment->id,
'warningcode' => 'errorcommentnotdeleted',
'message' => 'The comment could not be deleted.'
];
}
$results = array(
'id' => $comment->id,
'warnings' => $warnings
);
return $results;
}
/**
* Returns description of method result value
*/
public static function delete_comment_returns() {
return new external_single_structure(
[
'id' => new external_value(PARAM_INT, 'The comment id.'),
'warnings' => new external_warnings()
]
);
}
/**
* Get the board configuration
* @return external_function_parameters
*/
public static function get_configuration_parameters() {
return new external_function_parameters([
'id' => new external_value(PARAM_INT, 'The board id', VALUE_REQUIRED),
'ownerid' => new external_value(PARAM_INT, 'The ownerid', VALUE_DEFAULT, 0),
]);
}
/**
* Get the board configuration
* @param int $id
* @param int $ownerid
* @return array
*/
public static function get_configuration(int $id, int $ownerid): array {
// Validate recieved parameters.
$params = self::validate_parameters(self::get_configuration_parameters(), [
'id' => $id,
'ownerid' => $ownerid,
]);
// Request and permission validation.
$context = board::context_for_board($params['id']);
self::validate_context($context);
$settings = board::get_configuration($params['id'], $params['ownerid']);
$result['settings'] = json_encode($settings);
$result['warnings'] = [];
return $result;
}
/**
* Get the board configuration
* @return external_single_structure
*/
public static function get_configuration_returns(): external_single_structure {
return new external_single_structure(
array(
'settings' => new external_value(PARAM_RAW, 'The board settings'),
'warnings' => new external_warnings(),
)
);
}
}