From 2c7f0d7ac83ff15fd905d5cb355fed64b78a4888 Mon Sep 17 00:00:00 2001
From: Brendan Andersen <brendan@omnifik.com>
Date: Fri, 4 Mar 2011 00:57:03 -0800
Subject: [PATCH] Issue #871116: userpoints_flag D7 port.

---
 userpoints_flag/userpoints_flag.info    |    5 +-
 userpoints_flag/userpoints_flag.install |   39 ++++++++------
 userpoints_flag/userpoints_flag.module  |   89 +++++++++++++++++--------------
 3 files changed, 73 insertions(+), 60 deletions(-)

diff --git a/userpoints_flag/userpoints_flag.info b/userpoints_flag/userpoints_flag.info
index ed098de..6a6cfd6 100644
--- a/userpoints_flag/userpoints_flag.info
+++ b/userpoints_flag/userpoints_flag.info
@@ -1,7 +1,6 @@
 name = User Points Flag
 description = Integrates User Points with flag API.
 package = Userpoints
-core = 6.x
+core = 7.x
 dependencies[] = userpoints
-dependencies[] = flag
-
+dependencies[] = flag
\ No newline at end of file
diff --git a/userpoints_flag/userpoints_flag.install b/userpoints_flag/userpoints_flag.install
index 86c2893..837061b 100644
--- a/userpoints_flag/userpoints_flag.install
+++ b/userpoints_flag/userpoints_flag.install
@@ -1,17 +1,24 @@
-<?php
-
-/**
-* Implementation of hook_install().
-*/
-function userpoints_flag_install() {
-  drupal_set_message(t('User Points Flag API has been successfully installed.'));
-  drupal_set_message(t('You can configure the User Points Flag API module on the <a href="@url">User Points settings page</a>.', array('@url' => url('admin/settings/userpoints'))));
-}
-
-
-/**
-* Implementation of hook_uninstall().
-*/
-function userpoints_flag_uninstall() {
-  db_query("DELETE FROM {variable} WHERE name like '%userpoints_flag%'");
+<?php
+
+/**
+ * @file
+ * Install and uninstall functions for the userpoints_flag module.
+ */
+
+/**
+ * Implements hook_install().
+ */
+function userpoints_flag_install() {
+  drupal_set_message(st('User Points Flag API has been successfully installed.'));
+  drupal_set_message(st('You can configure the User Points Flag API module on the <a href="@url">User Points settings page</a>.', array('@url' => url('admin/config/people/userpoints/settings'))));
+}
+
+
+/**
+ * Implements hook_uninstall().
+ */
+function userpoints_flag_uninstall() {
+  db_delete('variable')
+    ->condition('name', '%userpoints_flag%', 'LIKE')
+    ->execute();
 }
\ No newline at end of file
diff --git a/userpoints_flag/userpoints_flag.module b/userpoints_flag/userpoints_flag.module
index 27d64a0..e265767 100644
--- a/userpoints_flag/userpoints_flag.module
+++ b/userpoints_flag/userpoints_flag.module
@@ -1,7 +1,12 @@
 <?php
 
 /**
- * Implementation of hook_userpoints().
+ * @file
+ * Update user points when items are flagged or unflagged.
+ */
+
+/**
+ * Implements hook_userpoints().
  */
 function userpoints_flag_userpoints($op, $params = array()) {
   switch ($op) {
@@ -17,42 +22,42 @@ function userpoints_flag_userpoints($op, $params = array()) {
     // Load the category list.
     $category_options = userpoints_get_categories();
 
-    foreach(flag_get_flags() as $flag) {
-      $form['flag']['userpoints_flag_flagger_'. $flag->name] = array(
+    foreach (flag_get_flags() as $flag) {
+      $form['flag']['userpoints_flag_flagger_' . $flag->name] = array(
         '#type' => 'textfield',
-        '#default_value' => variable_get('userpoints_flag_flagger_'. $flag->name, 0),
+        '#default_value' => variable_get('userpoints_flag_flagger_' . $flag->name, 0),
         '#size' => 5,
         '#maxlength' => 5,
       );
 
-      $form['flag']['userpoints_flag_flagger_category_'. $flag->name] = array(
+      $form['flag']['userpoints_flag_flagger_category_' . $flag->name] = array(
         '#type' => 'select',
         '#options' => $category_options,
-        '#default_value' => variable_get('userpoints_flag_flagger_category_'. $flag->name, 0),
+        '#default_value' => variable_get('userpoints_flag_flagger_category_' . $flag->name, 0),
       );
 
-      $form['flag']['userpoints_flag_author_'. $flag->name] = array(
+      $form['flag']['userpoints_flag_author_' . $flag->name] = array(
         '#type' => 'textfield',
-        '#default_value' => variable_get('userpoints_flag_author_'. $flag->name, 0),
+        '#default_value' => variable_get('userpoints_flag_author_' . $flag->name, 0),
         '#size' => 5,
         '#maxlength' => 5,
       );
 
-      $form['flag']['userpoints_flag_author_category_'. $flag->name] = array(
+      $form['flag']['userpoints_flag_author_category_' . $flag->name] = array(
         '#type' => 'select',
         '#options' => $category_options,
-        '#default_value' => variable_get('userpoints_flag_author_category_'. $flag->name, 0),
+        '#default_value' => variable_get('userpoints_flag_author_category_' . $flag->name, 0),
       );
 
-      $form['flag']['userpoints_flag_daily_limit_'. $flag->name] = array(
+      $form['flag']['userpoints_flag_daily_limit_' . $flag->name] = array(
         '#type' => 'textfield',
-        '#default_value' => variable_get('userpoints_flag_daily_limit_'. $flag->name, 0),
+        '#default_value' => variable_get('userpoints_flag_daily_limit_' . $flag->name, 0),
         '#size' => 5,
       );
 
-      $form['flag']['userpoints_flag_moderate_'. $flag->name] = array(
+      $form['flag']['userpoints_flag_moderate_' . $flag->name] = array(
         '#type' => 'checkbox',
-        '#default_value' => variable_get('userpoints_flag_moderate_'. $flag->name, 0),
+        '#default_value' => variable_get('userpoints_flag_moderate_' . $flag->name, 0),
       );
     }
 
@@ -65,7 +70,7 @@ function userpoints_flag_userpoints($op, $params = array()) {
     );
 
     $form['flag']['userpoints_flag_help']['info'] = array(
-      '#value' => '
+      '#markup' => '
       <dl><dt>Flagger Points</dt><dd>These points are given to the user who is doing the flagging.</dd></dl>
       <dl><dt>Flagger Category</dt><dd>This is the category to which "Flagger Points" are added.</dd></dl>
       <dl><dt>Author/User Points</dt><dd>These points are given to the user who is either the author of the node or comment being flagged, or the user that is being flagged, depending on the flag type.</dd></dl>
@@ -79,28 +84,30 @@ function userpoints_flag_userpoints($op, $params = array()) {
 }
 
 /**
- * Implementation of hook_theme().
+ * Implements hook_theme().
  */
 function userpoints_flag_theme() {
   return array(
-    'userpoints_flag_settings' => array('form' => NULL)
+    'userpoints_flag_settings' => array('render element' => 'form')
   );
 }
 
 /**
  * Theme the userpoints flag settings form.
  */
-function theme_userpoints_flag_settings($form) {
+function theme_userpoints_flag_settings($variables) {
+  $form = $variables['form'];
+
   $rows = array();
   foreach (flag_get_flags() as $flag) {
     $row = array(
       $flag->title,
-      drupal_render($form['userpoints_flag_flagger_'. $flag->name]),
-      drupal_render($form['userpoints_flag_flagger_category_'. $flag->name]),
-      drupal_render($form['userpoints_flag_author_'. $flag->name]),
-      drupal_render($form['userpoints_flag_author_category_'. $flag->name]),
-      drupal_render($form['userpoints_flag_daily_limit_'. $flag->name]),
-      drupal_render($form['userpoints_flag_moderate_'. $flag->name]),
+      drupal_render($form['userpoints_flag_flagger_' . $flag->name]),
+      drupal_render($form['userpoints_flag_flagger_category_' . $flag->name]),
+      drupal_render($form['userpoints_flag_author_' . $flag->name]),
+      drupal_render($form['userpoints_flag_author_category_' . $flag->name]),
+      drupal_render($form['userpoints_flag_daily_limit_' . $flag->name]),
+      drupal_render($form['userpoints_flag_moderate_' . $flag->name]),
     );
     $rows[] = $row;
   }
@@ -115,18 +122,18 @@ function theme_userpoints_flag_settings($form) {
     t('Moderate')
   );
 
-  $output = theme('table', $header, $rows);
-  $output .= drupal_render($form);
+  $output = theme('table', array('header' => $header, 'rows' => $rows));
+  $output .= drupal_render_children($form);
 
   return $output;
 }
 
 /**
- * Implementation of hook_flag().
+ * Implements hook_flag().
  */
 function userpoints_flag_flag($action, $flag, $content_id, $account) {
-  $flagger_points = variable_get('userpoints_flag_flagger_'. $flag->name, 0);
-  $author_points = variable_get('userpoints_flag_author_'. $flag->name, 0);
+  $flagger_points = variable_get('userpoints_flag_flagger_' . $flag->name, 0);
+  $author_points = variable_get('userpoints_flag_author_' . $flag->name, 0);
 
   // Only do something if points have been assigned to this flag type.
   if ($flagger_points != 0 || $author_points != 0) {
@@ -148,20 +155,20 @@ function userpoints_flag_flag($action, $flag, $content_id, $account) {
     if ($author_uid != $account->uid) {
 
       // Don't give points to the flagger or author if the flagger has exceeded the daily flagging limit for this flag type.
-      $flag_limit = variable_get('userpoints_flag_daily_limit_'. $flag->name, 0);
+      $flag_limit = variable_get('userpoints_flag_daily_limit_' . $flag->name, 0);
       if (($flag_limit == 0) || (_userpoints_flag_within_limit($account->uid, $flag->fid, $flag_limit))) {
 
         // Give points to the flagger.
         if ($flagger_points != 0) {
           userpoints_userpointsapi(array(
             'uid' => $account->uid,
-            'tid' => variable_get('userpoints_flag_flagger_category_'. $flag->name, 0),
+            'tid' => variable_get('userpoints_flag_flagger_category_' . $flag->name, 0),
             'points' => ($action == 'flag' ? $flagger_points : -1 * $flagger_points),
-            'moderate' => variable_get('userpoints_flag_moderate_'. $flag->name, 0),
+            'moderate' => variable_get('userpoints_flag_moderate_' . $flag->name, 0),
             'entity_id' => $content_id,
             'entity_type' => $flag->content_type,
-            'operation' => 'Flag '. $flag->title,
-            'description' => t('Flag: @content_type @content_id.',array('@content_type'=> $flag->content_type, '@content_id'=> $content_id)),
+            'operation' => 'Flag ' . $flag->title,
+            'description' => t('Flag: @content_type @content_id.', array('@content_type' => $flag->content_type, '@content_id' => $content_id)),
           ));
         }
 
@@ -169,13 +176,13 @@ function userpoints_flag_flag($action, $flag, $content_id, $account) {
         if (($author_points != 0) && ($author_uid != 0)) {
           userpoints_userpointsapi(array(
             'uid' => $author_uid,
-            'tid' => variable_get('userpoints_flag_author_category_'. $flag->name, 0),
+            'tid' => variable_get('userpoints_flag_author_category_' . $flag->name, 0),
             'points' => ($action == 'flag' ? $author_points : -1 * $author_points),
-            'moderate' => variable_get('userpoints_flag_moderate_'. $flag->name, 0),
+            'moderate' => variable_get('userpoints_flag_moderate_' . $flag->name, 0),
             'entity_id' => $content_id,
             'entity_type' => $flag->content_type,
-            'operation' => 'Flag Author '. $flag->title,
-            'description' => t('Flag author: @content_type @content_id.',array('@content_type'=> $flag->content_type, '@content_id'=> $content_id)),
+            'operation' => 'Flag Author ' . $flag->title,
+            'description' => t('Flag author: @content_type @content_id.', array('@content_type' => $flag->content_type, '@content_id' => $content_id)),
           ));
         }
       }
@@ -187,20 +194,20 @@ function userpoints_flag_flag($action, $flag, $content_id, $account) {
  * Get the user id of the author of a node.
  */
 function _userpoints_flag_get_node_author($nid) {
-  return db_result(db_query_range("SELECT uid FROM {node} WHERE nid = %d", $nid, 0, 1));
+  return db_query_range("SELECT uid FROM {node} WHERE nid = :nid", 0, 1, array(':nid' => $nid))->fetchField();
 }
 
 /**
  * Get the user id of the author of a comment.
  */
 function _userpoints_flag_get_comment_author($cid) {
-  return db_result(db_query("SELECT uid FROM {comments} WHERE cid = %d", $cid));
+  return db_query("SELECT uid FROM {comments} WHERE cid = :cid", array(':cid' => $cid))->fetchField();
 }
 
 /**
  * Find out whether the flagger has flagged a particular flag type more than a set number of times in the last 24 hours.
  */
 function _userpoints_flag_within_limit($uid, $flag_id, $flag_limit) {
-  $flag_count = db_result(db_query("SELECT COUNT(*) FROM {flag_content} WHERE uid = %d AND fid = %d AND timestamp > %d", $uid, $flag_id, time() - 86400));
+  $flag_count = db_query("SELECT COUNT(*) FROM {flag_content} WHERE uid = :uid AND fid = :fid AND timestamp > :time", array(':uid' => $uid, ':fid' => $flag_id, ':time' => time() - 86400))->fetchField();
   return ($flag_count <= $flag_limit ? TRUE : FALSE);
 }
-- 
1.7.1

