public function go()
 {
     $this->setViewTemplate('classmembership.tpl');
     $user_id = $_GET['id'];
     $user = User::getUser($user_id);
     if (isset($_POST['submit'])) {
         $class_id = $_POST['class_id'];
         if (ClassMemberships::doesMembershipExist($user_id, $class_id)) {
             $this->addErrorMessage("User is already a member of this class");
         } else {
             ClassMemberships::addMembership($user_id, $class_id);
             $this->addSuccessMessage("User has been added to the class succesfully");
         }
     } elseif (isset($_GET['action']) && $_GET['action'] == "del") {
         $class_id = $_GET['class_id'];
         ClassMemberships::deleteMembership($user_id, $class_id);
         $this->addSuccessMessage("User has been deleted from the class succesfully");
     }
     $class_memberships = ClassMemberships::getMembershipsOfUser($user_id);
     $classes = Classes::getAllClasses();
     $this->addToView('classes', $classes);
     $this->addToView('class_memberships', $class_memberships);
     $this->addToView('user', $user);
     $this->setViewTemplate('classmembership.tpl');
     $this->generateView();
 }
 public function go()
 {
     $this->setViewTemplate("rankings.tpl");
     if ($this->isLoggedIn()) {
         $username = $this->getLoggedInUser();
         if (Session::isAdmin() || Session::isTeacher()) {
             $classes = Classes::getAllClasses();
         } else {
             $user = User::findByUserName($username);
             $classes = ClassMemberships::getMembershipsOfUserObjects($user->id);
         }
         $this->addToView('classes', $classes);
     }
     if (!isset($_GET["class"]) || $_GET["class"] == "") {
         $rankings = ChallengeAttempts::getUniversalRankings();
     } else {
         $class_id = $_GET["class"];
         $class = Classes::getClass($class_id);
         if (!$class) {
             $this->addErrorMessage("Not a valid class");
             return $this->generateView();
         } else {
             $rankings = ChallengeAttempts::getClasswiseRankings($class_id);
         }
     }
     $final = array();
     $counter = 1;
     $rank = 1;
     $rankcount = 1;
     $prevcount = null;
     foreach ($rankings as $ranking) {
         if ($counter != 1 && $prevcount == $ranking['count']) {
             $rank = $rankcount;
             $rankcount++;
         }
         if ($counter != 1 && $prevcount != $ranking['count']) {
             $rankcount++;
             $rank = $rankcount;
         }
         $prevcount = $ranking['count'];
         $counter++;
         $temp = array('user_id' => $ranking['user_id'], 'time' => $ranking['time'], 'count' => $ranking['count'], 'username' => $ranking['username'], 'rank' => $rank);
         array_push($final, $temp);
     }
     $this->addToView('rankings', $final);
     return $this->generateView();
 }
 public function go()
 {
     $this->setViewTemplate('showclass.tpl');
     if (!isset($_GET['id'])) {
         header('Location: ' . SOURCE_ROOT_PATH . "admin/pages/manageclass.php");
     }
     $class_id = $_GET['id'];
     if (isset($_POST['submit'])) {
         if (isset($_POST['updateclassname'])) {
             if ($_POST['updateclassname'] == '') {
                 header('Location: ' . SOURCE_ROOT_PATH . "admin/pages/showclass.php?id={$class_id}&action=editerror");
             } else {
                 $this->name = $_POST['updateclassname'];
                 Classes::updateClassName($class_id, $this->name);
                 header('Location: ' . SOURCE_ROOT_PATH . "admin/pages/showclass.php?id={$class_id}&action=editsuccess");
             }
         }
     }
     if (isset($_GET['action']) && $_GET['action'] == "editerror") {
         $this->addErrorMessage("Class name should not be empty");
     }
     if (isset($_GET['action']) && $_GET['action'] == "editsuccess") {
         $this->addSuccessMessage("Class name updated successfully");
     }
     if (isset($_GET['action']) && $_GET['action'] == "del") {
         if (isset($_GET['uid'])) {
             ClassMemberships::deleteMembership($_GET['uid'], $class_id);
             $this->addSuccessMessage("User has been deleted from the class succesfully");
         } else {
             if (isset($_GET['cid'])) {
                 ClassChallenges::deleteMembership($_GET['cid'], $class_id);
                 $this->addSuccessMessage("Challenge has been deleted from the class succesfully");
             }
         }
     }
     $class = Classes::getClass($class_id);
     $user_members = ClassMemberships::getAllMemberships($class_id);
     $challenges_assigned = ClassChallenges::getAllMemberships($class_id);
     $this->addToView('class', $class);
     $this->addToView('users', $user_members);
     $this->addToView('challenges', $challenges_assigned);
     return $this->generateView();
 }
 public static function deleteClass($id)
 {
     global $db;
     $params = array(':id' => $id);
     $sql = "DELETE FROM classes WHERE id = :id";
     $query = $db->query($sql, $params);
     ClassChallenges::deleteAllMembershipsOfClass($id);
     ClassMemberships::deleteAllMembershipsOfClass($id);
     if ($db->affectedRows($query)) {
         return true;
     } else {
         return false;
     }
 }
 protected static function isAllowed($username, $challenge_id)
 {
     $user = User::findByUserName($username);
     $classes = ClassMemberships::getMembershipsOfUser($user->id);
     return ClassChallenges::isAllowed($challenge_id, $classes);
 }