original_string: public function store_item($cm) {
          global $CFG, $DB;
  
          require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  
          // Get more information.
          $modinfo = get_fast_modinfo($cm->course);
  
          if (!isset($modinfo->cms[$cm->id])) {
              return; // Can't continue without the module information.
          }
  
          $cminfo = $modinfo->cms[$cm->id];
  
          // Check backup/restore support.
          if (!plugin_supports('mod', $cminfo->modname , FEATURE_BACKUP_MOODLE2)) {
              return;
          }
  
          // As far as recycle bin is using MODE_AUTOMATED, it observes the backup_auto_storage
          // setting (storing backups @ real location. For recycle bin we want to ensure that
          // backup files are always stored in Moodle file area. In order to achieve that, we
          // hack the setting here via $CFG->forced_plugin_settings, so it won't interfere other
          // operations.  See MDL-65218 for more information.
          // This hack will be removed once recycle bin switches to use its own backup mode, with
          // own preferences and 100% appart from MODLE_AUTOMATED.
          // TODO: Remove this as part of MDL-65228.
          $CFG->forced_plugin_settings['backup'] = ['backup_auto_storage' => 0];
  
          // Backup the activity.
          $user = get_admin();
          $controller = new \backup_controller(
              \backup::TYPE_1ACTIVITY,
              $cm->id,
              \backup::FORMAT_MOODLE,
              \backup::INTERACTIVE_NO,
              \backup::MODE_AUTOMATED,
              $user->id
          );
          $controller->execute_plan();
  
          // We don't need the forced setting anymore, hence unsetting it.
          // TODO: Remove this as part of MDL-65228.
          unset($CFG->forced_plugin_settings['backup']);
  
          // Grab the result.
          $result = $controller->get_results();
          if (!isset($result['backup_destination'])) {
              throw new \moodle_exception('Failed to backup activity prior to deletion.');
          }
  
          // Have finished with the controller, let's destroy it, freeing mem and resources.
          $controller->destroy();
  
          // Grab the filename.
          $file = $result['backup_destination'];
          if (!$file->get_contenthash()) {
              throw new \moodle_exception('Failed to backup activity prior to deletion (invalid file).');
          }
  
          // Record the activity, get an ID.
          $activity = new \stdClass();
          $activity->courseid = $cm->course;
          $activity->section = $cm->section;
          $activity->module = $cm->module;
          $activity->name = $cminfo->name;
          $activity->timecreated = time();
          $binid = $DB->insert_record('tool_recyclebin_course', $activity);
  
          // Create the location we want to copy this file to.
          $filerecord = array(
              'contextid' => \context_course::instance($this->_courseid)->id,
              'component' => 'tool_recyclebin',
              'filearea' => TOOL_RECYCLEBIN_COURSE_BIN_FILEAREA,
              'itemid' => $binid,
              'timemodified' => time()
          );
  
          // Move the file to our own special little place.
          $fs = get_file_storage();
          if (!$fs->create_file_from_storedfile($filerecord, $file)) {
              // Failed, cleanup first.
              $DB->delete_records('tool_recyclebin_course', array(
                  'id' => $binid
              ));
  
              throw new \moodle_exception("Failed to copy backup file to recyclebin.");
          }
  
          // Delete the old file.
          $file->delete();
  
          // Fire event.
          $event = \tool_recyclebin\event\course_bin_item_created::create(array(
              'objectid' => $binid,
              'context' => \context_course::instance($cm->course)
          ));
          $event->trigger();
      }
code: public function store_item($cm) {
          global $CFG, $DB;
  
          require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  
          // Get more information.
          $modinfo = get_fast_modinfo($cm->course);
  
          if (!isset($modinfo->cms[$cm->id])) {
              return; // Can't continue without the module information.
          }
  
          $cminfo = $modinfo->cms[$cm->id];
  
          // Check backup/restore support.
          if (!plugin_supports('mod', $cminfo->modname , FEATURE_BACKUP_MOODLE2)) {
              return;
          }
  
          // As far as recycle bin is using MODE_AUTOMATED, it observes the backup_auto_storage
          // setting (storing backups @ real location. For recycle bin we want to ensure that
          // backup files are always stored in Moodle file area. In order to achieve that, we
          // hack the setting here via $CFG->forced_plugin_settings, so it won't interfere other
          // operations.  See MDL-65218 for more information.
          // This hack will be removed once recycle bin switches to use its own backup mode, with
          // own preferences and 100% appart from MODLE_AUTOMATED.
          // TODO: Remove this as part of MDL-65228.
          $CFG->forced_plugin_settings['backup'] = ['backup_auto_storage' => 0];
  
          // Backup the activity.
          $user = get_admin();
          $controller = new \backup_controller(
              \backup::TYPE_1ACTIVITY,
              $cm->id,
              \backup::FORMAT_MOODLE,
              \backup::INTERACTIVE_NO,
              \backup::MODE_AUTOMATED,
              $user->id
          );
          $controller->execute_plan();
  
          // We don't need the forced setting anymore, hence unsetting it.
          // TODO: Remove this as part of MDL-65228.
          unset($CFG->forced_plugin_settings['backup']);
  
          // Grab the result.
          $result = $controller->get_results();
          if (!isset($result['backup_destination'])) {
              throw new \moodle_exception('Failed to backup activity prior to deletion.');
          }
  
          // Have finished with the controller, let's destroy it, freeing mem and resources.
          $controller->destroy();
  
          // Grab the filename.
          $file = $result['backup_destination'];
          if (!$file->get_contenthash()) {
              throw new \moodle_exception('Failed to backup activity prior to deletion (invalid file).');
          }
  
          // Record the activity, get an ID.
          $activity = new \stdClass();
          $activity->courseid = $cm->course;
          $activity->section = $cm->section;
          $activity->module = $cm->module;
          $activity->name = $cminfo->name;
          $activity->timecreated = time();
          $binid = $DB->insert_record('tool_recyclebin_course', $activity);
  
          // Create the location we want to copy this file to.
          $filerecord = array(
              'contextid' => \context_course::instance($this->_courseid)->id,
              'component' => 'tool_recyclebin',
              'filearea' => TOOL_RECYCLEBIN_COURSE_BIN_FILEAREA,
              'itemid' => $binid,
              'timemodified' => time()
          );
  
          // Move the file to our own special little place.
          $fs = get_file_storage();
          if (!$fs->create_file_from_storedfile($filerecord, $file)) {
              // Failed, cleanup first.
              $DB->delete_records('tool_recyclebin_course', array(
                  'id' => $binid
              ));
  
              throw new \moodle_exception("Failed to copy backup file to recyclebin.");
          }
  
          // Delete the old file.
          $file->delete();
  
          // Fire event.
          $event = \tool_recyclebin\event\course_bin_item_created::create(array(
              'objectid' => $binid,
              'context' => \context_course::instance($cm->course)
          ));
          $event->trigger();
      }
