@@ -38,100 +38,146 @@ class Anficker
3838 /**
3939 * Anfick des User hinzufügen
4040 *
41- * @author ?
42- * @author IneX
43- * @version 2.1
41+ * @version 2.5
4442 * @since 1.0 function added
4543 * @since 2.0 `IneX` code enhancements
4644 * @since 2.1 `16.04.2020` `IneX` migrated mysql-functions to mysqli
45+ * @since 2.5 `21.01.2024` `IneX` Bug #667 : Anficks werden x-mal gespeichert
4746 *
48- * @see Anficker ::logAnfick(), Anficker ::getId()
47+ * @see self ::logAnfick(), self ::getId()
4948 * @param integer $user_id ID des Users, welcher gerade mit Spresim batteld
5049 * @param string $text Anfick des Users
5150 * @param boolean $spresim_trainieren Gibt an, ob Anfick des Users gespeichert werden soll oder nicht
5251 * @global object $db Globales Class-Object mit allen MySQL-Methoden
53- * @global object $user Globales Class-Object mit den User-Methoden & Variablen
52+ * @return bool
5453 *
55- * @todo Unterschied, ob Spresim trainieren oder nur battlen sollte möglich sein (Bug #487) (Mättä, 25.10.04) | IDEE: Eine möglich Lösung wäre, ein zusätzliches Flag in der Tabelle "battle_only" oder so...
56- * @todo Müsste es nicht "REPLACE INTO..." sein?? Jetzt werden x-Einträge mit gleichem Text gemacht! (IneX, 8.6.09)
54+ * // TODO Bug #487 : Unterschied, ob Spresim trainieren oder nur battlen sollte möglich sein (Mättä, 25.10.04) | IDEE: Eine möglich Lösung wäre, ein zusätzliches Flag in der Tabelle "battle_only" oder so...
5755 */
58- static function addAnfick ($ user_id , $ text , $ spresim_trainieren =FALSE ) {
59- global $ db , $ user ;
60-
61- // nur Anfick speichern, wenn Spresim trainiert werden soll:
62- //if ($spresim_trainieren == TRUE)
63- //{
64- if ($ text != '' && !empty ($ user_id ))//$user->id2user($user_id)))
65- {
66- $ sql = 'INSERT IGNORE INTO anficker_anficks ( text, user_id, datum) VALUES (?, ?, ?) ' ;
67- $ insert_id = $ db ->query ($ sql , __FILE__ , __LINE__ , __METHOD__ , [$ text , $ user_id , timestamp (true )]);
68- }
69- //else
70- //{
71-
72-
73- // Hier sollte was kommen, WENN SPRESIM NICHT TRAINIERT WERDEN SOLL... leider zur Zeit nicht realisierbar, IneX 8.6.09
74- // IDEE: Eine möglich Lösung wäre, ein zusätzliches Flag in der Tabelle "battle_only" oder so... (IneX, 8.6.09)
75-
56+ static function addAnfick ($ user_id , $ text , $ spresim_trainieren =FALSE )
57+ {
58+ global $ db ;
7659
60+ if ($ user_id > 0 && !empty ($ text ))
61+ {
62+ // nur Anfick speichern, wenn Spresim trainiert werden soll:
63+ //if ($spresim_trainieren == TRUE)
64+ //{
65+ /** Check if Anfick already exists */
66+ $ existing_anfick_id = 0 ;
67+ $ default_initial_score = 4.0 ;
68+ $ sql_check = 'SELECT id, note, votes, user_id FROM anficker_anficks WHERE text=? LIMIT 1 ' ;
69+ $ exists = $ db ->query ($ sql_check , __FILE__ , __LINE__ , __METHOD__ , [$ text ]);
70+ if ($ db ->num ($ exists ) > 0 ) {
71+ $ af = $ db ->fetch ($ exists );
72+ $ existing_anfick_id = intval ($ af ['id ' ]);
73+ $ existing_anfick_score = floatval ($ af ['note ' ]);
74+ $ existing_anfick_votes = intval ($ af ['votes ' ]);
75+ $ existing_anfick_creator = intval ($ af ['user_id ' ]);
76+ }
77+
78+ /** Update existing Anfick */
79+ if ($ existing_anfick_id > 0 )
80+ {
81+ $ new_votes = $ existing_anfick_votes +1 ;
82+ $ new_score = number_format (($ default_initial_score +$ existing_anfick_score )*$ existing_anfick_votes /$ new_votes , 8 , '. ' , '' );
83+ $ update = [
84+ 'note ' => $ new_score
85+ ,'votes ' => $ new_votes
86+ ,'user_id ' => (empty ($ existing_anfick_creator ) ? $ user_id : $ existing_anfick_creator )
87+ ];
88+ $ db ->update ('anficker_anficks ' , $ existing_anfick_id , $ update , __FILE__ , __LINE__ , __METHOD__ );
89+ }
90+ /** Add new Anfick */
91+ else {
92+ $ insert = [
93+ 'text ' => $ text
94+ ,'user_id ' => $ user_id
95+ ,'datum ' => timestamp (true )
96+ ];
97+ $ insert_id = $ db ->insert ('anficker_anficks ' , $ insert , __FILE__ , __LINE__ , __METHOD__ );
98+ }
99+
100+ //else
101+ //{
102+
103+ // Hier sollte was kommen, WENN SPRESIM NICHT TRAINIERT WERDEN SOLL... leider zur Zeit nicht realisierbar, IneX 8.6.09
104+ // IDEE: Eine möglich Lösung wäre, ein zusätzliches Flag in der Tabelle "battle_only" oder so... (IneX, 8.6.09)
105+
106+ //}
77107 //}
78- //}
79-
80- // DEBUGGING
81- //error_log('[DEBUG] ' . __FILE__ . ':' . __LINE__ . ' mysql_insert_id() = ' . mysql_insert_id());
82- //error_log('[DEBUG] ' . __FILE__ . ':' . __LINE__ . ' Anficker::getId($text) = `' . $text . '`');
83-
84- $ anfick_id = ($ insert_id > 0 ? $ insert_id : Anficker::getId ($ text ));
85-
86- Anficker::logAnfick ($ anfick_id , $ user_id , $ user_id );
108+ $ anfick_id = ($ existing_anfick_id > 0 ? $ existing_anfick_id : $ insert_id );
109+ $ log = self ::logAnfick ($ anfick_id , $ user_id , $ user_id );
110+ return (!$ log ? false : true );
111+ }
112+ return false ;
87113 }
88114
89115
90116 /**
91117 * Anfick im Anfick-Log ergänzen
92118 *
93- * @author ?
94- * @author IneX
119+ * @see self::addAnfick()
120+ *
95121 * @version 2.0
96- * @since 1.0
97- * @see Anficker::addAnfick()
122+ * @since 1.0 Method added
123+ * @since 2.0 `IneX` SQL- and code optimziations
98124 *
99125 * @param integer $anfick_id ID des Anficks wo das Log ergänzt werden soll
100126 * @param integer $user_id ID des Users, welcher angefickt wurde
101127 * @param integer $anficker_id ID des Users, welcher den Anfick gemacht hat
102128 * @global object $db Globales Class-Object mit allen MySQL-Methoden
129+ * @return int|bool Returns INSERT id - or false
103130 */
104131 static function logAnfick ($ anfick_id , $ user_id , $ anficker_id ) {
105132 global $ db ;
106- $ sql = 'INSERT INTO anficker_log (datum, user_id, anficker_id, anfick_id) VALUES (?, ?, ?, ?) ' ;
107- //return $db->query($sql, __FILE__, __LINE__);
108- $ db ->query ($ sql , __FILE__ , __LINE__ , __METHOD__ , [timestamp (true ), $ user_id , $ anficker_id , $ anfick_id ]);
133+
134+ $ result = false ;
135+ if ($ anfick_id > 0 && $ user_id > 0 && $ anficker_id > 0 ) {
136+ $ insert = [
137+ 'datum ' => timestamp (true )
138+ ,'user_id ' => intval ($ user_id )
139+ ,'anficker_id ' => intval ($ anficker_id )
140+ ,'anfick_id ' => intval ($ anfick_id )
141+ ];
142+ $ result = $ db ->insert ('anficker_log ' , $ insert , __FILE__ , __LINE__ , __METHOD__ );
143+ }
144+ return $ result ;
109145 }
110146
111147
112148 static function deleteLog ($ user_id ) {
113149 global $ db ;
114- $ sql = 'DELETE FROM anficker_log WHERE user_id=? ' ;
115- return $ db ->query ($ sql , __FILE__ , __LINE__ , __METHOD__ , [$ user_id ]);
150+
151+ $ result = false ;
152+ if ($ user_id > 0 ) {
153+ $ sql = 'DELETE FROM anficker_log WHERE user_id=? ' ;
154+ $ result = $ db ->query ($ sql , __FILE__ , __LINE__ , __METHOD__ , [$ user_id ]);
155+ }
156+ return $ result ;
116157 }
117158
118159
119160 static function getId ($ text ) {
120161 global $ db ;
121- $ sql = 'SELECT id FROM anficker_anficks WHERE text=? ' ;
122- $ rs = $ db ->fetch ($ db ->query ($ sql , __FILE__ , __LINE__ , __METHOD__ , [$ text ]));
123- return $ rs ['id ' ];
162+
163+ $ result = null ;
164+ if (!empty ($ text )) {
165+ $ sql = 'SELECT id FROM anficker_anficks WHERE text=? ' ;
166+ $ rs = $ db ->fetch ($ db ->query ($ sql , __FILE__ , __LINE__ , __METHOD__ , [$ text ]));
167+ $ result = intval ($ rs ['id ' ]);
168+ }
169+ return $ result ;
124170 }
125171
126172
127173 /**
128174 * Anfick-Log ausgeben
129175 *
130- * @author ?
131- * @author IneX
176+ * @see self::logAnfick(), self::anfickenMit()
177+ *
132178 * @version 2.0
133- * @since 1.0
134- * @see Anficker::logAnfick(), Anficker::anfickenMit()
179+ * @since 1.0 method added
180+ * @since 2.0 `IneX` SQL- and code optimizations
135181 *
136182 * @param integer $user_id ID des Users, welcher gerade mit Spresim batteld
137183 * @global object $db Globales Class-Object mit allen MySQL-Methoden
@@ -140,8 +186,8 @@ static function getId($text) {
140186 static function getLog ($ user_id ) {
141187 global $ db ;
142188
143- //Anficker ::addRandomAnfick2Log($user_id, ANFICKER_USER_ID);
144- Anficker ::logAnfick (Anficker ::anfickenMit (), $ user_id , ANFICKER_USER_ID );
189+ //self ::addRandomAnfick2Log($user_id, ANFICKER_USER_ID);
190+ self ::logAnfick (self ::anfickenMit (), $ user_id , ANFICKER_USER_ID );
145191
146192 $ sql = 'SELECT anficker_anficks.note, anficker_anficks.id, anficker_log.datum, anficker_anficks.text, anficker_log.anficker_id FROM anficker_log
147193 LEFT JOIN anficker_anficks ON (anficker_anficks.id = anficker_log.anfick_id) WHERE anficker_log.user_id=? ORDER BY anficker_log.id ASC ' ;
@@ -182,11 +228,11 @@ static function getNumAnficks($user_id=null)
182228 *
183229 * @deprecated
184230 *
185- * @author ?
186- * @author IneX
231+ * @see self::anfickenMit()
232+ *
187233 * @version 2.0
188- * @since 1.0
189- * @see Anficker::anfickenMit()
234+ * @since 1.0 method added
235+ * @since 2.0 `IneX` method deprecation mode activated
190236 *
191237 * @param integer $user_id ID des Users, welcher gerade mit Spresim batteld
192238 * @return array Gibt ganzes Log der Anfickerei für Ausgabe zurück
@@ -205,7 +251,7 @@ static function addRandomAnfick2Log($user_id) {
205251 $rs = $db->fetch($result);*/
206252
207253 //return Anficker::logAnfick($rs['id'], $user_id, ANFICKER_USER_ID);
208- Anficker ::logAnfick (Anficker ::anfickenMit (), $ user_id , ANFICKER_USER_ID );
254+ self ::logAnfick (self ::anfickenMit (), $ user_id , ANFICKER_USER_ID );
209255 }
210256
211257
@@ -217,7 +263,7 @@ static function addRandomAnfick2Log($user_id) {
217263 * @since 1.0 `08.06.2009` `IneX` function added
218264 * @since 1.1 `16.04.2020` `IneX` code optimizations, migrated mysql-functions to mysqli
219265 *
220- * @see Anficker ::getNumAnficks()
266+ * @see self ::getNumAnficks()
221267 * @global object $db Globales Class-Object mit allen MySQL-Methoden
222268 * @return integer ID des Anfick von Spresim
223269 */
@@ -226,10 +272,10 @@ static function anfickenMit()
226272 global $ db ;
227273
228274 $ sql = 'SELECT * FROM anficker_anficks ORDER BY note ASC ' ;
229- $ result = $ db ->query ($ sql , __FILE__ , __LINE__ , __FUNCTION__ );
275+ $ result = $ db ->query ($ sql , __FILE__ , __LINE__ , __METHOD__ );
230276
231277 /** zufällige id holen */
232- $ rs = Anficker ::getNumAnficks ();
278+ $ rs = self ::getNumAnficks ();
233279 $ id = rand (0 , $ rs ['num ' ]-1 ); // Zufalls #
234280 $ id = rand ($ id , $ rs ['num ' ]-1 ); // die besten bevorzugen.
235281 mysqli_data_seek ($ result , $ id );
@@ -243,21 +289,25 @@ static function anfickenMit()
243289 /**
244290 * Anfick-Spruch benoten
245291 *
246- * @author ?
247- * @author IneX
248292 * @version 1.1
249- * @since 1.0
293+ * @since 1.0 method added
294+ * @since 1.1 `IneX` SQL- and code optimizations
250295 *
251296 * @param integer $anfick_id ID des benoteten Anficks
252297 * @param integer $note Bewertung des Anficks
253298 * @global object $db Globales Class-Object mit allen MySQL-Methoden
299+ * @return integer
254300 */
255- static function vote ($ anfick_id , $ note ) {
301+ static function vote ($ anfick_id , $ note )
302+ {
256303 global $ db ;
257- $ sql = 'UPDATE anficker_anficks SET note=((?+note*votes)/(votes+1)), votes=(votes+1) WHERE id=? '
258- ;
259- //return $db->query($sql, __FILE__, __LINE__);
260- $ db ->query ($ sql , __FILE__ , __LINE__ , __METHOD__ , [$ note , $ anfick_id ]);
261- }
262304
305+ $ result = 0 ;
306+ if ($ anfick_id > 0 && $ note > 0 )
307+ {
308+ $ sql = 'UPDATE anficker_anficks SET note=((?+note)*votes/(votes+1)), votes=(votes+1) WHERE id=? ' ;
309+ $ result = $ db ->query ($ sql , __FILE__ , __LINE__ , __METHOD__ , [intval ($ note ), intval ($ anfick_id )]);
310+ }
311+ return $ result ;
312+ }
263313}
0 commit comments