Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
/**
18
 * This page performs bulk changes on reengagment users.
19
 *
20
 * @package    mod_reengagement
21
 * @author     Dan Marsden <dan@danmarsden.com>
22
 * @copyright  2018 Catalyst IT {@link http://www.catalyst.net.nz}
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
27
require_once(dirname(__FILE__).'/lib.php');
28
 
29
$id = required_param('id', PARAM_INT); // Course_module ID.
30
$formaction = required_param('formaction', PARAM_LOCALURL);
31
$userids = optional_param('userids', array(), PARAM_TEXT);
32
$confirm = optional_param('confirm', 0, PARAM_INT);
33
 
34
$cm = get_coursemodule_from_id('reengagement', $id, 0, false, MUST_EXIST);
35
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
36
$reengagement = $DB->get_record('reengagement', array('id' => $cm->instance), '*', MUST_EXIST);
37
 
38
$default = new moodle_url('/mod/reengagement/view.php', ['id' => $cm->id]);
39
$returnurl = new moodle_url(optional_param('returnto', $default, PARAM_URL));
40
 
41
require_sesskey(); // This is an action script.
42
 
43
$PAGE->set_url('/mod/reengagement/view.php', array('id' => $id, 'formaction' => $formaction));
44
 
45
require_login($course, true, $cm);
46
 
47
$context = context_module::instance($cm->id);
48
require_capability('mod/reengagement:bulkactions', $context);
49
 
50
$PAGE->set_title(format_string($reengagement->name));
51
$PAGE->set_heading(format_string($course->fullname));
52
$PAGE->set_context($context);
53
 
54
if (!empty($userids)) {
55
    $userids = explode(',', $userids);
56
}
57
// First initial post from view.php - users stored in array as "userX" => "on" - get the userids.
58
if (empty($userids) && $post = data_submitted()) {
59
    foreach ($post as $k => $v) {
60
        if (preg_match('/^user(\d+)$/', $k, $m)) {
61
            $userids[] = $m[1];
62
        }
63
    }
64
}
65
 
66
if (!$confirm) {
67
    echo $OUTPUT->header();
68
}
69
 
70
if ($formaction == 'resetbyspecificdate') {
71
    for ($i = 1; $i <= 31; $i++) {
72
        $days[$i] = $i;
73
    }
74
    for ($i = 1; $i <= 12; $i++) {
75
        $months[$i] = userdate(gmmktime(12, 0, 0, $i, 15, 2000), "%B");
76
    }
77
    for ($i = 2017; $i <= date('Y') + 4; $i++) {
78
        $years[$i] = $i;
79
    }
80
    echo '<div align="center">';
81
    echo '<h4>'.get_string('specifydate', 'mod_reengagement').'</h4>';
82
    echo '<form action="'.$CFG->wwwroot.'/mod/reengagement/bulkchange.php" method="POST"
83
           id="reengagementbulkchange" class="popupform">';
84
    echo html_writer::select($days, 'day', date('n'));
85
    echo html_writer::select($months, 'month', date('n'));
86
    echo html_writer::select($years,  'year',  date('Y'));
87
    echo '<input name="userids" value="'. implode(',', $userids).'" type="hidden" />';
88
    echo '<input name="id" value="'.$id.'" type="hidden" />';
89
    echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
90
    echo '<input name="formaction" value="resetbyspecificdate2" type="hidden" />';
91
    echo '<input name="submit" value="Go" type="submit" id="id_submit" />';
92
    echo '</form></div>';
93
    echo $OUTPUT->footer();
94
    exit;
95
}
96
 
97
$usernamefields = \core_user\fields::for_name()->get_sql($alias = 'u', false, '', '',  false)->selects;
98
list($usql, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, 'u');
99
 
100
if ($formaction == 'resetbyfirstaccess') {
101
    $sql = "SELECT u.id, $usernamefields, rip.id as ripid,
102
                   rip.completiontime, rip.emailtime, rip.completiontime, rip.completed,
103
                   min(l.timecreated) as firstaccess
104
        FROM {reengagement_inprogress} rip
105
        JOIN {user} u on u.id = rip.userid
106
        LEFT JOIN {logstore_standard_log} l ON l.userid = u.id AND l.courseid = :courseid
107
        WHERE rip.reengagement = :re AND u.id ".$usql ."
108
        GROUP BY u.id, u.firstname, u.lastname, rip.id,
109
                 rip.completiontime, rip.emailtime, rip.completiontime, rip.completed";
110
    $params['courseid'] = $course->id;
111
    $params['re'] = $reengagement->id;
112
    $users = $DB->get_records_sql($sql, $params);
113
    $duration = $reengagement->duration;
114
} else if ($formaction == 'resetbyenrolment') {
115
    $sql = "SELECT u.id, $usernamefields, rip.id as ripid,
116
                   rip.completiontime, rip.emailtime, rip.completiontime, rip.completed,
117
                   min(ue.timecreated) as firstaccess
118
        FROM {reengagement_inprogress} rip
119
        JOIN {user} u on u.id = rip.userid
120
        JOIN {user_enrolments} ue ON ue.userid = u.id
121
        JOIN {enrol} e ON (e.id = ue.enrolid) AND e.courseid = :courseid
122
        WHERE rip.reengagement = :re AND u.id ".$usql ."
123
        GROUP BY u.id, u.firstname, u.lastname, rip.id,
124
                 rip.completiontime, rip.emailtime, rip.completiontime, rip.completed";
125
    $params['courseid'] = $course->id;
126
    $params['re'] = $reengagement->id;
127
    $users = $DB->get_records_sql($sql, $params);
128
    $duration = $reengagement->duration;
129
} else if ($formaction == 'resetbyspecificdate2') {
130
    $timestamp = optional_param('timestamp', 0, PARAM_INT);
131
    if (empty($timestamp)) {
132
        $day = required_param('day', PARAM_INT);
133
        $month = required_param('month', PARAM_INT);
134
        $year = required_param('year', PARAM_INT);
135
        $timestamp = make_timestamp($year, $month, $day, 9);
136
    }
137
 
138
    $sql = "SELECT u.id, $usernamefields, rip.id as ripid,
139
                   rip.completiontime, rip.emailtime, rip.completiontime, rip.completed,
140
                   '".$timestamp."' as firstaccess
141
        FROM {reengagement_inprogress} rip
142
        JOIN {user} u on u.id = rip.userid
143
        WHERE rip.reengagement = :re AND u.id ".$usql ."
144
        GROUP BY u.id, u.firstname, u.lastname, rip.id,
145
                 rip.completiontime, rip.emailtime, rip.completiontime, rip.completed";
146
    $params['courseid'] = $course->id;
147
    $params['re'] = $reengagement->id;
148
    $users = $DB->get_records_sql($sql, $params);
149
    $duration = 0; // Don't add duration to this status - use specified date instead.
150
}
151
 
152
if (!empty($formaction) && !empty($users)) {
153
    // Get information on users and the updated date.
154
 
155
    if (!$confirm) {
156
        print '<table class="reengagementlist">' . "\n";
157
        print "<tr><th>" . get_string('user') . "</th>";
158
        print "<th>" . get_string('completiontime', 'reengagement') . '</th>';
159
        print "<th>" . get_string('newcompletiontime', 'reengagement') . '</th>';
160
        print "</tr>";
161
        foreach ($users as $user) {
162
            if (!empty($user->firstaccess)) {
163
                $newdate = $user->firstaccess + $duration;
164
                if ($newdate == $user->completiontime) {
165
                    // Date is already based on firstaccess.
166
                    $newdate = get_string('nochange', 'mod_reengagement');
167
                } else {
168
                    $newdate = userdate($newdate, get_string('strftimedatetimeshort', 'langconfig'));
169
                }
170
 
171
            } else {
172
                $newdate = get_string('nochangenoaccess', 'mod_reengagement');
173
            }
174
 
175
            print '<tr><td>' . fullname($user) . '</td>';
176
            print '<td>' . userdate($user->completiontime, get_string('strftimedatetimeshort', 'langconfig'))."</td>";
177
            print '<td>'. $newdate."</td></tr>";
178
        }
179
        print '</table>';
180
 
181
        $yesurl = new moodle_url('/mod/reengagement/bulkchange.php');
182
        $yesparams = array('id' => $cm->id, 'formaction' => $formaction,
183
            'userids' => implode(',', $userids), 'confirm' => 1);
184
        if ($formaction == 'resetbyspecificdate2') {
185
            // Add timestamp to form.
186
            $yesparams['timestamp'] = $timestamp;
187
        }
188
        $areyousure = get_string('areyousure', 'mod_reengagement');
189
        echo $OUTPUT->confirm($areyousure, new moodle_url($yesurl, $yesparams), $returnurl);
190
        echo $OUTPUT->footer();
191
        die;
192
    } else {
193
        foreach ($users as $user) {
194
            if (!empty($user->firstaccess)) {
195
                $newdate = $user->firstaccess + $duration;
196
                if ($newdate !== $user->completiontime) {
197
                    $rip = new stdClass();
198
                    $rip->id = $user->ripid;
199
                    $rip->userid = $user->id;
200
                    $rip->reengagement = $reengagement->id;
201
                    $rip->completiontime = $newdate;
202
                    $DB->update_record('reengagement_inprogress', $rip);
203
                }
204
            }
205
        }
206
        redirect($returnurl, get_string('completiondatesupdated', 'reengagement'));
207
    }
208
}
209
 
210
redirect($returnurl);
211