MDL-44598 assign: improve 'grant extension' form to show user names
authorJonathon Fowler <fowlerj@usq.edu.au>
Mon, 12 Oct 2015 02:50:33 +0000 (12:50 +1000)
committerJonathon Fowler <fowlerj@usq.edu.au>
Tue, 20 Oct 2015 02:19:16 +0000 (12:19 +1000)
Presents the first several students like the 'Set marking workflow
state' facility does, and processes the submitted ids through the
grading batch intermediary form. (For reference: MDL-42531)

mod/assign/extensionform.php
mod/assign/locallib.php
mod/assign/tests/behat/grant_extension.feature

index 0b39697..30d0376 100644 (file)
@@ -44,19 +44,25 @@ class mod_assign_extension_form extends moodleform {
      */
     public function definition() {
         $mform = $this->_form;
+        $params = $this->_customdata;
 
-        list($coursemoduleid, $userid, $batchusers, $instance, $data) = $this->_customdata;
         // Instance variable is used by the form validation function.
+        $instance = $params['instance'];
         $this->instance = $instance;
 
-        if ($batchusers) {
-            $listusersmessage = get_string('grantextensionforusers', 'assign', count(explode(',', $batchusers)));
-            $mform->addElement('static', 'applytoselectedusers', '', $listusersmessage);
+        if (!empty($params['userscount'])) {
+            $listusersmessage = get_string('grantextensionforusers', 'assign', $params['userscount']);
+            $mform->addElement('header', 'general', $listusersmessage);
+            $mform->addElement('static', 'userslist', get_string('selectedusers', 'assign'), $params['usershtml']);
+        } else {
+            $mform->addElement('static', 'userslist', '', $params['usershtml']);
         }
         if ($instance->allowsubmissionsfromdate) {
             $mform->addElement('static', 'allowsubmissionsfromdate', get_string('allowsubmissionsfromdate', 'assign'),
                                userdate($instance->allowsubmissionsfromdate));
         }
+
+        $finaldate = 0;
         if ($instance->duedate) {
             $mform->addElement('static', 'duedate', get_string('duedate', 'assign'), userdate($instance->duedate));
             $finaldate = $instance->duedate;
@@ -68,19 +74,17 @@ class mod_assign_extension_form extends moodleform {
         $mform->addElement('date_time_selector', 'extensionduedate',
                            get_string('extensionduedate', 'assign'), array('optional'=>true));
         $mform->setDefault('extensionduedate', $finaldate);
-        $mform->addElement('hidden', 'id', $coursemoduleid);
+
+        $mform->addElement('hidden', 'id');
         $mform->setType('id', PARAM_INT);
-        $mform->addElement('hidden', 'userid', $userid);
+        $mform->addElement('hidden', 'userid');
         $mform->setType('userid', PARAM_INT);
-        $mform->addElement('hidden', 'selectedusers', $batchusers);
+        $mform->addElement('hidden', 'selectedusers');
         $mform->setType('selectedusers', PARAM_SEQUENCE);
         $mform->addElement('hidden', 'action', 'saveextension');
         $mform->setType('action', PARAM_ALPHA);
-        $this->add_action_buttons(true, get_string('savechanges', 'assign'));
 
-        if ($data) {
-            $this->set_data($data);
-        }
+        $this->add_action_buttons(true, get_string('savechanges', 'assign'));
     }
 
     /**
index 6cfb48c..4074053 100644 (file)
@@ -2001,39 +2001,76 @@ class assign {
         require_once($CFG->dirroot . '/mod/assign/extensionform.php');
 
         $o = '';
-        $batchusers = optional_param('selectedusers', '', PARAM_SEQUENCE);
+
         $data = new stdClass();
-        $data->extensionduedate = null;
-        $userid = 0;
-        if (!$batchusers) {
-            $userid = required_param('userid', PARAM_INT);
+        $data->id = $this->get_course_module()->id;
 
-            $flags = $this->get_user_flags($userid, false);
+        $formparams = array(
+            'instance' => $this->get_instance()
+        );
+
+        $extrauserfields = get_extra_user_fields($this->get_context());
+
+        if ($mform) {
+            $submitteddata = $mform->get_data();
+            $users = $submitteddata->selectedusers;
+            $userlist = explode(',', $users);
+
+            $data->selectedusers = $users;
+            $data->userid = 0;
+
+            $usershtml = '';
+            $usercount = 0;
+            foreach ($userlist as $userid) {
+                if ($usercount >= 5) {
+                    $usershtml .= get_string('moreusers', 'assign', count($userlist) - 5);
+                    break;
+                }
+                $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
+
+                $usershtml .= $this->get_renderer()->render(new assign_user_summary($user,
+                                                                    $this->get_course()->id,
+                                                                    has_capability('moodle/site:viewfullnames',
+                                                                    $this->get_course_context()),
+                                                                    $this->is_blind_marking(),
+                                                                    $this->get_uniqueid_for_user($user->id),
+                                                                    $extrauserfields,
+                                                                    !$this->is_active_user($userid)));
+                $usercount += 1;
+            }
+
+            $formparams['userscount'] = count($userlist);
+            $formparams['usershtml'] = $usershtml;
 
+        } else {
+            $userid = required_param('userid', PARAM_INT);
             $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
+            $flags = $this->get_user_flags($userid, false);
 
+            $data->userid = $user->id;
             if ($flags) {
                 $data->extensionduedate = $flags->extensionduedate;
             }
-            $data->userid = $userid;
-        } else {
-            $data->batchusers = $batchusers;
+
+            $usershtml = $this->get_renderer()->render(new assign_user_summary($user,
+                                                                $this->get_course()->id,
+                                                                has_capability('moodle/site:viewfullnames',
+                                                                $this->get_course_context()),
+                                                                $this->is_blind_marking(),
+                                                                $this->get_uniqueid_for_user($user->id),
+                                                                $extrauserfields,
+                                                                !$this->is_active_user($userid)));
+            $formparams['usershtml'] = $usershtml;
         }
+
+        $mform = new mod_assign_extension_form(null, $formparams);
+        $mform->set_data($data);
         $header = new assign_header($this->get_instance(),
                                     $this->get_context(),
                                     $this->show_intro(),
                                     $this->get_course_module()->id,
                                     get_string('grantextension', 'assign'));
         $o .= $this->get_renderer()->render($header);
-
-        if (!$mform) {
-            $formparams = array($this->get_course_module()->id,
-                                $userid,
-                                $batchusers,
-                                $this->get_instance(),
-                                $data);
-            $mform = new mod_assign_extension_form(null, $formparams);
-        }
         $o .= $this->get_renderer()->render(new assign_form('extensionform', $mform));
         $o .= $this->view_footer();
         return $o;
@@ -3672,7 +3709,6 @@ class assign {
 
             if ($data->operation == 'grantextension') {
                 // Reset the form so the grant extension page will create the extension form.
-                $mform = null;
                 return 'grantextension';
             } else if ($data->operation == 'setmarkingworkflowstate') {
                 return 'viewbatchsetmarkingworkflowstate';
@@ -5244,34 +5280,34 @@ class assign {
         require_once($CFG->dirroot . '/mod/assign/extensionform.php');
         require_sesskey();
 
-        $batchusers = optional_param('selectedusers', '', PARAM_SEQUENCE);
-        $userid = 0;
-        if (!$batchusers) {
-            $userid = required_param('userid', PARAM_INT);
-            $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
-        }
-        $mform = new mod_assign_extension_form(null, array($this->get_course_module()->id,
-                                                           $userid,
-                                                           $batchusers,
-                                                           $this->get_instance(),
-                                                           null));
+        $formparams = array(
+            'instance' => $this->get_instance(),
+            'userscount' => 0,
+            'usershtml' => '',
+        );
+
+        $mform = new mod_assign_extension_form(null, $formparams);
 
         if ($mform->is_cancelled()) {
             return true;
         }
 
         if ($formdata = $mform->get_data()) {
-            if ($batchusers) {
-                $users = explode(',', $batchusers);
+            if (!empty($formdata->selectedusers)) {
+                $users = explode(',', $formdata->selectedusers);
                 $result = true;
                 foreach ($users as $userid) {
-                    $result = $this->save_user_extension($userid, $formdata->extensionduedate) && $result;
+                    $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
+                    $result = $this->save_user_extension($user->id, $formdata->extensionduedate) && $result;
                 }
                 return $result;
-            } else {
-                return $this->save_user_extension($userid, $formdata->extensionduedate);
+            }
+            if (!empty($formdata->userid)) {
+                $user = $DB->get_record('user', array('id' => $formdata->userid), '*', MUST_EXIST);
+                return $this->save_user_extension($user->id, $formdata->extensionduedate);
             }
         }
+
         return false;
     }
 
index fffc415..3617275 100644 (file)
@@ -13,11 +13,19 @@ Feature: Grant an extension to an offline student
       | teacher1 | Teacher | 1 | teacher1@example.com |
       | student1 | Student | 1 | student1@example.com |
       | student2 | Student | 2 | student2@example.com |
+      | student3 | Student | 3 | student3@example.com |
+      | student4 | Student | 4 | student4@example.com |
+      | student5 | Student | 5 | student5@example.com |
+      | student6 | Student | 6 | student6@example.com |
     And the following "course enrolments" exist:
       | user | course | role |
       | teacher1 | C1 | editingteacher |
       | student1 | C1 | student |
       | student2 | C1 | student |
+      | student3 | C1 | student |
+      | student4 | C1 | student |
+      | student5 | C1 | student |
+      | student6 | C1 | student |
 
   @javascript
   Scenario: Granting an extension to an offline assignment
@@ -30,6 +38,7 @@ Feature: Grant an extension to an offline student
     When I follow "View/grade all submissions"
     And I click on "Edit" "link" in the "Student 1" "table_row"
     And I follow "Grant extension"
+    And I should see "Student 1 (student1@example.com)"
     And I set the field "Enable" to "1"
     And I press "Save changes"
     Then I should see "Extension granted until:" in the "Student 1" "table_row"
@@ -51,10 +60,20 @@ Feature: Grant an extension to an offline student
     And I set the field "selectall" to "1"
     And I set the field "operation" to "Grant extension"
     And I click on "Go" "button" confirming the dialogue
+    And I should see "Student 1 (student1@example.com)"
+    And I should see "Student 2 (student2@example.com)"
+    And I should see "Student 3 (student3@example.com)"
+    And I should see "Student 4 (student4@example.com)"
+    And I should see "Student 5 (student5@example.com)"
+    And I should see "1 more..."
     And I set the field "Enable" to "1"
     And I press "Save changes"
     Then I should see "Extension granted until:" in the "Student 1" "table_row"
     And I should see "Extension granted until:" in the "Student 2" "table_row"
+    And I should see "Extension granted until:" in the "Student 3" "table_row"
+    And I should see "Extension granted until:" in the "Student 4" "table_row"
+    And I should see "Extension granted until:" in the "Student 5" "table_row"
+    And I should see "Extension granted until:" in the "Student 6" "table_row"
     And I log out
     And I log in as "student1"
     And I follow "Course 1"