Choice: MDL-20440 Added completion support to Choice (mark completed when user makes choice)

This commit is contained in:
Sam Marshall 2010-10-14 14:58:43 +00:00
parent 14333c1128
commit bc499733f8
9 changed files with 108 additions and 15 deletions

View file

@ -40,7 +40,8 @@ class backup_choice_activity_structure_step extends backup_activity_structure_st
$choice = new backup_nested_element('choice', array('id'), array( $choice = new backup_nested_element('choice', array('id'), array(
'name', 'intro', 'introformat', 'publish', 'name', 'intro', 'introformat', 'publish',
'showresults', 'display', 'allowupdate', 'allowunanswered', 'showresults', 'display', 'allowupdate', 'allowunanswered',
'limitanswers', 'timeopen', 'timeclose', 'timemodified')); 'limitanswers', 'timeopen', 'timeclose', 'timemodified',
'completionsubmit'));
$options = new backup_nested_element('options'); $options = new backup_nested_element('options');

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="mod/choice/db" VERSION="20090420" COMMENT="XMLDB file for Moodle mod/choice" <XMLDB PATH="mod/choice/db" VERSION="20101013" COMMENT="XMLDB file for Moodle mod/choice"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd" xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
> >
@ -11,6 +11,7 @@
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="course" NEXT="intro"/> <FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="course" NEXT="intro"/>
<FIELD NAME="intro" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" PREVIOUS="name" NEXT="introformat"/> <FIELD NAME="intro" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" PREVIOUS="name" NEXT="introformat"/>
<FIELD NAME="introformat" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="intro" NEXT="publish"/> <FIELD NAME="introformat" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="intro" NEXT="publish"/>
<FIELD NAME="publish" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="introformat" NEXT="showresults"/> <FIELD NAME="publish" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="introformat" NEXT="showresults"/>
<FIELD NAME="showresults" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="publish" NEXT="display"/> <FIELD NAME="showresults" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="publish" NEXT="display"/>
<FIELD NAME="display" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="showresults" NEXT="allowupdate"/> <FIELD NAME="display" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="showresults" NEXT="allowupdate"/>
@ -19,7 +20,9 @@
<FIELD NAME="limitanswers" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="showunanswered" NEXT="timeopen"/> <FIELD NAME="limitanswers" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="showunanswered" NEXT="timeopen"/>
<FIELD NAME="timeopen" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="limitanswers" NEXT="timeclose"/> <FIELD NAME="timeopen" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="limitanswers" NEXT="timeclose"/>
<FIELD NAME="timeclose" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timeopen" NEXT="timemodified"/> <FIELD NAME="timeclose" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timeopen" NEXT="timemodified"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timeclose"/> <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timeclose" NEXT="completionsubmit"/>
<FIELD NAME="completionsubmit" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" COMMENT="If this field is set to 1, then the activity will be automatically marked as 'complete' once the user submits their choice." PREVIOUS="timemodified"/>
</FIELDS> </FIELDS>
<KEYS> <KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/> <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
@ -28,6 +31,7 @@
<INDEX NAME="course" UNIQUE="false" FIELDS="course"/> <INDEX NAME="course" UNIQUE="false" FIELDS="course"/>
</INDEXES> </INDEXES>
</TABLE> </TABLE>
<TABLE NAME="choice_options" COMMENT="available options to choice" PREVIOUS="choice" NEXT="choice_answers"> <TABLE NAME="choice_options" COMMENT="available options to choice" PREVIOUS="choice" NEXT="choice_answers">
<FIELDS> <FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="choiceid"/> <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="choiceid"/>
@ -37,6 +41,7 @@
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="maxanswers"/> <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="maxanswers"/>
</FIELDS> </FIELDS>
<KEYS> <KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="choiceid"/> <KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="choiceid"/>
<KEY NAME="choiceid" TYPE="foreign" FIELDS="choiceid" REFTABLE="choice" REFFIELDS="id" PREVIOUS="primary"/> <KEY NAME="choiceid" TYPE="foreign" FIELDS="choiceid" REFTABLE="choice" REFFIELDS="id" PREVIOUS="primary"/>
</KEYS> </KEYS>
@ -46,6 +51,7 @@
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="choiceid"/> <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="choiceid"/>
<FIELD NAME="choiceid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="userid"/> <FIELD NAME="choiceid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="userid"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="choiceid" NEXT="optionid"/> <FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="choiceid" NEXT="optionid"/>
<FIELD NAME="optionid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="userid" NEXT="timemodified"/> <FIELD NAME="optionid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="userid" NEXT="timemodified"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="optionid"/> <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="optionid"/>
</FIELDS> </FIELDS>
@ -55,6 +61,7 @@
<KEY NAME="optionid" TYPE="foreign" FIELDS="optionid" REFTABLE="choice_options" REFFIELDS="id" PREVIOUS="choiceid"/> <KEY NAME="optionid" TYPE="foreign" FIELDS="optionid" REFTABLE="choice_options" REFFIELDS="id" PREVIOUS="choiceid"/>
</KEYS> </KEYS>
<INDEXES> <INDEXES>
<INDEX NAME="userid" UNIQUE="false" FIELDS="userid"/> <INDEX NAME="userid" UNIQUE="false" FIELDS="userid"/>
</INDEXES> </INDEXES>
</TABLE> </TABLE>

