PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/badges/backpack_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 151 lines | 87 code | 24 blank | 40 comment | 9 complexity | 78a34a3becabfbb74b81e21665070e41 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-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 class for mybackpack.php
  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. /**
  29. * Form to edit backpack initial details.
  30. *
  31. */
  32. class edit_backpack_form extends moodleform {
  33. /**
  34. * Defines the form
  35. */
  36. public function definition() {
  37. global $USER, $PAGE, $OUTPUT;
  38. $mform = $this->_form;
  39. $mform->addElement('html', html_writer::tag('span', '', array('class' => 'notconnected', 'id' => 'connection-error')));
  40. $mform->addElement('header', 'backpackheader', get_string('backpackconnection', 'badges'));
  41. $mform->addHelpButton('backpackheader', 'backpackconnection', 'badges');
  42. $mform->addElement('static', 'url', get_string('url'), BADGE_BACKPACKURL);
  43. $status = html_writer::tag('span', get_string('notconnected', 'badges'),
  44. array('class' => 'notconnected', 'id' => 'connection-status'));
  45. $mform->addElement('static', 'status', get_string('status'), $status);
  46. $nojs = html_writer::tag('noscript', get_string('error:personaneedsjs', 'badges'),
  47. array('class' => 'notconnected'));
  48. $personadiv = $OUTPUT->container($nojs, null, 'persona-container');
  49. $mform->addElement('static', 'persona', '', $personadiv);
  50. $mform->addHelpButton('persona', 'personaconnection', 'badges');
  51. $PAGE->requires->js(new moodle_url('https://login.persona.org/include.js'));
  52. $PAGE->requires->js('/badges/backpack.js');
  53. $PAGE->requires->js_init_call('badges_init_persona_login_button', null, false);
  54. $PAGE->requires->strings_for_js(array('error:backpackloginfailed', 'signinwithyouremail',
  55. 'error:noassertion', 'error:connectionunknownreason', 'error:badjson', 'connecting',
  56. 'notconnected'), 'badges');
  57. $mform->addElement('hidden', 'userid', $USER->id);
  58. $mform->setType('userid', PARAM_INT);
  59. $mform->addElement('hidden', 'backpackurl', BADGE_BACKPACKURL);
  60. $mform->setType('backpackurl', PARAM_URL);
  61. }
  62. /**
  63. * Validates form data
  64. */
  65. public function validation($data, $files) {
  66. global $DB;
  67. $errors = parent::validation($data, $files);
  68. $check = new stdClass();
  69. $check->backpackurl = $data['backpackurl'];
  70. $check->email = $data['email'];
  71. $bp = new OpenBadgesBackpackHandler($check);
  72. $request = $bp->curl_request('user');
  73. if (isset($request->status) && $request->status == 'missing') {
  74. $errors['email'] = get_string('error:nosuchuser', 'badges');
  75. }
  76. return $errors;
  77. }
  78. }
  79. /**
  80. * Form to select backpack collections.
  81. *
  82. */
  83. class edit_collections_form extends moodleform {
  84. /**
  85. * Defines the form
  86. */
  87. public function definition() {
  88. global $USER;
  89. $mform = $this->_form;
  90. $email = $this->_customdata['email'];
  91. $bid = $this->_customdata['backpackid'];
  92. $selected = $this->_customdata['selected'];
  93. if (isset($this->_customdata['groups'])) {
  94. $groups = $this->_customdata['groups'];
  95. $nogroups = null;
  96. } else {
  97. $groups = null;
  98. $nogroups = $this->_customdata['nogroups'];
  99. }
  100. $mform->addElement('header', 'backpackheader', get_string('backpackconnection', 'badges'));
  101. $mform->addHelpButton('backpackheader', 'backpackconnection', 'badges');
  102. $mform->addElement('static', 'url', get_string('url'), BADGE_BACKPACKURL);
  103. $status = html_writer::tag('span', get_string('connected', 'badges'), array('class' => 'connected'));
  104. $mform->addElement('static', 'status', get_string('status'), $status);
  105. $mform->addElement('static', 'email', get_string('email'), $email);
  106. $mform->addHelpButton('email', 'backpackemail', 'badges');
  107. $mform->addElement('submit', 'disconnect', get_string('disconnect', 'badges'));
  108. $mform->addElement('header', 'collectionheader', get_string('backpackimport', 'badges'));
  109. $mform->addHelpButton('collectionheader', 'backpackimport', 'badges');
  110. if (!empty($groups)) {
  111. $mform->addElement('static', 'selectgroup', '', get_string('selectgroup_start', 'badges'));
  112. foreach ($groups as $group) {
  113. $name = $group->name . ' (' . $group->badges . ')';
  114. $mform->addElement('advcheckbox', 'group[' . $group->groupId . ']', null, $name, array('group' => 1), array(false, $group->groupId));
  115. if (in_array($group->groupId, $selected)) {
  116. $mform->setDefault('group[' . $group->groupId . ']', $group->groupId);
  117. }
  118. }
  119. $mform->addElement('static', 'selectgroup', '', get_string('selectgroup_end', 'badges'));
  120. } else {
  121. $mform->addElement('static', 'selectgroup', '', $nogroups);
  122. }
  123. $mform->addElement('hidden', 'backpackid', $bid);
  124. $mform->setType('backpackid', PARAM_INT);
  125. $this->add_action_buttons();
  126. }
  127. }