public static function exportCommentsForText($projectId, $textId, $params)
 {
     $project = new ProjectModel($projectId);
     $text = new TextModel($project, $textId);
     $usxHelper = new UsxHelper($text->content);
     $textInfo = $usxHelper->getMetadata();
     $questionlist = new QuestionAnswersListModel($project, $textId);
     $questionlist->read();
     $dl = array('complete' => true, 'inprogress' => false, 'answerCount' => 0, 'commentCount' => 0, 'totalCount' => 0, 'xml' => "<CommentList>\n");
     foreach ($questionlist->entries as $question) {
         if (!array_key_exists('isArchived', $question) || !$question['isArchived']) {
             foreach ($question['answers'] as $answerId => $answer) {
                 if (!$params['exportFlagged'] || array_key_exists('isToBeExported', $answer) && $answer['isToBeExported']) {
                     // if the answer is tagged with an export tag
                     $dl['answerCount']++;
                     $dl['xml'] .= self::makeCommentXml($answer['tags'], $answer['score'], $textInfo, $answerId, $answer);
                     if ($params['exportComments']) {
                         foreach ($answer['comments'] as $commentId => $comment) {
                             $dl['xml'] .= self::makeCommentXml(array(), 0, $textInfo, $commentId, $comment);
                             $dl['commentCount']++;
                         }
                     }
                 }
             }
         }
     }
     $dl['totalCount'] = $dl['answerCount'] + $dl['commentCount'];
     $dl['xml'] .= "</CommentList>";
     $dl['filename'] = 'Comments_sf_' . date('Ymd_Gi') . '.xml';
     //$dl['filename'] = preg_replace("([^\w\d\-]|[\.]{2,})", '_', $filename) . '.xml';
     return $dl;
 }
 public function testGetMetadata_Ok()
 {
     $usx = MongoTestEnvironment::usxSample();
     $usxHelper = new UsxHelper($usx);
     $info = $usxHelper->getMetadata();
     $this->assertEqual($info['bookCode'], 'JHN');
     $this->assertEqual($info['startChapter'], 1);
     $this->assertEqual($info['endChapter'], 21);
     $this->assertEqual($info['startVerse'], 1);
     $this->assertEqual($info['endVerse'], 25);
 }
 /**
  *
  * @param string $projectId
  * @param string $textId
  * @param string $userId
  * @return array - the DTO array
  */
 public static function encode($projectId, $textId, $userId)
 {
     $project = new SfchecksProjectModel($projectId);
     $text = new TextModel($project, $textId);
     $user = new UserModel($userId);
     if (($project->isArchived || $text->isArchived) && $project->users[$userId]->role != ProjectRoles::MANAGER) {
         throw new ResourceNotAvailableException("This Text is no longer available. If this is incorrect contact your project manager.");
     }
     $questionList = new QuestionAnswersListModel($project, $textId);
     $questionList->read();
     $data = array();
     $data['rights'] = RightsHelper::encode($user, $project);
     $data['entries'] = array();
     $data['project'] = array('id' => $projectId, 'name' => $project->projectName, 'slug' => $project->databaseName(), 'allowAudioDownload' => $project->allowAudioDownload);
     $data['text'] = JsonEncoder::encode($text);
     $usxHelper = new UsxHelper($text->content);
     $data['text']['content'] = $usxHelper->toHtml();
     foreach ($questionList->entries as $questionData) {
         $question = new QuestionModel($project, $questionData['id']);
         if (!$question->isArchived) {
             // Just want answer count, not whole list
             $questionData['answerCount'] = count($questionData['answers']);
             $responseCount = 0;
             // "Reponses" = answers + comments
             foreach ($questionData['answers'] as $a) {
                 $commentCount = count($a['comments']);
                 $responseCount += $commentCount + 1;
                 // +1 for this answer
             }
             $questionData['responseCount'] = $responseCount;
             unset($questionData['answers']);
             $questionData['dateCreated'] = $question->dateCreated->format(\DateTime::RFC2822);
             $data['entries'][] = $questionData;
         }
     }
     // sort Questions with newest at the top
     usort($data['entries'], function ($a, $b) {
         $sortOn = 'dateCreated';
         if (array_key_exists($sortOn, $a) && array_key_exists($sortOn, $b)) {
             return strtotime($a[$sortOn]) < strtotime($b[$sortOn]) ? 1 : -1;
         } else {
             return 0;
         }
     });
     $data['count'] = count($data['entries']);
     return $data;
 }
 /**
  * Encodes a QuestionModel and related data for $questionId
  * @param string $projectId
  * @param string $questionId
  * @param string $userId
  * @return array - The DTO.
  */
 public static function encode($projectId, $questionId, $userId)
 {
     $user = new UserModel($userId);
     $project = new SfchecksProjectModel($projectId);
     $question = new QuestionModel($project, $questionId);
     $textId = $question->textRef->asString();
     $text = new TextModel($project, $textId);
     if (($text->isArchived || $question->isArchived) && $project->users[$userId]->role != ProjectRoles::MANAGER) {
         throw new ResourceNotAvailableException("This Question is no longer available. If this is incorrect contact your project manager.");
     }
     $usxHelper = new UsxHelper($text->content);
     //echo $usxHelper->toHtml();
     //echo $text->content;
     $votes = new UserVoteModel($userId, $projectId, $questionId);
     $votesDto = array();
     foreach ($votes->votes as $vote) {
         $votesDto[$vote->answerRef->id] = true;
     }
     $unreadAnswerModel = new UnreadAnswerModel($userId, $project->id->asString(), $questionId);
     $unreadAnswers = $unreadAnswerModel->unreadItems();
     $unreadAnswerModel->markAllRead();
     $unreadAnswerModel->write();
     $unreadCommentModel = new UnreadCommentModel($userId, $project->id->asString(), $questionId);
     $unreadComments = $unreadCommentModel->unreadItems();
     $unreadCommentModel->markAllRead();
     $unreadCommentModel->write();
     $unreadActivityModel = new UnreadActivityModel($userId, $projectId);
     $unreadActivity = $unreadActivityModel->unreadItems();
     $dto = array();
     $dto['question'] = QuestionCommentDtoEncoder::encode($question);
     $dto['votes'] = $votesDto;
     $dto['text'] = JsonEncoder::encode($text);
     $dto['text']['content'] = $usxHelper->toHtml();
     $dto['project'] = JsonEncoder::encode($project);
     $dto['project']['slug'] = $project->databaseName();
     $dto['rights'] = RightsHelper::encode($user, $project);
     $dto['unreadAnswers'] = $unreadAnswers;
     $dto['unreadComments'] = $unreadComments;
     $dto['unreadActivityCount'] = count($unreadActivity);
     return $dto;
 }
 private static function _getTextContent($projectModel, $textId)
 {
     if ($textId) {
         $text = new TextModel($projectModel, $textId);
         $usxHelper = new UsxHelper($text->content);
         $html = preg_replace('/<p[^>]*>/', ' ', $usxHelper->toHtml());
         $html = preg_replace('/<div[^>]*>/', ' ', $html);
         $html = preg_replace('/<\\/sup[^>]*>/', ' ', $html);
         $textContent = strip_tags($html);
         return array('title' => $text->title, 'content' => $textContent);
     }
 }