Put Items and delete Keys by batch
@param Context\BatchWrite $context
@return null|Context\BatchWrite Return a new BatchWrite context if some request were not processed
@throws Exception\AttributesException
502412
public function batchWrite(Context\BatchWrite $context)
    {
        if (null !== $this->logger) {
            $this->log('BatchWrite');
        }

        if (0 === count($context)) {
            $message = "BatchWrite context doesn't contain anything to write";
            if (null !== $this->logger) {
                $this->log($message, Logger::ERROR);
            }
            throw new Exception\AttributesException($message);
        }

        $parameters = $context->getForDynamoDB();

        if (null !== $this->logger) {
            $this->log('BatchWrite request paramaters : '.print_r($parameters, true), Logger::DEBUG);
        }

        $response = $this->connector->batchWriteItem($parameters);

        if (null !== $this->logger) {
            $this->log('BatchWrite request response : '.print_r($response, true), Logger::DEBUG);
        }

        // UnprocessedKeys
        if (count((array)$response['UnprocessedItems'])) {
            $newContext = new Context\BatchWrite();
            foreach ($response['UnprocessedItems'] as $table => $tableParameters) {
                foreach ($tableParameters as $request) {
                    if (isset($request['DeleteRequest'])) {
                        $keys = $request['DeleteRequest']['Key'];
                        $newContext->addKeyToDelete(
                            $table,
                            current($keys['HashKeyElement']),
                            (isset($keys['RangeKeyElement'])?current($keys['RangeKeyElement']):null)
                        );
                    } elseif (isset($request['PutRequest'])) {
                        $item = new Item($table);
                        $item->populateFromDynamoDB($request['PutRequest']['Item']);
                        $newContext->addItemToPut($item);
                    }
                }
            }

            if (null !== $this->logger) {
                $this->log('More unprocessed Items');
            }
        } else {
            $newContext = null;
        }

        // Write Unit
        foreach ($response['Responses'] as $table => $responseItems) {
            $this->addConsumedWriteUnits($table, floatval($responseItems['ConsumedCapacityUnits']));
        }

        return $newContext;
    }
345743
public static function delete_data_for_all_users_in_context(\context $context) {
        global $DB;

        if ($context->contextlevel == CONTEXT_MODULE) {
            $cm = get_coursemodule_from_id('assign', $context->instanceid);
            if ($cm) {
                // Get the assignment related to this context.
                $assign = new \assign($context, null, null);
                // What to do first... Get sub plugins to delete their stuff.
                $requestdata = new assign_plugin_request_data($context, $assign);
                manager::plugintype_class_callback('assignsubmission', self::ASSIGNSUBMISSION_INTERFACE,
                    'delete_submission_for_context', [$requestdata]);
                $requestdata = new assign_plugin_request_data($context, $assign);
                manager::plugintype_class_callback('assignfeedback', self::ASSIGNFEEDBACK_INTERFACE,
                    'delete_feedback_for_context', [$requestdata]);
                $DB->delete_records('assign_grades', ['assignment' => $assign->get_instance()->id]);

                // Delete advanced grading information.
                $gradingmanager = get_grading_manager($context, 'mod_assign', 'submissions');
                $controller = $gradingmanager->get_active_controller();
                if (isset($controller)) {
                    \core_grading\privacy\provider::delete_instance_data($context);
                }

                // Time to roll my own method for deleting overrides.
                static::delete_overrides_for_users($assign);
                $DB->delete_records('assign_submission', ['assignment' => $assign->get_instance()->id]);
                $DB->delete_records('assign_user_flags', ['assignment' => $assign->get_instance()->id]);
                $DB->delete_records('assign_user_mapping', ['assignment' => $assign->get_instance()->id]);
            }
        }
    }
214919
protected function _beforeInsert(DatabaseContext $context)
    {
        $mixer = $this->getMixer();
        $table = $mixer instanceof DatabaseRowInterface ?  $mixer->getTable() : $mixer;

        if($this->hasProperty('created_by') && empty($this->created_by)) {
            $this->created_by  = (int) $this->getObject('user')->getId();
        }

        if($this->hasProperty('created_on') && (empty($this->created_on) || $this->created_on == $table->getDefault('created_on'))) {
            $this->created_on  = gmdate('Y-m-d H:i:s');
        }
    }
717653
protected function _actionDelete($context)
    {
        $context->response->status = AnHttpResponse::NO_CONTENT;
        $this->getItem()->delete();
        $context->response->setRedirect(route($this->getItem()->owner->getURL()));
    }
85874
protected function batchFactory()
    {
        return new WriteBatch(
            $this->connection,
            $this->valueMapper,
            $this->databaseFromName($this->name)
        );
    }
571836
protected function _afterEntityInsert(AnCommandContext $context)
    {
        $this->ownable->getRepository()->getBehavior('sharable')->resetStats(array($this->ownable));
    }
544459
public function commit(Context $context = null)
    {
        $this->getDBQueryBuilder($context)->go(null, 'commit');
        return $this;
    }
