Example #1
0
 private function postAction($params)
 {
     $post = Options::get("active_post");
     $user = Services\FacebookAPI::getInstance()->getUser();
     if ($post && $user && ($post = Posts::getById($post))) {
         /** @var $post Posts */
         /** @var $score Score */
         $score = Score::get(["user_id" => $user->getId(), "post_id" => $post->getId()]);
         if (!$score) {
             $score = Score::Factory(["user_id" => $user->getId(), "post_id" => $post->getId(), "score" => [], "start" => date('Y-m-d H:i:s')]);
         }
         /** @var $post_entry PostEntry */
         $post_entries = $post->getPostentries();
         $score_data = $score->getScore();
         if (isset($params["answer"])) {
             $answer = $post_entries[count($score_data)];
             $answer = PostEntry::getById($answer)->getAnswer();
             $score_data[] = $params["answer"] == $answer ? 1 : 0;
             $score->setScore($score_data);
         }
         $pe_index = count($score_data);
         $score->save();
         $post_entry = isset($post_entries[$pe_index]) ? $post_entries[$pe_index] : null;
         $post_entry = PostEntry::getById($post_entry);
         if ($post_entry) {
             $post_form = new Services\Fields();
             $post_form->Title($post->getTitle(), "h1 class=\"quizz-title center\"");
             $post_form->desc($post_entry->getQuestion());
             foreach ($post_entry->getPropositions() as $k => $p) {
                 $post_form->radio(["name" => "post_entry", "slug" => "post_entry_{$k}", "id" => "post_entry_{$k}", "label" => $p]);
             }
             $post_form->submit(["action" => "send_post_answer", "slug" => "send_post_answer", "icon" => "send", "class" => "right", "text" => "Suivant", "noajax" => true]);
             echo json_encode(["html" => $post_form->render()]);
             exit;
         } else {
             $result = array_count_values($score_data);
             $result = isset($result[1]) ? $result[1] : 0;
             $score->setTotal($result);
             $score->setEnd(date('Y-m-d H:i:s'));
             $score->setTime($score->getTime());
             $score->save();
             $users_score = $score->getSuperiorsScores();
             $users_number = $score->getAllUsers($post->getId());
             $max_score = $users_score[0]['count(total)'];
             $all_users = $users_number[0]['count(id)'];
             $percent = $all_users > 1 ? $max_score / $all_users * 100 : 100;
             $time = $score->getTime();
             $view = Services\View::getInstance();
             echo json_encode(["html" => $view->render('partials/show-result.html', ['result' => $result, 'total' => count($score_data), 'users' => round($percent * 2) / 2, 'url' => "http://" . DOMAIN_NAME, 'app_id' => APP_ID, 'time' => $time])]);
         }
     } else {
         $view = Services\View::getInstance();
         echo json_encode(["html" => $view->render('partials/no-quizz.html')]);
     }
 }
Example #2
0
 public function admin_editPost($params)
 {
     /** @var $post Models\Posts */
     $view = Services\View::getInstance();
     if (isset($params["post"])) {
         $params["post_id"] = $this->savePost($params);
     }
     if (isset($params["post_id"]) && $params["post_id"] !== "new") {
         $post = Models\Posts::getById($params["post_id"]);
         if (!$post) {
             $post = Models\Posts::Factory(["postentries" => [], "id" => "", "title" => "", "created" => date("Y-m-d")]);
         }
         $post->entries = $post->getPostentries();
         foreach ($post->entries as $index => $entry) {
             /** @var $entry Models\PostEntry */
             $entry = Models\PostEntry::getById($entry);
             $post->entries[$index] = ["q" => $entry->getQuestion(), "p" => $entry->getPropositions(), "answer" => $entry->getAnswer(), "id" => $entry->getId()];
         }
     } else {
         $post = Models\Posts::Factory(["postentries" => "", "id" => "", "title" => "", "created" => date("Y-m-d")]);
     }
     $field = new Services\Fields("init");
     $field->text(["value" => $post->getTitle(), "slug" => "post_tilte", "id" => "post_tilte", "label" => "Nom du post"]);
     $field->hidden(["value" => $post->getId(), "slug" => "post_id", "id" => "post_id"]);
     return $this->content_page = $view->render("admin/subpages/edit_post.html", ["currentPostId" => $post->getId(), "isActive" => $post->isActive(), "field" => $field->render(), "entries" => isset($post->entries) ? $post->entries : []]);
 }