original_string: public function find_users($search) {
          global $DB;
  
          $whereconditions = array();
          list($wherecondition, $params) = $this->search_sql($search, 'u');
          if ($wherecondition) {
              $whereconditions[] = $wherecondition;
          }
  
          $existingids = array();
          foreach ($this->existingrecipients as $group) {
              foreach ($group as $user) {
                  $existingids[] = $user->id;
              }
          }
          if ($existingids) {
              list($usertest, $userparams) = $DB->get_in_or_equal($existingids, SQL_PARAMS_NAMED, 'ex', false);
              $whereconditions[] = 'u.id ' . $usertest;
              $params = array_merge($params, $userparams);
          }
  
          if ($whereconditions) {
              $wherecondition = ' WHERE ' . implode(' AND ', $whereconditions);
          }
  
          list($esql, $eparams) = get_enrolled_sql($this->context, 'moodle/badges:earnbadge', 0, true);
          $params = array_merge($params, $eparams);
  
          $fields      = 'SELECT ' . $this->required_fields_sql('u');
          $countfields = 'SELECT COUNT(u.id)';
  
          $params['badgeid'] = $this->badgeid;
          $params['issuerrole'] = $this->issuerrole;
  
          $sql = " FROM {user} u JOIN ($esql) je ON je.id = u.id
                   LEFT JOIN {badge_manual_award} bm
                       ON (bm.recipientid = u.id AND bm.badgeid = :badgeid AND bm.issuerrole = :issuerrole)
                   $wherecondition AND bm.id IS NULL";
  
          list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
          $order = ' ORDER BY ' . $sort;
  
          if (!$this->is_validating()) {
              $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
              if ($potentialmemberscount > self::MAX_USERS_PER_PAGE) {
                  return $this->too_many_results($search, $potentialmemberscount);
              }
          }
  
          $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
  
          if (empty($availableusers)) {
              return array();
          }
  
          return array(get_string('potentialrecipients', 'badges') => $availableusers);
      }
code: public function find_users($search) {
          global $DB;
  
          $whereconditions = array();
          list($wherecondition, $params) = $this->search_sql($search, 'u');
          if ($wherecondition) {
              $whereconditions[] = $wherecondition;
          }
  
          $existingids = array();
          foreach ($this->existingrecipients as $group) {
              foreach ($group as $user) {
                  $existingids[] = $user->id;
              }
          }
          if ($existingids) {
              list($usertest, $userparams) = $DB->get_in_or_equal($existingids, SQL_PARAMS_NAMED, 'ex', false);
              $whereconditions[] = 'u.id ' . $usertest;
              $params = array_merge($params, $userparams);
          }
  
          if ($whereconditions) {
              $wherecondition = ' WHERE ' . implode(' AND ', $whereconditions);
          }
  
          list($esql, $eparams) = get_enrolled_sql($this->context, 'moodle/badges:earnbadge', 0, true);
          $params = array_merge($params, $eparams);
  
          $fields      = 'SELECT ' . $this->required_fields_sql('u');
          $countfields = 'SELECT COUNT(u.id)';
  
          $params['badgeid'] = $this->badgeid;
          $params['issuerrole'] = $this->issuerrole;
  
          $sql = " FROM {user} u JOIN ($esql) je ON je.id = u.id
                   LEFT JOIN {badge_manual_award} bm
                       ON (bm.recipientid = u.id AND bm.badgeid = :badgeid AND bm.issuerrole = :issuerrole)
                   $wherecondition AND bm.id IS NULL";
  
          list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
          $order = ' ORDER BY ' . $sort;
  
          if (!$this->is_validating()) {
              $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
              if ($potentialmemberscount > self::MAX_USERS_PER_PAGE) {
                  return $this->too_many_results($search, $potentialmemberscount);
              }
          }
  
          $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
  
          if (empty($availableusers)) {
              return array();
          }
  
          return array(get_string('potentialrecipients', 'badges') => $availableusers);
      }
code_tokens: public function find_users ( $ search ) { global $ DB ; $ whereconditions = array ( ) ; list ( $ wherecondition , $ params ) = $ this -> search_sql ( $ search , 'u' ) ; if ( $ wherecondition ) { $ whereconditions [ ] = $ wherecondition ; } $ existingids = array ( ) ; foreach ( $ this -> existingrecipients as $ group ) { foreach ( $ group as $ user ) { $ existingids [ ] = $ user -> id ; } } if ( $ existingids ) { list ( $ usertest , $ userparams ) = $ DB -> get_in_or_equal ( $ existingids , SQL_PARAMS_NAMED , 'ex' , false ) ; $ whereconditions [ ] = 'u.id ' . $ usertest ; $ params = array_merge ( $ params , $ userparams ) ; } if ( $ whereconditions ) { $ wherecondition = ' WHERE ' . implode ( ' AND ' , $ whereconditions ) ; } list ( $ esql , $ eparams ) = get_enrolled_sql ( $ this -> context , 'moodle/badges:earnbadge' , 0 , true ) ; $ params = array_merge ( $ params , $ eparams ) ; $ fields = 'SELECT ' . $ this -> required_fields_sql ( 'u' ) ; $ countfields = 'SELECT COUNT(u.id)' ; $ params [ 'badgeid' ] = $ this -> badgeid ; $ params [ 'issuerrole' ] = $ this -> issuerrole ; $ sql = " FROM {user} u JOIN ($esql) je ON je.id = u.id
                   LEFT JOIN {badge_manual_award} bm
                       ON (bm.recipientid = u.id AND bm.badgeid = :badgeid AND bm.issuerrole = :issuerrole)
                   $wherecondition AND bm.id IS NULL" ; list ( $ sort , $ sortparams ) = users_order_by_sql ( 'u' , $ search , $ this -> accesscontext ) ; $ order = ' ORDER BY ' . $ sort ; if ( ! $ this -> is_validating ( ) ) { $ potentialmemberscount = $ DB -> count_records_sql ( $ countfields . $ sql , $ params ) ; if ( $ potentialmemberscount > self :: MAX_USERS_PER_PAGE ) { return $ this -> too_many_results ( $ search , $ potentialmemberscount ) ; } } $ availableusers = $ DB -> get_records_sql ( $ fields . $ sql . $ order , array_merge ( $ params , $ sortparams ) ) ; if ( empty ( $ availableusers ) ) { return array ( ) ; } return array ( get_string ( 'potentialrecipients' , 'badges' ) => $ availableusers ) ; }
docstring: Finds all potential badge recipients
  
  Potential badge recipients are all enroled users
  who haven't got a badge from current issuer role.
  
  @param string $search
  @return array
docstring_tokens: Finds all potential badge recipients