forked from Wunderbyte-GmbH/moodle-mod_mooduell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternal.php
1521 lines (1330 loc) · 59.1 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
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* Moodle external API
*
* @package mod_mooduell
* @category external
* @copyright 2020 Wunderbyte Gmbh <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_mooduell\game_control;
use mod_mooduell\manage_tokens;
use mod_mooduell\mooduell;
use mod_mooduell\completion\completion_utils;
use mod_mooduell\utils\wb_payment;
defined('MOODLE_INTERNAL') || die();
require_once('mooduell.php');
/**
* Class mod_mooduell_external
*/
class mod_mooduell_external extends external_api {
/**
* Starts new game against another user.
*
* @param int $courseid
* @param int $quizid
* @param int $playerbid
* @return mixed
*/
public static function start_attempt(int $courseid, int $quizid, int $playerbid) {
$params = [
'courseid' => $courseid,
'quizid' => $quizid,
'playerbid' => $playerbid,
];
$params = self::validate_parameters(self::start_attempt_parameters(), $params);
// Now security checks.
if (!$cm = get_coursemodule_from_id('mooduell', $params['quizid'])) {
throw new moodle_exception('invalidcoursemodule ' . $params['quizid'], 'mooduell', null, null,
"Course module id:" . $params['quizid']);
}
$context = context_module::instance($cm->id);
self::validate_context($context);
// We create Mooduell Instance.
$mooduell = new mooduell($params['quizid']);
// We create the game_controller Instance.
$gamecontroller = new game_control($mooduell);
// We create a new game: Save parameters to DB & trigger notification event.
$startgameresult = $gamecontroller->start_new_game($params['playerbid']);
$result = [];
$result['status'] = $startgameresult;
// Add challenges array with completion data to $startgameresult.
$startgameresult->challenges = completion_utils::get_completion_challenges_array($mooduell);
return $startgameresult;
}
/**
* Defines the parameters for start_attempt.
* @return external_function_parameters
*/
public static function start_attempt_parameters() {
return new external_function_parameters([
'courseid' => new external_value(PARAM_INT, 'course id'),
'quizid' => new external_value(PARAM_INT, 'quizid id'),
'playerbid' => new external_value(PARAM_INT, 'player B id'),
]);
}
/**
* Defines the return values for start_attempt.
* @return external_single_structure
*/
public static function start_attempt_returns() {
return self::get_game_data_returns();
}
/**
* Deletes a single purchase with an id as input
*
* @param int $itemid
* @return array
*/
public static function delete_iapurchases(int $itemid) {
global $DB, $USER;
if ($DB->record_exists('mooduell_purchase', ['id' => $itemid])) {
$DB->delete_records('mooduell_purchase', ['id' => $itemid]);
$returnarray['status'] = 1;
} else {
$returnarray['status'] = 0;
}
return $returnarray;
}
/**
* Define return of iapurchases
*
* @return external_single_structure
*/
public static function delete_iapurchases_returns() {
return new external_single_structure([
'status' => new external_value(PARAM_TEXT, 'status'),
]);
}
/**
* Defines paramters for deleting iapurchases
*
* @return external_function_parameters
*/
public static function delete_iapurchases_parameters() {
return new external_function_parameters(['itemid' => new external_value(PARAM_INT, 'itemid')]);
}
/**
* Returns external web token while using QR webservice token
*/
public static function get_usertoken() {
global $USER;
$params = [];
self::validate_parameters(self::get_usertoken_parameters(), $params);
// Returns mooduell_external token and delete mooduell_tokens token.
$tokenobject = manage_tokens::generate_token_for_user($USER->id, 'mod_mooduell_external', 0);
manage_tokens::delete_user_token('mod_mooduell_tokens');
$token = $tokenobject->token;
$return['token'] = $token;
return $return;
}
/**
* Defines return structure for get_ustertoken()
*
* @return external_single_structure
*/
public static function get_usertoken_returns() {
return new external_single_structure([
'token' => new external_value(PARAM_RAW, 'token'),
]);
}
/**
* Defines parameters for get_ustertoken()
*
*/
public static function get_usertoken_parameters() {
return new external_function_parameters([]);
}
/**
* Returns all courses for user with capabilities
*
* @return array
*/
public static function get_courses_with_caps() {
global $USER;
$userid = $USER->id;
self::validate_parameters(self::get_courses_with_caps_parameters(), []);
$allcourses = enrol_get_users_courses($userid);
$capcourses = [];
foreach ($allcourses as $course) {
$context = context_course::instance($course->id);
$hascaps = has_capability('mod/mooduell:canpurchase', $context);
if ($hascaps) {
$item = [
'courseid' => $course->id,
'coursename' => $course->fullname,
];
$capcourses[] = $item;
}
}
$return['courses'] = $capcourses;
return $return;
}
/**
* Get the courses with caps returns
*
* @return external_single_structure
*/
public static function get_courses_with_caps_returns() {
return new external_single_structure([
'courses' => new external_multiple_structure(new external_single_structure([
'courseid' => new external_value(PARAM_INT, 'id of course'),
'coursename' => new external_value(PARAM_TEXT, 'name of course'),
])),
]);
}
/**
* Defines params structure for get_courses_with_caps()
*
* @return external_function_parameters
*/
public static function get_courses_with_caps_parameters() {
return new external_function_parameters([]);
}
/**
* Returns all quizzes for user with capabilities
*
* @param int $userid
* @return array
*/
public static function get_quizzes_with_caps(int $userid = null) {
global $USER;
$userid = $USER->id;
self::validate_parameters(self::get_quizzes_with_caps_parameters(), []);
$allcourses = enrol_get_users_courses($userid);
$capcourses = [];
foreach ($allcourses as $index => $course) {
$context = context_course::instance($course->id);
$hascaps = has_capability('mod/mooduell:canpurchase', $context);
if ($hascaps) {
$capcourses[$index] = $course;
}
}
$quizzes = get_all_instances_in_courses("mooduell", $capcourses);
if (!empty($quizzes)) {
$returquizzes = [];
foreach ($quizzes as $quiz) {
// Entry to return.
$quizdetails = [];
$quizdetails['quizid'] = $quiz->coursemodule;
$quizdetails['quizname'] = $quiz->name;
$quizdetails['courseid'] = $quiz->course;
$returnquizzes[] = $quizdetails;
}
} else {
$returnquizzes = [];
}
$returnarray['quizzes'] = $returnquizzes;
return $returnarray;
}
/**
* Defines return structure for get_quizzes_with_caps()
*
* @return external_single_structure
*/
public static function get_quizzes_with_caps_returns() {
return new external_single_structure([
'quizzes' => new external_multiple_structure(new external_single_structure([
'quizid' => new external_value(PARAM_INT, 'id of quiz'),
'quizname' => new external_value(PARAM_TEXT, 'name of quiz'),
'courseid' => new external_value(PARAM_INT, 'id of course'),
])),
]);
}
/**
* Defines params structure for get_quizzes_with_caps()
*
* @return external_function_parameters
*/
public static function get_quizzes_with_caps_parameters() {
return new external_function_parameters([]);
}
/**
* Return support information to client.
* @return array
*/
public static function get_mooduell_support() {
global $DB;
$url = get_config('mooduell', 'supporturl');
$pay = wb_payment::pro_version_is_activated();
$badges = get_config('mooduell', 'disablebadges');
$themeimg = get_config('mod_mooduell', 'companylogo');
$themeimgalt = get_config('mod_mooduell', 'companylogoalternative');
$themejson = get_config('mod_mooduell', 'themejsonarea');
// Set minimum requirem App Version here.
$versions = [
"ios" => '1.0.0',
"android" => '0.9.0',
];
$support = [
'url' => $url,
'badges' => $badges,
'unlock' => $pay,
'versions' => $versions,
'themeimg' => $themeimg,
'themejson' => $themejson,
'themeimgalt' => $themeimgalt,
];
self::validate_parameters((self::get_mooduell_support_parameters()), []);
return $support;
}
/**
* Defines support return structure.
*
* @return external_single_structure
*/
public static function get_mooduell_support_returns() {
return new external_single_structure([
'url' => new external_value(PARAM_TEXT, 'url'),
'badges' => new external_value(PARAM_BOOL, 'badges'),
'unlock' => new external_value(PARAM_BOOL, 'unlock'),
'versions' => new external_single_structure(
[
"ios" => new external_value(PARAM_RAW, 'ios app version'),
"android" => new external_value(PARAM_RAW, 'android app version'),
]),
'themeimg' => new external_value(PARAM_TEXT, 'themeimg'),
'themeimgalt' => new external_value(PARAM_TEXT, 'themeimgalt'),
'themejson' => new external_value(PARAM_RAW, 'themejson'),
]);
}
/**
* Defines support input parameters.
*
* @return external_function_parameters
*/
public static function get_mooduell_support_parameters() {
return new external_function_parameters([]);
}
/**
* Gets purchases from Database.
*
* @return array
*/
public static function get_mooduell_purchases() {
global $COURSE, $USER;
$context = context_course::instance($COURSE->id);
self::validate_context($context);
$enrolledcourses = enrol_get_users_courses($USER->id, true);
$quizzes = get_all_instances_in_courses("mooduell", $enrolledcourses);
self::validate_parameters(self::get_mooduell_purchases_parameters(), []);
return mooduell::get_purchases($enrolledcourses, $quizzes);
}
/**
* Defines return Parameters for get_mooduell_purchases.
*
* @return external_single_structure
*/
public static function get_mooduell_purchases_returns() {
return new external_single_structure([
'purchases' => new external_multiple_structure(new external_single_structure(
[
'id' => new external_value(PARAM_INT, 'id'),
'productid' => new external_value(PARAM_TEXT, 'productid'),
'purchasetoken' => new external_value(PARAM_TEXT, 'purchasetoken'),
'receipt' => new external_value(PARAM_TEXT, 'receipt', VALUE_OPTIONAL, ''),
'signature' => new external_value(PARAM_TEXT, 'signature', VALUE_OPTIONAL, ''),
'orderid' => new external_value(PARAM_TEXT, 'orderid', VALUE_OPTIONAL, ''),
'free' => new external_value(PARAM_INT, 'free', VALUE_OPTIONAL, 0),
'userid' => new external_value(PARAM_INT, 'userid'),
'mooduellid' => new external_value(PARAM_INT, 'mooduellid', VALUE_OPTIONAL, 0),
'platformid' => new external_value(PARAM_TEXT, 'platformid', VALUE_OPTIONAL, ''),
'courseid' => new external_value(PARAM_INT, 'courseid', VALUE_OPTIONAL, 0),
'store' => new external_value(PARAM_TEXT, 'store', VALUE_OPTIONAL, ''),
'ispublic' => new external_value(PARAM_INT, 'ispublic'),
'timecreated' => new external_value(PARAM_INT, 'timecreated', VALUE_OPTIONAL, 0),
])),
]);
}
/**
* Defines Webservice Parameters for get_mooduell purchases.
*
* @return external_function_parameters
*/
public static function get_mooduell_purchases_parameters() {
return new external_function_parameters([]);
}
/**
* Stores a purchases to Database.
*
* @param string $productid
* @param string $purchasetoken
* @param string $receipt
* @param string $signature
* @param string $orderid
* @param string $free
* @param int $mooduellid
* @param int $courseid
* @param string $store
* @param int $ispublic
* @return array
*/
public static function update_iapurchases(string $productid, string $purchasetoken, string $receipt = null,
string $signature = null, string $orderid = null, string $free = null, int $mooduellid,
int $courseid = null, string $store, int $ispublic ) {
global $USER, $CFG;
$params = [
'productid' => $productid,
'purchasetoken' => $purchasetoken,
'receipt' => $receipt,
'signature' => $signature,
'orderid' => $orderid,
'free' => $free,
'mooduellid' => $mooduellid,
'courseid' => $courseid,
'store' => $store,
'ispublic' => $ispublic,
];
$params = self::validate_parameters(self::update_iapurchases_parameters(), $params);
$params['userid'] = $USER->id;
if ($params['productid'] === 'unlockplatformsubscription') {
$params['platformid'] = $CFG->wwwroot;
}
return mooduell::purchase_item($params);
}
/**
* Return params for iapurchases.
*
* @return external_single_structure
*/
public static function update_iapurchases_returns() {
return new external_single_structure([
'status' => new external_value(PARAM_TEXT, 'status'),
'itemid' => new external_value(PARAM_INT, 'itemid'),
'type' => new external_value(PARAM_TEXT, 'type'),
]);
}
/**
* Webservice params for iapurchases.
*
* @return external_function_parameters
*/
public static function update_iapurchases_parameters() {
return new external_function_parameters([
'productid' => new external_value(PARAM_RAW, 'productid'),
'purchasetoken' => new external_value(PARAM_RAW, 'purchasetoken'),
'receipt' => new external_value(PARAM_RAW, 'signature'),
'signature' => new external_value(PARAM_RAW, 'signature'),
'orderid' => new external_value(PARAM_RAW, 'orderid'),
'free' => new external_value(PARAM_INT, 'free'),
'mooduellid' => new external_value(PARAM_INT, 'mooduellid'),
'courseid' => new external_value(PARAM_INT, 'platformid'),
'store' => new external_value(PARAM_TEXT, 'store'),
'ispublic' => new external_value(PARAM_INT, 'ispublic'),
]);
}
/**
* We answer a question with the array of ids of the answers. Depending on the internal setting of the MooDuell Instance...
* ... we might either retrieve an array of the correct answer-ids ...
* ... or an array of with one value 0 for incorrect and 1 for correctly answered.
* @param int $quizid
* @param int $gameid
* @param int $questionid
* @param array $answerids IDs of answers given to single or multiple choice questions.
* @return array
* @throws coding_exception
* @throws dml_exception
* @throws invalid_parameter_exception
* @throws moodle_exception
* @throws restricted_context_exception
*/
public static function answer_question(int $quizid, int $gameid, int $questionid, array $answerids = []) {
global $DB;
$params = [
'quizid' => $quizid,
'gameid' => $gameid,
'questionid' => $questionid,
'answerids' => $answerids,
];
$params = self::validate_parameters(self::answer_question_parameters(), $params);
// Now security checks.
if (!$cm = get_coursemodule_from_id('mooduell', $params['quizid'])) {
throw new moodle_exception('invalidcoursemodule ' . $params['quizid'], 'mooduell', null, null,
"Course module id:" . $params['quizid']);
}
$context = context_module::instance($cm->id);
self::validate_context($context);
// We create the MooDuell instance.
$mooduell = new mooduell($params['quizid']);
// We create the game_controller instance.
$gamecontroller = new game_control($mooduell, $params['gameid']);
// Validate the question.
list ($response, $iscorrect, $answersfeedback) =
$gamecontroller->validate_question($params['questionid'], $params['answerids']);
$result['response'] = $response;
$result['iscorrect'] = $iscorrect;
// Answer-specific feedback and param to show or not show it.
if (!empty($answersfeedback)) {
$result['showanswersfeedback'] = $mooduell->settings->showanswersfeedback;
if ($mooduell->settings->showanswersfeedback == 1) {
// We only transfer the feedback JSON if the setting is turned on.
$result['answersfeedback'] = $answersfeedback;
} else {
$result['answersfeedback'] = [];
}
} else {
$result['answersfeedback'] = [];
// If there is no answer-specific feedback we always set the param to zero.
$result['showanswersfeedback'] = 0;
}
// Get the general feedback and set the param to show or not show it.
if ($generalfeedback = $DB->get_field('question', 'generalfeedback', ['id' => $questionid])) {
$result['generalfeedback'] = strip_tags($generalfeedback);
// Show (1) or don't show (0) general feedback depending on setting.
$result['showgeneralfeedback'] = $mooduell->settings->showgeneralfeedback;
} else {
$result['generalfeedback'] = '';
$result['showgeneralfeedback'] = 0;
}
$result['showgeneralfeedback'] = $mooduell->settings->showgeneralfeedback;
return $result;
}
/**
* Defines the paramters of answer_question.
* @return external_function_parameters
*/
public static function answer_question_parameters() {
return new external_function_parameters([
'quizid' => new external_value(PARAM_INT, 'quizid id'),
'gameid' => new external_value(PARAM_INT, 'gameid id'),
'questionid' => new external_value(PARAM_INT, 'question id'),
'answerids' => new external_multiple_structure(new external_value(PARAM_RAW, 'answer id'),
'Array of answer ids'),
]);
}
/**
* Defines the return value of answer_question.
* @return external_single_structure
*/
public static function answer_question_returns() {
return new external_single_structure(
[
'response' => new external_multiple_structure(
// For numerical questions, it will contain the correct answer.
new external_value(PARAM_RAW, 'ids of correct answers, correct answer OR 0 if false, 1 if true')
),
'iscorrect' => new external_value(PARAM_INT, '0 if false, 1 if true'),
'generalfeedback' => new external_value(PARAM_TEXT, 'general feedback'),
'showgeneralfeedback' => new external_value(PARAM_INT, '0 if false, 1 if true'),
'answersfeedback' => new external_multiple_structure(
new external_single_structure(
[
"answerid" => new external_value(PARAM_RAW, 'answer id'),
"answertext" => new external_value(PARAM_RAW, 'answer text'),
"feedback" => new external_value(PARAM_RAW, 'answer-specific feedback'),
]
)
),
'showanswersfeedback' => new external_value(PARAM_INT, '0 if false, 1 if true'),
]
);
}
/**
* Get all the open or closed MooDuell Games. By providing a date, we can limit the treated entries...
* ... to those which were upadted since the last glance we took.
* An empty courseid will return all the games of all the MooDuell Instances visible to this user.
*
* @param array $courseids
* @param int $timemodified
* @return array
* @throws coding_exception
* @throws dml_exception
* @throws invalid_parameter_exception
* @throws moodle_exception
*/
public static function get_games_by_courses(array $courseids, int $timemodified) {
$context = context_system::instance();
self::validate_context($context);
// We just call our function here to get all the quizzes.
$returnedquizzes = self::get_quizzes_by_courses($courseids, $timemodified);
// But we only want the quizzes array, no warnings.
$warnings = $returnedquizzes['warnings'];
$quizzes = $returnedquizzes['quizzes'];
$returnedquizzes = [];
// Now we run through all the quizzes to find the matching games.
foreach ($quizzes as $quiz) {
// We create Mooduell Instance.
$instanceid = $quiz['coursemodule'];
$mooduell = new mooduell($instanceid);
// We create the game_controller Instance.
// This function is used by the external webservice, here we only provide studentsview.
// This means we only provide games where the active player is involved.
$games = $mooduell->return_games_for_this_instance(true, null, $timemodified);
$quiz['challenges'] = completion_utils::get_completion_challenges_array($mooduell);
if ($games && count($games) > 0) {
foreach ($games as $game) {
$quiz['games'][] = [
'gameid' => $game->id,
'playeraid' => $game->playeraid,
'playerbid' => $game->playerbid,
'playeratime' => $game->playeratime,
'playerbtime' => $game->playerbtime,
'winnerid' => $game->winnerid,
'status' => $game->status,
'timecreated' => $game->timecreated,
'timemodified' => $game->timemodified,
];
}
} else {
$quiz['games'] = [];
}
$returnedquizzes[] = $quiz;
}
$result = [];
$result['quizzes'] = $returnedquizzes;
$result['warnings'] = $warnings;
return $result;
}
/**
* Describes the parameters for get_games_by_courses.
* @return external_function_parameters
* @since Moodle 3.1
*/
public static function get_games_by_courses_parameters() {
return new external_function_parameters([
'courseids' => new external_multiple_structure(new external_value(PARAM_INT, 'course id'),
'Array of course ids', VALUE_DEFAULT, []),
'timemodified' => new external_value(PARAM_INT, 'timemodified to reduce number of returned items',
VALUE_DEFAULT, -1),
]
);
}
/**
* Describes the values returned by get_games_by_courses.
* @return external_single_structure
*/
public static function get_games_by_courses_returns() {
return new external_single_structure([
'quizzes' => new external_multiple_structure(new external_single_structure([
'quizid' => new external_value(PARAM_INT, 'id of coursemodule'),
'quizname' => new external_value(PARAM_RAW, 'name of quiz'),
'courseid' => new external_value(PARAM_INT, 'courseid'),
'coursename' => new external_value(PARAM_RAW, 'coursename'),
'usefullnames' => new external_value(PARAM_INT, 'usefullnames'),
'showcorrectanswer' => new external_value(PARAM_INT, 'showcorrectanswer'),
'showcontinuebutton' => new external_value(PARAM_INT, 'showcontinuebutton'),
'showgeneralfeedback' => new external_value(PARAM_INT, 'showgeneralfeedback'),
'showanswersfeedback' => new external_value(PARAM_INT, 'showanswersfeedback'),
'countdown' => new external_value(PARAM_INT, 'countdown'),
'waitfornextquestion' => new external_value(PARAM_INT, 'waitfornextquestion'),
'isteacher' => new external_value(PARAM_INT, 'isteacher'),
'challenges' => new external_multiple_structure(new external_single_structure([
'id' => new external_value(PARAM_INT, 'challenge id'),
'challengename' => new external_value(PARAM_TEXT, 'challenge name'),
'challengetype' => new external_value(PARAM_TEXT, 'challenge type'),
'actualnumber' => new external_value(PARAM_INT, 'actual number'),
'status' => new external_value(PARAM_TEXT, 'challenge status'),
'targetnumber' => new external_value(PARAM_INT, 'target number'),
'challengepercentage' => new external_value(PARAM_INT, 'challenge percentage'),
'targetdate' => new external_value(PARAM_INT,
'unix timestamp of expected completion'),
'challengerank' => new external_value(PARAM_INT,
'a user\'s ranking within a challenge'),
'localizedstrings' => new external_multiple_structure(
new external_single_structure([
'lang' => new external_value(PARAM_TEXT, 'language identifier'),
'stringkey' => new external_value(PARAM_TEXT, 'string identifier'),
'stringval' => new external_value(PARAM_TEXT, 'string value'),
])
),
]
)
),
'games' => new external_multiple_structure(new external_single_structure([
'gameid' => new external_value(PARAM_INT, 'id of game'),
'playeraid' => new external_value(PARAM_INT, 'id of player A'),
'playerbid' => new external_value(PARAM_INT, 'id of player B'),
'playeratime' => new external_value(PARAM_INT, 'time of player B'),
'playerbtime' => new external_value(PARAM_INT, 'time of player B'),
'status' => new external_value(PARAM_INT,
'status, NULL is open game, 1 is player A\'s turn, 2 is player B\'s turn, 3 is finished'),
'winnerid' => new external_value(PARAM_INT, 'id of winner, 0 is not yet finished'),
'timemodified' => new external_value(PARAM_INT, 'time modified'),
])),
])),
]);
}
/**
* Get all the quizzes (Mooduell Instances) by courses.
*
* @param array $courseids
* @param int $timemodfied
* @return array
* @throws coding_exception
* @throws invalid_parameter_exception
*/
public static function get_quizzes_by_courses(array $courseids, int $timemodfied) {
$warnings = [];
$returnedquizzes = [];
$params = [
'courseids' => $courseids,
'timemodified' => $timemodfied,
];
$params = self::validate_parameters(self::get_quizzes_by_courses_parameters(), $params);
$mycourses = [];
if (empty($params['courseids'])) {
$mycourses = enrol_get_my_courses();
$params['courseids'] = array_keys($mycourses);
}
// Ensure there are courseids to loop through.
if (!empty($params['courseids'])) {
list ($courses, $warnings) = external_util::validate_courses($params['courseids'], $mycourses, false, true);
// Get the quizzes in this course, this function checks users visibility permissions.
// We can avoid then additional validate_context calls.
$quizzes = get_all_instances_in_courses("mooduell", $courses);
// If there are not MooDuell Instances at all in the courses we wanted, we return an exception.
if (count($quizzes) == 0) {
throw new moodle_exception('nomooduellincourses ', 'mooduell', null, null,
"There are no MooDuell instances in the courses you were looking in");
}
foreach ($quizzes as $quiz) {
$context = context_module::instance($quiz->coursemodule);
$course = get_course($quiz->course);
// Entry to return.
$quizdetails = [];
// First, we return information that any user can see in the web interface.
$quizdetails['quizid'] = $quiz->coursemodule;
$quizdetails['quizname'] = 'testname';
$quizdetails['usefullnames'] = $quiz->usefullnames;
$quizdetails['showcontinuebutton'] = $quiz->showcontinuebutton;
$quizdetails['showcorrectanswer'] = $quiz->showcorrectanswer;
$quizdetails['showgeneralfeedback'] = $quiz->showgeneralfeedback;
$quizdetails['showanswersfeedback'] = $quiz->showanswersfeedback;
$quizdetails['countdown'] = $quiz->countdown;
$quizdetails['waitfornextquestion'] = $quiz->waitfornextquestion;
$quizdetails['courseid'] = $quiz->course;
$quizdetails['coursename'] = $course->fullname;
$quizdetails['coursemodule'] = $quiz->coursemodule;
$quizdetails['quizname'] = external_format_string($quiz->name, $context->id);
if (has_capability('mod/quiz:view', $context)) {
// Fields only for managers.
if (has_capability('moodle/course:manageactivities', $context)) {
// We could do something here.
$quizdetails['isteacher'] = 1;
} else {
$quizdetails['isteacher'] = 0;
}
} else {
$quizdetails['isteacher'] = 0;
}
$returnedquizzes[] = $quizdetails;
}
}
$result = [];
$result['quizzes'] = $returnedquizzes;
$result['warnings'] = $warnings;
return $result;
}
/**
* Defines the parameters for get_quizzes_by_courses.
* @return external_function_parameters
*/
public static function get_quizzes_by_courses_parameters() {
return new external_function_parameters([
'courseids' => new external_multiple_structure(new external_value(PARAM_INT, 'course id'),
'Array of course ids', VALUE_DEFAULT, []),
'timemodified' => new external_value(PARAM_INT, 'timemodified to reduce number of returned items',
VALUE_DEFAULT, -1),
]
);
}
/**
* Defines the return value of get_quizzes_by_courses.
* @return external_single_structure
*/
public static function get_quizzes_by_courses_returns() {
return new external_single_structure([
'quizzes' => new external_multiple_structure(new external_single_structure([
'quizid' => new external_value(PARAM_INT, 'id of coursemodule'),
'quizname' => new external_value(PARAM_RAW, 'name of quiz'),
'courseid' => new external_value(PARAM_INT, 'courseid'),
'coursename' => new external_value(PARAM_RAW, 'coursename'),
'usefullnames' => new external_value(PARAM_INT, 'usefullnames'),
'showcorrectanswer' => new external_value(PARAM_INT, 'showcorrectanswer'),
'showcontinuebutton' => new external_value(PARAM_INT, 'showcontinuebutton'),
'showgeneralfeedback' => new external_value(PARAM_INT, 'showgeneralfeedback'),
'showanswersfeedback' => new external_value(PARAM_INT, 'showanswersfeedback'),
'countdown' => new external_value(PARAM_INT, 'countdown'),
'waitfornextquestion' => new external_value(PARAM_INT, 'waitfornextquestion'),
'isteacher' => new external_value(PARAM_INT, 'isteacher'),
])),
]);
}
/**
* Returns data of active game.
*
* @param int $courseid
* @param int $quizid
* @param int $gameid
* @return stdClass
* @throws coding_exception
* @throws dml_exception
* @throws invalid_parameter_exception
* @throws moodle_exception
* @throws restricted_context_exception
*/
public static function get_game_data(int $courseid, int $quizid, int $gameid) {
$params = [
'courseid' => $courseid,
'quizid' => $quizid,
'gameid' => $gameid,
];
$params = self::validate_parameters(self::get_game_data_parameters(), $params);
// Now security checks.
if (!$cm = get_coursemodule_from_id('mooduell', $params['quizid'])) {
throw new moodle_exception('invalidcoursemodule ' . $params['quizid'], 'mooduell', null, null,
"Course module id:" . $params['quizid']);
}
$context = context_module::instance($cm->id);
self::validate_context($context);
// We create the Mooduell instance.
$mooduell = new mooduell($params['quizid']);
// We create the game_controller Instance.
$gamecontroller = new game_control($mooduell, $params['gameid']);
// We can now retrieve the questions and add them to our gamedata.
$gamedata = $gamecontroller->get_questions();
// Add challenges JSON string with completion data to $gamedata.
$gamedata->challenges = completion_utils::get_completion_challenges_array($mooduell);
// On every call, we see if completion is already done.
$course = get_course($params['courseid']);
$completion = new completion_info($course);
if ($cm->completion != COMPLETION_TRACKING_MANUAL) {
// This calculates the completion and saves the state.
// Which will also trigger the completion_update event, which triggers badges.
$completion->update_state($cm);
}
return $gamedata;
}
/**
* Describes the parameters for get_game_data.
*
* @return external_function_parameters
* @since Moodle 3.1
*/
public static function get_game_data_parameters() {
return new external_function_parameters([
'courseid' => new external_value(PARAM_INT, 'course id'),
'quizid' => new external_value(PARAM_INT, 'quizid id'),
'gameid' => new external_value(PARAM_INT, 'gameid id'),
]);
}
/**
* Describes the return values for get_game_data.
* @return external_single_structure
*/
public static function get_game_data_returns() {
return new external_single_structure([
'mooduellid' => new external_value(PARAM_INT, 'mooduellid'),
'gameid' => new external_value(PARAM_INT, 'gameid'),
'playeraid' => new external_value(PARAM_INT, 'player A id'),
'playerbid' => new external_value(PARAM_INT, 'player B id'),
'winnerid' => new external_value(PARAM_INT, 'winner id'),
'timemodified' => new external_value(PARAM_INT, 'time modified'),
'status' => new external_value(PARAM_INT, 'status'),
'challenges' => new external_multiple_structure(new external_single_structure([
'id' => new external_value(PARAM_INT, 'challenge id'),
'challengename' => new external_value(PARAM_TEXT, 'challenge name'),
'challengetype' => new external_value(PARAM_TEXT, 'challenge type'),
'actualnumber' => new external_value(PARAM_INT, 'actual number'),
'status' => new external_value(PARAM_TEXT, 'challenge status'),
'targetnumber' => new external_value(PARAM_INT, 'target number'),
'challengepercentage' => new external_value(PARAM_INT, 'challenge percentage'),
'targetdate' => new external_value(PARAM_INT,
'unix timestamp of expected completion date'),
'challengerank' => new external_value(PARAM_INT,
'a user\'s ranking within a challenge'),
'localizedstrings' => new external_multiple_structure(
new external_single_structure([
'lang' => new external_value(PARAM_TEXT, 'language identifier'),
'stringkey' => new external_value(PARAM_TEXT, 'string identifier'),
'stringval' => new external_value(PARAM_TEXT, 'string value'),
])
),
])),
'questions' => new external_multiple_structure(new external_single_structure([
'questionid' => new external_value(PARAM_INT, 'questionid'),
'questiontext' => new external_value(PARAM_RAW, 'question text'),
'questiontype' => new external_value(PARAM_RAW, 'qtype'),
'category' => new external_value(PARAM_INT, 'category'),
'playeraanswered' => new external_value(PARAM_INT, 'answer player a'),
'playerbanswered' => new external_value(PARAM_INT, 'answer player a'),
'imageurl' => new external_value(PARAM_RAW, 'image URL'),
'imagetext' => new external_value(PARAM_RAW, 'image Text'),
'answers' => new external_multiple_structure(new external_single_structure([
'id' => new external_value(PARAM_INT, 'answerid'),
'answertext' => new external_value(PARAM_RAW,
'answer text'),
])),
'combinedfeedback' => new external_single_structure([
'correctfeedback' => new external_value(PARAM_TEXT, 'correct feedback'),
'partiallycorrectfeedback' => new external_value(PARAM_TEXT,
'partially correct feedback'),
'incorrectfeedback' => new external_value(PARAM_TEXT, 'incorrect feedback'),
]),
])),
]);
}
/**
* Returns available users for quiz.
* @param int $courseid
* @param int $quizid
* @return array
* @throws coding_exception
* @throws dml_exception
* @throws invalid_parameter_exception
* @throws moodle_exception
* @throws restricted_context_exception
*/
public static function get_quiz_users(int $courseid, int $quizid) {
$params = [
'courseid' => $courseid,
'quizid' => $quizid,