Skip to content

Commit

Permalink
MDL-71252 completion: Avoid showing the 'Mark as done' button
Browse files Browse the repository at this point in the history
The following activites will be applied:
 - Label
 - File (Force download/Open/In pop-up)
 - URL (Open/In pop-up)
  • Loading branch information
HuongNV13 committed Apr 26, 2021
1 parent 146a38d commit 3f9035b
Show file tree
Hide file tree
Showing 15 changed files with 539 additions and 10 deletions.
11 changes: 11 additions & 0 deletions completion/classes/activity_custom_completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ public function get_custom_rule_description(string $rule): string {
return $descriptions[$rule];
}

/**
* Show the manual completion or not regardless of the course's showcompletionconditions setting.
* Returns false by default for plugins that don't need to override the course's showcompletionconditions setting.
* Activity plugins that need to always show manual completion need to override this function.
*
* @return bool
*/
public function manual_completion_always_shown(): bool {
return false;
}

/**
* Fetches the module's custom completion class implementation if it's available.
*
Expand Down
38 changes: 32 additions & 6 deletions completion/classes/cm_completion_details.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class cm_completion_details {
/** @var bool Whether to return automatic completion details. */
protected $returndetails = true;

/** @var activity_custom_completion Activity custom completion object. */
protected $cmcompletion = null;

/**
* Constructor.
*
Expand All @@ -62,6 +65,10 @@ public function __construct(completion_info $completioninfo, cm_info $cminfo, in
$this->cminfo = $cminfo;
$this->userid = $userid;
$this->returndetails = $returndetails;
$cmcompletionclass = activity_custom_completion::get_cm_completion_class($this->cminfo->modname);
if ($cmcompletionclass) {
$this->cmcompletion = new $cmcompletionclass($this->cminfo, $this->userid);
}
}

/**
Expand Down Expand Up @@ -116,16 +123,12 @@ public function get_details(): array {
];
}

// Check if this activity has custom_completion class implemented.
$cmcompletionclass = activity_custom_completion::get_cm_completion_class($this->cminfo->modname);
if ($cmcompletionclass) {
if ($this->cmcompletion) {
if (isset($completiondata->customcompletion)) {
/** @var activity_custom_completion $cmcompletion */
$cmcompletion = new $cmcompletionclass($this->cminfo, $this->userid);
foreach ($completiondata->customcompletion as $rule => $status) {
$details[$rule] = (object)[
'status' => !$hasoverride ? $status : $completiondata->completionstate,
'description' => $cmcompletion->get_custom_rule_description($rule),
'description' => $this->cmcompletion->get_custom_rule_description($rule),
];
}
}
Expand Down Expand Up @@ -193,6 +196,29 @@ public function is_tracked_user(): bool {
return $this->completioninfo->is_tracked_user($this->userid);
}

/**
* Determine whether to show the manual completion or not.
*
* @return bool
*/
public function show_manual_completion(): bool {
global $PAGE;

if ($PAGE->context->contextlevel == CONTEXT_MODULE) {
// Manual completion should always be shown on the activity page.
return true;
} else {
$course = $this->cminfo->get_course();
if ($course->showcompletionconditions == COMPLETION_SHOW_CONDITIONS) {
return true;
} else if ($this->cmcompletion) {
return $this->cmcompletion->manual_completion_always_shown();
}
}

return false;
}

/**
* Generates an instance of this class.
*
Expand Down
26 changes: 26 additions & 0 deletions completion/tests/behat/behat_completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,32 @@ public function the_manual_completion_button_for_activity_should_be_disabled(str
$this->execute("behat_general::the_element_should_be_disabled", $params);
}

/**
* Check that the manual completion button for the activity does not exist.
*
* @Given /^the manual completion button for "(?P<activityname>(?:[^"]|\\")*)" should not exist/
* @param string $activityname The activity name.
*/
public function the_manual_completion_button_for_activity_should_not_exist(string $activityname): void {
$selector = "div[data-activityname='$activityname'] button";

$params = [$selector, "css_element"];
$this->execute('behat_general::should_not_exist', $params);
}

