+ /**
+ * 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;
+ }
+