@@ -66,49 +66,67 @@ protected function createInteractionHole()
66
66
*/
67
67
protected function getHtml ()
68
68
{
69
- $ this ->textHtml = $ this ->qtiTextWithHoles ;
69
+ $ this ->textHtml = ' <my-wrapper> ' . $ this ->qtiTextWithHoles . ' </my-wrapper> ' ;
70
70
$ newId = 1 ;
71
- $ regex = '(<textEntryInteraction.*?>|<inlineChoiceInteraction.*?</inlineChoiceInteraction>) ' ;
72
- preg_match_all ($ regex , $ this ->qtiTextWithHoles , $ matches );
73
- foreach ($ matches [0 ] as $ matche ) {
74
- $ tabMatche = explode ('" ' , $ matche );
75
- $ responseIdentifier = $ tabMatche [1 ];
71
+ $ dom = new \DOMDocument ();
72
+ $ dom ->loadXML ($ this ->textHtml );
73
+ $ xpath = new \DOMXPath ($ dom );
74
+ $ interactions = $ xpath ->query ('//textEntryInteraction | //inlineChoiceInteraction ' );
75
+
76
+ foreach ($ interactions as $ interaction ) {
77
+ $ responseIdentifier = $ interaction ->attributes ->getNamedItem ('responseIdentifier ' )->nodeValue ;
76
78
$ correctResponse = $ this ->getCorrectResponse ($ responseIdentifier );
77
- if (substr ($ matche , 1 , 20 ) === 'textEntryInteraction ' ) {
78
- $ expectedLength = $ tabMatche [5 ];
79
- $ text = str_replace ('textEntryInteraction ' , 'input ' , $ matche );
80
- /*For old questions with holes */
81
- $ text = preg_replace ('(name=".*?") ' , '' , $ text );
82
- if (isset ($ expectedLength )) {
83
- $ text = str_replace ('size=" ' .$ expectedLength .'" ' , 'size=" ' .$ expectedLength .'" type="text" value=" ' .$ correctResponse .'" ' , $ text );
84
- }
85
- /******************************/
86
- $ text = str_replace ('responseIdentifier=" ' .$ responseIdentifier .'" ' , 'id=" ' .$ newId .'" class="blank" autocomplete="off" name="blank_ ' .$ newId .'" ' , $ text );
87
- $ text = str_replace ('expectedLength=" ' .$ expectedLength .'" ' , 'size=" ' .$ expectedLength .'" type="text" value=" ' .$ correctResponse .'" ' , $ text );
79
+ $ originalText = $ dom ->saveXML ($ interaction );
80
+ $ text = $ dom ->saveXML ($ interaction );
81
+
82
+ if ($ interaction ->tagName === 'textEntryInteraction ' ) {
83
+ $ expectedLength = $ interaction ->attributes ->getNamedItem ('expectedLength ' )->nodeValue ;
84
+ $ text = str_replace ('textEntryInteraction ' , 'input ' , $ text );
85
+ $ text = str_replace (
86
+ 'responseIdentifier=" ' .$ responseIdentifier .'" ' ,
87
+ 'id=" ' .$ newId .'" class="blank" autocomplete="off" name="blank_ ' .$ newId .'" ' ,
88
+ $ text
89
+ );
90
+ $ text = str_replace (
91
+ 'expectedLength=" ' .$ expectedLength .'" ' ,
92
+ 'size=" ' .$ expectedLength .'" type="text" value=" ' .$ correctResponse .'" ' ,
93
+ $ text
94
+ );
88
95
$ this ->createHole ($ expectedLength , $ responseIdentifier , false , $ newId );
89
- } else {
90
- $ text = str_replace ('inlineChoiceInteraction ' , 'select ' , $ matche );
91
- $ text = str_replace ('responseIdentifier=" ' .$ responseIdentifier .'" ' , 'id=" ' .$ newId .'" class="blank" name="blank_ ' .$ newId .'" ' , $ text );
92
- $ text = str_replace ('inlineChoice ' , 'option ' , $ text );
93
- $ regexOpt = '(<option identifier=.*?>) ' ;
94
- preg_match_all ($ regexOpt , $ text , $ matchesOpt );
95
- foreach ($ matchesOpt [0 ] as $ matcheOpt ) {
96
- $ tabMatcheOpt = explode ('" ' , $ matcheOpt );
97
- $ holeID = $ tabMatcheOpt [1 ];
98
- if ($ correctResponse === $ holeID ) {
99
- $ opt = preg_replace ('(\s*identifier=" ' .$ holeID .'") ' , ' holeCorrectResponse="1" ' , $ matcheOpt );
96
+ $ this ->textHtml = str_replace ($ originalText , $ text , $ this ->textHtml );
97
+ ++$ newId ;
98
+ } elseif ($ interaction ->tagName === 'inlineChoiceInteraction ' ) {
99
+ $ text = str_replace (
100
+ 'responseIdentifier=" ' .$ responseIdentifier .'" ' ,
101
+ 'id=" ' .$ newId .'" class="blank" name="blank_ ' .$ newId .'" ' ,
102
+ $ text
103
+ );
104
+ $ text = str_replace ('inlineChoiceInteraction ' , 'select ' , $ text );
105
+
106
+ $ choices = $ interaction ->childNodes ;
107
+
108
+ foreach ($ choices as $ choice ) {
109
+ $ originalChoiceText = $ dom ->saveXML ($ choice );
110
+ $ choiceText = $ dom ->saveXML ($ choice );
111
+ $ holeId = $ choice ->attributes ->getNamedItem ('identifier ' )->nodeValue ;
112
+
113
+ if ($ correctResponse === $ holeId ) {
114
+ $ choiceText = preg_replace ('(\s*identifier=" ' .$ holeId .'") ' , ' holeCorrectResponse="1" ' , $ choiceText );
100
115
} else {
101
- $ opt = preg_replace ('(\s*identifier=" ' .$ holeID .'") ' , ' holeCorrectResponse="0" ' , $ matcheOpt );
116
+ $ choiceText = preg_replace ('(\s*identifier=" ' .$ holeId .'") ' , ' holeCorrectResponse="0" ' , $ choiceText );
102
117
}
103
- $ text = str_replace ($ matcheOpt , $ opt , $ text );
118
+ $ text = str_replace ($ originalChoiceText , $ choiceText , $ text );
104
119
}
120
+ $ text = str_replace ('inlineChoice ' , 'option ' , $ text );
105
121
$ this ->createHole (15 , $ responseIdentifier , true , $ newId );
122
+ $ this ->textHtml = str_replace ($ originalText , $ text , $ this ->textHtml );
123
+ ++$ newId ;
106
124
}
107
- ++$ newId ;
108
- $ this ->textHtml = str_replace ($ matche , $ text , $ this ->textHtml );
109
- $ textHtmlClean = preg_replace ('(<option holeCorrectResponse="0".*?</option>) ' , '' , $ this ->textHtml );
110
- $ textHtmlClean = str_replace (' holeCorrectResponse="1" ' , '' , $ textHtmlClean );
111
125
}
126
+ $ textHtmlClean = preg_replace ('(<option holeCorrectResponse="0".*?</option>) ' , '' , $ this ->textHtml );
127
+ $ textHtmlClean = str_replace (' holeCorrectResponse="1" ' , '' , $ textHtmlClean );
128
+ $ textHtmlClean = str_replace ('<my-wrapper> ' , '' , $ textHtmlClean );
129
+ $ textHtmlClean = str_replace ('</my-wrapper> ' , '' , $ textHtmlClean );
112
130
$ this ->interactionHole ->setHtml ($ textHtmlClean );
113
131
}
114
132
0 commit comments