Welcome Guest, Not a member yet? Register   Sign In
Developing CI Based Forum - WIP
#11

[eluser]srpurdy[/eluser]
Here is the code example for the main display. So that what I explained above actually makes some sense. Wouldn't mind some feedback on this code. Smile

This is part of the frontend forum controller.

Code:
/*
This Function Extracts The Logged in Users Group ID Numbers into an array of valid user groups for that user.
*/
private function user_group()
  {
  $logged_in = $this->ion_auth->logged_in();
  $uid = 0;
  if($logged_in == true)
   {
   //Lets Get the current logged in users, user group. We will use this to compare with private permissions groups.
   $get_user_groups = $this->forum_model->user_group_logged_in($this->session->userdata['user_id']);
   $user_group_name = '';
   foreach($get_user_groups as $ug)
    {
    //We got a result
    $user_group_name[$uid] = $ug->group_id;
    $uid++;
    }
   }
  else
   {
   //if the user is not logged in. than we will just compare with a 0. (This should force permissions to false.)
   $user_group_name = '0';
   }
  return $user_group_name;
  }
  
/*
This function extracts forum user group access rights and constructs an array. This is where the magic happens. :)
*/
private function private_permissions()
  {
  $perm = 0;
  $logged_in = $this->ion_auth->logged_in();
  if($logged_in == true)
   {
   $user_group_name = $this->user_group();
  
   $private_permissions = $this->forum_model->get_all_permissions($user_group_name);
   if($private_permissions)
    {
    for($perm = 0; $perm <= count($private_permissions) -1; $perm++)
     {
     $private_group_name[$perm] = $private_permissions[$perm];
     }
    }
   else
    {
    $private_group_name[$perm] = '';
    }
   //test jig
   //print_r($data['private_group_name'][0]['forum_id']);
   //end test jig
   }
  else
   {
   $private_group_name[$perm] = '';
   }
  return $private_group_name;
  }
  
/*
This function extracts all sub forums within the database, and constructs an array we can use to display subforums.
*/
private function all_sub_forums()
  {
  $sub_forums = $this->forum_model->get_sub_forums_array();
  if($sub_forums)
   {
   for($sub = 0; $sub <= count($sub_forums) -1; $sub++)
    {
    $forum_sub_list[$sub] = $sub_forums[$sub];
    }
   }
  else
   {
   $forum_sub_list = '';
   }
  //test jig
  //print_r($forum_sub_list);
  //end test jig
  return $forum_sub_list;
  }
  
function index()
  {
  $private_group_name = $this->private_permissions();
  $data['forum_sub_list'] = $this->all_sub_forums();
  //Construct some view settings.
  $data['heading'] = 'Main Forum';
  $template_path = $this->config->item('template_page');
  $data['page'] = $template_path . '/forums/main_forum';
  $data['forum_categories'] = $this->forum_model->get_all_sections();
  
  //Create some ints we will need for arrays.
  $i = 0;
  $int_two = 0;
  $i_three = 0;
  $i_four = 0;
  $sub_per = 0;
  $sub = 0;
  
  //Start getting forums.
  foreach($data['forum_categories']->result() as $fc)
   {
   //Extract All Forums within each category
   $data['forum_list'][$i] = $this->forum_model->get_all_forums($fc->fcat_id);
   foreach($data['forum_list'][$i]->result() as $fl)
    {    
    //Lets get any subforms from each forum section.
    //$data['forum_sub_list'][$i_three] = $this->forum_model->get_sub_forums($fl->forum_id);
    if(!$data['forum_sub_list'])
     {
     $data['forum_sub_list'] = '';
     }
    else
     {
     //Check permissions on subforms.
     //foreach($data['forum_sub_list'][$i_three] as $fsl)
     for($sub = 0; $sub < count($data['forum_sub_list']); $sub++)
      {
      $sub_forum_id = $data['forum_sub_list'][$sub]['forum_id'];
      $logged_in = $this->ion_auth->logged_in();
      $data['group_sub_private_name'][$sub_per] = false;
      if($logged_in == true)
       {
       for($fperm = 0; $fperm <= count($private_group_name) -1; $fperm++)
        {
        if($sub_forum_id == @$private_group_name[$fperm]['forum_id'])
         {
         $current_private_group = $private_group_name[$fperm]['user_group_id'];
         $data['group_sub_private_name'][$sub_per] = true;
         }
        else
         {
         $data['group_sub_private_name'][$sub_per] = false;
         }
        if($data['group_sub_private_name'][$sub_per] == true)
         {
         break;
         }
        }
       }
      else
       {
       $data['group_sub_private_name'][$sub_per] = false;
       }
      $sub_per++;
      }
     }
    $forum_id = $fl->forum_id;
    $logged_in = $this->ion_auth->logged_in();
    $data['group_private_name'][$i_four] = false;
    if($logged_in == true)
     {
     for($fperm = 0; $fperm <= count($private_group_name) -1; $fperm++)
      {
      if($forum_id == @$private_group_name[$fperm]['forum_id'])
       {
       $current_private_group = $private_group_name[$fperm]['user_group_id'];
       $data['group_private_name'][$i_four] = true;
       }
      else
       {
       $data['group_private_name'][$i_four] = false;
       }
      if($data['group_private_name'][$i_four] == true)
       {
       break;
       }
      }
     }
    else
     {
     $data['group_private_name'][$i_four] = false;
     }
    $i_four++;
    }
   $i++;
   }
  
  //Okay we're ready to render our view.
  $this->load->vars($data);
  $this->load->view($this->_container_forum);
  }
