PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/badges/edit_form.php

https://bitbucket.org/moodle/moodle
PHP | 251 lines | 159 code | 43 blank | 49 comment | 28 complexity | 0ef91aa17a717bfc978f3deccbf84160 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Form classes for editing badges
  18. *
  19. * @package core
  20. * @subpackage badges
  21. * @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. * @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. require_once($CFG->libdir . '/formslib.php');
  27. require_once($CFG->libdir . '/badgeslib.php');
  28. require_once($CFG->libdir . '/filelib.php');
  29. /**
  30. * Form to edit badge details.
  31. *
  32. */
  33. class edit_details_form extends moodleform {
  34. /**
  35. * Defines the form
  36. */
  37. public function definition() {
  38. global $CFG;
  39. $mform = $this->_form;
  40. $badge = (isset($this->_customdata['badge'])) ? $this->_customdata['badge'] : false;
  41. $action = $this->_customdata['action'];
  42. $mform->addElement('header', 'badgedetails', get_string('badgedetails', 'badges'));
  43. $mform->addElement('text', 'name', get_string('name'), array('size' => '70'));
  44. // Using PARAM_FILE to avoid problems later when downloading badge files.
  45. $mform->setType('name', PARAM_FILE);
  46. $mform->addRule('name', null, 'required');
  47. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  48. $mform->addElement('textarea', 'description', get_string('description', 'badges'), 'wrap="virtual" rows="8" cols="70"');
  49. $mform->setType('description', PARAM_NOTAGS);
  50. $mform->addRule('description', null, 'required');
  51. $str = $action == 'new' ? get_string('badgeimage', 'badges') : get_string('newimage', 'badges');
  52. $imageoptions = array('maxbytes' => 262144, 'accepted_types' => array('web_image'));
  53. $mform->addElement('filepicker', 'image', $str, null, $imageoptions);
  54. if ($action == 'new') {
  55. $mform->addRule('image', null, 'required');
  56. } else {
  57. $currentimage = $mform->createElement('static', 'currentimage', get_string('currentimage', 'badges'));
  58. $mform->insertElementBefore($currentimage, 'image');
  59. }
  60. $mform->addHelpButton('image', 'badgeimage', 'badges');
  61. $mform->addElement('header', 'issuerdetails', get_string('issuerdetails', 'badges'));
  62. $mform->addElement('text', 'issuername', get_string('name'), array('size' => '70'));
  63. $mform->setType('issuername', PARAM_NOTAGS);
  64. $mform->addRule('issuername', null, 'required');
  65. if (isset($CFG->badges_defaultissuername)) {
  66. $mform->setDefault('issuername', $CFG->badges_defaultissuername);
  67. }
  68. $mform->addHelpButton('issuername', 'issuername', 'badges');
  69. $mform->addElement('text', 'issuercontact', get_string('contact', 'badges'), array('size' => '70'));
  70. if (isset($CFG->badges_defaultissuercontact)) {
  71. $mform->setDefault('issuercontact', $CFG->badges_defaultissuercontact);
  72. }
  73. $mform->setType('issuercontact', PARAM_RAW);
  74. $mform->addHelpButton('issuercontact', 'contact', 'badges');
  75. $mform->addElement('header', 'issuancedetails', get_string('issuancedetails', 'badges'));
  76. $issuancedetails = array();
  77. $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('never', 'badges'), 0);
  78. $issuancedetails[] =& $mform->createElement('static', 'none_break', null, '<br/>');
  79. $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('fixed', 'badges'), 1);
  80. $issuancedetails[] =& $mform->createElement('date_selector', 'expiredate', '');
  81. $issuancedetails[] =& $mform->createElement('static', 'expirydate_break', null, '<br/>');
  82. $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('relative', 'badges'), 2);
  83. $issuancedetails[] =& $mform->createElement('duration', 'expireperiod', '', array('defaultunit' => 86400, 'optional' => false));
  84. $issuancedetails[] =& $mform->createElement('static', 'expiryperiods_break', null, get_string('after', 'badges'));
  85. $mform->addGroup($issuancedetails, 'expirydategr', get_string('expirydate', 'badges'), array(' '), false);
  86. $mform->addHelpButton('expirydategr', 'expirydate', 'badges');
  87. $mform->setDefault('expiry', 0);
  88. $mform->setDefault('expiredate', strtotime('+1 year'));
  89. $mform->disabledIf('expiredate[day]', 'expiry', 'neq', 1);
  90. $mform->disabledIf('expiredate[month]', 'expiry', 'neq', 1);
  91. $mform->disabledIf('expiredate[year]', 'expiry', 'neq', 1);
  92. $mform->disabledIf('expireperiod[number]', 'expiry', 'neq', 2);
  93. $mform->disabledIf('expireperiod[timeunit]', 'expiry', 'neq', 2);
  94. // Set issuer URL.
  95. // Have to parse URL because badge issuer origin cannot be a subfolder in wwwroot.
  96. $url = parse_url($CFG->wwwroot);
  97. $mform->addElement('hidden', 'issuerurl', $url['scheme'] . '://' . $url['host']);
  98. $mform->setType('issuerurl', PARAM_URL);
  99. $mform->addElement('hidden', 'action', $action);
  100. $mform->setType('action', PARAM_TEXT);
  101. if ($action == 'new') {
  102. $this->add_action_buttons(true, get_string('createbutton', 'badges'));
  103. } else {
  104. // Add hidden fields.
  105. $mform->addElement('hidden', 'id', $badge->id);
  106. $mform->setType('id', PARAM_INT);
  107. $this->add_action_buttons();
  108. $this->set_data($badge);
  109. // Freeze all elements if badge is active or locked.
  110. if ($badge->is_active() || $badge->is_locked()) {
  111. $mform->hardFreezeAllVisibleExcept(array());
  112. }
  113. }
  114. }
  115. /**
  116. * Load in existing data as form defaults
  117. *
  118. * @param stdClass|array $default_values object or array of default values
  119. */
  120. public function set_data($badge) {
  121. $default_values = array();
  122. parent::set_data($badge);
  123. if (!empty($badge->expiredate)) {
  124. $default_values['expiry'] = 1;
  125. $default_values['expiredate'] = $badge->expiredate;
  126. } else if (!empty($badge->expireperiod)) {
  127. $default_values['expiry'] = 2;
  128. $default_values['expireperiod'] = $badge->expireperiod;
  129. }
  130. $default_values['currentimage'] = print_badge_image($badge, $badge->get_context(), 'large');
  131. parent::set_data($default_values);
  132. }
  133. /**
  134. * Validates form data
  135. */
  136. public function validation($data, $files) {
  137. global $DB;
  138. $errors = parent::validation($data, $files);
  139. if (!empty($data['issuercontact']) && !validate_email($data['issuercontact'])) {
  140. $errors['issuercontact'] = get_string('invalidemail');
  141. }
  142. if ($data['expiry'] == 2 && $data['expireperiod'] <= 0) {
  143. $errors['expirydategr'] = get_string('error:invalidexpireperiod', 'badges');
  144. }
  145. if ($data['expiry'] == 1 && $data['expiredate'] <= time()) {
  146. $errors['expirydategr'] = get_string('error:invalidexpiredate', 'badges');
  147. }
  148. // Check for duplicate badge names.
  149. if ($data['action'] == 'new') {
  150. $duplicate = $DB->record_exists_select('badge', 'name = :name AND status != :deleted',
  151. array('name' => $data['name'], 'deleted' => BADGE_STATUS_ARCHIVED));
  152. } else {
  153. $duplicate = $DB->record_exists_select('badge', 'name = :name AND id != :badgeid AND status != :deleted',
  154. array('name' => $data['name'], 'badgeid' => $data['id'], 'deleted' => BADGE_STATUS_ARCHIVED));
  155. }
  156. if ($duplicate) {
  157. $errors['name'] = get_string('error:duplicatename', 'badges');
  158. }
  159. return $errors;
  160. }
  161. }
  162. /**
  163. * Form to edit badge message.
  164. *
  165. */
  166. class edit_message_form extends moodleform {
  167. public function definition() {
  168. global $CFG, $OUTPUT;
  169. $mform = $this->_form;
  170. $badge = $this->_customdata['badge'];
  171. $action = $this->_customdata['action'];
  172. $editoroptions = $this->_customdata['editoroptions'];
  173. // Add hidden fields.
  174. $mform->addElement('hidden', 'id', $badge->id);
  175. $mform->setType('id', PARAM_INT);
  176. $mform->addElement('hidden', 'action', $action);
  177. $mform->setType('action', PARAM_TEXT);
  178. $mform->addElement('header', 'badgemessage', get_string('configuremessage', 'badges'));
  179. $mform->addHelpButton('badgemessage', 'variablesubstitution', 'badges');
  180. $mform->addElement('text', 'messagesubject', get_string('subject', 'badges'), array('size' => '70'));
  181. $mform->setType('messagesubject', PARAM_TEXT);
  182. $mform->addRule('messagesubject', null, 'required');
  183. $mform->addRule('messagesubject', get_string('maximumchars', '', 255), 'maxlength', 255);
  184. $mform->addElement('editor', 'message_editor', get_string('message', 'badges'), null, $editoroptions);
  185. $mform->setType('message_editor', PARAM_RAW);
  186. $mform->addRule('message_editor', null, 'required');
  187. $mform->addElement('advcheckbox', 'attachment', get_string('attachment', 'badges'), '', null, array(0, 1));
  188. $mform->addHelpButton('attachment', 'attachment', 'badges');
  189. if (empty($CFG->allowattachments)) {
  190. $mform->freeze('attachment');
  191. }
  192. $options = array(
  193. BADGE_MESSAGE_NEVER => get_string('never'),
  194. BADGE_MESSAGE_ALWAYS => get_string('notifyevery', 'badges'),
  195. BADGE_MESSAGE_DAILY => get_string('notifydaily', 'badges'),
  196. BADGE_MESSAGE_WEEKLY => get_string('notifyweekly', 'badges'),
  197. BADGE_MESSAGE_MONTHLY => get_string('notifymonthly', 'badges'),
  198. );
  199. $mform->addElement('select', 'notification', get_string('notification', 'badges'), $options);
  200. $mform->addHelpButton('notification', 'notification', 'badges');
  201. $this->add_action_buttons();
  202. $this->set_data($badge);
  203. }
  204. public function validation($data, $files) {
  205. $errors = parent::validation($data, $files);
  206. return $errors;
  207. }
  208. }