File indexing completed on 2024-05-12 17:26:17

0001 <?php
0002 
0003 /*
0004  *   TRT GFX 3.0.1 (beta build) BackToSlash
0005  * 
0006  *   support: happy.snizzo@gmail.com
0007  *   website: http://trt-gfx.googlecode.com
0008  *   credits: Claudio Desideri
0009  *   
0010  *   This software is released under the MIT License.
0011  *   http://opensource.org/licenses/mit-license.php
0012  */ 
0013 
0014 
0015 class OCSComment{
0016   
0017   public $id;
0018   public $owner;
0019   public $type;
0020   public $content;
0021   public $content2;
0022   public $parent;
0023   public $votes;
0024   public $score;
0025   public $subject;
0026   public $message;
0027   
0028   
0029   private $ocs_comment;
0030   private $data;
0031   
0032   
0033   /*
0034    * Enabling main to be on a global context.
0035    */
0036   public function __construct(){
0037     $this->ocs_comment = new EModel("ocs_comment");
0038   }
0039   
0040   /*
0041    * Load into memory a content from table ocs_content
0042    */
0043   public function load($id){
0044     if($this->ocs_comment->is_there("id","id=$id")) {
0045       $r = $this->ocs_comment->find("*", "where id=$id LIMIT 1");
0046       $this->id = $id;
0047       $this->type = $r[0]["type"];
0048       $this->owner = $r[0]["owner"];
0049       $this->content = $r[0]["content"];
0050       $this->content2 = $r[0]["content2"];
0051       $this->parent = $r[0]["parent"];
0052       $this->votes = $r[0]["votes"];
0053       $this->score = $r[0]["score"];
0054       $this->subject = $r[0]["subject"];
0055       $this->message = $r[0]["message"];
0056       $this->date = $r[0]["date"];
0057       return true;
0058     } else {
0059       return false;
0060     }
0061   }
0062   
0063   /*
0064    * Checks if the current loaded comment is owned by $id
0065    */
0066   public function is_owned($id){
0067     if($this->owner == $id){
0068       return true;
0069     } else {
0070       return false;
0071     }
0072   }
0073   
0074   /*
0075    * This function returns the associated id for the selected content
0076    */
0077   public function id(){
0078     return $this->id;
0079   }
0080   
0081   /*
0082    * Return the number of people who has performed a vote action on this object.
0083    */
0084   public function votes(){
0085     return $this->votes;
0086   }
0087   
0088   /*
0089    * Returns the weighted score for this object.
0090    */
0091   public function score(){
0092     return $this->score;
0093   }
0094   
0095   /*
0096    * Saving the ram rapresentation into memory (database).
0097    */
0098   public function save_to_db(){
0099     //TODO: implement unique name.
0100     
0101     //saving
0102     EDatabase::q("INSERT INTO ocs_comment (type,owner,content,content2,parent,votes,score,subject,message) VALUES ('".$this->type."',".$this->owner.",".$this->content.",'".$this->content2."','".$this->parent."','".$this->votes."','".$this->score."','".$this->subject."','".$this->message."')");
0103     $this->id = $id = EDatabase::last_insert_id();
0104     return $id;
0105   }
0106   
0107   /*
0108    * Setting id of the owner of the content.
0109    */
0110   public function set_owner($owner){
0111     $this->owner = $owner;
0112   }
0113   
0114   /*
0115    * Manually setting internal data.
0116    */
0117   public function set_data($data){
0118     // assuring those are not evil data to be used as SQL injections
0119     EDatabase::safe($data);
0120     //data validations
0121     if(!isset($data['type'])){ $this->type = 1; } else { $this->type = $data['type']; }
0122     if(!isset($data['owner'])){ $this->owner = 0; /* anonymous */ } else { $this->owner = $data['owner']; }
0123     if(!isset($data['content'])){ $this->content = ""; } else { $this->content = $data['content']; }
0124     if(!isset($data['content2'])){ $this->content2 = ""; } else { $this->content2 = $data['content2']; }
0125     if(!isset($data['parent'])){ $this->parent = ""; } else { $this->parent = $data['parent']; }
0126     if(!isset($data['votes'])){ $this->votes = ""; } else { $this->votes = $data['votes']; }
0127     if(!isset($data['score'])){ $this->score = ""; } else { $this->score = $data['score']; }
0128     if(!isset($data['subject'])){ $this->subject = ""; } else { $this->subject = $data['subject']; }
0129     if(!isset($data['message'])){ $this->message = ""; } else { $this->message = $data['message']; }
0130     if(!isset($data['data'])){ $this->data = date(" j/n/Y H:i:s "); }
0131   }
0132   
0133   /*
0134    * Set a weighted score to the content.
0135    */
0136   public function set_score($score){
0137     //acquiring data
0138     $oldmedia = $this->score;
0139     $newscore = $score;
0140     
0141     $oldvotes = $this->votes;
0142     $newvotes = $this->votes + 1;
0143     //calculating new media
0144     $newmedia = ($oldmedia * $oldvotes + $newscore) / ($newvotes);
0145     
0146     //setting new infos to local memory object
0147     $this->score = $newmedia;
0148     $this->votes = $newvotes;
0149     
0150     //updating db
0151     EDatabase::q("UPDATE ocs_comment SET score=".$this->score.", votes=".$this->votes." WHERE id=".$this->id." LIMIT 1");
0152     
0153   }
0154   
0155 }
0156 
0157 
0158 ?>