source: trunk/lib/classes/score.class.php @ 21463

Revision 21463, 15.0 KB checked in by tgloeggl, 2 months ago (diff)

refs #651, fix violation of coding conventions, replace tabs by spaces

  • Property svn:eol-style set to native
Line 
1<?
2# Lifter002: TODO
3# Lifter007: TODO
4# Lifter003: TODO
5# Lifter010: TODO
6/**
7 * score.class.php - Score class
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * @author      Ralf Stockmann <rstockm@gwdg.de>
15 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
16 * @category    Stud.IP
17 */
18
19class Score
20{
21    var $score; // Score of the user
22    var $publik;    // whether or not the score is published
23    var $ismyscore; // wheter or not this is my own score
24    var $title; // Title that refers to the score
25    var $myscore;   // my own Score
26    var $mygender;
27    var $score_content_cache = null;
28
29
30    // Konstruktor
31    function Score($user_id)
32    {
33        $this->ismyscore = $this->CheckOwner($user_id);
34        if ($this->ismyscore){
35            $this->myscore = $this->GetMyScore();
36        }
37        $this->mygender = $this->GetGender($user_id);
38        $this->title = $this->gettitel($this->myscore, $this->mygender);
39        $this->publik = $this->CheckScore($user_id);
40    }
41
42    function CheckOwner($user_id)
43    {
44        global $user;
45        if ($user_id == $user->id)
46            return TRUE;
47        else
48            return FALSE;
49    }
50
51    function GetGender($user_id)
52    {
53        $db=new DB_Seminar;
54        $db->query("SELECT geschlecht AS gender FROM user_info WHERE user_id = '$user_id'");
55        $db->next_record();
56        return $db->f("gender");
57    }
58
59    function PublishScore()
60    {
61        global $user;
62        $db=new DB_Seminar;
63        $query = "UPDATE user_info "
64            ." SET score = '$this->myscore'"
65            ." WHERE user_id = '$user->id'";
66        $db->query($query);
67        $this->publik = $this->myscore;
68    }
69
70    function KillScore()
71    {
72        global $user;
73        $db=new DB_Seminar;
74        $query = "UPDATE user_info "
75            ." SET score = 0"
76            ." WHERE user_id = '$user->id'";
77        $db->query($query);
78        $this->publik = FALSE;
79    }
80
81    function IsMyScore()
82    {
83        return $this->ismyscore;
84    }
85
86    function ReturnMyScore()
87    {
88        return $this->myscore;
89    }
90
91    function ReturnMyTitle()
92    {
93        return $this->title;
94    }
95
96    function ReturnPublik()
97    {
98        return $this->publik;
99    }
100
101    function GetScore($user_id)
102    {
103        $db=new DB_Seminar;
104        $db->query("SELECT score FROM user_info WHERE user_id = '$user_id'");
105        $db->next_record();
106        return $db->f("score");
107    }
108
109    function CheckScore($user_id)
110    {
111        $db=new DB_Seminar;
112        $db->query("SELECT score FROM user_info WHERE user_id = '$user_id' AND score > 0");
113        if ($db->next_record())
114            return $db->f("score");
115        else
116            return FALSE;
117    }
118
119    function doRefreshScoreContentCache()
120    {
121        $db = new DB_Seminar("SELECT a.user_id,username FROM user_info a LEFT JOIN auth_user_md5 b USING (user_id) WHERE score > 0");
122        $s = 0;
123        while ($db->next_record()){
124            $this->score_content_cache[$db->f('user_id')]['username'] = $db->f('username');
125            ++$s;
126        }
127        if ($s) {
128            $db->query("SELECT count(u.user_id) as guestcount,u.user_id FROM user_info u INNER JOIN guestbook ON(range_id=u.user_id)
129                        WHERE score > 0 AND guestbook=1 GROUP BY u.user_id ORDER BY NULL");
130            while ($db->next_record()){
131                $this->score_content_cache[$db->f('user_id')]['guestcount'] = $db->f('guestcount');
132            }
133            $db->query("SELECT count(u.user_id) as newscount,u.user_id FROM user_info u
134                JOIN news_range nr ON(nr.range_id=u.user_id)
135                INNER JOIN news n ON nr.news_id=n.news_id
136                WHERE u.score > 0 AND (" . gmmktime() . "-n.date) <= n.expire
137                GROUP BY u.user_id
138                ORDER BY NULL");
139            while ($db->next_record()){
140                $this->score_content_cache[$db->f('user_id')]['newscount'] = $db->f('newscount');
141            }
142            $db->query("SELECT count(u.user_id) as eventcount,u.user_id FROM user_info u
143                INNER JOIN calendar_events ON (range_id=u.user_id AND class = 'PUBLIC')
144                WHERE score > 0  AND " . gmmktime() . " <= end
145                GROUP BY u.user_id ORDER BY NULL");
146            while ($db->next_record()){
147                $this->score_content_cache[$db->f('user_id')]['eventcount'] = $db->f('eventcount');
148            }
149            $db->query("SELECT count(u.user_id) AS litcount, u.user_id FROM user_info u INNER JOIN lit_list ON(range_id=u.user_id) INNER JOIN lit_list_content USING ( list_id )
150                        WHERE score > 0  AND visibility = 1 GROUP BY u.user_id ORDER BY NULL");
151            while ($db->next_record()){
152                $this->score_content_cache[$db->f('user_id')]['litcount'] = $db->f('litcount');
153            }
154            if (get_config('VOTE_ENABLE')){
155                $db->query("SELECT count(u.user_id) AS votecount,u.user_id FROM user_info u INNER JOIN vote ON(range_id=u.user_id) WHERE  score > 0 GROUP BY u.user_id ORDER BY NULL");
156                while ($db->next_record()){
157                    $this->score_content_cache[$db->f('user_id')]['votecount'] = $db->f('votecount');
158                }
159            }
160        }
161        return true;
162    }
163
164    /**
165     *
166     * @param md5 $user_id
167     */
168    function GetScoreContent($user_id)
169    {
170        if (!is_array($this->score_content_cache)){
171            $this->doRefreshScoreContentCache();
172        }
173        $username = $this->score_content_cache[$user_id]['username'];
174        if ( ($gaeste = $this->score_content_cache[$user_id]['guestcount']) !== null ) {
175            if ($gaeste == 1)
176                $tmp = _("Gästebuch mit einem Eintrag");
177            else
178                $tmp = sprintf(_("Gästebuch mit %s Einträgen"), $gaeste);
179            $content .= "<a href=\"about.php?username=$username&guestbook=open#guest\"><img src=\"".Assets::image_path('icons/16/blue/guestbook.png')."\" ".tooltip("$tmp")."></a> ";
180        } else {
181            $content .= "<img src=\"".$GLOBALS['ASSETS_URL']."images/blank.gif\" width=\"16\"> ";
182        }
183
184        if ( ($news = $this->score_content_cache[$user_id]['newscount']) ) {
185            if ($news == 1) {
186                $tmp = _("Eine persönliche Ankündigung");
187            } else {
188                $tmp = sprintf(_("%s persönliche Ankündigungen"), $news);
189            }
190            $content .= "<a href=\"about.php?username=$username\"><img src=\"".Assets::image_path('icons/16/blue/breaking-news.png')."\" ".tooltip($tmp)."></a> ";
191        } else {
192            $content .= "<img src=\"".$GLOBALS['ASSETS_URL']."images/blank.gif\" width=\"16\"> ";
193        }
194        if ( ($vote = $this->score_content_cache[$user_id]['votecount']) ) {
195            if ($vote == 1) {
196                $tmp = _("Eine Umfrage");
197            } else {
198                $tmp = sprintf(_("%s Umfragen"), $vote);
199            }
200            $content .= "<a href=\"about.php?username=$username\"><img src=\"".Assets::image_path('icons/16/blue/vote.png')."\" ".tooltip($tmp)."></a> ";
201        } else {
202            $content .= "<img src=\"".$GLOBALS['ASSETS_URL']."images/blank.gif\" width=\"16\"> ";
203        }
204
205        if ( ($termin = $this->score_content_cache[$user_id]['eventcount']) ) {
206            if ($termin == 1)
207                $tmp = _("Termin");
208            else
209                $tmp = _("Termine");
210            $content .= "<a href=\"about.php?username=$username#a\"><img src=\"".Assets::image_path('icons/16/blue/schedule.png')."\" ".tooltip("$termin $tmp")."></a> ";
211        } else {
212            $content .= "<img src=\"".$GLOBALS['ASSETS_URL']."images/blank.gif\" width=\"16\"> ";
213        }
214
215        if ( ($lit = $this->score_content_cache[$user_id]['litcount']) ) {
216            if ($lit == 1)
217                $tmp = _("Literaturangabe");
218            else
219                $tmp = _("Literaturangaben");
220            $content .= "<a href=\"about.php?username=$username\"><img src=\"".Assets::image_path('icons/16/blue/literature.png')."\" ".tooltip("$lit $tmp")."></a> ";
221        } else {
222            $content .= "<img src=\"".$GLOBALS['ASSETS_URL']."images/blank.gif\" width=\"16\"> ";
223        }
224        return $content;
225    }
226
227    /**
228    * Retrieves the titel for a given studip score
229    *
230    * @param        integer a score value
231    * @param        integer gender (0: unknown, 1: male; 2: female)
232    * @return       string  the titel
233    *
234    */
235    function gettitel($score, $gender = 0)
236    {
237        if ($score)
238            $logscore = floor(log10($score) / log10(2));
239        else
240            $logscore = 0;
241
242        if ($logscore > 20)
243            $logscore = 20;
244
245        $titel[0]  =    array(0 => _("Unbeschriebenes Blatt"), 1 => _("Unbeschriebenes Blatt"));
246        $titel[1]  =    array(0 => _("Unbeschriebenes Blatt"), 1 => _("Unbeschriebenes Blatt"));
247        $titel[2]  =    array(0 => _("Unbeschriebenes Blatt"), 1 => _("Unbeschriebenes Blatt"));
248        $titel[3]  =    array(0 => _("Neuling"), 1 => _("Neuling"));
249        $titel[4]  =    array(0 => _("Greenhorn"), 1 => _("Greenhorn"));
250        $titel[5]  =    array(0 => _("Anf&auml;nger"), 1 => _("Anf&auml;ngerin"));
251        $titel[6]  =    array(0 => _("Einsteiger"), 1 => _("Einsteigerin"));
252        $titel[7]  =    array(0 => _("Beginner"), 1 => _("Beginnerin"));
253        $titel[8]  =    array(0 => _("Novize"), 1 => _("Novizin"));
254        $titel[9]  =    array(0 => _("Fortgeschrittener"), 1 => _("Fortgeschrittene"));
255        $titel[10] =    array(0 => _("Kenner"), 1 => _("Kennerin"));
256        $titel[11] =    array(0 => _("K&ouml;nner"), 1 => _("K&ouml;nnerin"));
257        $titel[12] =    array(0 => _("Profi"), 1 => _("Profi"));
258        $titel[13] =    array(0 => _("Experte"), 1 => _("Expertin"));
259        $titel[14] =    array(0 => _("Meister"), 1 => _("Meisterin"));
260        $titel[15] =    array(0 => _("Gro&szlig;meister"), 1 => _("Gro&szlig;meisterin"));
261        $titel[16] =    array(0 => _("Idol"), 1 => _("Idol"));
262        $titel[17] =    array(0 => _("Guru"), 1 => _("Hohepriesterin"));
263        $titel[18] =    array(0 => _("Lichtgestalt"), 1 => _("Lichtgestalt"));
264        $titel[19] =    array(0 => _("Halbgott"), 1 => _("Halbg&ouml;ttin"));
265        $titel[20] =    array(0 => _("Gott"), 1 => _("G&ouml;ttin"));
266
267        return $titel[$logscore][$gender == 2 ? 1 : 0];
268    }
269
270    /**
271    * Retrieves the score for the current user
272    *
273    * @return       integer the score
274    *
275    */
276    function GetMyScore()
277    {
278        global $user, $auth;
279
280        $user_id=$user->id; //damit keiner schummelt...
281
282        // Werte holen...
283        $db=new DB_Seminar;
284        $db->query("SELECT count(*) as postings FROM px_topics WHERE user_id = '$user_id' ");
285        $db->next_record();
286        $postings=$db->f("postings");
287
288        $db->query("SELECT count(*) as dokumente FROM dokumente WHERE user_id = '$user_id' AND range_id <> 'provisional' ");
289        $db->next_record();
290        $dokumente=$db->f("dokumente");
291
292        $db->query("SELECT count(*) as seminare FROM seminar_user WHERE user_id = '$user_id' ");
293        $db->next_record();
294        $seminare=$db->f("seminare");
295
296        $db->query("SELECT count(*) as archiv FROM archiv_user WHERE user_id = '$user_id' ");
297        $db->next_record();
298        $archiv=$db->f("archiv");
299
300        $db->query("SELECT count(*) as institut FROM user_inst WHERE user_id = '$user_id' ");
301        $db->next_record();
302        $institut=$db->f("institut");
303
304        $db->query("SELECT count(*) as news FROM news WHERE user_id = '$user_id' ");
305        $db->next_record();
306        $news=$db->f("news");
307
308        $db->query("SELECT count(post_id) as guestcount FROM guestbook WHERE range_id = '$user_id' ");
309        $db->next_record();
310        $gaeste = $db->f("guestcount");
311
312        $db->query("SELECT count(contact_id) as contactcount FROM contact WHERE user_id = '$user_id' ");
313        $db->next_record();
314        $contact = $db->f("contactcount");
315
316        // TODO: Count only visible categories.
317        $db->query("SELECT count(kategorie_id) as katcount FROM kategorien WHERE range_id = '$user_id'");
318        $db->next_record();
319        $katcount = $db->f("katcount");
320        if ($katcount > 50) $katcount = 50;
321        $db->query("SELECT mkdate FROM user_info WHERE user_id = '$user_id' ");
322        $db->next_record();
323        $age = $db->f("mkdate");
324        if ($age == 0) $age = 1011275740;
325        $age = (time()-$age)/31536000;
326        $age = 2 + log($age);
327        if ($age <1 ) $age = 1;
328
329        if (get_config('VOTE_ENABLE')) {
330            $db->query("SELECT count(*) FROM vote WHERE range_id = '$user_id' AND state IN('active','stopvis')");
331            $db->next_record();
332            $vote = $db->f(0)*2;
333
334            $db->query("SELECT count(*) FROM vote_user WHERE user_id = '$user_id'");
335            $db->next_record();
336            $vote += $db->f(0);
337
338            $db->query("SELECT count( DISTINCT (vote_id) )
339                        FROM voteanswers_user
340                        LEFT JOIN voteanswers USING ( answer_id )
341                        WHERE user_id = '$user_id'
342                        GROUP BY user_id");
343            $db->next_record();
344            $vote += $db->f(0);
345
346            $db->query("SELECT count(*) FROM eval WHERE author_id = '$user_id' AND startdate < UNIX_TIMESTAMP( ) AND (stopdate > UNIX_TIMESTAMP( ) OR startdate + timespan > UNIX_TIMESTAMP( ) OR (stopdate IS NULL AND timespan IS NULL))");
347            $db->next_record();
348            $vote += 2*$db->f(0);
349
350            $db->query("SELECT count(*) FROM eval_user WHERE user_id = '$user_id'");
351            $db->next_record();
352            $vote += $db->f(0);
353        }
354
355        if (get_config('WIKI_ENABLE')) {
356            $db->query("SELECT count(*) FROM wiki WHERE user_id = '$user_id'");
357            $db->next_record();
358            $wiki = $db->f(0);
359        }
360
361        $visits = object_return_views($user_id);
362
363        $scoreplugins = PluginEngine::getPlugins('SystemPlugin') + PluginEngine::getPlugins('StandardPlugin');
364        $pluginscore = 0;
365        $pluginscount = 0;
366
367        foreach ($scoreplugins as $scoreplugin) {
368            if ($scoreplugin instanceof AbstractStudIPSystemPlugin ||
369                $scoreplugin instanceof AbstractStudIPStandardPlugin) {
370                $pluginscore += $scoreplugin->getScore();
371                $pluginscount++;
372            }
373        }
374        if ($pluginscount > 0) {
375            $pluginscore = round($pluginscore / $pluginscount);
376        }
377
378
379        // Die HOCHGEHEIME Formel:
380        $score = (5*$postings) + (5*$news) + (20*$dokumente) + (2*$institut) + (10*$archiv*$age) + (10*$contact) + (20*$katcount) + (5*$seminare) + (1*$gaeste) + (5*$vote) + (5*$wiki) + (3*$visits);
381        $score += $pluginscore;
382        $score = round($score/$age);
383
384        if (Avatar::getAvatar($user_id)->is_customized()) {
385            $score *=10;
386        }
387
388        //Schreiben des neuen Wertes
389        $query = "UPDATE user_info "
390            ." SET score = '$score'"
391            ." WHERE user_id = '$user_id' AND score > 0";
392        $db->query($query);
393        return $score;
394    }
395}
Note: See TracBrowser for help on using the repository browser.