From dd6c4048b2b0bace7e0415a589174139de993092 Mon Sep 17 00:00:00 2001
From: John Romine <jromine@uci.edu>
Date: Wed, 16 Mar 2011 18:14:45 -0700
Subject: [PATCH] Patches for http://drupal.org/node/577362

---
 role_help.install |   27 ++++++++-
 role_help.module  |  163 ++++++++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 160 insertions(+), 30 deletions(-)

diff --git a/role_help.install b/role_help.install
index 7087b71..44a8d8b 100644
--- role_help.install
+++ role_help.install
@@ -23,9 +23,15 @@ function role_help_schema() {
         'description' => 'Description text for the role.',
         'type' => 'text',
         'size' => 'big',
-        'not null' => TRUE,
+        'not null' => FALSE,
         'default' => '',
       ),
+      'summary' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => FALSE,
+        'description' => t('Summary details about a role.'),
+      ),
     ),
     'primary key' => array('rid'),
   );
@@ -51,6 +57,23 @@ function role_help_install() {
 function role_help_uninstall() {
   // Remove tables.
   drupal_uninstall_schema('role_help');
-
+  variable_del('role_help_summary');
 }
 
+function role_help_update_6001(&$sandbox) {
+  $ret = array();
+  db_change_field($ret, 'role_help', 'description', 'description', array(
+    'description' => 'Description text for the role.',
+    'type' => 'text',
+    'size' => 'big',
+    'not null' => FALSE, // changed
+    'default' => '',
+  ));
+  db_add_field($ret, 'role_help', 'summary', array(
+    'type' => 'varchar',
+    'length' => '255',
+    'not null' => FALSE,
+    'description' => t('Summary details about a role.'),
+  ));
+  return $ret;
+}
diff --git a/role_help.module b/role_help.module
index e405653..83f21a1 100644
--- role_help.module
+++ role_help.module
@@ -42,7 +42,8 @@ function role_help_help($path='', $arg) {
       $output .= '<p>'. t("Set description text for a role on its settings pages.") .'</p>';
       $output .= t('<p>You can</p>
 <ul>
-<li>set a description for each role on its <a href="@admin-roles-page">settings page</a>.</li>
+<li>set a summary and description for each role on its <a href="@admin-roles-page">settings page</a>.</li>
+<li>show the summary on the <a href="@admin-roles-page">roles list</a>, and user profile edit pages.</li>
 <li>set descriptions for the anonymous and authenticated roles on the <a href="@admin-role-help-page">role help settings page</a>.</li>
 <li>set the input format for all role help text on the <a href="@admin-role-help-page">role help settings page</a>.</li></ul>',
         array(
@@ -102,21 +103,31 @@ function _role_help_menu_access() {
 function role_help_form_user_admin_role_alter(&$form, &$form_state) {
   // role edit form
   // add a text area for the role's help text
-  $result = db_query('SELECT description FROM {role_help} WHERE rid = %d', $form['rid']['#value']);
-  $description = db_result($result);
+  if (isset($form['rid']['#value'])) {
+    $role_help = db_fetch_object(db_query('SELECT summary, description FROM {role_help} WHERE rid = %d', $form['rid']['#value']));
+  }
 
+  $form['role_summary'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Summary'),
+    '#size' => 80,
+    '#maxlength' => 255,
+    '#default_value' => isset($role_help->summary) ? $role_help->summary : '',
+    '#description' => t('Summary of what permissions are assigned to this role, and how the role is used.  This will be shown on the role list and user profile edit pages.'),
+    '#weight' => -6,
+    '#access' => variable_get('role_help_summary', 1),
+  );
   $form['role_help'] = array(
     '#type' => 'textarea',
     '#title' => t('Help text'),
-    '#default_value' => $description,
-    '#description' => t('A description of what a user with this role can accomplish. This will be shown to users on the site help page.'),
+    '#default_value' => isset($role_help->description) ? $role_help->description : '',
+    '#description' => t('A more detailed description of what a user with this role can accomplish. This will be shown to users on the site help page.'),
     '#weight' => -5,
   ); 
   $form['name']['#weight'] = -10;
   $form['#submit'][] = 'role_help_user_admin_role_form_submit';   
 } // function role_help_form_user_admin_role_alter
 
-
 /**
  * Custom submit function for user_admin_role form
  * submit form: save or update text, delete with role
@@ -129,34 +140,71 @@ function role_help_user_admin_role_form_submit($form, &$form_state) {
    buttons which need to invoke different validate or submit functionality
    should have button-specific functions. */
   if ($form_state['values']['op'] == t('Save role')) {
-    if ($form_state['values']['role_help'] != '') {
-      // text set: update or insert an entry
-      $result = db_query("SELECT description from {role_help} WHERE rid = %d", $form_state['values']['rid']);
-      if ($num_rows) {
-        db_query("UPDATE {role_help} SET description = '%s' WHERE rid = %d", $form_state['values']['role_help'], $form_state['values']['rid']);
-      }
-      else {
-        db_query("INSERT INTO {role_help} (rid, description) VALUES (%d, '%s')", $form_state['values']['rid'], $form_state['values']['role_help']);    
-      }         
+    if (isset($form_state['values']['rid'])) {
+      db_query('DELETE FROM {role_help} WHERE rid = %d', $form_state['values']['rid']);
+      db_query("INSERT INTO {role_help} (rid, summary, description) VALUES (%d, '%s', '%s')",
+        $form_state['values']['rid'], $form_state['values']['role_summary'], $form_state['values']['role_help']);
       drupal_set_message(t('The role help text has been saved.'));
     }
-    else {
-      // text blanked: delete entry
-      db_query('DELETE FROM {role_help} WHERE rid = %d', $form_state['values']['rid']);    
+  }
+  elseif ($form_state['values']['op'] == t('Add role')) {
+    // add role "long form" admin/user/roles/edit
+    if (isset($form_state['values']['role_help'])) {
+      $rid = db_last_insert_id('role', 'rid');
+      db_query("INSERT INTO {role_help} (rid, summary, description) VALUES (%d, '%s', '%s')", 
+        $rid, $form_state['values']['role_summary'], $form_state['values']['role_help']);
+      // allow edit of summary, description after quick add
+      $form_state['redirect'] = 'admin/user/roles/edit'. $rid;
+    }
+  }
+  elseif ($form_state['values']['op'] == t('Delete role')) {
+    if (isset($form_state['values']['rid'])) {
+      db_query('DELETE FROM {role_help} WHERE rid = %d', $form_state['values']['rid']);
     }
-  } 
-/* TODO The 'op' element in the form values is deprecated.
-   Each button can have #validate and #submit functions associated with it.
-   Thus, there should be one button that submits the form and which invokes
-   the normal form_id_validate and form_id_submit handlers. Any additional
-   buttons which need to invoke different validate or submit functionality
-   should have button-specific functions. */
-  else if ($form_state['values']['op'] == t('Delete role')) {
-    // role deleted: delete entry
-    db_query('DELETE FROM {role_help} WHERE rid = %d', $form_state['values']['rid']);
   }
 } // function role_help_user_admin_role_form_submit
 
+/**
+ * Implementation of hook_form_FORM_ID_alter().
+ */
+function role_help_form_user_admin_new_role_alter(&$form, &$form_state) {
+  $form['#submit'][] = 'role_help_user_admin_new_role_form_submit';   
+}
+
+/**
+ * Custom submit function for user_admin_new_role form;
+ * redirect to detailed edit page.
+ */
+function role_help_user_admin_new_role_form_submit($form, &$form_state) {
+  $rid = db_last_insert_id('role', 'rid');
+  // prompt for edit of summary, description after quick add
+  if ($rid) {
+    drupal_set_message(t('Please edit the role summary and help text.'));
+    $form_state['redirect'] = 'admin/user/roles/edit/'. $rid;
+  }
+}
+
+/**
+ * Implementation of hook_form_FORM_ID_alter().
+ *
+ * Add role summary to user profile edit form.
+ */
+function role_help_form_user_profile_form_alter(&$form, &$form_state) {
+  if (isset($form['account']['roles'])) {
+    $query = db_query("SELECT rid, summary FROM {role_help}");
+    $summary = array();
+    while ($data = db_fetch_object($query)) {
+      $summary[$data->rid] = $data->summary;
+    }
+    // add role summary to form #options
+    foreach ($form['account']['roles']['#options'] as $rid => $name) {
+      if (isset($summary[$rid])) {
+        $form['account']['roles']['#options'][$rid] .= ' ('. theme('placeholder', $summary[$rid]) .')';
+      }
+    }
+  }
+}
+
 
 /**
  * Display the role help admin settings page
@@ -188,6 +236,13 @@ function role_help_admin_settings() {
   $form['role_help_format']['#collapsed'] = 0;
   $form['role_help_format']['#description'] = 'The input format to use for all role help text.';
     
+  $form['role_help_summary'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Enable summary'),
+    '#description' => t('Enable a one-line summary field for each role, that will be shown on the role list, and user profile edit pages.'),
+    '#default_value' => variable_get('role_help_summary', 1),
+  );
+
   $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration') );
 
   return $form;  
@@ -200,6 +255,7 @@ function role_help_admin_settings() {
  */
 function role_help_admin_settings_submit($form, &$form_state) {
   variable_set('role_help_format', $form_state['values']['format']);
+  variable_set('role_help_summary', $form_state['values']['role_help_summary']);
 
   foreach (array(1 => 'anonymous_help', 2 => 'authenticated_help') as $rid => $role) {
     db_query('DELETE FROM {role_help} WHERE rid = %d', $rid);    
@@ -261,6 +317,14 @@ function role_help_theme() {
         'description' => NULL,
       ),
     ),
+    // override to provide additional themeing
+    'user_admin_new_role' => array(
+      'function' => 'role_help_user_admin_new_role',
+      'arguments' => array('form' => NULL),
+    ),
+    'role_name' => array(
+      'arguments' => array('name' => NULL, 'rid' => NULL),
+    ),
   );
 }
 
@@ -273,3 +337,46 @@ function theme_role_help_section($name, $description) {
   return $output;
 } // function theme_role_help
 
+/**
+ * Implementation of theme_role_name(). Add role summary to role name.
+ *
+ * @param $name
+ *   Role name
+ * @param $rid
+ *   Role id
+ *
+ * @ingroup themeable
+ */
+function theme_role_name($name, $rid = NULL) {
+  $summary = db_result(db_query("SELECT summary FROM {role_help} d WHERE d.rid = %d", $rid));
+  return '<div class="role-name">'. $name .'</div>' . theme('placeholder', $summary);
+}
+
+/**
+ * Theme the new-role form.
+ *
+ * Override of theme_user_admin_new_role() to add theme('role_name').
+ *
+ * @ingroup themeable
+ */
+function role_help_user_admin_new_role($form) {
+  $header = array(t('Name'), array('data' => t('Operations'), 'colspan' => 2));
+  foreach (user_roles() as $rid => $name) {
+    $edit_permissions = l(t('edit permissions'), 'admin/user/permissions/'. $rid);
+    if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
+      $rows[] = array(theme('role_name', $name, $rid), l(t('edit role'), 'admin/user/roles/edit/'. $rid), $edit_permissions);
+    }
+    else {
+      $rows[] = array($name, t('locked'), $edit_permissions);
+    }
+    if (!empty($summary)) {
+      $rows[] = array($summary);
+    }
+  }
+  $rows[] = array(drupal_render($form['name']), array('data' => drupal_render($form['submit']), 'colspan' => 2));
+
+  $output = drupal_render($form);
+  $output .= theme('table', $header, $rows);
+
+  return $output;
+}
-- 
1.5.5.6

