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

I do not want the same title for questions. Each question title in my forum is supposed to hold detailed information.

How would you change the core to prevent duplicate question titles?

 

I wrote a filter plugin to filter question text. There I can also access the question['title']. But how can I throw an error again? return ... ?

I am not at my computer, that's why i ask... as a memo, and maybe you want to implement it as well =)

1 Answer

+1 vote
by
edited by
 
Best answer

If an element can have Errors, your function can declare its value invalid by adding a textual error report with the same key to the $errors array.

Filter function receives substancean entity of Q2A form as argument. First($question) and second($errors) arguments are array. And, each array's key is same.

filter_question(&$question, &$errors, $oldquestion)

Example code:

require_once QA_INCLUDE_DIR.'qa-filter-basic.php';
class qa_question_title_checker {
  function filter_question(&$question, &$errors, $oldquestion) {
    // $fb = new qa_filter_basic();
    $result = ....select count('title') from qa_posts where type='Q' and title=question['title'] ....; // It-like processing...
    if($result) {
      $errors['title'] = 'Your question already exists.';
    }
}

Mimicking the program that I made, I made it with impromptu composition. Error exists !! Sorry.

My opinion:

The number of questions is enormous. Whole sentence compare may be worsen performance... ?

by
Note: It must be $errors['title']

You missed the "s" in the example code ;o)
by
edited by
In the filter module I am using this now:

// check if question title already exists, prevent duplicate
$quTitleExists = qa_db_read_one_value( qa_db_query_sub('SELECT title
                            FROM `^posts`
                            WHERE title = #
                            AND type = "Q"
                            LIMIT 1', $question['title']), true
                );

if( $quTitleExists && is_null($oldquestion) ) {
    $errors['title'] = 'This question already exists.';
}

Note: is_null($oldquestion) is necessary as editing would be blocked otherwise as the update has the same title! → check out http://www.question2answer.org/qa/25347/using-filter-module-find-question-posted-question-updated
by
Hi
Did you guys create a plugin for this?
by
In that page I insert this code?
...