Index: mod/choice/backup/moodle2/backup_choice_stepslib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/choice/backup/moodle2/backup_choice_stepslib.php,v
retrieving revision 1.2
diff -u -r1.2 backup_choice_stepslib.php
--- mod/choice/backup/moodle2/backup_choice_stepslib.php	3 Jul 2010 13:37:20 -0000	1.2
+++ mod/choice/backup/moodle2/backup_choice_stepslib.php	13 Oct 2010 14:04:03 -0000
@@ -40,7 +40,8 @@
         $choice = new backup_nested_element('choice', array('id'), array(
             'name', 'intro', 'introformat', 'publish',
             'showresults', 'display', 'allowupdate', 'allowunanswered',
-            'limitanswers', 'timeopen', 'timeclose', 'timemodified'));
+            'limitanswers', 'timeopen', 'timeclose', 'timemodified',
+            'completionsubmit'));
 
         $options = new backup_nested_element('options');
 
Index: mod/choice/report.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/choice/report.php,v
retrieving revision 1.82
diff -u -r1.82 report.php
--- mod/choice/report.php	13 Oct 2010 09:00:31 -0000	1.82
+++ mod/choice/report.php	13 Oct 2010 14:04:03 -0000
@@ -46,7 +46,7 @@
     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()) {
-        choice_delete_responses($attemptids, $choice->id); //delete responses.
+        choice_delete_responses($attemptids, $choice, $cm, $course); //delete responses.
         redirect("report.php?id=$cm->id");
     }
 
Index: mod/choice/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/choice/lib.php,v
retrieving revision 1.140
diff -u -r1.140 lib.php
--- mod/choice/lib.php	21 Sep 2010 08:37:42 -0000	1.140
+++ mod/choice/lib.php	13 Oct 2010 14:04:03 -0000
@@ -240,10 +240,10 @@
  * @param int $formanswer
  * @param object $choice
  * @param int $userid
- * @param int $courseid
+ * @param object $course Course object
  * @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;
     $current = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $userid));
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
@@ -291,7 +291,7 @@
             $newanswer->optionid = $formanswer;
             $newanswer->timemodified = time();
             $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 {
             $newanswer = NULL;
             $newanswer->choiceid = $choice->id;
@@ -299,7 +299,13 @@
             $newanswer->optionid = $formanswer;
             $newanswer->timemodified = time();
             $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 {
         if (!($current->optionid==$formanswer)) { //check to see if current choice already selected - if not display error
@@ -513,10 +519,12 @@
 /**
  * @global object
  * @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
  */
-function choice_delete_responses($attemptids, $choiceid) {
+function choice_delete_responses($attemptids, $choice, $cm, $course) {
     global $DB;
     if(!is_array($attemptids) || empty($attemptids)) {
         return false;
@@ -528,9 +536,14 @@
         }
     }
 
+    $completion = new completion_info($course);
     foreach($attemptids as $attemptid) {
-        if ($todelete = $DB->get_record('choice_answers', array('choiceid' => $choiceid, 'userid' => $attemptid))) {
-            $DB->delete_records('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' => $choice->id, 'userid' => $attemptid));
+            // Update completion state
+            if ($completion->is_enabled($cm) && $choice->completionsubmit) {
+                $completion->update_state($cm, COMPLETION_INCOMPLETE, $attemptid);
+            }
         }
     }
     return true;
@@ -772,6 +785,7 @@
         case FEATURE_GROUPMEMBERSONLY:        return true;
         case FEATURE_MOD_INTRO:               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_OUTCOMES:          return false;
         case FEATURE_BACKUP_MOODLE2:          return true;
@@ -809,3 +823,30 @@
         $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;
+    }
+}
Index: mod/choice/view.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/choice/view.php,v
retrieving revision 1.142
diff -u -r1.142 view.php
--- mod/choice/view.php	20 Aug 2010 02:40:13 -0000	1.142
+++ mod/choice/view.php	13 Oct 2010 14:04:03 -0000
@@ -38,6 +38,12 @@
     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))) {
             $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();
         if (has_capability('mod/choice:deleteresponses', $context)) {
             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");
             }
         }
