Skip to content

Commit 75d6d49

Browse files
Amaia Anabitartetonyjbutler
authored andcommitted
MDL-58001 core_grades: New is_gradable() function in gradelib
Backported from main, introduced in MDL-85837.
1 parent dfd9b86 commit 75d6d49

3 files changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
issueNumber: MDL-85837
2+
notes:
3+
core_grades:
4+
- message: >-
5+
New 'is_gradable()' function has been created to return whether the item
6+
has any gradeitem that is GRADE_TYPE_VALUE or GRADE_TYPE_SCALE.
7+
type: improved

lib/gradelib.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,35 @@ function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance,
305305
}
306306
}
307307

308+
/**
309+
* Returns whether the item is gradable or not. It's considered gradable when there is at least one gradeitem
310+
* set as GRADE_TYPE_VALUE or GRADE_TYPE_SCALE.
311+
*
312+
* @category grade
313+
* @param int $courseid ID of course
314+
* @param string $itemtype Type of grade item. For example, 'mod' or 'block'
315+
* @param string $itemmodule More specific then $itemtype. For example, 'forum' or 'quiz'. May be NULL for some item types
316+
* @param int $iteminstance Instance ID of graded item. For example the forum ID.
317+
* @return bool returns true if the there is any grade item set as GRADE_TYPE_VALUE, GRADE_TYPE_SCALE.
318+
* @category grade
319+
*/
320+
function is_gradable(int $courseid, string $itemtype, string $itemmodule, int $iteminstance): bool {
321+
$items = grade_item::fetch_all([
322+
'itemtype' => $itemtype,
323+
'itemmodule' => $itemmodule,
324+
'iteminstance' => $iteminstance,
325+
'courseid' => $courseid,
326+
]);
327+
if ($items) {
328+
foreach ($items as $item) {
329+
if ($item->gradetype == GRADE_TYPE_VALUE || $item->gradetype == GRADE_TYPE_SCALE) {
330+
return true;
331+
}
332+
}
333+
}
334+
return false;
335+
}
336+
308337
/**
309338
* Updates a user's outcomes. Manual outcomes can not be updated.
310339
*

lib/tests/gradelib_test.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,71 @@ public function test_grade_update_mod_grades(): void {
5858
$this->assertTrue(grade_update_mod_grades($modinstance));
5959
}
6060

61+
62+
/**
63+
* Tests is_gradable() function return.
64+
*
65+
* @covers \is_gradable()
66+
* @dataProvider graditems_provider
67+
* @param array $gradetypes Grade item types to create.
68+
* @param bool $expected The expected result for is_gradable() function.
69+
* @return void
70+
*/
71+
public function test_is_gradable(array $gradetypes, bool $expected): void {
72+
$this->resetAfterTest();
73+
74+
$generator = $this->getDataGenerator();
75+
$course = $generator->create_course();
76+
$assignment = $generator->create_module('assign', ['course' => $course->id, 'gradetype' => GRADE_TYPE_NONE]);
77+
// Create grade items.
78+
foreach ($gradetypes as $gradetype) {
79+
$generator->create_grade_item(
80+
[
81+
'courseid' => $course->id,
82+
'itemtype' => 'mod',
83+
'itemmodule' => 'assign',
84+
'iteminstance' => $assignment->id,
85+
'gradetype' => $gradetype,
86+
]
87+
);
88+
}
89+
$this->assertEquals($expected, is_gradable($course->id, 'mod', 'assign', $assignment->id));
90+
}
91+
92+
/**
93+
* Data provider for testing test_is_gradable function.
94+
*
95+
* @return array
96+
*/
97+
public static function graditems_provider(): array {
98+
return [
99+
'No grade items' => [
100+
'gradetypes' => [],
101+
'expected' => false,
102+
],
103+
'No grading item' => [
104+
'gradetypes' => [GRADE_TYPE_NONE],
105+
'expected' => false,
106+
],
107+
'Grading by feedback' => [
108+
'gradetypes' => [GRADE_TYPE_TEXT],
109+
'expected' => false,
110+
],
111+
'Grading by points' => [
112+
'gradetypes' => [GRADE_TYPE_VALUE],
113+
'expected' => true,
114+
],
115+
'Grading by scale' => [
116+
'gradetypes' => [GRADE_TYPE_SCALE],
117+
'expected' => true,
118+
],
119+
'Mix of grading' => [
120+
'gradetypes' => [GRADE_TYPE_TEXT, GRADE_TYPE_NONE, GRADE_TYPE_VALUE, GRADE_TYPE_SCALE],
121+
'expected' => true,
122+
],
123+
];
124+
}
125+
61126
/**
62127
* Tests the function remove_grade_letters().
63128
*/

0 commit comments

Comments
 (0)