Ejemplo n.º 1
0
 /**
  * Get array of up to 30
  * similar questions, create html block from
  * these questions and save in Question
  * under the sim_q key
  *
  * @param \Lampcms\Question $Question
  *
  * @throws \Exception
  * @internal param bool $ret indicates that this is a retry
  *           and prevents against retrying calling itself
  *           more than once
  *
  * @return object $this
  */
 public function getSimilarQuestions(\Lampcms\Question $Question)
 {
     if (!extension_loaded('pdo_mysql')) {
         d('pdo or pdo_mysql not loaded skipping parsing of similar items');
         return $this;
     }
     $qid = (int) $this->Question['_id'];
     $term = $Question['title'];
     $html = '';
     $aRes = array();
     $sql = "SELECT\r\n\t\t\t\tqid, \r\n\t\t\t\ttitle, \r\n\t\t\t\turl, \r\n\t\t\t\tintro\r\n\t\t\t\tFROM question_title\r\n\t\t\t\tWHERE \r\n\t\t\t\tqid != :qid\r\n \t\t\t\tAND " . self::BY_TITLE . "\r\n\t\t\t\tLIMIT 30";
     d('$sql: ' . $sql);
     try {
         $sth = $this->Registry->Db->makePrepared($sql);
         $sth->bindParam(':qid', $qid, \PDO::PARAM_INT);
         $sth->bindParam(':subj', $term, \PDO::PARAM_STR);
         $sth->execute();
         $aRes = $sth->fetchAll();
         d('found ' . count($aRes) . ' similar questions ' . print_r($aRes, 1));
         if (!empty($aRes)) {
             $html = \tplSimquestions::loop($aRes);
             $s = '<div id="sim_questions" class="similars">' . $html . '</div>';
             d('html: ' . $s);
             $Question->offsetSet('sim_q', $s);
             $Question->save();
         }
     } catch (\Exception $e) {
         $err = 'Exception: ' . get_class($e) . ' Unable to select mysql because: ' . $e->getMessage() . ' Err Code: ' . $e->getCode() . ' trace: ' . $e->getTraceAsString();
         d('mysql error: ' . $err);
         if ('42S02' === $e->getCode()) {
             if (true === TitleTagsTable::create($this->Registry)) {
                 return $this;
             } else {
                 throw $e;
             }
         } else {
             throw $e;
         }
     }
     return $this;
 }