MDL-59248 mod_workshop: New API check_examples_assessed_before_assessment
authorJuan Leyva <juanleyvadelgado@gmail.com>
Tue, 27 Jun 2017 10:06:23 +0000 (11:06 +0100)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Tue, 3 Oct 2017 06:51:29 +0000 (08:51 +0200)
For consistency I renamed the old check_examples_assessed function to
check_examples_assessed_before_submission.
I also call the new function in the existing
get_workshop_access_information external function.

mod/workshop/assessment.php
mod/workshop/classes/external.php
mod/workshop/locallib.php
mod/workshop/submission.php
mod/workshop/tests/external_test.php

index df504ff..a4efdc7 100644 (file)
@@ -69,31 +69,15 @@ if ($isreviewer and $workshop->assessing_allowed($USER->id)) {
 }
 
 // check that all required examples have been assessed by the user
-if ($assessmenteditable and $workshop->useexamples and $workshop->examplesmode == workshop::EXAMPLES_BEFORE_ASSESSMENT
-        and !has_capability('mod/workshop:manageexamples', $workshop->context)) {
-    // the reviewer must have submitted their own submission
-    $reviewersubmission = $workshop->get_submission_by_author($assessment->reviewerid);
-    $output = $PAGE->get_renderer('mod_workshop');
-    if (!$reviewersubmission) {
-        // no money, no love
-        $assessmenteditable = false;
+if ($assessmenteditable) {
+
+    list($assessed, $notice) = $workshop->check_examples_assessed_before_assessment($assessment->reviewerid);
+    if (!$assessed) {
         echo $output->header();
         echo $output->heading(format_string($workshop->name));
-        notice(get_string('exampleneedsubmission', 'workshop'), new moodle_url('/mod/workshop/view.php', array('id' => $cm->id)));
+        notice(get_string($notice, 'workshop'), new moodle_url('/mod/workshop/view.php', array('id' => $cm->id)));
         echo $output->footer();
         exit;
-    } else {
-        $examples = $workshop->get_examples_for_reviewer($assessment->reviewerid);
-        foreach ($examples as $exampleid => $example) {
-            if (is_null($example->grade)) {
-                $assessmenteditable = false;
-                echo $output->header();
-                echo $output->heading(format_string($workshop->name));
-                notice(get_string('exampleneedassessed', 'workshop'), new moodle_url('/mod/workshop/view.php', array('id' => $cm->id)));
-                echo $output->footer();
-                exit;
-            }
-        }
     }
 }
 
index 0d57439..78955bd 100644 (file)
@@ -199,7 +199,8 @@ class mod_workshop_external extends external_api {
         if (is_null($result['assessingexamplesallowed'])) {
             $result['assessingexamplesallowed'] = false;
         }
-        $result['examplesassessed'] = $workshop->check_examples_assessed($USER->id);
+        $result['examplesassessedbeforesubmission'] = $workshop->check_examples_assessed_before_submission($USER->id);
+        list($result['examplesassessedbeforeassessment'], $code) = $workshop->check_examples_assessed_before_assessment($USER->id);
 
         $result['warnings'] = array();
         return $result;
@@ -222,8 +223,12 @@ class mod_workshop_external extends external_api {
                 'Is the user allowed to create/edit his assessments?'),
             'assessingexamplesallowed' => new external_value(PARAM_BOOL,
                 'Are reviewers allowed to create/edit their assessments of the example submissions?.'),
-            'examplesassessed' => new external_value(PARAM_BOOL,
-                'Whether the given user has assessed all his required examples (always true if there are no examples to assess).'),
+            'examplesassessedbeforesubmission' => new external_value(PARAM_BOOL,
+                'Whether the given user has assessed all his required examples before submission
+                (always true if there are not examples to assess or not configured to check before submission).'),
+            'examplesassessedbeforeassessment' => new external_value(PARAM_BOOL,
+                'Whether the given user has assessed all his required examples before assessment
+                (always true if there are not examples to assessor not configured to check before assessment).'),
             'warnings' => new external_warnings()
         );
 
@@ -476,7 +481,7 @@ class mod_workshop_external extends external_api {
 
         // Check if we can submit now.
         $canaddsubmission = $workshop->creating_submission_allowed($USER->id);
-        $canaddsubmission = $canaddsubmission && $workshop->check_examples_assessed($USER->id);
+        $canaddsubmission = $canaddsubmission && $workshop->check_examples_assessed_before_submission($USER->id);
         if (!$canaddsubmission) {
             throw new moodle_exception('nopermissions', 'error', '', 'add submission');
         }
@@ -592,7 +597,7 @@ class mod_workshop_external extends external_api {
         // Check if we can update the submission.
         $canupdatesubmission = $submission->authorid == $USER->id;
         $canupdatesubmission = $canupdatesubmission && $workshop->modifying_submission_allowed($USER->id);
-        $canupdatesubmission = $canupdatesubmission && $workshop->check_examples_assessed($USER->id);
+        $canupdatesubmission = $canupdatesubmission && $workshop->check_examples_assessed_before_submission($USER->id);
         if (!$canupdatesubmission) {
             throw new moodle_exception('nopermissions', 'error', '', 'update submission');
         }
index 0a6cb3c..383ce61 100644 (file)
@@ -2767,13 +2767,13 @@ class workshop {
     }
 
     /**
-     * Check whether the given user has assessed all his required examples.
+     * Check whether the given user has assessed all his required examples before submission.
      *
      * @param  int $userid the user to check
      * @return bool        false if there are examples missing assessment, true otherwise.
      * @since  Moodle 3.4
      */
-    public function check_examples_assessed($userid) {
+    public function check_examples_assessed_before_submission($userid) {
 
         if ($this->useexamples and $this->examplesmode == self::EXAMPLES_BEFORE_SUBMISSION
             and !has_capability('mod/workshop:manageexamples', $this->context)) {
@@ -2792,6 +2792,35 @@ class workshop {
         return true;
     }
 
+    /**
+     * Check that all required examples have been assessed by the given user.
+     *
+     * @param  stdClass $userid     the user (reviewer) to check
+     * @return mixed bool|state     false and notice code if there are examples missing assessment, true otherwise.
+     * @since  Moodle 3.4
+     */
+    public function check_examples_assessed_before_assessment($userid) {
+
+        if ($this->useexamples and $this->examplesmode == self::EXAMPLES_BEFORE_ASSESSMENT
+                and !has_capability('mod/workshop:manageexamples', $this->context)) {
+
+            // The reviewer must have submitted their own submission.
+            $reviewersubmission = $this->get_submission_by_author($userid);
+            if (!$reviewersubmission) {
+                // No money, no love.
+                return array(false, 'exampleneedsubmission');
+            } else {
+                $examples = $this->get_examples_for_reviewer($userid);
+                foreach ($examples as $exampleid => $example) {
+                    if (is_null($example->grade)) {
+                        return array(false, 'exampleneedassessed');
+                    }
+                }
+            }
+        }
+        return array(true, null);
+    }
+
     /**
      * Trigger module viewed event and set the module viewed for completion.
      *
index 0772b96..f93967b 100644 (file)
@@ -103,7 +103,7 @@ if ($submission->id and !$workshop->modifying_submission_allowed($USER->id)) {
 
 $canviewall = $canviewall && $workshop->check_group_membership($submission->authorid);
 
-$editable = $editable && $workshop->check_examples_assessed($USER->id);
+$editable = ($editable && $workshop->check_examples_assessed_before_submission($USER->id));
 $edit = ($editable and $edit);
 
 if (!$candeleteall and $ownsubmission and $editable) {
index 17e30e3..5992fe5 100644 (file)
@@ -242,7 +242,8 @@ class mod_workshop_external_testcase extends externallib_advanced_testcase {
         $this->assertFalse($result['modifyingsubmissionallowed']);
         $this->assertFalse($result['assessingallowed']);
         $this->assertFalse($result['assessingexamplesallowed']);
-        $this->assertTrue($result['examplesassessed']);
+        $this->assertTrue($result['examplesassessedbeforesubmission']);
+        $this->assertTrue($result['examplesassessedbeforeassessment']);
 
         // Switch phase.
         $workshop = new workshop($this->workshop, $this->cm, $this->course);
@@ -254,7 +255,8 @@ class mod_workshop_external_testcase extends externallib_advanced_testcase {
         $this->assertTrue($result['modifyingsubmissionallowed']);
         $this->assertFalse($result['assessingallowed']);
         $this->assertFalse($result['assessingexamplesallowed']);
-        $this->assertTrue($result['examplesassessed']);
+        $this->assertTrue($result['examplesassessedbeforesubmission']);
+        $this->assertTrue($result['examplesassessedbeforeassessment']);
 
         // Switch to next (to assessment).
         $workshop = new workshop($this->workshop, $this->cm, $this->course);
@@ -266,7 +268,8 @@ class mod_workshop_external_testcase extends externallib_advanced_testcase {
         $this->assertFalse($result['modifyingsubmissionallowed']);
         $this->assertTrue($result['assessingallowed']);
         $this->assertFalse($result['assessingexamplesallowed']);
-        $this->assertTrue($result['examplesassessed']);
+        $this->assertTrue($result['examplesassessedbeforesubmission']);
+        $this->assertTrue($result['examplesassessedbeforeassessment']);
     }
 
     /**