MDL-57657 mod_lesson: New WS mod_lesson_get_user_attempt_grade

This commit is contained in:
Juan Leyva 2017-01-16 15:50:21 +01:00
parent 78dbb42c80
commit d37472d88f
3 changed files with 135 additions and 0 deletions

View File

@ -670,4 +670,80 @@ class mod_lesson_external extends external_api {
)
);
}
/**
* Describes the parameters for get_user_attempt_grade.
*
* @return external_external_function_parameters
* @since Moodle 3.3
*/
public static function get_user_attempt_grade_parameters() {
return new external_function_parameters (
array(
'lessonid' => new external_value(PARAM_INT, 'lesson instance id'),
'lessonattempt' => new external_value(PARAM_INT, 'lesson attempt number'),
'userid' => new external_value(PARAM_INT, 'the user id (empty for current user)', VALUE_DEFAULT, null),
)
);
}
/**
* Return grade information in the attempt for a given user.
*
* @param int $lessonid lesson instance id
* @param int $lessonattempt lesson attempt number
* @param int $userid only fetch attempts of the given user
* @return array of warnings and page attempts
* @since Moodle 3.3
* @throws moodle_exception
*/
public static function get_user_attempt_grade($lessonid, $lessonattempt, $userid = null) {
global $CFG, $USER;
require_once($CFG->libdir . '/gradelib.php');
$params = array(
'lessonid' => $lessonid,
'lessonattempt' => $lessonattempt,
'userid' => $userid,
);
$params = self::validate_parameters(self::get_user_attempt_grade_parameters(), $params);
$warnings = array();
list($lesson, $course, $cm, $context) = self::validate_lesson($params['lessonid']);
// Default value for userid.
if (empty($params['userid'])) {
$params['userid'] = $USER->id;
}
// Extra checks so only users with permissions can view other users attempts.
if ($USER->id != $params['userid']) {
self::check_can_view_user_data($params['userid'], $course, $cm, $context);
}
$result = (array) lesson_grade($lesson, $params['lessonattempt'], $params['userid']);
$result['warnings'] = $warnings;
return $result;
}
/**
* Describes the get_user_attempt_grade return value.
*
* @return external_single_structure
* @since Moodle 3.3
*/
public static function get_user_attempt_grade_returns() {
return new external_single_structure(
array(
'nquestions' => new external_value(PARAM_INT, 'Number of questions answered'),
'attempts' => new external_value(PARAM_INT, 'Number of question attempts'),
'total' => new external_value(PARAM_FLOAT, 'Max points possible'),
'earned' => new external_value(PARAM_FLOAT, 'Points earned by student'),
'grade' => new external_value(PARAM_FLOAT, 'Calculated percentage grade'),
'nmanual' => new external_value(PARAM_INT, 'Number of manually graded questions'),
'manualpoints' => new external_value(PARAM_FLOAT, 'Point value for manually graded questions'),
'warnings' => new external_warnings(),
)
);
}
}

View File

@ -68,4 +68,12 @@ $functions = array(
'capabilities' => 'mod/lesson:view',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
'mod_lesson_get_user_attempt_grade' => array(
'classname' => 'mod_lesson_external',
'methodname' => 'get_user_attempt_grade',
'description' => 'Return grade information in the attempt for a given user.',
'type' => 'read',
'capabilities' => 'mod/lesson:view',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
);

View File

@ -528,4 +528,55 @@ class mod_lesson_external_testcase extends externallib_advanced_testcase {
$this->expectException('moodle_exception');
$result = mod_lesson_external::get_user_grade($this->lesson->id, $this->teacher->id);
}
/**
* Test get_user_attempt_grade
*/
public function test_get_user_attempt_grade() {
global $DB;
// Create a fake attempt for the first possible answer.
$attemptnumber = 1;
$p2answers = $DB->get_records('lesson_answers', array('lessonid' => $this->lesson->id, 'pageid' => $this->page2->id), 'id');
$answerid = reset($p2answers)->id;
$newpageattempt = [
'lessonid' => $this->lesson->id,
'pageid' => $this->page2->id,
'userid' => $this->student->id,
'answerid' => $answerid,
'retry' => $attemptnumber,
'correct' => 1,
'useranswer' => '1',
'timeseen' => time(),
];
$DB->insert_record('lesson_attempts', (object) $newpageattempt);
// Test first without custom scoring. All questions receive the same value if correctly responsed.
$DB->set_field('lesson', 'custom', 0, array('id' => $this->lesson->id));
$this->setUser($this->student);
$result = mod_lesson_external::get_user_attempt_grade($this->lesson->id, $attemptnumber, $this->student->id);
$result = external_api::clean_returnvalue(mod_lesson_external::get_user_attempt_grade_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertEquals(1, $result['nquestions']);
$this->assertEquals(1, $result['attempts']);
$this->assertEquals(1, $result['total']);
$this->assertEquals(1, $result['earned']);
$this->assertEquals(100, $result['grade']);
$this->assertEquals(0, $result['nmanual']);
$this->assertEquals(0, $result['manualpoints']);
// With custom scoring, in this case, we don't retrieve any values since we are using questions without particular score.
$DB->set_field('lesson', 'custom', 1, array('id' => $this->lesson->id));
$result = mod_lesson_external::get_user_attempt_grade($this->lesson->id, $attemptnumber, $this->student->id);
$result = external_api::clean_returnvalue(mod_lesson_external::get_user_attempt_grade_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertEquals(1, $result['nquestions']);
$this->assertEquals(1, $result['attempts']);
$this->assertEquals(0, $result['total']);
$this->assertEquals(0, $result['earned']);
$this->assertEquals(0, $result['grade']);
$this->assertEquals(0, $result['nmanual']);
$this->assertEquals(0, $result['manualpoints']);
}
}