MDL-48715 mod_lesson: use only one field for time spent rule

This commit is contained in:
Jean-Michel Vedrine 2015-03-17 08:40:17 +01:00
parent b7a2633d7d
commit 10e2355f8c
9 changed files with 55 additions and 44 deletions

View File

@ -76,7 +76,7 @@ class backup_lesson_activity_structure_step extends backup_activity_structure_st
'mediafile', 'mediaheight', 'mediawidth', 'mediaclose', 'slideshow',
'width', 'height', 'bgcolor', 'displayleft', 'displayleftif', 'progressbar',
'showhighscores', 'maxhighscores', 'available', 'deadline', 'timemodified',
'completionendreached', 'completiontimespend', 'timetospend'
'completionendreached', 'completiontimespent'
));
// Tell the lesson element about the showhighscores elements mapping to the highscores
// database field.

View File

@ -76,9 +76,8 @@ class restore_lesson_activity_structure_step extends restore_activity_structure_
if (!isset($data->completionendreached)) {
$data->completionendreached = 0;
}
if (!isset($data->completiontimespend)) {
$data->completiontimespend = 0;
$data->timetospend = 0;
if (!isset($data->completiontimespent)) {
$data->completiontimespent = 0;
}
// Compatibility with old backups with maxtime and timed fields.

View File

@ -48,8 +48,7 @@
<FIELD NAME="deadline" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="completionendreached" TYPE="int" LENGTH="1" NOTNULL="false" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="completiontimespend" TYPE="int" LENGTH="1" NOTNULL="false" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timetospend" TYPE="int" LENGTH="11" NOTNULL="false" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="completiontimespent" TYPE="int" LENGTH="11" NOTNULL="false" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>

View File

@ -219,22 +219,15 @@ function xmldb_lesson_upgrade($oldversion) {
if ($oldversion < 2015031500) {
// Define field completiontimespend to be added to lesson.
// Define field completiontimespent to be added to lesson.
$table = new xmldb_table('lesson');
$field = new xmldb_field('completiontimespend', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'completionendreached');
$field = new xmldb_field('completiontimespent', XMLDB_TYPE_INTEGER, '11', null, null, null, '0', 'completionendreached');
// Conditionally launch add field completiontimespend.
// Conditionally launch add field completiontimespent.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Define field timetospend to be added to lesson.
$field = new xmldb_field('timetospend', XMLDB_TYPE_INTEGER, '11', null, null, null, '0', 'completiontimespend');
// Conditionally launch add field timetospend.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Lesson savepoint reached.
upgrade_mod_savepoint(true, 2015031500, 'lesson');
}

View File

@ -97,8 +97,8 @@ $string['completederror'] = 'Complete the lesson';
$string['completethefollowingconditions'] = 'You must complete the following condition(s) in <b>{$a}</b> lesson before you can proceed.';
$string['completionendreached'] = 'Require end reached';
$string['completionendreached_desc'] = 'Student must reach the end of lesson page to complete this activity';
$string['completiontimespend'] = 'Student must do this activity for';
$string['completiontimespendgroup'] = 'Require time spent';
$string['completiontimespent'] = 'Student must do this activity for';
$string['completiontimespentgroup'] = 'Require time spent';
$string['conditionsfordependency'] = 'Condition(s) for the dependency';
$string['configactionaftercorrectanswer'] = 'The default action to take after a correct answer';
$string['configmaxanswers'] = 'Default maximum number of answers per page';

View File

@ -790,7 +790,7 @@ function lesson_get_completion_state($course, $cm, $userid, $type) {
$result = $result || $value;
}
}
if ($lesson->completiontimespend) {
if ($lesson->completiontimespent != 0) {
$duration = $DB->get_field_sql(
"SELECT SUM(lessontime - starttime)
FROM {lesson_timer}
@ -801,9 +801,9 @@ function lesson_get_completion_state($course, $cm, $userid, $type) {
$duration = 0;
}
if ($type == COMPLETION_AND) {
$result = $result && ($lesson->timetospend < $duration);
$result = $result && ($lesson->completiontimespent < $duration);
} else {
$result = $result || ($lesson->timetospend < $duration);
$result = $result || ($lesson->completiontimespent < $duration);
}
}
return $result;

View File

@ -294,22 +294,26 @@ class mod_lesson_mod_form extends moodleform_mod {
/**
* Enforce defaults here
*
* @param array $default_values Form defaults
* @param array $defaultvalues Form defaults
* @return void
**/
function data_preprocessing(&$default_values) {
if (isset($default_values['conditions'])) {
$conditions = unserialize($default_values['conditions']);
$default_values['timespent'] = $conditions->timespent;
$default_values['completed'] = $conditions->completed;
$default_values['gradebetterthan'] = $conditions->gradebetterthan;
public function data_preprocessing(&$defaultvalues) {
if (isset($defaultvalues['conditions'])) {
$conditions = unserialize($defaultvalues['conditions']);
$defaultvalues['timespent'] = $conditions->timespent;
$defaultvalues['completed'] = $conditions->completed;
$defaultvalues['gradebetterthan'] = $conditions->gradebetterthan;
}
// Set up the completion checkbox which is not part of standard data.
$defaultvalues['completiontimespentenabled'] =
!empty($defaultvalues['completiontimespent']) ? 1 : 0;
if ($this->current->instance) {
// Editing existing instance - copy existing files into draft area.
$draftitemid = file_get_submitted_draft_itemid('mediafile');
file_prepare_draft_area($draftitemid, $this->context->id, 'mod_lesson', 'mediafile', 0, array('subdirs'=>0, 'maxbytes' => $this->course->maxbytes, 'maxfiles' => 1));
$default_values['mediafile'] = $draftitemid;
$defaultvalues['mediafile'] = $draftitemid;
}
}
@ -341,12 +345,14 @@ class mod_lesson_mod_form extends moodleform_mod {
get_string('completionendreached_desc', 'lesson'));
$group = array();
$group[] =& $mform->createElement('checkbox', 'completiontimespend', '', get_string('completiontimespend', 'lesson'));
$group[] =& $mform->createElement('duration', 'timetospend', array('optional' => true));
$mform->addGroup($group, 'completiontimespendgroup', get_string('completiontimespendgroup', 'lesson'), array(' '), false);
$mform->disabledIf('timetospend[number]', 'completiontimespend', 'notchecked');
$mform->disabledIf('timetospend[timeunit]', 'completiontimespend', 'notchecked');
return array('completionendreached', 'completiontimespendgroup');
$group[] =& $mform->createElement('checkbox', 'completiontimespentenabled', '',
get_string('completiontimespent', 'lesson'));
$group[] =& $mform->createElement('duration', 'completiontimespent', array('optional' => true));
$mform->addGroup($group, 'completiontimespentgroup', get_string('completiontimespentgroup', 'lesson'), array(' '), false);
$mform->disabledIf('completiontimespent[number]', 'completiontimespentenabled', 'notchecked');
$mform->disabledIf('completiontimespent[timeunit]', 'completiontimespentenabled', 'notchecked');
return array('completionendreached', 'completiontimespentgroup');
}
/**
@ -356,8 +362,22 @@ class mod_lesson_mod_form extends moodleform_mod {
* @return bool True if one or more rules is enabled, false if none are.
*/
public function completion_rule_enabled($data) {
return !empty($data['completionendreached']) ||
(!empty($data['completiontimespend']) && $data['timetospend'] != 0);
return !empty($data['completionendreached']) || $data['completiontimespent'] > 0;
}
public function get_data() {
$data = parent::get_data();
if (!$data) {
return false;
}
// Turn off completion setting if the checkbox is not ticked.
if (!empty($data->completionunlocked)) {
$autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
if (empty($data->completiontimespentenabled) || !$autocompletion) {
$data->completiontimespent = 0;
}
}
return $data;
}
}

View File

@ -32,9 +32,9 @@ Feature: Set time spent as a completion condition for a lesson
| Name | Test lesson |
| Description | Test lesson description |
| Completion tracking | Show activity as complete when conditions are met |
| completiontimespend | 1 |
| timetospend[timeunit] | 60 |
| timetospend[number] | 1 |
| completiontimespentenabled | 1 |
| completiontimespent[timeunit] | 60 |
| completiontimespent[number] | 1 |
And I follow "Test lesson"
And I follow "Add a content page"
And I set the following fields to these values:

View File

@ -458,11 +458,11 @@ if ($pageid != LESSON_EOL) {
// Update completion state.
$completion = new completion_info($course);
if ($completion->is_enabled($cm) && ($lesson->completionendreached || $lesson->completiontimespend)) {
if ($completion->is_enabled($cm) && ($lesson->completionendreached || $lesson->completiontimespent > 0)) {
$completion->update_state($cm, COMPLETION_COMPLETE);
}
if ($lesson->completiontimespend) {
if ($lesson->completiontimespent > 0) {
$duration = $DB->get_field_sql(
"SELECT SUM(lessontime - starttime)
FROM {lesson_timer}
@ -474,10 +474,10 @@ if ($pageid != LESSON_EOL) {
}
// If student has not spend enough time in the lesson, display a message.
if ($duration < $lesson->timetospend) {
if ($duration < $lesson->completiontimespent) {
$a = new stdClass;
$a->timespent = format_time($duration);
$a->timerequired = format_time($lesson->timetospend);
$a->timerequired = format_time($lesson->completiontimespent);
$lessoncontent .= $lessonoutput->paragraph(get_string("notenoughtimespent", "lesson", $a), 'center');
}
}