diff --git a/mod/survey/backup/moodle2/backup_survey_stepslib.php b/mod/survey/backup/moodle2/backup_survey_stepslib.php index ef042d702c7..5bd3b08c380 100644 --- a/mod/survey/backup/moodle2/backup_survey_stepslib.php +++ b/mod/survey/backup/moodle2/backup_survey_stepslib.php @@ -39,7 +39,7 @@ class backup_survey_activity_structure_step extends backup_activity_structure_st // Define each element separated $survey = new backup_nested_element('survey', array('id'), array( 'name', 'intro', 'introformat', 'template', - 'questions', 'days', 'timecreated', 'timemodified')); + 'questions', 'days', 'timecreated', 'timemodified', 'completionsubmit')); $answers = new backup_nested_element('answers'); diff --git a/mod/survey/db/install.xml b/mod/survey/db/install.xml index 07831a56969..ebf7e040333 100644 --- a/mod/survey/db/install.xml +++ b/mod/survey/db/install.xml @@ -1,5 +1,5 @@ - @@ -16,6 +16,7 @@ + diff --git a/mod/survey/db/upgrade.php b/mod/survey/db/upgrade.php index 004fc6d871e..6347e378d70 100644 --- a/mod/survey/db/upgrade.php +++ b/mod/survey/db/upgrade.php @@ -22,8 +22,8 @@ defined('MOODLE_INTERNAL') || die(); function xmldb_survey_upgrade($oldversion) { - global $CFG; - + global $DB; + $dbman = $DB->get_manager(); // Loads ddl manager and xmldb classes. // Moodle v2.8.0 release upgrade line. // Put any upgrade step following this. @@ -35,6 +35,20 @@ function xmldb_survey_upgrade($oldversion) { // Moodle v3.1.0 release upgrade line. // Put any upgrade step following this. + if ($oldversion < 2016061400) { + + // Define field completionsubmit to be added to survey. + $table = new xmldb_table('survey'); + $field = new xmldb_field('completionsubmit', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'questions'); + + // Conditionally launch add field completionsubmit. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Survey savepoint reached. + upgrade_mod_savepoint(true, 2016061400, 'survey'); + } return true; } diff --git a/mod/survey/lang/en/survey.php b/mod/survey/lang/en/survey.php index 849d2a002a4..10712fe9718 100644 --- a/mod/survey/lang/en/survey.php +++ b/mod/survey/lang/en/survey.php @@ -82,6 +82,7 @@ $string['attls9short'] = 'argue with authors'; $string['cannotfindanswer'] = 'There are no answers for this survey yet.'; $string['cannotfindquestion'] = 'Question doesn\'t exist'; $string['cannotfindsurveytmpt'] = 'No survey templates found!'; +$string['completionsubmit'] = 'Student must submit to this activity to complete it'; $string['ciqintro'] = 'While thinking about recent events in this class, answer the questions below.'; $string['ciqname'] = 'Critical incidents'; $string['ciq1'] = 'At what moment in class were you most engaged as a learner?'; diff --git a/mod/survey/lib.php b/mod/survey/lib.php index 9d57fb36df0..a3e7756a479 100644 --- a/mod/survey/lib.php +++ b/mod/survey/lib.php @@ -785,6 +785,7 @@ function survey_supports($feature) { case FEATURE_GROUPINGS: 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; @@ -1008,6 +1009,13 @@ function survey_save_answers($survey, $answersrawdata, $course, $context) { $DB->insert_records("survey_answers", $answerstoinsert); } + // Update completion state. + $cm = get_coursemodule_from_instance('survey', $survey->id, $course->id); + $completion = new completion_info($course); + if (isloggedin() && !isguestuser() && $completion->is_enabled($cm) && $survey->completionsubmit) { + $completion->update_state($cm, COMPLETION_COMPLETE); + } + $params = array( 'context' => $context, 'courseid' => $course->id, @@ -1016,3 +1024,29 @@ function survey_save_answers($survey, $answersrawdata, $course, $context) { $event = \mod_survey\event\response_submitted::create($params); $event->trigger(); } + +/** + * Obtains the automatic completion state for this survey based on the condition + * in feedback 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 survey_get_completion_state($course, $cm, $userid, $type) { + global $DB; + + // Get survey details. + $survey = $DB->get_record('survey', array('id' => $cm->instance), '*', MUST_EXIST); + + // If completion option is enabled, evaluate it and return true/false. + if ($survey->completionsubmit) { + $params = array('userid' => $userid, 'survey' => $survey->id); + return $DB->record_exists('survey_answers', $params); + } else { + // Completion option is not enabled so just return $type. + return $type; + } +} diff --git a/mod/survey/mod_form.php b/mod/survey/mod_form.php index 19219a14b9f..d59d3c41d66 100644 --- a/mod/survey/mod_form.php +++ b/mod/survey/mod_form.php @@ -46,6 +46,46 @@ class mod_survey_mod_form extends moodleform_mod { $this->add_action_buttons(); } + /** + * Return submitted data if properly submitted or returns NULL if validation fails or + * if there is no submitted data. + * + * @return stdClass submitted data; NULL if not valid or not submitted or cancelled + */ + public function get_data() { + $data = parent::get_data(); + if (!$data) { + return false; + } + if (!empty($data->completionunlocked)) { + // Turn off completion settings if the checkboxes aren't ticked. + $autocompletion = !empty($data->completion) && + $data->completion == COMPLETION_TRACKING_AUTOMATIC; + if (!$autocompletion || empty($data->completionsubmit)) { + $data->completionsubmit = 0; + } + } + return $data; + } + + /** + * Add completion rules to form. + * @return array + */ + public function add_completion_rules() { + $mform =& $this->_form; + $mform->addElement('checkbox', 'completionsubmit', '', get_string('completionsubmit', 'survey')); + return array('completionsubmit'); + } + + /** + * Enable completion rules + * @param stdclass $data + * @return array + */ + public function completion_rule_enabled($data) { + return !empty($data['completionsubmit']); + } } diff --git a/mod/survey/tests/behat/survey_completion.feature b/mod/survey/tests/behat/survey_completion.feature new file mode 100644 index 00000000000..33ebfaf7241 --- /dev/null +++ b/mod/survey/tests/behat/survey_completion.feature @@ -0,0 +1,46 @@ +@mod @mod_survey +Feature: A teacher can use activity completion to track a student progress + In order to use activity completion + As a teacher + I need to set survey activities and enable activity completion + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@example.com | + And the following "courses" exist: + | fullname | shortname | category | enablecompletion | + | Course 1 | C1 | 0 | 1 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + And I log in as "teacher1" + And I follow "Course 1" + And I turn editing mode on + + Scenario: Require survey view + Given I add a "Survey" to section "1" and I fill the form with: + | Name | Test survey name | + | Survey type | Critical incidents | + | Description | Test survey description | + | Completion tracking | Show activity as complete when conditions are met | + | id_completionview | 1 | + And I turn editing mode off + And the "Test survey name" "survey" activity with "auto" completion should be marked as not complete + When I follow "Test survey name" + And I follow "Course 1" + Then the "Test survey name" "survey" activity with "auto" completion should be marked as complete + + Scenario: Require survey submission + Given I add a "Survey" to section "1" and I fill the form with: + | Name | Test survey name | + | Survey type | Critical incidents | + | Description | Test survey description | + | Completion tracking | Show activity as complete when conditions are met | + | id_completionsubmit | 1 | + And I turn editing mode off + And the "Test survey name" "survey" activity with "auto" completion should be marked as not complete + When I follow "Test survey name" + And I press "Click here to continue" + And I follow "Course 1" + Then the "Test survey name" "survey" activity with "auto" completion should be marked as complete diff --git a/mod/survey/version.php b/mod/survey/version.php index bdd0297dfa5..cba05bfeb6f 100644 --- a/mod/survey/version.php +++ b/mod/survey/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2016052300; // The current module version (Date: YYYYMMDDXX) +$plugin->version = 2016061400; // The current module version (Date: YYYYMMDDXX) $plugin->requires = 2016051900; // Requires this Moodle version $plugin->component = 'mod_survey'; // Full name of the plugin (used for diagnostics) $plugin->cron = 0;