views:

34

answers:

1

I would like to build (or find) a php reporting library that I can use to allow my users to report any type of content as bad. Actually, if they could report content as good (which I guess would mean a rating system) that would be even better. So if anyone knows of a library like this I would love to hear about it.

Anyway, this is how I imagine the database working and I would like to know if this sounds correct.

  1. ID - ID of row
  2. USER_ID - ID of the user voting/reporting
  3. ITEM_ID - UNIQUE table row ID of the item reported (post, other user, link, etc...)
  4. TYPE - the name of the table this pertains to (posts, users, links, etc...)
  5. DATE - datetime of report
  6. VALUE - I guess this could be an UP vote or DOWN vote

By including the [TYPE] column I can use this voting system across all content types on my site instead of just one (like forum posts). By storing the user_id I can be sure that only one vote/report is cast per item per user. Does anyone have anything to add to this idea?

+1  A: 

Well, I went ahead and built it like I said. Frank was right, it wasn't too hard. Here is the code incase anyone else wants to use it on a MVC that supports AR styled DB calls like CodeIgniter or MicroMVC:

/*
 * Multi-table user voting model
 */
class votes extends model {

    public static $table = 'votes';


    function cast( $user_id = NULL, $item_id = NULL, $type = NULL, $vote = TRUE) {

     $where = array(
      'user_id' => $user_id,
      'item_id' => $item_id,
      'type'  => $type
     );

     //Try to fetch this row
     $row = $this->db->get_where(self::$table, $where );

     //Look for an existing vote row
     if( $row && ! empty($row[0]) ) {

      //Fetch row
      $row = $row[0];

      //If they already voted this way... then we're done!
      if( $row->vote == $vote) {
       return;
      }

      //Else update the row to the new vote
      $row->vote = $vote;
      $row->date = convert_time(time());

      $this->db->update( self::$table, $row, array('id' => $row->id) );

      return;
     }

     //Else create a new vote for this item
     $row = array(
      'user_id' => $user_id,
      'item_id' => $item_id,
      'type'  => $type,
      'date'  => convert_time(time()),
      'vote'  => $vote,
     );

     $this->db->insert( self::$table, $row);
    }
}
Xeoncross