mirrored from git://git.moodle.org/moodle.git
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-60211 User tours: New filters for User Tours
- Loading branch information
John Beedell
committed
Oct 13, 2017
1 parent
9a316f3
commit 77d2e46
Showing
5 changed files
with
403 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
// 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/>. | ||
|
||
/** | ||
* Category filter. | ||
* | ||
* @package tool_usertours | ||
* @copyright 2017 The Open University | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
namespace tool_usertours\local\filter; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
use tool_usertours\tour; | ||
use context; | ||
|
||
/** | ||
* Category filter. | ||
* | ||
* @copyright 2017 The Open University | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class category extends base { | ||
/** | ||
* The name of the filter. | ||
* | ||
* @return string | ||
*/ | ||
public static function get_filter_name() { | ||
return 'category'; | ||
} | ||
|
||
/** | ||
* Retrieve the list of available filter options. | ||
* | ||
* @return array An array whose keys are the valid options | ||
* And whose values are the values to display | ||
*/ | ||
public static function get_filter_options() { | ||
$options = \coursecat::make_categories_list(); | ||
return $options; | ||
} | ||
|
||
/** | ||
* Check whether the filter matches the specified tour and/or context. | ||
* | ||
* @param tour $tour The tour to check | ||
* @param context $context The context to check | ||
* @return boolean | ||
*/ | ||
public static function filter_matches(tour $tour, context $context) { | ||
$values = $tour->get_filter_values(self::get_filter_name()); | ||
if (empty($values) || empty($values[0])) { | ||
// There are no values configured, meaning all. | ||
return true; | ||
} | ||
if ($context->contextlevel < CONTEXT_COURSECAT) { | ||
return false; | ||
} | ||
return self::check_contexts($context, $values); | ||
} | ||
|
||
/** | ||
* Recursive function allows checking of parent categories. | ||
* | ||
* @param context $context | ||
* @param array $values | ||
* @return boolean | ||
*/ | ||
private static function check_contexts(context $context, $values) { | ||
if ($context->contextlevel > CONTEXT_COURSECAT) { | ||
return self::check_contexts($context->get_parent_context(), $values); | ||
} else if ($context->contextlevel == CONTEXT_COURSECAT) { | ||
if (in_array($context->instanceid, $values)) { | ||
return true; | ||
} else { | ||
return self::check_contexts($context->get_parent_context(), $values); | ||
} | ||
} else { | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
// 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 filter. | ||
* | ||
* @package tool_usertours | ||
* @copyright 2017 The Open University | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
namespace tool_usertours\local\filter; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
use tool_usertours\tour; | ||
use context; | ||
|
||
/** | ||
* Course filter. | ||
* | ||
* @copyright 2017 The Open University | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class course extends base { | ||
/** | ||
* The name of the filter. | ||
* | ||
* @return string | ||
*/ | ||
public static function get_filter_name() { | ||
return 'course'; | ||
} | ||
|
||
/** | ||
* Overrides the base add form element with a course selector. | ||
* | ||
* @param \MoodleQuickForm $mform | ||
*/ | ||
public static function add_filter_to_form(\MoodleQuickForm &$mform) { | ||
$options = ['multiple' => true]; | ||
|
||
$filtername = self::get_filter_name(); | ||
$key = "filter_{$filtername}"; | ||
|
||
$mform->addElement('course', $key, get_string($key, 'tool_usertours'), $options); | ||
$mform->setDefault($key, '0'); | ||
$mform->addHelpButton($key, $key, 'tool_usertours'); | ||
} | ||
|
||
/** | ||
* Check whether the filter matches the specified tour and/or context. | ||
* | ||
* @param tour $tour The tour to check | ||
* @param context $context The context to check | ||
* @return boolean | ||
*/ | ||
public static function filter_matches(tour $tour, context $context) { | ||
global $COURSE; | ||
$values = $tour->get_filter_values(self::get_filter_name()); | ||
if (empty($values) || empty($values[0])) { | ||
// There are no values configured, meaning all. | ||
return true; | ||
} | ||
if (empty($COURSE->id)) { | ||
return false; | ||
} | ||
return in_array($COURSE->id, $values); | ||
} | ||
|
||
/** | ||
* Overrides the base prepare the filter values for the form with an integer value. | ||
* | ||
* @param tour $tour The tour to prepare values from | ||
* @param stdClass $data The data value | ||
* @return stdClass | ||
*/ | ||
public static function prepare_filter_values_for_form(tour $tour, \stdClass $data) { | ||
$filtername = static::get_filter_name(); | ||
$key = "filter_{$filtername}"; | ||
$values = $tour->get_filter_values($filtername); | ||
if (empty($values)) { | ||
$values = 0; | ||
} | ||
$data->$key = $values; | ||
return $data; | ||
} | ||
|
||
/** | ||
* Overrides the base save the filter values from the form to the tour. | ||
* | ||
* @param tour $tour The tour to save values to | ||
* @param stdClass $data The data submitted in the form | ||
*/ | ||
public static function save_filter_values_from_form(tour $tour, \stdClass $data) { | ||
$filtername = static::get_filter_name(); | ||
$key = "filter_{$filtername}"; | ||
$newvalue = $data->$key; | ||
if (empty($data->$key)) { | ||
$newvalue = []; | ||
} | ||
$tour->set_filter_values($filtername, $newvalue); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
admin/tool/usertours/classes/local/filter/courseformat.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
// 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 format filter. | ||
* | ||
* @package tool_usertours | ||
* @copyright 2017 The Open University | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
namespace tool_usertours\local\filter; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
use tool_usertours\tour; | ||
use context; | ||
|
||
/** | ||
* Course format filter. | ||
* | ||
* @copyright 2017 The Open University | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class courseformat extends base { | ||
/** | ||
* The name of the filter. | ||
* | ||
* @return string | ||
*/ | ||
public static function get_filter_name() { | ||
return 'courseformat'; | ||
} | ||
|
||
/** | ||
* Retrieve the list of available filter options. | ||
* | ||
* @return array An array whose keys are the valid options | ||
* And whose values are the values to display | ||
*/ | ||
public static function get_filter_options() { | ||
$options = []; | ||
$courseformats = get_sorted_course_formats(true); | ||
foreach ($courseformats as $courseformat) { | ||
$options[$courseformat] = get_string('pluginname', "format_$courseformat"); | ||
} | ||
return $options; | ||
} | ||
|
||
/** | ||
* Check whether the filter matches the specified tour and/or context. | ||
* | ||
* @param tour $tour The tour to check | ||
* @param context $context The context to check | ||
* @return boolean | ||
*/ | ||
public static function filter_matches(tour $tour, context $context) { | ||
global $COURSE; | ||
$values = $tour->get_filter_values('courseformat'); | ||
if (empty($values)) { | ||
// There are no values configured, meaning all. | ||
return true; | ||
} | ||
if (empty($COURSE->format)) { | ||
return false; | ||
} | ||
return in_array($COURSE->format, $values); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.