public function go()
 {
     if (isset($_GET['id'])) {
         $id = $_GET['id'];
     }
     if (isset($_POST['submit'])) {
         if ($_POST['title'] == '') {
             $this->addErrorMessage("Title of the challenge should not be empty");
         } elseif ($_POST['description'] == '') {
             $this->addErrorMessage("Description should not be empty");
         } elseif ($_POST['visibility'] == '') {
             $this->addErrorMessage("Visibility field should not be empty");
         } else {
             $this->title = $_POST['title'];
             $this->description = $_POST['description'];
             $this->visibility = $_POST['visibility'];
             $this->publish = $_POST['publish'];
             ChallengeBackend::updateChallenge($id, $this->title, $this->description, $this->visibility, $this->publish);
             $this->addSuccessMessage("Challenge details have been updated succesfully");
         }
     }
     $challenges = Challenge::getChallenge($id);
     $this->setViewTemplate('editchallenge.tpl');
     $this->addToView('challenge', $challenges[0]);
     $this->generateView();
 }
 public function go()
 {
     if (isset($_GET["action"]) && $_GET["action"] == "del") {
         $id = $_GET['id'];
         $challenge = ChallengeBackend::getChallenge($id);
         $pkg_name = $challenge[0]->pkg_name;
         self::rrmdir(HACKADEMIC_PATH . "challenges/" . $pkg_name);
         ChallengeBackend::deleteChallenge($id);
         $this->addSuccessMessage("Challenge has been deleted succesfully");
     } else {
         if (isset($_GET['action']) && $_GET['action'] == "add") {
             $this->addSuccessMessage("Challenge has been added succesfully. You can enable the challenge now.");
             if (isset($_SESSION['challenge_arr'])) {
                 unset($_SESSION['challenge_arr']);
             }
         }
     }
     if (isset($_GET['limit']) && $_GET['limit'] != "") {
         $limit = $_GET['limit'];
     } else {
         $limit = 25;
     }
     $total_pages = ChallengeBackend::getNumberOfChallenges();
     $targetpage = SOURCE_ROOT_PATH . "admin/pages/challengemanager.php";
     $stages = 3;
     $page = 0;
     if (isset($_GET['page'])) {
         $page = $_GET['page'];
     }
     if ($page) {
         $start = ($page - 1) * $limit;
     } else {
         $start = 0;
     }
     // Initial page num setup
     if ($page == 0) {
         $page = 1;
     }
     $prev = $page - 1;
     $next = $page + 1;
     $lastpage = ceil($total_pages / $limit);
     $LastPagem1 = $lastpage - 1;
     $pagination = array('lastpage' => $lastpage, 'page' => $page, 'targetpage' => $targetpage, 'prev' => $prev, 'next' => $next, 'stages' => $stages, 'last_page_m1' => $LastPagem1);
     if (isset($_GET['search']) && isset($_GET['category']) && $_GET['search'] != '' && $_GET['category'] != '') {
         $challenges = ChallengeBackend::getNchallenges($start, $limit, $_GET['search'], $_GET['category']);
     } else {
         $challenges = ChallengeBackend::getNchallenges($start, $limit);
     }
     $this->addToView('challenges', $challenges);
     $this->addToView('total_pages', $total_pages);
     $this->addToView('pagination', $pagination);
     $this->setViewTemplate('challengemanager.tpl');
     $this->generateView();
 }
 public function go()
 {
     $this->setViewTemplate('addchallenge.tpl');
     if (isset($_GET['type']) && $_GET['type'] == "code") {
         $add_type = "code";
     } else {
         $add_type = "challenge";
     }
     if (isset($_POST['continue'])) {
         if ($_POST['title'] == '') {
             $this->addErrorMessage("Title of the challenge should not be empty");
         } elseif ($_POST['description'] == '') {
             $this->addErrorMessage("Description should not be empty");
         } elseif ($_POST['authors'] == '') {
             $this->addErrorMessage("Authors field should not be empty");
         } elseif ($_POST['category'] == '') {
             $this->addErrorMessage("Category field should not be empty");
         } else {
             $array = array('title' => $_POST['title'], 'description' => $_POST['description'], 'authors' => $_POST['authors'], 'category' => $_POST['category']);
             $_SESSION['challenge_arr'] = $array;
             $this->addSuccessMessage("Now Please upload the challenge code");
             $this->addToView('step', 'step2');
         }
     }
     if (isset($_FILES['fupload'])) {
         $filename = $_FILES['fupload']['name'];
         $source = $_FILES['fupload']['tmp_name'];
         $type = $_FILES['fupload']['type'];
         $name = explode('.', $filename);
         $target = HACKADEMIC_PATH . "challenges/" . $name[0] . '/';
         if (!isset($name[1])) {
             $this->addErrorMessage("Please select a file");
             return $this->generateView();
         }
         if (isset($name[0])) {
             $challenge = ChallengeBackend::doesChallengeExist($name[0]);
             if ($challenge == true) {
                 if (isset($_SESSION['challenge_arr'])) {
                     $this->addToView('step', 'step2');
                 } else {
                     $this->addToView('type', $add_type);
                 }
                 $this->addErrorMessage("This file already exists!!");
                 return $this->generateView();
             }
         }
         $okay = strtolower($name[1]) == 'zip' ? true : false;
         if (!$okay) {
             if (isset($_SESSION['challenge_arr'])) {
                 $this->addToView('step', 'step2');
             } else {
                 $this->addToView('type', $add_type);
             }
             $this->addErrorMessage("Please choose a zip file!");
             return $this->generateView();
         }
         mkdir($target);
         $saved_file_location = $target . $filename;
         if (move_uploaded_file($source, $target . $filename)) {
             $data = $this->installChallenge($saved_file_location, $target, $name[0]);
             if ($data == true) {
                 $pkg_name = $name[0];
                 $date_posted = date("Y-m-d H-i-s");
                 ChallengeBackend::addchallenge($data['title'], $pkg_name, $data['description'], $data['author'], $data['category'], $date_posted);
                 header('Location: ' . SOURCE_ROOT_PATH . "admin/pages/challengemanager.php?action=add");
             }
         }
     }
     $this->addToView('type', $add_type);
     return $this->generateView();
 }