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
+182
View File
@@ -0,0 +1,182 @@
<?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 class for mybackpack.php
*
* @package core
* @subpackage badges
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/
namespace core_badges\form;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->libdir . '/badgeslib.php');
use html_writer;
use moodleform;
use stdClass;
/**
* Form to edit backpack initial details.
*
*/
class backpack extends external_backpack {
/**
* Defines the form
*/
public function definition() {
global $USER, $PAGE, $OUTPUT, $CFG;
$mform = $this->_form;
$this->_customdata['userbackpack'] = 1;
$mform->addElement('html', html_writer::tag('span', '', array('class' => 'notconnected', 'id' => 'connection-error')));
$mform->addElement('header', 'backpackheader', get_string('backpackconnection', 'badges'));
$mform->addHelpButton('backpackheader', 'backpackconnection', 'badges');
$mform->addElement('hidden', 'userid', $USER->id);
$mform->setType('userid', PARAM_INT);
$freeze = [];
$status = null;
if (isset($this->_customdata['email'])) {
// Email will be passed in when we're in the process of verifying the user's email address,
// so set the connection status, lock the email field, and provide options to resend the verification
// email or cancel the verification process entirely and start over.
$freeze = ['backpackemail'];
$mform->addElement('hidden', 'password', $this->_customdata['backpackpassword']);
$mform->setType('password', PARAM_RAW);
$mform->addElement('hidden', 'externalbackpackid', $this->_customdata['backpackid']);
$mform->setType('externalbackpackid', PARAM_INT);
$status = html_writer::tag('span', get_string('backpackemailverificationpending', 'badges'),
array('class' => 'notconnected', 'id' => 'connection-status'));
} else {
$sitebackpacks = badges_get_site_backpacks();
$choices = [
'' => get_string('choosedots'),
];
$restrictedoptions = [''];
foreach ($sitebackpacks as $backpack) {
$choices[$backpack->id] = $backpack->backpackweburl;
if ($backpack->apiversion == OPEN_BADGES_V2P1) {
$restrictedoptions[] = $backpack->id;
}
}
$mform->addElement('select', 'externalbackpackid', get_string('backpackprovider', 'badges'), $choices);
$mform->setType('externalbackpackid', PARAM_INT);
$mform->addRule('externalbackpackid', get_string('required'), 'required');
$mform->hideIf('password', 'externalbackpackid', 'in', $restrictedoptions);
$mform->hideIf('backpackemail', 'externalbackpackid', 'in', $restrictedoptions);
// Static form element can't be used because they don't support hideIf. This is a workaround until MDL-66251 is fixed.
$group = [];
$group[] = $mform->createElement('static', 'loginbackpackgroup', '', get_string('loginbackpacktitle', 'badges'));
$mform->addGroup($group, 'loginbackpackgroup', '', '', false);
$mform->hideIf('loginbackpackgroup', 'externalbackpackid', 'in', $restrictedoptions);
}
if ($status) {
// Only display the status if it's set.
$mform->addElement('static', 'status', get_string('status'), $status);
}
$this->add_auth_fields($this->_customdata['email'] ?? $USER->email, !isset($this->_customdata['email']));
// Only display email and password when the user has selected a backpack.
$mform->hideIf('backpackemail', 'externalbackpackid', 'eq', '');
$mform->hideIf('password', 'externalbackpackid', 'eq', '');
$mform->setDisableShortforms(false);
// Freeze any elemnts after definition.
if ($freeze) {
$mform->freeze($freeze);
}
$this->add_action_buttons();
}
/**
* Override add_action_buttons
*
* @param bool $cancel
* @param null|text $submitlabel
*/
public function add_action_buttons($cancel = true, $submitlabel = null) {
$mform = $this->_form;
if (isset($this->_customdata['email'])) {
$buttonarray = [];
$buttonarray[] = &$mform->createElement('submit', 'submitbutton',
get_string('backpackconnectionresendemail', 'badges'));
$buttonarray[] = &$mform->createElement('submit', 'revertbutton',
get_string('backpackconnectioncancelattempt', 'badges'));
$mform->addGroup($buttonarray, 'buttonar', '', [''], false);
$mform->closeHeaderBefore('buttonar');
} else {
// Email isn't present, so provide an input element to get it and a button to start the verification process.
parent::add_action_buttons(false, get_string('backpackconnectionconnect', 'badges'));
}
}
/**
* Validates form data
*/
public function validation($data, $files) {
// Verify that the user has selected a backpack.
if (empty($data['externalbackpackid'])) {
$errors['externalbackpackid'] = get_string('externalbackpack_required', 'badges');
return $errors;
}
// We don't need to verify anything for OBv2.1.
if (badges_open_badges_backpack_api() == OPEN_BADGES_V2P1) {
return [];
}
// We don't need to verify the email address if we're clearing a pending email verification attempt.
if (isset($data['revertbutton'])) {
return [];
}
$errors = [];
// Email and password can't be blank.
if (empty($data['backpackemail'])) {
$errors['backpackemail'] = get_string('backpackemail_required', 'badges');
}
if (empty($data['password'])) {
$errors['password'] = get_string('password_required', 'badges');
}
if (!empty($errors)) {
return $errors;
}
// Check the given credentials (email and password) are valid for this backpack.
$check = new stdClass();
$check->email = $data['backpackemail'];
$check->password = $data['password'];
$sitebackpack = badges_get_site_backpack($data['externalbackpackid']);
$bp = new \core_badges\backpack_api($sitebackpack, $check);
$result = $bp->authenticate();
if ($result === false || !empty($result->error)) {
$msg = $bp->get_authentication_error();
$errors['backpackemail'] = get_string('backpackconnectionunexpectedresult', 'badges', $msg);
}
return $errors;
}
}
+240
View File
@@ -0,0 +1,240 @@
<?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 classes for editing badges
*
* @package core
* @subpackage badges
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/
namespace core_badges\form;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->libdir . '/badgeslib.php');
require_once($CFG->libdir . '/filelib.php');
use moodleform;
/**
* Form to edit badge details.
*
*/
class badge extends moodleform {
/**
* Defines the form
*/
public function definition() {
global $CFG;
$mform = $this->_form;
$badge = (isset($this->_customdata['badge'])) ? $this->_customdata['badge'] : false;
$action = $this->_customdata['action'];
$mform->addElement('header', 'badgedetails', get_string('badgedetails', 'badges'));
$mform->addElement('text', 'name', get_string('name'), array('size' => '70'));
// When downloading badge, it will be necessary to clean the name as PARAM_FILE.
$mform->setType('name', PARAM_TEXT);
$mform->addRule('name', null, 'required');
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->addElement('text', 'version', get_string('version', 'badges'), array('size' => '70'));
$mform->setType('version', PARAM_TEXT);
$mform->addHelpButton('version', 'version', 'badges');
$languages = get_string_manager()->get_list_of_languages();
$mform->addElement('select', 'language', get_string('language'), $languages);
$mform->addHelpButton('language', 'language', 'badges');
$mform->addElement('textarea', 'description', get_string('description', 'badges'), 'wrap="virtual" rows="8" cols="70"');
$mform->setType('description', PARAM_NOTAGS);
$mform->addRule('description', null, 'required');
$str = $action == 'new' ? get_string('badgeimage', 'badges') : get_string('newimage', 'badges');
$imageoptions = array('maxbytes' => 262144, 'accepted_types' => array('optimised_image'));
$mform->addElement('filepicker', 'image', $str, null, $imageoptions);
if ($action == 'new') {
$mform->addRule('image', null, 'required');
} else {
$currentimage = $mform->createElement('static', 'currentimage', get_string('currentimage', 'badges'));
$mform->insertElementBefore($currentimage, 'image');
}
$mform->addHelpButton('image', 'badgeimage', 'badges');
$mform->addElement('text', 'imageauthorname', get_string('imageauthorname', 'badges'), array('size' => '70'));
$mform->setType('imageauthorname', PARAM_TEXT);
$mform->addHelpButton('imageauthorname', 'imageauthorname', 'badges');
$mform->addElement('text', 'imageauthoremail', get_string('imageauthoremail', 'badges'), array('size' => '70'));
$mform->setType('imageauthoremail', PARAM_TEXT);
$mform->addHelpButton('imageauthoremail', 'imageauthoremail', 'badges');
$mform->addElement('text', 'imageauthorurl', get_string('imageauthorurl', 'badges'), array('size' => '70'));
$mform->setType('imageauthorurl', PARAM_URL);
$mform->addHelpButton('imageauthorurl', 'imageauthorurl', 'badges');
$mform->addElement('text', 'imagecaption', get_string('imagecaption', 'badges'), array('size' => '70'));
$mform->setType('imagecaption', PARAM_TEXT);
$mform->addHelpButton('imagecaption', 'imagecaption', 'badges');
$mform->addElement('tags', 'tags', get_string('tags', 'badges'), ['itemtype' => 'badge', 'component' => 'core_badges']);
if (badges_open_badges_backpack_api() == OPEN_BADGES_V1) {
$mform->addElement('header', 'issuerdetails', get_string('issuerdetails', 'badges'));
$mform->addElement('text', 'issuername', get_string('name'), array('size' => '70'));
$mform->setType('issuername', PARAM_NOTAGS);
$mform->addRule('issuername', null, 'required');
if (isset($CFG->badges_defaultissuername)) {
$mform->setDefault('issuername', $CFG->badges_defaultissuername);
}
$mform->addHelpButton('issuername', 'issuername', 'badges');
$mform->addElement('text', 'issuercontact', get_string('contact', 'badges'), array('size' => '70'));
if (isset($CFG->badges_defaultissuercontact)) {
$mform->setDefault('issuercontact', $CFG->badges_defaultissuercontact);
}
$mform->setType('issuercontact', PARAM_RAW);
$mform->addHelpButton('issuercontact', 'contact', 'badges');
// Set issuer URL.
// Have to parse URL because badge issuer origin cannot be a subfolder in wwwroot.
$url = parse_url($CFG->wwwroot);
$mform->addElement('hidden', 'issuerurl', $url['scheme'] . '://' . $url['host']);
$mform->setType('issuerurl', PARAM_URL);
}
$mform->addElement('header', 'issuancedetails', get_string('issuancedetails', 'badges'));
$issuancedetails = array();
$issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('never', 'badges'), 0);
$issuancedetails[] =& $mform->createElement('static', 'none_break', null, '<br/>');
$issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('fixed', 'badges'), 1);
$issuancedetails[] =& $mform->createElement('date_selector', 'expiredate', '');
$issuancedetails[] =& $mform->createElement('static', 'expirydate_break', null, '<br/>');
$issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('relative', 'badges'), 2);
$issuancedetails[] =& $mform->createElement('duration', 'expireperiod', '', array('defaultunit' => 86400, 'optional' => false));
$issuancedetails[] =& $mform->createElement('static', 'expiryperiods_break', null, get_string('after', 'badges'));
$mform->addGroup($issuancedetails, 'expirydategr', get_string('expirydate', 'badges'), array(' '), false);
$mform->addHelpButton('expirydategr', 'expirydate', 'badges');
$mform->setDefault('expiry', 0);
$mform->setDefault('expiredate', strtotime('+1 year'));
$mform->disabledIf('expiredate[day]', 'expiry', 'neq', 1);
$mform->disabledIf('expiredate[month]', 'expiry', 'neq', 1);
$mform->disabledIf('expiredate[year]', 'expiry', 'neq', 1);
$mform->disabledIf('expireperiod[number]', 'expiry', 'neq', 2);
$mform->disabledIf('expireperiod[timeunit]', 'expiry', 'neq', 2);
$mform->addElement('hidden', 'action', $action);
$mform->setType('action', PARAM_TEXT);
if ($action == 'new') {
// Try to set default badge language to that of current language, or it's parent.
$language = current_language();
if (isset($languages[$language])) {
$defaultlanguage = $language;
} else {
// Calling get_parent_language returns an empty string instead of 'en'.
$defaultlanguage = get_parent_language($language) ?: 'en';
}
$mform->setDefault('language', $defaultlanguage);
$this->add_action_buttons(true, get_string('createbutton', 'badges'));
} else {
// Add hidden fields.
$mform->addElement('hidden', 'id', $badge->id);
$mform->setType('id', PARAM_INT);
$this->add_action_buttons();
$this->set_data($badge);
// Freeze all elements if badge is active or locked.
if ($badge->is_active() || $badge->is_locked()) {
$mform->hardFreezeAllVisibleExcept(array());
}
}
}
/**
* Load in existing data as form defaults
*
* @param stdClass|array $badge object or array of default values
*/
public function set_data($badge) {
$defaultvalues = [];
parent::set_data($badge);
if (!empty($badge->expiredate)) {
$defaultvalues['expiry'] = 1;
$defaultvalues['expiredate'] = $badge->expiredate;
} else if (!empty($badge->expireperiod)) {
$defaultvalues['expiry'] = 2;
$defaultvalues['expireperiod'] = $badge->expireperiod;
}
$defaultvalues['tags'] = \core_tag_tag::get_item_tags_array('core_badges', 'badge', $badge->id);
$defaultvalues['currentimage'] = print_badge_image($badge, $badge->get_context(), 'large');
parent::set_data($defaultvalues);
}
/**
* Validates form data
*/
public function validation($data, $files) {
global $DB;
$errors = parent::validation($data, $files);
if (badges_open_badges_backpack_api() == OPEN_BADGES_V1) {
if (!empty($data['issuercontact']) && !validate_email($data['issuercontact'])) {
$errors['issuercontact'] = get_string('invalidemail');
}
}
if ($data['expiry'] == 2 && $data['expireperiod'] <= 0) {
$errors['expirydategr'] = get_string('error:invalidexpireperiod', 'badges');
}
if ($data['expiry'] == 1 && $data['expiredate'] <= time()) {
$errors['expirydategr'] = get_string('error:invalidexpiredate', 'badges');
}
if ($data['imageauthoremail'] && !validate_email($data['imageauthoremail'])) {
$errors['imageauthoremail'] = get_string('invalidemail');
}
// Check for duplicate badge names.
if ($data['action'] == 'new') {
$duplicate = $DB->record_exists_select('badge', 'name = :name AND status != :deleted',
array('name' => $data['name'], 'deleted' => BADGE_STATUS_ARCHIVED));
} else {
$duplicate = $DB->record_exists_select('badge', 'name = :name AND id != :badgeid AND status != :deleted',
array('name' => $data['name'], 'badgeid' => $data['id'], 'deleted' => BADGE_STATUS_ARCHIVED));
}
if ($duplicate) {
$errors['name'] = get_string('error:duplicatename', 'badges');
}
if ($data['imageauthorurl'] && !preg_match('@^https?://.+@', $data['imageauthorurl'])) {
$errors['imageauthorurl'] = get_string('invalidurl', 'badges');
}
return $errors;
}
}
+124
View File
@@ -0,0 +1,124 @@
<?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 class for mybackpack.php
*
* @package core
* @subpackage badges
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/
namespace core_badges\form;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->libdir . '/badgeslib.php');
use html_writer;
use moodleform;
/**
* Form to select backpack collections.
*
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class collections extends moodleform {
/**
* Defines the form
*/
public function definition() {
global $USER;
$mform = $this->_form;
$email = $this->_customdata['email'];
$backpackweburl = $this->_customdata['backpackweburl'];
$selected = $this->_customdata['selected'];
if (isset($this->_customdata['groups'])) {
$groups = $this->_customdata['groups'];
$nogroups = null;
} else {
$groups = null;
$nogroups = $this->_customdata['nogroups'];
}
$backpack = get_backpack_settings($USER->id);
$sitebackpack = badges_get_site_backpack($backpack->backpackid);
$mform->addElement('header', 'backpackheader', get_string('backpackconnection_connected', 'badges'));
$mform->addElement('static', 'url', get_string('url'), $backpackweburl);
$status = html_writer::tag('span', get_string('connected', 'badges'), array('class' => 'connected'));
$mform->addElement('static', 'status', get_string('status'), $status);
$mform->addElement('static', 'email', get_string('email'), $email);
$mform->addElement('submit', 'disconnect', get_string('disconnect', 'badges'));
$mform->addElement('header', 'collectionheader', get_string('backpackimport', 'badges'));
$mform->addHelpButton('collectionheader', 'backpackimport', 'badges');
$hasgroups = false;
if (!empty($groups)) {
foreach ($groups as $group) {
$count = 0;
// Handle attributes based on backpack's supported version.
if ($sitebackpack->apiversion == OPEN_BADGES_V2) {
// OpenBadges v2 data attributes.
if (empty($group->published)) {
// Only public collections.
continue;
}
// Get the number of badges associated with this collection from the assertions array returned.
$count = count($group->assertions);
} else {
// OpenBadges v1 data attributes.
$group->entityId = $group->groupId;
// Get the number of badges associated with this collection. In that case, the number is returned directly.
$count = $group->badges;
}
if (!$hasgroups) {
$mform->addElement('static', 'selectgroup', '', get_string('selectgroup_start', 'badges'));
}
$hasgroups = true;
$name = $group->name . ' (' . $count . ')';
$mform->addElement(
'advcheckbox',
'group[' . $group->entityId . ']',
null,
$name,
array('group' => 1),
array(false, $group->entityId)
);
if (in_array($group->entityId, $selected)) {
$mform->setDefault('group[' . $group->entityId . ']', $group->entityId);
}
}
$mform->addElement('static', 'selectgroup', '', get_string('selectgroup_end', 'badges', $backpackweburl));
}
if (!$hasgroups) {
$mform->addElement('static', 'selectgroup', '', $nogroups);
}
$this->add_action_buttons();
}
}
+184
View File
@@ -0,0 +1,184 @@
<?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/>.
/**
* External backpack form
*
* @package core_badges
* @copyright 2019 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_badges\form;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/formslib.php');
/**
* Backpack form class.
*
* @package core_badges
* @copyright 2019 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class external_backpack extends \moodleform {
/**
* Create the form.
*
*/
public function definition() {
global $CFG;
$mform = $this->_form;
$backpack = false;
if (isset($this->_customdata['externalbackpack'])) {
$backpack = $this->_customdata['externalbackpack'];
}
$mform->addElement('hidden', 'action', 'edit');
$mform->setType('action', PARAM_ALPHA);
$apiversions = badges_get_badge_api_versions();
$mform->addElement('select', 'apiversion', get_string('apiversion', 'core_badges'), $apiversions);
$mform->setType('apiversion', PARAM_RAW);
$mform->setDefault('apiversion', OPEN_BADGES_V2P1);
$mform->addRule('apiversion', null, 'required', null, 'client');
$mform->addElement('text', 'backpackweburl', get_string('backpackweburl', 'core_badges'));
$mform->setType('backpackweburl', PARAM_URL);
$mform->addRule('backpackweburl', null, 'required', null, 'client');
$mform->addRule('backpackweburl', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->addElement('text', 'backpackapiurl', get_string('backpackapiurl', 'core_badges'));
$mform->setType('backpackapiurl', PARAM_URL);
$mform->addRule('backpackapiurl', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->addElement('hidden', 'id', ($backpack->id ?? null));
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'badgebackpack', 0);
$mform->setType('badgebackpack', PARAM_INT);
$mform->addElement('hidden', 'userid', 0);
$mform->setType('userid', PARAM_INT);
$mform->addElement('hidden', 'backpackuid', 0);
$mform->setType('backpackuid', PARAM_INT);
$mform->addElement('advcheckbox', 'includeauthdetails', null, get_string('includeauthdetails', 'core_badges'));
if (!empty($backpack->backpackemail) || !empty($backpack->password)) {
$mform->setDefault('includeauthdetails', 1);
}
$issuercontact = $CFG->badges_defaultissuercontact;
$this->add_auth_fields($issuercontact);
if ($backpack) {
$this->set_data($backpack);
}
$mform->hideIf('includeauthdetails', 'apiversion', 'in', [OPEN_BADGES_V2P1]);
$mform->hideIf('backpackemail', 'includeauthdetails');
$mform->hideIf('backpackemail', 'apiversion', 'in', [OPEN_BADGES_V2P1]);
$mform->hideIf('password', 'includeauthdetails');
$mform->hideIf('password', 'apiversion', 'in', [OPEN_BADGES_V1, OPEN_BADGES_V2P1]);
$mform->hideIf('backpackapiurl', 'apiversion', 'in', [OPEN_BADGES_V1, OPEN_BADGES_V2P1]);
// Disable short forms.
$mform->setDisableShortforms();
$this->add_action_buttons();
}
/**
* Validate the data from the form.
*
* @param array $data form data
* @param array $files form files
* @return array An array of error messages.
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
// Ensure backpackapiurl and backpackweburl are valid URLs.
$isobv21 = isset($data['apiversion']) && $data['apiversion'] == OPEN_BADGES_V2P1;
if (!$isobv21) {
if (empty($data['backpackapiurl'])) {
$errors['backpackapiurl'] = get_string('err_required', 'form');
} else if (!preg_match('@^https?://.+@', $data['backpackapiurl'])) {
$errors['backpackapiurl'] = get_string('invalidurl', 'badges');
}
}
if (!empty($data['backpackweburl']) && !preg_match('@^https?://.+@', $data['backpackweburl'])) {
$errors['backpackweburl'] = get_string('invalidurl', 'badges');
}
return $errors;
}
/**
* Return submitted data if properly submitted or returns NULL if validation fails or
* if there is no submitted data.
*
* @return object|void
*/
public function get_data() {
$data = parent::get_data();
if ($data ) {
if ((isset($data->includeauthdetails) && !$data->includeauthdetails)
|| (isset($data->apiversion) && $data->apiversion == 2.1)) {
$data->backpackemail = "";
$data->password = "";
}
if ((isset($data->apiversion) && $data->apiversion == 1)) {
$data->password = "";
}
}
return $data;
}
/**
* Add backpack specific auth details.
*
* @param string|null $email The email addressed provided or null if it's new.
* @param bool $includepassword Include the password field. Defaults to true
* @throws \coding_exception
*/
protected function add_auth_fields(?string $email, bool $includepassword = true) {
$mform = $this->_form;
$emailstring = get_string('email');
$passwordstring = get_string('password');
$showpasswordhelp = false;
if (!isset($this->_customdata['userbackpack'])) {
$emailstring = get_string('defaultissuercontact', 'core_badges');
$passwordstring = get_string('defaultissuerpassword', 'core_badges');
$showpasswordhelp = true;
}
$mform->addElement('text', 'backpackemail', $emailstring);
$mform->setType('backpackemail', PARAM_EMAIL);
$mform->setDefault('backpackemail', $email);
if ($includepassword) {
$mform->addElement('passwordunmask', 'password', $passwordstring);
$mform->setType('password', PARAM_RAW);
if ($showpasswordhelp) {
$mform->addHelpButton('password', 'defaultissuerpassword', 'badges');
}
}
}
}
+94
View File
@@ -0,0 +1,94 @@
<?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 class for badge message.
*
* @package core
* @subpackage badges
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/
namespace core_badges\form;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->libdir . '/badgeslib.php');
require_once($CFG->libdir . '/filelib.php');
use moodleform;
/**
* Form to edit badge message.
*
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class message extends moodleform {
/**
* Create the form.
*/
public function definition() {
global $CFG, $OUTPUT;
$mform = $this->_form;
$badge = $this->_customdata['badge'];
$action = $this->_customdata['action'];
$editoroptions = $this->_customdata['editoroptions'];
// Add hidden fields.
$mform->addElement('hidden', 'id', $badge->id);
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'action', $action);
$mform->setType('action', PARAM_TEXT);
$mform->addElement('header', 'badgemessage', get_string('configuremessage', 'badges'));
$mform->addHelpButton('badgemessage', 'variablesubstitution', 'badges');
$mform->addElement('text', 'messagesubject', get_string('subject', 'badges'), array('size' => '70'));
$mform->setType('messagesubject', PARAM_TEXT);
$mform->addRule('messagesubject', null, 'required');
$mform->addRule('messagesubject', get_string('maximumchars', '', 255), 'maxlength', 255);
$mform->addElement('editor', 'message_editor', get_string('message', 'badges'), null, $editoroptions);
$mform->setType('message_editor', PARAM_RAW);
$mform->addRule('message_editor', null, 'required');
$mform->addElement('advcheckbox', 'attachment', get_string('attachment', 'badges'), '', null, array(0, 1));
$mform->addHelpButton('attachment', 'attachment', 'badges');
if (empty($CFG->allowattachments)) {
$mform->freeze('attachment');
}
$options = array(
BADGE_MESSAGE_NEVER => get_string('never'),
BADGE_MESSAGE_ALWAYS => get_string('notifyevery', 'badges'),
BADGE_MESSAGE_DAILY => get_string('notifydaily', 'badges'),
BADGE_MESSAGE_WEEKLY => get_string('notifyweekly', 'badges'),
BADGE_MESSAGE_MONTHLY => get_string('notifymonthly', 'badges'),
);
$mform->addElement('select', 'notification', get_string('notification', 'badges'), $options);
$mform->addHelpButton('notification', 'notification', 'badges');
$this->add_action_buttons();
$this->set_data($badge);
}
}