View file

@ -53,6 +53,21 @@ function xmldb_choice_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2009042001, 'choice'); upgrade_mod_savepoint(true, 2009042001, 'choice');
} }
if ($oldversion < 2010101300) {
// Define field completionsubmit to be added to choice
$table = new xmldb_table('choice');
$field = new xmldb_field('completionsubmit', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'timemodified');
// Conditionally launch add field completionsubmit
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// choice savepoint reached
upgrade_mod_savepoint(true, 2010101300, 'choice');
}
return true; return true;
} }

View file

@ -26,6 +26,7 @@
$string['addmorechoices'] = 'Add more choices'; $string['addmorechoices'] = 'Add more choices';
$string['allowupdate'] = 'Allow choice to be updated'; $string['allowupdate'] = 'Allow choice to be updated';
$string['answered'] = 'Answered'; $string['answered'] = 'Answered';
$string['completionsubmit'] = 'Show as complete when user makes a choice';
$string['displayhorizontal'] = 'Display horizontally'; $string['displayhorizontal'] = 'Display horizontally';
$string['displaymode'] = 'Display Mode'; $string['displaymode'] = 'Display Mode';
$string['displayvertical'] = 'Display vertically'; $string['displayvertical'] = 'Display vertically';

View file

@ -240,10 +240,10 @@ function choice_prepare_options($choice, $user, $coursemodule, $allresponses) {
* @param int $formanswer * @param int $formanswer
* @param object $choice * @param object $choice
* @param int $userid * @param int $userid
* @param int $courseid * @param object $course Course object
* @param object $cm * @param object $cm
*/ */
function choice_user_submit_response($formanswer, $choice, $userid, $courseid, $cm) { function choice_user_submit_response($formanswer, $choice, $userid, $course, $cm) {
global $DB; global $DB;
$current = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $userid)); $current = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $userid));
$context = get_context_instance(CONTEXT_MODULE, $cm->id); $context = get_context_instance(CONTEXT_MODULE, $cm->id);
@ -291,7 +291,7 @@ WHERE
$newanswer->optionid = $formanswer; $newanswer->optionid = $formanswer;
$newanswer->timemodified = time(); $newanswer->timemodified = time();
$DB->update_record("choice_answers", $newanswer); $DB->update_record("choice_answers", $newanswer);
add_to_log($courseid, "choice", "choose again", "view.php?id=$cm->id", $choice->id, $cm->id); add_to_log($course->id, "choice", "choose again", "view.php?id=$cm->id", $choice->id, $cm->id);
} else { } else {
$newanswer = NULL; $newanswer = NULL;
$newanswer->choiceid = $choice->id; $newanswer->choiceid = $choice->id;
@ -299,7 +299,13 @@ WHERE
$newanswer->optionid = $formanswer; $newanswer->optionid = $formanswer;
$newanswer->timemodified = time(); $newanswer->timemodified = time();
$DB->insert_record("choice_answers", $newanswer); $DB->insert_record("choice_answers", $newanswer);
add_to_log($courseid, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id);
// Update completion state
$completion = new completion_info($course);
if ($completion->is_enabled($cm) && $choice->completionsubmit) {
$completion->update_state($cm, COMPLETION_COMPLETE);
}
add_to_log($course->id, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id);
} }
} else { } else {
if (!($current->optionid==$formanswer)) { //check to see if current choice already selected - if not display error if (!($current->optionid==$formanswer)) { //check to see if current choice already selected - if not display error
@ -513,10 +519,12 @@ function prepare_choice_show_results($choice, $course, $cm, $allresponses, $forc
/** /**
* @global object * @global object
* @param array $attemptids * @param array $attemptids
* @param int $choiceid * @param object $choice Choice main table row
* @param object $cm Course-module object
* @param object $course Course object
* @return bool * @return bool
*/ */
function choice_delete_responses($attemptids, $choiceid) { function choice_delete_responses($attemptids, $choice, $cm, $course) {
global $DB; global $DB;
if(!is_array($attemptids) || empty($attemptids)) { if(!is_array($attemptids) || empty($attemptids)) {
return false; return false;
@ -528,9 +536,14 @@ function choice_delete_responses($attemptids, $choiceid) {
} }
} }
$completion = new completion_info($course);
foreach($attemptids as $attemptid) { foreach($attemptids as $attemptid) {
if ($todelete = $DB->get_record('choice_answers', array('choiceid' => $choiceid, 'userid' => $attemptid))) { if ($todelete = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $attemptid))) {
$DB->delete_records('choice_answers', array('choiceid' => $choiceid, 'userid' => $attemptid)); $DB->delete_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $attemptid));
// Update completion state
if ($completion->is_enabled($cm) && $choice->completionsubmit) {
$completion->update_state($cm, COMPLETION_INCOMPLETE, $attemptid);
}
} }
} }
return true; return true;
@ -772,6 +785,7 @@ function choice_supports($feature) {
case FEATURE_GROUPMEMBERSONLY: return true; case FEATURE_GROUPMEMBERSONLY: return true;
case FEATURE_MOD_INTRO: return true; case FEATURE_MOD_INTRO: return true;
case FEATURE_COMPLETION_TRACKS_VIEWS: return true; case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
case FEATURE_COMPLETION_HAS_RULES: return true;
case FEATURE_GRADE_HAS_GRADE: return false; case FEATURE_GRADE_HAS_GRADE: return false;
case FEATURE_GRADE_OUTCOMES: return false; case FEATURE_GRADE_OUTCOMES: return false;
case FEATURE_BACKUP_MOODLE2: return true; case FEATURE_BACKUP_MOODLE2: return true;
@ -809,3 +823,30 @@ function choice_extend_settings_navigation(settings_navigation $settings, naviga
$choicenode->add(get_string("viewallresponses", "choice", $responsecount), new moodle_url('/mod/choice/report.php', array('id'=>$PAGE->cm->id))); $choicenode->add(get_string("viewallresponses", "choice", $responsecount), new moodle_url('/mod/choice/report.php', array('id'=>$PAGE->cm->id)));
} }
} }
/**
* Obtains the automatic completion state for this choice based on any conditions
* in forum settings.
*
* @param object $course Course
* @param object $cm Course-module
* @param int $userid User ID
* @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
* @return bool True if completed, false if not, $type if conditions not set.
*/
function choice_get_completion_state($course, $cm, $userid, $type) {
global $CFG,$DB;
// Get choice details
$choice = $DB->get_record('choice', array('id'=>$cm->instance), '*',
MUST_EXIST);
// If completion option is enabled, evaluate it and return true/false
if($choice->completionsubmit) {
return $DB->record_exists('choice_answers', array(
'choiceid'=>$choice->id, 'userid'=>$userid));
} else {
// Completion option is not enabled so just return $type
return $type;
}
}

