first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,46 @@
<?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/>.
namespace tool_usertours\local\clientside_filter;
use stdClass;
use tool_usertours\local\filter\base;
use tool_usertours\tour;
/**
* Clientside filter base.
*
* @package tool_usertours
* @copyright 2020 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class clientside_filter extends base {
/**
* Returns the filter values needed for client side filtering.
*
* @param tour $tour The tour to find the filter values for
* @return stdClass
*/
public static function get_client_side_values(tour $tour): stdClass {
$data = (object) [];
if (is_a(static::class, self::class, true)) {
$data->filterdata = $tour->get_filter_values(static::get_filter_name());
}
return $data;
}
}
@@ -0,0 +1,109 @@
<?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/>.
namespace tool_usertours\local\clientside_filter;
use stdClass;
use tool_usertours\tour;
/**
* Course filter.
*
* @package tool_usertours
* @copyright 2020 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cssselector extends clientside_filter {
/**
* The name of the filter.
*
* @return string
*/
public static function get_filter_name() {
return 'cssselector';
}
/**
* Overrides the base add form element with a selector text box.
*
* @param \MoodleQuickForm $mform
*/
public static function add_filter_to_form(\MoodleQuickForm &$mform) {
$filtername = self::get_filter_name();
$key = "filter_{$filtername}";
$mform->addElement('text', $key, get_string($key, 'tool_usertours'));
$mform->setType($key, PARAM_RAW);
$mform->addHelpButton($key, $key, 'tool_usertours');
}
/**
* Prepare the filter values for the form.
*
* @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 = [""];
}
$data->$key = $values[0];
return $data;
}
/**
* 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);
}
/**
* Returns the filter values needed for client side filtering.
*
* @param tour $tour The tour to find the filter values for
* @return stdClass
*/
public static function get_client_side_values(tour $tour): stdClass {
$filtername = static::get_filter_name();
$filtervalues = $tour->get_filter_values($filtername);
// Filter values might not exist for tours that were created before this filter existed.
if (!$filtervalues) {
return new stdClass();
}
return (object) $filtervalues;
}
}
@@ -0,0 +1,221 @@
<?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/>.
namespace tool_usertours\local\filter;
use context;
use tool_usertours\tour;
/**
* Access date filter. Used to determine if USER should see a tour based on a particular access date.
*
* @package tool_usertours
* @copyright 2019 Tom Dickman <tomdickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class accessdate extends base {
/**
* Access date filtering constant for setting base date as account creation date.
*/
const FILTER_ACCOUNT_CREATION = 'tool_usertours_accountcreation';
/**
* Access date filtering constant for setting base date as account first login date.
*/
const FILTER_FIRST_LOGIN = 'tool_usertours_firstlogin';
/**
* Access date filtering constant for setting base date as account last login date.
*/
const FILTER_LAST_LOGIN = 'tool_usertours_lastlogin';
/**
* Default this filter to not be enabled.
*/
const FILTER_ENABLED_DEFAULT = 0;
/**
* The name of the filter.
*
* @return string
*/
public static function get_filter_name() {
return 'accessdate';
}
/**
* 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
* @throws \coding_exception
*/
public static function get_filter_options() {
return [
self::FILTER_ACCOUNT_CREATION => get_string('filter_date_account_creation', 'tool_usertours'),
self::FILTER_FIRST_LOGIN => get_string('filter_date_first_login', 'tool_usertours'),
self::FILTER_LAST_LOGIN => get_string('filter_date_last_login', 'tool_usertours'),
];
}
/**
* Add the form elements for the filter to the supplied form.
*
* @param \MoodleQuickForm $mform The form to add filter settings to.
*
* @throws \coding_exception
*/
public static function add_filter_to_form(\MoodleQuickForm &$mform) {
$filtername = static::get_filter_name();
$key = "filter_{$filtername}";
$range = "{$key}_range";
$enabled = "{$key}_enabled";
$mform->addElement(
'advcheckbox',
$enabled,
get_string($key, 'tool_usertours'),
get_string('filter_accessdate_enabled', 'tool_usertours'),
null,
[0, 1]
);
$mform->addHelpButton($enabled, $enabled, 'tool_usertours');
$mform->addElement('select', $key, ' ', self::get_filter_options());
$mform->setDefault($key, self::FILTER_ACCOUNT_CREATION);
$mform->hideIf($key, $enabled, 'notchecked');
$mform->addElement('duration', $range, null, [
'optional' => false,
'defaultunit' => DAYSECS,
]);
$mform->setDefault($range, 90 * DAYSECS);
$mform->hideIf($range, $enabled, 'notchecked');
}
/**
* Prepare the filter values for the form.
*
* @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}";
$range = "{$key}_range";
$enabled = "{$key}_enabled";
$values = $tour->get_filter_values($filtername);
// Prepare the advanced checkbox value and prepare filter values based on previously set values.
if (!empty($values)) {
$data->$enabled = $values->$enabled ? $values->$enabled : self::FILTER_ENABLED_DEFAULT;
if ($data->$enabled) {
if (isset($values->$key)) {
$data->$key = $values->$key;
}
if (isset($values->$range)) {
$data->$range = $values->$range;
}
}
} else {
$data->$enabled = self::FILTER_ENABLED_DEFAULT;
}
return $data;
}
/**
* 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}";
$range = "{$key}_range";
$enabled = "{$key}_enabled";
$savedata = [];
$savedata[$key] = $data->$key;
$savedata[$range] = $data->$range;
$savedata[$enabled] = $data->$enabled;
$tour->set_filter_values($filtername, $savedata);
}
/**
* 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 $USER;
$filtername = static::get_filter_name();
$key = "filter_{$filtername}";
$range = "{$key}_range";
$enabled = "{$key}_enabled";
// Default behaviour is to match filter.
$result = true;
$values = (array) $tour->get_filter_values(self::get_filter_name());
// If the access date filter is not enabled, end here.
if (empty($values[$enabled])) {
return $result;
}
if (!empty($values[$key])) {
switch ($values[$key]) {
case (self::FILTER_ACCOUNT_CREATION):
$filterbasedate = (int) $USER->timecreated;
break;
case (self::FILTER_FIRST_LOGIN):
$filterbasedate = (int) $USER->firstaccess;
break;
case (self::FILTER_LAST_LOGIN):
$filterbasedate = (int) $USER->lastlogin;
break;
default:
// Use account creation as default.
$filterbasedate = (int) $USER->timecreated;
break;
}
// If the base date has no value because a user hasn't accessed Moodle yet, default to account creation.
if (empty($filterbasedate)) {
$filterbasedate = (int) $USER->timecreated;
}
if (!empty($values[$range])) {
$filterrange = (int) $values[$range];
} else {
$filterrange = 90 * DAYSECS;
}
// If we're outside the set range from the set base date, filter out tour.
if ((time() > ($filterbasedate + $filterrange))) {
$result = false;
}
}
return $result;
}
}
@@ -0,0 +1,126 @@
<?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/>.
namespace tool_usertours\local\filter;
use tool_usertours\tour;
use context;
/**
* Filter base.
*
* @package tool_usertours
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class base {
/**
* Any Value.
*/
const ANYVALUE = '__ANYVALUE__';
/**
* The name of the filter.
*
* @return string
*/
public static function get_filter_name() {
throw new \coding_exception('get_filter_name() must be defined');
}
/**
* Retrieve the list of available filter options.
*
* @return array An array whose keys are the valid options
*/
public static function get_filter_options() {
return [];
}
/**
* 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) {
return true;
}
/**
* Add the form elements for the filter to the supplied form.
*
* @param MoodleQuickForm $mform The form to add filter settings to.
*/
public static function add_filter_to_form(\MoodleQuickForm &$mform) {
$options = [
static::ANYVALUE => get_string('all'),
];
$options += static::get_filter_options();
$filtername = static::get_filter_name();
$key = "filter_{$filtername}";
$mform->addElement('select', $key, get_string($key, 'tool_usertours'), $options, [
'multiple' => true,
]);
$mform->setDefault($key, static::ANYVALUE);
$mform->addHelpButton($key, $key, 'tool_usertours');
}
/**
* Prepare the filter values for the form.
*
* @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 = static::ANYVALUE;
}
$data->$key = $values;
return $data;
}
/**
* 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;
foreach ($data->$key as $value) {
if ($value === static::ANYVALUE) {
$newvalue = [];
break;
}
}
$tour->set_filter_values($filtername, $newvalue);
}
}
@@ -0,0 +1,89 @@
<?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/>.
namespace tool_usertours\local\filter;
use tool_usertours\tour;
use context;
/**
* Category filter.
*
* @package tool_usertours
* @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 = \core_course_category::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;
}
}
}
@@ -0,0 +1,108 @@
<?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/>.
namespace tool_usertours\local\filter;
use tool_usertours\tour;
use context;
/**
* Course filter.
*
* @package tool_usertours
* @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);
}
}
@@ -0,0 +1,73 @@
<?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/>.
namespace tool_usertours\local\filter;
use tool_usertours\tour;
use context;
/**
* Course format filter.
*
* @package tool_usertours
* @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);
}
}
@@ -0,0 +1,131 @@
<?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/>.
namespace tool_usertours\local\filter;
use tool_usertours\tour;
use context;
/**
* Theme filter.
*
* @package tool_usertours
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class role extends base {
/**
* The Site Admin pseudo-role.
*
* @var ROLE_SITEADMIN int
*/
const ROLE_SITEADMIN = -1;
/**
* The name of the filter.
*
* @return string
*/
public static function get_filter_name() {
return 'role';
}
/**
* 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() {
$allroles = role_get_names(null, ROLENAME_ALIAS);
$roles = [];
foreach ($allroles as $role) {
if ($role->archetype === 'guest') {
// No point in including the 'guest' role as it isn't possible to show tours to a guest.
continue;
}
$roles[$role->shortname] = $role->localname;
}
// Add the Site Administrator pseudo-role.
$roles[self::ROLE_SITEADMIN] = get_string('administrator', 'core');
// Sort alphabetically too.
\core_collator::asort($roles);
return $roles;
}
/**
* 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 $USER;
$values = $tour->get_filter_values(self::get_filter_name());
if (empty($values)) {
// There are no values configured.
// No values means all.
return true;
}
// Presence within the array is sufficient. Ignore any value.
$values = array_flip($values);
if (isset($values[self::ROLE_SITEADMIN]) && is_siteadmin()) {
// This tour has been restricted to a role including site admin, and this user is a site admin.
return true;
}
// Use a request cache to save on DB queries.
// We may be checking multiple tours and they'll all be for the same userid, and contextid.
$cache = \cache::make_from_params(\cache_store::MODE_REQUEST, 'tool_usertours', 'filter_role');
// Get all of the roles used in this context, including special roles such as user, and frontpageuser.
$cachekey = "{$USER->id}_{$context->id}";
$userroles = $cache->get($cachekey);
if ($userroles === false) {
$userroles = get_user_roles_with_special($context);
$cache->set($cachekey, $userroles);
}
// Some special roles do not include the shortname.
// Therefore we must fetch all roles too. Thankfully these don't actually change based on context.
// They do require a DB call, so let's cache it.
$cachekey = "allroles";
$allroles = $cache->get($cachekey);
if ($allroles === false) {
$allroles = get_all_roles();
$cache->set($cachekey, $allroles);
}
// Now we can check whether any of the user roles are in the list of allowed roles for this filter.
foreach ($userroles as $role) {
$shortname = $allroles[$role->roleid]->shortname;
if (isset($values[$shortname])) {
return true;
}
}
return false;
}
}
@@ -0,0 +1,93 @@
<?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/>.
namespace tool_usertours\local\filter;
use tool_usertours\tour;
use context;
/**
* Theme filter.
*
* @package tool_usertours
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class theme extends base {
/**
* The name of the filter.
*
* @return string
*/
public static function get_filter_name() {
return 'theme';
}
/**
* 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() {
$manager = \core_plugin_manager::instance();
$themes = $manager->get_installed_plugins('theme');
$options = [];
foreach (array_keys($themes) as $themename) {
try {
$theme = \theme_config::load($themename);
} catch (Exception $e) {
// Bad theme, just skip it for now.
continue;
}
if ($themename !== $theme->name) {
// Obsoleted or broken theme, just skip for now.
continue;
}
if ($theme->hidefromselector) {
// The theme doesn't want to be shown in the theme selector and as theme
// designer mode is switched off we will respect that decision.
continue;
}
$options[$theme->name] = get_string('pluginname', "theme_{$theme->name}");
}
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 $PAGE;
$values = $tour->get_filter_values('theme');
if (empty($values)) {
// There are no values configured.
// No values means all.
return true;
}
// Presence within the array is sufficient. Ignore any value.
$values = array_flip($values);
return isset($values[$PAGE->theme->name]);
}
}
@@ -0,0 +1,221 @@
<?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/>.
/**
* Form for editing steps.
*
* @package tool_usertours
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_usertours\local\forms;
use stdClass;
use tool_usertours\helper;
use tool_usertours\step;
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
require_once($CFG->libdir . '/formslib.php');
/**
* Form for editing steps.
*
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class editstep extends \moodleform {
/**
* @var tool_usertours\step $step
*/
protected $step;
/**
* @var int Display the step's content by using Moodle language string.
*/
private const CONTENTTYPE_LANGSTRING = 0;
/**
* @var int Display the step's content by entering it manually.
*/
private const CONTENTTYPE_MANUAL = 1;
/**
* Create the edit step form.
*
* @param string $target The target of the form.
* @param step $step The step being editted.
*/
public function __construct($target, \tool_usertours\step $step) {
$this->step = $step;
parent::__construct($target);
}
/**
* Form definition.
*/
public function definition() {
global $CFG;
$mform = $this->_form;
$mform->addElement('header', 'heading_target', get_string('target_heading', 'tool_usertours'));
$types = [];
foreach (\tool_usertours\target::get_target_types() as $value => $type) {
$types[$value] = get_string('target_' . $type, 'tool_usertours');
}
$mform->addElement('select', 'targettype', get_string('targettype', 'tool_usertours'), $types);
$mform->addHelpButton('targettype', 'targettype', 'tool_usertours');
// The target configuration.
foreach (\tool_usertours\target::get_target_types() as $value => $type) {
$targetclass = \tool_usertours\target::get_classname($type);
$targetclass::add_config_to_form($mform);
}
// Content of the step.
$mform->addElement('header', 'heading_content', get_string('content_heading', 'tool_usertours'));
$mform->addElement('textarea', 'title', get_string('title', 'tool_usertours'));
$mform->addRule('title', get_string('required'), 'required', null, 'client');
$mform->setType('title', PARAM_TEXT);
$mform->addHelpButton('title', 'title', 'tool_usertours');
// Content type.
$typeoptions = [
static::CONTENTTYPE_LANGSTRING => get_string('content_type_langstring', 'tool_usertours'),
static::CONTENTTYPE_MANUAL => get_string('content_type_manual', 'tool_usertours'),
];
$mform->addElement('select', 'contenttype', get_string('content_type', 'tool_usertours'), $typeoptions);
$mform->addHelpButton('contenttype', 'content_type', 'tool_usertours');
$mform->setDefault('contenttype', static::CONTENTTYPE_MANUAL);
// Language identifier.
$mform->addElement('textarea', 'contentlangstring', get_string('moodle_language_identifier', 'tool_usertours'));
$mform->setType('contentlangstring', PARAM_TEXT);
$mform->hideIf('contentlangstring', 'contenttype', 'eq', static::CONTENTTYPE_MANUAL);
$editoroptions = [
'subdirs' => 1,
'maxbytes' => $CFG->maxbytes,
'maxfiles' => EDITOR_UNLIMITED_FILES,
'changeformat' => 1,
'trusttext' => true,
];
$mform->addElement('editor', 'content', get_string('content', 'tool_usertours'), null, $editoroptions);
$mform->addHelpButton('content', 'content', 'tool_usertours');
$mform->hideIf('content', 'contenttype', 'eq', static::CONTENTTYPE_LANGSTRING);
// Add the step configuration.
$mform->addElement('header', 'heading_options', get_string('options_heading', 'tool_usertours'));
// All step configuration is defined in the step.
$this->step->add_config_to_form($mform);
// And apply any form constraints.
foreach (\tool_usertours\target::get_target_types() as $value => $type) {
$targetclass = \tool_usertours\target::get_classname($type);
$targetclass::add_disabled_constraints_to_form($mform);
}
$this->add_action_buttons();
}
/**
* Validate the database on the submitted content type.
*
* @param array $data array of ("fieldname"=>value) of submitted data
* @param array $files array of uploaded files "element_name"=>tmp_file_path
* @return array of "element_name"=>"error_description" if there are errors,
* or an empty array if everything is OK (true allowed for backwards compatibility too).
*/
public function validation($data, $files): array {
$errors = parent::validation($data, $files);
if ($data['contenttype'] == static::CONTENTTYPE_LANGSTRING) {
if (!isset($data['contentlangstring']) || trim($data['contentlangstring']) == '') {
$errors['contentlangstring'] = get_string('required');
} else {
$splitted = explode(',', trim($data['contentlangstring']), 2);
$langid = $splitted[0];
$langcomponent = $splitted[1];
if (!get_string_manager()->string_exists($langid, $langcomponent)) {
$errors['contentlangstring'] = get_string('invalid_lang_id', 'tool_usertours');
}
}
}
// Validate manually entered text content. Validation logic derived from \MoodleQuickForm_Rule_Required::validate()
// without the checking of the "strictformsrequired" admin setting.
if ($data['contenttype'] == static::CONTENTTYPE_MANUAL) {
$value = $data['content']['text'] ?? '';
// All tags except img, canvas and hr, plus all forms of whitespaces.
$stripvalues = [
'#</?(?!img|canvas|hr).*?>#im',
'#(\xc2\xa0|\s|&nbsp;)#',
];
$value = preg_replace($stripvalues, '', (string)$value);
if (empty($value)) {
$errors['contenthtmlgrp'] = get_string('required');
}
}
return $errors;
}
/**
* Load in existing data as form defaults. Usually new entry defaults are stored directly in
* form definition (new entry form); this function is used to load in data where values
* already exist and data is being edited (edit entry form).
*
* @param stdClass|array $data object or array of default values
*/
public function set_data($data): void {
$data = (object) $data;
if (!isset($data->contenttype)) {
if (!empty($data->content['text']) && helper::is_language_string_from_input($data->content['text'])) {
$data->contenttype = static::CONTENTTYPE_LANGSTRING;
$data->contentlangstring = $data->content['text'];
// Empty the editor content.
$data->content = ['text' => ''];
} else {
$data->contenttype = static::CONTENTTYPE_MANUAL;
}
}
parent::set_data($data);
}
/**
* Return submitted data if properly submitted or returns NULL if validation fails or
* if there is no submitted data.
*
* @return object|null submitted data; NULL if not valid or not submitted or cancelled
*/
public function get_data(): ?object {
$data = parent::get_data();
if ($data) {
if ($data->contenttype == static::CONTENTTYPE_LANGSTRING) {
$data->content = [
'text' => $data->contentlangstring,
'format' => FORMAT_MOODLE,
];
}
}
return $data;
}
}
@@ -0,0 +1,116 @@
<?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/>.
/**
* Form for editing tours.
*
* @package tool_usertours
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_usertours\local\forms;
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
require_once($CFG->libdir . '/formslib.php');
use tool_usertours\helper;
use tool_usertours\tour;
/**
* Form for editing tours.
*
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class edittour extends \moodleform {
/**
* @var tool_usertours\tour $tour
*/
protected $tour;
/**
* Create the edit tour form.
*
* @param tour $tour The tour being editted.
*/
public function __construct(\tool_usertours\tour $tour) {
$this->tour = $tour;
parent::__construct($tour->get_edit_link());
}
/**
* Form definition.
*/
public function definition() {
$mform = $this->_form;
// ID of existing tour.
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
// Name of the tour.
$mform->addElement('text', 'name', get_string('name', 'tool_usertours'));
$mform->addRule('name', get_string('required'), 'required', null, 'client');
$mform->setType('name', PARAM_TEXT);
$mform->addHelpButton('name', 'name', 'tool_usertours');
// Admin-only descriptions.
$mform->addElement('textarea', 'description', get_string('description', 'tool_usertours'));
$mform->setType('description', PARAM_RAW);
$mform->addHelpButton('description', 'description', 'tool_usertours');
// Application.
$mform->addElement('text', 'pathmatch', get_string('pathmatch', 'tool_usertours'));
$mform->setType('pathmatch', PARAM_RAW);
$mform->addHelpButton('pathmatch', 'pathmatch', 'tool_usertours');
$mform->addElement('checkbox', 'enabled', get_string('tourisenabled', 'tool_usertours'));
$mform->addElement('text', 'endtourlabel', get_string('endtourlabel', 'tool_usertours'));
$mform->setType('endtourlabel', PARAM_TEXT);
$mform->addHelpButton('endtourlabel', 'endtourlabel', 'tool_usertours');
$mform->addElement('checkbox', 'displaystepnumbers', get_string('displaystepnumbers', 'tool_usertours'));
$mform->addHelpButton('displaystepnumbers', 'displaystepnumbers', 'tool_usertours');
$mform->addElement(
'select',
'showtourwhen',
get_string('showtourwhen', 'tool_usertours'),
[
tour::SHOW_TOUR_UNTIL_COMPLETE => get_string('showtouruntilcomplete', 'tool_usertours'),
tour::SHOW_TOUR_ON_EACH_PAGE_VISIT => get_string('showtoureachtime', 'tool_usertours'),
]
);
$mform->setDefault('showtourwhen', tour::SHOW_TOUR_UNTIL_COMPLETE);
// Configuration.
$this->tour->add_config_to_form($mform);
// Filters.
$mform->addElement('header', 'filters', get_string('filter_header', 'tool_usertours'));
$mform->addElement('static', 'filterhelp', '', get_string('filter_help', 'tool_usertours'));
foreach (helper::get_all_filters() as $filterclass) {
$filterclass::add_filter_to_form($mform);
}
$this->add_action_buttons();
}
}
@@ -0,0 +1,56 @@
<?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/>.
/**
* Form for editing tours.
*
* @package tool_usertours
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_usertours\local\forms;
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
require_once($CFG->libdir . '/formslib.php');
/**
* Form for importing tours.
*
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class importtour extends \moodleform {
/**
* Create the import tour form.
*/
public function __construct() {
parent::__construct(\tool_usertours\helper::get_import_tour_link());
}
/**
* Form definition.
*/
public function definition() {
$mform = $this->_form;
$mform->addElement('filepicker', 'tourconfig', get_string('tourconfig', 'tool_usertours'));
$mform->addRule('tourconfig', null, 'required');
$this->add_action_buttons();
}
}
@@ -0,0 +1,159 @@
<?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/>.
/**
* Table to show the list of steps in a tour.
*
* @package tool_usertours
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_usertours\local\table;
defined('MOODLE_INTERNAL') || die();
use tool_usertours\helper;
use tool_usertours\tour;
use tool_usertours\step;
require_once($CFG->libdir . '/tablelib.php');
/**
* Table to show the list of steps in a tour.
*
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class step_list extends \flexible_table {
/**
* @var int $tourid The id of the tour.
*/
protected $tourid;
/**
* Construct the table for the specified tour ID.
*
* @param int $tourid The id of the tour.
*/
public function __construct($tourid) {
parent::__construct('steps');
$this->tourid = $tourid;
$baseurl = new \moodle_url('/tool/usertours/configure.php', [
'id' => $tourid,
]);
$this->define_baseurl($baseurl);
// Column definition.
$this->define_columns([
'title',
'content',
'target',
'actions',
]);
$this->define_headers([
get_string('title', 'tool_usertours'),
get_string('content', 'tool_usertours'),
get_string('target', 'tool_usertours'),
get_string('actions', 'tool_usertours'),
]);
$this->set_attribute('class', 'admintable generaltable steptable');
$this->setup();
}
/**
* Format the current row's title column.
*
* @param step $step The step for this row.
* @return string
*/
protected function col_title(step $step) {
global $OUTPUT;
return $OUTPUT->render(helper::render_stepname_inplace_editable($step));
}
/**
* Format the current row's content column.
*
* @param step $step The step for this row.
* @return string
*/
protected function col_content(step $step) {
$content = $step->get_content();
$systemcontext = \context_system::instance();
$content = file_rewrite_pluginfile_urls(
$content,
'pluginfile.php',
$systemcontext->id,
'tool_usertours',
'stepcontent',
$step->get_id()
);
$content = helper::get_string_from_input($content);
$content = step::get_step_image_from_input($content);
return format_text($content, $step->get_contentformat());
}
/**
* Format the current row's target column.
*
* @param step $step The step for this row.
* @return string
*/
protected function col_target(step $step) {
return $step->get_target()->get_displayname();
}
/**
* Format the current row's actions column.
*
* @param step $step The step for this row.
* @return string
*/
protected function col_actions(step $step) {
$actions = [];
if ($step->is_first_step()) {
$actions[] = helper::get_filler_icon();
} else {
$actions[] = helper::format_icon_link($step->get_moveup_link(), 't/up', get_string('movestepup', 'tool_usertours'));
}
if ($step->is_last_step()) {
$actions[] = helper::get_filler_icon();
} else {
$actions[] = helper::format_icon_link(
$step->get_movedown_link(),
't/down',
get_string('movestepdown', 'tool_usertours')
);
}
$actions[] = helper::format_icon_link($step->get_edit_link(), 't/edit', get_string('edit'));
$actions[] = helper::format_icon_link($step->get_delete_link(), 't/delete', get_string('delete'), 'moodle', [
'data-action' => 'delete',
'data-id' => $step->get_id(),
]);
return implode('&nbsp;', $actions);
}
}
@@ -0,0 +1,164 @@
<?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/>.
/**
* Table to show the list of tours.
*
* @package tool_usertours
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_usertours\local\table;
defined('MOODLE_INTERNAL') || die();
use tool_usertours\helper;
use tool_usertours\tour;
require_once($CFG->libdir . '/tablelib.php');
/**
* Table to show the list of tours.
*
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tour_list extends \flexible_table {
/** @var int The count of all tours. */
protected int $tourcount = 0;
/**
* Construct the tour table.
*/
public function __construct() {
parent::__construct('tours');
$baseurl = new \moodle_url('/tool/usertours/configure.php');
$this->define_baseurl($baseurl);
// Column definition.
$this->define_columns([
'name',
'description',
'appliesto',
'enabled',
'actions',
]);
$this->define_headers([
get_string('name', 'tool_usertours'),
get_string('description', 'tool_usertours'),
get_string('appliesto', 'tool_usertours'),
get_string('enabled', 'tool_usertours'),
get_string('actions', 'tool_usertours'),
]);
$this->set_attribute('class', 'admintable generaltable');
$this->setup();
$this->tourcount = helper::count_tours();
}
/**
* Format the current row's name column.
*
* @param tour $tour The tour for this row.
* @return string
*/
protected function col_name(tour $tour) {
global $OUTPUT;
return $OUTPUT->render(helper::render_tourname_inplace_editable($tour));
}
/**
* Format the current row's description column.
*
* @param tour $tour The tour for this row.
* @return string
*/
protected function col_description(tour $tour) {
global $OUTPUT;
return $OUTPUT->render(helper::render_tourdescription_inplace_editable($tour));
}
/**
* Format the current row's appliesto column.
*
* @param tour $tour The tour for this row.
* @return string
*/
protected function col_appliesto(tour $tour) {
return $tour->get_pathmatch();
}
/**
* Format the current row's enabled column.
*
* @param tour $tour The tour for this row.
* @return string
*/
protected function col_enabled(tour $tour) {
global $OUTPUT;
return $OUTPUT->render(helper::render_tourenabled_inplace_editable($tour));
}
/**
* Format the current row's actions column.
*
* @param tour $tour The tour for this row.
* @return string
*/
protected function col_actions(tour $tour) {
$actions = [];
if ($tour->is_first_tour()) {
$actions[] = helper::get_filler_icon();
} else {
$actions[] = helper::format_icon_link(
$tour->get_moveup_link(),
't/up',
get_string('movetourup', 'tool_usertours')
);
}
if ($tour->is_last_tour($this->tourcount)) {
$actions[] = helper::get_filler_icon();
} else {
$actions[] = helper::format_icon_link(
$tour->get_movedown_link(),
't/down',
get_string('movetourdown', 'tool_usertours')
);
}
$actions[] = helper::format_icon_link($tour->get_view_link(), 't/viewdetails', get_string('view'));
$actions[] = helper::format_icon_link($tour->get_edit_link(), 't/edit', get_string('edit'));
$actions[] = helper::format_icon_link($tour->get_duplicate_link(), 't/copy', get_string('duplicate'));
$actions[] = helper::format_icon_link(
$tour->get_export_link(),
't/export',
get_string('exporttour', 'tool_usertours'),
'tool_usertours'
);
$actions[] = helper::format_icon_link($tour->get_delete_link(), 't/delete', get_string('delete'), null, [
'data-action' => 'delete',
'data-id' => $tour->get_id(),
]);
return implode('&nbsp;', $actions);
}
}
@@ -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/>.
namespace tool_usertours\local\target;
use tool_usertours\step;
/**
* Target base.
*
* @package tool_usertours
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class base {
/**
* @var step $step The step being targetted.
*/
protected $step;
/**
* @var array $forcedsettings The settings forced by this type.
*/
protected static $forcedsettings = [];
/**
* Create the target type.
*
* @param step $step The step being targetted.
*/
public function __construct(step $step) {
$this->step = $step;
}
/**
* Convert the target value to a valid CSS selector for use in the
* output configuration.
*
* @return string
*/
abstract public function convert_to_css();
/**
* Convert the step target to a friendly name for use in the UI.
*
* @return string
*/
abstract public function get_displayname();
/**
* Add the target type configuration to the form.
*
* @param MoodleQuickForm $mform The form to add configuration to.
*/
public static function add_config_to_form(\MoodleQuickForm $mform) {
}
/**
* Add the disabledIf values.
*
* @param MoodleQuickForm $mform The form to add configuration to.
*/
public static function add_disabled_constraints_to_form(\MoodleQuickForm $mform) {
}
/**
* Prepare data to submit to the form.
*
* @param object $data The data being passed to the form
*/
abstract public function prepare_data_for_form($data);
/**
* Whether the specified step setting is forced by this target type.
*
* @param string $key The name of the key to check.
* @return boolean
*/
public function is_setting_forced($key) {
return isset(static::$forcedsettings[$key]);
}
/**
* The value of the forced setting.
*
* @param string $key The name of the key to check.
* @return mixed
*/
public function get_forced_setting_value($key) {
if ($this->is_setting_forced($key)) {
return static::$forcedsettings[$key];
}
return null;
}
/**
* Fetch the targetvalue from the form for this target type.
*
* @param stdClass $data The data submitted in the form
* @return string
*/
abstract public function get_value_from_form($data);
}
@@ -0,0 +1,118 @@
<?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/>.
namespace tool_usertours\local\target;
/**
* Block target.
*
* @package tool_usertours
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block extends base {
/**
* Convert the target value to a valid CSS selector for use in the
* output configuration.
*
* @return string
*/
public function convert_to_css() {
// The block has the following CSS class selector style:
// .block-region .block_[name] .
return sprintf('.block-region .block_%s', $this->step->get_targetvalue());
}
/**
* Convert the step target to a friendly name for use in the UI.
*
* @return string
*/
public function get_displayname() {
return get_string('block_named', 'tool_usertours', $this->get_block_name());
}
/**
* Get the translated name of the block.
*
* @return string
*/
protected function get_block_name() {
return get_string('pluginname', self::get_frankenstyle($this->step->get_targetvalue()));
}
/**
* Get the frankenstyle name of the block.
*
* @param string $block The block name.
* @return The frankenstyle block name.
*/
protected static function get_frankenstyle($block) {
return sprintf('block_%s', $block);
}
/**
* Add the target type configuration to the form.
*
* @param MoodleQuickForm $mform The form to add configuration to.
* @return $this
*/
public static function add_config_to_form(\MoodleQuickForm $mform) {
global $PAGE;
$blocks = [];
foreach ($PAGE->blocks->get_installed_blocks() as $block) {
$blocks[$block->name] = get_string('pluginname', 'block_' . $block->name);
}
\core_collator::asort($blocks);
$mform->addElement('select', 'targetvalue_block', get_string('block', 'tool_usertours'), $blocks);
}
/**
* Add the disabledIf values.
*
* @param MoodleQuickForm $mform The form to add configuration to.
*/
public static function add_disabled_constraints_to_form(\MoodleQuickForm $mform) {
$mform->hideIf(
'targetvalue_block',
'targettype',
'noteq',
\tool_usertours\target::get_target_constant_for_class(self::class)
);
}
/**
* Prepare data to submit to the form.
*
* @param object $data The data being passed to the form
*/
public function prepare_data_for_form($data) {
$data->targetvalue_block = $this->step->get_targetvalue();
}
/**
* Fetch the targetvalue from the form for this target type.
*
* @param stdClass $data The data submitted in the form
* @return string
*/
public function get_value_from_form($data) {
return $data->targetvalue_block;
}
}
@@ -0,0 +1,108 @@
<?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/>.
namespace tool_usertours\local\target;
/**
* Selector target.
*
* @package tool_usertours
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class selector extends base {
/**
* Convert the target value to a valid CSS selector for use in the
* output configuration.
*
* @return string
*/
public function convert_to_css() {
return $this->step->get_targetvalue();
}
/**
* Convert the step target to a friendly name for use in the UI.
*
* @return string
*/
public function get_displayname() {
return get_string('selectordisplayname', 'tool_usertours', $this->step->get_targetvalue());
}
/**
* Get the default title.
*
* @return string
*/
public function get_default_title() {
return get_string('selector_defaulttitle', 'tool_usertours');
}
/**
* Get the default content.
*
* @return string
*/
public function get_default_content() {
return get_string('selector_defaultcontent', 'tool_usertours');
}
/**
* Add the target type configuration to the form.
*
* @param MoodleQuickForm $mform The form to add configuration to.
* @return $this
*/
public static function add_config_to_form(\MoodleQuickForm $mform) {
$mform->addElement('text', 'targetvalue_selector', get_string('cssselector', 'tool_usertours'));
$mform->setType('targetvalue_selector', PARAM_RAW);
$mform->addHelpButton('targetvalue_selector', 'target_selector_targetvalue', 'tool_usertours');
}
/**
* Add the disabledIf values.
*
* @param MoodleQuickForm $mform The form to add configuration to.
*/
public static function add_disabled_constraints_to_form(\MoodleQuickForm $mform) {
$mform->hideIf(
'targetvalue_selector',
'targettype',
'noteq',
\tool_usertours\target::get_target_constant_for_class(self::class)
);
}
/**
* Prepare data to submit to the form.
*
* @param object $data The data being passed to the form
*/
public function prepare_data_for_form($data) {
$data->targetvalue_selector = $this->step->get_targetvalue();
}
/**
* Fetch the targetvalue from the form for this target type.
*
* @param stdClass $data The data submitted in the form
* @return string
*/
public function get_value_from_form($data) {
return $data->targetvalue_selector;
}
}
@@ -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/>.
namespace tool_usertours\local\target;
/**
* A step designed to be orphaned.
*
* @package tool_usertours
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class unattached extends base {
/**
* @var array $forcedsettings The settings forced by this type.
*/
protected static $forcedsettings = [
'placement' => 'top',
'orphan' => true,
'reflex' => false,
];
/**
* Convert the target value to a valid CSS selector for use in the
* output configuration.
*
* @return string
*/
public function convert_to_css() {
return '';
}
/**
* Convert the step target to a friendly name for use in the UI.
*
* @return string
*/
public function get_displayname() {
return get_string('target_unattached', 'tool_usertours');
}
/**
* Add the target type configuration to the form.
*
* @param MoodleQuickForm $mform The form to add configuration to.
* @return $this
*/
public static function add_config_to_form(\MoodleQuickForm $mform) {
// There is no relevant value here.
$mform->addElement('hidden', 'targetvalue_unattached', '');
$mform->setType('targetvalue_unattached', PARAM_TEXT);
}
/**
* Add the disabledIf values.
*
* @param MoodleQuickForm $mform The form to add configuration to.
*/
public static function add_disabled_constraints_to_form(\MoodleQuickForm $mform) {
$myvalue = \tool_usertours\target::get_target_constant_for_class(self::class);
foreach (array_keys(self::$forcedsettings) as $settingname) {
$mform->hideIf($settingname, 'targettype', 'eq', $myvalue);
}
}
/**
* Prepare data to submit to the form.
*
* @param object $data The data being passed to the form
*/
public function prepare_data_for_form($data) {
$data->targetvalue_unattached = '';
}
/**
* Fetch the targetvalue from the form for this target type.
*
* @param stdClass $data The data submitted in the form
* @return string
*/
public function get_value_from_form($data) {
return '';
}
}