MDL-48715 mod_lesson: add a time spent custom completion rule

This commit is contained in:
Jean-Michel Vedrine
2015-03-15 15:24:55 +01:00
parent 06122e46fd
commit d0445cf70d
9 changed files with 90 additions and 10 deletions
@@ -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'
'completionendreached', 'completiontimespend', 'timetospend'
));
// Tell the lesson element about the showhighscores elements mapping to the highscores
// database field.
@@ -72,10 +72,14 @@ class restore_lesson_activity_structure_step extends restore_activity_structure_
unset($data->showhighscores);
}
// Supply item that maybe missing from previous versions.
// Supply items that maybe missing from previous versions.
if (!isset($data->completionendreached)) {
$data->completionendreached = 0;
}
if (!isset($data->completiontimespend)) {
$data->completiontimespend = 0;
$data->timetospend = 0;
}
// Compatibility with old backups with maxtime and timed fields.
if (!isset($data->timelimit)) {
+2
View File
@@ -48,6 +48,8 @@
<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"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
+22
View File
@@ -217,5 +217,27 @@ function xmldb_lesson_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2015030401, 'lesson');
}
if ($oldversion < 2015031500) {
// Define field completiontimespend to be added to lesson.
$table = new xmldb_table('lesson');
$field = new xmldb_field('completiontimespend', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'completionendreached');
// Conditionally launch add field completiontimespend.
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');
}
return true;
}
+3
View File
@@ -97,6 +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['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';
@@ -322,6 +324,7 @@ $string['normal'] = 'Normal - follow lesson path';
$string['notcompleted'] = 'Not completed';
$string['notdefined'] = 'Not defined';
$string['notenoughsubquestions'] = 'Not enough sub-questions have been defined!';
$string['notenoughtimespent'] = 'You completed this lesson in {$a->timespent}, which is less than the required time of {$a->timerequired}. You might need to attempt the lesson again.';
$string['nothighscore'] = 'You did not make the top {$a} high scores list.';
$string['notitle'] = 'No title';
$string['numberofcorrectanswers'] = 'Number of correct answers: {$a}';
+24 -4
View File
@@ -779,14 +779,34 @@ function lesson_get_completion_state($course, $cm, $userid, $type) {
$lesson = $DB->get_record('lesson', array('id' => $cm->instance), '*',
MUST_EXIST);
$result = $type; // Default return value.
// If completion option is enabled, evaluate it and return true/false.
if ($lesson->completionendreached) {
return $DB->record_exists('lesson_timer', array(
$value = $DB->record_exists('lesson_timer', array(
'lessonid' => $lesson->id, 'userid' => $userid, 'completed' => 1));
} else {
// Completion option is not enabled so just return $type.
return $type;
if ($type == COMPLETION_AND) {
$result = $result && $value;
} else {
$result = $result || $value;
}
}
if ($lesson->completiontimespend) {
$duration = $DB->get_field_sql(
"SELECT SUM(lessontime - starttime)
FROM {lesson_timer}
WHERE lessonid = :lessonid
AND userid = :userid",
array('userid' => $userid, 'lessonid' => $lesson->id));
if (!$duration) {
$duration = 0;
}
if ($type == COMPLETION_AND) {
$result = $result && ($lesson->timetospend < $duration);
} else {
$result = $result || ($lesson->timetospend < $duration);
}
}
return $result;
}
/**
* This function extends the settings navigation block for the site.
+10 -2
View File
@@ -339,7 +339,14 @@ class mod_lesson_mod_form extends moodleform_mod {
$mform->addElement('checkbox', 'completionendreached', get_string('completionendreached', 'lesson'),
get_string('completionendreached_desc', 'lesson'));
return array('completionendreached');
$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');
}
/**
@@ -349,7 +356,8 @@ 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']);
return !empty($data['completionendreached']) ||
(!empty($data['completiontimespend']) && $data['timetospend'] != 0);
}
}
+1 -1
View File
@@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2015030401; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2015031500; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2014110400; // Requires this Moodle version
$plugin->component = 'mod_lesson'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 0;
+22 -1
View File
@@ -458,10 +458,31 @@ if ($pageid != LESSON_EOL) {
// Update completion state.
$completion = new completion_info($course);
if ($completion->is_enabled($cm) && $lesson->completionendreached) {
if ($completion->is_enabled($cm) && ($lesson->completionendreached || $lesson->completiontimespend)) {
$completion->update_state($cm, COMPLETION_COMPLETE);
}
if ($lesson->completiontimespend) {
$duration = $DB->get_field_sql(
"SELECT SUM(lessontime - starttime)
FROM {lesson_timer}
WHERE lessonid = :lessonid
AND userid = :userid",
array('userid' => $USER->id, 'lessonid' => $lesson->id));
if (!$duration) {
$duration = 0;
}
// If student has not spend enough time in the lesson, display a message.
if ($duration < $lesson->timetospend) {
$a = new stdClass;
$a->timespent = format_time($duration);
$a->timerequired = format_time($lesson->timetospend);
$lessoncontent .= $lessonoutput->paragraph(get_string("notenoughtimespent", "lesson", $a), 'center');
}
}
if ($gradeinfo->attempts) {
if (!$lesson->custom) {
$lessoncontent .= $lessonoutput->paragraph(get_string("numberofpagesviewed", "lesson", $gradeinfo->nquestions), 'center');