Update question preview window from question_edit_form

Update question preview window from question_edit_form

by Juergen Zimmer -
Number of replies: 4

Dear all, 


I'm developing a question type and in my question edit form the number of answers is flexible, similar to MC questions. Currently, I realise a change in the number of answers by submitting the form, after changing the number of options (a change via JS AJAX was very difficult because the editors have to be rendered, initialised, etc). 


My problem:

 When a question preview window is open, that window should be updated/reloaded after changing the number of options (i.e. after submitting the form and a page reload). 

My first try was to override the preview button/link and do
   var previewWindow = window.open(previewUrl, 'PreviewQuestion', 'height=620,width=750');

after the page reload I do 

   var existingWindow = window.open('', 'PreviewQuestion');

which is an undocumented javascript feature and doesn't work in FF Windows because it always just opens a new tab whenever the window does not exist. So I thought I try to keep track of whether the preview window is open or not. 

I tried to add a hidden field "previewopen" to the form and set it's value with JS. 

However, the value doesn't seem to be in the submitted data (maybe because it only takes the fields that are actually in the $question object?).

Has anyone an idea how I could implement the feature that the preview window always shows the current state of the question as it is stored in the database?

THanks

  Juergen 



Average of ratings: -
In reply to Juergen Zimmer

Re: Update question preview window from question_edit_form

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If you just call Window.open with the preview URL, it will reload in the same window/tab. So, pass a real URL, not a blank one.

In reply to Tim Hunt

Re: Update question preview window from question_edit_form

by Juergen Zimmer -

Sorry, maybe I'm missing something, but if I put this in the Javascript that is loaded with my question edit page then preview will always be opened. The problem I have is determining whether the preview had been open before the 'update' button was pressed and the page was reloaded. 

Maybe this explains better, why I did what I did:

https://medium.com/@bluepnume/every-known-way-to-get-references-to-windows-in-javascript-223778bede2d

under "Existing Popup Window".

In reply to Juergen Zimmer

Re: Update question preview window from question_edit_form

by Juergen Zimmer -

OK, in a session cookie in window.sessionStorage I can store the fact that the preview is open. Now the problem is to delete the cookie when the user closes the preview window...

In reply to Juergen Zimmer

Re: Update question preview window from question_edit_form

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Ah! Sorry. I did not think fo that.