Skip to content

Commit 4b14068

Browse files
author
David Monllao
committed
Merge branch 'MDL-45837_master' of git://github.com/markn86/moodle
2 parents 5ac4e27 + e960152 commit 4b14068

12 files changed

+898
-4
lines changed

grade/edit/letter/index.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,45 @@
184184
// The letter has been assigned to another boundary, we update it.
185185
$record->id = $pool[$boundary]->id;
186186
$DB->update_record('grade_letters', $record);
187+
// Trigger the letter grade updated event.
188+
$event = \core\event\grade_letter_updated::create(array(
189+
'objectid' => $record->id,
190+
'context' => $context,
191+
));
192+
$event->trigger();
187193
}
188194
unset($pool[$boundary]); // Remove the letter from the pool.
189195
} else if ($candidate = array_pop($pool)) {
190196
// The boundary is new, we update a random record from the pool.
191197
$record->id = $candidate->id;
192198
$DB->update_record('grade_letters', $record);
199+
// Trigger the letter grade updated event.
200+
$event = \core\event\grade_letter_updated::create(array(
201+
'objectid' => $record->id,
202+
'context' => $context,
203+
));
204+
$event->trigger();
193205
} else {
194206
// No records were found, this must be a new letter.
195-
$DB->insert_record('grade_letters', $record);
207+
$newid = $DB->insert_record('grade_letters', $record);
208+
// Trigger the letter grade added event.
209+
$event = \core\event\grade_letter_created::create(array(
210+
'objectid' => $newid,
211+
'context' => $context,
212+
));
213+
$event->trigger();
196214
}
197215
}
198216

199217
// Delete the unused records.
200218
foreach($pool as $leftover) {
201219
$DB->delete_records('grade_letters', array('id' => $leftover->id));
220+
// Trigger the letter grade deleted event.
221+
$event = \core\event\grade_letter_deleted::create(array(
222+
'objectid' => $leftover->id,
223+
'context' => $context,
224+
));
225+
$event->trigger();
202226
}
203227

204228
redirect($returnurl);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
@core @core_grades
2+
Feature: We can view the logs for any changes to grade letters.
3+
In order to view changes the letter boundary of a course
4+
As an administrator
5+
I need to add make changes and then view the logs.
6+
7+
Scenario: I override the letter boundaries and check the logs.
8+
Given the following "courses" exist:
9+
| fullname | shortname | category |
10+
| Course 1 | C1 | 0 |
11+
And I log in as "admin"
12+
And I am on "Course 1" course homepage
13+
And I navigate to "Setup > Course grade settings" in the course gradebook
14+
And I set the following fields to these values:
15+
| Grade display type | Letter |
16+
And I press "Save changes"
17+
And I navigate to "Letters" in the course gradebook
18+
And I follow "Edit grade letters"
19+
And I set the following fields to these values:
20+
| id_override | 1 |
21+
| id_gradeboundary10 | 57 |
22+
And I press "Save changes"
23+
And I follow "Edit grade letters"
24+
And I set the following fields to these values:
25+
| id_override | 1 |
26+
| id_gradeboundary10 | 50 |
27+
And I press "Save changes"
28+
And I follow "Edit grade letters"
29+
And I set the following fields to these values:
30+
| id_override | 1 |
31+
| id_gradeletter11 | |
32+
| id_gradeboundary11 | |
33+
And I press "Save changes"
34+
When I navigate to "Live logs" node in "Site administration > Reports"
35+
Then I should see "Grade letter created"
36+
And I should see "Grade letter updated"
37+
And I should see "Grade letter deleted"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@core @core_grades
2+
Feature: We can view the logs for any changes to grade scales.
3+
In order to view changes grade scales
4+
As an administrator
5+
I need to add make changes and then view the logs.
6+
7+
Scenario: I edit scales and then view the logs.
8+
Given I log in as "admin"
9+
And I navigate to "Scales" node in "Site administration > Grades"
10+
# Add a scale
11+
And I press "Add a new scale"
12+
And I set the following fields to these values:
13+
| Name | Letterscale |
14+
| Scale | F,D,C,B,A |
15+
And I press "Save changes"
16+
# Delete first scale
17+
And I follow "Delete"
18+
And I press "Continue"
19+
# Edit first scale
20+
And I follow "Edit"
21+
And I set the following fields to these values:
22+
| id_scale | ONE,TWO,THREE |
23+
And I press "Save changes"
24+
When I navigate to "Live logs" node in "Site administration > Reports"
25+
Then I should see "Scale created"
26+
And I should see "Scale updated"
27+
And I should see "Scale deleted"

