public function mode_editgroup()

in classes/page/groupcp.php [218:366]


    public function mode_editgroup() {
        global $DB, $PAGE, $CFG, $OUTPUT;
        require_once($CFG->dirroot.'/lib/formslib.php');

        $courseid = optional_param('courseid', 0, PARAM_INT);
        $id = optional_param('id', 0, PARAM_INT);
        $groupid = optional_param('groupid', 0, PARAM_INT);
        $delete = optional_param('delete', 0, PARAM_BOOL);
        $confirm = optional_param('confirm', 0, PARAM_BOOL);

        if (!empty($id)) {
            $this->set_url(new \moodle_url('/local/o365/groupcp.php', ['action' => 'editgroup', 'id' => $id]));
            $group = $DB->get_record('local_o365_coursegroupdata', ['id' => $id]);
            if (empty($group)) {
                print_error('invalidgroupid');
            }
            if (empty($courseid)) {
                $courseid = $group->courseid;
            } else if ($courseid != $group->courseid) {
                print_error('invalidcourseid');
            }

            $course = $DB->get_record('course', ['id' => $courseid]);
            if (empty($course)) {
                print_error('invalidcourseid');
            }
            if (\local_o365\feature\usergroups\utils::course_is_group_enabled($course->id) !== true) {
                print_error('groups_notenabledforcourse', 'local_o365');
            }
        } else if (!empty($courseid)) {
            $this->set_url(new \moodle_url('/local/o365/groupcp.php', ['action' => 'editgroup', 'courseid' => $courseid]));
            $course = $DB->get_record('course', ['id' => $courseid]);
            if (empty($course) || $courseid == SITEID) {
                print_error('invalidcourseid');
            }
            if (\local_o365\feature\usergroups\utils::course_is_group_enabled($course->id) !== true) {
                print_error('groups_notenabledforcourse', 'local_o365');
            }

            $filter = [
                'courseid' => $course->id,
                'groupid' => 0
            ];
            $subtype = 'course';
            $moodleid = $course->id;
            if ($groupid) {
                $subtype = 'usergroup';
                $filter['groupid'] = $groupid;
                $moodleid = $groupid;
            }
            $group = $DB->get_record('local_o365_coursegroupdata', $filter);
            if (empty($group)) {
                $group = \local_o365\feature\usergroups\utils::create_coursegroupdata($course->id, $groupid);
                $id = \local_o365\feature\usergroups\utils::create_group($group);
                $group->id = $id;
            }
        } else {
            print_error('invalidgroupid');
        }

        $this->set_context(\context_course::instance($course->id));
        require_login($course);

        if (empty($group)) {
            print_error('invalidgroupid');
        }

        // Check if user has capability to manage the course group.
        $context = \context_course::instance($course->id);
        require_capability('local/o365:managegroups', $context);
        if (!empty($group->groupid)) {
            // If managing a Moodle course group require access to groups.
            require_capability('moodle/course:managegroups', $context);
        }

        $PAGE->set_pagelayout('admin');
        $this->set_title($course->fullname.': '.get_string('groups', 'local_o365'));

        // Prepare the description editor.
        $editoroptions = [
            'maxfiles' => EDITOR_UNLIMITED_FILES,
            'maxbytes' => $course->maxbytes,
            'trust' => false,
            'context' => $context,
            'noclean' => true
        ];

        $component = 'local_o365';
        if (!empty($group->groupid)) {
            $editorid = $group->id;
            $component = 'group';
            $editorid = $group->groupid;
            // For study groups/Moodle groups the groups table is authority.
            $moodlegroup = $DB->get_record('groups', ['id' => $group->groupid]);
            $group->description = $moodlegroup->description;
            $group->descriptionformat = $moodlegroup->descriptionformat;
        } else if (!empty($group->id)) {
            $editorid = $group->id;
        } else {
            $editorid = null;
        }

        if (!empty($editorid)) {
            $editoroptions['subdirs'] = file_area_contains_subdirs($context, $component, 'description', $editorid);
            $group = file_prepare_standard_editor($group, 'description', $editoroptions, $context, $component, 'description',
                $editorid);
        } else {
            $editoroptions['subdirs'] = false;
            $group = file_prepare_standard_editor($group, 'description', $editoroptions, $context, $component, 'description',
                null);
        }

        // First create the form.
        $editform = new \local_o365\form\groupedit($this->url, ['editoroptions' => $editoroptions]);
        $editform->set_data($group);

        $returnurlparams = ['action' => 'editgroup', 'courseid' => $group->courseid, 'groupid' => $group->groupid];
        $returnurl = new \moodle_url('/local/o365/groupcp.php', $returnurlparams);
        if ($editform->is_cancelled()) {
            redirect($returnurl);
        } else if ($data = $editform->get_data()) {
            if ($data->id) {
                \local_o365\feature\usergroups\utils::update_group($data, $editform, $editoroptions);
            } else {
                $id = \local_o365\feature\usergroups\utils::create_group($data, $editform, $editoroptions);
                $group = $DB->get_record('local_o365_coursegroupdata', ['id' => $id]);
                $returnurl = new \moodle_url('/local/o365/groupcp.php',
                    ['courseid' => $group->courseid, 'groupid' => $group->groupid]);
            }
            redirect($returnurl);
        }

        $strstudygroup = get_string('groups_studygroup', 'local_o365');
        $strheading = get_string('groups_editsettings', 'local_o365');

        $PAGE->navbar->add($strheading);

        echo $OUTPUT->header();
        echo \html_writer::start_tag('div', ['id' => 'o365grouppicture']);
        if (!empty($group->displayname)) {
            echo \html_writer::tag('h1', $group->displayname);
        }
        if ($id) {
            echo \local_o365\feature\usergroups\utils::print_group_picture($group);
        }
        echo \html_writer::end_tag('div');
        $editform->display();
        $this->standard_footer();
    }