Skip to content

Commit

Permalink
Merge branch 'MDL-49500-master' of git://github.com/jleyva/moodle
Browse files Browse the repository at this point in the history
Conflicts:
	lib/db/services.php
	version.php
  • Loading branch information
danpoltawski committed Apr 7, 2015
2 parents 522eff0 + 45e091a commit 464ba8e
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 10 deletions.
8 changes: 8 additions & 0 deletions grade/report/user/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,13 @@
'description' => 'Get the user/s report grades table for a course',
'type' => 'read',
'capabilities' => 'gradereport/user:view'
),
'gradereport_user_view_grade_report' => array(
'classname' => 'gradereport_user_external',
'methodname' => 'view_grade_report',
'classpath' => 'grade/report/user/externallib.php',
'description' => 'Trigger the report view event',
'type' => 'write',
'capabilities' => 'gradereport/user:view'
)
);
95 changes: 95 additions & 0 deletions grade/report/user/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,99 @@ public static function get_grades_table_returns() {
)
);
}

/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 2.9
*/
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 2.9
* @throws moodle_exception
*/
public static function view_grade_report($courseid, $userid = 0) {
global $CFG, $USER;
require_once($CFG->dirroot . "/grade/lib.php");
require_once($CFG->dirroot . "/grade/report/user/lib.php");

$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);
if ($user->deleted) {
throw new moodle_exception('userdeleted');
}
if (isguestuser($user)) {
// Can not view profile of guest - thre is nothing to see there.
throw new moodle_exception('invaliduserid');
}
}

$access = false;

if (has_capability('moodle/grade:viewall', $context)) {
// Can view all course grades (any user).
$access = true;
} else if ($userid == $USER->id and has_capability('moodle/grade:view', $context) and $course->showgrades) {
// View own grades.
$access = true;
}

if (!$access) {
throw new moodle_exception('nopermissiontoviewgrades', 'error');
}

// Create a report instance. We don't need the gpr second parameter.
$report = new grade_report_user($course->id, null, $context, $userid);
$report->viewed();

$result = array();
$result['status'] = true;
$result['warnings'] = $warnings;
return $result;
}

/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 2.9
*/
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()
)
);
}
}
10 changes: 2 additions & 8 deletions grade/report/user/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,7 @@
}
}

$event = \gradereport_user\event\grade_report_viewed::create(
array(
'context' => $context,
'courseid' => $courseid,
'relateduserid' => $userid,
)
);
$event->trigger();
// Trigger report viewed event.
$report->viewed();

echo $OUTPUT->footer();
16 changes: 16 additions & 0 deletions grade/report/user/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,22 @@ function calculate_averages() {
}
}
}

/**
* Trigger the grade_report_viewed event
*
* @since Moodle 2.9
*/
public function viewed() {
$event = \gradereport_user\event\grade_report_viewed::create(
array(
'context' => $this->context,
'courseid' => $this->courseid,
'relateduserid' => $this->user->id,
)
);
$event->trigger();
}
}

function grade_report_user_settings_definition(&$mform) {
Expand Down
47 changes: 47 additions & 0 deletions grade/report/user/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,51 @@ public function test_get_grades_table_permissions() {

}

/**
* Test view_grade_report function
*/
public function test_view_grade_report() {
global $USER;

$this->resetAfterTest(true);

$s1grade = 80;
$s2grade = 60;
list($course, $teacher, $student1, $student2) = $this->load_data($s1grade, $s2grade);

// Redirect events to the sink, so we can recover them later.
$sink = $this->redirectEvents();

$this->setUser($student1);
gradereport_user_external::view_grade_report($course->id);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);

// Check the event details are correct.
$this->assertInstanceOf('\gradereport_user\event\grade_report_viewed', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals($USER->id, $event->get_data()['relateduserid']);

$this->setUser($teacher);
gradereport_user_external::view_grade_report($course->id, $student1->id);
$events = $sink->get_events();
$event = reset($events);
$sink->close();

// Check the event details are correct.
$this->assertInstanceOf('\gradereport_user\event\grade_report_viewed', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals($student1->id, $event->get_data()['relateduserid']);

$this->setUser($student2);
try {
$studentgrade = gradereport_user_external::view_grade_report($course->id, $student1->id);
$this->fail('Exception expected due to not permissions to view other user grades.');
} catch (moodle_exception $e) {
$this->assertEquals('nopermissiontoviewgrades', $e->errorcode);
}

}

}
2 changes: 1 addition & 1 deletion grade/report/user/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2014111001; // The current plugin version (Date: YYYYMMDDXX)
$plugin->version = 2014111002; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2014110400; // Requires this Moodle version
$plugin->component = 'gradereport_user'; // Full name of the plugin (used for diagnostics)
1 change: 1 addition & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,7 @@
'core_notes_view_notes',
'mod_forum_view_forum_discussion',
'core_user_view_user_profile',
'gradereport_user_view_grade_report',
),
'enabled' => 0,
'restrictedusers' => 0,
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

defined('MOODLE_INTERNAL') || die();

$version = 2015040200.04; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2015040200.05; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.

Expand Down

0 comments on commit 464ba8e

Please sign in to comment.