#12

[eluser]Aken[/eluser]
[quote author="srpurdy" date="1344730892"]As for production. I guess you mean in terms of performance and security?[/quote]

No, not particularly that. More so something that's more complete (IE a release candidate or beta) rather than a work in progress.

Is this on / going to be on Github? That'd make me happy. :-p
#13

[eluser]srpurdy[/eluser]
[quote author="Aken" date="1345249940"][quote author="srpurdy" date="1344730892"]As for production. I guess you mean in terms of performance and security?[/quote]

No, not particularly that. More so something that's more complete (IE a release candidate or beta) rather than a work in progress.

Is this on / going to be on Github? That'd make me happy. :-p[/quote]

Yeah I may post it on Github. I want to clean things up before I do that though. Tongue I know I'll probably get kicked in the face with some code somewhere... Tongue and it's definitely not at a stage where I would consider it more complete yet. Smile Although for what it does do it does it well so far. Smile
#14

[eluser]Aken[/eluser]
Github is certainly an excellent place for open source. I'd recommend it, or at least something like it (Bitbucket or what have you). Github is my favorite, though.
#15

[eluser]srpurdy[/eluser]
[quote author="Aken" date="1345250655"]Github is certainly an excellent place for open source. I'd recommend it, or at least something like it (Bitbucket or what have you). Github is my favorite, though.[/quote]

Yeah I'm more familiar with GitHub so I'd likely use that. Smile

I've revised my topic tracking logic below. I think this makes the most sense.

