Skip to content

Commit

Permalink
strong improvments of get_grades operations. A course idnumber not an…
Browse files Browse the repository at this point in the history
…ymore required II
  • Loading branch information
patrickpollet committed Apr 13, 2011
1 parent 6055e86 commit 4c51009
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion wspp/lang/en_utf8/local_wspp.php
Expand Up @@ -66,7 +66,7 @@
$string['ws_operationnotallowed']='you don\'t have the permissions to do this.';
$string['ws_nocourseforuser']='no courses found for $a';
$string['ws_coursewithoutidnumber']='the course $a does not have idnumber (report to your administrator)';
$string['ws_nocoursewithidnumberforuser']='no course with that idnumber exists for user $a (report to your administrator)';
$string['ws_nocoursewithidnumberforuser']='no course with non empty idnumber exists for user $a (report to your administrator)';
$string['ws_roleidnotfound']='role id $a not found.';
$string['ws_nothingtodo']='nothing to do.';
$string['ws_notsupportedgradebook']='the notebook prior to Moodle 1.8 is not supported.';
Expand Down
2 changes: 1 addition & 1 deletion wspp/lang/fr_utf8/local_wspp.php
Expand Up @@ -63,7 +63,7 @@


$string['ws_noseegrades']='pas autorisé à voir les notes de $a';
$string['ws_nogradesfound']='pas de notes pour $a->user dans $a->course';
$string['ws_nogradesfound']='aucune note pour $a->user dans $a->course';
$string['ws_operationnotallowed']='vous n\'avez pas les permissions de faire cela.';
$string['ws_nocourseforuser']='aucun cours trouvé pour $a';
$string['ws_coursewithoutidnumber']='le cours $a n\'a pas de idnumber (signalez le à votre administrateur)';
Expand Down
2 changes: 1 addition & 1 deletion wspp/local/wspp/lang/en/local_wspp.php
Expand Up @@ -67,7 +67,7 @@
$string['ws_operationnotallowed']='you don\'t have the permissions to do this.';
$string['ws_nocourseforuser']='no courses found for {$a}';
$string['ws_coursewithoutidnumber']='the course {$a} does not have idnumber (report to your administrator)';
$string['ws_nocoursewithidnumberforuser']='no course with that idnumber exists for user {$a} (report to your administrator)';
$string['ws_nocoursewithidnumberforuser']='no course with non empty idnumber exists for user {$a} (report to your administrator)';
$string['ws_roleidnotfound']='role id {$a} not found.';
$string['ws_nothingtodo']='nothing to do.';
$string['ws_notsupportedgradebook']='the notebook prior to Moodle 1.8 is not supported.';
Expand Down
2 changes: 1 addition & 1 deletion wspp/mdl_soapserver.class.php
Expand Up @@ -500,7 +500,7 @@ public function get_grades($client, $sesskey, $userid, $useridfield = 'idnumber'
*/
public function get_user_grades($client, $sesskey, $userid, $idfield = "idnumber") {

return $this->send($this->to_soap_array(parent :: get_user_grades($client, $sesskey, $userid, $idfield), 'grades', 'gradeRecord', "no grades found for user $userid"));
return $this->send($this->to_soap_array(parent :: get_user_grades($client, $sesskey, $userid, $idfield), 'grades', 'gradeRecord', get_string('nogradesfor', 'local_wspp', $userid)));

}

Expand Down
37 changes: 28 additions & 9 deletions wspp/server.class.php
Expand Up @@ -669,20 +669,26 @@ public function get_instances_bytype($client, $sesskey, $courseids, $idfield = '
*
*/
function get_grades($client, $sesskey, $userid, $useridfield = 'idnumber', $courseids, $courseidfield = 'idnumber') {
global $CFG;
global $CFG,$USER;

if (!$this->validate_client($client, $sesskey, __FUNCTION__)) {
return $this->error(get_string('ws_invalidclient', 'local_wspp'));
}
if (empty ($courseids))
return server :: get_user_grades($client, $sesskey, $userid, $useridfield);

if (!$this->using19)
return $this->error(get_string(' ws_notsupportedgradebook', 'local_wspp'));

if (empty ($courseids))
return server :: get_user_grades($client, $sesskey, $userid, $useridfield);

require_once ($CFG->dirroot . '/grade/lib.php');
require_once ($CFG->dirroot . '/grade/querylib.php');

if (empty($userid)) { // rev 1.8.3 default to logged in user !
$userid=$USER->id;
$useridfield='id';
}

if (!$user = ws_get_record('user', $useridfield, $userid)) {
return $this->error(get_string('ws_userunknown', 'local_wspp', $userid));
}
Expand All @@ -692,17 +698,18 @@ function get_grades($client, $sesskey, $userid, $useridfield = 'idnumber', $cour
$rgrade = new stdClass;
/// Get the student grades for each course requested.
if ($course = ws_get_record('course', $courseidfield, $cid)) {
if ($this->has_capability('moodle/grade:viewall', CONTEXT_COURSE, $course->id)) {
// rev 1.8.3 the loggedin user can see his own grades
if ($USER->id == $user->id || $this->has_capability('moodle/grade:viewall', CONTEXT_COURSE, $course->id)) {
// get the floating point final grade
if ($legrade = grade_get_course_grade($user->id, $course->id)) {
$rgrade = $legrade;
$rgrade->error = '';
$rgrade->itemid = $cid;
$rgrade->itemid = $course->idnumber?$course->idnumber:$course->shortname;
} else {
$a = new StdClass();
$a->user = fullname($user);
$a->course = $course->fullname;
$rgrade->error = get_string('ws_nogrades', 'local_wspp', $a);
$rgrade->error = get_string('ws_nogradesfound', 'local_wspp', $a);
}
} else {
$rgrade->error = get_string('ws_noseegrades', 'local_wspp', $course->fullname);
Expand Down Expand Up @@ -730,20 +737,26 @@ function get_grades($client, $sesskey, $userid, $useridfield = 'idnumber', $cour
*
*/
public function get_user_grades($client, $sesskey, $userid, $idfield = "idnumber") {

global $CFG,$USER;
if (!$this->validate_client($client, $sesskey, __FUNCTION__)) {
return $this->error(get_string('ws_invalidclient', 'local_wspp'));
}
if (!$this->using19)
return $this->error(get_string(' ws_notsupportedgradebook', 'local_wspp'));

if (empty($userid)) { // rev 1.8.3 default to logged in user !
$userid=$USER->id;
$idfield='id';
}

if (!$user = ws_get_record('user', $idfield, $userid)) {
return $this->error(get_string('ws_userunknown', 'local_wspp', $idfield . '=' . $userid));
}
// we cannot call API grade_get_course_grade($user->id) since it does not set the courseid as we want it
if (!$courses = ws_get_my_courses($user->id, $sort = 'sortorder ASC', $fields = 'idnumber')) {
return $this->error(get_string('ws_nocourseforuser', 'local_wspp', $userid));
}
/**
$courseids = array ();
foreach ($courses as $c)
if (!empty ($c->idnumber))
Expand All @@ -755,6 +768,8 @@ public function get_user_grades($client, $sesskey, $userid, $idfield = "idnumber
// caution not $this->get_user_grades THAT WILL call mdl_sopaserver::get_grades
// resulting in two calls of to_soaparray !!!!
return server :: get_grades($client, $sesskey, $userid, $idfield, $courseids, 'idnumber');
**/
return server :: get_grades($client, $sesskey, $userid, $idfield, array_keys($courses), 'id');
}

/**
Expand All @@ -776,6 +791,10 @@ public function get_course_grades($client, $sesskey, $courseid, $idfield = "idnu
if (!$this->using19)
return $this->error(get_string(' ws_notsupportedgradebook', 'local_wspp'));

if (empty($courseid))
return $this->error(get_string(' ws_missingvalue', 'local_wspp','courseid'));


require_once ($CFG->dirroot . '/grade/lib.php');
require_once ($CFG->dirroot . '/grade/querylib.php');

Expand All @@ -791,15 +810,15 @@ public function get_course_grades($client, $sesskey, $courseid, $idfield = "idnu
if ($legrade = grade_get_course_grade($user->id, $course->id)) {
$rgrade = $legrade;
$rgrade->error = '';
$rgrade->itemid = $user->idnumber;
$rgrade->itemid = $user->idnumber ? $user->idnumber :$user->id;
// $this->debug_output("IDS=".print_r($legrade,true));
$return[] = $rgrade;
} else {
$a = new StdClass();
$a->user = fullname($user);
$a->course = $course->fullname;
$rgrade = new StdClass();
$rgrade->error = get_string('ws_nogrades', 'local_wspp', $a);
$rgrade->error = get_string('ws_nogradesfound', 'local_wspp', $a);
$return[] = $rgrade;
}
}
Expand Down
4 changes: 3 additions & 1 deletion wspp/wslib.php
Expand Up @@ -35,10 +35,12 @@ function ws_get_record($table, $field1, $value1, $field2 = '', $value2 = '', $fi

global $CFG, $DB;

if (empty($field1) || empty($value1)) return false; // rev 1.8.3 avoid unexpected results if some parameters are empty

if ($CFG->wspp_using_moodle20) {
try {
$params = array ();
if ($field1)
//if ($field1)
$params[$field1] = $value1;
if ($field2)
$params[$field2] = $value2;
Expand Down

0 comments on commit 4c51009

Please sign in to comment.