Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
2.3k views
in Q2A Core by

I want to prevent users losing half-finished questions and answers through lost connections etc.

I realize (thanks to NoahY) that simply saving a half finished form to the database is useless, as this would be immediately visible to all viewers.

Now I'm tring to understand exactly where to look to add that functionality. The obvious place to me is the WYSIWYG plugin, but it might be easier to extend Question2Answer.

I think you can see from my questions that I am no programmer. If the only answer is for me to learn to program, then I am willing to do so, but I'd rather not reinvent the wheel. The phrase:

and of course you'll need to add in q2a parameters - most of the form fields don't have ids, so you have to add them in as well if you want to get their values easily

means very little to me. I hope I do not sound lazy or ungrateful, I just do not want to waste time trying to get something working when it is clear that I am looking in the wrong direction.

related to an answer for: Is there a simple autosave for CKEditor?

3 Answers

+1 vote
by
Well, probably the easiest would be to just make the posts hidden by default, as per this post:

http://www.question2answer.org/qa/2978/is-there-any-way-to-make-new-questions-hidden-by-default#a3089

Then once you submit the form, you would be shown the hidden post, which you could then "show".

As for the IDs, you need a custom layer, like this:

<?php

    class qa_html_theme_layer extends qa_html_theme_base {
        
        function doctype(){
            qa_error_log($_POST);
            global $qa_state;
            if(strpos($qa_state,'edit') === 0) {
                if(isset($this->content['form_q_edit'])) {
                    $this->content['form_q_edit']['fields']['title']['tags'] = @$this->content['form_q_edit']['fields']['title']['tags'].' id="qtitle"';
                    $this->content['form_q_edit']['fields']['notify']['tags'] = @$this->content['form_q_edit']['fields']['notify']['tags'].' id="notify"';
                    $this->content['form_q_edit']['hidden']['AUTO_SAVE_URL'] = qa_self_html().'" id="AUTO_SAVE_URL';
                }
            }
            qa_html_theme_base::doctype();
        }
    }

 

This allows you to get the fields using jQuery.  I just put this in a file called qa-wysiwyg-layer.php, then added to the qa-plugin.php file.  More information is here:

http://www.question2answer.org/layers.php

Then you have to modify the js file in the autosave plugin to get the required data from the form and process it.  But I still don't see this as being feasible; suppose someone writes half a post, then changes their mind?  They will end up with half of a hidden post somewhere.  It wouldn't work for editing either, since you couldn't hide those posts.

What you need, I think, is an entirely new draft posting system, where the post edits are saved in some table like a wiki.  All pretty complicated, not worth the effort, in my mind.
by
Thank you very much.
I agree - it is just too complicated for what I need, but I appreciate you making that clear for me.
0 votes
by

I think this would be possible in a plugin with a few steps:

  • A new database table storing drafts, essentially a simpler version of qa_posts with fields like type, title, userid, content, parentid, and the date the draft is written (so you can delete old drafts later).
  • A page plugin that you can post data to and it will store/update a draft - no output display apart from a message you can read with AJAX.
  • A layer widget that adds some Javascript to post to that page plugin, when the user is asking or answering a question.
  • Probably another plugin (if not part of the AJAX layer) that retrieves draft content when the user visits a certain page, i.e. when they load a question page the plugin checks if the user has a saved answer draft and fills in the answer form for them.

Based on what you say this may be outside your expertise, but hopefully the above is useful if anyone else wants to take on the idea.

by
Instead of posting all the data to the server, and handling it later, we could just save the text using Jquery within a cookie (that can store up to 5 MB). Should be the easiest solution.
by
You'd be better off using HTML5 localStorage which can store up to 5 MB. Cookies are not usable for this problem because they are sent to the server on every request. You don't want to be sending 5 MB on every page load (and image and JS and CSS file)!!
by
okay I correct myself: you should not have text more than 40 kb :)
0 votes
by
Thank you to NoahY and DisgruntledGoat. You both add a tremendous amount to my understanding and enjoyment of Question2Answer.

As noted in my original question, I now believe that autosave should not be bloating web applications.

I have solved it with Lazarus, an add-on for Firefox, which I now recommend to my visitors as the best way to avoid losing incomplete submissons.
...