Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An Exampel of use of Moodle Web Service function mod_assign_save_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.
//
// ref. https://gist.github.com/jleyva/9687810
// http://www.slideshare.net/hig3/moodle-bulkgradeimport in Japanese
session_start();
if(isset($_POST["endsession"]) && $_POST["endsession"]==1){
session_destroy();
session_start();
}
$teachertoken = 'hidden';
$domainname = 'http://mymoodleserver.org';
$restformat = 'json'; //Moodle 2.2 and later
$restformat = ($restformat == 'json')?'&moodlewsrestformat=' . $restformat:'';
define('MAXLENGTH',300);
if(isset($_FILES['csvfile'])){
$_SESSION["grade"]=array();
if( ($handle = fopen($_FILES['csvfile']['tmp_name'],"r")) !== FALSE){
while(($line = fgetcsv($handle,MAXLENGTH,",")) !== FALSE){
array_push($_SESSION["grade"],array('id'=>$line[0], 'grade'=>$line[1], 'feedback'=>$line[2]));
}
}
// print_r($_SESSION["grade"]);
}
require_once('./curl.php'); // https://github.com/moodlehq/sample-ws-clients/blob/master/PHP-REST/curl.php
$curl = new curl;
if(!isset($_POST["mode"])){
$mode=-1;
} else {
$mode=$_POST["mode"];
if($mode==1){
$courseid=$_POST["courseid"];
$functionname = 'mod_assign_get_assignments';
$params = array(
'courseids'=>array($courseid)//,
);
$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $studenttoken . '&wsfunction='.$functionname;
$resp = $curl->post($serverurl . $restformat, $params);
$resp = json_decode($resp);
$assignments=$resp->courses[0]->assignments;
}
if ($mode==2){
$courseid=$_POST["courseid"];
$assignmentid=$_POST["assignmentid"];
$grade2=array();
// In the documentation they say that core_user_get_users_by_field and core_user_get_users can search with idnumber, but they cannot.
$functionname = 'core_user_get_users';
foreach($_SESSION["grade"] as $g){
$params = array('criteria'=>array(
array('key'=>'firstname',
'value'=>$g["id"])
)
);
$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $studenttoken . '&wsfunction='.$functionname;
$resp = $curl->post($serverurl . $restformat, $params);
$resp = json_decode($resp);
if(count($resp->users)>0 ){
// $g['userid']=$resp->users[0]->id;
array_push($grade2,
array(
'userid' => $resp->users[0]->id,
'grade'=> $g["grade"],
'attemptnumber' => -1,
'addattempt' => 1,
'workflowstate' => 'released',
'plugindata'=>array('assignfeedbackcomments_editor'=>array('text'=>$g["feedback"], 'format'=>1),'files_filemanager'=>0)
));
}
}
$functionname = 'mod_assign_save_grades';
$params = array(
'assignmentid' => $assignmentid,
'applytoall' =>1,
'grades' => $grade2);
$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $teachertoken . '&wsfunction='.$functionname;
// print_r($params);
$resp=$curl->post($serverurl . $restformat, $params);
$resp=json_decode($resp);
}
}
/// REST CALL
header('Content-Type: text/html');
echo "<head><title>Grading Offline Assignment with Moodle Web Service</title></head>\n";
echo "<html>\n";
echo "<h1>Grading Offline Assignment with Moodle Web Service</h1>\n";
?>
<?php if($mode==-1):?>
<h2>Upload a CSV file</h2>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data"/>
<input type="file" name="csvfile" size="40"/>
<input type="submit" value="Upload" />
<input type="hidden" name="mode" value="0"/>
</form>
<?php endif;?>
<?php
if(isset($_SESSION["grade"])){
echo "<h2>Data</h2>\n";
echo "<table border>\n";
foreach($_SESSION["grade"] as $row){
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['grade']."</td>";
echo "<td>".$row['feedback']."</td>";
echo "</tr>";
}
echo "</table>";
}
?>
<?php if($mode==0):?>
<h2>Course</h2>
<FORM method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>" >
Course ID <input type="text" name="courseid"></input>
<input type="hidden" name="mode" value="1"></iunput>
<input type="submit"></input>
</FORM>
<?php endif;?>
<?php if($mode==1):?>
<h2>Course</h2>
courseid:<?php echo $courseid;?>
<h2>Assignment</h2>
<?php if(count($assignments)>0): ?>
<FORM method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>" >
<input type="hidden" name="courseid" value="<?php echo $courseid;?>"></input>
<input type="hidden" name="mode" value="2"></iunput>
<?php
foreach($assignments as $a){
echo '<input type="radio" name="assignmentid" value="'. $a->id .'"> '.$a->name.'</br/>';
}
?>
<input type="submit"/>
</FORM>
<?php else:?>
Course Not Found
<?php endif;?>
<?php endif;?>
<?php if($mode==2):?>
<ul>
<li>courseid:<?php echo $courseid;?></li>
<li>assignment:<?php echo $assignmentid;?></li>
</ul>
<h3>Done</h3>
<h4>Parameters</h4>
<pre>
<?php print_r($params);?>
</pre>
<h4>Response</h4>
<pre>
<?php print_r($resp);?>
</pre>
<?php endif;?>
<?php
if(FALSE){
echo "<pre>";
print_r($grade2);
print_r($params);
print_r($resp);
echo "</pre>";
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>" />
<input type="submit" value="End Session"/>
<input type="hidden" name="endsession" value="1"/>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.