@@ -143,7 +143,7 @@ public function load_section($params) {
143
143
$ this ->id = $ feedbackrec ->id ;
144
144
$ this ->surveyid = $ feedbackrec ->surveyid ;
145
145
$ this ->section = $ feedbackrec ->section ;
146
- $ this ->scorecalculation = $ this ->decode_scorecalculation ($ feedbackrec ->scorecalculation );
146
+ $ this ->scorecalculation = $ this ->get_valid_scorecalculation ($ feedbackrec ->scorecalculation );
147
147
$ this ->sectionlabel = $ feedbackrec ->sectionlabel ;
148
148
$ this ->sectionheading = $ feedbackrec ->sectionheading ;
149
149
$ this ->sectionheadingformat = $ feedbackrec ->sectionheadingformat ;
@@ -253,20 +253,20 @@ public function update() {
253
253
254
254
$ this ->scorecalculation = $ this ->encode_scorecalculation ($ this ->scorecalculation );
255
255
$ DB ->update_record (self ::TABLE , $ this );
256
- $ this ->scorecalculation = $ this ->decode_scorecalculation ($ this ->scorecalculation );
256
+ $ this ->scorecalculation = $ this ->get_valid_scorecalculation ($ this ->scorecalculation );
257
257
258
258
foreach ($ this ->sectionfeedback as $ sectionfeedback ) {
259
259
$ sectionfeedback ->update ();
260
260
}
261
261
}
262
262
263
263
/**
264
- * Return the decoded calculation array/
265
- * @param string $codedstring
266
- * @return mixed
264
+ * Decode and ensure scorecalculation is what we expect.
265
+ * @param string|null $codedstring
266
+ * @return array
267
267
* @throws coding_exception
268
268
*/
269
- protected function decode_scorecalculation ($ codedstring ) {
269
+ public static function decode_scorecalculation (? string $ codedstring ): array {
270
270
// Expect a serialized data string.
271
271
if (($ codedstring == null )) {
272
272
$ codedstring = '' ;
@@ -275,11 +275,33 @@ protected function decode_scorecalculation($codedstring) {
275
275
throw new coding_exception ('Invalid scorecalculation format. ' );
276
276
}
277
277
if (!empty ($ codedstring )) {
278
- $ scorecalculation = unserialize ($ codedstring );
278
+ $ scorecalculation = unserialize_array ($ codedstring ) ?: [] ;
279
279
} else {
280
280
$ scorecalculation = [];
281
281
}
282
282
283
+ if (!is_array ($ scorecalculation )) {
284
+ throw new coding_exception ('Invalid scorecalculation format. ' );
285
+ }
286
+
287
+ foreach ($ scorecalculation as $ score ) {
288
+ if (!empty ($ score ) && !is_numeric ($ score )) {
289
+ throw new coding_exception ('Invalid scorecalculation format. ' );
290
+ }
291
+ }
292
+
293
+ return $ scorecalculation ;
294
+ }
295
+
296
+ /**
297
+ * Return the decoded and validated calculation array.
298
+ * @param string $codedstring
299
+ * @return mixed
300
+ * @throws coding_exception
301
+ */
302
+ protected function get_valid_scorecalculation ($ codedstring ) {
303
+ $ scorecalculation = static ::decode_scorecalculation ($ codedstring );
304
+
283
305
// Check for deleted questions and questions that don't support scores.
284
306
foreach ($ scorecalculation as $ qid => $ score ) {
285
307
if (!isset ($ this ->questions [$ qid ])) {
0 commit comments