1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
/// A section that checks the user's understanding of the concepts presented in a tutorial.
public struct TutorialAssessmentsRenderSection: RenderSection, Equatable {
public var kind: RenderSectionKind = .assessments
/// The questions for this assessment section.
public var assessments: [Assessment]
/// An identifier for this section of the page.
///
/// The identifier can be used to construct an anchor link to this section of the page.
public var anchor: String
/// The display title for an assessments section.
public static let title = "Check Your Understanding"
/// A render-friendly representation of an assessment question.
public struct Assessment: Codable, TextIndexing, Equatable {
/// The type of assessment question.
///
/// The default value is `multiple-choice`.
public var type = "multiple-choice"
/// The title of the assessment.
public var title: [RenderBlockContent]
/// The content of this question.
///
/// This content includes the phrasing of the question.
public var content: [RenderBlockContent]?
/// The possible answers to this multiple-choice question.
public var choices: [Choice]
/// A render-friendly representation of an answer to a
/// multiple-choice assessment question.
public struct Choice: Codable, Equatable {
/// The content of the choice.
public var content: [RenderBlockContent]
/// A Boolean value that determines whether this choice is correct.
public var isCorrect: Bool
/// An explanation of why this choice is correct or incorrect.
public var justification: [RenderBlockContent]?
/// Additional text that can be displayed if this choice is selected.
public var reaction: String?
/// Creates a new choice from the given parameters.
///
/// - Parameters:
/// - content: The content of the choice.
/// - isCorrect: A Boolean value that determines whether this choice is correct.
/// - justification: An explanation of why this choice is correct or incorrect.
/// - reaction: Additional text that can be displayed if this choice is selected.
public init(content: [RenderBlockContent], isCorrect: Bool, justification: [RenderBlockContent]?, reaction: String?) {
self.content = content
self.isCorrect = isCorrect
self.justification = justification
}
}
/// Creates a new multiple-choice assessment question from the given parameters.
///
/// - Parameters:
/// - title: The title of the assessment.
/// - content: The content of the question.
/// - choices: The possible answers to this question.
public init(title: [RenderBlockContent], content: [RenderBlockContent]?, choices: [Choice]) {
self.title = title
self.content = content
self.choices = choices
}
}
/// Creates a new assessment section from the given list of questions.
///
/// - Parameters:
/// - assessments: The questions for this assessment section.
/// - anchor: An identifier for this assessment section.
public init(assessments: [Assessment], anchor: String) {
self.assessments = assessments
self.anchor = anchor
}
}
// Diffable conformance
extension TutorialAssessmentsRenderSection: RenderJSONDiffable {
/// Returns the differences between this TutorialAssessmentsRenderSection and the given one.
func difference(from other: TutorialAssessmentsRenderSection, at path: CodablePath) -> JSONPatchDifferences {
var diffBuilder = DifferenceBuilder(current: self, other: other, basePath: path)
diffBuilder.addDifferences(atKeyPath: \.kind, forKey: CodingKeys.kind)
diffBuilder.addDifferences(atKeyPath: \.assessments, forKey: CodingKeys.assessments)
diffBuilder.addDifferences(atKeyPath: \.anchor, forKey: CodingKeys.anchor)
return diffBuilder.differences
}
/// Returns if this TutorialAssessmentsRenderSection is similar enough to the given one.
func isSimilar(to other: TutorialAssessmentsRenderSection) -> Bool {
return self.assessments == other.assessments
}
}
// Diffable conformance
extension TutorialAssessmentsRenderSection.Assessment: RenderJSONDiffable {
/// Returns the differences between this Assessment and the given one.
func difference(from other: TutorialAssessmentsRenderSection.Assessment, at path: CodablePath) -> JSONPatchDifferences {
var diffBuilder = DifferenceBuilder(current: self, other: other, basePath: path)
diffBuilder.addDifferences(atKeyPath: \.title, forKey: CodingKeys.title)
diffBuilder.addDifferences(atKeyPath: \.content, forKey: CodingKeys.content)
diffBuilder.addDifferences(atKeyPath: \.choices, forKey: CodingKeys.choices)
return diffBuilder.differences
}
/// Returns if this Assessment is similar enough to the given one.
func isSimilar(to other: TutorialAssessmentsRenderSection.Assessment) -> Bool {
return self.title == other.title || self.content == other.content
}
}
// Diffable conformance
extension TutorialAssessmentsRenderSection.Assessment.Choice: RenderJSONDiffable {
/// Returns the differences between this Choice and the given one.
func difference(from other: TutorialAssessmentsRenderSection.Assessment.Choice, at path: CodablePath) -> JSONPatchDifferences {
var diffBuilder = DifferenceBuilder(current: self, other: other, basePath: path)
diffBuilder.addDifferences(atKeyPath: \.content, forKey: CodingKeys.content)
diffBuilder.addDifferences(atKeyPath: \.isCorrect, forKey: CodingKeys.isCorrect)
diffBuilder.addDifferences(atKeyPath: \.justification, forKey: CodingKeys.justification)
diffBuilder.addDifferences(atKeyPath: \.reaction, forKey: CodingKeys.reaction)
return diffBuilder.differences
}
/// Returns if this Choice is similar enough to the given one.
func isSimilar(to other: TutorialAssessmentsRenderSection.Assessment.Choice) -> Bool {
return self.content == other.content
}
}
|