Process a lesson override restore
@param object $data The data in object form
@return void
184884
protected function process_lesson_override($data) {
        global $DB;

        $data = (object)$data;
        $oldid = $data->id;

        // Based on userinfo, we'll restore user overides or no.
        $userinfo = $this->get_setting_value('userinfo');

        // Skip user overrides if we are not restoring userinfo.
        if (!$userinfo && !is_null($data->userid)) {
            return;
        }

        $data->lessonid = $this->get_new_parentid('lesson');

        if (!is_null($data->userid)) {
            $data->userid = $this->get_mappingid('user', $data->userid);
        }
        if (!is_null($data->groupid)) {
            $data->groupid = $this->get_mappingid('group', $data->groupid);
        }

        $data->available = $this->apply_date_offset($data->available);
        $data->deadline = $this->apply_date_offset($data->deadline);

        $newitemid = $DB->insert_record('lesson_overrides', $data);

        // Add mapping, restore of logs needs it.
        $this->set_mapping('lesson_override', $oldid, $newitemid);
    }
83673
protected function process_ltitype($data) {
        global $DB, $USER;

        $data = (object)$data;
        $oldid = $data->id;
        if (!empty($data->createdby)) {
            $data->createdby = $this->get_mappingid('user', $data->createdby) ?: $USER->id;
        }

        $courseid = $this->get_courseid();
        $data->course = ($this->get_mappingid('course', $data->course) == $courseid) ? $courseid : SITEID;

        // Try to find existing lti type with the same properties.
        $ltitypeid = $this->find_existing_lti_type($data);

        $this->newltitype = false;
        if (!$ltitypeid && $data->course == $courseid) {
            unset($data->toolproxyid); // Course tools can not use LTI2.
            $ltitypeid = $DB->insert_record('lti_types', $data);
            $this->newltitype = true;
            $this->set_mapping('ltitype', $oldid, $ltitypeid);
        }

        // Add the typeid entry back to LTI module.
        $DB->update_record('lti', ['id' => $this->get_new_parentid('lti'), 'typeid' => $ltitypeid]);
    }
270994
public function restore(string $data, int $timeout = 0): bool
    {
        return $this->handle->restore($this->name, $timeout, $data);
    }
684842
public function process_course_completion_crit_compl($data) {
        global $DB;

        $data = (object)$data;

        // This may be empty if criteria could not be restored
        $data->criteriaid = $this->get_mappingid('course_completion_criteria', $data->criteriaid);

        $data->course = $this->get_courseid();
        $data->userid = $this->get_mappingid('user', $data->userid);

        if (!empty($data->criteriaid) && !empty($data->userid)) {
            $params = array(
                'userid' => $data->userid,
                'course' => $data->course,
                'criteriaid' => $data->criteriaid,
                'timecompleted' => $data->timecompleted
            );
            if (isset($data->gradefinal)) {
                $params['gradefinal'] = $data->gradefinal;
            }
            if (isset($data->unenroled)) {
                $params['unenroled'] = $data->unenroled;
            }
            $DB->insert_record('course_completion_crit_compl', $params);
        }
    }
1354770
protected function process_assign_grade($data) {
        global $DB;

        $data = (object)$data;
        $oldid = $data->id;

        $data->assignment = $this->get_new_parentid('assign');

        $data->userid = $this->get_mappingid('user', $data->userid);
        $data->grader = $this->get_mappingid('user', $data->grader);

        // Handle flags restore to a different table (for upgrade from old backups).
        if (!empty($data->extensionduedate) ||
                !empty($data->mailed) ||
                !empty($data->locked)) {
            $flags = new stdClass();
            $flags->assignment = $this->get_new_parentid('assign');
            if (!empty($data->extensionduedate)) {
                $flags->extensionduedate = $this->apply_date_offset($data->extensionduedate);
            }
            if (!empty($data->mailed)) {
                $flags->mailed = $data->mailed;
            }
            if (!empty($data->locked)) {
                $flags->locked = $data->locked;
            }
            $flags->userid = $this->get_mappingid('user', $data->userid);
            $DB->insert_record('assign_user_flags', $flags);
        }
        // Fix null grades that were rescaled.
        if ($data->grade < 0 && $data->grade != ASSIGN_GRADE_NOT_SET) {
            $data->grade = ASSIGN_GRADE_NOT_SET;
        }
        $newitemid = $DB->insert_record('assign_grades', $data);

        // Note - the old contextid is required in order to be able to restore files stored in
        // sub plugin file areas attached to the gradeid.
        $this->set_mapping('grade', $oldid, $newitemid, false, null, $this->task->get_old_contextid());
        $this->set_mapping(restore_gradingform_plugin::itemid_mapping('submissions'), $oldid, $newitemid);
    }
463569
public static function group_deleted($event) {
        if (!empty(self::$resetinprogress)) {
            // We will take care of that once the course reset ends.
            return;
        }
        lesson_process_group_deleted_in_course($event->courseid, $event->objectid);
    }