Topic Tracking
-> if topic has no row in the database for X User than topic is currently unread.
-> If the topic does have a record in the database than compare the datetime stamp with the last post made in the topic to determine wither the topic is read or unread.
-> If the topic has no row in the database and the user visits the topic. Than add a row for that user with the current date time.
-> However if the user has more than 100 topics currently being tracked. Remove the oldest datetime topic from tracking.
-> If the topic is older than 10 days old without any new posts than we will mark the topic as read. We will also not attempt to insert any row if the user visits that topic. (This will mean if someone does post. The 10 day window will now be erased so the topic should display as unread.
-> Both the 10 Day window and the 100 max tracked topics per user will be configuration variables so these can be adjusted.
-> Also an option for Mark All Topics as Read. This will put a timestamp into the users information, and any topic older than that datetime will be consider read at that point.

If anyone see's some glaringly obvious flaws with that logic let me know. Smile
#16

[eluser]a.somervell[/eluser]
FYI: Forum built in CI http://www.biggie.co.nz/discussions
#17

[eluser]srpurdy[/eluser]
For anyone that would like to see how things are right now. Tongue

http://www.cidevforum.newedgedevelopment.com/en/forum/
#18

[eluser]Altazar[/eluser]
Very cute, can I buy it, please? Smile
#19

[eluser]srpurdy[/eluser]
It's not in a state that's worth buying right now lol. (I also haven't decided wither it will be free or not) But it will be open source that's for sure. So if it isn't free than it'll just depend on the honor of the people using it.

But much of the admin area right now is not fully functional, there is no moderation, can't move or lock topics. So a lot missing still lol Smile

Only cool thing I got in there right now is the multi quote. Tongue. Currently doing structural planning on moderation tools. So at least when that's in there it'll be a more solid base. Smile

Also I have 1 query that's starting to bug me a bit, not sure if it's going to be good on a large forum. Seems a bit slow. So I'm not happy with that. Tongue (0.2 to 0.4 seconds for 1 query is too much!) It's only 0.002 after mysql query cache kicks in, and it's only 0.2 or so when the full list is displayed (20 topics default)

I only wrote it this way to avoid query stacking so I can display everything in a single query. Just not sure how well it will do if the database has 20,000 records or more in it. I keep thinking I must be missing an index somewhere but looked 17 times lol.. Smile

This is the one below bit of a monster Smile

Code:
function get_posts_by_forum($limit,$offset)
  {
  $uri = $this->uri->segment(4);
  $posts_by_forum = $this->db
   ->where('forums.forum_url_name', $this->uri->segment(3))
   ->where('forum_topics.forum_id = forums.forum_id')
   ->where('forum_permissions.forum_id = forums.forum_id')
   //->where('forum_posts.is_topic', 'Y')
   ->select('
    forum_permissions.allow_html,
    forum_permissions.allow_bbcode,
    forum_permissions.allow_topic,
    forum_permissions.allow_reply,
    forum_permissions.allow_edit,
    forum_permissions.allow_delete,
    forum_permissions.allow_attachments,
    forum_permissions.allow_sticky,
    forum_topics.post_title,
    forum_topics.topic_id,
    forum_topics.post_text,
    forum_topics.sticky,
    forum_topics.topic_views,
    forums.public,
    forums.forum_id,
    (select users.first_name from forum_posts,users,profile_fields where forum_topics.topic_id = forum_posts.topic_id AND forum_posts.approved = "Y" AND forum_posts.user_id = users.id AND users.id = profile_fields.user_id ORDER BY forum_posts.post_date ASC LIMIT 1) as first_name,
    (select users.last_name from forum_posts,users,profile_fields where forum_topics.topic_id = forum_posts.topic_id AND forum_posts.approved = "Y" AND forum_posts.user_id = users.id AND users.id = profile_fields.user_id ORDER BY forum_posts.post_date ASC LIMIT 1) as last_name,
    (select profile_fields.display_name from forum_posts,users,profile_fields where forum_topics.topic_id = forum_posts.topic_id AND forum_posts.approved = "Y" AND forum_posts.user_id = users.id AND users.id = profile_fields.user_id ORDER BY forum_posts.post_date ASC LIMIT 1) as display_name,
    (select profile_fields.nickname from forum_posts,users,profile_fields where forum_topics.topic_id = forum_posts.topic_id AND forum_posts.approved = "Y" AND forum_posts.user_id = users.id AND users.id = profile_fields.user_id ORDER BY forum_posts.post_date ASC LIMIT 1) as nickname,
    (select count(forum_posts.topic_id) from forum_posts where forum_posts.topic_id = forum_topics.topic_id) as total_posts_count,
    (select count(forum_is_post.topic_id) from forum_is_post where forum_is_post.topic_id = forum_topics.topic_id) as total_posts,
    (select forum_posts.post_id from forum_posts where forum_posts.topic_id = forum_topics.topic_id ORDER BY forum_posts.post_date DESC LIMIT 1) as post_date_order,
    (select users.first_name from forum_posts,users,profile_fields where forum_topics.topic_id = forum_posts.topic_id AND forum_posts.approved = "Y" AND forum_posts.user_id = users.id AND users.id = profile_fields.user_id ORDER BY forum_posts.post_date DESC LIMIT 1) as last_first_name,
    (select users.last_name from forum_posts,users,profile_fields where forum_topics.topic_id = forum_posts.topic_id AND forum_posts.approved = "Y" AND forum_posts.user_id = users.id AND users.id = profile_fields.user_id ORDER BY forum_posts.post_date DESC LIMIT 1) as last_last_name,
    (select profile_fields.display_name from forum_posts,users,profile_fields where forum_topics.topic_id = forum_posts.topic_id AND forum_posts.approved = "Y" AND forum_posts.user_id = users.id AND users.id = profile_fields.user_id ORDER BY forum_posts.post_date DESC LIMIT 1) as last_display_name,
    (select profile_fields.nickname from forum_posts,users,profile_fields where forum_topics.topic_id = forum_posts.topic_id AND forum_posts.approved = "Y" AND forum_posts.user_id = users.id AND users.id = profile_fields.user_id ORDER BY forum_posts.post_date DESC LIMIT 1) as last_nickname,
    (select forum_posts.topic_id from forum_posts,users,profile_fields where forum_topics.topic_id = forum_posts.topic_id AND forum_posts.approved = "Y" AND forum_posts.user_id = users.id AND users.id = profile_fields.user_id ORDER BY forum_posts.post_date DESC LIMIT 1) as last_topic_id,
    (select users.id from forum_posts,users,profile_fields where forum_topics.topic_id = forum_posts.topic_id AND forum_posts.approved = "Y" AND forum_posts.user_id = users.id AND users.id = profile_fields.user_id ORDER BY forum_posts.post_date DESC LIMIT 1) as last_user_id,
    (select forum_posts.post_date from forum_posts,users,profile_fields where forum_topics.topic_id = forum_posts.topic_id AND forum_posts.approved = "Y" AND forum_posts.user_id = users.id AND users.id = profile_fields.user_id ORDER BY forum_posts.post_date DESC LIMIT 1) as last_post_date,
   ')
   ->from('forums,forum_topics,forum_permissions')
   ->order_by('forum_topics.sticky', 'desc')
   ->order_by('post_date_order', 'desc')
   ->limit($limit,$offset)
   //->join('forum_posts', 'forum_posts.topic_id = forum_topics.topic_id')
   //->order_by('post_date_order', 'desc')
   ->get();
  return $posts_by_forum;
  }

Shawn
#20

[eluser]Altazar[/eluser]
Maybe Twitter Bootstrap makes it slow?




Theme © iAndrew 2016 - Forum software by © MyBB