Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]
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 namespace gradereport_user; 18 19 use core_external\external_api; 20 use externallib_advanced_testcase; 21 use gradereport_user\external\user as user_external; 22 23 defined('MOODLE_INTERNAL') || die(); 24 25 global $CFG; 26 27 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 28 29 /** 30 * User grade report functions unit tests 31 * 32 * @package gradereport_user 33 * @category external 34 * @copyright 2015 Juan Leyva <juan@moodle.com> 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class externallib_test extends externallib_advanced_testcase { 38 39 /** 40 * Loads some data to be used by the different tests 41 * @param int $s1grade Student 1 grade 42 * @param int $s2grade Student 2 grade 43 * @return array Course and users instances 44 */ 45 private function load_data(int $s1grade, int $s2grade): array { 46 global $DB; 47 48 $course = $this->getDataGenerator()->create_course(['groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1]); 49 50 $studentrole = $DB->get_record('role', ['shortname' => 'student']); 51 $student1 = $this->getDataGenerator()->create_user(['idnumber' => 'testidnumber']); 52 $this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id); 53 54 $student2 = $this->getDataGenerator()->create_user(); 55 $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id); 56 57 $teacherrole = $DB->get_record('role', ['shortname' => 'teacher']); 58 $teacher = $this->getDataGenerator()->create_user(); 59 $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id); 60 61 $context = \context_course::instance($course->id); 62 assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $teacherrole->id, $context); 63 accesslib_clear_all_caches_for_unit_testing(); 64 65 $group1 = $this->getDataGenerator()->create_group(['courseid' => $course->id]); 66 $group2 = $this->getDataGenerator()->create_group(['courseid' => $course->id]); 67 groups_add_member($group1->id, $student1->id); 68 groups_add_member($group1->id, $teacher->id); 69 groups_add_member($group2->id, $student2->id); 70 71 $assignment = $this->getDataGenerator()->create_module('assign', ['name' => "Test assign", 'course' => $course->id]); 72 $modcontext = get_coursemodule_from_instance('assign', $assignment->id, $course->id); 73 $assignment->cmidnumber = $modcontext->id; 74 75 $student1grade = ['userid' => $student1->id, 'rawgrade' => $s1grade, 'idnumber' => 'testidnumber1']; 76 $student2grade = ['userid' => $student2->id, 'rawgrade' => $s2grade, 'idnumber' => 'testidnumber2']; 77 $studentgrades = [$student1->id => $student1grade, $student2->id => $student2grade]; 78 assign_grade_item_update($assignment, $studentgrades); 79 80 return [ 81 $course, 82 $teacher, 83 $student1, 84 $student2, 85 $assignment 86 ]; 87 } 88 89 /** 90 * Test get_grades_table function case teacher 91 */ 92 public function test_get_grades_table_teacher() { 93 94 $this->resetAfterTest(true); 95 96 $s1grade = 80; 97 $s2grade = 60; 98 99 list($course, $teacher, $student1, $student2, $assignment) = $this->load_data($s1grade, $s2grade); 100 101 // A teacher must see all student grades (in their group only). 102 $this->setUser($teacher); 103 104 $studentgrades = user_external::get_grades_table($course->id); 105 $studentgrades = external_api::clean_returnvalue(user_external::get_grades_table_returns(), $studentgrades); 106 107 // No warnings returned. 108 $this->assertCount(0, $studentgrades['warnings']); 109 110 // Check that only grades for the student in the teacher group are returned. 111 $this->assertCount(1, $studentgrades['tables']); 112 113 // Read returned grades. 114 $studentreturnedgrades = []; 115 116 $studentreturnedgrades[$studentgrades['tables'][0]['userid']] = 117 (int) $studentgrades['tables'][0]['tabledata'][2]['grade']['content']; 118 119 $this->assertEquals($s1grade, $studentreturnedgrades[$student1->id]); 120 } 121 122 /** 123 * Test get_grades_table function case student 124 */ 125 public function test_get_grades_table_student() { 126 global $CFG, $DB; 127 128 $this->resetAfterTest(true); 129 130 $s1grade = 80; 131 $s2grade = 60; 132 133 list($course, $teacher, $student1, $student2, $assignment) = $this->load_data($s1grade, $s2grade); 134 135 // A user can see his own grades. 136 $this->setUser($student1); 137 $studentgrade = user_external::get_grades_table($course->id, $student1->id); 138 $studentgrade = external_api::clean_returnvalue(user_external::get_grades_table_returns(), $studentgrade); 139 140 // No warnings returned. 141 $this->assertTrue(count($studentgrade['warnings']) == 0); 142 143 $this->assertTrue(count($studentgrade['tables']) == 1); 144 $student1returnedgrade = (int) $studentgrade['tables'][0]['tabledata'][2]['grade']['content']; 145 $this->assertEquals($s1grade, $student1returnedgrade); 146 147 } 148 149 /** 150 * Test get_grades_table function case incorrect permissions 151 */ 152 public function test_get_grades_table_permissions() { 153 global $CFG, $DB; 154 155 $this->resetAfterTest(true); 156 157 $s1grade = 80; 158 $s2grade = 60; 159 160 list($course, $teacher, $student1, $student2, $assignment) = $this->load_data($s1grade, $s2grade); 161 162 $this->setUser($student2); 163 164 try { 165 $studentgrade = user_external::get_grades_table($course->id, $student1->id); 166 $this->fail('Exception expected due to not perissions to view other user grades.'); 167 } catch (\moodle_exception $e) { 168 $this->assertEquals('notingroup', $e->errorcode); 169 } 170 } 171 172 /** 173 * Test view_grade_report function 174 */ 175 public function test_view_grade_report() { 176 global $USER; 177 178 $this->resetAfterTest(true); 179 180 $s1grade = 80; 181 $s2grade = 60; 182 list($course, $teacher, $student1, $student2, $assignment) = $this->load_data($s1grade, $s2grade); 183 184 // Redirect events to the sink, so we can recover them later. 185 $sink = $this->redirectEvents(); 186 187 $this->setUser($student1); 188 $result = user_external::view_grade_report($course->id); 189 $result = external_api::clean_returnvalue(user_external::view_grade_report_returns(), $result); 190 $events = $sink->get_events(); 191 $this->assertCount(1, $events); 192 $event = reset($events); 193 194 // Check the event details are correct. 195 $this->assertInstanceOf('\gradereport_user\event\grade_report_viewed', $event); 196 $this->assertEquals(\context_course::instance($course->id), $event->get_context()); 197 $this->assertEquals($USER->id, $event->get_data()['relateduserid']); 198 199 $this->setUser($teacher); 200 $result = user_external::view_grade_report($course->id, $student1->id); 201 $result = external_api::clean_returnvalue(user_external::view_grade_report_returns(), $result); 202 $events = $sink->get_events(); 203 $event = reset($events); 204 $sink->close(); 205 206 // Check the event details are correct. 207 $this->assertInstanceOf('\gradereport_user\event\grade_report_viewed', $event); 208 $this->assertEquals(\context_course::instance($course->id), $event->get_context()); 209 $this->assertEquals($student1->id, $event->get_data()['relateduserid']); 210 211 $this->setUser($student2); 212 try { 213 $studentgrade = user_external::view_grade_report($course->id, $student1->id); 214 $this->fail('Exception expected due to not permissions to view other user grades.'); 215 } catch (\moodle_exception $e) { 216 $this->assertEquals('nopermissiontoviewgrades', $e->errorcode); 217 } 218 } 219 220 /** 221 * Test get_grades_items function case teacher 222 */ 223 public function test_get_grade_items_teacher() { 224 225 $this->resetAfterTest(true); 226 227 $s1grade = 80; 228 $s2grade = 60; 229 230 list($course, $teacher, $student1, $student2, $assignment) = $this->load_data($s1grade, $s2grade); 231 232 // A teacher must see all student grades (in their group only). 233 $this->setUser($teacher); 234 235 grade_set_setting($course->id, 'report_user_showrank', 1); 236 grade_set_setting($course->id, 'report_user_showpercentage', 1); 237 grade_set_setting($course->id, 'report_user_showhiddenitems', 1); 238 grade_set_setting($course->id, 'report_user_showgrade', 1); 239 grade_set_setting($course->id, 'report_user_showfeedback', 1); 240 grade_set_setting($course->id, 'report_user_showweight', 1); 241 grade_set_setting($course->id, 'report_user_showcontributiontocoursetotal', 1); 242 grade_set_setting($course->id, 'report_user_showlettergrade', 1); 243 grade_set_setting($course->id, 'report_user_showaverage', 1); 244 245 $studentgrades = user_external::get_grade_items($course->id); 246 $studentgrades = external_api::clean_returnvalue(user_external::get_grade_items_returns(), $studentgrades); 247 // No warnings returned. 248 $this->assertCount(0, $studentgrades['warnings']); 249 250 // Check that only grades for the student in the teacher group are returned. 251 $this->assertCount(1, $studentgrades['usergrades']); 252 $this->assertCount(2, $studentgrades['usergrades'][0]['gradeitems']); 253 254 $this->assertEquals($course->id, $studentgrades['usergrades'][0]['courseid']); 255 $this->assertEquals($student1->id, $studentgrades['usergrades'][0]['userid']); 256 // Module grades. 257 $this->assertEquals($assignment->name, $studentgrades['usergrades'][0]['gradeitems'][0]['itemname']); 258 $this->assertEquals('mod', $studentgrades['usergrades'][0]['gradeitems'][0]['itemtype']); 259 $this->assertEquals('assign', $studentgrades['usergrades'][0]['gradeitems'][0]['itemmodule']); 260 $this->assertEquals($assignment->id, $studentgrades['usergrades'][0]['gradeitems'][0]['iteminstance']); 261 $this->assertFalse($studentgrades['usergrades'][0]['gradeitems'][0]['locked']); 262 $this->assertEquals($assignment->cmidnumber, $studentgrades['usergrades'][0]['gradeitems'][0]['cmid']); 263 $this->assertEquals(0, $studentgrades['usergrades'][0]['gradeitems'][0]['itemnumber']); 264 $this->assertEmpty($studentgrades['usergrades'][0]['gradeitems'][0]['outcomeid']); 265 $this->assertEmpty($studentgrades['usergrades'][0]['gradeitems'][0]['scaleid']); 266 $this->assertEquals(80, $studentgrades['usergrades'][0]['gradeitems'][0]['graderaw']); 267 $this->assertEquals('80.00', $studentgrades['usergrades'][0]['gradeitems'][0]['gradeformatted']); 268 $this->assertEquals(0, $studentgrades['usergrades'][0]['gradeitems'][0]['grademin']); 269 $this->assertEquals(100, $studentgrades['usergrades'][0]['gradeitems'][0]['grademax']); 270 $this->assertEquals('0–100', $studentgrades['usergrades'][0]['gradeitems'][0]['rangeformatted']); 271 $this->assertEquals('80.00 %', $studentgrades['usergrades'][0]['gradeitems'][0]['percentageformatted']); 272 $this->assertEmpty($studentgrades['usergrades'][0]['gradeitems'][0]['feedback']); 273 $this->assertFalse($studentgrades['usergrades'][0]['gradeitems'][0]['gradehiddenbydate']); 274 $this->assertFalse($studentgrades['usergrades'][0]['gradeitems'][0]['gradeneedsupdate']); 275 $this->assertFalse($studentgrades['usergrades'][0]['gradeitems'][0]['gradeishidden']); 276 $this->assertFalse($studentgrades['usergrades'][0]['gradeitems'][0]['gradeislocked']); 277 $this->assertFalse($studentgrades['usergrades'][0]['gradeitems'][0]['gradeisoverridden']); 278 $this->assertEquals('B-', $studentgrades['usergrades'][0]['gradeitems'][0]['lettergradeformatted']); 279 $this->assertEquals(1, $studentgrades['usergrades'][0]['gradeitems'][0]['rank']); 280 $this->assertEquals(2, $studentgrades['usergrades'][0]['gradeitems'][0]['numusers']); 281 $this->assertEquals(70, $studentgrades['usergrades'][0]['gradeitems'][0]['averageformatted']); 282 283 // Course grades. 284 $this->assertEquals('course', $studentgrades['usergrades'][0]['gradeitems'][1]['itemtype']); 285 $this->assertEquals(80, $studentgrades['usergrades'][0]['gradeitems'][1]['graderaw']); 286 $this->assertEquals('80.00', $studentgrades['usergrades'][0]['gradeitems'][1]['gradeformatted']); 287 $this->assertEquals(0, $studentgrades['usergrades'][0]['gradeitems'][1]['grademin']); 288 $this->assertEquals(100, $studentgrades['usergrades'][0]['gradeitems'][1]['grademax']); 289 $this->assertFalse($studentgrades['usergrades'][0]['gradeitems'][1]['locked']); 290 $this->assertEquals('0–100', $studentgrades['usergrades'][0]['gradeitems'][1]['rangeformatted']); 291 $this->assertEquals('80.00 %', $studentgrades['usergrades'][0]['gradeitems'][1]['percentageformatted']); 292 $this->assertEmpty($studentgrades['usergrades'][0]['gradeitems'][1]['feedback']); 293 $this->assertFalse($studentgrades['usergrades'][0]['gradeitems'][1]['gradehiddenbydate']); 294 $this->assertFalse($studentgrades['usergrades'][0]['gradeitems'][1]['gradeneedsupdate']); 295 $this->assertFalse($studentgrades['usergrades'][0]['gradeitems'][1]['gradeishidden']); 296 $this->assertFalse($studentgrades['usergrades'][0]['gradeitems'][1]['gradeislocked']); 297 $this->assertFalse($studentgrades['usergrades'][0]['gradeitems'][1]['gradeisoverridden']); 298 $this->assertEquals('B-', $studentgrades['usergrades'][0]['gradeitems'][1]['lettergradeformatted']); 299 $this->assertEquals(1, $studentgrades['usergrades'][0]['gradeitems'][1]['rank']); 300 $this->assertEquals(2, $studentgrades['usergrades'][0]['gradeitems'][1]['numusers']); 301 $this->assertEquals(70, $studentgrades['usergrades'][0]['gradeitems'][1]['averageformatted']); 302 303 // Now, override and lock a grade. 304 $gradegrade = \grade_grade::fetch(['itemid' => $studentgrades['usergrades'][0]['gradeitems'][0]['id'], 305 'userid' => $studentgrades['usergrades'][0]['userid']]); 306 $gradegrade->set_overridden(true); 307 $gradegrade->set_locked(1); 308 309 $studentgrades = user_external::get_grade_items($course->id); 310 $studentgrades = external_api::clean_returnvalue(user_external::get_grade_items_returns(), $studentgrades); 311 // No warnings returned. 312 $this->assertCount(0, $studentgrades['warnings']); 313 314 // Module grades. 315 $this->assertTrue($studentgrades['usergrades'][0]['gradeitems'][0]['gradeislocked']); 316 $this->assertTrue($studentgrades['usergrades'][0]['gradeitems'][0]['gradeisoverridden']); 317 } 318 319 /** 320 * Test get_grades_items function case student 321 */ 322 public function test_get_grade_items_student() { 323 324 $this->resetAfterTest(true); 325 326 $s1grade = 80; 327 $s2grade = 60; 328 329 list($course, $teacher, $student1, $student2, $assignment) = $this->load_data($s1grade, $s2grade); 330 331 grade_set_setting($course->id, 'report_user_showrank', 1); 332 grade_set_setting($course->id, 'report_user_showpercentage', 1); 333 grade_set_setting($course->id, 'report_user_showgrade', 1); 334 grade_set_setting($course->id, 'report_user_showfeedback', 1); 335 grade_set_setting($course->id, 'report_user_showweight', 1); 336 grade_set_setting($course->id, 'report_user_showcontributiontocoursetotal', 1); 337 grade_set_setting($course->id, 'report_user_showlettergrade', 1); 338 grade_set_setting($course->id, 'report_user_showaverage', 1); 339 340 $this->setUser($student1); 341 342 $studentgrades = user_external::get_grade_items($course->id, $student1->id); 343 $studentgrades = external_api::clean_returnvalue(user_external::get_grade_items_returns(), $studentgrades); 344 // No warnings returned. 345 $this->assertCount(0, $studentgrades['warnings']); 346 347 // Check that only grades for the student in the teacher group are returned. 348 $this->assertCount(1, $studentgrades['usergrades']); 349 $this->assertCount(2, $studentgrades['usergrades'][0]['gradeitems']); 350 351 $this->assertEquals($course->id, $studentgrades['usergrades'][0]['courseid']); 352 $this->assertEquals($student1->id, $studentgrades['usergrades'][0]['userid']); 353 $this->assertEquals($student1->idnumber, $studentgrades['usergrades'][0]['useridnumber']); 354 $this->assertEquals($assignment->name, $studentgrades['usergrades'][0]['gradeitems'][0]['itemname']); 355 $this->assertEquals('mod', $studentgrades['usergrades'][0]['gradeitems'][0]['itemtype']); 356 $this->assertEquals('assign', $studentgrades['usergrades'][0]['gradeitems'][0]['itemmodule']); 357 $this->assertEquals($assignment->id, $studentgrades['usergrades'][0]['gradeitems'][0]['iteminstance']); 358 $this->assertNull($studentgrades['usergrades'][0]['gradeitems'][0]['locked']); 359 $this->assertEquals($assignment->cmidnumber, $studentgrades['usergrades'][0]['gradeitems'][0]['cmid']); 360 $this->assertEquals(0, $studentgrades['usergrades'][0]['gradeitems'][0]['itemnumber']); 361 $this->assertEmpty($studentgrades['usergrades'][0]['gradeitems'][0]['outcomeid']); 362 $this->assertEmpty($studentgrades['usergrades'][0]['gradeitems'][0]['scaleid']); 363 $this->assertEquals(80, $studentgrades['usergrades'][0]['gradeitems'][0]['graderaw']); 364 $this->assertEquals('80.00', $studentgrades['usergrades'][0]['gradeitems'][0]['gradeformatted']); 365 $this->assertEquals(0, $studentgrades['usergrades'][0]['gradeitems'][0]['grademin']); 366 $this->assertEquals(100, $studentgrades['usergrades'][0]['gradeitems'][0]['grademax']); 367 $this->assertEquals('0–100', $studentgrades['usergrades'][0]['gradeitems'][0]['rangeformatted']); 368 $this->assertEquals('80.00 %', $studentgrades['usergrades'][0]['gradeitems'][0]['percentageformatted']); 369 $this->assertEmpty($studentgrades['usergrades'][0]['gradeitems'][0]['feedback']); 370 $this->assertFalse($studentgrades['usergrades'][0]['gradeitems'][0]['gradehiddenbydate']); 371 $this->assertFalse($studentgrades['usergrades'][0]['gradeitems'][0]['gradeneedsupdate']); 372 $this->assertFalse($studentgrades['usergrades'][0]['gradeitems'][0]['gradeishidden']); 373 $this->assertNull($studentgrades['usergrades'][0]['gradeitems'][0]['gradeislocked']); 374 $this->assertNull($studentgrades['usergrades'][0]['gradeitems'][0]['gradeisoverridden']); 375 $this->assertEquals('B-', $studentgrades['usergrades'][0]['gradeitems'][0]['lettergradeformatted']); 376 $this->assertEquals(1, $studentgrades['usergrades'][0]['gradeitems'][0]['rank']); 377 $this->assertEquals(2, $studentgrades['usergrades'][0]['gradeitems'][0]['numusers']); 378 $this->assertEquals(70, $studentgrades['usergrades'][0]['gradeitems'][0]['averageformatted']); 379 380 // Check that the idnumber for assignment grades is equal to the cmid. 381 $this->assertEquals((string) $studentgrades['usergrades'][0]['gradeitems'][0]['cmid'], 382 $studentgrades['usergrades'][0]['gradeitems'][0]['idnumber']); 383 384 // Hide one grade for the user. 385 $gradegrade = new \grade_grade([ 386 'userid' => $student1->id, 387 'itemid' => $studentgrades['usergrades'][0]['gradeitems'][0]['id'] 388 ], true); 389 $gradegrade->set_hidden(1); 390 $studentgrades = user_external::get_grade_items($course->id, $student1->id); 391 $studentgrades = external_api::clean_returnvalue(user_external::get_grade_items_returns(), $studentgrades); 392 393 // Check we get only the course final grade. 394 $this->assertCount(1, $studentgrades['usergrades']); 395 $this->assertCount(1, $studentgrades['usergrades'][0]['gradeitems']); 396 $this->assertEquals('course', $studentgrades['usergrades'][0]['gradeitems'][0]['itemtype']); 397 } 398 399 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body