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
 * Upgrade tasks.
19
 *
20
 * @package    mod_reengagement
21
 * @author     Peter Bulmer
22
 * @copyright  2016 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
/**
27
 * Code run to upgrade the reengagment database tables.
28
 *
29
 * @param int $oldversion
30
 * @return bool always true
31
 */
32
function xmldb_reengagement_upgrade($oldversion=0) {
33
    global $DB;
34
    $dbman = $DB->get_manager();
35
    if ($oldversion < 2014071701) {
36
        // Define new fields to support emailing managers.
37
        // Define field emailrecipient to be added to reengagement to record who should receive emails.
38
        $table = new xmldb_table('reengagement');
39
        $field = new xmldb_field('emailrecipient', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0');
40
        if (!$dbman->field_exists($table, $field)) {
41
            $dbman->add_field($table, $field);
42
        }
43
 
44
        $table = new xmldb_table('reengagement');
45
        // Define field to hold the email subject which should be used in emails to user's managers.
46
        $field = new xmldb_field('emailsubjectmanager', XMLDB_TYPE_CHAR, '255', null, null, null, null);
47
        if (!$dbman->field_exists($table, $field)) {
48
            $dbman->add_field($table, $field);
49
        }
50
 
51
        $table = new xmldb_table('reengagement');
52
        // Define field to hold the email content which should be used in emails to user's managers.
53
        $field = new xmldb_field('emailcontentmanager', XMLDB_TYPE_TEXT, null, null, null, null, null);
54
        if (!$dbman->field_exists($table, $field)) {
55
            $dbman->add_field($table, $field);
56
        }
57
 
58
        $table = new xmldb_table('reengagement');
59
        $field = new xmldb_field('emailcontentmanagerformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0');
60
        if (!$dbman->field_exists($table, $field)) {
61
            $dbman->add_field($table, $field);
62
        }
63
 
64
        upgrade_mod_savepoint(true, 2014071701, 'reengagement');
65
    }
66
 
67
    // Add remindercount fields.
68
    if ($oldversion < 2016080301) {
69
 
70
        $table = new xmldb_table('reengagement');
71
        $field = new xmldb_field('remindercount', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '1');
72
        if (!$dbman->field_exists($table, $field)) {
73
            $dbman->add_field($table, $field);
74
        }
75
        upgrade_mod_savepoint(true, 2016080301, 'reengagement');
76
    }
77
 
78
    // Add third-party email fields.
79
    if ($oldversion < 2016112400) {
80
        // Define new fields to support emailing thirdparties.
81
        $table = new xmldb_table('reengagement');
82
 
83
        // Define field thirdpartyemails to be added to reengagement to record who should receive emails.
84
        $field = new xmldb_field('thirdpartyemails', XMLDB_TYPE_TEXT, null, null, null, null, null);
85
        if (!$dbman->field_exists($table, $field)) {
86
            $dbman->add_field($table, $field);
87
        }
88
 
89
        // Define field to hold the email subject which should be used in emails to user's thirdparties.
90
        $field = new xmldb_field('emailsubjectthirdparty', XMLDB_TYPE_CHAR, '255', null, null, null, null);
91
        if (!$dbman->field_exists($table, $field)) {
92
            $dbman->add_field($table, $field);
93
        }
94
 
95
        // Define field to hold the email content which should be used in emails to user's thirdparties.
96
        $field = new xmldb_field('emailcontentthirdparty', XMLDB_TYPE_TEXT, null, null, null, null, null);
97
        if (!$dbman->field_exists($table, $field)) {
98
            $dbman->add_field($table, $field);
99
        }
100
 
101
        $field = new xmldb_field('emailcontentthirdpartyformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0');
102
        if (!$dbman->field_exists($table, $field)) {
103
            $dbman->add_field($table, $field);
104
        }
105
 
106
        upgrade_mod_savepoint(true, 2016112400, 'reengagement');
107
    }
108
    // Set default value.
109
    if ($oldversion < 2017040400) {
110
 
111
        $table = new xmldb_table('reengagement');
112
        $field = new xmldb_field('thirdpartyemails', XMLDB_TYPE_TEXT, null, null, false, null, null);
113
        $dbman->change_field_notnull($table, $field);
114
 
115
        $field = new xmldb_field('emailsubjectthirdparty', XMLDB_TYPE_CHAR, '255', null, false, null, null);
116
        $dbman->change_field_notnull($table, $field);
117
 
118
        $field = new xmldb_field('emailcontentthirdparty', XMLDB_TYPE_TEXT, null, null, false, null, null);
119
        $dbman->change_field_notnull($table, $field);
120
 
121
        $field = new xmldb_field('emailcontentthirdpartyformat', XMLDB_TYPE_INTEGER, '4', null, false, null, '0');
122
        $dbman->change_field_notnull($table, $field);
123
 
124
        upgrade_mod_savepoint(true, 2017040400, 'reengagement');
125
    }
126
 
127
    if ($oldversion < 2017102001) {
128
        global $CFG;
129
        require_once($CFG->dirroot.'/mod/reengagement/lib.php');
130
        // A bug in previous versions prevented some e-mails from being sent.
131
        // Flag these old broken reengagment e-mails as sent as they may no longer be relevant.
132
        $timenow = time();
133
        $sql = "SELECT rin.*, cm.id as cmid, r.emailuser
134
                  FROM {reengagement_inprogress} rin
135
                  JOIN {course_modules} cm ON cm.instance = rin.reengagement
136
                  JOIN {modules} m ON m.id = cm.module
137
                  JOIN {reengagement} r on r.id = rin.reengagement
138
             LEFT JOIN {course_modules_completion} cmc ON cmc.coursemoduleid = cm.id AND cmc.userid = rin.userid
139
                 WHERE m.name = 'reengagement' AND cmc.id is null
140
                       AND rin.completiontime < ? AND rin.completed = 0";
141
        $missingprogress = $DB->get_recordset_sql($sql, array($timenow));
142
 
143
        foreach ($missingprogress as $missing) {
144
            // Add course completion record.
145
            $activitycompletion = new stdClass();
146
            $activitycompletion->coursemoduleid = $missing->cmid;
147
            $activitycompletion->completionstate = COMPLETION_COMPLETE_PASS;
148
            $activitycompletion->timemodified = $timenow;
149
            $activitycompletion->userid = $missing->userid;
150
            $DB->insert_record('course_modules_completion', $activitycompletion);
151
 
152
            // Flag re-enagement as complete if required - or delete record.
153
            // logic copied from cron function.
154
            if (($missing->emailuser == REENGAGEMENT_EMAILUSER_COMPLETION) ||
155
                ($missing->emailuser == REENGAGEMENT_EMAILUSER_NEVER) ||
156
                ($missing->emailuser == REENGAGEMENT_EMAILUSER_TIME && !empty($missing->emailsent))) {
157
                // No need to keep 'inprogress' record for later emailing
158
                // Delete inprogress record.
159
                debugging('', DEBUG_DEVELOPER) && mtrace("mode $missing->emailuser reengagementid $missing->id.
160
                      User marked complete, deleting inprogress record for user $missing->userid");
161
                $DB->delete_records('reengagement_inprogress', array('id' => $missing->id));
162
            } else {
163
                // Update inprogress record to indicate completion done.
164
                debugging('', DEBUG_DEVELOPER) && mtrace("mode $missing->emailuser reengagementid $missing->id
165
                      updating inprogress record for user $missing->userid to indicate completion");
166
                $updaterecord = new stdClass();
167
                $updaterecord->id = $missing->id;
168
                $updaterecord->completed = COMPLETION_COMPLETE;
169
                $DB->update_record('reengagement_inprogress', $updaterecord);
170
            }
171
 
172
            // Don't send e-mail as this record may be stale.
173
        }
174
        $missingprogress->close();
175
 
176
        upgrade_mod_savepoint(true, 2017102001, 'reengagement');
177
    }
178
 
179
    return true;
180
}