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
 * Process expirations task.
19
 *
20
 * @package   enrol_coursecompleted
21
 * @copyright 2020 eWallah (www.eWallah.net)
22
 * @author    Renaat Debleu <info@eWallah.net>
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace enrol_coursecompleted\task;
27
 
28
use context_course;
29
use core_user;
30
use moodle_url;
31
use stdClass;
32
 
33
/**
34
 * Process expirations task.
35
 *
36
 * @package   enrol_coursecompleted
37
 * @copyright 2020 eWallah (www.eWallah.net)
38
 * @author    Renaat Debleu <info@eWallah.net>
39
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
41
class send_welcome extends \core\task\adhoc_task {
42
    /**
43
     * Execute scheduled task
44
     *
45
     * @return boolean
46
     */
47
    public function execute() {
48
        global $CFG, $DB;
49
        $data = $this->get_custom_data();
50
        if ($user = core_user::get_user($data->userid)) {
51
            if ($course = $DB->get_field('course', 'fullname', ['id' => $data->courseid])) {
52
                if ($complcourse = $DB->get_field('course', 'fullname', ['id' => $data->completedid])) {
53
                    $context = context_course::instance($data->courseid);
54
                    $context2 = context_course::instance($data->completedid);
55
                    $a = new stdClass();
56
                    $a->coursename = format_string($course, true, ['context' => $context]);
57
                    $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&course=$data->courseid";
58
                    $a->completed = format_string($complcourse, true, ['context' => $context2]);
59
                    $custom = $DB->get_field('enrol', 'customtext1', ['id' => $data->enrolid]);
60
                    if ($custom != '') {
61
                        $key = ['{$a->coursename}', '{$a->completed}', '{$a->profileurl}', '{$a->fullname}', '{$a->email}'];
62
                        $value = [$a->coursename, $a->completed, $a->profileurl, fullname($user), $user->email];
63
                        $message = str_replace($key, $value, $custom);
64
                    } else {
65
                        $message = get_string('welcometocourse', 'enrol_coursecompleted', $a);
66
                    }
67
                    if (strpos($message, '<') === false) {
68
                        $messagehtml = $message;
69
                    } else {
70
                        // This is most probably the tag/newline soup known as FORMAT_MOODLE.
71
                        $messagehtml = format_text(
72
                            $message,
73
                            FORMAT_MOODLE,
74
                            ['context' => $context, 'para' => false, 'newlines' => true, 'filter' => true]
75
                        );
76
                    }
77
                    $subject = get_string('welcometocourse', 'moodle', $a->coursename);
78
                    // Directly emailing welcome message rather than using messaging.
79
                    email_to_user($user, core_user::get_noreply_user(), $subject, $message, $messagehtml);
80
                }
81
            }
82
        }
83
    }
84
}