Beispiel #1
0
 /**
  * Delete all answers made by user
  * Also update questions affected by
  * deletion of those answers
  *
  * @return object $this
  */
 protected function deleteAnswers()
 {
     $coll = $this->Registry->Mongo->ANSWERS;
     $cur = $coll->find(array('i_uid' => $this->Request['uid']));
     if ($cur && $cur->count() > 0) {
         foreach ($cur as $a) {
             $Question = new \Lampcms\Question($this->Registry);
             try {
                 $Answer = new Answer($this->Registry, $a);
                 $Question->by_id((int) $Answer->getQuestionId());
                 $Question->removeAnswer($Answer);
                 $Question->save();
                 /**
                  * setSaved() because we don't need auto-save feature
                  * to save each answer
                  * since all answers will be removed at end of this method
                  */
                 $Answer->setSaved();
             } catch (\MongoException $e) {
                 d('Question not found by _id: ' . $a['i_qid']);
             }
             if (!empty($a['cc'])) {
                 $this->aCountries[] = $a['cc'];
             }
         }
         $res = $coll->remove(array('i_uid' => $this->Request['uid']), array('safe' => true));
         d('questions removed: ' . print_r($res, 1));
     }
     return $this;
 }
Beispiel #2
0
 /**
  * If answer is deleted and it was
  * the "selected" answer then
  * we must update question and set it as
  * 'unanswered' again
  *
  * @return object $this;
  */
 protected function updateQuestion()
 {
     if ('ANSWERS' === $this->collection) {
         $Question = new \Lampcms\Question($this->Registry);
         $Question->by_id($this->Resource['i_qid']);
         $Question->removeAnswer($this->Resource);
         if (true === $this->Resource['accepted']) {
             d('this was an accepted answer');
             $this->Resource->unsetAccepted();
         }
         $Question->touch()->save();
     }
     return $this;
 }