grade/tests/events_test.php

+204
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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+
* Unit tests for events found in /grade/letter and /grade/scale.
19+
*
20+
* @package core_grades
21+
* @category test
22+
* @copyright 2017 Stephen Bourget
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
24+
*/
25+
26+
defined('MOODLE_INTERNAL') || die();
27+
28+
global $CFG;
29+
require_once($CFG->dirroot . '/grade/lib.php');
30+
31+
/**
32+
* Unit tests for grade events.
33+
*
34+
* @package core_grades
35+
* @category test
36+
* @copyright 2017 Stephen Bourget
37+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38+
*/
39+
class core_grade_events_test extends advanced_testcase {
40+
41+
/** @var stdClass the course used for testing */
42+
private $course;
43+
44+
/**
45+
* Test set up.
46+
*
47+
* This is executed before running any test in this file.
48+
*/
49+
public function setUp() {
50+
$this->resetAfterTest();
51+
52+
$this->setAdminUser();
53+
$this->course = $this->getDataGenerator()->create_course();
54+
}
55+
56+
/**
57+
* Test the grade letter created event.
58+
*
59+
* There is no external API for triggering this event, so the unit test will simply
60+
* create and trigger the event and ensure the data is returned as expected.
61+
*/
62+
public function test_grade_letter_created() {
63+
// Create a grade letter created event.
64+
$event = \core\event\grade_letter_created::create(array(
65+
'objectid' => 10,
66+
'context' => context_course::instance($this->course->id)
67+
));
68+
69+
// Trigger and capture the event.
70+
$sink = $this->redirectEvents();
71+
$event->trigger();
72+
$events = $sink->get_events();
73+
$event = reset($events);
74+
75+
// Check that the event data is valid.
76+
$this->assertInstanceOf('\core\event\grade_letter_created', $event);
77+
$this->assertEquals(context_course::instance($this->course->id), $event->get_context());
78+
}
79+
80+
/**
81+
* Test the grade letter deleted event.
82+
*
83+
* There is no external API for triggering this event, so the unit test will simply
84+
* create and trigger the event and ensure the data is returned as expected.
85+
*/
86+
public function test_grade_letter_deleted() {
87+
// Create a grade letter deleted event.
88+
$event = \core\event\grade_letter_deleted::create(array(
89+
'objectid' => 10,
90+
'context' => context_course::instance($this->course->id)
91+
));
92+
93+
// Trigger and capture the event.
94+
$sink = $this->redirectEvents();
95+
$event->trigger();
96+
$events = $sink->get_events();
97+
$event = reset($events);
98+
99+
// Check that the event data is valid.
100+
$this->assertInstanceOf('\core\event\grade_letter_deleted', $event);
101+
$this->assertEquals(context_course::instance($this->course->id), $event->get_context());
102+
}
103+
104+
/**
105+
* Test the grade letter updated event.
106+
*
107+
* There is no external API for triggering this event, so the unit test will simply
108+
* create and trigger the event and ensure the data is returned as expected.
109+
*/
110+
public function test_grade_letter_updated() {
111+
// Create a grade letter updated event.
112+
$event = \core\event\grade_letter_updated::create(array(
113+
'objectid' => 10,
114+
'context' => context_course::instance($this->course->id)
115+
));
116+
117+
// Trigger and capture the event.
118+
$sink = $this->redirectEvents();
119+
$event->trigger();
120+
$events = $sink->get_events();
121+
$event = reset($events);
122+
123+
// Check that the event data is valid.
124+
$this->assertInstanceOf('\core\event\grade_letter_updated', $event);
125+
$this->assertEquals(context_course::instance($this->course->id), $event->get_context());
126+
}
127+
128+
/**
129+
* Test the scale created event.
130+
*/
131+
public function test_scale_created() {
132+
$gradescale = new grade_scale();
133+
$gradescale->name = 'unittestscale3';
134+
$gradescale->courseid = $this->course->id;
135+
$gradescale->userid = 317;
136+
$gradescale->scale = 'Distinction, Very Good, Good, Pass, Fail';
137+
$gradescale->description = 'This scale is used to mark standard assignments.';
138+
139+
$url = new \moodle_url('/grade/edit/scale/index.php', array('id' => $this->course->id));
140+
141+
// Trigger and capture the event.
142+
$sink = $this->redirectEvents();
143+
$id = $gradescale->insert();
144+
$events = $sink->get_events();
145+
$event = reset($events);
146+
147+
// Check that the event data is valid.
148+
$this->assertInstanceOf('\core\event\scale_created', $event);
149+
$this->assertEquals($id, $event->objectid);
150+
$this->assertEquals($url, $event->get_url());
151+
$this->assertEquals(context_course::instance($this->course->id), $event->get_context());
152+
}
153+
154+
/**
155+
* Test the scale deleted event.
156+
*/
157+
public function test_scale_deleted() {
158+
$gradescale = new grade_scale();
159+
$gradescale->name = 'unittestscale3';
160+
$gradescale->courseid = $this->course->id;
161+
$gradescale->userid = 317;
162+
$gradescale->scale = 'Distinction, Very Good, Good, Pass, Fail';
163+
$gradescale->description = 'This scale is used to mark standard assignments.';
164+
$gradescale->insert();
165+
166+
// Trigger and capture the event.
167+
$sink = $this->redirectEvents();
168+
$gradescale->delete();
169+
$events = $sink->get_events();
170+
$event = reset($events);
171+
172+
// Check that the event data is valid.
173+
$this->assertInstanceOf('\core\event\scale_deleted', $event);
174+
$this->assertEquals(context_course::instance($this->course->id), $event->get_context());
175+
}
176+
177+
/**
178+
* Test the scale updated event.
179+
*/
180+
public function test_scale_updated() {
181+
$gradescale = new grade_scale();
182+
$gradescale->name = 'unittestscale3';
183+
$gradescale->courseid = $this->course->id;
184+
$gradescale->userid = 317;
185+
$gradescale->scale = 'Distinction, Very Good, Good, Pass, Fail';
186+
$gradescale->description = 'This scale is used to mark standard assignments.';
187+
$id = $gradescale->insert();
188+
189+
$gradescale->name = 'Updated info for this unittest grade_scale';
190+
$url = new \moodle_url('/grade/edit/scale/index.php', array('id' => $this->course->id));
191+
192+
// Trigger and capture the event.
193+
$sink = $this->redirectEvents();
194+
$gradescale->update();
195+
$events = $sink->get_events();
196+
$event = reset($events);
197+
198+
// Check that the event data is valid.
199+
$this->assertInstanceOf('\core\event\scale_updated', $event);
200+
$this->assertEquals($id, $event->objectid);
201+
$this->assertEquals($url, $event->get_url());
202+
$this->assertEquals(context_course::instance($this->course->id), $event->get_context());
203+
}
204+
}

lang/en/grades.php

+6
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,13 @@
194194
$string['errorupdatinggradecategoryaggregation'] = 'Error updating the aggregation type of grade category ID {$a->id}';
195195
$string['errorupdatinggradeitemaggregationcoef'] = 'Error updating the aggregation coefficient (weight or extra credit) of grade item ID {$a->id}';
196196
$string['eventgradedeleted'] = 'Grade deleted';
197+
$string['eventgradelettercreated'] = 'Grade letter created';
198+
$string['eventgradeletterdeleted'] = 'Grade letter deleted';
199+
$string['eventgradeletterupdated'] = 'Grade letter updated';
197200
$string['eventgradeviewed'] = 'Grades were viewed in the gradebook';
201+
$string['eventscalecreated'] = 'Scale created';
202+
$string['eventscaledeleted'] = 'Scale deleted';
203+
$string['eventscaleupdated'] = 'Scale updated';
198204
$string['eventusergraded'] = 'User graded';
199205
$string['excluded'] = 'Excluded';
200206
$string['excluded_help'] = 'If ticked, the grade will not be included in any aggregation.';

0 commit comments

Comments
 (0)