root/trunk/lib/model/User.php

Revision 88, 3.2 kB (checked in by fabien, 2 years ago)

updated to symfony 1.0 beta 1

  • 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 require_once 'lib/model/om/BaseUser.php';
4
5
6 /**
7  * Skeleton subclass for representing a row from the 'ask_user' table.
8  *
9  *
10  *
11  * You should add additional methods to this class to meet the
12  * application requirements.  This class will only be generated as
13  * long as it does not already exist in the output directory.
14  *
15  * @package model
16  */   
17 class User extends BaseUser
18 {
19   public function __toString()
20   {
21     if ($this->getLastName())
22     {
23       return ucfirst(strtolower($this->getFirstName())).' '.strtoupper($this->getLastName());
24     }
25     else
26     {
27       return $this->getNickname();
28     }
29   }
30
31   public function isInterestedIn($question)
32   {
33     $interest = new Interest();
34     $interest->setQuestion($question);
35     $interest->setUserId($this->getId());
36     $interest->save();
37   }
38
39   public function setPassword($password)
40   {
41     $salt = md5(rand(100000, 999999).$this->getNickname().$this->getEmail());
42     $this->setSalt($salt);
43     $this->setSha1Password(sha1($salt.$password));
44   }
45
46   public function getTagsFor($question, $max = 10)
47   {
48     $tags = array();
49
50     $con = Propel::getConnection();
51     $query = '
52       SELECT %s AS tag, %s AS raw_tag, COUNT(%s) AS count
53       FROM %s
54       WHERE %s = ? AND %s = ?
55       GROUP BY %s
56       ORDER BY count DESC
57     ';
58
59     $query = sprintf($query,
60       QuestionTagPeer::NORMALIZED_TAG,
61       QuestionTagPeer::TAG,
62       QuestionTagPeer::NORMALIZED_TAG,
63       QuestionTagPeer::TABLE_NAME,
64       QuestionTagPeer::QUESTION_ID,
65       QuestionTagPeer::USER_ID,
66       QuestionTagPeer::NORMALIZED_TAG
67     );
68
69     $stmt = $con->prepareStatement($query);
70     $stmt->setInt(1, $question->getId());
71     $stmt->setInt(2, $this->getId());
72     $stmt->setLimit($max);
73     $rs = $stmt->executeQuery();
74     while ($rs->next())
75     {
76       if (sfConfig::get('app_permanent_tag') == $rs->getString('tag'))
77       {
78         continue;
79       }
80
81       $tags[$rs->getString('tag')] = $rs->getString('raw_tag');
82     }
83
84     return $tags;
85   }
86
87   public function removeTag($question, $tag)
88   {
89     $c = new Criteria();
90     $c->add(QuestionTagPeer::QUESTION_ID, $question->getId());
91     $c->add(QuestionTagPeer::USER_ID, $this->getId());
92     $c->add(QuestionTagPeer::NORMALIZED_TAG, Tag::normalize($tag));
93
94     QuestionTagPeer::doDelete($c);
95   }
96
97   public function getPopularTags($max = 40)
98   {
99     $tags = array();
100
101     $con = Propel::getConnection();
102
103     // get popular tags
104     $query = '
105       SELECT '.QuestionTagPeer::NORMALIZED_TAG.' AS tag, COUNT('.QuestionTagPeer::NORMALIZED_TAG.') AS count
106       FROM '.QuestionTagPeer::TABLE_NAME.'
107       WHERE '.QuestionTagPeer::USER_ID.' = ?
108       GROUP BY '.QuestionTagPeer::NORMALIZED_TAG.'
109       ORDER BY count DESC
110     ';
111
112     $stmt = $con->prepareStatement($query);
113     $stmt->setInt(1, $this->getId());
114     $stmt->setLimit($max);
115     $rs = $stmt->executeQuery();
116     $max_popularity = 0;
117     while ($rs->next())
118     {
119       if (sfConfig::get('app_permanent_tag') == $rs->getString('tag'))
120       {
121         continue;
122       }
123
124       if (!$max_popularity)
125       {
126         $max_popularity = $rs->getInt('count');
127       }
128
129       $tags[$rs->getString('tag')] = floor(($rs->getInt('count') / $max_popularity * 3) + 1);
130     }
131
132     ksort($tags);
133
134     return $tags;
135   }
136 }
137
138 ?>
Note: See TracBrowser for help on using the browser.