-
Notifications
You must be signed in to change notification settings - Fork 43
/
bulkchange.php
211 lines (188 loc) · 8.92 KB
/
bulkchange.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?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/>.
/**
* This page performs bulk changes on reengagment users.
*
* @package mod_reengagement
* @author Dan Marsden <[email protected]>
* @copyright 2018 Catalyst IT {@link http://www.catalyst.net.nz}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once(dirname(__FILE__).'/lib.php');
$id = required_param('id', PARAM_INT); // Course_module ID.
$formaction = required_param('formaction', PARAM_LOCALURL);
$userids = optional_param('userids', array(), PARAM_TEXT);
$confirm = optional_param('confirm', 0, PARAM_INT);
$cm = get_coursemodule_from_id('reengagement', $id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$reengagement = $DB->get_record('reengagement', array('id' => $cm->instance), '*', MUST_EXIST);
$default = new moodle_url('/mod/reengagement/view.php', ['id' => $cm->id]);
$returnurl = new moodle_url(optional_param('returnto', $default, PARAM_URL));
require_sesskey(); // This is an action script.
$PAGE->set_url('/mod/reengagement/view.php', array('id' => $id, 'formaction' => $formaction));
require_login($course, true, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/reengagement:bulkactions', $context);
$PAGE->set_title(format_string($reengagement->name));
$PAGE->set_heading(format_string($course->fullname));
$PAGE->set_context($context);
if (!empty($userids)) {
$userids = explode(',', $userids);
}
// First initial post from view.php - users stored in array as "userX" => "on" - get the userids.
if (empty($userids) && $post = data_submitted()) {
foreach ($post as $k => $v) {
if (preg_match('/^user(\d+)$/', $k, $m)) {
$userids[] = $m[1];
}
}
}
if (!$confirm) {
echo $OUTPUT->header();
}
if ($formaction == 'resetbyspecificdate') {
for ($i = 1; $i <= 31; $i++) {
$days[$i] = $i;
}
for ($i = 1; $i <= 12; $i++) {
$months[$i] = userdate(gmmktime(12, 0, 0, $i, 15, 2000), "%B");
}
for ($i = 2017; $i <= date('Y') + 4; $i++) {
$years[$i] = $i;
}
echo '<div align="center">';
echo '<h4>'.get_string('specifydate', 'mod_reengagement').'</h4>';
echo '<form action="'.$CFG->wwwroot.'/mod/reengagement/bulkchange.php" method="POST"
id="reengagementbulkchange" class="popupform">';
echo html_writer::select($days, 'day', date('n'));
echo html_writer::select($months, 'month', date('n'));
echo html_writer::select($years, 'year', date('Y'));
echo '<input name="userids" value="'. implode(',', $userids).'" type="hidden" />';
echo '<input name="id" value="'.$id.'" type="hidden" />';
echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
echo '<input name="formaction" value="resetbyspecificdate2" type="hidden" />';
echo '<input name="submit" value="Go" type="submit" id="id_submit" />';
echo '</form></div>';
echo $OUTPUT->footer();
exit;
}
$usernamefields = \core_user\fields::for_name()->get_sql($alias = 'u', false, '', '', false)->selects;
list($usql, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, 'u');
if ($formaction == 'resetbyfirstaccess') {
$sql = "SELECT u.id, $usernamefields, rip.id as ripid,
rip.completiontime, rip.emailtime, rip.completiontime, rip.completed,
min(l.timecreated) as firstaccess
FROM {reengagement_inprogress} rip
JOIN {user} u on u.id = rip.userid
LEFT JOIN {logstore_standard_log} l ON l.userid = u.id AND l.courseid = :courseid
WHERE rip.reengagement = :re AND u.id ".$usql ."
GROUP BY u.id, u.firstname, u.lastname, rip.id,
rip.completiontime, rip.emailtime, rip.completiontime, rip.completed";
$params['courseid'] = $course->id;
$params['re'] = $reengagement->id;
$users = $DB->get_records_sql($sql, $params);
$duration = $reengagement->duration;
} else if ($formaction == 'resetbyenrolment') {
$sql = "SELECT u.id, $usernamefields, rip.id as ripid,
rip.completiontime, rip.emailtime, rip.completiontime, rip.completed,
min(ue.timecreated) as firstaccess
FROM {reengagement_inprogress} rip
JOIN {user} u on u.id = rip.userid
JOIN {user_enrolments} ue ON ue.userid = u.id
JOIN {enrol} e ON (e.id = ue.enrolid) AND e.courseid = :courseid
WHERE rip.reengagement = :re AND u.id ".$usql ."
GROUP BY u.id, u.firstname, u.lastname, rip.id,
rip.completiontime, rip.emailtime, rip.completiontime, rip.completed";
$params['courseid'] = $course->id;
$params['re'] = $reengagement->id;
$users = $DB->get_records_sql($sql, $params);
$duration = $reengagement->duration;
} else if ($formaction == 'resetbyspecificdate2') {
$timestamp = optional_param('timestamp', 0, PARAM_INT);
if (empty($timestamp)) {
$day = required_param('day', PARAM_INT);
$month = required_param('month', PARAM_INT);
$year = required_param('year', PARAM_INT);
$timestamp = make_timestamp($year, $month, $day, 9);
}
$sql = "SELECT u.id, $usernamefields, rip.id as ripid,
rip.completiontime, rip.emailtime, rip.completiontime, rip.completed,
'".$timestamp."' as firstaccess
FROM {reengagement_inprogress} rip
JOIN {user} u on u.id = rip.userid
WHERE rip.reengagement = :re AND u.id ".$usql ."
GROUP BY u.id, u.firstname, u.lastname, rip.id,
rip.completiontime, rip.emailtime, rip.completiontime, rip.completed";
$params['courseid'] = $course->id;
$params['re'] = $reengagement->id;
$users = $DB->get_records_sql($sql, $params);
$duration = 0; // Don't add duration to this status - use specified date instead.
}
if (!empty($formaction) && !empty($users)) {
// Get information on users and the updated date.
if (!$confirm) {
print '<table class="reengagementlist">' . "\n";
print "<tr><th>" . get_string('user') . "</th>";
print "<th>" . get_string('completiontime', 'reengagement') . '</th>';
print "<th>" . get_string('newcompletiontime', 'reengagement') . '</th>';
print "</tr>";
foreach ($users as $user) {
if (!empty($user->firstaccess)) {
$newdate = $user->firstaccess + $duration;
if ($newdate == $user->completiontime) {
// Date is already based on firstaccess.
$newdate = get_string('nochange', 'mod_reengagement');
} else {
$newdate = userdate($newdate, get_string('strftimedatetimeshort', 'langconfig'));
}
} else {
$newdate = get_string('nochangenoaccess', 'mod_reengagement');
}
print '<tr><td>' . fullname($user) . '</td>';
print '<td>' . userdate($user->completiontime, get_string('strftimedatetimeshort', 'langconfig'))."</td>";
print '<td>'. $newdate."</td></tr>";
}
print '</table>';
$yesurl = new moodle_url('/mod/reengagement/bulkchange.php');
$yesparams = array('id' => $cm->id, 'formaction' => $formaction,
'userids' => implode(',', $userids), 'confirm' => 1);
if ($formaction == 'resetbyspecificdate2') {
// Add timestamp to form.
$yesparams['timestamp'] = $timestamp;
}
$areyousure = get_string('areyousure', 'mod_reengagement');
echo $OUTPUT->confirm($areyousure, new moodle_url($yesurl, $yesparams), $returnurl);
echo $OUTPUT->footer();
die;
} else {
foreach ($users as $user) {
if (!empty($user->firstaccess)) {
$newdate = $user->firstaccess + $duration;
if ($newdate !== $user->completiontime) {
$rip = new stdClass();
$rip->id = $user->ripid;
$rip->userid = $user->id;
$rip->reengagement = $reengagement->id;
$rip->completiontime = $newdate;
$DB->update_record('reengagement_inprogress', $rip);
}
}
}
redirect($returnurl, get_string('completiondatesupdated', 'reengagement'));
}
}
redirect($returnurl);