1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Course filter. 19 * 20 * @package tool_usertours 21 * @copyright 2017 The Open University 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace tool_usertours\local\filter; 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 use tool_usertours\tour; 30 use context; 31 32 /** 33 * Course filter. 34 * 35 * @copyright 2017 The Open University 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 class course extends base { 39 /** 40 * The name of the filter. 41 * 42 * @return string 43 */ 44 public static function get_filter_name() { 45 return 'course'; 46 } 47 48 /** 49 * Overrides the base add form element with a course selector. 50 * 51 * @param \MoodleQuickForm $mform 52 */ 53 public static function add_filter_to_form(\MoodleQuickForm &$mform) { 54 $options = ['multiple' => true]; 55 56 $filtername = self::get_filter_name(); 57 $key = "filter_{$filtername}"; 58 59 $mform->addElement('course', $key, get_string($key, 'tool_usertours'), $options); 60 $mform->setDefault($key, '0'); 61 $mform->addHelpButton($key, $key, 'tool_usertours'); 62 } 63 64 /** 65 * Check whether the filter matches the specified tour and/or context. 66 * 67 * @param tour $tour The tour to check 68 * @param context $context The context to check 69 * @return boolean 70 */ 71 public static function filter_matches(tour $tour, context $context) { 72 global $COURSE; 73 $values = $tour->get_filter_values(self::get_filter_name()); 74 if (empty($values) || empty($values[0])) { 75 // There are no values configured, meaning all. 76 return true; 77 } 78 if (empty($COURSE->id)) { 79 return false; 80 } 81 return in_array($COURSE->id, $values); 82 } 83 84 /** 85 * Overrides the base prepare the filter values for the form with an integer value. 86 * 87 * @param tour $tour The tour to prepare values from 88 * @param stdClass $data The data value 89 * @return stdClass 90 */ 91 public static function prepare_filter_values_for_form(tour $tour, \stdClass $data) { 92 $filtername = static::get_filter_name(); 93 $key = "filter_{$filtername}"; 94 $values = $tour->get_filter_values($filtername); 95 if (empty($values)) { 96 $values = 0; 97 } 98 $data->$key = $values; 99 return $data; 100 } 101 102 /** 103 * Overrides the base save the filter values from the form to the tour. 104 * 105 * @param tour $tour The tour to save values to 106 * @param stdClass $data The data submitted in the form 107 */ 108 public static function save_filter_values_from_form(tour $tour, \stdClass $data) { 109 $filtername = static::get_filter_name(); 110 $key = "filter_{$filtername}"; 111 $newvalue = $data->$key; 112 if (empty($data->$key)) { 113 $newvalue = []; 114 } 115 $tour->set_filter_values($filtername, $newvalue); 116 } 117 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body