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 |
* Prints an instance of mod_custommailing.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_custommailing
|
|
|
21 |
* @author olivier@cblue.be, jeanfrancois@cblue.be
|
|
|
22 |
* @copyright 2021 CBlue SPRL
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
use core\output\notification;
|
|
|
27 |
use mod_custommailing\Mailing;
|
|
|
28 |
|
|
|
29 |
require_once __DIR__ . '/../../config.php';
|
|
|
30 |
|
|
|
31 |
global $CFG, $DB, $PAGE, $OUTPUT;
|
|
|
32 |
|
|
|
33 |
require_once $CFG->dirroot . '/mod/custommailing/lib.php';
|
|
|
34 |
require_once $CFG->dirroot . '/mod/custommailing/mailing_form.php';
|
|
|
35 |
require_once $CFG->dirroot . '/lib/completionlib.php';
|
|
|
36 |
|
|
|
37 |
$id = required_param('id', PARAM_INT);
|
|
|
38 |
$mailing_id = optional_param('mailingid', 0, PARAM_INT);
|
|
|
39 |
|
|
|
40 |
[$course, $cm] = get_course_and_cm_from_cmid($id, 'custommailing');
|
|
|
41 |
$custommailing = $DB->get_record("custommailing", ['id' => $cm->instance], '*', MUST_EXIST);
|
|
|
42 |
$context = context_module::instance($cm->id);
|
|
|
43 |
|
|
|
44 |
require_login($course, false, $cm);
|
|
|
45 |
require_capability('mod/custommailing:manage', $context);
|
|
|
46 |
|
|
|
47 |
$PAGE->set_title(format_string($course->shortname . ': ' . $custommailing->name));
|
|
|
48 |
$PAGE->set_heading(format_string($course->fullname));
|
|
|
49 |
$PAGE->set_context($context);
|
|
|
50 |
|
|
|
51 |
if (!empty($mailing_id)) {
|
|
|
52 |
$action = 'update';
|
|
|
53 |
$mailing = $DB->get_record("custommailing_mailing", ['id' => $mailing_id], '*', MUST_EXIST);
|
|
|
54 |
$url = new moodle_url('/mod/custommailing/upsert.php', ['id' => $cm->id, 'mailingid' => $mailing->id]);
|
|
|
55 |
$form = new mailing_form(null, ['mailingid' => $mailing->id]);
|
|
|
56 |
} else {
|
|
|
57 |
$action = 'create';
|
|
|
58 |
$url = new moodle_url('/mod/custommailing/upsert.php', ['id' => $cm->id]);
|
|
|
59 |
$form = new mailing_form();
|
|
|
60 |
}
|
|
|
61 |
$PAGE->set_url($url);
|
|
|
62 |
|
|
|
63 |
if ($form->is_cancelled()) {
|
|
|
64 |
redirect(new moodle_url('/mod/custommailing/view.php', ['id' => $id]));
|
|
|
65 |
} elseif ($data = $form->get_data()) {
|
|
|
66 |
if ($action == 'create') {
|
|
|
67 |
$mailing = new stdClass();
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
$mailing->custommailingid = (int) $custommailing->id;
|
|
|
71 |
$mailing->mailingname = $data->mailingname;
|
|
|
72 |
$mailing->mailinglang = 'en'; //disabled in v1
|
|
|
73 |
$mailing->mailingsubject = $data->mailingsubject;
|
|
|
74 |
$mailing->mailingcontent = $data->mailingcontent['text'];
|
|
|
75 |
$mailing->mailingcontentformat = $data->mailingcontent['format'];
|
|
|
76 |
$mailing->mailingmode = (int) (!empty($data->mailingmode) ? $data->mailingmode : 0);
|
|
|
77 |
$mailing->mailingdelay = null;
|
|
|
78 |
|
|
|
79 |
if (empty($data->mailingmodecompletion)) {
|
|
|
80 |
$data->mailingmodecompletion = 0;
|
|
|
81 |
}
|
|
|
82 |
$mailing->targetmodulestatus = $data->mailingmodecompletion;
|
|
|
83 |
if (isset($data->mailingmode) && $data->mailingmode == 'option' && !empty($data->mailingmodeoption)) {
|
|
|
84 |
$mailing->mailingmode = $data->mailingmodeoption;
|
|
|
85 |
$mailing->mailingdelay = (int) $data->mailingdelay;
|
|
|
86 |
} elseif (isset($data->mailingmodemodule) && $data->mailingmodemodule == 'option' && !empty($data->mailingmodemoduleoption)) {
|
|
|
87 |
$mailing->mailingmode = $data->mailingmodemoduleoption;
|
|
|
88 |
$mailing->mailingdelay = (int) $data->mailingdelaymodule;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
$mailing->mailingstatus = (bool) $data->mailingstatus;
|
|
|
92 |
$mailing->retroactive = (bool) $data->retroactive;
|
|
|
93 |
if (empty($data->targetmoduleid)) {
|
|
|
94 |
$data->targetmoduleid = 0;
|
|
|
95 |
}
|
|
|
96 |
$mailing->targetmoduleid = (int) $data->targetmoduleid;
|
|
|
97 |
$mailing->starttime = 0; //$data->starttimehour * 3600 + $data->starttimeminute * 60;
|
|
|
98 |
if (!empty($data->customcert)) {
|
|
|
99 |
$mailing->mailingmode = MAILING_MODE_SEND_CERTIFICATE;
|
|
|
100 |
$mailing->customcertmoduleid = (int) $data->customcert;
|
|
|
101 |
} else {
|
|
|
102 |
$mailing->customcertmoduleid = null;
|
|
|
103 |
}
|
|
|
104 |
if ($action == 'create') {
|
|
|
105 |
Mailing::create($mailing);
|
|
|
106 |
redirect(new moodle_url('/mod/custommailing/view.php', ['id' => $cm->id]), get_string('mailingadded', 'mod_custommailing'), null, notification::NOTIFY_SUCCESS);
|
|
|
107 |
} else {
|
|
|
108 |
Mailing::update($mailing);
|
|
|
109 |
redirect(new moodle_url('/mod/custommailing/view.php', ['id' => $cm->id]), get_string('mailingupdated', 'mod_custommailing'), null, notification::NOTIFY_SUCCESS);
|
|
|
110 |
}
|
|
|
111 |
} else {
|
|
|
112 |
echo $OUTPUT->header();
|
|
|
113 |
echo $OUTPUT->heading(format_string($custommailing->name));
|
|
|
114 |
|
|
|
115 |
if ($action == 'update') {
|
|
|
116 |
$data = clone $mailing;
|
|
|
117 |
$data->id = $id;
|
|
|
118 |
$data->mailingid = $mailing->id;
|
|
|
119 |
$mailingcontenteditor = [
|
|
|
120 |
'text' => $data->mailingcontent,
|
|
|
121 |
'format' => $data->mailingcontentformat
|
|
|
122 |
];
|
|
|
123 |
$data->mailingcontent = $mailingcontenteditor;
|
|
|
124 |
//Todo v2 : starttime
|
|
|
125 |
$data->starttimehour = 0; //floor($data->starttime / 3600);
|
|
|
126 |
$data->starttimeminute = 0; //floor(($data->starttime / 60) % 60);
|
|
|
127 |
if (empty($data->customcertmoduleid) && in_array($data->mailingmode, [MAILING_MODE_DAYSFROMINSCRIPTIONDATE, MAILING_MODE_DAYSFROMLASTCONNECTION, MAILING_MODE_DAYSFROMFIRSTLAUNCH, MAILING_MODE_DAYSFROMLASTLAUNCH])) {
|
|
|
128 |
$data->mailingmode = 'option';
|
|
|
129 |
$data->mailingmodeoption = $mailing->mailingmode;
|
|
|
130 |
}
|
|
|
131 |
if (!empty($data->targetmoduleid)) {
|
|
|
132 |
$data->source = MAILING_SOURCE_MODULE;
|
|
|
133 |
} elseif (!empty($data->customcertmoduleid)) {
|
|
|
134 |
$data->source = MAILING_SOURCE_CERT;
|
|
|
135 |
} else {
|
|
|
136 |
$data->source = MAILING_SOURCE_COURSE;
|
|
|
137 |
}
|
|
|
138 |
$data->retroactive = $mailing->retroactive;
|
|
|
139 |
$form->set_data($data);
|
|
|
140 |
}
|
|
|
141 |
$form->display();
|
|
|
142 |
|
|
|
143 |
echo $OUTPUT->footer();
|
|
|
144 |
}
|