You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

78 lines
2.7 KiB

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Event_comment_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->db->query("SET @@group_concat_max_len =30000");
}
public function save_comment($comment) {
$data = array(
'name' => $comment['name'],
'rate' => isset($comment['rate'])?(int)$comment['rate']:0,
'date' => $comment['date'],
'comment' => $comment['comment'],
'event_id' => $comment['event_id']
);
$this->db->insert('event_comment', $data);
}
public function get_comments_by_event_id($event_id) {
$this->db->select('*');
$this->db->where('event_id', $event_id);
$this->db->where('status', 1);
return $this->db->get('event_comment')->result();
}
public function get_formatted_comments_by_event_id($event_id) {
$this->db->select('name, rate, DATE_FORMAT(date, "%e %M %Y") as date, comment');
$this->db->where('event_id', $event_id);
$this->db->where('status', 1);
$comments = $this->db->get('event_comment')->result();
$commentCount = $this->count_comments_with_rate($event_id);
$rate = $this->get_comment_rate_average($event_id);
return array(
'comments' =>$comments,
'rate_avg' => $commentCount > 0 ? ($rate/$commentCount).'/10': 'n/a'
);
}
private function count_comments_with_rate($event_id) {
$this->db->select('*');
$this->db->where('event_id', $event_id);
$this->db->where('status', 1);
$this->db->where('rate >', 0);
return $this->db->get('event_comment')->num_rows();
}
private function get_comment_rate_average($event_id) {
$this->db->select_sum('rate');
$this->db->from('event_comment');
$this->db->where('event_id', $event_id);
$this->db->where('status', 1);
$this->db->count_all();
$query=$this->db->get();
if(!empty($query->row()->rate))
return $query->row()->rate;
else
return 0;
}
public function update_comment($comment_id, $comment) {
$data = array(
'name' => $comment['name'],
'rate' => isset($comment['rate'])?(int)$comment['rate']:0,
'date' => $comment['date'],
'comment' => $comment['comment'],
);
$this->db->where('id', $comment_id);
$this->db->update('event_comment', $data);
}
public function delete_comment($id) {
$data = array(
'status' => 0
);
$this->db->where('id', $id);
$this->db->update('event_comment', $data);
}
}