View file

@ -137,5 +137,27 @@ class mod_choice_mod_form extends moodleform_mod {
return $errors; return $errors;
} }
function get_data() {
$data = parent::get_data();
if (!$data) {
return false;
}
// Set up completion section even if checkbox is not ticked
if (empty($data->completionsection)) {
$data->completionsection=0;
}
return $data;
}
function add_completion_rules() {
$mform =& $this->_form;
$mform->addElement('checkbox', 'completionsubmit', '', get_string('completionsubmit', 'choice'));
return array('completionsubmit');
}
function completion_rule_enabled($data) {
return !empty($data['completionsubmit']);
}
} }

View file

@ -46,7 +46,7 @@
add_to_log($course->id, "choice", "report", "report.php?id=$cm->id", "$choice->id",$cm->id); add_to_log($course->id, "choice", "report", "report.php?id=$cm->id", "$choice->id",$cm->id);
if (data_submitted() && $action == 'delete' && has_capability('mod/choice:deleteresponses',$context) && confirm_sesskey()) { if (data_submitted() && $action == 'delete' && has_capability('mod/choice:deleteresponses',$context) && confirm_sesskey()) {
choice_delete_responses($attemptids, $choice->id); //delete responses. choice_delete_responses($attemptids, $choice, $cm, $course); //delete responses.
redirect("report.php?id=$cm->id"); redirect("report.php?id=$cm->id");
} }

