Updates the value in database and returns itself, called from inplace_editable callback

@param int $itemid
@param mixed $newvalue
@return \self
432419
public static function update($itemid, $newvalue) {
        global $DB, $CFG;

        require_once($CFG->libdir . '/external/externallib.php');
        // Check caps.
        // Do the thing.
        // Return one of me.
        // Validate the inputs.
        list($courseid, $userid) = explode(':', $itemid, 2);

        $courseid = clean_param($courseid, PARAM_INT);
        $userid = clean_param($userid, PARAM_INT);
        $roleids = json_decode($newvalue);
        foreach ($roleids as $index => $roleid) {
            $roleids[$index] = clean_param($roleid, PARAM_INT);
        }

        // Check user is enrolled in the course.
        $context = context_course::instance($courseid);
        core_external::validate_context($context);

        // Check permissions.
        require_capability('moodle/role:assign', $context);

        if (!is_enrolled($context, $userid)) {
            throw new coding_exception('User does not belong to the course');
        }

        // Check that all the groups belong to the course.
        $allroles = role_fix_names(get_all_roles($context), $context);
        $assignableroles = get_assignable_roles($context, ROLENAME_ALIAS, false);
        $viewableroles = get_viewable_roles($context);
        $userrolesbyid = get_user_roles($context, $userid, true, 'c.contextlevel DESC, r.sortorder ASC');
        $profileroles = get_profile_roles($context);

        // Set an array where the index is the roleid.
        $userroles = array();
        foreach ($userrolesbyid as $id => $role) {
            $userroles[$role->roleid] = $role;
        }

        $rolestoprocess = [];
        foreach ($roleids as $roleid) {
            if (!isset($assignableroles[$roleid])) {
                throw new coding_exception('Role cannot be assigned in this course.');
            }
            $rolestoprocess[$roleid] = $roleid;
        }

        // Process adds.
        foreach ($rolestoprocess as $roleid) {
            if (!isset($userroles[$roleid])) {
                // Add them.
                $id = role_assign($roleid, $userid, $context);
                // Keep this variable in sync.
                $role = new \stdClass();
                $role->id = $id;
                $role->roleid = $roleid;
                $role->contextid = $context->id;
                $userroles[$role->roleid] = $role;
            }
        }

        // Process removals.
        foreach ($assignableroles as $roleid => $rolename) {
            if (isset($userroles[$roleid]) && !isset($rolestoprocess[$roleid])) {
                // Do not remove the role if we are not in the same context.
                if ($userroles[$roleid]->contextid != $context->id) {
                    continue;
                }
                $ras = $DB->get_records('role_assignments', ['contextid' => $context->id, 'userid' => $userid,
                    'roleid' => $roleid]);
                $allremoved = true;
                foreach ($ras as $ra) {
                    if ($ra->component) {
                        if (strpos($ra->component, 'enrol_') !== 0) {
                            continue;
                        }
                        if (!$plugin = enrol_get_plugin(substr($ra->component, 6))) {
                            continue;
                        }
                        if ($plugin->roles_protected()) {
                            $allremoved = false;
                            continue;
                        }
                    }
                    role_unassign($ra->roleid, $ra->userid, $ra->contextid, $ra->component, $ra->itemid);
                }
                if ($allremoved) {
                    unset($userroles[$roleid]);
                }
            }
        }

        $course = get_course($courseid);
        $user = core_user::get_user($userid);
        return new self($course, $context, $user, $allroles, $assignableroles, $profileroles, $userroles, $viewableroles);
    }
728727
public function update($newValues)
    {
        foreach ($newValues as $key => $value) {
            $this->offsetSet($key, $value);
        }

        return $this;
    }
1345072
public function setMediotecaItemId(int $nb_medioteca_item_id) : CNabuDataObject
    {
        if ($nb_medioteca_item_id === null) {
            throw new ENabuCoreException(
                    ENabuCoreException::ERROR_NULL_VALUE_NOT_ALLOWED_IN,
                    array("\$nb_medioteca_item_id")
            );
        }
        $this->setValue('nb_medioteca_item_id', $nb_medioteca_item_id);
        
        return $this;
    }
693335
public function replace($oldValue, $newValue)
    {
        if (!is_string($oldValue)) {
            throw new UnexpectedTypeException($oldValue, 'string', 'oldValue');
        }
        if (empty($oldValue)) {
            return;
        }
        if (!is_string($newValue) && null !== $newValue) {
            throw new UnexpectedTypeException($newValue, 'string or null', 'newValue');
        }

        $key = array_search($oldValue, $this->values, true);
        if (false !== $key) {
            if (empty($newValue)) {
                unset($this->values[$key]);
            } else {
                $this->values[$key] = $newValue;
            }
        }
    }
1334471
public function removeByID($itemID)
    {
        if (!is_numeric($itemID)) {
            throw new InvalidArgumentException("ManyManyList::removeById() expecting an ID");
        }

        $query = SQLDelete::create("\"{$this->joinTable}\"");

        if ($filter = $this->foreignIDWriteFilter($this->getForeignID())) {
            $query->setWhere($filter);
        } else {
            user_error("Can't call ManyManyList::remove() until a foreign ID is set");
        }

        $query->addWhere([
            "\"{$this->joinTable}\".\"{$this->localKey}\"" => $itemID
        ]);
        $query->execute();
    }