/**
* Check that the manual completion button for the activity exists.
*
* @Given /^the manual completion button for "(?P<activityname>(?:[^"]|\\")*)" should exist/
* @param string $activityname The activity name.
*/
public function the_manual_completion_button_for_activity_should_exist(string $activityname): void {
$selector = "div[data-activityname='$activityname'] button";

$params = [$selector, "css_element"];
$this->execute('behat_general::should_exist', $params);
}

/**
* Check that the activity has the given automatic completion condition.
*
Expand Down
1 change: 1 addition & 0 deletions course/classes/output/activity_information.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ protected function build_completion_data(): stdClass {

$data->hascompletion = $this->cmcompletion->has_completion();
$data->isautomatic = $this->cmcompletion->is_automatic();
$data->showmanualcompletion = $this->cmcompletion->show_manual_completion();

// Get the name of the user overriding the completion condition, if available.
$data->overrideby = null;
Expand Down
5 changes: 4 additions & 1 deletion course/templates/activity_info.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"hasdates": true,
"isautomatic": true,
"istrackeduser": true,
"showmanualcompletion": true,
"activitydates": [
{
"label": "Opens:",
Expand Down Expand Up @@ -67,7 +68,9 @@
</div>
{{/isautomatic}}
{{^isautomatic}}
{{> core_course/completion_manual }}
{{#showmanualcompletion}}
{{> core_course/completion_manual }}
{{/showmanualcompletion}}
{{/isautomatic}}
</div>
{{/uservisible}}
Expand Down
63 changes: 63 additions & 0 deletions mod/assign/tests/behat/assign_activity_completion.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
@mod @mod_assign @core_completion
Feature: View activity completion information in the Assign resource
In order to have visibility of assign completion requirements
As a student
I need to be able to view my assign completion progress

Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| student1 | Vinnie | Student1 | student1@example.com |
| teacher1 | Darrell | Teacher1 | teacher1@example.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "course enrolments" exist:
| user | course | role |
| student1 | C1 | student |
| teacher1 | C1 | editingteacher |
And I log in as "teacher1"

@javascript
Scenario: The manual completion button will be shown on the course page if the Show completion conditions is set to Yes
Given I am on "Course 1" course homepage with editing mode on
And I navigate to "Edit settings" in current page administration
And I expand all fieldsets
And I set the following fields to these values:
| Enable completion tracking | Yes |
| Show completion conditions | Yes |
And I press "Save and display"
And I add a "Assignment" to section "1" and I fill the form with:
| Assignment name | History of ants |
| Completion tracking | Students can manually mark the activity as completed |
# Teacher view.
And the manual completion button for "History of ants" should exist
And the manual completion button for "History of ants" should be disabled
And I log out
# Student view.
When I log in as "student1"
And I am on "Course 1" course homepage
Then the manual completion button for "History of ants" should exist
And the manual completion button of "History of ants" is displayed as "Mark as done"
And I toggle the manual completion state of "History of ants"
And the manual completion button of "History of ants" is displayed as "Done"

@javascript
Scenario: The manual completion button will not be shown on the course page if the Show completion conditions is set to No
Given I am on "Course 1" course homepage with editing mode on
And I navigate to "Edit settings" in current page administration
And I expand all fieldsets
And I set the following fields to these values:
| Enable completion tracking | Yes |
| Show completion conditions | No |
And I press "Save and display"
And I add a "Assignment" to section "1" and I fill the form with:
| Assignment name | History of ants |
| Completion tracking | Students can manually mark the activity as completed |
# Teacher view.
And the manual completion button for "History of ants" should not exist
And I log out
# Student view.
When I log in as "student1"
And I am on "Course 1" course homepage
Then the manual completion button for "History of ants" should not exist
73 changes: 73 additions & 0 deletions mod/label/classes/completion/custom_completion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

declare(strict_types=1);

namespace mod_label\completion;

use core_completion\activity_custom_completion;

/**
* Activity custom completion subclass for the label.
*
* Class for defining mod_label's custom completion rules and fetching the completion statuses
* of the custom completion rules for a given label instance and a user.
*
* @package mod_label
* @copyright 2021 Huong Nguyen <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class custom_completion extends activity_custom_completion {

/**
* Fetches the completion state for a given completion rule.
*
* @param string $rule The completion rule.
* @return int The completion state.
*/
public function get_state(string $rule): int {
return COMPLETION_UNKNOWN;
}

