Merge branch 'MDL-57665_master' of git://github.com/dmonllao/moodle

This commit is contained in:
David Monllao 2017-03-20 11:25:09 +01:00
commit 2ddcf27219
6 changed files with 155 additions and 9 deletions

View File

@ -832,4 +832,85 @@ class mod_lesson_external extends external_api {
)
);
}
/**
* Describes the parameters for get_user_timers.
*
* @return external_external_function_parameters
* @since Moodle 3.3
*/
public static function get_user_timers_parameters() {
return new external_function_parameters (
array(
'lessonid' => new external_value(PARAM_INT, 'lesson instance id'),
'userid' => new external_value(PARAM_INT, 'the user id (empty for current user)', VALUE_DEFAULT, null),
)
);
}
/**
* Return the timers in the current lesson for the given user.
*
* @param int $lessonid lesson instance id
* @param int $userid only fetch timers of the given user
* @return array of warnings and timers
* @since Moodle 3.3
* @throws moodle_exception
*/
public static function get_user_timers($lessonid, $userid = null) {
global $USER;
$params = array(
'lessonid' => $lessonid,
'userid' => $userid,
);
$params = self::validate_parameters(self::get_user_timers_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);
}
$timers = $lesson->get_user_timers($params['userid']);
$result = array();
$result['timers'] = $timers;
$result['warnings'] = $warnings;
return $result;
}
/**
* Describes the get_user_timers return value.
*
* @return external_single_structure
* @since Moodle 3.3
*/
public static function get_user_timers_returns() {
return new external_single_structure(
array(
'timers' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'The attempt id'),
'lessonid' => new external_value(PARAM_INT, 'The lesson id'),
'userid' => new external_value(PARAM_INT, 'The user id'),
'starttime' => new external_value(PARAM_INT, 'First access time for a new timer session'),
'lessontime' => new external_value(PARAM_INT, 'Last access time to the lesson during the timer session'),
'completed' => new external_value(PARAM_INT, 'If the lesson for this timer was completed'),
),
'The timers'
)
),
'warnings' => new external_warnings(),
)
);
}
}

View File

@ -84,4 +84,12 @@ $functions = array(
'capabilities' => 'mod/lesson:view',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
'mod_lesson_get_user_timers' => array(
'classname' => 'mod_lesson_external',
'methodname' => 'get_user_timers',
'description' => 'Return the timers in the current lesson for the given user.',
'type' => 'read',
'capabilities' => 'mod/lesson:view',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
);

View File

@ -1583,10 +1583,9 @@ class lesson extends lesson_base {
// clock code
// get time information for this user
$params = array("lessonid" => $this->properties->id, "userid" => $USER->id);
if (!$timer = $DB->get_records('lesson_timer', $params, 'starttime DESC', '*', 0, 1)) {
if (!$timer = $this->get_user_timers($USER->id, 'starttime DESC', '*', 0, 1)) {
$this->start_timer();
$timer = $DB->get_records('lesson_timer', $params, 'starttime DESC', '*', 0, 1);
$timer = $this->get_user_timers($USER->id, 'starttime DESC', '*', 0, 1);
}
$timer = current($timer); // This will get the latest start time record.
@ -2433,6 +2432,28 @@ class lesson extends lesson_base {
$completion = new completion_info($this->get_courserecord());
$completion->set_module_viewed($this->get_cm());
}
/**
* Return the timers in the current lesson for the given user.
*
* @param int $userid the user id
* @param string $sort an order to sort the results in (optional, a valid SQL ORDER BY parameter).
* @param string $fields a comma separated list of fields to return
* @param int $limitfrom return a subset of records, starting at this point (optional).
* @param int $limitnum return a subset comprising this many records in total (optional, required if $limitfrom is set).
* @return array list of timers for the given user in the lesson
* @since Moodle 3.3
*/
public function get_user_timers($userid = null, $sort = '', $fields = '*', $limitfrom = 0, $limitnum = 0) {
global $DB, $USER;
if ($userid === null) {
$userid = $USER->id;
}
$params = array('lessonid' => $this->properties->id, 'userid' => $userid);
return $DB->get_records('lesson_timer', $params, $sort, $fields, $limitfrom, $limitnum);
}
}

View File

@ -74,15 +74,13 @@ if ($action === 'delete') {
$try -= $modifier;
/// Clean up the timer table by removing using the order - this is silly, it should be linked to specific attempt (skodak)
$params = array ("userid" => $userid, "lessonid" => $lesson->id);
$timers = $DB->get_records_sql("SELECT id FROM {lesson_timer}
WHERE userid = :userid AND lessonid = :lessonid
ORDER BY starttime", $params, $try, 1);
$timers = $lesson->get_user_timers($userid, 'starttime', 'id', $try, 1);
if ($timers) {
$timer = reset($timers);
$DB->delete_records('lesson_timer', array('id' => $timer->id));
}
$params = array ("userid" => $userid, "lessonid" => $lesson->id);
// Remove the grade from the grades tables - this is silly, it should be linked to specific attempt (skodak).
$grades = $DB->get_records_sql("SELECT id FROM {lesson_grades}
WHERE userid = :userid AND lessonid = :lessonid
@ -687,7 +685,8 @@ if ($action === 'delete') {
$completed = $grade->completed;
$grade = round($grade->grade, 2);
}
if (!$times = $DB->get_records_select("lesson_timer", "lessonid = :lessonid and userid = :userid", $params, "starttime", "*", $try, 1)) {
if (!$times = $lesson->get_user_timers($userid, 'starttime', '*', $try, 1)) {
$timetotake = -1;
} else {
$timetotake = current($times);

View File

@ -629,4 +629,41 @@ class mod_lesson_external_testcase extends externallib_advanced_testcase {
$this->assertCount(0, $result['warnings']);
$this->assertCount(0, $result['pages']);
}
/**
* Test get_user_timers
*/
public function test_get_user_timers() {
global $DB;
// Create a couple of timers for the current user.
$timer1 = new stdClass;
$timer1->lessonid = $this->lesson->id;
$timer1->userid = $this->student->id;
$timer1->completed = 1;
$timer1->starttime = time() - WEEKSECS;
$timer1->lessontime = time();
$timer1->id = $DB->insert_record("lesson_timer", $timer1);
$timer2 = new stdClass;
$timer2->lessonid = $this->lesson->id;
$timer2->userid = $this->student->id;
$timer2->completed = 0;
$timer2->starttime = time() - DAYSECS;
$timer2->lessontime = time() + 1;
$timer2->id = $DB->insert_record("lesson_timer", $timer2);
// Test retrieve timers.
$result = mod_lesson_external::get_user_timers($this->lesson->id, $this->student->id);
$result = external_api::clean_returnvalue(mod_lesson_external::get_user_timers_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertCount(2, $result['timers']);
foreach ($result['timers'] as $timer) {
if ($timer['id'] == $timer1->id) {
$this->assertEquals($timer1, (object) $timer);
} else {
$this->assertEquals($timer2, (object) $timer);
}
}
}
}

View File

@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2016120506; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2016120507; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2016112900; // Requires this Moodle version
$plugin->component = 'mod_lesson'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 0;