|
| 1 | +# Custom Questions in Standalone UI |
| 2 | + |
| 3 | +This directory contains example implementations of custom question types for the Vue Skuilder platform. These examples demonstrate how to create new `Question` subclasses and their corresponding Vue components, and how to integrate them into your application. |
| 4 | + |
| 5 | +## Example Questions Provided |
| 6 | + |
| 7 | +- **SimpleTextQuestion**: A basic question that asks for a text input and checks for an exact string match. |
| 8 | +- **MultipleChoiceQuestion**: Presents a question with multiple options and checks for the correct selection. |
| 9 | +- **NumberRangeQuestion**: Asks for a numeric input and validates if it falls within a specified range. |
| 10 | + |
| 11 | +## How to Use These Examples |
| 12 | + |
| 13 | +Each question type consists of two main parts: |
| 14 | +1. A TypeScript file (`.ts`) defining the `Question` subclass, which handles the question logic, data shapes, and answer evaluation. |
| 15 | +2. A Vue component file (`.vue`) that provides the user interface for the question. |
| 16 | + |
| 17 | +These examples are already integrated into the `exampleCourse.ts` file, which you can use to see them in action. |
| 18 | + |
| 19 | +## Integrating Custom Questions into Your Course at Runtime |
| 20 | + |
| 21 | +To use your custom questions in a course, you need to: |
| 22 | + |
| 23 | +1. **Define your Question Class**: Create a new TypeScript file (e.g., `MyCustomQuestion.ts`) that extends the `Question` class from `@vue-skuilder/courses`. Define its `dataShapes` and `views` static properties. |
| 24 | + |
| 25 | + ```typescript |
| 26 | + // MyCustomQuestion.ts |
| 27 | + import { Question, DataShape, ViewData, Answer } from '@vue-skuilder/courses'; |
| 28 | + import { FieldType } from '@vue-skuilder/common'; |
| 29 | + import MyCustomQuestionView from './MyCustomQuestionView.vue'; |
| 30 | + |
| 31 | + export class MyCustomQuestion extends Question { |
| 32 | + public static dataShapes: DataShape[] = [ |
| 33 | + new DataShape('MyCustomQuestion', [ |
| 34 | + { name: 'myField', type: FieldType.STRING }, |
| 35 | + ]), |
| 36 | + ]; |
| 37 | + |
| 38 | + public static views = [ |
| 39 | + { name: 'MyCustomQuestionView', component: MyCustomQuestionView }, |
| 40 | + ]; |
| 41 | + |
| 42 | + constructor(data: ViewData[]) { |
| 43 | + super(data); |
| 44 | + // Initialize your question data from `data` |
| 45 | + } |
| 46 | + |
| 47 | + public dataShapes(): DataShape[] { |
| 48 | + return MyCustomQuestion.dataShapes; |
| 49 | + } |
| 50 | + |
| 51 | + public views() { |
| 52 | + return MyCustomQuestion.views; |
| 53 | + } |
| 54 | + |
| 55 | + protected isCorrect(answer: Answer): boolean { |
| 56 | + // Implement your answer evaluation logic here |
| 57 | + return false; |
| 58 | + } |
| 59 | + } |
| 60 | + ``` |
| 61 | + |
| 62 | +2. **Create Your Vue Component**: Create a Vue component (e.g., `MyCustomQuestionView.vue`) that will render your question and allow user interaction. This component will receive props based on the `ViewData` you define for your question. |
| 63 | + |
| 64 | + ```vue |
| 65 | + <!-- MyCustomQuestionView.vue --> |
| 66 | + <template> |
| 67 | + <div> |
| 68 | + <p>{{ questionData.myField }}</p> |
| 69 | + <!-- Your input elements and UI --> |
| 70 | + <button @click="submitAnswer">Submit</button> |
| 71 | + </div> |
| 72 | + </template> |
| 73 | +
|
| 74 | + <script setup lang="ts"> |
| 75 | + import { ref } from 'vue'; |
| 76 | + import { useStudySessionStore } from '@vue-skuilder/common-ui'; |
| 77 | +
|
| 78 | + const props = defineProps({ |
| 79 | + // Define props based on your question's data |
| 80 | + questionData: { type: Object, required: true }, |
| 81 | + }); |
| 82 | +
|
| 83 | + const studySessionStore = useStudySessionStore(); |
| 84 | + const userAnswer = ref(''); |
| 85 | +
|
| 86 | + const submitAnswer = () => { |
| 87 | + // Collect user's answer and submit it |
| 88 | + studySessionStore.submitAnswer({ response: userAnswer.value }); |
| 89 | + }; |
| 90 | + </script> |
| 91 | + ``` |
| 92 | + |
| 93 | +3. **Register Your Question and Course**: In your application's entry point (e.g., `src/main.ts` or `src/App.vue`), you need to import your custom question and include it in a `Course` instance. Then, register this course with the `allCourses` list. |
| 94 | + |
| 95 | + ```typescript |
| 96 | + // src/main.ts (example) |
| 97 | + import { createApp } from 'vue'; |
| 98 | + import App from './App.vue'; |
| 99 | + import { createPinia } from 'pinia'; |
| 100 | + import { allCourses, Course } from '@vue-skuilder/courses'; |
| 101 | +
|
| 102 | + // Import your custom question |
| 103 | + import { MyCustomQuestion } from './questions/MyCustomQuestion'; |
| 104 | +
|
| 105 | + // Create a new Course instance with your custom question |
| 106 | + const myCustomCourse = new Course('MyCustomCourse', [ |
| 107 | + new MyCustomQuestion([{ myField: 'Hello Custom Question!' }]), |
| 108 | + ]); |
| 109 | +
|
| 110 | + // Add your custom course to the global allCourses list |
| 111 | + allCourses.courses.push(myCustomCourse); |
| 112 | +
|
| 113 | + const app = createApp(App); |
| 114 | + app.use(createPinia()); |
| 115 | + app.mount('#app'); |
| 116 | + ``` |
| 117 | + |
| 118 | + **Note**: The `allCourses` object is a singleton that manages all available courses and their associated questions and views. By adding your custom course to `allCourses.courses`, it becomes discoverable by the `CardViewer` and other components that rely on the course registry. |
| 119 | + |
| 120 | +## Developing New Questions |
| 121 | + |
| 122 | +When developing new questions, consider the following: |
| 123 | + |
| 124 | +- **DataShape Definition**: Carefully define the `DataShape` for your question. This dictates the structure of the data that will be passed to your question's constructor and Vue component. |
| 125 | +- **Answer Evaluation**: Implement the `isCorrect` method in your `Question` subclass to define how user answers are evaluated. |
| 126 | +- **Vue Component Props**: Ensure your Vue component's `props` match the data fields defined in your `DataShape` and any additional data you pass from your `Question` instance. |
| 127 | +- **StudySessionStore**: Use the `useStudySessionStore()` composable from `@vue-skuilder/common-ui` to submit user answers and interact with the study session logic. |
| 128 | + |
| 129 | +Feel free to modify and extend the provided examples to suit your needs. |
0 commit comments