+ * Adds a new uploaded video to the DB.
+ *
+ * @param string $name
+ * @param string $title
+ * @param string $description
+ * @param string $tags comma separated tags
+ * @param string $duration video duration formatted [HH:]mm:ss
+ * @param array $formats a dictionary corresponding to `formats` JSON
+ * column from `videos` table
+ * @param int $category_id
+ * @param int $user_id
+ * @param string $uploaded_file the raw video file uploaded by the user
+ * @return mixed returns an activation code on success or FALSE otherwise
+ */
+ public function add_video($name, $title, $description, $tags, $duration,
+ $formats, $category_id, $user_id, $uploaded_file)
+ {
+ $this->load->config('content_ingestion');
+
+ // Tags.
+ $json_tags = array();
+ $tok = strtok($tags, ',');
+ while ($tok != FALSE)
+ {
+ $json_tags[trim($tok)] = 0;
+
+ $tok = strtok(',');
+ }
+ $json_tags = json_encode($json_tags);
+
+ $json_formats = json_encode($formats);
+
+ // Thumbnail images
+ $thumbs_count = $this->config->item('thumbs_count');
+ $default_thumb = rand(0, $thumbs_count - 1);
+
+ $query = $this->db->query("INSERT INTO `videos`
+ (name, title, description, duration, formats, category_id,
+ user_id, tags, date, thumbs_count, default_thumb)
+ VALUES ('$name', '$title', '$description', '$duration',
+ '$json_formats', $category_id,
+ $user_id, '$json_tags', utc_timestamp(),
+ $thumbs_count, $default_thumb)");
+ if ($query === FALSE)
+ return FALSE;
+
+ // Find out the id of the new video added.
+ $query = $this->db->query("SELECT id from `videos`
+ WHERE name = '$name'");
+ if ($query->num_rows() === 0)
+ return FALSE;
+ $video_id = $query->row()->id;
+
+ // Activation code.
+ $activation_code = Videos_model::gen_activation_code();
+
+ $query = $this->db->query("INSERT INTO `videos_unactivated`
+ (video_id, activation_code, uploaded_file)
+ VALUES ($video_id, '$activation_code', '$uploaded_file')");
+
+ return $activation_code;
+ }
+
+ public function set_content_ingested($activation_code)
+ {
+ return $this->db->query("UPDATE `videos_unactivated`
+ SET content_ingested = 1
+ WHERE activation_code = '$activation_code'");
+ }
+
+ /**
+ * Activates a video by deleting its entry from `videos_unactivated`.
+ *
+ * @param mixed $code_or_id use type string for activation_code or type
+ * int for video_id
+ * @return boolean TRUE on success, FALSE otherwise
+ */
+ public function activate_video($code_or_id)
+ {
+ if (is_string($code_or_id))
+ $query = $this->db->query("SELECT uploaded_file from `videos_unactivated`
+ WHERE activation_code = '$code_or_id'");
+ else if (is_int($code_or_id))
+ $query = $this->db->query("SELECT uploaded_file from `videos_unactivated`
+ WHERE video_id = '$code_or_id'");
+ else
+ return FALSE;
+
+ if ($query->num_rows() > 0)
+ $uploaded_file = $query->row()->uploaded_file;
+ else
+ return FALSE;
+
+ if (is_string($code_or_id))
+ $query = $this->db->query("DELETE FROM `videos_unactivated`
+ WHERE activation_code = '$code_or_id'");
+ else if (is_int($code_or_id))
+ $query = $this->db->query("DELETE FROM `videos_unactivated`
+ WHERE video_id = '$code_or_id'");
+ else
+ return FALSE;
+
+ if (!$query)
+ return $query;
+
+ return unlink("data/upload/$uploaded_file");
+ }
+
+ public function get_unactivated_videos()
+ {
+ $query = $this->db->query("SELECT a.video_id, v.name, a.content_ingested
+ FROM `videos_unactivated` a, `videos` v
+ WHERE a.video_id = v.id AND a.content_ingested = 1");
+
+ if ($query->num_rows() > 0)
+ return $query->result_array();
+ else
+ return FALSE;
+ }
+
+ /**
+ * Retrieves comments for a video.
+ *
+ * @param int $video_id
+ * @param int $offset
+ * @param int $count
+ * @param string $ordering control comments ording by these possibilities:
+ * <ul>
+ * <li><strong>'hottest':</strong> newest most appreciated first. An
+ * appreciated comment is one which has a bigger
+ * score = likes - dislikes.</li>
+ * <li><strong>'newest':</strong> newest first.</li>
+ * </ul>
+ * @return array an array with comments
+ */
+ public function get_video_comments($video_id, $offset, $count,
+ $ordering = 'newest')
+ {
+ $this->load->helper('date');
+ $cond_hottest = '';
+
+ // Ordering
+ switch ($ordering)
+ {
+ case 'newest':
+ $order_statement = "ORDER BY time DESC";
+ break;
+ case 'hottest':
+ $order_statement = "ORDER BY score DESC, time DESC";
+ $cond_hottest = "AND c.likes + c.dislikes > 0";
+ break;
+
+ default:
+ $order_statement = "";
+ }
+
+ $query = $this->db->query(
+ "SELECT c.*, u.username, u.time_zone, (c.likes + c.dislikes) AS score
+ FROM `videos_comments` c, `users` u
+ WHERE c.user_id = u.id AND video_id = $video_id $cond_hottest
+ $order_statement
+ LIMIT $offset, $count");
+
+ if ($query->num_rows() == 0)
+ return array();
+
+ $comments = $query->result_array();
+
+ foreach ($comments as & $comment)
+ {
+ $comment['local_time'] = human_gmt_to_human_local($comment['time'],
+ $comment['time_zone']);
+ }
+
+ return $comments;
+ }
+
+ public function get_video_comments_count($video_id)
+ {
+ $query = $this->db->query(
+ "SELECT COUNT(*) count
+ FROM `videos_comments`
+ WHERE video_id = $video_id");
+
+ if ($query->num_rows() == 0)
+ return FALSE;
+
+ return $query->row()->count;
+ }
+
+ /**
+ * Insert in DB a comment for a video.
+ *
+ * @param int $video_id
+ * @param int $user_id
+ * @param string $content
+ */
+ public function comment_video($video_id, $user_id, $content)
+ {
+ // Prepping content.
+ $content = substr($content, 0, 512);
+ $content = htmlspecialchars($content);
+ $content = nl2br($content);
+
+ return $query = $this->db->query(
+ "INSERT INTO `videos_comments` (video_id, user_id, content, time)
+ VALUES ($video_id, $user_id, '$content', UTC_TIMESTAMP())");
+ }
+
+ /**
+ * Increments views count for a video.