Need help with plugin to allow only one thread in selected forums
mark-in-dallas Offline
Junior Member
**
Posts: 5
Joined: Jul 2010
Post: #1
Need help with plugin to allow only one thread in selected forums
I am trying to make a plugin to prevent users from posting more than one thread in selected forums.  This should be pretty simple, but I'm not a very experienced programmer, and just can't seem get it right.  I think I'm pretty close, but just not quite there.

Any help would be appreciated.  Here's what I have:

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php

if(!defined("IN_MYBB"))
{
    die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}

$plugins->add_hook("newthread_do_newthread_end", "limitthreadsinforums_addthread");

function limitthreadsinforums_info()
{
    return array(
        "name"            => "Limit to one thread in selected Forums",
        "description"    => "Limits users from posting more than one thread in selected forums!",
        "website"        => "http://websitesdallas.com",
        "author"        => "mark-in-dallas",
        "authorsite"    => "http://websitesdallas.com",
        "version"        => "1.0",
        "guid"            => "",
        "compatibility" => "1*",
    );
}

function limitthreadsinforums_activate()
{
	global $db;
	
	$limitthreadsinforums_group = array(
		"name"			=> "limitonethreadinforums",
		"title"			=> "Limit to one thread post in specified forums",
		"description"	=> "Limits users from posting more than one thread in specified forums",
		"disporder"		=> "2",
		"isdefault"		=> "no",
	);
	
	$db->insert_query("settinggroups", $limitthreadsinforums_group);
	$gid = $db->insert_id();
	
	$new_setting = array(
		'name'			=> 'limitthreadsinforums',
		'title'			=> 'Limit users to posting only one thread in specified forums.  Separate FIDs with commas',
		'description'	=> 'Forums to limit',
		'optionscode'	=> 'text',
		'value'			=> '',
		'disporder'		=> '1',
		'gid'			=> intval($gid)
	);

	$db->insert_query('settings', $new_setting);
	
	rebuildsettings();
}

function limitthreadsinforums_deactivate()
{
	global $db;
	

	$db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name='limitthreadsinforums'");
	$db->delete_query("settinggroups","name='limitonethreadinforum'");
	

	
	rebuildsettings();
}

function limitthreadsinforums_addthread()
{
        global $new_thread, $mybb, $db, $fid;
		
        if (empty($new_thread['fid']))
			return;	

		if (empty($mybb->settings['fid']))
			return;		

		$limit = explode(",", $mybb->settings['fid']);
		$forum = $new_thread['fid'];

			if (in_array($forum,$limit))			
		
		$query = $db->simple_select("threads", "uid");		
		$uid = explode(",", $db->threads['uid']);
		$forum = $new_thread['fid'];	

		if (in_array($forum,$uid))			

		{
			exit("You cannot post more than one thread in this forum");
		}				
        
		elseif (empty($new_thread['uid']))
		return;

			
	
}		

?>

(This post was last modified: 07-06-2010 09:52 AM by mark-in-dallas.)
07-06-2010 09:52 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #2
RE: Need help with plugin to allow only one thread in selected forums
Okay, firstly the hook newthread_do_newthread_end is probably too late for your purposes.  The check should be done before the thread is inserted, not after.
Try hooking into datahandler_post_validate_thread and perform the check there.
Obviously, your function will need to be changed to reflect this new hook.

You seem to have some idea of what's going on, but you have some issues in this piece of code:

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
			if (in_array($forum,$limit))			
		
		$query = $db->simple_select("threads", "uid");		
		$uid = explode(",", $db->threads['uid']);
		$forum = $new_thread['fid'];	

		if (in_array($forum,$uid))			

		{
			exit("You cannot post more than one thread in this forum");
		}				
        
		elseif (empty($new_thread['uid']))
		return;

There's a number of problems, including your lack of braces around the if block, there is no $db->threads['uid'] variable (check MyBB code to see how it performs queries with the $db->fetch_array function), and if (in_array($forum,$uid)) doesn't really make sense (why check if a forum is in a uid?).

Some other issues - be more consistent with the following two lines:

PHP Code:
	
	$db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name='limitthreadsinforums'");
	$db->delete_query("settinggroups","name='limitonethreadinforum'");


Also, rebuildsettings() should be rebuild_settings().  Your compatibility should probably be set to 14* and higher, since your code won't work on MyBb 1.2.

Hope that helps.


