/[drupal]/contributions/modules/pcp/pcp.module
ViewVC logotype

Diff of /contributions/modules/pcp/pcp.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1.4.1, Thu Oct 9 21:49:20 2008 UTC revision 1.1.4.2, Fri Oct 17 20:53:39 2008 UTC
# Line 0  Line 1 
1    <?php
2    // $Id$
3    
4    /**
5     * @file
6     * Allows users with valid permissions to tag profile fields created
7     * from the profile module as required fields for a
8     * users profile to be considered complete.
9     */
10    
11    /**
12     * Implementation of hook_perm()
13     */
14    function pcp_perm() {
15      return array(
16        'administer pcp',
17        );
18    }
19    
20    /**
21     * Implementation of hook_menu()
22     */
23    function pcp_menu() {
24      $items = array();
25    
26      $items['admin/user/pcp'] = array(
27        'title' => 'Profile Complete Percentages',
28        'description' => 'Tag profile fields as required for percent complete handling.',
29        'page callback' => 'pcp_admin_settings',
30        'access arguments' => array('administer pcp'),
31        );
32    
33      return $items;
34    }
35    
36    /**
37     * Implementation of hook_block()
38     */
39    function pcp_block($op = 'list', $delta = 0, $edit = array()) {
40      switch ($op) {
41        case 'list':
42          $blocks[0]['info'] = t('Profile Complete Percentage');
43          return $blocks;
44          break;
45        case 'view':
46          switch ($delta) {
47            case 0:
48              global $user;
49              $complete_data = pcp_get_complete_percentage_data($user);
50              $block = array(
51                'subject' => t('Profile Complete'),
52                'content' => theme('pcp_profile_percent_complete', $complete_data),
53                );
54              break;
55          }
56          return $block;
57          break;
58      }
59    }
60    
61    /**
62     * Implementation of hook_form_alter()
63     */
64    function pcp_form_alter(&$form, $form_state, $form_id) {
65      if ($form_id == 'profile_field_form' && user_access('administer pcp')) {
66        $fid = $form['fid']['#value'];
67        $tag = TRUE;
68        if ($fid) {
69          $field_data = pcp_get_tagged_profile_fields($fid);
70          if (!$field_data[0]['fid']) {
71            $tag = FALSE;
72          }
73        }
74        $form['pcp_settings'] = array(
75          '#type' => 'fieldset',
76          '#title' => t('PCP Settings'),
77          '#weight' => 0,
78          );
79        $form['pcp_settings']['tag'] = array(
80          '#type' => 'checkbox',
81          '#title' => t('Make required for PCP module'),
82          '#description' => t('Checking this box will tag this field as a required field for completion of the users profile.'),
83          '#default_value' => $tag,
84          );
85        $form['#submit'][] = 'pcp_profile_field_form_submit';
86      }
87    
88      if ($form_id == 'user_profile_form' && arg(3) != '' && $_GET['fieldname'] != '') {
89        $fieldname = 'edit-'. preg_replace("/_/", "-", $_GET['fieldname']);
90        drupal_add_js("
91          $('#". $fieldname ."').css({
92            'border' : '2px solid red'
93            });
94        ", 'inline', 'footer');
95      }
96    }
97    
98    /**
99     * Called when a user submits a profile field form from the
100     * profile module (when adding or editing a profile field).
101     */
102    function pcp_profile_field_form_submit($form, &$form_state) {
103      $fid = $form_state['values']['fid'] ? $form_state['values']['fid'] : db_result(db_query("SELECT MAX(fid) FROM {profile_fields}"));
104      db_query("DELETE FROM {profile_pcp} WHERE fid = %d", $form_state['values']['fid']);
105      if ($form_state['values']['tag']) {
106        db_query("INSERT INTO {profile_pcp} (`fid`) VALUES (%d)", $fid);
107      }
108    }
109    
110    /**
111     * Menu Callback Function
112     *  Build output of the pcp module settings which allows the
113     *  administrator to tag specific profile fields which will be
114     *  used to determine the completion of a users profile.
115     */
116    function pcp_admin_settings() {
117      $header = t('Checking a profile field below will add that field to the logic of the complete percentage.');
118      $form = drupal_get_form('pcp_admin_settings_form');
119      return $header . $form;
120    }
121    
122    /**
123     * Admin settings form
124     */
125    function pcp_admin_settings_form() {
126      $options = pcp_admin_settings_form_data();
127      $form = array();
128      $form['profile_fields'] = array(
129        '#title' => t('Profile Fields'),
130        '#type' => 'checkboxes',
131        '#options' => $options['profile_fields_options'],
132        '#default_value' => $options['default_values'],
133        );
134      $form['submit'] = array(
135        '#type' => 'submit',
136        '#value' => t('Save'),
137        );
138      return $form;
139    }
140    
141    /**
142     * Admin settings form submit
143     */
144    function pcp_admin_settings_form_submit($form, &$form_state) {
145      if (is_array($form_state['values']['profile_fields']) && !empty($form_state['values']['profile_fields'])) {
146        db_query("DELETE FROM {profile_pcp}");
147        foreach ($form_state['values']['profile_fields'] as $fid) {
148          if ($fid) {
149            db_query("INSERT INTO {profile_pcp} VALUES (%d)", $fid);
150          }
151        }
152        drupal_set_message("Your settings have been saved.");
153      }
154    }
155    
156    /**
157     * Function that sets up parameters to be used
158     * when the pcp_admin_settings_form() function
159     * is executed.
160     *
161     * @return - assoc array
162     *  ['profile_fields_options']
163     *    - An associative array of all fields created from the profile module.
164     *  ['default_values']
165     *    - An indexed array of all (if any) default values for the form.
166     */
167    function pcp_admin_settings_form_data() {
168      $profile_fields = pcp_get_profile_fields();
169      foreach ($profile_fields as $key => $value) {
170        $profile_fields_options[$value['fid']] = $value['title'];
171      }
172      $tagged_profile_fields = pcp_get_tagged_profile_fields();
173      foreach ($tagged_profile_fields as $key => $value) {
174        $default_values[] = $value['fid'];
175      }
176      $options['profile_fields_options'] = $profile_fields_options;
177      $options['default_values'] = $default_values;
178      return $options;
179    }
180    
181    /**
182     * Return a users profile field values that have been saved
183     * for a given user.
184     *
185     * @param int $uid - The uid of the user we are returning data for.
186     *
187     * @return assoc array of all profile fields for the user.
188     */
189    function pcp_get_user_profile_values($uid) {
190      $values = array();
191      if ($uid) {
192        $query = db_query("SELECT * FROM {profile_values} WHERE uid = %d", $uid);
193        while ($result = db_fetch_array($query)) {
194          $values[$result['fid']] = $result['value'];
195        }
196      }
197      return $values;
198    }
199    
200    /**
201     * Get the profile complete percentage data for a given user.
202     *
203     * @param obj $user
204    *  - The user object to get data for.
205     *
206     * @return assoc array of all values needed at the theme layer.
207     *  - Refer to comments in theme_pcp_profile_percent_complete() for specific values.
208     */
209    function pcp_get_complete_percentage_data($user) {
210      $fields = pcp_get_tagged_profile_fields();
211      $user_values = pcp_get_user_profile_values($user->uid);
212      $percent = 0;
213      $complete = 0;
214      $nextfield_set = FALSE;
215    
216      if (is_array($fields) && !empty($fields)) {
217        foreach ($fields as $key => $value) {
218          if ($user_values[$value['fid']] == '') {
219            if ($nextfield_set === FALSE) {
220              $nextfield_set = TRUE;
221              $fid = $value['fid'];
222              $nextdata = db_fetch_array(db_query("SELECT title, name, category FROM {profile_fields} WHERE fid = %d", $fid));
223              $nextfield = $nextdata['title'];
224              $nextcategory = $nextdata['category'];
225              $nextname = $nextdata['name'];
226            }
227            continue;
228          }
229          $complete++;
230        }
231    
232        $dec = number_format(($complete / count($fields)), 2);
233        $percent = $dec * 100;
234        if ($nextfield_set) {
235          $next = number_format((($complete + 1) / count($fields)), 2);
236          $nextpercent = $next * 100;
237        }
238      }
239    
240      $complete_data['uid'] = $user->uid;
241      $complete_data['percent'] = $percent;
242      $complete_data['completed']  = $complete;
243      $complete_data['incomplete'] = count($fields) - $complete;
244      $complete_data['total'] = count($fields);
245      $complete_data['nextfield'] = $nextfield;
246      $complete_data['nextpercent'] = $nextpercent;
247      $complete_data['nextcategory'] = $nextcategory;
248      $complete_data['nextname'] = $nextname;
249    
250      return $complete_data;
251    }
252    
253    /**
254     * Get all the profile fields that have been tagged.
255     * If an $fid is passed in, only the data for that field will be returned.
256     *
257     * @param int $fid - The fid of the field data should be returned for. If 0, all fields are returned.
258     *
259     * @return indexed array - All fields and their information returned from the query.
260     */
261    function pcp_get_tagged_profile_fields($fid = 0) {
262      $where = '';
263      $args = '';
264    
265      if ($fid > 0) {
266        $where = " WHERE fid = %d";
267        $args = array($fid);
268      }
269      $query = db_query("SELECT * FROM {profile_pcp} LEFT JOIN {profile_fields} USING(fid) $where", $args);
270      $fields = array();
271      while ($result = db_fetch_array($query)) {
272        $fields[] = $result;
273      }
274      return $fields;
275    }
276    
277    /**
278     * Get all the profile fields stored in the system, tagged or not tagged.
279     */
280    function pcp_get_profile_fields() {
281      $query = db_query("SELECT * FROM {profile_fields}");
282      $fields = array();
283      while ($result = db_fetch_array($query)) {
284        $fields[] = $result;
285      }
286      return $fields;
287    }
288    
289    
290    function pcp_theme() {
291      return array(
292        'pcp_profile_percent_complete' => array(
293          'arguments' => array('complete_data' => NULL),
294        ),
295      );
296    }
297    
298    /**
299     * Block Theme function that displays the default output of a users
300     * profile complete percent. Use this theme function to override
301     * the output / display of this block.
302     *
303     * @param - assoc array $complete_data
304     *  ['uid'] - int - The user ID of the user viewing the page.
305     *  ['percent'] - int - A number that represents the total percent complete (ie 50).
306     *  ['completed'] - int - How many fields total that have been completed (filled out) by $user.
307     *  ['incomplete'] - int - How many fields still need to be filled out.
308     *  ['total'] - int - The count of all tagged profile fields.
309     *  ['nextfield'] - str - The next field to fill out that is currently empty.
310     *  ['nextpercent'] - int - What the users total percent complete value will be when ['nextfield'] is complete.
311     *  ['nextcategory] - str - The category the next field falls under for targeting with a link.
312     *  ['nextname'] - str - The field name of the next field for field focus after linked to the profile field.
313     */
314    function theme_pcp_profile_percent_complete($complete_data) {
315      $output = '<style type="text/css">';
316      $output .= '#pcp-percent-bar-wrapper {width: 100%; border: 1px solid #000; padding: 1px;}';
317      $output .= '#pcp-percent-bar { width: '. $complete_data['percent'] .'%; height: 10px; background-color: #777777;}';
318      $output .= '</style>';
319    
320      $output .= '<div id="pcp-wrapper">';
321      $output .= t('!complete% Complete', array('!complete' => $complete_data['percent']));
322      $output .= '<div id="pcp-percent-bar-wrapper">';
323      $output .= '<div id="pcp-percent-bar"></div>';
324      $output .= '</div>';
325      $output .= '</div>';
326    
327      if ($complete_data['nextfield'] && $complete_data['nextpercent']) {
328        $output .= 'Filling out <i>'. l($complete_data['nextfield'], 'user/'. $complete_data['uid'] .'/edit/'. $complete_data['nextcategory'], array('query' => 'fieldname='. $complete_data['nextname'])) .'</i> will bring your profile to '. t('!complete% Complete', array('!complete' => $complete_data['nextpercent']));
329      }
330    
331      return $output;
332    }

Legend:
Removed from v.1.1.4.1  
changed lines
  Added in v.1.1.4.2

  ViewVC Help
Powered by ViewVC 1.1.2