/**
* Fetch the list of custom completion rules that this module defines.
*
* @return array
*/
public static function get_defined_custom_rules(): array {
// This activity/resource do not have any custom rules.
return [];
}

/**
* Returns an associative array of the descriptions of custom completion rules.
*
* @return array
*/
public function get_custom_rule_descriptions(): array {
// This activity/resource do not have any custom rule descriptions.
return [];
}

/**
* Show the manual completion or not regardless of the course's showcompletionconditions setting.
*
* @return bool
*/
public function manual_completion_always_shown(): bool {
return true;
}
}
45 changes: 45 additions & 0 deletions mod/label/tests/behat/label_activity_completion.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@mod @mod_label @core_completion
Feature: View activity completion information for the label
In order to have visibility of Label completion requirements
As a student
I need to be able to view my Label completion progress

Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| student1 | Vinnie | Student1 | student1@example.com |
| teacher1 | Darrell | Teacher1 | teacher1@example.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "course enrolments" exist:
| user | course | role |
| student1 | C1 | student |
| teacher1 | C1 | editingteacher |
And I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to "Edit settings" in current page administration
And I expand all fieldsets
And I set the following fields to these values:
| Enable completion tracking | Yes |
| Show completion conditions | No |
And I press "Save and display"

@javascript
Scenario: The manual completion button will be shown on the course page if the Show completion conditions is set to No
Given I am on "Course 1" course homepage with editing mode on
When I add a "label" to section "1" and I fill the form with:
| Label text | Swanky label |
| Availability | Show on course page |
| Completion tracking | Students can manually mark the activity as completed |
# Teacher view.
And the manual completion button for "Swanky label" should exist
And the manual completion button for "Swanky label" should be disabled
And I log out
# Student view.
When I log in as "student1"
And I am on "Course 1" course homepage
Then the manual completion button for "Swanky label" should exist
And the manual completion button of "Swanky label" is displayed as "Mark as done"
And I toggle the manual completion state of "Swanky label"
And the manual completion button of "Swanky label" is displayed as "Done"
20 changes: 20 additions & 0 deletions mod/page/tests/behat/page_activity_completion.feature
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,23 @@ Feature: View activity completion information in the Page resource
Then the manual completion button of "Music history" is displayed as "Mark as done"
And I toggle the manual completion state of "Music history"
And the manual completion button of "Music history" is displayed as "Done"

Scenario: The manual completion button will not be shown on the course page if the Show completion conditions is set to No
Given I am on "Course 1" course homepage with editing mode on
And I navigate to "Edit settings" in current page administration
And I expand all fieldsets
And I set the following fields to these values:
| Enable completion tracking | Yes |
| Show completion conditions | No |
And I press "Save and display"
And I add a "Page" to section "1" and I fill the form with:
| Name | Music history |
| Page content | A lesson learned in life |
| Completion tracking | Students can manually mark the activity as completed |
# Teacher view.
And the manual completion button for "Music history" should not exist
And I log out
# Student view.
When I log in as "student1"
And I am on "Course 1" course homepage
Then the manual completion button for "Music history" should not exist
Loading

0 comments on commit 3f9035b

Please sign in to comment.