public function mode_sharepointcourseselect()

in classes/page/acp.php [1649:1843]


    public function mode_sharepointcourseselect() {
        global $OUTPUT, $PAGE, $DB, $CFG;

        // If custom setting is not enabled, redirect back to the settings page.
        $selectisenabled = get_config('local_o365', 'sharepointcourseselect');
        if ($selectisenabled !== 'oncustom') {
            $linkattrs = ['section' => 'local_o365', 's_local_o365_tabs' => '1'];
            $actionurl = new \moodle_url('/admin/settings.php', $linkattrs);
            redirect($actionurl);
        }

        require_once($CFG->libdir . '/coursecatlib.php');
        require_once($CFG->libdir . '/formslib.php');
        $spcustomsynced = get_config('local_o365', 'spcustomsynced');
        $spcustomsynced = $spcustomsynced ? $spcustomsynced : false;

        $PAGE->navbar->add(get_string('acp_sharepointcourseselect', 'local_o365'),
            new \moodle_url($this->url, ['mode' => 'sharepointcourseselect']));

        $totalcount = 0;
        $perpage = 20;

        $curpage = optional_param('page', 0, PARAM_INT);
        $sort = optional_param('sort', '', PARAM_ALPHA);
        $search = optional_param('search', '', PARAM_TEXT);
        $sortdir = strtolower(optional_param('sortdir', 'asc', PARAM_ALPHA));

        $headers = [
            'shortname' => get_string('shortnamecourse'),
            'fullname' => get_string('fullnamecourse'),
            'category' => get_string('category'),
        ];
        if (empty($sort) || !isset($headers[$sort])) {
            $sort = 'shortname';
        }
        if (!in_array($sortdir, ['asc', 'desc'], true)) {
            $sortdir = 'asc';
        }

        $table = new \html_table;
        foreach ($headers as $hkey => $desc) {
            $diffsortdir = ($sort === $hkey && $sortdir === 'asc') ? 'desc' : 'asc';
            $linkattrs = ['mode' => 'sharepointcourseselect', 'sort' => $hkey, 'sortdir' => $diffsortdir];
            $link = new \moodle_url('/local/o365/acp.php', $linkattrs);

            if ($sort === $hkey) {
                $desc .= ' '.$OUTPUT->pix_icon('t/'.'sort_'.$sortdir, 'sort');
            }
            $table->head[] = \html_writer::link($link, $desc);
        }
        $table->head[] = get_string('acp_sharepointcourseselectlabel_enabled', 'local_o365');

        $limitfrom = $curpage * $perpage;
        if (empty($search)) {
            $sortdir = 1;
            if ($sortdir == 'desc') {
                $sortdir = -1;
            }
            $options = [
                'recursive' => true,
                'sort' => [$sort => $sortdir],
                'offset' => $limitfrom,
                'limit' => $perpage,
            ];
            $topcat = \core_course_category::get(0);
            $courses = $topcat->get_courses($options);
            $totalcount = $topcat->get_courses_count($options);
        } else {
            $searchar = explode(' ', $search);
            $courses = get_courses_search($searchar, 'c.'.$sort.' '.$sortdir, $curpage, $perpage, $totalcount);
        }
        $coursesid = [];
        $categories = array();
        foreach ($courses as $course) {
            if ($course->id == SITEID) {
                continue;
            }
            $coursesid[] = $course->id;
            $isenabled = \local_o365\feature\sharepointcustom\utils::course_is_sharepoint_enabled($course->id);
            $enabledname = 'course_'.$course->id.'_enabled';

            $enablecheckboxattrs = [
                'onchange' => '$(this).toggleClass(\'changed\');'
            ];

            $category = $DB->get_record('course_categories', array('id' => $course->category));
            $name = $category->name;
            array_push($categories, $name);

            $rowdata = [
                $course->shortname,
                $course->fullname,
                '<span class="category-'.$category->id.'">'.$category->name.'</span>',
                \html_writer::checkbox($enabledname, 1, $isenabled, '', $enablecheckboxattrs),
            ];
            $table->data[] = $rowdata;
        }

        $PAGE->requires->jquery();
        $this->standard_header();

        // Endpoint for checkbox toggle.
        $endpoint = new \moodle_url('/local/o365/acp.php', ['mode' => 'sharepointcourseenabled_change', 'sesskey' => sesskey()]);
        // Bulk save endpoint.
        $bulkendpoint = new \moodle_url('/local/o365/acp.php', ['mode' => 'sharepointcustom_bulkchange', 'sesskey' => sesskey()]);
        // End point for sync courses.
        $syncendpoint = new \moodle_url('/local/o365/acp.php', ['mode' => 'sharepointcourseenabled_sync', 'sesskey' => sesskey()]);
        // Build JS script content.
        $js = 'var local_o365_set_sharepoint_enabled = function(courseid, state, checkbox) { ';
        $js .= 'data = {courseid: courseid, state: state}; ';
        $js .= '$.post(\''.$endpoint->out(false).'\', data, function(data) { console.log(data); }); ';
        $js .= 'var newfeaturedisabled = (state == 0) ? true : false; ';
        $js .= 'var newfeaturechecked = (state == 1) ? true : false; ';
        $js .= 'var featurecheckboxes = checkbox.parents("tr").find("input.feature"); ';
        $js .= 'featurecheckboxes.prop("disabled", newfeaturedisabled); ';
        $js .= 'featurecheckboxes.prop("checked", newfeaturechecked); ';
        $js .= 'console.log("local_o365_set_sharepoint_enabled"); ';
        $js .= '};';
        // Bulk save changes.
        $js .= 'var local_o365_sharepointcustom_save = function() { '."\n";
        $js .= 'var local_o365_spcourseschanged = $(\'table input.changed\');'."\n";
        $js .= 'if (local_o365_spcourseschanged.length >= 1) {'."\n";
        $js .= ' // Build data to send. '."\n";
        $js .= 'var $coursedata = {}; '."\n";
        $js .= 'for (var i = 0; i < local_o365_spcourseschanged.length; i++) {'."\n";
        $js .= 'var $courseid = $(local_o365_spcourseschanged[i]).attr(\'name\'); '."\n";
        $js .= '$courseid = $courseid.replace(/course|enabled|_/gi, \'\'); '."\n";
        $js .= 'var $enabled = $(local_o365_spcourseschanged[i]).prop(\'checked\'); '."\n";
        $js .= '$coursedata[$courseid] = $enabled; '."\n";
        $js .= '}; '."\n";
        $js .= 'data = JSON.stringify($coursedata); '."\n";
        $js .= ' // Send data to server. '."\n";
        $js .= '$.ajax({
            url: \''.$bulkendpoint->out(false).'\',
            data: {coursedata: JSON.stringify($coursedata)},
            type: "POST",
            success: function(data) {
                console.log(\'Success: \'+data);
                $(\'#acp_sharepointcustom_savemessage\').show();
                setTimeout(function () { $(\'#acp_sharepointcustom_savemessage\').hide(); }, 5000);
                $(local_o365_spcourseschanged).each( function() { $(this).toggleClass(\'changed\'); } );
            }
        }); '."\n";
        $js .= '} '."\n";
        $js .= '}; '."\n";
        echo \html_writer::script($js);

        // Print functionality heading.
        echo \html_writer::tag('h2', get_string('acp_sharepointcourseselect', 'local_o365'));
        // If we haven't updated by syncing data from sharepoint, and there are courses, give the option to do that.
        if (!$spcustomsynced && (count($courses) > 1)) {
            $linkattrs = ['mode' => 'sharepointcourseenabled_sync'];
            $link = new \moodle_url('/local/o365/acp.php', $linkattrs);
            echo \html_writer::tag('h5', get_string('acp_sharepointcourseselect_syncopt', 'local_o365'));
            echo \html_writer::tag('p', get_string('acp_sharepointcourseselect_syncopt_inst', 'local_o365'));
            echo \html_writer::empty_tag('img', ['src' => $OUTPUT->pix_url('spinner', 'local_o365'), 'alt' => 'In process...',
                'class' => 'local_o365_spinner']);
            echo $OUTPUT->single_button($syncendpoint, get_string('acp_sharepointcourseselect_syncopt_btn', 'local_o365'), 'get');
            $js = '$(\'div.singlebutton :submit\').click(function() { $(\'img.local_o365_spinner\').show(); });';
            echo \html_writer::script($js);
        }
        // Search form.
        echo \html_writer::tag('h5', get_string('search'));
        echo \html_writer::start_tag('form', ['id' => 'coursesearchform', 'method' => 'get']);
        echo \html_writer::start_tag('fieldset', ['class' => 'coursesearchbox invisiblefieldset']);
        echo \html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'mode', 'value' => 'sharepointcourseselect']);
        echo \html_writer::empty_tag('input', ['type' => 'text', 'id' => 'coursesearchbox', 'size' => 30, 'name' => 'search',
            'value' => s($search)]);
        echo \html_writer::empty_tag('input', ['type' => 'submit', 'value' => get_string('go')]);
        echo \html_writer::div(\html_writer::tag('strong', get_string('acp_sharepointcourseselect_searchwarning', 'local_o365')));
        echo \html_writer::end_tag('fieldset');
        echo \html_writer::end_tag('form');
        echo \html_writer::empty_tag('br');

        // Write instructions for selecting courses.
        echo \html_writer::tag('h5', get_string('acp_sharepointcourseselect_instr_header', 'local_o365'));
        echo \html_writer::tag('p', get_string('acp_sharepointcourseselect_instr', 'local_o365'));
        // Begin courses table.
        echo \html_writer::tag('h5', get_string('courses'));
        echo \html_writer::table($table);
        // URL and paging elements.
        $cururl = new \moodle_url('/local/o365/acp.php', ['mode' => 'sharepointcourseselect']);
        echo $OUTPUT->paging_bar($totalcount, $curpage, $perpage, $cururl);
         // Notification box to confirm bulk save.
        echo \html_writer::start_tag('div', ['class' => 'alert alert-success alert-block fade in',
            'id' => 'acp_sharepointcustom_savemessage', 'style' => 'display: none;', 'role' => 'alert']);
        echo \html_writer::tag('button', '×', ['class' => 'close', 'data-dismiss' => 'alert', 'type' => 'button']);
        echo get_string('acp_sharepointcustom_savemessage', 'local_o365');
        echo \html_writer::end_tag('div');
        // Bulk save button.
        echo  \html_writer::tag('button', get_string('savechanges'),
            ['class' => 'buttonsbar', 'onclick' => 'local_o365_sharepointcustom_save()']);

        $this->standard_footer();
    }