Skip to content

Cannot create and import answers without an image for an quiz #1894

@pro2s

Description

@pro2s

Hi, I encountered the problem that I can't save answers to questions in a quiz that doesn't contain an image, and later when exporting and importing such answers, they are not imported.
It seems for my database using an empty string instead of null in 'image_id' does not allow saving the answer to database.
I had to patch the plugin to be able to use the null value in Image ID.
If the plans are fixed it this or is it possible to somehow use answers without images for MariaDB without changing the plugin?

There are patches:

diff --git a/helpers/QueryHelper.php b/helpers/QueryHelper.php
--- a/helpers/QueryHelper.php
+++ b/helpers/QueryHelper.php
@@ -174,7 +174,11 @@
 				if ( $sanitize_value && $do_sanitize ) {
 					$sanitize_value = sanitize_text_field( $sanitize_value );
 				}
-				$column_values .= is_numeric( $sanitize_value ) ? $sanitize_value . ',' : "'$sanitize_value'" . ',';
+				$column_values .= match( true ) {
+					is_null( $sanitize_value ) => 'NULL,',
+					is_numeric( $sanitize_value ) => $sanitize_value . ',',
+					default => "'$sanitize_value',",
+				};
 			// Trim trailing comma.
 			$column_keys   = rtrim( $column_keys, ',' );
diff --git a/classes/QuizBuilder.php b/classes/QuizBuilder.php
--- a/classes/QuizBuilder.php
+++ b/classes/QuizBuilder.php
@@ -98,7 +98,7 @@
                        'belongs_question_type' => $question_type,
                        'answer_title'          => $answer_title,
                        'is_correct'            => $is_correct,
-                       'image_id'              => $image_id,
+                       'image_id'              => empty ( $image_id ) ? null : $image_id,
                        'answer_two_gap_match'  => $answer_two_gap_match,
                        'answer_view_format'    => $answer_view_format,
                        'answer_settings'       => $answer_settings,
```

```
WordPress version:
6.6.2
WordPress multisite: ✓
WordPress debug mode: -
Language: en_US

Tutor version: 3.6.1
WordPress memory limit: 512 MB
WordPress Cron: -
External object cache: ✓

Server environment
Server info: 
PHP version: 8.1.32

PHP post max size: 100 MB
PHP time limit: 30

cURL version: 7.81.0, OpenSSL/3.0.2
Language: en_US

MySQL version
Server: 10.6.22-MariaDB, Client: mysqlnd 8.1.32
Default timezone is UTC: ✓
fsockopen/cURL: ✓
DOMDocument: ✓
GZip: ✓
Multibyte string: ✓
```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions