MDL-63724 messaging: Prefix field with the table alias
[moodle.git] / badges / lib / backpacklib.php
blob1fe156f6a7ebb982dcd3581d38235e441be61707
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.
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/>.
17 /**
18 * External backpack library.
20 * @package core
21 * @subpackage badges
22 * @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->libdir . '/filelib.php');
32 // Adopted from https://github.com/jbkc85/openbadges-class-php.
33 // Author Jason Cameron <jbkc85@gmail.com>.
35 class OpenBadgesBackpackHandler {
36 private $backpack;
37 private $email;
38 private $backpackuid = 0;
40 public function __construct($record) {
41 $this->backpack = $record->backpackurl;
42 $this->email = $record->email;
43 $this->backpackuid = isset($record->backpackuid) ? $record->backpackuid : 0;
46 public function curl_request($action, $collection = null) {
47 $curl = new curl();
49 switch($action) {
50 case 'user':
51 $url = $this->backpack . "/displayer/convert/email";
52 $param = array('email' => $this->email);
53 break;
54 case 'groups':
55 $url = $this->backpack . '/displayer/' . $this->backpackuid . '/groups.json';
56 break;
57 case 'badges':
58 $url = $this->backpack . '/displayer/' . $this->backpackuid . '/group/' . $collection . '.json';
59 break;
62 $curl->setHeader(array('Accept: application/json', 'Expect:'));
63 $options = array(
64 'FRESH_CONNECT' => true,
65 'RETURNTRANSFER' => true,
66 'FORBID_REUSE' => true,
67 'HEADER' => 0,
68 'CONNECTTIMEOUT' => 3,
69 // Follow redirects with the same type of request when sent 301, or 302 redirects.
70 'CURLOPT_POSTREDIR' => 3
73 if ($action == 'user') {
74 $out = $curl->post($url, $param, $options);
75 } else {
76 $out = $curl->get($url, array(), $options);
79 return json_decode($out);
82 private function check_status($status) {
83 switch($status) {
84 case "missing":
85 $response = array(
86 'status' => $status,
87 'message' => get_string('error:nosuchuser', 'badges')
89 return $response;
93 public function get_collections() {
94 $json = $this->curl_request('user', $this->email);
95 if (isset($json->status)) {
96 if ($json->status != 'okay') {
97 return $this->check_status($json->status);
98 } else {
99 $this->backpackuid = $json->userId;
100 return $this->curl_request('groups');
105 public function get_badges($collection) {
106 $json = $this->curl_request('user', $this->email);
107 if (isset($json->status)) {
108 if ($json->status != 'okay') {
109 return $this->check_status($json->status);
110 } else {
111 return $this->curl_request('badges', $collection);
116 public function get_url() {
117 return $this->backpack;
122 * Create and send a verification email to the email address supplied.
124 * Since we're not sending this email to a user, email_to_user can't be used
125 * but this function borrows largely the code from that process.
127 * @param string $email the email address to send the verification email to.
128 * @return true if the email was sent successfully, false otherwise.
130 function send_verification_email($email) {
131 global $DB, $USER;
133 // Store a user secret (badges_email_verify_secret) and the address (badges_email_verify_address) as users prefs.
134 // The address will be used by edit_backpack_form for display during verification and to facilitate the resending
135 // of verification emails to said address.
136 $secret = random_string(15);
137 set_user_preference('badges_email_verify_secret', $secret);
138 set_user_preference('badges_email_verify_address', $email);
140 // To, from.
141 $tempuser = $DB->get_record('user', array('id' => $USER->id), '*', MUST_EXIST);
142 $tempuser->email = $email;
143 $noreplyuser = core_user::get_noreply_user();
145 // Generate the verification email body.
146 $verificationurl = '/badges/backpackemailverify.php';
147 $verificationurl = new moodle_url($verificationurl);
148 $verificationpath = $verificationurl->out(false);
150 $site = get_site();
151 $args = new stdClass();
152 $args->link = $verificationpath . '?data='. $secret;
153 $args->sitename = $site->fullname;
154 $args->admin = generate_email_signoff();
156 $messagesubject = get_string('backpackemailverifyemailsubject', 'badges', $site->fullname);
157 $messagetext = get_string('backpackemailverifyemailbody', 'badges', $args);
158 $messagehtml = text_to_html($messagetext, false, false, true);
160 return email_to_user($tempuser, $noreplyuser, $messagesubject, $messagetext, $messagehtml);