/** * Skip if $this->Question['status'] is accptd * which would be the case when question came from API * and is already answered * * @return object $this */ protected function addUnansweredTags() { if ('accptd' !== $this->Question['status']) { if (count($this->Question['a_tags']) > 0) { $o = new UnansweredTags($this->Registry); $Question = $this->Question; $callable = function () use($o, $Question) { $o->set($Question); }; d('cp'); runLater($callable); } d('cp'); } return $this; }
/** * Delete all questions by this user * as well as remove tags * and add ip address of every question * as key to $this->aIPs array * (as keys and not as values so that * only unique values are added) * * If question is unanswered, also remove from * UNANSWERED_TAGS collection * * @return object $this */ protected function deleteQuestions() { $coll = $this->Registry->Mongo->QUESTIONS; $uid = (int) $this->Request['uid']; $cur = $coll->find(array('i_uid' => $uid)); if ($cur && $cur->count() > 0) { d('got ' . $cur->count() . ' questions to delete'); foreach ($cur as $a) { if (!empty($a['ip'])) { $this->aIPs[$a['ip']] = 1; } /** * If this question is unanswered then also * remove from UNANSWERED_TAGS */ if (empty($a['i_sel_ans']) && !empty($a['a_tags'])) { d('going to add to Unanswered tags'); \Lampcms\UnansweredTags::factory($this->Registry)->remove($a['a_tags']); } /** * Remove from QUESTION_TAGS */ if (!empty($a['a_tags'])) { \Lampcms\Qtagscounter::factory($this->Registry)->removeTags($a['a_tags']); } } /** * Now delete actual question */ $res = $coll->remove(array('i_uid' => $uid), array('safe' => true)); d('questions removed: ' . print_r($res, 1)); } return $this; }
/** * Update USER_TAGS and QUESTION_TAGS collections * to add new tags that belong to this questions * * @return object $this */ protected function addNewTags() { if (count($this->aSubmitted) > 0) { \Lampcms\Qtagscounter::factory($this->Registry)->parse($this->Question); \Lampcms\UserTags::factory($this->Registry)->addTags($this->Question['i_uid'], $this->Question); \Lampcms\Relatedtags::factory($this->Registry)->addTags($this->Question); if (0 === $this->Question['i_sel_ans']) { d('going to add to Unanswered tags'); \Lampcms\UnansweredTags::factory($this->Registry)->set($this->Question); } } return $this; }
/** * Now update tags counter to decrease tags count * but ONLY if deleted item is Question * * @return object $this; */ protected function updateTags() { if (!$this->requested) { if ('QUESTIONS' === $this->collection) { $Question = $this->Resource; \Lampcms\Qtagscounter::factory($this->Registry)->removeTags($Question); if (0 === $this->Resource['i_sel_ans']) { d('going to remove to Unanswered tags'); \Lampcms\UnansweredTags::factory($this->Registry)->remove($Question); } } else { $Question = new \Lampcms\Question($this->Registry); $Question->by_id($this->Resource->getQuestionId()); d('tags: ' . print_r($Question['a_tags'], 1)); } /** * Must extract uid from $this->Resource because in case * the resource is an answer, then the * $Question has different owner, thus * will remove user tags for the wrong user * */ $uid = $this->Resource->getOwnerId(); \Lampcms\UserTags::factory($this->Registry)->removeTags($Question, $uid); } return $this; }
/** * Removes one element from a_latest array * that represents answer passed in param. * * If that array had only one element * then also unset the whole 'a_latest' key * from this object * * @param \Lampcms\Answer|object $Answer object of type Answer * * @return object $this */ public function removeAnswer(Answer $Answer) { $id = $Answer->getResourceId(); $aLatest = $this->offsetGet('a_latest'); for ($i = 0; $i < count($aLatest); $i += 1) { if (!empty($aLatest[$i]) && $id === $aLatest[$i]['id']) { \array_splice($aLatest, $i, 1); break; } } if (0 === count($aLatest)) { $this->offsetUnset('a_latest'); } else { parent::offsetSet('a_latest', $aLatest); } /** * If removed Answer was also a "accepted" answer * then change status to just "answrd" here * * The updateAnswerCount(-1) method * may then change the status to "unans" * if it's determined that this was * the only answer * * Also need to add this question to * UNANSWERED_TAGS again because now * this question is technically unanswered again */ if (true === $Answer['accepted'] && $id === $this->offsetGet(Schema::SELECTED_ANSWER_ID)) { parent::offsetSet(Schema::STATUS, 'answrd'); $this->offsetUnset(Schema::SELECTED_ANSWER_ID); $this->offsetUnset('i_sel_uid'); UnansweredTags::factory($this->Registry)->set($this); } $this->updateAnswerCount(-1)->removeContributor($Answer->getOwnerId()); $this->touch(false); return $this; }