original_string: protected static function ensure_users_enrolled($courseusers) {
          global $DB;
          // If the input array is too big, split it into chunks.
          $maxcoursesinquery = 20;
          if (count($courseusers) > $maxcoursesinquery) {
              $rv = array();
              for ($offset = 0; $offset < count($courseusers); $offset += $maxcoursesinquery) {
                  $chunk = array_slice($courseusers, $offset, $maxcoursesinquery, true);
                  $rv = $rv + self::ensure_users_enrolled($chunk);
              }
              return $rv;
          }
  
          // Create a query verifying valid user enrolments for the number of courses.
          $sql = "SELECT DISTINCT e.courseid, ue.userid
            FROM {user_enrolments} ue
            JOIN {enrol} e ON e.id = ue.enrolid
            WHERE ue.status = :active
              AND e.status = :enabled
              AND ue.timestart < :now1 AND (ue.timeend = 0 OR ue.timeend > :now2)";
          $now = round(time(), -2); // Rounding helps caching in DB.
          $params = array('enabled' => ENROL_INSTANCE_ENABLED,
              'active' => ENROL_USER_ACTIVE,
              'now1' => $now, 'now2' => $now);
          $cnt = 0;
          $subsqls = array();
          $enrolled = array();
          foreach ($courseusers as $id => $userids) {
              $enrolled[$id] = array();
              if (count($userids)) {
                  list($sql2, $params2) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, 'userid'.$cnt.'_');
                  $subsqls[] = "(e.courseid = :courseid$cnt AND ue.userid ".$sql2.")";
                  $params = $params + array('courseid'.$cnt => $id) + $params2;
                  $cnt++;
              }
          }
          if (count($subsqls)) {
              $sql .= "AND (". join(' OR ', $subsqls).")";
              $rs = $DB->get_recordset_sql($sql, $params);
              foreach ($rs as $record) {
                  $enrolled[$record->courseid][] = $record->userid;
              }
              $rs->close();
          }
          return $enrolled;
      }
code: protected static function ensure_users_enrolled($courseusers) {
          global $DB;
          // If the input array is too big, split it into chunks.
          $maxcoursesinquery = 20;
          if (count($courseusers) > $maxcoursesinquery) {
              $rv = array();
              for ($offset = 0; $offset < count($courseusers); $offset += $maxcoursesinquery) {
                  $chunk = array_slice($courseusers, $offset, $maxcoursesinquery, true);
                  $rv = $rv + self::ensure_users_enrolled($chunk);
              }
              return $rv;
          }
  
          // Create a query verifying valid user enrolments for the number of courses.
          $sql = "SELECT DISTINCT e.courseid, ue.userid
            FROM {user_enrolments} ue
            JOIN {enrol} e ON e.id = ue.enrolid
            WHERE ue.status = :active
              AND e.status = :enabled
              AND ue.timestart < :now1 AND (ue.timeend = 0 OR ue.timeend > :now2)";
          $now = round(time(), -2); // Rounding helps caching in DB.
          $params = array('enabled' => ENROL_INSTANCE_ENABLED,
              'active' => ENROL_USER_ACTIVE,
              'now1' => $now, 'now2' => $now);
          $cnt = 0;
          $subsqls = array();
          $enrolled = array();
          foreach ($courseusers as $id => $userids) {
              $enrolled[$id] = array();
              if (count($userids)) {
                  list($sql2, $params2) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, 'userid'.$cnt.'_');
                  $subsqls[] = "(e.courseid = :courseid$cnt AND ue.userid ".$sql2.")";
                  $params = $params + array('courseid'.$cnt => $id) + $params2;
                  $cnt++;
              }
          }
          if (count($subsqls)) {
              $sql .= "AND (". join(' OR ', $subsqls).")";
              $rs = $DB->get_recordset_sql($sql, $params);
              foreach ($rs as $record) {
                  $enrolled[$record->courseid][] = $record->userid;
              }
              $rs->close();
          }
          return $enrolled;
      }
code_tokens: protected static function ensure_users_enrolled ( $ courseusers ) { global $ DB ; // If the input array is too big, split it into chunks. $ maxcoursesinquery = 20 ; if ( count ( $ courseusers ) > $ maxcoursesinquery ) { $ rv = array ( ) ; for ( $ offset = 0 ; $ offset < count ( $ courseusers ) ; $ offset += $ maxcoursesinquery ) { $ chunk = array_slice ( $ courseusers , $ offset , $ maxcoursesinquery , true ) ; $ rv = $ rv + self :: ensure_users_enrolled ( $ chunk ) ; } return $ rv ; } // Create a query verifying valid user enrolments for the number of courses. $ sql = "SELECT DISTINCT e.courseid, ue.userid
            FROM {user_enrolments} ue
            JOIN {enrol} e ON e.id = ue.enrolid
            WHERE ue.status = :active
              AND e.status = :enabled
              AND ue.timestart < :now1 AND (ue.timeend = 0 OR ue.timeend > :now2)" ; $ now = round ( time ( ) , - 2 ) ; // Rounding helps caching in DB. $ params = array ( 'enabled' => ENROL_INSTANCE_ENABLED , 'active' => ENROL_USER_ACTIVE , 'now1' => $ now , 'now2' => $ now ) ; $ cnt = 0 ; $ subsqls = array ( ) ; $ enrolled = array ( ) ; foreach ( $ courseusers as $ id => $ userids ) { $ enrolled [ $ id ] = array ( ) ; if ( count ( $ userids ) ) { list ( $ sql2 , $ params2 ) = $ DB -> get_in_or_equal ( $ userids , SQL_PARAMS_NAMED , 'userid' . $ cnt . '_' ) ; $ subsqls [ ] = "(e.courseid = :courseid$cnt AND ue.userid " . $ sql2 . ")" ; $ params = $ params + array ( 'courseid' . $ cnt => $ id ) + $ params2 ; $ cnt ++ ; } } if ( count ( $ subsqls ) ) { $ sql .= "AND (" . join ( ' OR ' , $ subsqls ) . ")" ; $ rs = $ DB -> get_recordset_sql ( $ sql , $ params ) ; foreach ( $ rs as $ record ) { $ enrolled [ $ record -> courseid ] [ ] = $ record -> userid ; } $ rs -> close ( ) ; } return $ enrolled ; }
docstring: Verify user enrollments for multiple course-user combinations
  
  @param array $courseusers array where keys are course ids and values are array
  of users in this course whose enrolment we wish to verify
  @return array same structure as input array but values list only users from input
  who are enrolled in the course
docstring_tokens: Verify user enrollments for multiple course - user combinations