diff --git a/grade/report/overview/classes/external.php b/grade/report/overview/classes/external.php new file mode 100644 index 00000000000..abdb080087e --- /dev/null +++ b/grade/report/overview/classes/external.php @@ -0,0 +1,224 @@ +. + +/** + * External grade report overview API + * + * @package gradereport_overview + * @copyright 2016 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +require_once($CFG->libdir . '/externallib.php'); +require_once($CFG->libdir . '/gradelib.php'); +require_once($CFG->dirroot . '/grade/lib.php'); +require_once($CFG->dirroot . '/grade/report/overview/lib.php'); + +/** + * External grade overview report API implementation + * + * @package gradereport_overview + * @copyright 2016 Juan Leyva + * @category external + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class gradereport_overview_external extends external_api { + + /** + * Describes the parameters for get_course_grades. + * + * @return external_external_function_parameters + * @since Moodle 3.2 + */ + public static function get_course_grades_parameters() { + return new external_function_parameters ( + array( + 'userid' => new external_value(PARAM_INT, 'Get grades for this user (optional, default current)', VALUE_DEFAULT, 0) + ) + ); + } + + /** + * Get the given user courses final grades + * + * @param int $userid get grades for this user (optional, default current) + * + * @return array the grades tables + * @since Moodle 3.2 + */ + public static function get_course_grades($userid = 0) { + global $USER; + + $warnings = array(); + + // Validate the parameter. + $params = self::validate_parameters(self::get_course_grades_parameters(), + array( + 'userid' => $userid + ) + ); + + $userid = $params['userid']; + if (empty($userid)) { + $userid = $USER->id; + } + + $systemcontext = context_system::instance(); + self::validate_context($systemcontext); + + if ($USER->id != $userid) { + // We must check if the current user can view other users grades. + $user = core_user::get_user($userid, '*', MUST_EXIST); + core_user::require_active_user($user); + require_capability('moodle/grade:viewall', $systemcontext); + } + + // We need the site course, and course context. + $course = get_course(SITEID); + $context = context_course::instance($course->id); + + // Force a regrade if required. + grade_regrade_final_grades_if_required($course); + // Get the course final grades now. + $gpr = new grade_plugin_return(array('type' => 'report', 'plugin' => 'overview', 'courseid' => $course->id, + 'userid' => $userid)); + $report = new grade_report_overview($userid, $gpr, $context); + $coursesgrades = $report->setup_courses_data(true); + + $grades = array(); + foreach ($coursesgrades as $coursegrade) { + $gradeinfo = array( + 'courseid' => $coursegrade['course']->id, + 'grade' => grade_format_gradevalue($coursegrade['finalgrade'], $coursegrade['courseitem'], true), + 'rawgrade' => $coursegrade['finalgrade'], + ); + if (isset($coursegrade['rank'])) { + $gradeinfo['rank'] = $coursegrade['rank']; + } + $grades[] = $gradeinfo; + } + + $result = array(); + $result['grades'] = $grades; + $result['warnings'] = $warnings; + return $result; + } + + /** + * Describes the get_course_grades return value. + * + * @return external_single_structure + * @since Moodle 3.2 + */ + public static function get_course_grades_returns() { + return new external_single_structure( + array( + 'grades' => new external_multiple_structure( + new external_single_structure( + array( + 'courseid' => new external_value(PARAM_INT, 'Course id'), + 'grade' => new external_value(PARAM_RAW, 'Grade formatted'), + 'rawgrade' => new external_value(PARAM_RAW, 'Raw grade, not formatted'), + 'rank' => new external_value(PARAM_INT, 'Your rank in the course', VALUE_OPTIONAL), + ) + ) + ), + 'warnings' => new external_warnings() + ) + ); + } + + /** + * Returns description of method parameters + * + * @return external_function_parameters + * @since Moodle 3.2 + */ + public static function view_grade_report_parameters() { + return new external_function_parameters( + array( + 'courseid' => new external_value(PARAM_INT, 'id of the course'), + 'userid' => new external_value(PARAM_INT, 'id of the user, 0 means current user', VALUE_DEFAULT, 0) + ) + ); + } + + /** + * Trigger the user report events, do the same that the web interface view of the report + * + * @param int $courseid id of course + * @param int $userid id of the user the report belongs to + * @return array of warnings and status result + * @since Moodle 3.2 + * @throws moodle_exception + */ + public static function view_grade_report($courseid, $userid = 0) { + global $USER; + + $params = self::validate_parameters(self::view_grade_report_parameters(), + array( + 'courseid' => $courseid, + 'userid' => $userid + ) + ); + + $warnings = array(); + $course = get_course($params['courseid']); + + $context = context_course::instance($course->id); + self::validate_context($context); + + $userid = $params['userid']; + if (empty($userid)) { + $userid = $USER->id; + } else { + $user = core_user::get_user($userid, '*', MUST_EXIST); + core_user::require_active_user($user); + } + $systemcontext = context_system::instance(); + $personalcontext = context_user::instance($userid); + + $access = grade_report_overview::check_access($systemcontext, $context, $personalcontext, $course, $userid); + + if (!$access) { + throw new moodle_exception('nopermissiontoviewgrades', 'error'); + } + + grade_report_overview::viewed($context, $course->id, $userid); + + $result = array(); + $result['status'] = true; + $result['warnings'] = $warnings; + return $result; + } + + /** + * Returns description of method result value + * + * @return external_description + * @since Moodle 3.2 + */ + public static function view_grade_report_returns() { + return new external_single_structure( + array( + 'status' => new external_value(PARAM_BOOL, 'status: true if success'), + 'warnings' => new external_warnings() + ) + ); + } +} diff --git a/grade/report/overview/db/services.php b/grade/report/overview/db/services.php new file mode 100644 index 00000000000..56f4ea8d5b3 --- /dev/null +++ b/grade/report/overview/db/services.php @@ -0,0 +1,44 @@ +. + +/** + * Overview grade report external functions and service definitions. + * + * @package gradereport_overview + * @copyright 2016 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$functions = array( + + 'gradereport_overview_get_course_grades' => array( + 'classname' => 'gradereport_overview_external', + 'methodname' => 'get_course_grades', + 'description' => 'Get the given user courses final grades', + 'type' => 'read', + 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE), + ), + 'gradereport_overview_view_grade_report' => array( + 'classname' => 'gradereport_overview_external', + 'methodname' => 'view_grade_report', + 'description' => 'Trigger the report view event', + 'type' => 'write', + 'capabilities' => 'gradereport/overview:view', + 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE), + ) +); diff --git a/grade/report/overview/index.php b/grade/report/overview/index.php index 14eef55bc6b..41251570eba 100644 --- a/grade/report/overview/index.php +++ b/grade/report/overview/index.php @@ -71,27 +71,7 @@ if ($userid == $USER->id) { $PAGE->navigation->extend_for_user($user); } -$access = false; -if (has_capability('moodle/grade:viewall', $systemcontext)) { - // Ok - can view all course grades. - $access = true; - -} else if (has_capability('moodle/grade:viewall', $context)) { - // Ok - can view any grades in context. - $access = true; - -} else if ($userid == $USER->id and ((has_capability('moodle/grade:view', $context) and $course->showgrades) - || $courseid == SITEID)) { - // Ok - can view own course grades. - $access = true; - -} else if (has_capability('moodle/grade:viewall', $personalcontext) and $course->showgrades) { - // Ok - can view grades of this user - parent most probably. - $access = true; -} else if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext) and $course->showgrades) { - // Ok - can view grades of this user - parent most probably. - $access = true; -} +$access = grade_report_overview::check_access($systemcontext, $context, $personalcontext, $course, $userid); if (!$access) { // no access to grades! @@ -214,15 +194,6 @@ if (has_capability('moodle/grade:viewall', $context) && $courseid != SITEID) { } } -$event = \gradereport_overview\event\grade_report_viewed::create( - array( - 'context' => $context, - 'courseid' => $courseid, - 'relateduserid' => $userid, - ) -); -$event->trigger(); +grade_report_overview::viewed($context, $courseid, $userid); echo $OUTPUT->footer(); - - diff --git a/grade/report/overview/lib.php b/grade/report/overview/lib.php index 923fc2f2607..d8bf895cb04 100644 --- a/grade/report/overview/lib.php +++ b/grade/report/overview/lib.php @@ -164,6 +164,97 @@ class grade_report_overview extends grade_report { $this->table->setup(); } + /** + * Set up the courses grades data for the report. + * + * @param bool $studentcoursesonly Only show courses that the user is a student of. + * @return array of course grades information + */ + public function setup_courses_data($studentcoursesonly) { + global $USER, $DB; + + $coursesdata = array(); + $numusers = $this->get_numusers(false); + + foreach ($this->courses as $course) { + if (!$course->showgrades) { + continue; + } + + // If we are only showing student courses and this course isn't part of the group, then move on. + if ($studentcoursesonly && !isset($this->studentcourseids[$course->id])) { + continue; + } + + $coursecontext = context_course::instance($course->id); + + if (!$course->visible && !has_capability('moodle/course:viewhiddencourses', $coursecontext)) { + // The course is hidden and the user isn't allowed to see it. + continue; + } + + if (!has_capability('moodle/user:viewuseractivitiesreport', context_user::instance($this->user->id)) && + ((!has_capability('moodle/grade:view', $coursecontext) || $this->user->id != $USER->id) && + !has_capability('moodle/grade:viewall', $coursecontext))) { + continue; + } + + $coursesdata[$course->id]['course'] = $course; + $coursesdata[$course->id]['context'] = $coursecontext; + + $canviewhidden = has_capability('moodle/grade:viewhidden', $coursecontext); + + // Get course grade_item. + $courseitem = grade_item::fetch_course_item($course->id); + + // Get the stored grade. + $coursegrade = new grade_grade(array('itemid' => $courseitem->id, 'userid' => $this->user->id)); + $coursegrade->grade_item =& $courseitem; + $finalgrade = $coursegrade->finalgrade; + + if (!$canviewhidden and !is_null($finalgrade)) { + if ($coursegrade->is_hidden()) { + $finalgrade = null; + } else { + $adjustedgrade = $this->blank_hidden_total_and_adjust_bounds($course->id, + $courseitem, + $finalgrade); + + // We temporarily adjust the view of this grade item - because the min and + // max are affected by the hidden values in the aggregation. + $finalgrade = $adjustedgrade['grade']; + $courseitem->grademax = $adjustedgrade['grademax']; + $courseitem->grademin = $adjustedgrade['grademin']; + } + } else { + // We must use the specific max/min because it can be different for + // each grade_grade when items are excluded from sum of grades. + if (!is_null($finalgrade)) { + $courseitem->grademin = $coursegrade->get_grade_min(); + $courseitem->grademax = $coursegrade->get_grade_max(); + } + } + + $coursesdata[$course->id]['finalgrade'] = $finalgrade; + $coursesdata[$course->id]['courseitem'] = $courseitem; + + if ($this->showrank['any'] && $this->showrank[$course->id] && !is_null($finalgrade)) { + // Find the number of users with a higher grade. + // Please note this can not work if hidden grades involved :-( to be fixed in 2.0. + $params = array($finalgrade, $courseitem->id); + $sql = "SELECT COUNT(DISTINCT(userid)) + FROM {grade_grades} + WHERE finalgrade IS NOT NULL AND finalgrade > ? + AND itemid = ?"; + $rank = $DB->count_records_sql($sql, $params) + 1; + + $coursesdata[$course->id]['rank'] = $rank; + $coursesdata[$course->id]['numusers'] = $numusers; + } + } + return $coursesdata; + } + /** * Fill the table for displaying. * @@ -179,30 +270,14 @@ class grade_report_overview extends grade_report { // Only show user's courses instead of all courses. if ($this->courses) { - $numusers = $this->get_numusers(false); + $coursesdata = $this->setup_courses_data($studentcoursesonly); - foreach ($this->courses as $course) { - if (!$course->showgrades) { - continue; - } + foreach ($coursesdata as $coursedata) { - // If we are only showing student courses and this course isn't part of the group, then move on. - if ($studentcoursesonly && !isset($this->studentcourseids[$course->id])) { - continue; - } - - $coursecontext = context_course::instance($course->id); - - if (!$course->visible && !has_capability('moodle/course:viewhiddencourses', $coursecontext)) { - // The course is hidden and the user isn't allowed to see it - continue; - } - - if (!has_capability('moodle/user:viewuseractivitiesreport', context_user::instance($this->user->id)) && - ((!has_capability('moodle/grade:view', $coursecontext) || $this->user->id != $USER->id) && - !has_capability('moodle/grade:viewall', $coursecontext))) { - continue; - } + $course = $coursedata['course']; + $coursecontext = $coursedata['context']; + $finalgrade = $coursedata['finalgrade']; + $courseitem = $coursedata['courseitem']; $coursename = format_string(get_course_display_name_for_list($course), true, array('context' => $coursecontext)); // Link to the activity report version of the user grade report. @@ -213,66 +288,25 @@ class grade_report_overview extends grade_report { $courselink = html_writer::link(new moodle_url('/grade/report/user/index.php', array('id' => $course->id, 'userid' => $this->user->id)), $coursename); } - $canviewhidden = has_capability('moodle/grade:viewhidden', $coursecontext); - // Get course grade_item - $course_item = grade_item::fetch_course_item($course->id); + $data = array($courselink, grade_format_gradevalue($finalgrade, $courseitem, true)); - // Get the stored grade - $course_grade = new grade_grade(array('itemid'=>$course_item->id, 'userid'=>$this->user->id)); - $course_grade->grade_item =& $course_item; - $finalgrade = $course_grade->finalgrade; - - if (!$canviewhidden and !is_null($finalgrade)) { - if ($course_grade->is_hidden()) { - $finalgrade = null; + if ($this->showrank['any']) { + if ($this->showrank[$course->id] && !is_null($finalgrade)) { + $rank = $coursedata['rank']; + $numusers = $coursedata['numusers']; + $data[] = "$rank/$numusers"; } else { - $adjustedgrade = $this->blank_hidden_total_and_adjust_bounds($course->id, - $course_item, - $finalgrade); - - // We temporarily adjust the view of this grade item - because the min and - // max are affected by the hidden values in the aggregation. - $finalgrade = $adjustedgrade['grade']; - $course_item->grademax = $adjustedgrade['grademax']; - $course_item->grademin = $adjustedgrade['grademin']; + // No grade, no rank. + // Or this course wants rank hidden. + $data[] = '-'; } - } else { - // We must use the specific max/min because it can be different for - // each grade_grade when items are excluded from sum of grades. - if (!is_null($finalgrade)) { - $course_item->grademin = $course_grade->get_grade_min(); - $course_item->grademax = $course_grade->get_grade_max(); - } - } - - $data = array($courselink, grade_format_gradevalue($finalgrade, $course_item, true)); - - if (!$this->showrank['any']) { - //nothing to do - - } else if ($this->showrank[$course->id] && !is_null($finalgrade)) { - /// find the number of users with a higher grade - /// please note this can not work if hidden grades involved :-( to be fixed in 2.0 - $params = array($finalgrade, $course_item->id); - $sql = "SELECT COUNT(DISTINCT(userid)) - FROM {grade_grades} - WHERE finalgrade IS NOT NULL AND finalgrade > ? - AND itemid = ?"; - $rank = $DB->count_records_sql($sql, $params) + 1; - - $data[] = "$rank/$numusers"; - - } else { - // No grade, no rank. - // Or this course wants rank hidden. - $data[] = '-'; } $this->table->add_data($data); } - return true; + return true; } else { echo $OUTPUT->notification(get_string('notenrolled', 'grades'), 'notifymessage'); return false; @@ -325,6 +359,63 @@ class grade_report_overview extends grade_report { public static function supports_mygrades() { return true; } + + /** + * Check if the user can access the report. + * + * @param stdClass $systemcontext system context + * @param stdClass $context course context + * @param stdClass $personalcontext personal context + * @param stdClass $course course object + * @param int $userid userid + * @return bool true if the user can access the report + * @since Moodle 3.2 + */ + public static function check_access($systemcontext, $context, $personalcontext, $course, $userid) { + global $USER; + + $access = false; + if (has_capability('moodle/grade:viewall', $systemcontext)) { + // Ok - can view all course grades. + $access = true; + + } else if (has_capability('moodle/grade:viewall', $context)) { + // Ok - can view any grades in context. + $access = true; + + } else if ($userid == $USER->id and ((has_capability('moodle/grade:view', $context) and $course->showgrades) + || $course->id == SITEID)) { + // Ok - can view own course grades. + $access = true; + + } else if (has_capability('moodle/grade:viewall', $personalcontext) and $course->showgrades) { + // Ok - can view grades of this user - parent most probably. + $access = true; + } else if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext) and $course->showgrades) { + // Ok - can view grades of this user - parent most probably. + $access = true; + } + return $access; + } + + /** + * Trigger the grade_report_viewed event + * + * @param stdClass $context course context + * @param int $courseid course id + * @param int $userid user id + * @since Moodle 3.2 + */ + public static function viewed($context, $courseid, $userid) { + $event = \gradereport_overview\event\grade_report_viewed::create( + array( + 'context' => $context, + 'courseid' => $courseid, + 'relateduserid' => $userid, + ) + ); + $event->trigger(); + } } function grade_report_overview_settings_definition(&$mform) { diff --git a/grade/report/overview/tests/externallib_test.php b/grade/report/overview/tests/externallib_test.php new file mode 100644 index 00000000000..1301c0d84dc --- /dev/null +++ b/grade/report/overview/tests/externallib_test.php @@ -0,0 +1,231 @@ +. + +/** + * Overview grade report functions unit tests + * + * @package gradereport_overview + * @category external + * @copyright 2015 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->dirroot . '/webservice/tests/helpers.php'); + +/** + * Overview grade report functions unit tests + * + * @package gradereport_overview + * @category external + * @copyright 2015 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class gradereport_overview_externallib_testcase extends externallib_advanced_testcase { + + /** + * Set up for every test + */ + public function setUp() { + global $DB; + $this->resetAfterTest(true); + + $s1grade1 = 80; + $s1grade2 = 40; + $s2grade = 60; + + $this->course1 = $this->getDataGenerator()->create_course(); + $this->course2 = $this->getDataGenerator()->create_course(); + + $studentrole = $DB->get_record('role', array('shortname' => 'student')); + $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); + $this->student1 = $this->getDataGenerator()->create_user(); + $this->teacher = $this->getDataGenerator()->create_user(); + $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course1->id, $teacherrole->id); + $this->getDataGenerator()->enrol_user($this->student1->id, $this->course1->id, $studentrole->id); + $this->getDataGenerator()->enrol_user($this->student1->id, $this->course2->id, $studentrole->id); + + $this->student2 = $this->getDataGenerator()->create_user(); + $this->getDataGenerator()->enrol_user($this->student2->id, $this->course1->id, $studentrole->id); + $this->getDataGenerator()->enrol_user($this->student2->id, $this->course2->id, $studentrole->id); + + $assignment1 = $this->getDataGenerator()->create_module('assign', array('name' => "Test assign", 'course' => $this->course1->id)); + $assignment2 = $this->getDataGenerator()->create_module('assign', array('name' => "Test assign", 'course' => $this->course2->id)); + $modcontext1 = get_coursemodule_from_instance('assign', $assignment1->id, $this->course1->id); + $modcontext2 = get_coursemodule_from_instance('assign', $assignment2->id, $this->course2->id); + $assignment1->cmidnumber = $modcontext1->id; + $assignment2->cmidnumber = $modcontext2->id; + + $this->student1grade1 = array('userid' => $this->student1->id, 'rawgrade' => $s1grade1); + $this->student1grade2 = array('userid' => $this->student1->id, 'rawgrade' => $s1grade2); + $this->student2grade = array('userid' => $this->student2->id, 'rawgrade' => $s2grade); + $studentgrades = array($this->student1->id => $this->student1grade1, $this->student2->id => $this->student2grade); + assign_grade_item_update($assignment1, $studentgrades); + $studentgrades = array($this->student1->id => $this->student1grade2); + assign_grade_item_update($assignment2, $studentgrades); + + grade_get_setting($this->course1->id, 'report_overview_showrank', 1); + } + + /** + * Test get_course_grades function case student + */ + public function test_get_course_grades_student() { + + // A user can see his own grades in both courses. + $this->setUser($this->student1); + $studentgrades = gradereport_overview_external::get_course_grades(); + $studentgrades = external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades); + + $this->assertCount(0, $studentgrades['warnings']); + $this->assertCount(2, $studentgrades['grades']); + foreach ($studentgrades['grades'] as $grade) { + if ($grade['courseid'] == $this->course1->id) { + $this->assertEquals(80.00, $grade['grade']); + $this->assertEquals(80.0000, $grade['rawgrade']); + $this->assertEquals(1, $grade['rank']); + } else { + $this->assertEquals(40.00, $grade['grade']); + $this->assertEquals(40.0000, $grade['rawgrade']); + $this->assertArrayNotHasKey('rank', $grade); + } + } + + // Second student, no grade in one course. + $this->setUser($this->student2); + $studentgrades = gradereport_overview_external::get_course_grades(); + $studentgrades = external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades); + + $this->assertCount(0, $studentgrades['warnings']); + $this->assertCount(2, $studentgrades['grades']); + foreach ($studentgrades['grades'] as $grade) { + if ($grade['courseid'] == $this->course1->id) { + $this->assertEquals(60.00, $grade['grade']); + $this->assertEquals(60.0000, $grade['rawgrade']); + $this->assertEquals(2, $grade['rank']); + } else { + $this->assertEquals('-', $grade['grade']); + $this->assertEmpty($grade['rawgrade']); + $this->assertArrayNotHasKey('rank', $grade); + } + } + } + + /** + * Test get_course_grades function case admin + */ + public function test_get_course_grades_admin() { + + // A admin must see all student grades. + $this->setAdminUser(); + + $studentgrades = gradereport_overview_external::get_course_grades($this->student1->id); + $studentgrades = external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades); + $this->assertCount(0, $studentgrades['warnings']); + $this->assertCount(2, $studentgrades['grades']); + foreach ($studentgrades['grades'] as $grade) { + if ($grade['courseid'] == $this->course1->id) { + $this->assertEquals(80.00, $grade['grade']); + $this->assertEquals(80.0000, $grade['rawgrade']); + } else { + $this->assertEquals(40.00, $grade['grade']); + $this->assertEquals(40.0000, $grade['rawgrade']); + } + } + + $studentgrades = gradereport_overview_external::get_course_grades($this->student2->id); + $studentgrades = external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades); + $this->assertCount(0, $studentgrades['warnings']); + $this->assertCount(2, $studentgrades['grades']); + + // Admins don't see grades. + $studentgrades = gradereport_overview_external::get_course_grades(); + $studentgrades = external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades); + $this->assertCount(0, $studentgrades['warnings']); + $this->assertCount(0, $studentgrades['grades']); + } + + /** + * Test get_course_grades function case teacher + */ + public function test_get_course_grades_teacher() { + // Teachers don't see grades. + $this->setUser($this->teacher); + + $studentgrades = gradereport_overview_external::get_course_grades(); + $studentgrades = external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades); + $this->assertCount(0, $studentgrades['warnings']); + $this->assertCount(0, $studentgrades['grades']); + } + + /** + * Test get_course_grades function case incorrect permissions + */ + public function test_get_course_grades_permissions() { + // Student can't see other student grades. + $this->setUser($this->student2); + + $this->expectException('required_capability_exception'); + $studentgrade = gradereport_overview_external::get_course_grades($this->student1->id); + } + + /** + * Test view_grade_report function + */ + public function test_view_grade_report() { + global $USER; + + // Redirect events to the sink, so we can recover them later. + $sink = $this->redirectEvents(); + + $this->setUser($this->student1); + $result = gradereport_overview_external::view_grade_report($this->course1->id); + $result = external_api::clean_returnvalue(gradereport_overview_external::view_grade_report_returns(), $result); + $events = $sink->get_events(); + $this->assertCount(1, $events); + $event = reset($events); + + // Check the event details are correct. + $this->assertInstanceOf('\gradereport_overview\event\grade_report_viewed', $event); + $this->assertEquals(context_course::instance($this->course1->id), $event->get_context()); + $this->assertEquals($USER->id, $event->get_data()['relateduserid']); + + $this->setUser($this->teacher); + $result = gradereport_overview_external::view_grade_report($this->course1->id, $this->student1->id); + $result = external_api::clean_returnvalue(gradereport_overview_external::view_grade_report_returns(), $result); + $events = $sink->get_events(); + $event = reset($events); + $sink->close(); + + // Check the event details are correct. + $this->assertInstanceOf('\gradereport_overview\event\grade_report_viewed', $event); + $this->assertEquals(context_course::instance($this->course1->id), $event->get_context()); + $this->assertEquals($this->student1->id, $event->get_data()['relateduserid']); + } + + /** + * Test view_grade_report_permissions function + */ + public function test_view_grade_report_permissions() { + $this->setUser($this->student2); + + $this->expectException('moodle_exception'); + $studentgrade = gradereport_overview_external::view_grade_report($this->course1->id, $this->student1->id); + } +} diff --git a/grade/report/overview/version.php b/grade/report/overview/version.php index 686718ebc77..aeb122f8d18 100644 --- a/grade/report/overview/version.php +++ b/grade/report/overview/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2016052300; // The current plugin version (Date: YYYYMMDDXX) +$plugin->version = 2016052301; // The current plugin version (Date: YYYYMMDDXX) $plugin->requires = 2016051900; // Requires this Moodle version $plugin->component = 'gradereport_overview'; // Full name of the plugin (used for diagnostics)