# Excel and Word exam question import

## Using the feature

1. Sign in as an Admin or Trainer (teacher).
2. Create an exam under Online Exams, including its module, duration, total marks and pass marks. Leave its status as **Draft**.
3. Open **Manage Questions > Import Questions**, or use the import icon in the exam list.
4. Download the Excel or Word template. Replace the example questions. Excel uses one row per question on the `Questions` sheet; Word uses one two-column Field / Value table per question.
5. Upload the completed `.xlsx` or `.docx` file and select **Validate and Preview**.
6. Review the question text, choices, answers and marks. Select **Import Questions** to save all questions together.
7. Review the exam and activate it when ready.

Teachers can import only into exams they created. Admins can import into any exam. Imports require a draft exam with no student attempts. Existing questions remain. Duplicate question text (ignoring case and repeated whitespace) is rejected. Preview expires after 30 minutes; uploading another file replaces the current preview.

## Supported fields

Keep all nine field names unchanged:

| Field | Rule |
| --- | --- |
| `question_text` | Required plain-text question. |
| `question_type` | `multiple_choice` or `true_false`. |
| `marks` | Positive whole number, up to 999999. Combined marks, including existing questions, must not exceed the exam total. |
| `option_a` | Required for multiple choice. |
| `option_b` | Required for multiple choice. |
| `option_c` | Optional multiple-choice option. |
| `option_d` | Optional multiple-choice option. |
| `correct_answer` | A/B/C/D referencing a filled option, or TRUE/FALSE. |
| `explanation` | Optional plain-text explanation. |

Each option has a maximum of 500 characters. True/false options are supplied automatically. A file can contain at most 500 questions and be at most 5 MB. Formulas, images, merged Excel cells, tracked Word changes and password-protected files are unsupported. Save older `.xls` / `.doc` files as `.xlsx` / `.docx` using the supplied template layout. Arbitrary Word exam layouts are not parsed; all questions must be inside the template tables. The template examples will be imported if you leave them in the file.

## Deploying to your existing server

Back up the files being replaced, then upload the following paths into the application's `elearninghub` directory, preserving the folders:

- `core/ExamImport.php` (new)
- `public/modules/import_exam.php` (new)
- `public/modules/exam_questions.php` (updated)
- `public/modules/exams.php` (updated)
- `resources/exam_templates/exam_questions_template.xlsx` (new)
- `resources/exam_templates/exam_questions_template.docx` (new)

No database migration or Composer update is needed. The new importer reads the templates using PHP ZIP, DOM/XML and mbstring; it does not load Composer, so it does not introduce the certificate dependency error. The full application still needs PHP 8.2 or newer for its existing Composer libraries. Set `upload_max_filesize` to at least `5M` and `post_max_size` to at least `8M` if you want to permit the full upload limit. The server also needs working PHP sessions and the existing InnoDB exam/question tables.

After deployment, create a test Draft exam with at least three total marks. Import either unchanged template, confirm that two questions and three marks appear, then remove the test exam using your normal admin workflow. Test with a Trainer account as well before regular use.

## Developer checks

Run `php tests/exam_import_test.php` for template and invalid-file tests. Add `--database` for transaction and role checks against the configured local MySQL connection. Database tests use connection-local temporary tables that shadow `exams`, `questions` and `exam_attempts`; they do not modify application exam data. The test account needs CREATE TEMPORARY TABLES permission. The tests also verify complete rollback when the second insert fails.