code_tokens: public function store_item ( $ cm ) { global $ CFG , $ DB ; require_once ( $ CFG -> dirroot . '/backup/util/includes/backup_includes.php' ) ; // Get more information. $ modinfo = get_fast_modinfo ( $ cm -> course ) ; if ( ! isset ( $ modinfo -> cms [ $ cm -> id ] ) ) { return ; // Can't continue without the module information. } $ cminfo = $ modinfo -> cms [ $ cm -> id ] ; // Check backup/restore support. if ( ! plugin_supports ( 'mod' , $ cminfo -> modname , FEATURE_BACKUP_MOODLE2 ) ) { return ; } // As far as recycle bin is using MODE_AUTOMATED, it observes the backup_auto_storage // setting (storing backups @ real location. For recycle bin we want to ensure that // backup files are always stored in Moodle file area. In order to achieve that, we // hack the setting here via $CFG->forced_plugin_settings, so it won't interfere other // operations.  See MDL-65218 for more information. // This hack will be removed once recycle bin switches to use its own backup mode, with // own preferences and 100% appart from MODLE_AUTOMATED. // TODO: Remove this as part of MDL-65228. $ CFG -> forced_plugin_settings [ 'backup' ] = [ 'backup_auto_storage' => 0 ] ; // Backup the activity. $ user = get_admin ( ) ; $ controller = new \ backup_controller ( \ backup :: TYPE_1ACTIVITY , $ cm -> id , \ backup :: FORMAT_MOODLE , \ backup :: INTERACTIVE_NO , \ backup :: MODE_AUTOMATED , $ user -> id ) ; $ controller -> execute_plan ( ) ; // We don't need the forced setting anymore, hence unsetting it. // TODO: Remove this as part of MDL-65228. unset ( $ CFG -> forced_plugin_settings [ 'backup' ] ) ; // Grab the result. $ result = $ controller -> get_results ( ) ; if ( ! isset ( $ result [ 'backup_destination' ] ) ) { throw new \ moodle_exception ( 'Failed to backup activity prior to deletion.' ) ; } // Have finished with the controller, let's destroy it, freeing mem and resources. $ controller -> destroy ( ) ; // Grab the filename. $ file = $ result [ 'backup_destination' ] ; if ( ! $ file -> get_contenthash ( ) ) { throw new \ moodle_exception ( 'Failed to backup activity prior to deletion (invalid file).' ) ; } // Record the activity, get an ID. $ activity = new \ stdClass ( ) ; $ activity -> courseid = $ cm -> course ; $ activity -> section = $ cm -> section ; $ activity -> module = $ cm -> module ; $ activity -> name = $ cminfo -> name ; $ activity -> timecreated = time ( ) ; $ binid = $ DB -> insert_record ( 'tool_recyclebin_course' , $ activity ) ; // Create the location we want to copy this file to. $ filerecord = array ( 'contextid' => \ context_course :: instance ( $ this -> _courseid ) -> id , 'component' => 'tool_recyclebin' , 'filearea' => TOOL_RECYCLEBIN_COURSE_BIN_FILEAREA , 'itemid' => $ binid , 'timemodified' => time ( ) ) ; // Move the file to our own special little place. $ fs = get_file_storage ( ) ; if ( ! $ fs -> create_file_from_storedfile ( $ filerecord , $ file ) ) { // Failed, cleanup first. $ DB -> delete_records ( 'tool_recyclebin_course' , array ( 'id' => $ binid ) ) ; throw new \ moodle_exception ( "Failed to copy backup file to recyclebin." ) ; } // Delete the old file. $ file -> delete ( ) ; // Fire event. $ event = \ tool_recyclebin \ event \ course_bin_item_created :: create ( array ( 'objectid' => $ binid , 'context' => \ context_course :: instance ( $ cm -> course ) ) ) ; $ event -> trigger ( ) ; }
docstring: Store a course module in the recycle bin.
  
  @param \stdClass $cm Course module
  @throws \moodle_exception
docstring_tokens: Store a course module in the recycle bin .