original_string: public function apply_default_completion($data, $updatecustomrules) {
          global $DB;
  
          $courseid = $data->id;
          $coursecontext = context_course::instance($courseid);
          if (!$modids = $data->modids) {
              return;
          }
          $defaults = [
              'completion' => COMPLETION_DISABLED,
              'completionview' => COMPLETION_VIEW_NOT_REQUIRED,
              'completionexpected' => 0,
              'completionusegrade' => 0
          ];
  
          $data = (array)$data;
  
          if ($updatecustomrules) {
              $customdata = array_diff_key($data, $defaults);
              $data['customrules'] = $customdata ? json_encode($customdata) : null;
              $defaults['customrules'] = null;
          }
          $data = array_intersect_key($data, $defaults);
  
          // Get names of the affected modules.
          list($modidssql, $params) = $DB->get_in_or_equal($modids);
          $params[] = 1;
          $modules = $DB->get_records_select_menu('modules', 'id ' . $modidssql . ' and visible = ?', $params, '', 'id, name');
  
          // Get an associative array of [module_id => course_completion_defaults_id].
          list($in, $params) = $DB->get_in_or_equal($modids);
          $params[] = $courseid;
          $defaultsids = $DB->get_records_select_menu('course_completion_defaults', 'module ' . $in . ' and course = ?', $params, '',
                                                        'module, id');
  
          foreach ($modids as $modid) {
              if (!array_key_exists($modid, $modules)) {
                  continue;
              }
              if (isset($defaultsids[$modid])) {
                  $DB->update_record('course_completion_defaults', $data + ['id' => $defaultsids[$modid]]);
              } else {
                  $defaultsids[$modid] = $DB->insert_record('course_completion_defaults', $data + ['course' => $courseid,
                                                                                                   'module' => $modid]);
              }
              // Trigger event.
              \core\event\completion_defaults_updated::create([
                  'objectid' => $defaultsids[$modid],
                  'context' => $coursecontext,
                  'other' => ['modulename' => $modules[$modid]],
              ])->trigger();
          }
  
          // Add notification.
          \core\notification::add(get_string('defaultcompletionupdated', 'completion'), \core\notification::SUCCESS);
      }
code: public function apply_default_completion($data, $updatecustomrules) {
          global $DB;
  
          $courseid = $data->id;
          $coursecontext = context_course::instance($courseid);
          if (!$modids = $data->modids) {
              return;
          }
          $defaults = [
              'completion' => COMPLETION_DISABLED,
              'completionview' => COMPLETION_VIEW_NOT_REQUIRED,
              'completionexpected' => 0,
              'completionusegrade' => 0
          ];
  
          $data = (array)$data;
  
          if ($updatecustomrules) {
              $customdata = array_diff_key($data, $defaults);
              $data['customrules'] = $customdata ? json_encode($customdata) : null;
              $defaults['customrules'] = null;
          }
          $data = array_intersect_key($data, $defaults);
  
          // Get names of the affected modules.
          list($modidssql, $params) = $DB->get_in_or_equal($modids);
          $params[] = 1;
          $modules = $DB->get_records_select_menu('modules', 'id ' . $modidssql . ' and visible = ?', $params, '', 'id, name');
  
          // Get an associative array of [module_id => course_completion_defaults_id].
          list($in, $params) = $DB->get_in_or_equal($modids);
          $params[] = $courseid;
          $defaultsids = $DB->get_records_select_menu('course_completion_defaults', 'module ' . $in . ' and course = ?', $params, '',
                                                        'module, id');
  
          foreach ($modids as $modid) {
              if (!array_key_exists($modid, $modules)) {
                  continue;
              }
              if (isset($defaultsids[$modid])) {
                  $DB->update_record('course_completion_defaults', $data + ['id' => $defaultsids[$modid]]);
              } else {
                  $defaultsids[$modid] = $DB->insert_record('course_completion_defaults', $data + ['course' => $courseid,
                                                                                                   'module' => $modid]);
              }
              // Trigger event.
              \core\event\completion_defaults_updated::create([
                  'objectid' => $defaultsids[$modid],
                  'context' => $coursecontext,
                  'other' => ['modulename' => $modules[$modid]],
              ])->trigger();
          }
  
          // Add notification.
          \core\notification::add(get_string('defaultcompletionupdated', 'completion'), \core\notification::SUCCESS);
      }
code_tokens: public function apply_default_completion ( $ data , $ updatecustomrules ) { global $ DB ; $ courseid = $ data -> id ; $ coursecontext = context_course :: instance ( $ courseid ) ; if ( ! $ modids = $ data -> modids ) { return ; } $ defaults = [ 'completion' => COMPLETION_DISABLED , 'completionview' => COMPLETION_VIEW_NOT_REQUIRED , 'completionexpected' => 0 , 'completionusegrade' => 0 ] ; $ data = ( array ) $ data ; if ( $ updatecustomrules ) { $ customdata = array_diff_key ( $ data , $ defaults ) ; $ data [ 'customrules' ] = $ customdata ? json_encode ( $ customdata ) : null ; $ defaults [ 'customrules' ] = null ; } $ data = array_intersect_key ( $ data , $ defaults ) ; // Get names of the affected modules. list ( $ modidssql , $ params ) = $ DB -> get_in_or_equal ( $ modids ) ; $ params [ ] = 1 ; $ modules = $ DB -> get_records_select_menu ( 'modules' , 'id ' . $ modidssql . ' and visible = ?' , $ params , '' , 'id, name' ) ; // Get an associative array of [module_id => course_completion_defaults_id]. list ( $ in , $ params ) = $ DB -> get_in_or_equal ( $ modids ) ; $ params [ ] = $ courseid ; $ defaultsids = $ DB -> get_records_select_menu ( 'course_completion_defaults' , 'module ' . $ in . ' and course = ?' , $ params , '' , 'module, id' ) ; foreach ( $ modids as $ modid ) { if ( ! array_key_exists ( $ modid , $ modules ) ) { continue ; } if ( isset ( $ defaultsids [ $ modid ] ) ) { $ DB -> update_record ( 'course_completion_defaults' , $ data + [ 'id' => $ defaultsids [ $ modid ] ] ) ; } else { $ defaultsids [ $ modid ] = $ DB -> insert_record ( 'course_completion_defaults' , $ data + [ 'course' => $ courseid , 'module' => $ modid ] ) ; } // Trigger event. \ core \ event \ completion_defaults_updated :: create ( [ 'objectid' => $ defaultsids [ $ modid ] , 'context' => $ coursecontext , 'other' => [ 'modulename' => $ modules [ $ modid ] ] , ] ) -> trigger ( ) ; } // Add notification. \ core \ notification :: add ( get_string ( 'defaultcompletionupdated' , 'completion' ) , \ core \ notification :: SUCCESS ) ; }
docstring: Saves default completion from edit form to all selected module types
  
  @param stdClass $data data received from the core_completion_bulkedit_form
  @param bool $updatecustomrules if we need to update the custom rules of the module -
  if no module-specific completion rules were added to the form, update of the module table is not needed.
docstring_tokens: Saves default completion from edit form to all selected module types