@@ -74,15 +74,19 @@ public function save($data) {
74
74
75
75
$ DB ->update_record ('customcert_templates ' , $ savedata );
76
76
77
- \mod_customcert \event \template_updated::create_from_template ($ this )->trigger ();
77
+ // Only trigger event if the name has changed.
78
+ if ($ savedata ->name != $ data ->name ) {
79
+ \mod_customcert \event \template_updated::create_from_template ($ this )->trigger ();
80
+ }
78
81
}
79
82
80
83
/**
81
84
* Handles adding another page to the template.
82
85
*
86
+ * @param bool $triggertemplateupdatedevent
83
87
* @return int the id of the page
84
88
*/
85
- public function add_page () {
89
+ public function add_page (bool $ triggertemplateupdatedevent = true ) {
86
90
global $ DB ;
87
91
88
92
// Set the page number to 1 to begin with.
@@ -110,7 +114,10 @@ public function add_page() {
110
114
$ page ->id = $ pageid ;
111
115
112
116
\mod_customcert \event \page_created::create_from_page ($ page , $ this )->trigger ();
113
- \mod_customcert \event \template_updated::create_from_template ($ this )->trigger ();
117
+
118
+ if ($ triggertemplateupdatedevent ) {
119
+ \mod_customcert \event \template_updated::create_from_template ($ this )->trigger ();
120
+ }
114
121
115
122
return $ page ->id ;
116
123
}
@@ -130,26 +137,29 @@ public function save_page($data) {
130
137
if ($ pages = $ DB ->get_records ('customcert_pages ' , array ('templateid ' => $ data ->tid ))) {
131
138
// Loop through existing pages.
132
139
foreach ($ pages as $ page ) {
133
- // Get the name of the fields we want from the form.
134
- $ width = 'pagewidth_ ' . $ page ->id ;
135
- $ height = 'pageheight_ ' . $ page ->id ;
136
- $ leftmargin = 'pageleftmargin_ ' . $ page ->id ;
137
- $ rightmargin = 'pagerightmargin_ ' . $ page ->id ;
138
- // Create the page data to update the DB with.
139
- $ p = new \stdClass ();
140
- $ p ->id = $ page ->id ;
141
- $ p ->width = $ data ->$ width ;
142
- $ p ->height = $ data ->$ height ;
143
- $ p ->leftmargin = $ data ->$ leftmargin ;
144
- $ p ->rightmargin = $ data ->$ rightmargin ;
145
- $ p ->timemodified = $ time ;
146
- // Update the page.
147
- $ DB ->update_record ('customcert_pages ' , $ p );
148
-
149
- \mod_customcert \event \page_updated::create_from_page ($ p , $ this )->trigger ();
140
+ // Only update if there is a difference.
141
+ if ($ this ->has_page_been_updated ($ page , $ data )) {
142
+ $ width = 'pagewidth_ ' . $ page ->id ;
143
+ $ height = 'pageheight_ ' . $ page ->id ;
144
+ $ leftmargin = 'pageleftmargin_ ' . $ page ->id ;
145
+ $ rightmargin = 'pagerightmargin_ ' . $ page ->id ;
146
+
147
+ $ p = new \stdClass ();
148
+ $ p ->id = $ page ->id ;
149
+ $ p ->width = $ data ->$ width ;
150
+ $ p ->height = $ data ->$ height ;
151
+ $ p ->leftmargin = $ data ->$ leftmargin ;
152
+ $ p ->rightmargin = $ data ->$ rightmargin ;
153
+ $ p ->timemodified = $ time ;
154
+
155
+ // Update the page.
156
+ $ DB ->update_record ('customcert_pages ' , $ p );
157
+
158
+ \mod_customcert \event \page_updated::create_from_page ($ p , $ this )->trigger ();
159
+
160
+ \mod_customcert \event \template_updated::create_from_template ($ this )->trigger ();
161
+ }
150
162
}
151
-
152
- \mod_customcert \event \template_updated::create_from_template ($ this )->trigger ();
153
163
}
154
164
}
155
165
@@ -552,4 +562,36 @@ public static function create($templatename, $contextid) {
552
562
553
563
return $ template ;
554
564
}
565
+
566
+ /**
567
+ * Checks if a page has been updated given form information
568
+ *
569
+ * @param $page
570
+ * @param $formdata
571
+ * @return bool
572
+ */
573
+ private function has_page_been_updated ($ page , $ formdata ): bool {
574
+ $ width = 'pagewidth_ ' . $ page ->id ;
575
+ $ height = 'pageheight_ ' . $ page ->id ;
576
+ $ leftmargin = 'pageleftmargin_ ' . $ page ->id ;
577
+ $ rightmargin = 'pagerightmargin_ ' . $ page ->id ;
578
+
579
+ if ($ page ->width != $ formdata ->$ width ) {
580
+ return true ;
581
+ }
582
+
583
+ if ($ page ->height != $ formdata ->$ height ) {
584
+ return true ;
585
+ }
586
+
587
+ if ($ page ->leftmargin != $ formdata ->$ leftmargin ) {
588
+ return true ;
589
+ }
590
+
591
+ if ($ page ->rightmargin != $ formdata ->$ rightmargin ) {
592
+ return true ;
593
+ }
594
+
595
+ return false ;
596
+ }
555
597
}
0 commit comments