3

We are using civiCRM 4.7.25 with Joomla 3.8.1

We have a proximity search to allow people to find nearby members of an national association.

The proximity search returns matching members alphabetically....

But we want the members to have "equal" opportunity to appear towards the top of the results list -- so we'd like to have the results of the proximity search return results randomly, instead of alphabetically.

Is there a way to do this?

1

Take a look at https://github.com/civicrm/civicrm-core/blob/master/CRM/Contact/Form/Search/Custom/RandomSegment.php

It's a little tricky because if there's multiple pages of results you don't want the second page to repeat anything on the first, so it creates the full random-ordered list first and draws from that.

If you don't really care, or know there's only one page, you could override addSortOffset() in CRM/Contact/Form/Search/Custom/Proximity.php to do something similar to what the Base does in CRM/Contact/Form/Search/Custom/Base.php, and instead of "ORDER BY $sort", make it say "ORDER BY RAND()", e.g.

public function addSortOffset(&$sql, $offset, $rowcount, $sort) {
    $sql .= " ORDER BY RAND() ";

    if ($rowcount > 0 && $offset >= 0) {
      $offset = CRM_Utils_Type::escape($offset, 'Int');
      $rowcount = CRM_Utils_Type::escape($rowcount, 'Int');

      $sql .= " LIMIT $offset, $rowcount ";
    }
  }
  • Thanks for this help. How do I get the Proximity Search profile to use the RandomSegment.php? – A. Block Oct 19 '17 at 13:27
  • No the randomsegment is an example of how to fully do random listings if you want the full treatment. But either way you'd need to edit proximity.php, or better, create a new custom search that's a copy of proximity.php and edit that. See docs.civicrm.org/dev/en/latest/extensions/civix/… – Demerit Oct 19 '17 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.