From aefc0be7f5df5bd75bb9cd67c6190411ee558bc3 Mon Sep 17 00:00:00 2001 From: rootcoma Date: Mon, 9 Dec 2013 10:37:28 -0800 Subject: [PATCH] Adding recent activites, adding config for turning off logging --- config.py | 5 ++ php/controllers/IndexController.php | 26 +++++++--- php/includes/Storage.php | 12 +++++ php/views/home.html.php | 36 ++++++++++++-- plugin.py | 75 +++++++++++++++++++++++++++-- 5 files changed, 140 insertions(+), 14 deletions(-) diff --git a/config.py b/config.py index b6e55eb..8c2245d 100644 --- a/config.py +++ b/config.py @@ -49,6 +49,11 @@ conf.registerChannelValue(TriviaTime.commands, 'showHintCommandKAOS', """The command for showing the remaining KAOS""") ) +conf.registerChannelValue(TriviaTime.general, 'logGames', + registry.Boolean(True, + """Log changes to questions and games""") + ) + conf.registerChannelValue(TriviaTime.general, 'globalStats', registry.Boolean(False, """Stats are global across all channels""") diff --git a/php/controllers/IndexController.php b/php/controllers/IndexController.php index 0d7c9c1..7cffe23 100644 --- a/php/controllers/IndexController.php +++ b/php/controllers/IndexController.php @@ -5,21 +5,33 @@ class IndexController extends Controller $container = $this->container; $storage = $container->getStorage(); - $errors = array(); - $result = array(); + $errorsQ = array(); + $resultQ = array(); try { - $result = $storage->getRecentAskedQuestions(); + $resultQ = $storage->getRecentAskedQuestions(); } catch(StorageSchemaException $e) { - $errors[] = "Error: Database schema is not queryable"; + $errorsQ[] = "Error: Database schema is not queryable"; } catch(StorageConnectionException $e) { - $errors[] = "Error: Database is not available"; + $errorsQ[] = "Error: Database is not available"; + } + + $errorsA = array(); + $resultA = array(); + try { + $resultA = $storage->getRecentActivities(); + } catch(StorageSchemaException $e) { + $errorsA[] = "Error: Database schema is not queryable"; + } catch(StorageConnectionException $e) { + $errorsA[] = "Error: Database is not available"; } $storage->close(); $values = array(); - $values['result'] = $result; - $values['errors'] = $errors; + $values['resultQuestions'] = $resultQ; + $values['errorsQuestions'] = $errorsQ; + $values['resultActivities'] = $resultA; + $values['errorsActivities'] = $errorsA; $container->setTitle('Home'); diff --git a/php/includes/Storage.php b/php/includes/Storage.php index 6ada561..e007682 100644 --- a/php/includes/Storage.php +++ b/php/includes/Storage.php @@ -647,6 +647,18 @@ class Storage return $result; } + public function getRecentActivities() { + if(!$this->isConnected()) { + throw new StorageConnectionException(); + } + $q = $this->db->query('SELECT * FROM triviaactivity ORDER BY id DESC LIMIT 10'); + if($q === false) { + throw new StorageSchemaException(); + } + $result = $q->fetchAll(); + return $result; + } + public function getUserProfileInformation($usernameCanonical) { if(!$this->isConnected()) { throw new StorageConnectionException(); diff --git a/php/views/home.html.php b/php/views/home.html.php index 1e18a06..c236902 100644 --- a/php/views/home.html.php +++ b/php/views/home.html.php @@ -6,7 +6,7 @@

Latest questions asked

$error
"; } ?> @@ -16,17 +16,45 @@ Round # Channel Question - Question # + Question # '; echo '' . $res['round_num'] . ''; echo '' . $res['channel'] . ''; echo '' . $res['question'] . ''; - echo '' . $res['line_num'] . ''; + echo '' . $res['line_num'] . ''; + echo ''; + } + ?> + + + + +
+
+

Latest Activities

+ $error
"; + } + ?> + + + + + + + + + '; + echo ''; + echo ''; echo ''; } ?> diff --git a/plugin.py b/plugin.py index 4dba6d4..877868d 100644 --- a/plugin.py +++ b/plugin.py @@ -47,7 +47,7 @@ class TriviaTime(callbacks.Plugin): self.dbamends = {} #Formatted like this: : "; ;" (This IS valid SQL as long as we include the semicolons) #logger - self.logger = self.Logger() + self.logger = self.Logger(self) # connections dbLocation = self.registryValue('admin.sqlitedb') @@ -60,6 +60,8 @@ class TriviaTime(callbacks.Plugin): log.info('The database location did not exist, creating folder structure') os.makedirs(dbFolder) self.storage = self.Storage(dbLocation) + #self.storage.dropActivityTable() + self.storage.makeActivityTable() #self.storage.dropUserLogTable() self.storage.makeUserLogTable() #self.storage.dropGameTable() @@ -231,11 +233,21 @@ class TriviaTime(callbacks.Plugin): n = int(n / len(chars)) return ''.join(L) + def addActivity(self, activityType, activityText, channel, irc, storage=None): + if storage is None: + dbLocation = self.registryValue('admin.sqlitedb') + threadStorage = self.Storage(dbLocation) + else: + threadStorage = storage + threadStorage.removeOldActivity() + threadStorage.insertActivity(activityType, activityText, channel, irc.network) + def acceptdelete(self, irc, msg, arg, user, channel, num): """[] Accept a question deletion Channel is only necessary when editing from outside of the channel """ + channel = msg.args[0] dbLocation = self.registryValue('admin.sqlitedb') threadStorage = self.Storage(dbLocation) delete = self.storage.getDeleteById(num) @@ -251,6 +263,8 @@ class TriviaTime(callbacks.Plugin): threadStorage.removeDelete(num) threadStorage.removeDeleteByQuestionNumber(questionNumber) self.logger.doLog(irc, channel, "%s accepted delete# %i, question# %i deleted" % (msg.nick, num, questionNumber)) + activityText = '%s deleted a question, approved by %s' % (delete[1], msg.nick) + self.addActivity('delete', activityText, channel, irc, threadStorage) acceptdelete = wrap(acceptdelete, ['user', ('checkChannelCapability', 'triviamod'), 'int']) def acceptedit(self, irc, msg, arg, user, channel, num): @@ -258,6 +272,7 @@ class TriviaTime(callbacks.Plugin): Accept a question edit, and remove edit. Channel is only necessary when editing from outside of the channel """ + channel = msg.args[0] dbLocation = self.registryValue('admin.sqlitedb') threadStorage = self.Storage(dbLocation) edit = self.storage.getEditById(num) @@ -276,6 +291,8 @@ class TriviaTime(callbacks.Plugin): question = question[0] questionOld = question[2] self.logger.doLog(irc, channel, "%s accepted edit# %i, question# %i edited NEW: '%s' OLD '%s'" % (msg.nick, num, edit[1], edit[2], questionOld)) + activityText = '%s edited a question, approved by %s' % (edit[4], msg.nick) + self.addActivity('edit', activityText, channel, irc, threadStorage) irc.sendMsg(ircmsgs.notice(msg.nick, 'NEW: %s' % (edit[2]))) if questionOld != '': irc.sendMsg(ircmsgs.notice(msg.nick, 'OLD: %s' % (questionOld))) @@ -288,6 +305,7 @@ class TriviaTime(callbacks.Plugin): Accept a new question, and add it to the database. Channel is only necessary when editing from outside of the channel """ + channel = msg.args[0] dbLocation = self.registryValue('admin.sqlitedb') threadStorage = self.Storage(dbLocation) q = threadStorage.getTemporaryQuestionById(num) @@ -300,6 +318,8 @@ class TriviaTime(callbacks.Plugin): threadStorage.removeTemporaryQuestion(q[0]) irc.reply('Question accepted!') self.logger.doLog(irc, channel, "%s accepted new question# %i, '%s'" % (msg.nick, num, q[3])) + activityText = '%s added a new question, approved by %s' % (q[1], msg.nick) + self.addActivity('new', activityText, channel, irc, threadStorage) acceptnew = wrap(acceptnew, ['user', ('checkChannelCapability', 'triviamod'), 'int']) def addquestion(self, irc, msg, arg, question): @@ -450,7 +470,7 @@ class TriviaTime(callbacks.Plugin): return threadStorage.insertDelete(username, channel, id, reason) irc.reply('Question %d marked for deletion and pending review.' % id) - self.logger.doLog(irc, channel, "%s marked question# %i for deletion: '%s'" % (username, id, q[0][2])) + self.logger.doLog(irc, channel, "%s marked question# %i for deletion" % (username, id)) deletequestion = wrap(deletequestion, ["channel", optional(('literal',("question", "QUESTION", "ROUND", "round"))),'int', optional('text')]) def edit(self, irc, msg, arg, user, channel, num, question): @@ -2017,6 +2037,14 @@ class TriviaTime(callbacks.Plugin): self.conn.commit() c.close() + def dropActivityTable(self): + c = self.conn.cursor() + try: + c.execute('''DROP TABLE triviaactivity''') + except: + pass + c.close() + def dropDeleteTable(self): c = self.conn.cursor() try: @@ -2811,6 +2839,15 @@ class TriviaTime(callbacks.Plugin): return True return False + def insertActivity(self, aType, activity, channel, network, timestamp=None): + if timestamp is None: + timestamp = int(time.mktime(time.localtime())) + channelCanonical = ircutils.toLower(channel) + c = self.conn.cursor() + c.execute('insert into triviaactivity values (NULL, ?, ?, ?, ?, ?, ?)', + (aType, activity, channel, channelCanonical, network, timestamp)) + self.conn.commit() + def insertDelete(self, username, channel, lineNumber, reason): usernameCanonical = ircutils.toLower(username) channelCanonical = ircutils.toLower(channel) @@ -2956,6 +2993,23 @@ class TriviaTime(callbacks.Plugin): return True return False + def makeActivityTable(self): + c = self.conn.cursor() + try: + c.execute('''create table triviaactivity ( + id integer primary key autoincrement, + type text, + activity text, + channel text, + channel_canonical text, + network text, + timestamp integer + )''') + except: + pass + self.conn.commit() + c.close() + def makeDeleteTable(self): c = self.conn.cursor() try: @@ -3165,6 +3219,18 @@ class TriviaTime(callbacks.Plugin): return True return False + def removeOldActivity(self,count=100): + c = self.conn.cursor() + c.execute('''delete from triviaactivity + where id not in ( + select id + from triviaactivity + order by id desc + limit ? + )''', (count,)) + self.conn.commit() + c.close() + def removeDelete(self, deleteId): c = self.conn.cursor() c.execute('''delete from triviadelete @@ -3719,8 +3785,9 @@ class TriviaTime(callbacks.Plugin): #A log wrapper, ripoff of ChannelLogger class Logger: - def __init__(self): + def __init__(self, base): self.logs = {} + self.registryValue = base.registryValue def logNameTimestamp(self, channel): return time.strftime('%Y-%m-%d') @@ -3773,6 +3840,8 @@ class TriviaTime(callbacks.Plugin): return self.FakeLog() def doLog(self, irc, channel, s, *args): + if not self.registryValue('general.logGames'): + return s = format(s, *args) channel = self.normalizeChannel(irc, channel) log = self.getLog(irc, channel)