public function execute()
 {
     global $wgUser;
     if (!$wgUser->isAllowed('contestadmin') || $wgUser->isBlocked()) {
         $this->dieUsageMsg(array('badaccess-groups'));
     }
     $params = $this->extractRequestParams();
     $everythingOk = true;
     $contestIds = is_null($params['contestids']) ? array() : $params['contestids'];
     $challengeIds = is_null($params['challengeids']) ? array() : $params['challengeids'];
     if (!is_null($params['challengetitles'])) {
         $challenges = ContestChallenge::s()->select('id', array('title' => $params['challengetitles']));
         if ($challenges === false) {
             // TODO: error
         }
         foreach ($challenges as $challenge) {
             $challengeIds[] = $challenge->getId();
         }
     }
     if (!is_null($params['contestnames'])) {
         $contests = Contest::s()->select('id', array('name' => $params['contestnames']));
         if ($contests === false) {
             // TODO: error
         }
         foreach ($contests as $contest) {
             $contestIds[] = $contest->getId();
         }
     }
     $conditions = array();
     if (count($contestIds) > 0) {
         $conditions['contest_id'] = $contestIds;
     }
     if (count($challengeIds) > 0) {
         $conditions['challenge_id'] = $challengeIds;
     }
     if (!is_null($params['ids']) && count($params['ids']) > 0) {
         $conditions['id'] = $params['ids'];
     }
     $contestants = ContestContestant::s()->select(array('id', 'user_id', 'contest_id', 'email'), $conditions);
     $contestantCount = count($contestants);
     if ($contestants !== false && $contestantCount > 0) {
         $setSize = ContestSettings::get('reminderJobSize');
         $limit = count($contestants);
         for ($i = 0; $i <= $limit; $i += $setSize) {
             $this->createReminderJob(array_slice($contestants, $i, $setSize));
         }
     } else {
         $everythingOk = false;
     }
     $this->getResult()->addValue(null, 'success', $everythingOk);
     if ($everythingOk) {
         $this->getResult()->addValue(null, 'contestantcount', $contestantCount);
     }
 }
 /**
  * Called when changing user email address.
  * @see https://www.mediawiki.org/wiki/Manual:Hooks/UserSetEmail
  *
  * Checks if there are any active contests in which the user is participating,
  * and if so, updates the email there as well.
  *
  * @since 0.1
  *
  * @param User $user
  * @param string $email
  *
  * @return true
  */
 public static function onUserSetEmail(User $user, &$email)
 {
     $dbr = wfGetDB(DB_SLAVE);
     $contestants = $dbr->select(array('contest_contestants', 'contests'), array('contestant_id'), array('contest_status' => Contest::STATUS_ACTIVE, 'contestant_user_id' => $user->getId()), __METHOD__, array(), array('contests' => array('INNER JOIN', array('contest_id=contestant_contest_id'))));
     $contestantIds = array();
     foreach ($contestants as $contestant) {
         $contestantIds[] = $contestant->contestant_id;
     }
     if (count($contestantIds) > 0) {
         ContestContestant::s()->update(array('email' => $email), array('id' => $contestantIds));
     }
     return true;
 }
 /**
  * Gets the field definitions for the form.
  *
  * @since 0.1
  *
  * @param Contest $contest
  * @param integer|false $challengeId
  * @return array
  */
 protected function getFormFields(Contest $contest, $challengeId)
 {
     $fields = array();
     $user = $this->getUser();
     $fields['contest-id'] = array('type' => 'hidden', 'default' => $contest->getId(), 'id' => 'contest-id');
     $fields['contestant-realname'] = array('type' => 'text', 'default' => $user->getRealName(), 'label-message' => 'contest-signup-realname', 'required' => true, 'validation-callback' => array(__CLASS__, 'validateNameField'));
     $fields['contestant-email'] = array('type' => 'text', 'default' => $user->getEmail(), 'label-message' => 'contest-signup-email', 'required' => true, 'validation-callback' => array(__CLASS__, 'validateEmailField'));
     $fields['contestant-country'] = array('type' => 'select', 'label-message' => 'contest-signup-country', 'required' => true, 'options' => ContestContestant::getCountriesForInput(true), 'validation-callback' => array(__CLASS__, 'validateCountryField'));
     $fields['contestant-challengeid'] = array('type' => 'radio', 'label-message' => 'contest-signup-challenge', 'options' => $this->getChallengesList($contest), 'required' => true, 'validation-callback' => array(__CLASS__, 'validateChallengeField'));
     if ($challengeId !== false) {
         $fields['contestant-challengeid']['default'] = $challengeId;
     }
     $fields['contestant-volunteer'] = array('type' => 'check', 'default' => '0', 'label-message' => 'contest-signup-volunteer');
     $fields['contestant-wmf'] = array('type' => 'check', 'default' => '0', 'label-message' => 'contest-signup-wmf');
     $fields['contestant-readrules'] = array('type' => 'check', 'default' => '0', 'label-message' => array('contest-signup-readrules', $contest->getField('rules_page')), 'validation-callback' => array(__CLASS__, 'validateRulesField'), 'id' => 'contest-rules', 'data-foo' => 'bar');
     return $fields;
 }
 /**
  * (non-PHPdoc)
  * @see ContestDBObject::writeToDB()
  * @return bool
  */
 public function writeToDB()
 {
     $success = parent::writeToDB();
     if ($success) {
         $contestant = new ContestContestant(array('id' => $this->getField('contestant_id')));
         $contestant->updateVotes();
         $contestant->writeToDB();
     }
     return $success;
 }
 /**
  * Gets a list of contests that can be fed directly to the options field of
  * an HTMLForm radio input.
  * challenge title => challenge id
  *
  * @since 0.1
  *
  * @param ContestContestant $contestant
  *
  * @return array
  */
 protected function getChallengesList(ContestContestant $contestant)
 {
     $list = array();
     $challenges = ContestChallenge::s()->select(array('id', 'title'), array('contest_id' => $contestant->getField('contest_id')));
     foreach ($challenges as $challenge) {
         $list[$challenge->getField('title')] = $challenge->getId();
     }
     return $list;
 }
 /**
  * (non-PHPdoc)
  * @see ContestDBObject::insertIntoDB()
  * @return bool
  */
 protected function insertIntoDB()
 {
     $success = parent::insertIntoDB();
     if ($success) {
         $contestant = new ContestContestant(array('id' => $this->getField('contestant_id')));
         $contestant->addToField('comments', 1);
     }
     return $success;
 }
 /**
  * Remove the contest and all it's linked data from the database.
  *
  * @since 0.1
  *
  * @return boolean Success indicator
  */
 public function removeAllFromDB()
 {
     if (!ContestSettings::get('contestDeletionEnabled')) {
         // Shouldn't get here (UI should prevent it)
         throw new MWException('Contest deletion is disabled', 'contestdeletiondisabled');
     }
     $condition = array('contest_id' => $this->getId());
     $success = ContestChallenge::s()->delete($condition);
     if ($success) {
         $contestantIds = array();
         foreach (ContestContestant::s()->select('id', $condition) as $contestant) {
             $contestantIds[] = $contestant->getId();
         }
         if (count($contestantIds) > 0) {
             $success = ContestComment::s()->delete(array('contestant_id' => $contestantIds)) && $success;
             $success = ContestVote::s()->delete(array('contestant_id' => $contestantIds)) && $success;
         }
         $success = ContestContestant::s()->delete($condition) && $success;
     }
     if ($success) {
         $success = parent::removeFromDB();
     }
     return $success;
 }
    /**
     * Show the comments and a control to add additional ones.
     *
     * @since 0.1
     *
     * @param ContestContestant $contestant
     */
    protected function showComments(ContestContestant $contestant)
    {
        $out = $this->getOutput();
        $out->addHTML(Html::element('h2', array(), wfMsg('contest-contestant-comments')));
        $out->addHTML('<div class="contestant-comments">');
        if ($this->getRequest()->wasPosted()) {
            ContestComment::s()->setReadDb(DB_MASTER);
        }
        $comments = $contestant->getComments();
        ContestComment::s()->setReadDb(DB_SLAVE);
        foreach ($comments as $comment) {
            $out->addHTML($this->getCommentHTML($comment));
        }
        $out->addHTML('</div>');
        $out->addHTML('<div class="contestant-new-comment">
				<textarea cols="40" rows="10" name="new-comment-text"></textarea>
			</div>');
        $out->addHTML(Html::input('submitChanges', wfMsg('contest-contestant-submit'), 'submit'));
    }
 protected function showEnabledPage(Contest $contest)
 {
     $out = $this->getOutput();
     $alreadySignedup = $this->getUser()->isLoggedIn();
     if ($alreadySignedup) {
         // Check if the user is already a contestant in this contest.
         // If he is, reirect to submission page, else show signup form.
         $alreadySignedup = ContestContestant::s()->selectRow('id', array('contest_id' => $contest->getId(), 'user_id' => $this->getUser()->getId())) !== false;
     }
     if ($alreadySignedup) {
         $out->redirect(SpecialPage::getTitleFor('MyContests', $contest->getField('name'))->getLocalURL());
     } else {
         $out->setPageTitle($contest->getField('name'));
         $this->showIntro($contest);
         $this->showChallenges($contest);
         $this->showOpportunities($contest);
         $this->showRules($contest);
         $out->addModules('contest.special.welcome');
     }
 }