@@ -58,7 +64,7 @@
         if (empty($answer)) {
             redirect("view.php?id=$cm->id", get_string('mustchooseone', 'choice'));
         } 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->notification(get_string('choicesaved', 'choice'),'notifysuccess');
Index: mod/choice/version.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/choice/version.php,v
retrieving revision 1.40
diff -u -r1.40 version.php
--- mod/choice/version.php	3 Aug 2010 10:07:21 -0000	1.40
+++ mod/choice/version.php	13 Oct 2010 14:04:03 -0000
@@ -5,7 +5,7 @@
 //  This fragment is called by /admin/index.php
 ////////////////////////////////////////////////////////////////////////////////
 
-$module->version  = 2010080300;
+$module->version  = 2010101300;
 $module->requires = 2010080300;  // Requires this Moodle version
 $module->cron     = 0;
 
Index: mod/choice/mod_form.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/choice/mod_form.php,v
retrieving revision 1.39
diff -u -r1.39 mod_form.php
--- mod/choice/mod_form.php	2 Sep 2010 18:29:40 -0000	1.39
+++ mod/choice/mod_form.php	13 Oct 2010 14:04:03 -0000
@@ -137,5 +137,27 @@
         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']);
+    }
 }
 
Index: mod/choice/db/upgrade.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/choice/db/upgrade.php,v
retrieving revision 1.13
diff -u -r1.13 upgrade.php
--- mod/choice/db/upgrade.php	18 Sep 2010 12:31:21 -0000	1.13
+++ mod/choice/db/upgrade.php	13 Oct 2010 14:04:03 -0000
@@ -53,6 +53,21 @@
         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;
 }
 
Index: mod/choice/db/install.xml
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/choice/db/install.xml,v
retrieving revision 1.6
diff -u -r1.6 install.xml
--- mod/choice/db/install.xml	1 May 2009 14:07:46 -0000	1.6
+++ mod/choice/db/install.xml	13 Oct 2010 14:04:03 -0000
@@ -1,5 +1,5 @@
 <?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"
     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="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="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="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="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="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>
       <KEYS>
         <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
@@ -28,6 +31,7 @@
         <INDEX NAME="course" UNIQUE="false" FIELDS="course"/>
       </INDEXES>
     </TABLE>
+
     <TABLE NAME="choice_options" COMMENT="available options to choice" PREVIOUS="choice" NEXT="choice_answers">
       <FIELDS>
         <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"/>
       </FIELDS>
       <KEYS>
+
         <KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="choiceid"/>
         <KEY NAME="choiceid" TYPE="foreign" FIELDS="choiceid" REFTABLE="choice" REFFIELDS="id" PREVIOUS="primary"/>
       </KEYS>
@@ -46,6 +51,7 @@
         <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="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="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="optionid"/>
       </FIELDS>
@@ -55,6 +61,7 @@
         <KEY NAME="optionid" TYPE="foreign" FIELDS="optionid" REFTABLE="choice_options" REFFIELDS="id" PREVIOUS="choiceid"/>
       </KEYS>
       <INDEXES>
+
         <INDEX NAME="userid" UNIQUE="false" FIELDS="userid"/>
       </INDEXES>
     </TABLE>
Index: mod/choice/lang/en/choice.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/choice/lang/en/choice.php,v
retrieving revision 1.10
diff -u -r1.10 choice.php
--- mod/choice/lang/en/choice.php	17 Aug 2010 17:35:13 -0000	1.10
+++ mod/choice/lang/en/choice.php	13 Oct 2010 14:04:03 -0000
@@ -26,6 +26,7 @@
 $string['addmorechoices'] = 'Add more choices';
 $string['allowupdate'] = 'Allow choice to be updated';
 $string['answered'] = 'Answered';
+$string['completionsubmit'] = 'Show as complete when user makes a choice';
 $string['displayhorizontal'] = 'Display horizontally';
 $string['displaymode'] = 'Display Mode';
 $string['displayvertical'] = 'Display vertically';