603820
protected function restoreState(array $data)
    {
        if (isset($data['status'])) {
            $this->status = array_merge($this->status, $data['status']);
        }
    
        if (isset($data['jobUuid'])) {
            $this->jobUuid = $data['jobUuid'];
            $this->logContext['jobUuid'] = $data['jobUuid'];
        } else {
            $this->jobUuid = null;
            $this->logContext['jobUuid'] = null;
        }
    }
863899
public function data_postprocessing($data) {
        parent::data_postprocessing($data);
        if (!empty($data->completionunlocked)) {
            // Turn off completion settings if the checkboxes aren't ticked.
            $autocompletion = !empty($data->completion) &&
                $data->completion == COMPLETION_TRACKING_AUTOMATIC;
            if (!$autocompletion || empty($data->completionsubmit)) {
                $data->completionsubmit = 0;
            }
        }
    }
143812
public function restoreFromState($stateData)
    {
        $publicProperties = $this->getExposableModelProperties();

        foreach ($publicProperties as $key) {
            if (isset($stateData[$key])) {
                $this->$key = $stateData[$key];
            }
        }
    }
968575
public function process_lesson_answer($data) {

        // replay the upgrade step 2010072003
        $flags = intval($data['flags']);
        if ($flags & 1) {
            $data['answer_text']  = text_to_html($data['answer_text'], false, false, true);
            $data['answerformat'] = FORMAT_HTML;
        }
        if ($flags & 2) {
            $data['response']       = text_to_html($data['response'], false, false, true);
            $data['responseformat'] = FORMAT_HTML;
        }

        // buffer for conversion of <jumpto> in line with
        // upgrade step 2010121400 from mod/lesson/db/upgrade.php
        $this->answers[] = $data;
    }
1068920
public function process_lesson($data) {

        // get the course module id and context id
        $instanceid     = $data['id'];
        $cminfo         = $this->get_cminfo($instanceid);
        $this->moduleid = $cminfo['id'];
        $contextid      = $this->converter->get_contextid(CONTEXT_MODULE, $this->moduleid);

        // get a fresh new file manager for this instance
        $this->fileman = $this->converter->get_file_manager($contextid, 'mod_lesson');

        // migrate referenced local media files
        if (!empty($data['mediafile']) and strpos($data['mediafile'], '://') === false) {
            $this->fileman->filearea = 'mediafile';
            $this->fileman->itemid   = 0;
            try {
                $this->fileman->migrate_file('course_files/'.$data['mediafile']);
            } catch (moodle1_convert_exception $e) {
                // the file probably does not exist
                $this->log('error migrating lesson mediafile', backup::LOG_WARNING, 'course_files/'.$data['mediafile']);
            }
        }

        // start writing lesson.xml
        $this->open_xml_writer("activities/lesson_{$this->moduleid}/lesson.xml");
        $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $this->moduleid,
            'modulename' => 'lesson', 'contextid' => $contextid));
        $this->xmlwriter->begin_tag('lesson', array('id' => $instanceid));

        foreach ($data as $field => $value) {
            if ($field <> 'id') {
                $this->xmlwriter->full_tag($field, $value);
            }
        }

        return $data;
    }
702401
protected function handleResult($data)
    {
        if (empty($data->data)) {
            $data->data = new stdClass();
        }
        if ($data->http_code !== 200) {
            $data->data->is_error = true;
            $data->data->http_code = $data->http_code;
        }

        return $data->data;
    }
727728
public function override($data, $rules)
    {
        $this->setData($data);
        $this->setRules($rules);
        $this->setCustomMessages($this->overrideCustomMessages);
    }
