original_string: protected static function get_calendar_event_ids_by_context(\context $context, $userids = array()) {
          global $DB;
  
          // Calendar Events can exist at Site (CONTEXT_SYSTEM), Course Category (CONTEXT_COURSECAT),
          // Course and Course Group (CONTEXT_COURSE), User (CONTEXT_USER), or Course Modules (CONTEXT_MODULE) contexts.
          if (!in_array($context->contextlevel, [CONTEXT_SYSTEM, CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_USER, CONTEXT_MODULE])) {
              return [];
          }
  
          $whereusersql = '';
          $userparams = array();
          if (!empty($userids)) {
              list($usersql, $userparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
              $whereusersql = "AND e.userid {$usersql}";
          }
  
          if ($context->contextlevel == CONTEXT_MODULE) { // Course Module events.
              $params = ['cmid' => $context->instanceid];
  
              // Get Calendar Events for the specified Course Module context.
              $sql = "SELECT DISTINCT e.id AS eventid
                        FROM {course_modules} cm
                        JOIN {modules} m ON m.id = cm.module
                        JOIN {event} e ON e.modulename = m.name AND e.courseid = cm.course AND e.instance = cm.instance
                       WHERE cm.id = :cmid
                             $whereusersql";
          } else if ($context->contextlevel == CONTEXT_SYSTEM) { // Site events.
              $params = [];
              $sql = "SELECT DISTINCT e.id AS eventid
                        FROM {event} e
                       WHERE e.eventtype = 'site'
                             $whereusersql";
          } else { // The rest.
              $eventfields = [
                  CONTEXT_COURSECAT   => 'categoryid',
                  CONTEXT_COURSE      => 'courseid',
                  CONTEXT_USER        => 'userid'
              ];
              $eventfield = $eventfields[$context->contextlevel];
  
              $eventtypes = [
                  CONTEXT_COURSECAT   => 'category',
                  CONTEXT_COURSE      => ['course' , 'group'],
                  CONTEXT_USER        => 'user'
              ];
              list($eventtypesql, $eventtypeparams) = $DB->get_in_or_equal($eventtypes[$context->contextlevel], SQL_PARAMS_NAMED);
  
              $params = $eventtypeparams + ['instanceid' => $context->instanceid];
  
              // Get Calendar Events for the specified Moodle context.
              $sql = "SELECT DISTINCT e.id AS eventid
                        FROM {event} e
                       WHERE e.eventtype $eventtypesql
                             AND e.{$eventfield} = :instanceid
                             $whereusersql";
          }
  
          $params += $userparams;
  
          return $DB->get_records_sql($sql, $params);
      }
code: protected static function get_calendar_event_ids_by_context(\context $context, $userids = array()) {
          global $DB;
  
          // Calendar Events can exist at Site (CONTEXT_SYSTEM), Course Category (CONTEXT_COURSECAT),
          // Course and Course Group (CONTEXT_COURSE), User (CONTEXT_USER), or Course Modules (CONTEXT_MODULE) contexts.
          if (!in_array($context->contextlevel, [CONTEXT_SYSTEM, CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_USER, CONTEXT_MODULE])) {
              return [];
          }
  
          $whereusersql = '';
          $userparams = array();
          if (!empty($userids)) {
              list($usersql, $userparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
              $whereusersql = "AND e.userid {$usersql}";
          }
  
          if ($context->contextlevel == CONTEXT_MODULE) { // Course Module events.
              $params = ['cmid' => $context->instanceid];
  
              // Get Calendar Events for the specified Course Module context.
              $sql = "SELECT DISTINCT e.id AS eventid
                        FROM {course_modules} cm
                        JOIN {modules} m ON m.id = cm.module
                        JOIN {event} e ON e.modulename = m.name AND e.courseid = cm.course AND e.instance = cm.instance
                       WHERE cm.id = :cmid
                             $whereusersql";
          } else if ($context->contextlevel == CONTEXT_SYSTEM) { // Site events.
              $params = [];
              $sql = "SELECT DISTINCT e.id AS eventid
                        FROM {event} e
                       WHERE e.eventtype = 'site'
                             $whereusersql";
          } else { // The rest.
              $eventfields = [
                  CONTEXT_COURSECAT   => 'categoryid',
                  CONTEXT_COURSE      => 'courseid',
                  CONTEXT_USER        => 'userid'
              ];
              $eventfield = $eventfields[$context->contextlevel];
  
              $eventtypes = [
                  CONTEXT_COURSECAT   => 'category',
                  CONTEXT_COURSE      => ['course' , 'group'],
                  CONTEXT_USER        => 'user'
              ];
              list($eventtypesql, $eventtypeparams) = $DB->get_in_or_equal($eventtypes[$context->contextlevel], SQL_PARAMS_NAMED);
  
              $params = $eventtypeparams + ['instanceid' => $context->instanceid];
  
              // Get Calendar Events for the specified Moodle context.
              $sql = "SELECT DISTINCT e.id AS eventid
                        FROM {event} e
                       WHERE e.eventtype $eventtypesql
                             AND e.{$eventfield} = :instanceid
                             $whereusersql";
          }
  
          $params += $userparams;
  
          return $DB->get_records_sql($sql, $params);
      }
code_tokens: protected static function get_calendar_event_ids_by_context ( \ context $ context , $ userids = array ( ) ) { global $ DB ; // Calendar Events can exist at Site (CONTEXT_SYSTEM), Course Category (CONTEXT_COURSECAT), // Course and Course Group (CONTEXT_COURSE), User (CONTEXT_USER), or Course Modules (CONTEXT_MODULE) contexts. if ( ! in_array ( $ context -> contextlevel , [ CONTEXT_SYSTEM , CONTEXT_COURSECAT , CONTEXT_COURSE , CONTEXT_USER , CONTEXT_MODULE ] ) ) { return [ ] ; } $ whereusersql = '' ; $ userparams = array ( ) ; if ( ! empty ( $ userids ) ) { list ( $ usersql , $ userparams ) = $ DB -> get_in_or_equal ( $ userids , SQL_PARAMS_NAMED ) ; $ whereusersql = "AND e.userid {$usersql}" ; } if ( $ context -> contextlevel == CONTEXT_MODULE ) { // Course Module events. $ params = [ 'cmid' => $ context -> instanceid ] ; // Get Calendar Events for the specified Course Module context. $ sql = "SELECT DISTINCT e.id AS eventid
                        FROM {course_modules} cm
                        JOIN {modules} m ON m.id = cm.module
                        JOIN {event} e ON e.modulename = m.name AND e.courseid = cm.course AND e.instance = cm.instance
                       WHERE cm.id = :cmid
                             $whereusersql" ; } else if ( $ context -> contextlevel == CONTEXT_SYSTEM ) { // Site events. $ params = [ ] ; $ sql = "SELECT DISTINCT e.id AS eventid
                        FROM {event} e
                       WHERE e.eventtype = 'site'
                             $whereusersql" ; } else { // The rest. $ eventfields = [ CONTEXT_COURSECAT => 'categoryid' , CONTEXT_COURSE => 'courseid' , CONTEXT_USER => 'userid' ] ; $ eventfield = $ eventfields [ $ context -> contextlevel ] ; $ eventtypes = [ CONTEXT_COURSECAT => 'category' , CONTEXT_COURSE => [ 'course' , 'group' ] , CONTEXT_USER => 'user' ] ; list ( $ eventtypesql , $ eventtypeparams ) = $ DB -> get_in_or_equal ( $ eventtypes [ $ context -> contextlevel ] , SQL_PARAMS_NAMED ) ; $ params = $ eventtypeparams + [ 'instanceid' => $ context -> instanceid ] ; // Get Calendar Events for the specified Moodle context. $ sql = "SELECT DISTINCT e.id AS eventid
                        FROM {event} e
                       WHERE e.eventtype $eventtypesql
                             AND e.{$eventfield} = :instanceid
                             $whereusersql" ; } $ params += $ userparams ; return $ DB -> get_records_sql ( $ sql , $ params ) ; }
docstring: Helper function to return all Calendar Event id results for a specified context and optionally
  included user list.
  
  @param \context $context
  @param array $userids
  @return array|null
  @throws \dml_exception
docstring_tokens: Helper function to return all Calendar Event id results for a specified context and optionally included user list .