-
Notifications
You must be signed in to change notification settings - Fork 0
/
locallib.php
181 lines (157 loc) · 5.7 KB
/
locallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?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/>.
/**
* Contains the functions to be executed on actions.php
*
* @package block_ps_selfstudy
* @copyright 2015 Andres Ramos
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Delete a request
* @param integer $requestid
* @return true if was deleted successfully, false otherwise
*/
function delete_request($requestid) {
global $DB;
// Delete course record.
if (!$DB->delete_records('block_ps_selfstudy_request', array('id' => $requestid))) {
return false;
}
// Delete all completions with this requests if any.
$completionid = $DB->get_record('block_ps_selfstudy_complete', array('request_id' => $requestid), $fields = 'id');
if ($completionid) {
if (!$DB->delete_records('block_ps_selfstudy_complete', array('id' => $completionid->id))) {
return false;
}
}
return true;
}
/**
* Deliver a request
* @param integer $requestid
* @return true if the request status was updated successfully, false otherwise
*/
function deliver_request($requestid) {
global $DB;
// Update the request status to 1, meaning it has been delivered.
if (!$DB->update_record('block_ps_selfstudy_request', array('id' => $requestid, 'request_status' => '1'))) {
return false;
}
// Notify the user that his/her course has been delivered.
$sql = "SELECT course_name AS coursename,
course_code AS coursecode,
request.student_id AS student_id,
firstname,
lastname,
email
FROM {block_ps_selfstudy_request} request
JOIN {block_ps_selfstudy_course} course ON course.id = request.course_id
JOIN {user} u ON u.id = student_id
WHERE request.id = ?";
$requestinfo = $DB->get_record_sql($sql, array($requestid));
notify_user($requestinfo);
return true;
}
/**
* Delete a course request and requests and completion associated.
* @param int $courseid with coursename,coursecode
* firstname,lastname and email of the user.
* @return true if was deleted successfully, false otherwise
*/
function delete_course_request($courseid) {
global $DB;
// Delete the course record.
if (!$DB->delete_records('block_ps_selfstudy_course', array('id' => $courseid))) {
return false;
}
// Delete all requests related to this course if any.
$requestlist = $DB->get_records(
'block_ps_selfstudy_request',
array('course_id' => $courseid),
$sort = '', $fields = 'id'
);
if ($requestlist) {
foreach ($requestlist as $request) {
delete_request($request->id);
}
}
return true;
}
/**
* Complete a course request.
* @param int $requestid
* @return true if was deleted successfully, false otherwise
*/
function complete_course_request($requestid) {
global $DB;
// Success when a user mark a course as completed.
$today = time();
$completion = new stdClass();
$completion->request_id = $requestid;
$completion->completion_status = "completed";
$completion->completion_date = $today;
if (!$DB->insert_record('block_ps_selfstudy_complete', $completion)) {
return false;
}
return true;
}
/**
* Add a new course request.
* @param object $request with all fields
* @return true if was added successfully, false otherwise
*/
function add_course_request($request) {
global $DB;
if (!$DB->insert_record('block_ps_selfstudy_request', $request)) {
return false;
}
return true;
}
/**
* Notify the user that a request was updated.
* @param object $requestinfo with coursename,coursecode
* firstname,lastname and email of the user.
* @return true if was deleted successfully, false otherwise
*/
function notify_user($requestinfo) {
global $CFG;
$subject = "Course Shipment Your order for $requestinfo->coursecode $requestinfo->coursename has been shipped";
$userinfo = new stdClass;
$userinfo->id = $requestinfo->student_id;
$userinfo->email = $requestinfo->email;
$userinfo->firstname = $requestinfo->firstname;
$userinfo->lastname = $requestinfo->lastname;
$userinfo->mailformat = 1;
$userinfo->maildisplay = true;
$userinfo->firstnamephonetic = '';
$userinfo->lastnamephonetic = '';
$userinfo->middlename = '';
$userinfo->alternatename = '';
$from = new stdClass;
$from->email = "noreply@ibm.com";
$from->firstname = "No ";
$from->lastname = "Reply";
$message = "
<p>Hello $userinfo->firstname $userinfo->lastname,</p>
<p>You should receive this order within the next week. Be sure to come back into
the EPS system <a href='{$CFG->wwwroot}/blocks/ps_selfstudy/myrequests.php'>{$CFG->wwwroot}</a>
and indicate the date you complete the course so that your training history can be updated.</p>
<p>Thank you.</p>
";
email_to_user($userinfo, $from, $subject, $message, $message, ",", false);
}