-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-723 Update logic to store responses in central instance
- Loading branch information
1 parent
a0f6183
commit 2662929
Showing
2 changed files
with
162 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?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/>. | ||
|
||
namespace local_catquiz\remote\response; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
use local_catquiz\remote\hash\question_hasher; | ||
|
||
/** | ||
* Handles storage and processing of remote responses. | ||
* | ||
* @package local_catquiz | ||
* @copyright 2024 Wunderbyte GmbH <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class response_handler { | ||
|
||
/** | ||
* Store a response from a remote instance. | ||
* | ||
* @param string $questionhash The question hash | ||
* @param string $response The response data | ||
* @param int $remoteuserid The user ID from remote instance | ||
* @param string $sourceurl The source URL | ||
* @return bool Success status | ||
*/ | ||
public static function store_response($questionhash, $response, $remoteuserid, $sourceurl) { | ||
global $DB; | ||
|
||
$record = new \stdClass(); | ||
$record->questionhash = $questionhash; | ||
$record->remoteuserid = $remoteuserid; | ||
$record->response = $response; | ||
$record->sourceurl = $sourceurl; | ||
$record->timecreated = time(); | ||
|
||
try { | ||
$DB->insert_record('local_catquiz_remote_responses', $record); | ||
return true; | ||
} catch (\Exception $e) { | ||
debugging('Error storing remote response: ' . $e->getMessage(), DEBUG_DEVELOPER); | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Process stored responses. | ||
* | ||
* @param int $batchsize Number of responses to process in one batch | ||
* @return array Processing results | ||
*/ | ||
public static function process_responses($batchsize = 100) { | ||
global $DB; | ||
|
||
$responses = $DB->get_records('local_catquiz_remote_responses', | ||
['timeprocessed' => null], | ||
'timecreated ASC', | ||
'*', | ||
0, | ||
$batchsize); | ||
|
||
$results = ['processed' => 0, 'errors' => 0]; | ||
|
||
foreach ($responses as $response) { | ||
$questionid = question_hasher::get_questionid_from_hash($response->questionhash); | ||
if (!$questionid) { | ||
self::mark_response_error($response, 'Question not found'); | ||
$results['errors']++; | ||
continue; | ||
} | ||
|
||
try { | ||
// Process response using existing model_responses class. | ||
// This needs to be implemented based on your existing logic. | ||
self::mark_response_processed($response); | ||
$results['processed']++; | ||
} catch (\Exception $e) { | ||
self::mark_response_error($response, $e->getMessage()); | ||
$results['errors']++; | ||
} | ||
} | ||
|
||
return $results; | ||
} | ||
|
||
/** | ||
* Marks a response as processed. | ||
* | ||
* @param \stdClass $response The response record | ||
*/ | ||
private static function mark_response_processed($response) { | ||
global $DB; | ||
$response->timeprocessed = time(); | ||
$response->processinginfo = json_encode(['status' => 'success']); | ||
$DB->update_record('local_catquiz_remote_responses', $response); | ||
} | ||
|
||
/** | ||
* Marks a response as failed with error information. | ||
* | ||
* @param \stdClass $response The response record | ||
* @param string $error The error message | ||
*/ | ||
private static function mark_response_error($response, $error) { | ||
global $DB; | ||
$response->timeprocessed = time(); | ||
$response->processinginfo = json_encode(['status' => 'error', 'message' => $error]); | ||
$DB->update_record('local_catquiz_remote_responses', $response); | ||
} | ||
} |