Skip to content

Instantly share code, notes, and snippets.

@jleyva
Created April 15, 2014 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jleyva/10719337 to your computer and use it in GitHub Desktop.
Save jleyva/10719337 to your computer and use it in GitHub Desktop.
MDL-45062
<?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;
$resp = $curl->post($serverurl . $restformat, $params);
echo "Method response, it should be a numeric value: ";
print_r($resp);
echo "\n";
echo "Possible results: 0 => OK, 1 => Fail, 2 => Updated Multiple, 4 => Locked (Yes, 0 means OK and 3 is missing, see lib/grade/constans.php)";
echo "\n\n\n";
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";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment