@@ -161,6 +161,40 @@ private function process_submit_form(block_vmchecker\form\submit_form $form) {
161161 return true ;
162162 }
163163
164+
165+ /**
166+ * Loads the assignment of the current block.
167+ * @param int $assignmentid
168+ * @param int $courseid
169+ * @return \assign|null
170+ */
171+ private function load_assignment (int $ assignmentid , int $ courseid ): \assign |null {
172+ if ($ assignmentid == null ) {
173+ return null ;
174+ }
175+
176+ $ context = null ;
177+ try {
178+ $ course_info = get_fast_modinfo ($ courseid );
179+ foreach ($ course_info ->instances ['assign ' ] as $ instance ) {
180+ if ($ instance ->instance != $ assignmentid ) {
181+ continue ;
182+ }
183+
184+ $ context = \context_module::instance ($ instance ->id );
185+ break ;
186+ }
187+ } catch (dml_missing_record_exception | dml_multiple_records_exception $ e ) {
188+ return null ;
189+ }
190+
191+ if (!$ context ) {
192+ return null ;
193+ }
194+
195+ return new \assign ($ context , null , null );
196+ }
197+
164198 /**
165199 * Get the HTML content for the block.
166200 * @return string
@@ -178,27 +212,12 @@ public function get_content() {
178212
179213 $ this ->content = new stdClass ();
180214
181- if ($ this ->config ->assignment == null ) {
215+ $ assign = $ this ->load_assignment ($ this ->config ->assignment , $ this ->page ->course ->id );
216+ if ($ assign == null ) {
182217 $ this ->content ->text = get_string ('no_assignment_selected ' , 'block_vmchecker ' );
183218 return $ this ->content ;
184219 }
185220
186- try {
187- $ course_info = get_fast_modinfo ($ this ->page ->course ->id );
188- foreach ($ course_info ->instances ['assign ' ] as $ instance ) {
189- if ($ instance ->instance == $ this ->config ->assignment ) {
190- $ cm = $ instance ;
191- break ;
192- }
193- }
194- } catch (dml_missing_record_exception | dml_multiple_records_exception $ e ) {
195- $ this ->content ->text = get_string ('no_assignment_selected ' , 'block_vmchecker ' );
196- return $ this ->content ;
197- }
198- $ context = \context_module::instance ($ cm ->id );
199-
200- $ assign = new \assign ($ context , null , null );
201-
202221 $ this ->set_title ($ assign );
203222 $ backendurl = get_config ('block_vmchecker ' , 'backend ' );
204223 $ api = new \block_vmchecker \backend \api ($ backendurl );
@@ -316,6 +335,10 @@ public function instance_create() {
316335 public function instance_config_save ($ data , $ nolongerused = false ) {
317336 global $ DB ;
318337
338+ if (!$ DB ->record_exists ('block_vmchecker_options ' , array ('blockinstanceid ' => $ this ->instance ->id ))) {
339+ $ this ->instance_create ();
340+ }
341+
319342 parent ::instance_config_save ($ data , $ nolongerused );
320343 $ DB ->update_record ('block_vmchecker_options ' , [
321344 'id ' => $ DB ->get_record ('block_vmchecker_options ' , array ('blockinstanceid ' => $ this ->instance ->id ), 'id ' )->id ,
0 commit comments