@@ -532,6 +532,24 @@ public function i_receive_a_push_notification(TableNode $data) {
532532 $ this ->wait_for_pending_js ();
533533 }
534534
535+ /**
536+ * Replace arguments from the content in the given activity field.
537+ *
538+ * @Given /^I replace the arguments in "([^"]+)" "([^"]+)"$/
539+ */
540+ public function i_replace_arguments_in_the_activity (string $ idnumber , string $ field ) {
541+ global $ DB ;
542+
543+ $ coursemodule = $ DB ->get_record ('course_modules ' , compact ('idnumber ' ));
544+ $ module = $ DB ->get_record ('modules ' , ['id ' => $ coursemodule ->module ]);
545+ $ activity = $ DB ->get_record ($ module ->name , ['id ' => $ coursemodule ->instance ]);
546+
547+ $ DB ->update_record ($ module ->name , [
548+ 'id ' => $ coursemodule ->instance ,
549+ $ field => $ this ->replace_arguments ($ activity ->{$ field }),
550+ ]);
551+ }
552+
535553 /**
536554 * Opens a custom link.
537555 *
@@ -889,4 +907,31 @@ public function replace_wwwroot($text) {
889907 return str_replace ('$WWWROOT ' , $ CFG ->behat_wwwroot , $ text );
890908 }
891909
910+ /**
911+ * Replace arguments with the format "${activity:field}" from a string, where "activity" is
912+ * the idnumber of an activity and "field" is the activity's field to get replacement from.
913+ *
914+ * At the moment, the only field supported is "cmid", the id of the course module for this activity.
915+ *
916+ * @param string $text Original text.
917+ * @return string Text with arguments replaced.
918+ */
919+ protected function replace_arguments (string $ text ): string {
920+ global $ DB ;
921+
922+ preg_match_all ("/ \\$ \\{([^:}]+):([^}]+) \\}/ " , $ text , $ matches );
923+
924+ foreach ($ matches [0 ] as $ index => $ match ) {
925+ switch ($ matches [2 ][$ index ]) {
926+ case 'cmid ' :
927+ $ coursemodule = $ DB ->get_record ('course_modules ' , ['idnumber ' => $ matches [1 ][$ index ]]);
928+ $ text = str_replace ($ match , $ coursemodule ->id , $ text );
929+
930+ break ;
931+ }
932+ }
933+
934+ return $ text ;
935+ }
936+
892937}
0 commit comments