public function getdatas(Request $request)
 {
     $dateline = null;
     $data = null;
     $sort = Input::get("sort");
     $filters = [Input::get("subject") ? Input::get("subject") : null, Input::get("chapter") ? Input::get("chapter") : null, Input::get("topic") ? Input::get("topic") : null, Input::get("rating") ? Input::get("rating") : null, Input::get("level") ? Input::get("level") : null, Input::get("type") ? Input::get("type") : null];
     $activequestions = Question::where('type', '!=', 7)->active()->orderBy("created_at", "DESC");
     //filter subject,chapter,topic
     if ($filters[2]) {
         $activequestions = $activequestions->where('topic_id', $filters[2]);
     } else {
         if ($filters[1]) {
             $activequestions = $activequestions->where('chapter_id', $filters[1]);
         } else {
             if ($filters[0]) {
                 $activequestions = $activequestions->where('subject_id', $filters[0]);
             }
         }
     }
     //filter RATING if
     if ($filters[3]) {
         $activequestions = $activequestions->where('rating', $filters[3]);
     }
     //filter LEVEL if
     if ($filters[4]) {
         $activequestions = $activequestions->where('level', $filters[4]);
     }
     //filter TYPE if
     if ($filters[5]) {
         $activequestions = $activequestions->where('type', $filters[5]);
     }
     //get the query results
     $activequestions = $activequestions->get();
     //
     if ($sort == "authorwise") {
         $data = $this->convertToAuthorwise($activequestions);
         $dateline = $this->getDateLine($activequestions);
     } else {
         if ($sort == "datewise") {
             $data = $this->convertToDatewise($activequestions);
         } else {
             $data = $this->convertToOverall($activequestions);
             $dateline = $this->getDateLine($activequestions);
         }
     }
     return view('superadmin.analytics')->with('questions', $data)->with('datelines', $dateline)->with('sort', $sort)->with('filters', $filters)->with('qtypes', \App\QuestionType::all())->with('subjects', Subject::all())->with('path', $request->path());
 }
 public function payuser($uid)
 {
     $questions = Question::where('type', '!=', 7)->where('author_id', $uid)->active()->get();
     foreach ($questions as $key => $question) {
         if ($question->credit) {
             $question->credit()->update(["status" => 1]);
         }
     }
     return redirect()->back();
 }
 public function questionsbyTopicName($coursename, $subject, $chapterName, $topicName)
 {
     $topics = Topic::where('name', $topicName)->get();
     $topicID = null;
     if (count($topics) == 1) {
         $topicID = $topics->first()->id;
     } else {
         $subjects = Course::where('name', $coursename)->first()->subjects()->where('name', $subject)->get();
         $questions = collect([]);
         foreach ($subjects as $subject) {
             foreach ($subject->modules as $module) {
                 foreach ($module->chapters as $chapter) {
                     if ($chapter->name === $chapterName) {
                         foreach ($chapter->topics as $topic) {
                             if ($topic->name === $topicName) {
                                 $topicID = $topic->id;
                                 break 4;
                             }
                         }
                     }
                 }
             }
         }
     }
     return Question::where('topic_id', $topicID);
 }
 public function getUserCredit($id)
 {
     $questions = Question::where('status', 1)->where('type', '!=', 7)->where('author_id', $id)->get();
     $totalCredits = 0;
     $unpaid = 0;
     $paid = 0;
     foreach ($questions as $key => $question) {
         $credit = $question->credit;
         if ($credit) {
             //old questions dont have credit
             if ($credit->status == 0) {
                 $unpaid += $credit->credit;
             } else {
                 if ($credit->status == 1) {
                     $paid += $credit->credit;
                 }
             }
             $totalCredits += $credit->credit;
         }
     }
     return ["total" => $totalCredits, "paid" => $paid, "unpaid" => $unpaid];
 }