X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=blobdiff_plain;f=application%2Fmodels%2Fvideos_model.php;h=87b0d3b0e8d869ea5e7cf7a19ee907e9b0c4cf90;hb=961611537619595dc7eed2104faafcd0c9cdc57b;hp=c8eeb7f7613a6d9baa8d070afe5c9b08ec0d3d44;hpb=f8bfacb65b055af85f639e65aff66a680d0d3231;p=living-lab-site.git diff --git a/application/models/videos_model.php b/application/models/videos_model.php index c8eeb7f..87b0d3b 100644 --- a/application/models/videos_model.php +++ b/application/models/videos_model.php @@ -11,6 +11,8 @@ class Videos_model extends CI_Model { public function __construct() { + parent::__construct(); + if ($this->db === NULL) { $this->load->library('singleton_db'); @@ -19,13 +21,17 @@ class Videos_model extends CI_Model { } /** - * Retrieves information about a set of videos which are going to be - * displayed in the catalog. + * Retrieves a set of videos information which can be used for displaying + * that videos as a list with few details. * - * @param int $category_id DB category ID - * @param int $offset - * @param int $count - * @param string $ordering control videos ording by these + * @param int $category_id DB category ID; pass NULL for all + * categories + * @param mixed $user an user_id (as int) or an username + * (as string); pass NULL for all users + * @param int $offset + * @param int $count number of videos to retrieve; if set to TRUE this + * method will retrieve the number of videos that satisfy condition + * @param string $ordering control videos ording by these * possibilities: * - * @return array a list of videos, each one being an assoc array with: + * @param bool $unactivated whether to retrieve or not ingested unactivated + * videos; typically only administrators should see this kind of assets + * @return array a list of videos, each one being an assoc array with: * */ - public function get_videos_summary($category_id, $offset, $count, $ordering = 'hottest') + public function get_videos_summary($category_id, $user, $offset, $count, + $ordering = 'hottest', $unactivated = FALSE) { $this->load->helper('text'); - // Ordering - switch ($ordering) + $order_statement = ""; + if ($count !== TRUE) { - case 'hottest': - $order_statement = "ORDER BY date DESC, score DESC, RAND()"; - break; - case 'newest': - $order_statement = "ORDER BY date DESC"; - break; - case 'alphabetically': - $order_statement = "ORDER BY title"; - break; - - default: - $order_statement = ""; + // Ordering + switch ($ordering) + { + case 'hottest': + $order_statement = "ORDER BY date DESC, score DESC, RAND()"; + break; + case 'newest': + $order_statement = "ORDER BY date DESC"; + break; + case 'alphabetically': + $order_statement = "ORDER BY title"; + break; + + default: + $order_statement = ""; + } + } + + // Show unactivated videos. + $cond_unactivated = ($unactivated + ? '(a.activation_code IS NULL OR a.activation_code IS NOT NULL + AND a.content_ingested = 1)' + : 'a.activation_code IS NULL'); + + // Category filtering + if ($category_id === NULL) + $cond_category = "1"; + else + { + $category_id = intval($category_id); + $cond_category = "category_id = $category_id"; + } + + // User filtering + if ($user === NULL) + $cond_user = "1"; + else + { + if (is_int($user)) + $cond_user = "v.user_id = $user"; + else if (is_string($user)) + $cond_user = "u.username = '$user'"; } + if ($count === TRUE) + $fields = "COUNT(*) count"; + else + $fields = "v.id, name, title, duration, user_id, u.username, views, + thumbs_count, default_thumb, + (views + likes - dislikes) AS score, + a.activation_code, a.content_ingested"; + $query = $this->db->query( - "SELECT id, name, title, duration, user_id, views, thumbs_count, - default_thumb, (views + likes - dislikes) AS score - FROM `videos` - WHERE category_id = ? + "SELECT $fields + FROM `videos` v + LEFT JOIN `videos_unactivated` a ON (id = a.video_id), + `users` u + WHERE v.user_id = u.id AND $cond_category AND $cond_user + AND $cond_unactivated $order_statement - LIMIT ?, ?", - array(intval($category_id), $offset, $count)); + LIMIT $offset, $count"); if ($query->num_rows() > 0) + { + if ($count === TRUE) + return $query->row()->count; + $videos = $query->result_array(); + } else - return NULL; + return array(); foreach ($videos as & $video) { @@ -91,27 +144,25 @@ class Videos_model extends CI_Model { // Ellipsized title //$video['shorted_title'] = ellipsize($video['title'], 45, 0.75); $video['shorted_title'] = character_limiter($video['title'], 50); - - // TODO: user information - $video['user_name'] = 'TODO'; } return $videos; } - public function get_videos_count($category_id) + /** + * Returns the number of videos from database from a specific category or + * user. + * NULL parameters count videos from all categories and / or all users. + * + * @param int $category_id + * @param mixed $user an user_id (as int) or an username (as string) + * @return int number of videos or FALSE if an error occured + */ + public function get_videos_count($category_id = NULL, $user = NULL, + $unactivated = FALSE) { - $query = $this->db->query( - 'SELECT COUNT(*) count - FROM `videos` - WHERE category_id = ?', - $category_id); - - if ($query->num_rows() > 0) - return $query->row()->count; - - // Error - return NULL; + return $this->get_videos_summary($category_id, $user, 0, TRUE, NULL, + $unactivated); } /** @@ -120,11 +171,11 @@ class Videos_model extends CI_Model { * If $name does not match with the video's `name` from the DB an error is * marked in the key 'err'. If it's NULL it is ignored. * - * @access public - * @param string $id video's `id` column from `videos` DB table - * @param string $name video's `name` column from `videos` DB + * @access public + * @param string $id video's `id` column from `videos` DB table + * @param string $name video's `name` column from `videos` DB * table. NULL means there is no name provided. - * @return array an associative list with information about a video + * @return array an associative list with information about a video * with the following keys: *