original_string: public static function delete_data_for_instances(\context $context, array $itemids = []) {
          global $DB;
          $itemsql = '';
          $params = ['contextid' => $context->id];
          if (!empty($itemids)) {
              list($itemsql, $itemparams) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED);
              $params = array_merge($params, $itemparams);
              $itemsql = "AND itemid $itemsql";
          }
          $sql = "SELECT gi.id AS instanceid, gd.id, gd.method
                    FROM {grading_definitions} gd
                    JOIN {grading_instances} gi ON gi.definitionid = gd.id
                    JOIN {grading_areas} ga ON ga.id = gd.areaid
                   WHERE ga.contextid = :contextid $itemsql";
          $records = $DB->get_records_sql($sql, $params);
          if ($records) {
              $firstrecord = current($records);
              $method = $firstrecord->method;
              $instanceids = array_map(function($record) {
                  return $record->instanceid;
              }, $records);
              manager::component_class_callback(
                  "gradingform_{$method}",
                  gradingform_provider_v2::class,
                  'delete_gradingform_for_instances',
                  [$instanceids]);
              // Delete grading_instances rows.
              $DB->delete_records_list('grading_instances', 'id', $instanceids);
          }
      }
code: public static function delete_data_for_instances(\context $context, array $itemids = []) {
          global $DB;
          $itemsql = '';
          $params = ['contextid' => $context->id];
          if (!empty($itemids)) {
              list($itemsql, $itemparams) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED);
              $params = array_merge($params, $itemparams);
              $itemsql = "AND itemid $itemsql";
          }
          $sql = "SELECT gi.id AS instanceid, gd.id, gd.method
                    FROM {grading_definitions} gd
                    JOIN {grading_instances} gi ON gi.definitionid = gd.id
                    JOIN {grading_areas} ga ON ga.id = gd.areaid
                   WHERE ga.contextid = :contextid $itemsql";
          $records = $DB->get_records_sql($sql, $params);
          if ($records) {
              $firstrecord = current($records);
              $method = $firstrecord->method;
              $instanceids = array_map(function($record) {
                  return $record->instanceid;
              }, $records);
              manager::component_class_callback(
                  "gradingform_{$method}",
                  gradingform_provider_v2::class,
                  'delete_gradingform_for_instances',
                  [$instanceids]);
              // Delete grading_instances rows.
              $DB->delete_records_list('grading_instances', 'id', $instanceids);
          }
      }
code_tokens: public static function delete_data_for_instances ( \ context $ context , array $ itemids = [ ] ) { global $ DB ; $ itemsql = '' ; $ params = [ 'contextid' => $ context -> id ] ; if ( ! empty ( $ itemids ) ) { list ( $ itemsql , $ itemparams ) = $ DB -> get_in_or_equal ( $ itemids , SQL_PARAMS_NAMED ) ; $ params = array_merge ( $ params , $ itemparams ) ; $ itemsql = "AND itemid $itemsql" ; } $ sql = "SELECT gi.id AS instanceid, gd.id, gd.method
                    FROM {grading_definitions} gd
                    JOIN {grading_instances} gi ON gi.definitionid = gd.id
                    JOIN {grading_areas} ga ON ga.id = gd.areaid
                   WHERE ga.contextid = :contextid $itemsql" ; $ records = $ DB -> get_records_sql ( $ sql , $ params ) ; if ( $ records ) { $ firstrecord = current ( $ records ) ; $ method = $ firstrecord -> method ; $ instanceids = array_map ( function ( $ record ) { return $ record -> instanceid ; } , $ records ) ; manager :: component_class_callback ( "gradingform_{$method}" , gradingform_provider_v2 :: class , 'delete_gradingform_for_instances' , [ $ instanceids ] ) ; // Delete grading_instances rows. $ DB -> delete_records_list ( 'grading_instances' , 'id' , $ instanceids ) ; } }
docstring: Deletes all user data related to a context and possibly itemids.
  
  @param  \context $context The context to delete on.
  @param  array $itemids  An optional list of item IDs to refine the deletion.
docstring_tokens: Deletes all user data related to a context and possibly itemids .