478623
public static function delete_data_for_all_users_in_context(\context $context) {
        global $CFG, $DB;
        require_once($CFG->libdir.'/gradelib.php');

        if ($context->contextlevel != CONTEXT_MODULE) {
            return;
        }

        $cm = get_coursemodule_from_id('workshop', $context->instanceid, 0, false, IGNORE_MISSING);

        if (!$cm) {
            // Probably some kind of expired context.
            return;
        }

        $workshop = $DB->get_record('workshop', ['id' => $cm->instance], 'id, course', MUST_EXIST);

        $submissions = $DB->get_records('workshop_submissions', ['workshopid' => $workshop->id], '', 'id');
        $assessments = $DB->get_records_list('workshop_assessments', 'submissionid', array_keys($submissions), '', 'id');

        $DB->delete_records('workshop_aggregations', ['workshopid' => $workshop->id]);
        $DB->delete_records_list('workshop_grades', 'assessmentid', array_keys($assessments));
        $DB->delete_records_list('workshop_assessments', 'id', array_keys($assessments));
        $DB->delete_records_list('workshop_submissions', 'id', array_keys($submissions));

        $fs = get_file_storage();
        $fs->delete_area_files($context->id, 'mod_workshop', 'submission_content');
        $fs->delete_area_files($context->id, 'mod_workshop', 'submission_attachment');
        $fs->delete_area_files($context->id, 'mod_workshop', 'overallfeedback_content');
        $fs->delete_area_files($context->id, 'mod_workshop', 'overallfeedback_attachment');

        grade_update('mod/workshop', $workshop->course, 'mod', 'workshop', $workshop->id, 0, null, ['reset' => true]);
        grade_update('mod/workshop', $workshop->course, 'mod', 'workshop', $workshop->id, 1, null, ['reset' => true]);

        \core_plagiarism\privacy\provider::delete_plagiarism_for_context($context);
    }
371313
public function batch()
    {
        return new WriteBatch(
            $this->connection,
            $this->valueMapper,
            $this->databaseName(
                $this->projectId,
                $this->database
            )
        );
    }
169852
public function remove($attributeCode, $value, array $context = null)
    {
        $attribute = $this->getAttribute($attributeCode);

        $method = 'remove'.ucfirst($attributeCode);
        if (method_exists($this, $method)) {
            return $this->$method($value, $context);
        }

        return $this->removeValueData($attribute, $value, $context);
    }
341318
protected function _afterEntityDelete(AnCommandContext $context)
    {
        $this->votee->getRepository()
                    ->getBehavior('votable')
                    ->resetStats(array($this->votee));
    }
179037
protected static function cache_add(context $context) {
        if (isset(self::$cache_contextsbyid[$context->id])) {
            // already cached, no need to do anything - this is relatively cheap, we do all this because count() is slow
            return;
        }

        if (self::$cache_count >= CONTEXT_CACHE_MAX_SIZE) {
            $i = 0;
            foreach(self::$cache_contextsbyid as $ctx) {
                $i++;
                if ($i <= 100) {
                    // we want to keep the first contexts to be loaded on this page, hopefully they will be needed again later
                    continue;
                }
                if ($i > (CONTEXT_CACHE_MAX_SIZE / 3)) {
                    // we remove oldest third of the contexts to make room for more contexts
                    break;
                }
                unset(self::$cache_contextsbyid[$ctx->id]);
                unset(self::$cache_contexts[$ctx->contextlevel][$ctx->instanceid]);
                self::$cache_count--;
            }
        }

        self::$cache_contexts[$context->contextlevel][$context->instanceid] = $context;
        self::$cache_contextsbyid[$context->id] = $context;
        self::$cache_count++;
    }
858615
public static function delete_data_for_all_users_in_context(\context $context) {
        global $DB;

        if (!$context instanceof \context_course) {
            return;
        }

        $DB->delete_records('enrol_paypal', array('courseid' => $context->instanceid));
    }
780284
public static function delete_data_for_all_users_in_context(context $context) {
        global $DB;
        list($sql, $params) = static::get_sql_where_from_contexts([$context]);
        if (empty($sql)) {
            return;
        }
        $DB->delete_records_select('log', $sql, $params);
    }
643225
public static function delete_data_for_all_users_in_context(\context $context) {
        global $DB;

        if ($context instanceof \context_course) {
            $sectionsql = "itemid IN (SELECT id FROM {course_sections} WHERE course = ?) AND type = ?";
            $DB->delete_records_select('backup_controllers', $sectionsql, [$context->instanceid, \backup::TYPE_1SECTION]);
            $DB->delete_records('backup_controllers', ['itemid' => $context->instanceid, 'type' => \backup::TYPE_1COURSE]);
        }
        if ($context instanceof \context_module) {
            $DB->delete_records('backup_controllers', ['itemid' => $context->instanceid, 'type' => \backup::TYPE_1ACTIVITY]);
        }
        return;
    }
1279242
public function deleteContext($context) {
        global $DB;

        $contextid = $context->getRecordId();

        $params = ['id' => $contextid];

        // Delete any outstanding share keys for resource links for this context.
        $where = "resourcelinkid IN (
                    SELECT rl.id
                      FROM {{$this->resourcelinktable}} rl
                     WHERE rl.contextid = :id
               )";
        $DB->delete_records_select($this->sharekeytable, $where, $params);

        // Delete any users in resource links for this context.
        $DB->delete_records_select($this->userresulttable, $where, $params);

        // Update any resource links for which this consumer is acting as a primary resource link.
        $where = "primaryresourcelinkid IN (
                    SELECT rl.id
                      FROM {{$this->resourcelinktable}} rl
                     WHERE rl.contextid = :id
               )";
        $updaterecords = $DB->get_records_select($this->resourcelinktable, $where, $params);
        foreach ($updaterecords as $record) {
            $record->primaryresourcelinkid = null;
            $record->shareapproved = null;
            $DB->update_record($this->resourcelinktable, $record);
        }

        // Delete any resource links for this context.
        $DB->delete_records($this->resourcelinktable, ['contextid' => $contextid]);

        // Delete context.
        $DB->delete_records($this->contexttable, $params);

        $context->initialize();

        return true;
    }
822143
protected function batchCanConvert(Batch $batch)
    {
        return ($this->schema->hasConverter($batch->getAttribute()) && !$batch->isTypeRemoveAll());
    }