uploading works, but AV info is not automatically detected and video activation featu...
[living-lab-site.git] / application / models / videos_model.php
index 711b967..f927561 100644 (file)
@@ -166,11 +166,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:
         * <ul>
         *   <li>all columns form DB with some exceptions that are overwritten or new</li>
@@ -248,6 +248,63 @@ class Videos_model extends CI_Model {
                return $video;
        }
        
+       /**
+        * 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
+        * @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)
+       {
+               // Tags.
+               $json_tags = array();
+               $tok = strtok($tags, ',');
+               while ($tok != FALSE)
+               {
+                       $json_tags[trim($tok)] = 0;
+                       
+                       $tok = strtok(',');
+               }
+               $json_tags = json_encode($json_tags);
+               
+               // TODO formats
+               $json_formats = json_encode($formats);
+               
+               $query = $this->db->query("INSERT INTO `videos`
+                               (name, title, description, duration, formats, category_id,
+                                               user_id, tags, date)
+                               VALUES ('$name', '$title', '$description', '$duration',
+                                               '$json_formats', $category_id,
+                                               $user_id, '$json_tags', utc_timestamp())");
+               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)
+                               VALUES ($video_id, '$activation_code')");
+               
+               return $activation_code;
+       }
+       
        /**
         * Retrieves comments for a video.
         * 
@@ -471,7 +528,7 @@ class Videos_model extends CI_Model {
                // Search word fragments.
                // sfc = search fragment condition
                $sfc = "( ";
-               // sfr = serach fragment relevation
+               // sfr = search fragment relevance
                $sfr = "( ";
                $sep = ' +-*<>()~"';
                $fragm = strtok($search_query, $sep);
@@ -481,7 +538,7 @@ class Videos_model extends CI_Model {
                                        OR description LIKE '%$fragm%'
                                        OR tags LIKE '%$fragm%') OR ";
                        
-                       // Frament relevations are half of boolean relevations such
+                       // Frament relevances are half of boolean relevances such
                        // that they will appear at the end of the results.
                        $sfr .= "0.25 * (title LIKE '%$fragm%')
                                        + 0.1 * (description LIKE '%$fragm%')
@@ -617,6 +674,19 @@ class Videos_model extends CI_Model {
                return (preg_match('/\*|\+|\-|>|\<|\(|\)|~|"/', $search_query) == 0
                        ? FALSE : TRUE);
        }
+       
+       public static function gen_activation_code()
+       {
+               $ci =& get_instance();
+               
+               $activation_code = substr(
+                       sha1($ci->config->item('encryption_key')
+                               . mt_rand()),
+                       0,
+                       16);
+               
+               return $activation_code;
+       }
 }
 
 /* End of file videos_model.php */