Skip to content

Instantly share code, notes, and snippets.

@jleyva
Last active May 7, 2021
Embed
What would you like to do?
MDL-30085 core get _grades
<?php
// This file is NOT a part of Moodle - http://moodle.org/
//
// This client for Moodle 2 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
/// SETUP - NEED TO BE CHANGED
//
// Student token
$studenttoken = '200283ba9395030324317216862ae274';
// Teacher Token.
$teachertoken = '298dee2e40eeac025a642e6231599917';
$domainname = 'http://localhost/moodlebugs';
//$domainname = 'http://localhost/m/stable_24';
$courseid = 2;
// Student id.
$userid = 3;
// the cmid
$activityid = 56;
// REST RETURNED VALUES FORMAT
$restformat = 'json'; //Also possible in Moodle 2.2 and later: 'json'
//Setting it to 'json' will fail all calls on earlier Moodle version
$restformat = ($restformat == 'json')?'&moodlewsrestformat=' . $restformat:'';
/// REST CALL
header('Content-Type: text/plain');
require_once('./curl.php');
$curl = new curl;
$functionname = 'core_grades_get_grades';
$params = array(
'courseid' => $courseid,
'component' => 'mod_assign',
'activityid' => $activityid,
'userids' => array($userid)
);
$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $studenttoken . '&wsfunction='.$functionname;
$resp = $curl->post($serverurl . $restformat, $params);
$resp = json_decode($resp);
echo "Current grade is: " . $resp->items[0]->grades[0]->grade;
echo "\n\n\n\n";
$newgrade = rand(0, 100);
echo "Updating current grade to $newgrade (random(0, 100))";
echo "\n\n\n\n";
$functionname = 'core_grades_update_grades';
$params = array(
'source' => 'mod/assign',
'courseid' => $courseid,
'component' => 'mod_assign',
'activityid' => $activityid,
'itemnumber' => 0,
'grades' => array(
array('studentid' => $userid,
'grade' => $newgrade)
)
);
$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $teachertoken . '&wsfunction='.$functionname;
$curl->post($serverurl . $restformat, $params);
echo "Grade updated";
echo "\n\n\n\n";
echo "Retrieving grade again to check if matches the new grade ($newgrade)";
echo "\n\n\n\n";
$functionname = 'core_grades_get_grades';
$params = array(
'courseid' => $courseid,
'component' => 'mod_assign',
'activityid' => $activityid,
'userids' => array($userid)
);
$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $studenttoken . '&wsfunction='.$functionname;
$resp = $curl->post($serverurl . $restformat, $params);
$resp = json_decode($resp);
$grade = $resp->items[0]->grades[0]->grade;
echo "Grade retrieved is: " . $grade;
echo "\n\n\n\n";
if ($grade == $newgrade) {
echo "OK it worked!! Thanks for testing";
} else {
echo "Wow, something failed!! Thanks for testing";
}
@juliandavidmr

This comment has been minimized.

Copy link

@juliandavidmr juliandavidmr commented Mar 14, 2018

For moodle 2.9:

$params = [
	'wsfunction' => 'core_grades_get_grades',
	'moodlewsrestformat' => $format,
	'userids[0]' => 1345, # ID user
	'courseid' => 313 # ID Course,
	# 'component' => 'mod_assign'
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment