hits are calculated

This commit is contained in:
Claude 2012-10-10 13:31:30 +02:00
parent 23c239d045
commit 83bd7892fb
4 changed files with 96 additions and 13 deletions

View File

@ -185,8 +185,8 @@ class Main extends CI_Controller
$this->load->dbforge();
$fields = array(
'paste_id' => array(
'type' => 'INT',
'constraint' => 10,
'type' => 'VARCHAR',
'constraint' => 8,
) ,
'ip_address' => array(
'type' => 'VARCHAR',
@ -483,7 +483,7 @@ class Main extends CI_Controller
{
$this->load->model('pastes');
$data = $this->pastes->getTrends();
$this->load->view('list', $data);
$this->load->view('trends', $data);
}
}

View File

@ -8,8 +8,10 @@
* - createPaste()
* - checkPaste()
* - getPaste()
* - calculate_hits()
* - getReplies()
* - getLists()
* - getTrends()
* - getSpamLists()
* - cron()
* Classes list:
@ -185,6 +187,8 @@ class Pastes extends CI_Model
$data['created'] = $row['created'];
$data['url'] = site_url('view/' . $row['pid']);
$data['raw'] = $row['raw'];
$data['hits'] = $row['hits'];
$data['hits_updated'] = $row['hits_updated'];
$data['snipurl'] = $row['snipurl'];
$inreply = $row['replyto'];
}
@ -247,10 +251,45 @@ class Pastes extends CI_Model
{
$replies = false;
}
// hits
$hits_data = array(
'paste_id' => $pid,
'ip_address' => $this->input->ip_address() ,
'created' => mktime() ,
);
$insert_query = $this->db->insert_string('trending', $hits_data);
$insert_query = str_replace('INSERT INTO', 'INSERT IGNORE INTO', $insert_query);
$this->db->query($insert_query);
if (mktime() > (60 + $data['hits_updated']))
{
$this->calculate_hits($pid, $data['hits']);
}
}
return $data;
}
function calculate_hits($pid, $current_hits)
{
$this->db->select('count(paste_id) as count');
$this->db->where('paste_id', $pid);
$query = $this->db->get('trending');
$hits_count = $query->result_array();
$hits_count = $hits_count[0]['count'];
if ($hits_count != $current_hits)
{
//update
$this->db->where('pid', $pid);
$this->db->update('pastes', array(
'hits' => $hits_count,
'hits_updated' => mktime() ,
));
}
}
function getReplies($seg = 3)
{
$amount = $this->config->item('per_page');
@ -333,14 +372,14 @@ class Pastes extends CI_Model
return $data;
}
function getTrends($root = 'lists/', $seg = 2)
function getTrends($root = 'trends/', $seg = 2)
{
$this->load->library('pagination');
$this->load->library('process');
$amount = $this->config->item('per_page');
$page = ($this->uri->segment(2) ? $this->uri->segment(2) : 0);
$this->db->select('id, title, name, created, pid, lang, raw');
$this->db->select('id, title, name, created, pid, lang, raw, hits');
$this->db->where('private', 0);
$this->db->order_by('hits', 'desc');
$this->db->order_by('created', 'desc');
$query = $this->db->get('pastes', $amount, $page);
@ -355,12 +394,8 @@ class Pastes extends CI_Model
$data['pastes'][$n]['created'] = $row['created'];
$data['pastes'][$n]['lang'] = $row['lang'];
$data['pastes'][$n]['pid'] = $row['pid'];
if ($this->uri->segment(2) == 'rss')
{
$data['pastes'][$n]['paste'] = $this->process->syntax(htmlspecialchars_decode($row['raw']) , $row['lang']);
}
$data['pastes'][$n]['raw'] = $row['raw'];
$data['pastes'][$n]['hits'] = $row['hits'];
$n++;
}
}

View File

@ -0,0 +1,44 @@
<?php $this->load->view('defaults/header');?>
<h1>Trending Pastes</h1>
<?php
function checkNum($num){
return ($num%2) ? TRUE : FALSE;
}
$n = 0;
if(!empty($pastes)){ ?>
<table class="recent">
<tbody>
<tr>
<th class="title">Title</th>
<th class="name">Name</th>
<th class="lang">Language</th>
<th class="hits">Hits</th>
<th class="time">When</th>
</tr>
<?php foreach($pastes as $paste) {
if(checkNum($n) == TRUE) {
$eo = "even";
} else {
$eo = "odd";
}
$n++;
?>
<tr class="<?php echo $eo; ?>">
<td class="first"><a href="<?php echo site_url("view/".$paste['pid']); ?>"><?php echo $paste['title']; ?></a></td>
<td><?php echo $paste['name']; ?></td>
<td><?php echo $paste['lang']; ?></td>
<td><?php echo $paste['hits']; ?></td>
<td><?php $p = explode(",", timespan($paste['created'], time())); echo $p[0]; ?> ago.</td>
</tr>
<?php }?>
</tbody>
</table>
<?php } else { ?>
<p>There have been no pastes :(</p>
<?php }?>
<?php echo $pages; ?>
<div class="spacer"></div>
<?php $this->load->view('defaults/footer');?>

View File

@ -314,7 +314,7 @@ h4 {
}
.recent .name {
width: 250px;
width: 180px;
}
.recent .lang {
@ -325,6 +325,10 @@ h4 {
width: 100px;
}
.recent .hits {
width: 70px;
}
.recent tr {
line-height: 30px;
}
@ -497,4 +501,4 @@ h4 {
text-align: center;
background-color: #FFBABA;
color: #D8000C;
}
}