root/trunk/lib/model/QuestionTagPeer.php

Revision 40, 3.4 kB (checked in by fabien, 8 years ago)

added filters + some refactoring (model)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3   // include base peer class
4   require_once 'model/om/BaseQuestionTagPeer.php';
5  
6   // include object class
7   include_once 'model/QuestionTag.php';
8
9
10 /**
11  * Skeleton subclass for performing query and update operations on the 'ask_question_tag' table.
12  *
13  *
14  *
15  * You should add additional methods to this class to meet the
16  * application requirements.  This class will only be generated as
17  * long as it does not already exist in the output directory.
18  *
19  * @package model
20  */   
21 class QuestionTagPeer extends BaseQuestionTagPeer
22 {
23   public function getPopularTags($max = 5)
24   {
25     $tags = array();
26
27     $con = Propel::getConnection();
28     $query = '
29       SELECT t1.normalized_tag AS tag,
30       COUNT(t1.normalized_tag) AS count
31       FROM '.QuestionTagPeer::TABLE_NAME.' AS t1';
32
33     if (defined('APP_PERMANENT_TAG'))
34     {
35       $query .= '
36         INNER JOIN '.QuestionTagPeer::TABLE_NAME.' AS t2 ON t1.question_id = t2.question_id
37         WHERE t2.normalized_tag = ? AND t1.normalized_tag != ?
38       ';
39     }
40
41     $query .= '
42       GROUP BY t1.normalized_tag
43       ORDER BY count DESC
44     ';
45
46     $stmt = $con->prepareStatement($query);
47     if (defined('APP_PERMANENT_TAG'))
48     {
49       $stmt->setString(1, APP_PERMANENT_TAG);
50       $stmt->setString(2, APP_PERMANENT_TAG);
51     }
52     $stmt->setLimit($max);
53     $rs = $stmt->executeQuery();
54     $max_popularity = 0;
55     while ($rs->next())
56     {
57       if (!$max_popularity)
58       {
59         $max_popularity = $rs->getInt('count');
60       }
61
62       $tags[$rs->getString('tag')] = floor(($rs->getInt('count') / $max_popularity * 3) + 1);
63     }
64
65     ksort($tags);
66
67     return $tags;
68   }
69
70   public static function getPopularTagsFor($question, $max = 10)
71   {
72     $tags = array();
73
74     $con = Propel::getConnection();
75
76     // get popular tags
77     $query = '
78       SELECT '.QuestionTagPeer::NORMALIZED_TAG.' AS tag, COUNT('.QuestionTagPeer::NORMALIZED_TAG.') AS count
79       FROM '.QuestionTagPeer::TABLE_NAME;
80     if ($question !== null)
81     {
82       $query .= '  WHERE '.QuestionTagPeer::QUESTION_ID.' = ?';
83     }
84     $query .= '
85       GROUP BY '.QuestionTagPeer::NORMALIZED_TAG.'
86       ORDER BY count DESC
87     ';
88
89     $stmt = $con->prepareStatement($query);
90     if ($question !== null)
91     {
92       $stmt->setInt(1, $question->getId());
93     }
94     $stmt->setLimit($max);
95     $rs = $stmt->executeQuery();
96     $max_popularity = 0;
97     while ($rs->next())
98     {
99       if (defined('APP_PERMANENT_TAG') && APP_PERMANENT_TAG == $rs->getString('tag'))
100       {
101         continue;
102       }
103
104       if (!$max_popularity)
105       {
106         $max_popularity = $rs->getInt('count');
107       }
108
109       $tags[$rs->getString('tag')] = floor(($rs->getInt('count') / $max_popularity * 3) + 1);
110     }
111
112     ksort($tags);
113
114     return $tags;
115   }
116
117   public static function getForUserLike($user_id, $tag)
118   {
119     $tags = array();
120
121     $con = Propel::getConnection();
122     $query = '
123       SELECT DISTINCT %s AS tag
124       FROM %s
125       WHERE %s = ? AND %s LIKE ?
126       ORDER BY %s
127     ';
128
129     $query = sprintf($query,
130       QuestionTagPeer::TAG,
131       QuestionTagPeer::TABLE_NAME,
132       QuestionTagPeer::USER_ID,
133       QuestionTagPeer::TAG,
134       QuestionTagPeer::TAG
135     );
136
137     $stmt = $con->prepareStatement($query);
138     $stmt->setInt(1, $user_id);
139     $stmt->setString(2, $tag.'%');
140     $stmt->setLimit(10);
141     $rs = $stmt->executeQuery();
142     while ($rs->next())
143     {
144       $tags[] = $rs->getString('tag');
145     }
146
147     return $tags;
148   }
149 }
150
151 ?>
Note: See TracBrowser for help on using the browser.