Esempio n. 1
0
 public function getDocs()
 {
     // Handle order by.
     $order_field = Input::get('order', 'updated_at');
     $order_dir = Input::get('order_dir', 'DESC');
     // Handle pagination.
     $limit = null;
     $offset = null;
     $title = null;
     if (Input::has('limit')) {
         $limit = Input::get('limit');
         if (Input::has('page')) {
             $offset = (Input::get('page') - 1) * Input::get('limit');
         }
     }
     // Activity is a wholly different beast right now, requiring complicated
     // queries.
     if ($order_field === 'activity') {
         // TODO: Make this handle DESC order, maybe?
         $docs = Doc::getActive($limit, $offset);
     } else {
         $doc = Doc::getEager()->orderBy($order_field, $order_dir)->where('private', '!=', '1')->where('is_template', '!=', '1');
         if (Input::has('category')) {
             $doc = Doc::getEager()->whereHas('categories', function ($q) {
                 $category = Input::get('category');
                 $q->where('categories.name', 'LIKE', "%{$category}%");
             })->where('private', '!=', '1')->where('is_template', '!=', '1');
         }
         if (isset($limit)) {
             $doc->take($limit);
             if (isset($offset)) {
                 $doc->skip($offset);
             }
         }
         if (Input::has('title')) {
             $title = Input::get('title');
             $doc->where('title', 'LIKE', "%{$title}%");
         }
         $docs = $doc->get();
     }
     $return_docs = array();
     if ($docs) {
         foreach ($docs as $doc) {
             $doc->enableCounts();
             $return_doc = $doc->toArray();
             $return_doc['updated_at'] = date('c', strtotime($return_doc['updated_at']));
             $return_doc['created_at'] = date('c', strtotime($return_doc['created_at']));
             $return_docs[] = $return_doc;
         }
     }
     return Response::json($return_docs);
 }
Esempio n. 2
0
 public static function getActive($num, $offset)
 {
     // Defaults to limit 10 because of the expense here.
     if (!$num) {
         $num = 10;
     }
     if (!$offset) {
         $offset = 0;
     }
     $docIds = DB::select(DB::raw("SELECT doc_id, SUM(num) AS total FROM (\n                    SELECT doc_id, COUNT(*) AS num\n                        FROM annotations\n                        GROUP BY doc_id\n                    UNION ALL\n                    SELECT doc_id, COUNT(*) AS num\n                        FROM comments\n                        GROUP BY doc_id\n                    UNION ALL\n                    SELECT annotations.doc_id, COUNT(*) AS num\n                        FROM annotation_comments\n                        INNER JOIN annotations\n                        ON annotation_comments.annotation_id = annotations.id\n                        GROUP BY doc_id\n\n                ) total_count\n                LEFT JOIN docs on doc_id = docs.id\n                WHERE docs.private != 1\n                GROUP BY doc_id\n                ORDER BY total DESC\n                LIMIT :offset, :limit"), array(':offset' => $offset, ':limit' => $num));
     $docArray = [];
     //Create array of [id => total] for each document
     foreach ($docIds as $docId) {
         $docArray[$docId->doc_id] = $docId->total;
     }
     $docs = false;
     if (count($docArray) > 0) {
         //Grab out most active documents
         $docs = Doc::getEager()->whereIn('id', array_keys($docArray))->get();
         //Set the sort value to the total count
         foreach ($docs as $doc) {
             $doc->participationTotal = $docArray[$doc->id];
         }
         //Sort by the sort value descending
         $docs = $docs->sortByDesc('participationTotal');
     }
     return $docs;
 }