1 | <?php |
||
2 | // This file is part of Moodle - http://moodle.org/ |
||
3 | // |
||
4 | // Moodle is free software: you can redistribute it and/or modify |
||
5 | // it under the terms of the GNU General Public License as published by |
||
6 | // the Free Software Foundation, either version 3 of the License, or |
||
7 | // (at your option) any later version. |
||
8 | // |
||
9 | // Moodle is distributed in the hope that it will be useful, |
||
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
12 | // GNU General Public License for more details. |
||
13 | // |
||
14 | // You should have received a copy of the GNU General Public License |
||
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
||
16 | |||
17 | /** |
||
18 | * The assignsubmission_edulegit core class. |
||
19 | * |
||
20 | * @package assignsubmission_edulegit |
||
21 | * @author Alex Crosby <[email protected]> |
||
22 | * @copyright @2024 EduLegit.com |
||
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||
24 | */ |
||
25 | |||
26 | namespace assignsubmission_edulegit; |
||
27 | |||
28 | /** |
||
29 | * Callback handler for the edulegit plugin. |
||
30 | * |
||
31 | * This class handles incoming payloads and processes specific events |
||
32 | * related to the EduLegit webhook request. |
||
33 | * |
||
34 | * @package assignsubmission_edulegit |
||
35 | * @author Alex Crosby <[email protected]> |
||
36 | * @copyright @2024 EduLegit.com |
||
37 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||
38 | */ |
||
39 | class edulegit_callback { |
||
40 | |||
41 | /** |
||
42 | * Instance of the edulegit submission repository. |
||
43 | * |
||
44 | * @var edulegit_submission_repository |
||
45 | */ |
||
46 | protected edulegit_submission_repository $repository; |
||
47 | |||
48 | /** |
||
49 | * Constructor for edulegit_callback class. |
||
50 | * |
||
51 | * @param edulegit_submission_repository|null $repository An instance of edulegit_submission_repository or null. |
||
52 | */ |
||
53 | public function __construct(?edulegit_submission_repository $repository = null) { |
||
54 | $this->repository = $repository ?? new edulegit_submission_repository(); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Handles incoming payload and processes the event. |
||
59 | * |
||
60 | * @param object $payload The payload object containing the event and data. |
||
61 | * @return mixed Returns the result of the event handling or null if the event is not recognized. |
||
62 | */ |
||
63 | public function handle(object $payload) { |
||
64 | $event = $payload->event ?? null; |
||
65 | $data = $payload->data ?? []; |
||
66 | |||
67 | if ($event == 'taskUser.sync') { |
||
68 | return $this->sync_task_user($data); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
69 | } |
||
70 | return null; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Synchronizes task user information with the repository. |
||
75 | * |
||
76 | * @param object $data The data object containing the task user's information. |
||
77 | * @return int|null Returns the submission ID if successful, or null if not. |
||
78 | */ |
||
79 | private function sync_task_user($data) { |
||
80 | $id = $data->externalId ?? null; |
||
81 | if (!$id) { |
||
82 | return null; |
||
83 | } |
||
84 | |||
85 | $submission = $this->repository->get_by_id($id); |
||
86 | |||
87 | if (!$submission) { |
||
88 | return null; |
||
89 | } |
||
90 | |||
91 | $submission->status = 1; |
||
92 | $submission->error = ''; |
||
93 | |||
94 | if (isset($data->title)) { |
||
95 | $submission->title = $data->title; |
||
96 | } |
||
97 | if (isset($data->content)) { |
||
98 | $submission->content = $data->content; |
||
99 | } |
||
100 | if (isset($data->url)) { |
||
101 | $submission->url = $data->url; |
||
102 | } |
||
103 | if (isset($data->authKey)) { |
||
104 | $submission->authkey = $data->authKey; |
||
105 | } |
||
106 | if (isset($data->score)) { |
||
107 | $submission->score = $data->score; |
||
108 | } |
||
109 | if (isset($data->plagiarism)) { |
||
110 | $submission->plagiarism = $data->plagiarism; |
||
111 | } |
||
112 | if (isset($data->aiAverageProbability)) { |
||
113 | $submission->airate = $data->aiAverageProbability; |
||
114 | } |
||
115 | if (isset($data->aiProbability)) { |
||
116 | $submission->aiprobability = $data->aiProbability; |
||
117 | } |
||
118 | if (isset($data->loginTimeToken)) { |
||
119 | $submission->userkey = $data->loginTimeToken; |
||
120 | } |
||
121 | |||
122 | return $this->repository->update_submission($submission) ? $submission->id : null; |
||
123 | } |
||
124 | } |
||
125 |