1060877
public function update(array &$itemObj)
    {
        if (isset($itemObj['id'])) {
            $field = Craft::$app->fields->getFieldById($itemObj['id']);
        } else {
            $field = Craft::$app->fields->getFieldByHandle($itemObj['handle']);
            if ($field) {
                $itemObj['id'] = $field->id;
            }
        }
        if ($field) {
            if ($itemObj['type'] !== \get_class($field)) {
                $errors = [
                    'type' => [
                        Architect::t('Type does not match existing type: "{fieldType}".', ['fieldType' => \get_class($field) ])
                    ]
                ];
                return $errors;
            }
            switch ($itemObj['type']) {
                case Matrix::class:
                    /* @var Matrix $field */
                    $itemObj['blockTypes'] = $this->mergeBlockTypes($field->getBlockTypes(), $itemObj['blockTypes']);
                    break;
                case SuperTableField::class:
                    /* @var SuperTableField $field */
                    $itemObj['blockTypes'] = $this->mergeSuperTableBlockTypes($field->getBlockTypes(), $itemObj['blockTypes']);
                    break;
                case Neo::class:
                    /* @var Neo $field */
                    $itemObj['blockTypes'] = $this->mergeNeoBlockTypes($field->getBlockTypes(), $itemObj['blockTypes']);
                    break;
            }
        }
        return null;
    }
721313
function set($item, $value)
    {
        if (isset($this->$item)) {
            $this->$item = $value;
            return $this;
        }
        return false;
    }
1259572
public static function update($itemid, $newvalue) {
        global $DB;
        require_capability('moodle/tag:manage', context_system::instance());
        $tagarea = $DB->get_record('tag_area', array('id' => $itemid), '*', MUST_EXIST);
        $newvalue = $newvalue ? 1 : 0;
        $data = array('enabled' => $newvalue);
        \core_tag_area::update($tagarea, $data);
        $tagarea->enabled = $newvalue;
        $tmpl = new self($tagarea);
        return $tmpl;
    }
49489
public function setItemListElements($itemListElements) {
		if (!is_array($itemListElements)) {
		    throw new \Exception('The value is expected to be an array');
		}
		foreach ($itemListElements as $itemListElement) {
		    if (!$itemListElement instanceof Property\ItemListElement) {
		        throw new \Exception('Unexpected value type');
		    }
		}
		$this->itemListElement = $itemListElements;

		return $this;
	}
873688
public function updateItem($itemId, $data, $user = null, $locale = null)
    {
        $cart = $this->getUserCart($user, $locale);
        $userId = $user ? $user->getId() : null;

        $this->validateOrCreateAddresses($cart, $user);

        $item = $this->orderManager->getOrderItemById($itemId, $cart->getEntity());

        $this->orderManager->updateItem($item, $data, $locale, $userId);

        $this->removeOrderAddressIfContactAddressIdIsEqualTo(
            $item,
            $cart->getDeliveryAddress()->getContactAddress()->getId()
        );

        $this->orderManager->updateApiEntity($cart, $locale);

        return $cart;
    }
404761
public function updateValue($group, $variableCode, $newValue)
    {
        $variable = clone $this->getVariable($variableCode);
        $variable->setValue($newValue);

        if (!isset($this->queuedForUpdate[$group->getName()])) {
            $this->queuedForUpdate[$group->getName()]['time'] = microtime(true);
        }

        $checkVariable = clone $this->checkVariable;
        $uniqueId = uniqid('exp_', true);
        $checkVariable->setValue("\"$uniqueId\"");

        $this->checkWindow->setValue(Builder::escapeText(get_called_class()));
        $this->queuedForUpdate[$group->getName()]['group'] = $group;
        $this->queuedForUpdate[$group->getName()]['variables'][$variableCode] = $variable;
        $this->queuedForUpdate[$group->getName()]['check'] = $checkVariable;
    }
1082230
public static function update($itemid, $newvalue) {
        global $DB;
        require_capability('moodle/tag:manage', context_system::instance());
        $tagcoll = $DB->get_record('tag_coll', array('id' => $itemid), '*', MUST_EXIST);
        \core_tag_collection::update($tagcoll, array('name' => $newvalue));
        return new self($tagcoll);
    }
1276120
public function setItem(Item $item)
    {
        $this->childOf = $item->id;
        $this->_item = $item;
        
        return $this;
    }
1077092
function (newValue) {
            if (newValue == null) {
                // a null value is allowed for the selectedIndex property, but it cannot be used to set the property
                // as it means several items are selected and we do not know which ones
                // just ignore the null value
                return;
            }
            var items = this._data.items;
            if (newValue < 0 || newValue >= items.length) {
                // unselect all values in case newValue is -1 or invalid
                this.setSelectedValues([]);
            } else {
                this.setSelectedValues([items[newValue].value]);
            }
        }
1059871
public function get_mappingid($itemname, $oldid, $ifnotfound = false) {
        $mapping = $this->get_mapping($itemname, $oldid);
        return $mapping ? $mapping->newitemid : $ifnotfound;
    }
115916
public function hasValueChanged($newValue, DataContainer $dc): bool
    {
        if (null !== ($entity = $this->findModelInstanceByPk($dc->table, $dc->id))) {
            return $newValue != $entity->{$dc->field};
        }

        return true;
    }
669169
public function build($itemName = '', $itemValue = '')
    {
        // Create hidden checkbox
        $fi = new FormInput($this->getHTMLDocument(), 'checkbox', $itemName, $id = '', $class = 'swt_chk', $itemValue, $required = false);
        $this->append($fi);

        // Set checked value for checkbox
        if ($this->active) {
            $fi->attr('checked', 'checked');
        }

        // Add switch
        $switch = $this->getHTMLDocument()->create('div', '', '', 'switch');
        $this->append($switch);

        return $this;
    }