My Blog
07-06-2010 10:29 AM
Find all posts by this user Quote this message in a reply
mark-in-dallas Offline
Junior Member
**
Posts: 5
Joined: Jul 2010
Post: #3
RE: Need help with plugin to allow only one thread in selected forums
Thanks Yumi!  I'll work on it again tomorrow and see if I can't get a bit further.  It was actually driving me a bit crazy because I tried exiting the script if the array containing the fid in settings and fid in new_thread if there was a match, and the thread still posted.  your explaination makes sense as to why.

I tried placing braces around the 2nd aray, but that didn't work either, so I took them back out.  The rebuildsettings has worked, but I'll change it anyway.

Eventually I will get it.   Thanks for you help!
07-06-2010 11:03 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #4
RE: Need help with plugin to allow only one thread in selected forums
(07-06-2010 11:03 AM)mark-in-dallas Wrote:  I tried placing braces around the 2nd aray, but that didn't work either, so I took them back out.
if-block, not array

PHP Code:
if(condition)
{
  // statements
}


(07-06-2010 11:03 AM)mark-in-dallas Wrote:  The rebuildsettings has worked, but I'll change it anyway.
The function is depreciated, which is why I suggest not using it.
Be careful over something that *works* - things that might work for you might not for others (if you're writing only for yourself, I guess this isn't as big of an issue, but you might get a few surprises when you do an upgrade... so you may as well get it right first time and not have to bother with it later).

My Blog
07-06-2010 11:24 AM
Find all posts by this user Quote this message in a reply
flash.tato Offline
Junior Member
**
Posts: 11
Joined: Jun 2010
Post: #5
RE: Need help with plugin to allow only one thread in selected forums
Only a suggestion delegating a job which MySQL can do to PHP i think is wrong.

PHP Code:
1
2
3
4
5
6
7
8
9
		$query = $db->simple_select("threads", "uid");		
		$uid = explode(",", $db->threads['uid']);
		$forum = $new_thread['fid'];	

		if (in_array($forum,$uid))			

		{
			exit("You cannot post more than one thread in this forum");
		}


Nah! Add a WHERE clause with UID supplied and LIMIT 1 and you're done. Wink
The point is that now you're fetching all uids of the forums when you need to know only if it exists.

07-07-2010 07:31 AM
Find all posts by this user Quote this message in a reply
mark-in-dallas Offline
Junior Member
**
Posts: 5
Joined: Jul 2010
Post: #6
RE: Need help with plugin to allow only one thread in selected forums
I'm actually starting over, because Yumi's suggestion of hooking to datahandler_post_validate_thread made me realize that this check should be done before the editor is even opened.  I think it might be rather irratating to any member that was posting a new thread to be told that they could not, after they had already typed it and tried to submit.

Hooking into newthread_do_newthread_end would be the place to do so, right?

And dlash.tato, thanks for your suggestion.  You are absolutely right.  I am not really a programmer though and have just been trying to piece this together from other things I've been able to do and get to work.  Oops

I took that querie from a modification I made to the tweet to Twitter plugin, to block threads in private forums from being tweeted, which did work, but now you've got me thinking I should go back and try to change that to a WHERE clause too.
07-09-2010 10:17 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #7
RE: Need help with plugin to allow only one thread in selected forums
Ideally, actually, you'd check at the beginning of newthread, telling them they cannot post a new thread.  Perhaps also hide the New Thread button in forums too.

But when helping to code, I just generally try to point out the main "behind the scenes" problems and leave implementation details, including what users may thing, to the person making the thing.

My Blog
07-10-2010 09:54 AM
Find all posts by this user Quote this message in a reply
mark-in-dallas Offline
Junior Member
**
Posts: 5
Joined: Jul 2010
Post: #8
RE: Need help with plugin to allow only one thread in selected forums
Yep, I meant to say do_newthread_start, was the place I was going to try hooking into.

I had a really weird issue with my forum the other day:  I had modified the reputation.php, postbit_reputation, and rep_details pages so that reps were tied to posts and displayed on the rep_details pages, and it had been working fine for months.  But for whatever reason it had stopped posting the PID to the database and was not displaying on the rep_details pages.

I have a test forum that I implement all changes and modifications on to make sure they won't cause problems, before incorporating them into my live forum, and the rep system was working fine on the test forum.

I checked through each of the modified templates, as well as reputation.php and member.php, and none were causing the problem.  I then replaced all top level files in the root directory and eveything in the inc folder, and that did not solve the issue.

I finally replaced the cache and archive directories and that solved it.

Strange!
07-14-2010 10:24 PM
Find all posts by this user Quote this message in a reply

« Next Oldest | Next Newest »

 Standard Tools
Forum Jump: