Skip to content

Commit ad481ba

Browse files
committed
#564 Only fire events when necessary
1 parent 885577b commit ad481ba

File tree

2 files changed

+65
-23
lines changed

2 files changed

+65
-23
lines changed

classes/template.php

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,19 @@ public function save($data) {
7474

7575
$DB->update_record('customcert_templates', $savedata);
7676

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+
}
7881
}
7982

8083
/**
8184
* Handles adding another page to the template.
8285
*
86+
* @param bool $triggertemplateupdatedevent
8387
* @return int the id of the page
8488
*/
85-
public function add_page() {
89+
public function add_page(bool $triggertemplateupdatedevent = true) {
8690
global $DB;
8791

8892
// Set the page number to 1 to begin with.
@@ -110,7 +114,10 @@ public function add_page() {
110114
$page->id = $pageid;
111115

112116
\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+
}
114121

115122
return $page->id;
116123
}
@@ -130,26 +137,29 @@ public function save_page($data) {
130137
if ($pages = $DB->get_records('customcert_pages', array('templateid' => $data->tid))) {
131138
// Loop through existing pages.
132139
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+
}
150162
}
151-
152-
\mod_customcert\event\template_updated::create_from_template($this)->trigger();
153163
}
154164
}
155165

@@ -552,4 +562,36 @@ public static function create($templatename, $contextid) {
552562

553563
return $template;
554564
}
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+
}
555597
}

lib.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function customcert_add_instance($data, $mform) {
4444
$data->id = $DB->insert_record('customcert', $data);
4545

4646
// Add a page to this customcert.
47-
$template->add_page();
47+
$template->add_page(false);
4848

4949
return $data->id;
5050
}

0 commit comments

Comments
 (0)