Projet

Général

Profil

Révision 3461d8cb

Ajouté par Assos Assos il y a environ 10 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/fivestar/fivestar.module
53 53
    'average_rating' => array(
54 54
      '#itemprop' => array('ratingValue'),
55 55
    ),
56
    'rating_count' => array(
57
      '#itemprop' => array('ratingCount'),
58
    ),
56 59
  );
57 60

  
58 61
  return $mappings;
......
142 145
 * voting, etc...
143 146
 */
144 147

  
145
function _fivestar_cast_vote($entity_type, $id, $value, $tag = NULL, $uid = NULL, $skip_validation = FALSE) {
148
function _fivestar_cast_vote($entity_type, $id, $value, $tag = NULL, $uid = NULL, $skip_validation = FALSE, $vote_source = NULL) {
146 149
  global $user;
147 150
  $tag = empty($tag) ? 'vote' : $tag;
148 151
  $uid = isset($uid) ? $uid : $user->uid;
......
159 162
    // Get the user's current vote.
160 163
    $criteria = array('entity_type' => $entity_type, 'entity_id' => $id, 'tag' => $tag, 'uid' => $uid);
161 164
    // Get the unique identifier for the user (IP Address if anonymous).
162
    $user_criteria = votingapi_current_user_identifier();
163
    $user_votes = votingapi_select_votes($criteria + $user_criteria);
165
    
166
    if ($vote_source != NULL) {
167
      $user_criteria = array('vote_source' => $vote_source);
168
      $limit = 1;
169
    }
170
    else {
171
      $user_criteria = votingapi_current_user_identifier();
172
      $limit = 0;
173
    }
174
    $user_votes = votingapi_select_votes($criteria + $user_criteria, $limit);
164 175

  
165 176
    if ($value == 0) {
166 177
      votingapi_delete_votes($user_votes);
......
197 208
 *   - count: An array of VotingAPI results, including the count 'value'.
198 209
 *   - user: An array of VotingAPI results, including the user's vote 'value'.
199 210
 */
200
function fivestar_get_votes($entity_type, $id, $tag = 'vote', $uid = NULL) {
211
function fivestar_get_votes_multiple($entity_type, $ids, $tag = 'vote', $uid = NULL) {
201 212
  global $user;
202 213

  
203 214
  if (!isset($uid)) {
......
206 217

  
207 218
  $criteria = array(
208 219
    'entity_type' => $entity_type,
209
    'entity_id' => $id,
220
    'entity_id' => $ids,
210 221
    'value_type' => 'percent',
211 222
    'tag' => $tag,
212 223
  );
213 224

  
214
  $votes = array(
215
    'average' => array(),
216
    'count' => array(),
217
    'user' => array(),
218
  );
225
  $votes = array();
226

  
227
  // Initialize defaults.
228
  foreach($ids as $id) {
229
    $votes[$entity_type][$id] = array(
230
      'average' => array(),
231
      'count' => array(),
232
      'user' => array(),
233
    );
234
  }
219 235

  
220 236
  $results = votingapi_select_results($criteria);
237
  $user_votes = array();
238
  if(!empty($results)) {
239
    foreach(votingapi_select_votes($criteria += array('uid' => $uid)) as $uv) {
240
      $user_votes[$uv['entity_type']][$uv['entity_id']] = $uv;
241
    }
242
  }
243

  
221 244
  foreach ($results as $result) {
222 245
    if ($result['function'] == 'average') {
223
      $votes['average'] = $result;
246
      $votes[$result['entity_type']][$result['entity_id']]['average'] = $result;
224 247
    }
225 248
    if ($result['function'] == 'count') {
226
      $votes['count'] = $result;
249
      $votes[$result['entity_type']][$result['entity_id']]['count'] = $result;
227 250
    }
228
  }
229
  if ($uid) {
230
    $user_vote = votingapi_select_votes($criteria += array('uid' => $uid));
231
    if ($user_vote) {
232
      $votes['user'] = $user_vote[0];
233
      $votes['user']['function'] = 'user';
234
    }
235
  }
236
  else {
237
    // If the user is anonymous, we never bother loading their existing votes.
238
    // Not only would it be hit-or-miss, it would break page caching. Safer to always
239
    // show the 'fresh' version to anon users.
240
    $votes['user'] = array('value' => 0);
251
	  if ($uid) {
252
	    if (!empty($user_votes[$result['entity_type']][$result['entity_id']])) {
253
        $votes[$result['entity_type']][$result['entity_id']]['user'] = $user_votes[$result['entity_type']][$result['entity_id']];
254
        $votes[$result['entity_type']][$result['entity_id']]['user']['function'] = 'user';
255
	    }
256
	  }
257
	  else {
258
	    // If the user is anonymous, we never bother loading their existing votes.
259
	    // Not only would it be hit-or-miss, it would break page caching. Safer to always
260
	    // show the 'fresh' version to anon users.
261
      $votes[$result['entity_type']][$result['entity_id']]['user'] = array('value' => 0);
262
	  }
241 263
  }
242 264

  
243 265
  return $votes;
244 266
}
245 267

  
268
function fivestar_get_votes($entity_type, $id, $tag = 'vote', $uid = NULL) {
269
  $multiple = fivestar_get_votes_multiple($entity_type, array($id), $tag, $uid);
270
  return $multiple[$entity_type][$id];
271
}
272

  
246 273
/**
247 274
 * Check that an item being voted upon is a valid vote.
248 275
 *
......
351 378
 *   file for a fivestar widget. Each value should be the name of the widget.
352 379
 */
353 380
function fivestar_fivestar_widgets() {
354
  $files = &drupal_static(__FUNCTION__);
355
  if (!isset($files)) {
381
  $widgets = &drupal_static(__FUNCTION__);
382
  if (!isset($widgets)) {
383
    $cache = cache_get(__FUNCTION__);
384
    if ($cache) {
385
      $widgets = $cache->data;
386
    }
387
  }
388

  
389
  if (!isset($widgets)) {
356 390
    $widgets_directory = drupal_get_path('module', 'fivestar') . '/widgets';
357 391
    $files = file_scan_directory($widgets_directory, '/\.css$/');
358
  }
359
  $widgets = array();
360
  foreach ($files as $file) {
361
    if (strpos($file->filename, '-rtl.css') === FALSE) {
362
      $widgets[$file->uri] = drupal_ucfirst(str_replace('-color', '', $file->name));
392
    foreach ($files as $file) {
393
      if (strpos($file->filename, '-rtl.css') === FALSE) {
394
        $widgets[$file->uri] = drupal_ucfirst(str_replace('-color', '', $file->name));
395
      }
363 396
    }
397
    cache_set(__FUNCTION__, $widgets);
364 398
  }
365 399
  return $widgets;
366 400
}
......
411 445
    '#stars' => $settings['stars'],
412 446
    '#auto_submit' => isset($settings['autosubmit']) ? $settings['autosubmit'] : TRUE,
413 447
    '#allow_clear' => $settings['allow_clear'],
448
    '#allow_revote' => $settings['allow_revote'],
449
    '#allow_ownvote' => $settings['allow_ownvote'],
414 450
    '#required' => isset($settings['required']) ? $settings['required'] : FALSE,
415 451
    '#widget' => isset($settings['widget']) ? $settings['widget'] : array('name' => 'default', 'css' => 'default'),
416 452
    '#values' => $values,
......
431 467
 * Submit handler for the above form (non-javascript version).
432 468
 */
433 469
function fivestar_form_submit($form, &$form_state) {
470

  
434 471
  // Cast the vote.
435 472
  _fivestar_cast_vote($form_state['settings']['content_type'], $form_state['settings']['content_id'], $form_state['values']['vote'], $form_state['settings']['tag']);
436 473

  
......
448 485
 */
449 486
function fivestar_ajax_submit($form, $form_state) {
450 487
  if (!empty($form_state['settings']['content_id'])) {
451
    _fivestar_update_field_value($form_state['settings']['content_type'], $form_state['settings']['entity'], $form_state['settings']['field_name'], $form_state['settings']['langcode'], $form_state['values']['vote']);
488
    $entity = entity_load($form_state['settings']['entity_type'], array($form_state['settings']['entity_id']));
489
    $entity = reset($entity);
490
    _fivestar_update_field_value($form_state['settings']['content_type'], $entity, $form_state['settings']['field_name'], $form_state['settings']['langcode'], $form_state['values']['vote']);
452 491
    $votes = _fivestar_cast_vote($form_state['settings']['content_type'], $form_state['settings']['content_id'], $form_state['values']['vote'], $form_state['settings']['tag']);
453 492
  }
454 493

  
455

  
456 494
  $values = array();
457 495
  $values['user'] = isset($votes['user']['value']) ? $votes['user']['value'] : 0;
458 496
  $values['average'] = isset($votes['average']['value']) ? $votes['average']['value'] : 0;
......
463 501
  $new_element = fivestar_expand($form['vote']);
464 502
  // Update the description. Since it doesn't account of our cast vote above.
465 503
  // TODO: Look into all this, I honestly don't like it and it's a bit weird.
466
  $form['vote']['vote']['#description'] = $new_element['vote']['#description'];
504
  $form['vote']['vote']['#description'] = isset($new_element['vote']['#description']) ? $new_element['vote']['#description'] : '';
467 505

  
468 506
  return array(
469 507
    '#type' => 'ajax',
......
486 524
    '#input' => TRUE,
487 525
    '#stars' => 5,
488 526
    '#allow_clear' => FALSE,
527
    '#allow_revote' => FALSE,
528
    '#allow_ownvote' => FALSE,
489 529
    '#auto_submit' => FALSE,
490 530
    '#process' => array('fivestar_expand'),
491 531
    '#theme_wrappers' => array('form_element'),
......
510 550
 * Process callback for fivestar_element -- see fivestar_element()
511 551
 */
512 552
function fivestar_expand($element) {
553
  if (!_fivestar_allow_vote($element)){
554
    $element['#input'] = FALSE;
555
  }
556

  
513 557
  // Add CSS and JS
514 558
  $path = drupal_get_path('module', 'fivestar');
515 559
  $element['#attached']['js'][] = $path . '/js/fivestar.js';
......
532 576
    '#type' => 'select',
533 577
    '#options' => $options,
534 578
    '#required' => $element['#required'],
535
    '#theme' => 'fivestar_select',
536 579
    '#attributes' => $element['#attributes'],
580
    '#theme' => _fivestar_allow_vote($element) ? 'fivestar_select' : 'fivestar_static',
537 581
    '#default_value' => _fivestar_get_element_default_value($element),
538 582
    '#weight' => -2,
539 583
  );
......
582 626
      ));
583 627
      $class[] = ' fivestar-combo-text';
584 628
      break;
629
    case 'none':
630
      $element['vote']['#description'] = NULL;
631
      break;
585 632
  }
586 633

  
587 634
  switch ($settings['style']) {
......
897 944
    'entity_type' => 'node',
898 945
  );
899 946
}
947

  
948
/**
949
 * Helper function to determine if a user can vote on content.
950
 *
951
 * @return Boolean
952
 */
953
function _fivestar_allow_vote($element) {
954
  global $user;
955

  
956
  // check allowed to re-vote
957
  $can_revote = FALSE;
958
  if ($element['#allow_revote']) {
959
    $can_revote = TRUE;
960
  }
961
  else {
962
    $criteria = array(
963
      'entity_id' => isset($element['#settings']['content_id']) ? $element['#settings']['content_id'] : NULL,
964
      'entity_type' => isset($element['#settings']['content_type']) ? $element['#settings']['content_type'] : NULL,
965
      'uid' => $user->uid,
966
    );
967

  
968
  $can_revote = !votingapi_select_votes($criteria);
969
  }
970
  if (!$can_revote) {
971
    return FALSE;
972
  }
973
  // check allowed own vote
974
  if ($element['#allow_ownvote']) {
975
    return TRUE;
976
  }
977
  else {
978
    $entity_id = $element['#settings']['entity_id'];
979
    //$entity = entity_load($element['#settings']['entity_type'], array($element['#settings']['entity_id']));
980
    $entity = entity_load($element['#settings']['entity_type'], array($entity_id));
981
    $entity = $entity[$entity_id];
982
    $uid1 = $entity->uid;
983
    $uid2 = $user->uid;
984
    return $entity->uid != $user->uid;
985
  }
986
}

Formats disponibles : Unified diff