Guest User

Untitled

a guest
May 20th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.70 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.     $rating = new ratings($_POST['widget_id']);
  5.    
  6.    
  7.     isset($_POST['fetch']) ? $rating->get_ratings() : $rating->vote();
  8.    
  9.    
  10.    
  11.  
  12.  
  13.  
  14. class ratings {
  15.    
  16.     var $data_file = './ratings.data.txt';
  17.     private $widget_id;
  18.     private $data = array();
  19.    
  20.    
  21. function __construct($wid) {
  22.    
  23.     $this->widget_id = $wid;
  24.  
  25.     $all = file_get_contents($this->data_file);
  26.    
  27.     if($all) {
  28.         $this->data = unserialize($all);
  29.     }
  30. }
  31. public function get_ratings() {
  32.     if($this->data[$this->widget_id]) {
  33.         echo json_encode($this->data[$this->widget_id]);
  34.     }
  35.     else {
  36.         $data['widget_id'] = $this->widget_id;
  37.         $data['number_votes'] = 0;
  38.         $data['total_points'] = 0;
  39.         $data['dec_avg'] = 0;
  40.         $data['whole_avg'] = 0;
  41.         echo json_encode($data);
  42.     }
  43. }
  44. public function vote() {
  45.    
  46.     # Get the value of the vote
  47.    preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);
  48.     $vote = $match[1];
  49.    
  50.     $ID = $this->widget_id;
  51.     # Update the record if it exists
  52.    if($this->data[$ID]) {
  53.         $this->data[$ID]['number_votes'] += 1;
  54.         $this->data[$ID]['total_points'] += $vote;
  55.     }
  56.     # Create a new one if it doesn't
  57.    else {
  58.         $this->data[$ID]['number_votes'] = 1;
  59.         $this->data[$ID]['total_points'] = $vote;
  60.     }
  61.    
  62.     $this->data[$ID]['dec_avg'] = round( $this->data[$ID]['total_points'] / $this->data[$ID]['number_votes'], 1 );
  63.     $this->data[$ID]['whole_avg'] = round( $this->data[$ID]['dec_avg'] );
  64.        
  65.        
  66.     file_put_contents($this->data_file, serialize($this->data));
  67.     $this->get_ratings();
  68. }
  69.  
  70. # ---
  71. # end class
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. //function return_rating($raw_id) {
  88. //    
  89. //    $widget_data = fetch_rating($raw_id);
  90. //    echo json_encode($widget_data);
  91. //}
  92. //
  93. //# Data is stored as:
  94. //#     widget_id:number_of_voters:total_points:dec_avg:whole_avg
  95. //function fetch_rating($raw_id) {
  96. //    
  97. //    $all  = file('./ratings.data.txt');
  98. //    
  99. //    foreach($all as $k => $record) {
  100. //        if(preg_match("/$raw_id:/", $record)) {
  101. //            $selected = $all[$k];
  102. //            break;
  103. //        }
  104. //    }
  105. //
  106. //    if($selected) {
  107. //        $data = split(':', $selected);
  108. //        $data[] = round( $data[2] / $data[1], 1 );
  109. //        $data[] = round( $data[3] );
  110. //    }
  111. //    else {
  112. //        $data[0] = $raw_id;
  113. //        $data[1] = 0;
  114. //        $data[2] = 0;
  115. //        $data[3] = 0;
  116. //        $data[4] = 0;
  117. //    }
  118. //    
  119. //    return $data;
  120. //}
  121. //
  122. //
  123. //
  124. //
  125. //function register_vote() {
  126. //    
  127. //    preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);
  128. //    $vote = $match[1];
  129. //    
  130. //    $current_data = fetch_rating($_POST['widget']);
  131. //    
  132. //    $new_data[] = $current_data['stars'] + $vote;
  133. //    $new_data[] = $current_data['cast'] + 1;
  134. //    
  135. //
  136. //    # --> This needs to be fixed, since a widget ID is ALWAYS passed in
  137. //    # it should be a class property
  138. //    file_put_contents($_POST['widget'] . '.txt', "{$new_data[0]}\n{$new_data[1]}");
  139. //    
  140. //    return_rating($_POST['widget']);
  141. //}
  142.  
  143.     //foreach($all as $k => $record) {
  144.     //    if(preg_match("/$raw_id:/", $record)) {
  145.     //        $selected = $all[$k];
  146.     //        break;
  147.     //    }
  148.     //}
  149.     //
  150.     //if($selected) {
  151.     //    $this->data = split(':', $selected);
  152.     //    $this->data[] = round( $this->data[2] / $this->data[1], 1 );
  153.     //    $this->data[] = round( $this->data[3] );
  154.     //}
  155.     //else {
  156.     //    $this->data[0] = $this->widget_id;
  157.     //    $this->data[1] = 0;
  158.     //    $this->data[2] = 0;
  159.     //    $this->data[3] = 0;
  160.     //    $this->data[4] = 0;
  161.     //}
  162. ?>
Add Comment
Please, Sign In to add comment