Skip to content
Snippets Groups Projects
block_hshcourselist.php 10.9 KiB
Newer Older
  • Learn to ignore specific revisions
  • Peter Fricke's avatar
    Peter Fricke committed
    <?php
    
    Tobias Baden's avatar
    Tobias Baden committed
    // This file is part of Moodle - http://moodle.org/
    //
    // Moodle is free software: you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.
    //
    // Moodle is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.
    //
    // You should have received a copy of the GNU General Public License
    // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
    
    /**
     * Course list block.
     * Original: block_course_list
     *
     * @package    block_hshcourselist
     * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com), Edited by Tobias Baden, Hochschule Hannover
     * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
     */
    
    Peter Fricke's avatar
    Peter Fricke committed
    
    defined('MOODLE_INTERNAL') || die();
    class block_hshcourselist extends block_base {
        private $globalconf;
        public function init() {
            $this->globalconf = get_config('block_hshcourselist');
            $this->title = get_string('default_blocktitle', 'block_hshcourselist');
        }
    
    
        // Stop it showing up on any add block lists.
    
    Peter Fricke's avatar
    Peter Fricke committed
        public function applicable_formats() {
            return (array(
                'all' => false,
                'site' => true,
                'my' => true,
                'course-index' => true
            ));
        }
    
        public function has_config() {
            return true;
        }
    
        public function get_content() {
    
    Tobias Baden's avatar
    Tobias Baden committed
            global $CFG, $DB, $OUTPUT;
    
    Peter Fricke's avatar
    Peter Fricke committed
    
            $this->page->requires->js('/blocks/hshcourselist/javascript/jquery-3.2.0.min.js');
            $this->page->requires->css('/blocks/hshcourselist/style.css');
    
            if ($this->content !== null) {
                return $this->content;
            }
    
            $this->content = new stdClass();
    
            $contextblock = context_block::instance($this->instance->id);
    
    Peter Fricke's avatar
    Peter Fricke committed
            $search = optional_param('hshcoursesearch', '', PARAM_TEXT);
    
    Peter Fricke's avatar
    Peter Fricke committed
            $hshcoursesubmit = optional_param('hshcoursesubmit', false, PARAM_TEXT);
    
            // Set placeholder.
            if ($this->config->placeholder) {
    
    Tobias Baden's avatar
    Tobias Baden committed
                $placeholder = $this->config->placeholder;
            } else {
                $placeholder = "";
            }
    
            $listcontents = '';
    
    Tobias Baden's avatar
    Tobias Baden committed
            $anchor = html_writer::tag('a', '', array('name' => 'hshcourselistanchor'));
    
    Peter Fricke's avatar
    Peter Fricke committed
    
    
            $ordercheckbox = html_writer::empty_tag('input',
                array('type' => 'checkbox', 'name' => 'orderbysemestercheckbox', 'id' => 'orderbysemestercheckbox_id'));
    
    Peter Fricke's avatar
    Peter Fricke committed
            $ordercheckbox .= html_writer::start_tag('label', array('for' => 'orderbysemestercheckbox_id'));
            $ordercheckbox .= get_string('orderbysemester', 'block_hshcourselist');
            $ordercheckbox .= html_writer::end_tag('label');
    
    
            $idsearchinglabel = html_writer::empty_tag('p', array('class' => 'idSarchingLabel'));
            $idsearchinglabel .= get_string('idsearchinglabel', 'block_hshcourselist');
            $idsearchinglabel .= html_writer::end_tag('p');
    
    Peter Fricke's avatar
    Peter Fricke committed
    
            $inputattrs = array(
                'autocomplete' => 'off',
                'name' => 'hshcourselistsearch',
                'id' => 'hshcourselistsearch',
                'class' => 'form-control',
    
    Tobias Baden's avatar
    Tobias Baden committed
                'value' => $search,
                'placeholder' => $placeholder
    
    Peter Fricke's avatar
    Peter Fricke committed
            );
            $input = html_writer::empty_tag('input', $inputattrs);
    
    
    Tobias Baden's avatar
    Tobias Baden committed
            $questionattrs = array(
    
    Tobias Baden's avatar
    Tobias Baden committed
                'class' => 'icon fa fa-question-circle text-info fa-fw',
                'title' => 'Hilfe für HsH Kurssuche',
                'aria-label' => 'Hilfe für HsH Kurssuche',
                'aria-hidden' => 'true'
            );
    
            $atagparams = array(
    
                'class' => 'btn btn-link p-0',
                'role' => 'button',
                'data-container' => 'body',
                'data-toggle' => 'popover',
                'data-placement' => 'right',
                'data-content' => "<div class='no-overflow'><p>Die Kurssuche ist eine Volltextsuche in der Sie auch mehrere
     Suchbegriffe (mit Leerzeichen getrennt) eingeben können.<br/>
                   Verschiedene Suchbegriffe werden einzeln in den Kursnamen gesucht und dann eine vollständige
                    Liste angezeigt.<br/>
                    Bsp: WiSe Programmieren => zeigt Ihnen alle Kurse, dessen Kursname oder Kurskurzname 'WiSe'
                    <i>UND</i> 'Programmieren' enthalten.</p>
                    <p>Kurzsuche: Möchten Sie einen Kurs schnell über die <i>'id'</i> finden, so können Sie dies
                    durch das Voranstellen einer Raute <i>'#'</i> erreichen.<br/>
    
    Tobias Baden's avatar
    Tobias Baden committed
                    Bsp: <i>#3303</i> zeigt Ihnen den Kurs <i>'Moodle Demo-Kurs, ELC'</i></p>",
    
                'data-html' => 'true',
                'tabindex' => '0',
                'data-trigger' => 'focus',
                'data-original-title' => '',
                'title' => ''
    
    Tobias Baden's avatar
    Tobias Baden committed
    
            $questionhsh = html_writer::start_span("helpbutton");
    
    Tobias Baden's avatar
    Tobias Baden committed
            $questionhsh .= html_writer::start_tag('a', $atagparams);
            $questionhsh .= html_writer::start_tag('i', $questionattrs);
            $questionhsh .= html_writer::end_tag('i');
            $questionhsh .= html_writer::end_tag('a');
    
    Tobias Baden's avatar
    Tobias Baden committed
            $questionhsh .= html_writer::end_span();
    
    Peter Fricke's avatar
    Peter Fricke committed
            $progressattrs = array(
    
    Tobias Baden's avatar
    Tobias Baden committed
                'src' => $OUTPUT->image_url('i/loading_small', 'moodle'),
    
    Peter Fricke's avatar
    Peter Fricke committed
                'class' => 'hshcourseprogress',
                'id' => 'hshcourseprogress',
                'alt' => get_string('loading', 'block_hshcourselist')
            );
            $progress = html_writer::empty_tag('img', $progressattrs);
    
            $formattrs = array(
    
    Tobias Baden's avatar
    Tobias Baden committed
                'id' => 'hshcourseform',
                'method' => 'post',
                'name' => 'hshcourselistform',
                'action' => new moodle_url('/')
            );
    
            $form = html_writer::tag('form', $idsearchinglabel.$ordercheckbox.$input.$questionhsh.$progress, $formattrs);
    
    Peter Fricke's avatar
    Peter Fricke committed
            if (!empty($hshcoursesubmit)) {
    
                $courses = self::get_courses($search, $contextblock, $this->globalconf->splitterms,
    
    Tobias Baden's avatar
    Tobias Baden committed
                    $this->globalconf->restrictcontext, $this->page->context,
    
                    $this->globalconf->idsearching, $this->config->orderbysemester);
    
    Peter Fricke's avatar
    Peter Fricke committed
    
                if (!empty($courses)) {
                    foreach ($courses as $course) {
                        $url = new moodle_url('/course/view.php', array('id' => $course->id));
                        $resultstr = null;
                        if (!empty($this->config->displaymode)) {
                            $displaymode = $this->config->displaymode;
                        } else {
                            $displaymode = 2;
                        }
                        switch ($displaymode):
    
                            case 1: $resultstr = $course->shortname;
                                break;
                            case 2: $resultstr = $course->fullname;
                                break;
                            default: $resultstr = $course->shortname.': '.$course->fullname;
                                break;
    
    Peter Fricke's avatar
    Peter Fricke committed
                        endswitch;
    
                        $link = html_writer::tag('a', $resultstr, array('href' => $url->out()));
                        $li = html_writer::tag('li', $link);
    
                        $listcontents .= $li;
    
    Peter Fricke's avatar
    Peter Fricke committed
                    }
                }
            }
    
    
            if (!isset($this->config->orderbysemester)) {
    
    Tobias Baden's avatar
    Tobias Baden committed
                $orderbysemester = false;
            } else {
                $orderbysemester = $this->config->orderbysemester;
            }
    
            if (!isset($this->config->displaymode)) {
    
    Tobias Baden's avatar
    Tobias Baden committed
                $displaymode = 2;
            } else {
                $displaymode = $this->config->displaymode;
            }
    
    
            $list = html_writer::tag('ul', $listcontents, array('id' => 'hshcourselistul'));
    
    Peter Fricke's avatar
    Peter Fricke committed
            $this->content->text = $anchor.$form.$list;
            $jsdata = array(
                'instanceid' => $this->instance->id,
                'sesskey' => sesskey(),
    
    Tobias Baden's avatar
    Tobias Baden committed
                'displaymode' => $displaymode,
                'orderbysemester' => $orderbysemester,
    
    Peter Fricke's avatar
    Peter Fricke committed
                'contextid' => $this->page->context->id
            );
    
            $this->page->requires->js_call_amd('block_hshcourselist/jmodule', 'init', array($jsdata, $CFG->wwwroot));
    
    Peter Fricke's avatar
    Peter Fricke committed
    
    
            // Einstellungen für den Custom-Footer.
    
    Peter Fricke's avatar
    Peter Fricke committed
            if (!empty($this->config->footer) && $this->config->enablefooter) {
                $this->content->footer = $this->config->footer;
            }
    
    
            // Einstellungen für den Custom-Header.
    
    Peter Fricke's avatar
    Peter Fricke committed
            if (!empty($this->config->title) && $this->config->enablecustomheader) {
                $this->title = $this->config->title;
            }
    
    Tobias Baden's avatar
    Tobias Baden committed
            $this->page->requires->js_call_amd('block_hshcourselist/listselect', 'init', array($CFG->wwwroot));
    
    Peter Fricke's avatar
    Peter Fricke committed
            return $this->content;
        }
    
    
        public static function get_courses($search, $blockcontext, $splitterms = false,
                                           $restrictcontext = false, $pagecontext = null, $idsearch, $orderbysemester) {
    
    Peter Fricke's avatar
    Peter Fricke committed
            global $DB;
            $params = array(SITEID);
    
            $keywordarray = explode(" ", $search);
            $paramscount = count($keywordarray);
            $duplicatedkeywordarray = array();
            for ($x = 0; $x <= $paramscount - 1; $x++) {
                $actuallength = count($duplicatedkeywordarray);
                $duplicatedkeywordarray[$actuallength] = "%".$keywordarray[$x]."%";
                $duplicatedkeywordarray[$actuallength + 1] = "%".$keywordarray[$x]."%";
    
    Peter Fricke's avatar
    Peter Fricke committed
            $where = 'id != ? AND (';
            if ($splitterms) {
                $terms = explode(' ', $search);
                $like = '%1$s LIKE';
                foreach ($terms as $key => $term) {
                    $like .= ' ?';
    
                    if ($key < count($terms) - 1) {
    
    Peter Fricke's avatar
    Peter Fricke committed
                        $like .= ' AND %1$s LIKE';
                    }
                    $terms[$key] = '%'.$term.'%';
                }
                $params = array_merge($params, $terms, $terms);
                $where .= sprintf($like, 'shortname').' OR '.sprintf($like, 'fullname');
            } else {
    
                if ($idsearch) {
    
    Peter Fricke's avatar
    Peter Fricke committed
                    $params = array_merge($params, array($search));
                    $where .= 'id = ?';
                } else {
    
                    $params = array_merge($params, $duplicatedkeywordarray);
    
    Tobias Baden's avatar
    Tobias Baden committed
                    $where .= '(fullname ILIKE ? OR shortname ILIKE ?)';
    
                    for ($i = 1; $i <= $paramscount - 1; $i++) {
    
    Tobias Baden's avatar
    Tobias Baden committed
                        $where .= ' AND (fullname ILIKE ? OR shortname ILIKE ?)';
    
    Peter Fricke's avatar
    Peter Fricke committed
                }
            }
            $where .= ')';
            if (!has_capability('moodle/course:viewhiddencourses', $blockcontext)) {
                $where .= ' AND visible=1';
            }
            if ($restrictcontext) {
                if ($pagecontext && $pagecontext->get_level_name() == get_string('category')) {
                    $where .= ' AND category = ?';
                    $params[] = $pagecontext->instanceid;
                }
            }
    
            if ($orderbysemester) {
    
    Peter Fricke's avatar
    Peter Fricke committed
                $order = 'startdate DESC';
    
            } else if ($idsearch) {
    
    Peter Fricke's avatar
    Peter Fricke committed
                $order = 'idnumber';
            } else {
    
    Peter Fricke's avatar
    Peter Fricke committed
                $order = 'fullname';
    
    Peter Fricke's avatar
    Peter Fricke committed
            }
    
            $fields = 'id, idnumber, shortname, fullname';
            $courses = $DB->get_recordset_select('course', $where, $params, $order, $fields);
    
    Tobias Baden's avatar
    Tobias Baden committed
            return $courses;
    
    Peter Fricke's avatar
    Peter Fricke committed
        }