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

I have one question related to How to forbid user to edit his own question? 

As I must prevent users editing their posts I need to disable that option.

How I hacked the qa-page-question-view.php now:

At the end of function qa_page_q_post_rules() I added:

$rules['editable'] = $rules['editbutton'] = $rules['hideable'] = false;
$level=qa_get_logged_in_level();
if ($level>=QA_USER_LEVEL_ADMIN) {
   $rules['editable'] = $rules['editbutton'] = $rules['hideable'] = true;
}


This works (and I hope is correct). could be coded shorter of course.


However, it would be great to have a time-based disable of the edit button. So how can I check how old the question is?

Something like 10 minutes (for sure wrong call, and does it give unix timestamp?):

if($this -> $post['created']>10*60) { ... }

Thanks!

by
Thanks for your code but I spend ages to understand why moderators couldn't edit post, so I find out it was coming from your code, then I add :

  if ($level>=QA_USER_LEVEL_MODERATOR) {
        $rules['editable'] = $rules['editbutton'] = $rules['hideable'] = true;
    }
    // only admin can close questions (and check if question is already closed)
    $rules['closeable'] = ($level>=QA_USER_LEVEL_MODERATOR) && (!$rules['closed']);
by
Sorry to hear that. This is the downside with core hacks.

Unfortunately with v1.6.3 there is no "proper" way to change the rules with a plugin. I only could override core function function qa_page_q_post_rules() - but this won't be good for new versions of q2a where this function might be changed...

3 Answers

+4 votes
by

I have an old version of q2a, namely: 1.4, but I used to use the following code in order get the month and day of a question:

$created = $question['raw']['created'];

$day = date('j',$created); //get day of which question was crated
$month = date('n',$created); //get month of which question was crated

You should try whether this works:

$month = date('h',$created);

Hopefully you will get the hour back on which the question was crated. And then compare that value with the current time and decide whether someone has the right to edit his/her question.


 

by
Thanks Aslan that helped me to find the solution. 1.5 is different, you need to call $post['created'] instead of $question['raw']['created'];
+4 votes
by
edited by

Thanks @Aslan who gave me a start. This is how I solved it in qa-page-question-view.php:

// users are never allowed to hide posts
$rules['hideable'] = false;

/* users are not allowed to edit posts after 10 min */
$timestamp = time(); // time now
// 10 min time frame to edit
$rules['editable'] = $rules['editbutton'] = ($timestamp - $post['created'] < 600);

// admin has all rights
$level=qa_get_logged_in_level();
if ($level>=QA_USER_LEVEL_ADMIN) {
    $rules['editable'] = $rules['editbutton'] = $rules['hideable'] = true;
}

 

Seems to work!

by
Thanks for this code. Been looking for this for awhile. I think this should be a core feature of Q2A.
by
I just realized that the "edit" button is displayed now even for other posts that don't belong to the user. To change this, change line from above:
$rules['editable'] = $rules['editbutton'] = ($timestamp - $post['created'] < 600);

to:
$rules['editable'] = $rules['editbutton'] = $rules['isbyuser'] && ($timestamp - $post['created'] < 600);

now it works... clean!
by
i am using v1.5.4 but this code is no working and all users are able to edit and hide their posts. plz help !
0 votes
by
reshown by
Where to insert this code? line numbers?

thks
...