MDL-40631: Prevent students from submitting empty assignments.
authorDamyon Wiese <damyon@moodle.com>
Fri, 26 Jul 2013 07:39:17 +0000 (15:39 +0800)
committerDamyon Wiese <damyon.wiese@gmail.com>
Mon, 2 Sep 2013 13:59:15 +0000 (21:59 +0800)
mod/assign/locallib.php
mod/assign/tests/locallib_test.php

index 51274f4..b26f125 100644 (file)
@@ -2723,11 +2723,13 @@ class assign {
             if ($teamsubmission) {
                 $showsubmit = $showedit &&
                               $teamsubmission &&
-                              ($teamsubmission->status == ASSIGN_SUBMISSION_STATUS_DRAFT);
+                              ($teamsubmission->status != ASSIGN_SUBMISSION_STATUS_SUBMITTED) &&
+                              !$this->submission_empty($teamsubmission);
             } else {
                 $showsubmit = $showedit &&
                               $submission &&
-                              ($submission->status == ASSIGN_SUBMISSION_STATUS_DRAFT);
+                              ($submission->status != ASSIGN_SUBMISSION_STATUS_SUBMITTED) &&
+                              !$this->submission_empty($submission);
             }
             if (!$this->get_instance()->submissiondrafts) {
                 $showsubmit = false;
@@ -3548,10 +3550,16 @@ class assign {
             }
 
             $showsubmit = ($submission || $teamsubmission) && $showlinks;
-            if ($teamsubmission && ($teamsubmission->status != ASSIGN_SUBMISSION_STATUS_DRAFT)) {
+            if ($teamsubmission && ($teamsubmission->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED)) {
                 $showsubmit = false;
             }
-            if ($submission && ($submission->status != ASSIGN_SUBMISSION_STATUS_DRAFT)) {
+            if ($teamsubmission && $this->submission_empty($teamsubmission)) {
+                $showsubmit = false;
+            }
+            if ($submission && ($submission->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED)) {
+                $showsubmit = false;
+            }
+            if ($submission && $this->submission_empty($submission)) {
                 $showsubmit = false;
             }
             if (!$this->get_instance()->submissiondrafts) {
@@ -5093,6 +5101,25 @@ class assign {
         return true;
     }
 
+    /**
+     * Determine if the current submission is empty or not.
+     *
+     * @param submission $submission the students submission record to check.
+     * @return bool
+     */
+    public function submission_empty($submission) {
+        $allempty = true;
+
+        foreach ($this->submissionplugins as $plugin) {
+            if ($plugin->is_enabled() && $plugin->is_visible()) {
+                if (!$allempty || !$plugin->is_empty($submission)) {
+                    $allempty = false;
+                }
+            }
+        }
+        return $allempty;
+    }
+
     /**
      * Save assignment submission.
      *
@@ -5137,7 +5164,6 @@ class assign {
                 return true;
             }
 
-            $allempty = true;
             $pluginerror = false;
             foreach ($this->submissionplugins as $plugin) {
                 if ($plugin->is_enabled() && $plugin->is_visible()) {
@@ -5145,11 +5171,9 @@ class assign {
                         $notices[] = $plugin->get_error();
                         $pluginerror = true;
                     }
-                    if (!$allempty || !$plugin->is_empty($submission)) {
-                        $allempty = false;
-                    }
                 }
             }
+            $allempty = $this->submission_empty($submission);
             if ($pluginerror || $allempty) {
                 if ($allempty) {
                     $notices[] = get_string('submissionempty', 'mod_assign');
index 774ca1c..564f8ef 100644 (file)
@@ -292,6 +292,43 @@ class mod_assign_locallib_testcase extends mod_assign_base_testcase {
         $this->assertEquals($now, $instance->duedate);
     }
 
+    public function test_cannot_submit_empty() {
+        global $PAGE;
+
+        $this->setUser($this->editingteachers[0]);
+        $assign = $this->create_instance(array('submissiondrafts'=>1));
+
+        $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
+
+        // Test you cannot see the submit button for an offline assignment regardless.
+        $this->setUser($this->students[0]);
+        $output = $assign->view_student_summary($this->students[0], true);
+        $this->assertNotContains(get_string('submitassignment', 'assign'), $output, 'Can submit empty offline assignment');
+
+        // Test you cannot see the submit button for an online text assignment with no submission.
+        $this->setUser($this->editingteachers[0]);
+        $instance = $assign->get_instance();
+        $instance->instance = $instance->id;
+        $instance->assignsubmission_onlinetext_enabled = 1;
+
+        $assign->update_instance($instance);
+        $this->setUser($this->students[0]);
+        $output = $assign->view_student_summary($this->students[0], true);
+        $this->assertNotContains(get_string('submitassignment', 'assign'), $output, 'Cannot submit empty onlinetext assignment');
+
+        // Simulate a submission.
+        $submission = $assign->get_user_submission($this->students[0]->id, true);
+        $data = new stdClass();
+        $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
+                                         'text'=>'Submission text',
+                                         'format'=>FORMAT_MOODLE);
+        $plugin = $assign->get_submission_plugin_by_type('onlinetext');
+        $plugin->save($submission, $data);
+        // Test you can see the submit button for an online text assignment with a submission.
+        $output = $assign->view_student_summary($this->students[0], true);
+        $this->assertContains(get_string('submitassignment', 'assign'), $output, 'Can submit non empty onlinetext assignment');
+    }
+
     public function test_list_participants() {
         $this->create_extra_users();
         $this->setUser($this->editingteachers[0]);