/
newsletter.php
2376 lines (2184 loc) · 86.5 KB
/
newsletter.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* Internal library of functions for module newsletter
*
* @package mod_newsletter
* @copyright 2013 Ivan Šakić <ivan.sakic3@gmail.com>
* @copyright 2015 onwards David Bogner <info@edulabs.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_newsletter;
use coding_exception;
use context_module;
use core\event\user_created;
use core_user;
use mod_newsletter_instance_store;
use newsletter_section_list;
use renderable;
use renderer_base;
use stdClass;
use context;
use moodle_url;
use html_writer;
use moodle_exception;
use user_picture;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/newsletter/renderable.php');
require_once($CFG->dirroot . '/mod/newsletter/CssToInlineStyles/CssToInlineStyles.php');
require_once(dirname(__FILE__) . '/subscription/subscription_filter_form.php');
require_once($CFG->dirroot . '/mod/newsletter/guest_signup_form.php');
require_once($CFG->dirroot . '/mod/newsletter/resubscribe_form.php');
class newsletter implements renderable {
/** @var stdClass the newsletter record that contains the settings for this newsletter instance */
private $instance = null;
/** @var context of the course module for this newsletter instance (or just the course if we are creating a new one) */
private $context;
/** @var stdClass the course this newsletter instance belongs to */
private $course = null;
/** @var ?stdClass the course module for this assign instance */
private ?stdClass $coursemodule = null;
/** @var ?stdClass the admin config for all newsletter instances */
private ?stdClass $config = null;
/** @var ?renderer_base the custom renderer for this module */
private ?renderer_base $renderer = null;
/** @var ?int the subscription id of $USER, if subscribed */
private ?int $subscriptionid = null;
/** @var array of objects containing data records of newsletter issues sorted be issueid */
private array $issues = array();
/**
* get a newsletter object by providing the id of the newsletter table (default: mdl_newsletter)
*
* @param int $newsletterid
* @param bool $eagerload
* @return newsletter the newsletter instance
*/
public static function get_newsletter_by_instance(int $newsletterid, bool $eagerload = false): newsletter {
$cm = get_coursemodule_from_instance('newsletter', $newsletterid);
$context = context_module::instance($cm->id);
return self::create_newsletter_instance($context, $eagerload);
}
/**
* get a newsletter object by providing the course module id (view.php?id=xxx)
*
* @param int $cmid
* @param bool $eagerload
* @return newsletter
*/
public static function get_newsletter_by_course_module(int $cmid, bool $eagerload = false): newsletter {
$context = context_module::instance($cmid);
return self::create_newsletter_instance($context, $eagerload);
}
/**
* When not cached create the newsletter instance otherwise return it from the cache
*
* @param context_module $context
* @param bool $eagerload
* @return newsletter $nl the newsletter instance
*/
private static function create_newsletter_instance(context_module $context, bool $eagerload): newsletter {
$cmid = $context->id;
if (!$nl = mod_newsletter_instance_store::instance($cmid, 'newsletter')) {
$nl = new newsletter($context, $eagerload);
mod_newsletter_instance_store::register($cmid, 'newsletter', $nl);
}
return $nl;
}
/**
* Constructor for the newsletter class. Always use get_newsletter_by_instance
* or get_newsletter_by_course_module to get a new instance Do not use
* $nl = new \newsletter($context)!!!
*
* @param context_module $context
* @param boolean $eagerload
*/
public function __construct(context_module $context, bool $eagerload = false) {
global $DB;
$this->context = $context;
if ($eagerload) {
$this->coursemodule = get_coursemodule_from_id(
'newsletter',
$this->context->instanceid,
0,
false,
MUST_EXIST
);
$this->course = $DB->get_record(
'course',
array('id' => $this->coursemodule->course),
'*',
MUST_EXIST
);
$this->instance = $DB->get_record(
'newsletter',
array('id' => $this->get_course_module()->instance),
'*',
MUST_EXIST
);
$this->config = get_config('mod_newsletter');
$this->context = $context;
}
}
/**
* Get context module
*
* @return context
*/
public function get_context() {
return $this->context;
}
/**
* Get the current course module
*
* @return ?stdClass The course module
*/
public function get_course_module(): ?stdClass {
if (!$this->coursemodule) {
if ($this->context && $this->context->contextlevel == CONTEXT_MODULE) {
$this->coursemodule = get_coursemodule_from_id(
'newsletter',
$this->context->instanceid,
0,
false,
MUST_EXIST
);
}
}
return $this->coursemodule;
}
/**
* Get the settings for the current instance of this newsletter.
*
* @return object The settings
*/
public function get_instance(): object {
global $DB;
if (!$this->instance) {
if ($this->get_course_module()) {
$this->instance = $DB->get_record(
'newsletter',
array('id' => $this->get_course_module()->instance),
'*',
MUST_EXIST
);
} else {
throw new coding_exception(
'Improper use of the newsletter class. Cannot load the newsletter record.'
);
}
}
return $this->instance;
}
/**
* Get subscription id of user if $userid = 0 id of current user is returned
*
* @param int $userid
* @return int|boolean subscriptionid | false if no subscription is found
*/
public function get_subid(int $userid = 0) {
global $DB, $USER;
if ($userid === 0 && !$this->subscriptionid) {
$this->subscriptionid = $DB->get_field(
'newsletter_subscriptions',
'id',
array('userid' => $USER->id, 'newsletterid' => $this->get_instance()->id)
);
return $this->subscriptionid;
} else {
return $DB->get_field(
'newsletter_subscriptions',
'id',
array('userid' => $userid, 'newsletterid' => $this->get_instance()->id)
);
}
}
/**
* Get the current course
*
* @return mixed stdClass|null The course
*/
public function get_course() {
global $DB;
if (!$this->course) {
if ($this->context) {
$this->course = $DB->get_record(
'course',
array('id' => $this->get_instance()->course),
'*',
MUST_EXIST
);
}
}
return $this->course;
}
/**
* Get the module renderer
*
* @return renderer_base The module renderer
*/
public function get_renderer(): renderer_base {
global $PAGE;
if (!$this->renderer) {
$this->renderer = $PAGE->get_renderer('mod_newsletter');
}
return $this->renderer;
}
/**
* get global newsletter settings (admin settings)
*
* @return object configuration object
*/
public function get_config(): object {
if (!$this->config) {
$this->config = get_config('mod_newsletter');
}
return $this->config;
}
/**
* Reset user data
*
* @param \stdClass $data
* @return array
*/
public function reset_userdata($data) {
global $DB;
$componentstr = get_string('modulenameplural', 'mod_newsletter');
$status = array();
$newsletterssql = "SELECT n.id
FROM {newsletter} n
WHERE n.course = :course";
$params = array("course" => $data->courseid);
$nlids = $DB->get_fieldset_sql($newsletterssql, $params);
list($sql, $inparams) = $DB->get_in_or_equal($nlids);
$DB->delete_records_select('newsletter_subscriptions', "newsletterid $sql", $inparams);
$DB->delete_records_select('newsletter_deliveries', "newsletterid $sql", $inparams);
$status[] = array(
'component' => $componentstr,
'item' => get_string('delete_all_subscriptions', 'newsletter'), 'error' => false
);
return $status;
}
private function get_js_module($strings = array()) {
$jsmodule = array(
'name' => 'mod_newsletter', 'fullpath' => '/mod/newsletter/module.js',
'requires' => array('node', 'event', 'node-screen', 'panel', 'node-event-delegate'),
'strings' => $strings
);
return $jsmodule;
}
/**
* returns an array of localised subscription status names with according key stored in database column "health"
*
* @return array
*/
public function get_subscription_statuslist() {
return array(
NEWSLETTER_SUBSCRIBER_STATUS_OK => get_string('health_0', 'mod_newsletter'),
NEWSLETTER_SUBSCRIBER_STATUS_PROBLEMATIC => get_string('health_1', 'mod_newsletter'),
NEWSLETTER_SUBSCRIBER_STATUS_BLACKLISTED => get_string('health_2', 'mod_newsletter'),
NEWSLETTER_SUBSCRIBER_STATUS_UNSUBSCRIBED => get_string('health_4', 'mod_newsletter')
);
}
/**
* Render the view according to passed parameters
*
* @return string rendered view
*/
public function view($params): string {
$output = '';
switch ($params[NEWSLETTER_PARAM_ACTION]) {
case NEWSLETTER_ACTION_VIEW_NEWSLETTER:
require_capability('mod/newsletter:viewnewsletter', $this->context);
$output = $this->view_newsletter($params);
break;
case NEWSLETTER_ACTION_CREATE_ISSUE:
require_capability('mod/newsletter:createissue', $this->context);
$output = $this->view_edit_issue_page($params);
break;
case NEWSLETTER_ACTION_DUPLICATE_ISSUE:
require_capability('mod/newsletter:createissue', $this->context);
$this->duplicate_issue($params['issue']);
$output = $this->view_newsletter($params);
break;
case NEWSLETTER_ACTION_EDIT_ISSUE:
require_capability('mod/newsletter:editissue', $this->context);
$output = $this->view_edit_issue_page($params);
break;
case NEWSLETTER_ACTION_READ_ISSUE:
require_capability('mod/newsletter:readissue', $this->context);
$output = $this->view_read_issue_page($params);
break;
case NEWSLETTER_ACTION_DELETE_ISSUE:
require_capability('mod/newsletter:deleteissue', $this->context);
$output = $this->view_delete_issue_page($params);
break;
case NEWSLETTER_ACTION_MANAGE_SUBSCRIPTIONS:
require_capability('mod/newsletter:managesubscriptions', $this->context);
$output = $this->view_manage_subscriptions($params);
break;
case NEWSLETTER_ACTION_EDIT_SUBSCRIPTION:
require_capability('mod/newsletter:editsubscription', $this->context);
$output = $this->view_edit_subscription($params);
break;
case NEWSLETTER_ACTION_DELETE_SUBSCRIPTION:
require_capability('mod/newsletter:deletesubscription', $this->context);
$output = $this->view_delete_subscription($params);
break;
case NEWSLETTER_ACTION_SUBSCRIBE:
require_capability('mod/newsletter:manageownsubscription', $this->context);
if ($this->get_subscription_status($this->get_subid()) == NEWSLETTER_SUBSCRIBER_STATUS_UNSUBSCRIBED) {
$output = $this->display_resubscribe_form($params);
} else {
$this->subscribe();
$url = $this->get_url();
redirect($url);
}
break;
case NEWSLETTER_ACTION_UNSUBSCRIBE:
require_capability('mod/newsletter:manageownsubscription', $this->context);
$this->unsubscribe($this->get_subid());
$url = $this->get_url();
redirect($url);
break;
case NEWSLETTER_ACTION_GUESTSUBSCRIBE:
require_capability('mod/newsletter:viewnewsletter', $this->context);
$output = $this->display_guest_subscribe_form($params);
break;
default:
throw new moodle_exception (
'Wrong ' . NEWSLETTER_PARAM_ACTION . ' parameter value: ' . $params[NEWSLETTER_PARAM_ACTION]
);
}
return $output;
}
/**
* display a subscription form for guest users.
* requires email auth plugin to be enabled
*
* @param array $params url params passed as get variables
* @return string html rendered guest subscription
*/
private function display_guest_subscribe_form(array $params): string {
global $PAGE;
$PAGE->requires->js_module($this->get_js_module());
$authplugin = get_auth_plugin('email');
if (!$authplugin->can_signup()) {
throw new moodle_exception ('notlocalisederrormessage', 'error', '', 'Sorry, you may not use this page.');
}
$output = '';
$renderer = $this->get_renderer();
$output .= $renderer->render(
new \newsletter_header(
$this->get_instance(),
$this->get_context(),
false,
$this->get_course_module()->id,
$params['embed']
)
);
$mform = new \mod_newsletter_guest_signup_form(
null,
array(
'id' => $this->get_course_module()->id,
NEWSLETTER_PARAM_ACTION => NEWSLETTER_ACTION_GUESTSUBSCRIBE,
'embed' => $params['embed']
)
);
if ($mform->is_cancelled()) {
redirect(new moodle_url('view.php', array(
'id' => $this->get_course_module()->id,
NEWSLETTER_PARAM_ACTION => NEWSLETTER_ACTION_VIEW_NEWSLETTER,
'embed' => $params['embed']
)));
} else if ($data = $mform->get_data()) {
$this->subscribe_guest($data->firstname, $data->lastname, $data->email);
$a = $data->email;
$output .= html_writer::div(get_string('guestsubscriptionsuccess', 'newsletter', $a));
$url = $this->get_url();
if(isset($data->embed)) {
$url->params(['embed' => $data->embed]);
}
$output .= html_writer::link(
$url,
get_string('continue'),
array('class' => 'btn mdl-align', 'target' => '_top')
);
return $output;
} else if ($this->get_instance()->allowguestusersubscriptions && (!isloggedin() || isguestuser())) {
$output .= $renderer->render(new \newsletter_form($mform, null));
$output .= $renderer->render_footer();
return $output;
}
return $output;
}
/**
* display a resubscription form for users who are unsubscribed and want to subscribe again.
*
* @param array $params url params passed as get variables
* @return string html rendered resubscription
*/
private function display_resubscribe_form(array $params): string {
global $PAGE;
$PAGE->requires->js_module($this->get_js_module());
$output = '';
$renderer = $this->get_renderer();
$output .= $renderer->render(
new \newsletter_header(
$this->get_instance(),
$this->get_context(),
false,
$this->get_course_module()->id
)
);
$mform = new \mod_newsletter_resubscribe_form(
null,
array(
'id' => $this->get_course_module()->id,
NEWSLETTER_PARAM_ACTION => NEWSLETTER_ACTION_SUBSCRIBE
)
);
if ($mform->is_cancelled()) {
redirect(
new moodle_url(
'view.php',
array(
'id' => $this->get_course_module()->id,
NEWSLETTER_PARAM_ACTION => NEWSLETTER_ACTION_VIEW_NEWSLETTER
)
)
);
} else if ($data = $mform->get_data()) {
if ($data->resubscribe_confirmation) {
$this->subscribe(0, false, NEWSLETTER_SUBSCRIBER_STATUS_OK, true, 0);
$output .= html_writer::div(' ');
$output .= html_writer::div(get_string('resubscriptionsuccess', 'mod_newsletter'));
$output .= html_writer::div(' ');
$url = $this->get_url();
$output .= html_writer::link(
$url,
get_string('continue'),
array('class' => 'btn mdl-align')
);
$output .= $renderer->render_footer();
return $output;
} else {
redirect(
new moodle_url(
'view.php',
array(
'id' => $this->get_course_module()->id,
NEWSLETTER_PARAM_ACTION => NEWSLETTER_ACTION_VIEW_NEWSLETTER
)
)
);
}
} else {
$output .= $renderer->render(new \newsletter_form($mform, null));
$output .= $renderer->render_footer();
return $output;
}
return $output;
}
/**
* Display all newsletter issues in a view. Display action links according to capabilities
*
* @param array $params
* @return string html
*/
private function view_newsletter(array $params): string {
global $CFG;
$renderer = $this->get_renderer();
$output = '';
$output .= $renderer->render(
new \newsletter_header(
$this->get_instance(),
$this->get_context(),
false,
$this->get_course_module()->id
)
);
$str = file_rewrite_pluginfile_urls(
$this->get_instance()->intro,
'pluginfile.php',
$this->get_context()->id,
'mod_newsletter',
'intro',
$this->get_course_module()->id
);
$output .= $renderer->render(
new \newsletter_main_toolbar(
$this->get_course_module()->id,
$params[NEWSLETTER_PARAM_GROUP_BY],
has_capability('mod/newsletter:createissue', $this->context),
has_capability('mod/newsletter:managesubscriptions', $this->context)
)
);
if (
has_capability('mod/newsletter:manageownsubscription', $this->context) &&
$this->instance->subscriptionmode != NEWSLETTER_SUBSCRIPTION_MODE_FORCED
) {
if (!$this->is_subscribed()) {
$url = $this->get_subsribe_url();
$text = get_string('subscribe', 'mod_newsletter');
$output .= html_writer::link($url, $text, ['class', 'btn btn-primary m-2']);
} else {
$url = new moodle_url(
'/mod/newsletter/view.php',
array(
NEWSLETTER_PARAM_ID => $this->get_course_module()->id,
NEWSLETTER_PARAM_ACTION => NEWSLETTER_ACTION_UNSUBSCRIBE
)
);
$text = get_string('unsubscribe', 'mod_newsletter');
$output .= html_writer::link($url, $text, ['class' => 'btn btn-primary m-2']);
}
} else {
if (!empty($CFG->registerauth) and is_enabled_auth('email')) {
$guestsignuppossible = true;
} else {
$guestsignuppossible = false;
}
if ($this->get_instance()->allowguestusersubscriptions && (!isloggedin() || isguestuser()) && $guestsignuppossible) {
$url = new moodle_url(
'/mod/newsletter/view.php',
array(
NEWSLETTER_PARAM_ID => $this->get_course_module()->id,
NEWSLETTER_PARAM_ACTION => NEWSLETTER_ACTION_GUESTSUBSCRIBE
)
);
$text = get_string('subscribe', 'mod_newsletter');
$output .= html_writer::link($url, $text, array('class' => 'btn btn-primary m-2'));
}
}
$issuelist = $this->prepare_issue_list('', $params[NEWSLETTER_PARAM_GROUP_BY]);
if ($issuelist) {
$output .= $renderer->render($issuelist);
} else {
$output .= '<h2>' . get_string('no_issues', 'mod_newsletter') . '</h2>';
}
$output .= $renderer->render_footer();
return $output;
}
/**
* Prepare newsletter issue for reading
*
* @param array $params
* @return string html of newsletter issue
*/
private function view_read_issue_page(array $params) {
global $CFG;
if (!(has_capability('mod/newsletter:editissue', $this->get_context())) && $this->get_issue(
$params[NEWSLETTER_PARAM_ISSUE]
)->publishon > time()) {
require_capability('mod/newsletter:editissue', $this->get_context());
}
if (!(has_capability('mod/newsletter:createissue', $this->get_context())) && $this->get_issue(
$params[NEWSLETTER_PARAM_ISSUE]
)->publishon > time()) {
require_capability('mod/newsletter:createissue', $this->get_context());
}
$renderer = $this->get_renderer();
$output = $renderer->render(
new \newsletter_header(
$this->get_instance(),
$this->get_context(),
false,
$this->get_course_module()->id
)
);
$currentissue = $this->get_issue($params[NEWSLETTER_PARAM_ISSUE]);
if (has_capability('mod/newsletter:editissue', $this->get_context())) {
$url = new moodle_url(
'/mod/newsletter/view.php',
array(
NEWSLETTER_PARAM_ID => $this->get_course_module()->id,
'action' => NEWSLETTER_ACTION_EDIT_ISSUE,
NEWSLETTER_PARAM_ISSUE => $currentissue->id
)
);
$output .= $renderer->render(
new \newsletter_action_link(
$url,
get_string('edit_issue', 'mod_newsletter'),
'btn btn-default'
)
);
}
$navigationbar = new \newsletter_navigation_bar(
$currentissue,
$this->get_first_issue($currentissue),
$this->get_previous_issue($currentissue),
$this->get_next_issue($currentissue),
$this->get_last_issue($currentissue)
);
$output .= $renderer->render($navigationbar);
// Generate table of content.
$parsedhtml = new \mod_newsletter\issue_parser($currentissue);
$currentissue->htmlcontent = $parsedhtml->get_parsed_html();
// Render the html with inline css based on the used stylesheet.
$currentissue->htmlcontent = file_rewrite_pluginfile_urls(
$currentissue->htmlcontent,
'pluginfile.php',
$this->get_context()->id,
'mod_newsletter',
NEWSLETTER_FILE_AREA_ISSUE,
$params[NEWSLETTER_PARAM_ISSUE],
\mod_newsletter\issue_form::editor_options(
$this->get_context(),
$params[NEWSLETTER_PARAM_ISSUE]
)
);
$currentissue->htmlcontent = $this->inline_css(
$currentissue->htmlcontent,
$currentissue->stylesheetid
);
$output .= $renderer->render(new \newsletter_issue($currentissue));
$fs = get_file_storage();
$files = $fs->get_area_files(
$this->get_context()->id,
'mod_newsletter',
NEWSLETTER_FILE_AREA_ATTACHMENT,
$currentissue->id,
"",
false
);
foreach ($files as $file) {
$file->link = file_encode_url(
$CFG->wwwroot . '/pluginfile.php',
'/' . $this->get_context()->id . '/mod_newsletter/attachments/' . $currentissue->id . '/' . $file->get_filename()
);
}
if (!empty($files)) {
$output .= $renderer->render(new \newsletter_attachment_list($files));
} else {
$output .= $renderer->render_newsletter_attachment_list_empty();
}
if (has_capability('mod/newsletter:editissue', $this->get_context())) {
$output .= $renderer->render(
new \newsletter_action_link(
$url,
get_string('edit_issue', 'mod_newsletter'),
'btn btn-default'
)
);
}
$output .= $renderer->render($navigationbar);
$output .= $renderer->render_footer();
$params = array(
'context' => $this->get_context(),
'objectid' => $params[NEWSLETTER_PARAM_ISSUE],
'other' => array('newsletterid' => $this->get_instance()->id)
);
$event = \mod_newsletter\event\issue_viewed::create($params);
$event->trigger();
return $output;
}
/**
* delete a single issue of a newsletter can only be done when not yet sent
*
* @param array $params
* @return string rendered html
*/
private function view_delete_issue_page(array $params) {
global $OUTPUT;
if (!$params[NEWSLETTER_PARAM_ISSUE] || !$this->check_issue_id(
$params[NEWSLETTER_PARAM_ISSUE]
)) {
throw new moodle_exception (
'Wrong ' . NEWSLETTER_PARAM_ISSUE . ' parameter value: ' . $params[NEWSLETTER_PARAM_ISSUE]
);
}
if ($params[NEWSLETTER_PARAM_CONFIRM] != NEWSLETTER_CONFIRM_UNKNOWN) {
$url = $this->get_url();
if ($params[NEWSLETTER_PARAM_CONFIRM] == NEWSLETTER_CONFIRM_YES) {
$this->delete_issue($params[NEWSLETTER_PARAM_ISSUE]);
redirect($url);
} else if ($params[NEWSLETTER_PARAM_CONFIRM] == NEWSLETTER_CONFIRM_NO) {
redirect($url);
} else {
throw new moodle_exception ("Wrong confirm!");
}
}
$renderer = $this->get_renderer();
$output = '';
$output .= $renderer->render(
new \newsletter_header(
$this->get_instance(),
$this->get_context(),
false,
$this->get_course_module()->id
)
);
$url = new moodle_url(
'/mod/newsletter/view.php',
array(
NEWSLETTER_PARAM_ID => $this->get_course_module()->id,
NEWSLETTER_PARAM_ISSUE => $params[NEWSLETTER_PARAM_ISSUE],
NEWSLETTER_PARAM_ACTION => NEWSLETTER_ACTION_DELETE_ISSUE
)
);
$output .= $OUTPUT->confirm(
get_string('delete_issue_question', 'mod_newsletter'),
new moodle_url($url, array(NEWSLETTER_PARAM_CONFIRM => NEWSLETTER_CONFIRM_YES)),
new moodle_url($url, array(NEWSLETTER_PARAM_CONFIRM => NEWSLETTER_CONFIRM_NO))
);
$output .= $renderer->render_footer();
return $output;
}
/**
* Display newsletter issue in editing mode
*
* @param array $params
* @return string rendered HTML
*/
private function view_edit_issue_page(array $params) {
global $CFG, $PAGE;
if (!$this->check_issue_id($params[NEWSLETTER_PARAM_ISSUE])) {
throw new moodle_exception (
'Wrong ' . NEWSLETTER_PARAM_ISSUE . ' parameter value: ' . $params[NEWSLETTER_PARAM_ISSUE]
);
}
$context = $this->get_context();
$issue = $this->get_issue($params[NEWSLETTER_PARAM_ISSUE]);
$newsletterconfig = $this->get_config();
if (is_null($issue)) {
$issue = new stdClass();
$issue->htmlcontent = '';
$issue->id = 0;
$issue->title = '';
$issue->publishon = strtotime("+48 hours");
$issue->toc = 0;
$issue->stylesheetid = NEWSLETTER_DEFAULT_STYLESHEET;
$issue->delivered = NEWSLETTER_DELIVERY_STATUS_UNKNOWN;
}
// Publishon can not be altered, if delivery has already started or been completed.
$deliverystartedorcompleted = 'yes';
if ($issue->delivered == NEWSLETTER_DELIVERY_STATUS_UNKNOWN || $issue->delivered == NEWSLETTER_DELIVERY_STATUS_FAILED) {
$deliverystartedorcompleted = 'no';
}
$issue->messageformat = editors_get_preferred_format();
$issue->newsletterid = $this->get_instance()->id;
$fs = get_file_storage();
$files = $fs->get_area_files(
$context->id,
'mod_newsletter',
NEWSLETTER_FILE_AREA_STYLESHEET,
$this->get_instance()->id,
'filename',
false
);
$options = array();
$options[NEWSLETTER_DEFAULT_STYLESHEET] = "{$CFG->wwwroot}/mod/newsletter/reset.css";
foreach ($files as $file) {
$url = "{$CFG->wwwroot}/pluginfile.php/{$file->get_contextid()}/mod_newsletter/" . NEWSLETTER_FILE_AREA_STYLESHEET;
$options[$file->get_id()] = $url . $file->get_filepath() . $file->get_itemid() . '/' . $file->get_filename();
}
$PAGE->requires->js_module($this->get_js_module());
$PAGE->requires->js_init_call(
'M.mod_newsletter.init_editor',
array($options, $issue->id ? $issue->stylesheetid : NEWSLETTER_DEFAULT_STYLESHEET)
);
$mform = new \mod_newsletter\issue_form(
null,
array('newsletter' => $this, 'issue' => $issue, 'context' => $context)
);
$draftitemid = file_get_submitted_draft_itemid('attachments');
file_prepare_draft_area(
$draftitemid,
$context->id,
'mod_newsletter',
NEWSLETTER_FILE_AREA_ATTACHMENT,
empty($issue->id) ? null : $issue->id,
\mod_newsletter\issue_form::attachment_options($newsletterconfig, $this->get_context())
);
$issueid = empty($issue->id) ? null : $issue->id;
$draftideditor = file_get_submitted_draft_itemid('htmlcontent');
$currenttext = file_prepare_draft_area(
$draftideditor,
$context->id,
'mod_newsletter',
NEWSLETTER_FILE_AREA_ISSUE,
$issueid,
\mod_newsletter\issue_form::editor_options($context, $issueid),
$issue->htmlcontent
);
$setarray = array(
'attachments' => $draftitemid, 'title' => $issue->title,
'htmlcontent' => array(
'text' => $currenttext,
'format' => empty($issue->messageformat) ? editors_get_preferred_format() : $issue->messageformat,
'itemid' => $draftideditor
), 'deliverystarted' => $deliverystartedorcompleted,
'toc' => $issue->toc, 'publishon' => $issue->publishon,
'stylesheetid' => $issue->stylesheetid
);
// When we only want to recalculate filter.
if ($mform->no_submit_button_pressed()) {
$data = $mform->get_submitted_data();
if ($data->issue) {
$issue = $this->update_issue($data);
} else {
// We need the new issue object.
$issue = $this->return_issue_from_form_data($data);
}
// Now we set the form with the right values, before it will be rendered again.
// As we want a static element to be rendered again, we recreate the whole form.
$mform = new \mod_newsletter\issue_form(
null,
array('newsletter' => $this, 'issue' => $issue, 'context' => $context)
);
} else if ($data = $mform->get_data()) {
if (!$data->issue) {
$this->add_issue($data);
} else {
$this->update_issue($data);
}
redirect($this->get_url());
}
userfilter::set_form_values($issue, $setarray);
$mform->set_data($setarray);
$output = '';
$renderer = $this->get_renderer();
$output .= $renderer->render(
new \newsletter_header(
$this->get_instance(),
$this->get_context(),
false,
$this->get_course_module()->id
)
);
// TODO: remove ugly config hack and provide js for atto.
$texteditors = $CFG->texteditors;
$CFG->texteditors = 'tinymce,textarea';
$output .= $renderer->render(
new \newsletter_form($mform, get_string('edit_issue_title', 'mod_newsletter'))
);
$CFG->texteditors = $texteditors;
$output .= $renderer->render_footer();
return $output;
}
/**
* Display a list of users with statusses and action links in order to manage single subscriptions
*
* @param array $params
* @return string rendered html
*/
private function view_manage_subscriptions(array $params) {
global $DB, $OUTPUT;
$url = new moodle_url(
'/mod/newsletter/view.php',
array(
'id' => $this->get_course_module()->id,
'action' => NEWSLETTER_ACTION_MANAGE_SUBSCRIPTIONS
)
);
list($filtersql, $filterparams) = $this->get_filter_sql($params);
if ($params['resetbutton'] !== '') {
redirect($url);
}
$from = $params[NEWSLETTER_PARAM_FROM];
$count = $params[NEWSLETTER_PARAM_COUNT];
$subscriptions = $DB->get_records_sql($filtersql, $filterparams, $from, $count);
$sqlparams = array('newsletterid' => $this->get_instance()->id);
$total = $DB->count_records('newsletter_subscriptions', $sqlparams);
list($countsql, $countparams) = $this->get_filter_sql($params, true);
if ($countsql > 0) {
$totalfiltered = $DB->count_records_sql($countsql, $countparams);
} else {
$totalfiltered = 0;
}
$pages = $this->calculate_pages($totalfiltered, $from, $count);
$columns = array(
NEWSLETTER_SUBSCRIPTION_LIST_COLUMN_EMAIL,
NEWSLETTER_SUBSCRIPTION_LIST_COLUMN_NAME, NEWSLETTER_SUBSCRIPTION_LIST_COLUMN_HEALTH,
NEWSLETTER_SUBSCRIPTION_LIST_COLUMN_BOUNCERATIO,
NEWSLETTER_SUBSCRIPTION_LIST_COLUMN_TIMESUBSCRIBED,
NEWSLETTER_SUBSCRIPTION_LIST_COLUMN_ACTIONS
);
$filterform = new \mod_newsletter\subscription\mod_newsletter_subscription_filter_form(
'view.php',
array('newsletter' => $this),
'get',
'',
array('id' => 'filterform')
);
$filterform->set_data(
array(
'search' => $params['search'], 'status' => $params['status'],
'count' => $params['count'], 'orderby' => $params['orderby']
)
);
$renderer = $this->get_renderer();