1091655
public function process($data) {
        $bulk = new bulk_insert($this->item);
        // Bulk insert messages the data to be passed in
        // ie: for all grades of empty grades apply the specified value.
        if ($bulk->is_applied($data)) {
            $filter = $bulk->get_type($data);
            $insertvalue = $bulk->get_insert_value($data);

            $userid = $this->item->id;
            foreach ($this->items as $gradeitemid => $gradeitem) {
                $null = $gradeitem->gradetype == GRADE_TYPE_SCALE ? -1 : '';
                $field = "finalgrade_{$gradeitem->id}_{$this->itemid}";
                if (isset($data->$field)) {
                    continue;
                }

                $oldfinalgradefield = "oldfinalgrade_{$gradeitem->id}_{$this->itemid}";
                // Bulk grade changes for all grades need to be processed and shouldn't be skipped if they had a previous grade.
                if ($gradeitem->is_course_item() || ($filter != 'all' && !empty($data->$oldfinalgradefield))) {
                    if ($gradeitem->is_course_item()) {
                        // The course total should not be overridden.
                        unset($data->$field);
                        unset($data->oldfinalgradefield);
                        $oldoverride = "oldoverride_{$gradeitem->id}_{$this->itemid}";
                        unset($data->$oldoverride);
                        $oldfeedback = "oldfeedback_{$gradeitem->id}_{$this->itemid}";
                        unset($data->$oldfeedback);
                    }
                    continue;
                }
                $grade = grade_grade::fetch(array(
                    'itemid' => $gradeitemid,
                    'userid' => $userid
                ));

                $data->$field = empty($grade) ? $null : $grade->finalgrade;
                $data->{"old$field"} = $data->$field;
            }

            foreach ($data as $varname => $value) {
                if (preg_match('/^oldoverride_(\d+)_(\d+)/', $varname, $matches)) {
                    // If we've selected overriding all grades.
                    if ($filter == 'all') {
                        $override = "override_{$matches[1]}_{$matches[2]}";
                        $data->$override = '1';
                    }
                }
                if (!preg_match('/^finalgrade_(\d+)_(\d+)/', $varname, $matches)) {
                    continue;
                }

                $gradeitem = grade_item::fetch(array(
                    'courseid' => $this->courseid,
                    'id' => $matches[1]
                ));

                $isscale = ($gradeitem->gradetype == GRADE_TYPE_SCALE);

                $empties = (trim($value) === '' or ($isscale and $value == -1));

                if ($filter == 'all' or $empties) {
                    $data->$varname = ($isscale and empty($insertvalue)) ?
                        -1 : $insertvalue;
                }
            }
        }
        return parent::process($data);
    }
470538
public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) {
        global $DB;

        if (!$DB->record_exists('enrol', array('courseid' => $data->courseid, 'enrol' => $this->get_name()))) {
            $this->add_instance($course, (array)$data);
        }

        // No need to set mapping, we do not restore users or roles here.
        $step->set_mapping('enrol', $oldid, 0);
    }
80139
public function process_enrol($data) {
        global $DB;

        $data = (object)$data;
        $oldid = $data->id; // We'll need this later.
        unset($data->id);

        $this->originalstatus[$oldid] = $data->status;

        if (!$courserec = $DB->get_record('course', array('id' => $this->get_courseid()))) {
            $this->set_mapping('enrol', $oldid, 0);
            return;
        }

        if (!isset($this->plugins)) {
            $this->plugins = enrol_get_plugins(true);
        }

        if (!$this->enrolsynced) {
            // Make sure that all plugin may create instances and enrolments automatically
            // before the first instance restore - this is suitable especially for plugins
            // that synchronise data automatically using course->idnumber or by course categories.
            foreach ($this->plugins as $plugin) {
                $plugin->restore_sync_course($courserec);
            }
            $this->enrolsynced = true;
        }

        // Map standard fields - plugin has to process custom fields manually.
        $data->roleid   = $this->get_mappingid('role', $data->roleid);
        $data->courseid = $courserec->id;

        if (!$this->get_setting_value('users') && $this->get_setting_value('enrolments') == backup::ENROL_WITHUSERS) {
            $converttomanual = true;
        } else {
            $converttomanual = ($this->get_setting_value('enrolments') == backup::ENROL_NEVER);
        }

        if ($converttomanual) {
            // Restore enrolments as manual enrolments.
            unset($data->sortorder); // Remove useless sortorder from <2.4 backups.
            if (!enrol_is_enabled('manual')) {
                $this->set_mapping('enrol', $oldid, 0);
                return;
            }
            if ($instances = $DB->get_records('enrol', array('courseid'=>$data->courseid, 'enrol'=>'manual'), 'id')) {
                $instance = reset($instances);
                $this->set_mapping('enrol', $oldid, $instance->id);
            } else {
                if ($data->enrol === 'manual') {
                    $instanceid = $this->plugins['manual']->add_instance($courserec, (array)$data);
                } else {
                    $instanceid = $this->plugins['manual']->add_default_instance($courserec);
                }
                $this->set_mapping('enrol', $oldid, $instanceid);
            }

        } else {
            if (!enrol_is_enabled($data->enrol) or !isset($this->plugins[$data->enrol])) {
                $this->set_mapping('enrol', $oldid, 0);
                $message = "Enrol plugin '$data->enrol' data can not be restored because it is not enabled, consider restoring without enrolment methods";
                $this->log($message, backup::LOG_WARNING);
                return;
            }
            if ($task = $this->get_task() and $task->get_target() == backup::TARGET_NEW_COURSE) {
                // Let's keep the sortorder in old backups.
            } else {
                // Prevent problems with colliding sortorders in old backups,
                // new 2.4 backups do not need sortorder because xml elements are ordered properly.
                unset($data->sortorder);
            }
            // Note: plugin is responsible for setting up the mapping, it may also decide to migrate to different type.
            $this->plugins[$data->enrol]->restore_instance($this, $data, $courserec, $oldid);
        }
    }
782928
public function process(DataObjectInterface $data = null)
    {
        if ( ! is_null($data)) {
            $this->data = $data;
        }

        $this->before();

        $this->doProcessing();

        $this->after();

        return $this->getResult();
    }