View file

@ -5,7 +5,7 @@
// This fragment is called by /admin/index.php // This fragment is called by /admin/index.php
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
$module->version = 2010080300; $module->version = 2010101300;
$module->requires = 2010080300; // Requires this Moodle version $module->requires = 2010080300; // Requires this Moodle version
$module->cron = 0; $module->cron = 0;

View file

@ -38,6 +38,12 @@
if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, 'mod/choice:choose') and $choice->allowupdate) { if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, 'mod/choice:choose') and $choice->allowupdate) {
if ($answer = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id))) { if ($answer = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id))) {
$DB->delete_records('choice_answers', array('id' => $answer->id)); $DB->delete_records('choice_answers', array('id' => $answer->id));
// Update completion state
$completion = new completion_info($course);
if ($completion->is_enabled($cm) && $choice->completionsubmit) {
$completion->update_state($cm, COMPLETION_INCOMPLETE);
}
} }
} }
@ -49,7 +55,7 @@
$timenow = time(); $timenow = time();
if (has_capability('mod/choice:deleteresponses', $context)) { if (has_capability('mod/choice:deleteresponses', $context)) {
if ($action == 'delete') { //some responses need to be deleted if ($action == 'delete') { //some responses need to be deleted
choice_delete_responses($attemptids, $choice->id); //delete responses. choice_delete_responses($attemptids, $choice, $cm, $course); //delete responses.
redirect("view.php?id=$cm->id"); redirect("view.php?id=$cm->id");
} }
} }
@ -58,7 +64,7 @@
if (empty($answer)) { if (empty($answer)) {
redirect("view.php?id=$cm->id", get_string('mustchooseone', 'choice')); redirect("view.php?id=$cm->id", get_string('mustchooseone', 'choice'));
} else { } else {
choice_user_submit_response($answer, $choice, $USER->id, $course->id, $cm); choice_user_submit_response($answer, $choice, $USER->id, $course, $cm);
} }
echo $OUTPUT->header(); echo $OUTPUT->header();
echo $OUTPUT->notification(get_string('choicesaved', 'choice'),'notifysuccess'); echo $OUTPUT->notification(get_string('choicesaved', 'choice'),'notifysuccess');