MDL-58139 completion: bulk update completion

Part of MDL-58138 epic
This commit is contained in:
Marina Glancy 2017-03-15 10:35:51 +08:00 committed by Jake Dallimore
parent 0cbc248dd3
commit 06cdda468a
18 changed files with 896 additions and 61 deletions

View File

@ -0,0 +1,345 @@
<?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/>.
/**
* Bulk activity completion form
*
* @package core_completion
* @copyright 2017 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
require_once ($CFG->libdir.'/formslib.php');
require_once($CFG->libdir.'/completionlib.php');
require_once($CFG->dirroot.'/course/modlib.php');
/**
* Bulk activity completion form
*
* @package core_completion
* @copyright 2017 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_completion_bulkedit_form extends moodleform {
/** @var cm_info[] list of selected course modules */
protected $cms = [];
/** @var array Do not use directly, call $this->get_module_names() */
protected $_modnames = null;
/** @var moodleform_mod Do not use directly, call $this->get_module_form() */
protected $_moduleform = null;
/** @var bool */
protected $hascustomrules = false;
/**
* Returns list of types of selected modules
*
* @return array modname=>modfullname
*/
protected function get_module_names() {
if ($this->_modnames !== null) {
return $this->_modnames;
}
$this->_modnames = [];
foreach ($this->cms as $cm) {
$this->_modnames[$cm->modname] = $cm->modfullname;
}
return $this->_modnames;
}
/**
* Returns true if all selected modules support tracking view.
*
* @return bool
*/
protected function support_views() {
foreach ($this->get_module_names() as $modname => $modfullname) {
if (!plugin_supports('mod', $modname, FEATURE_COMPLETION_TRACKS_VIEWS, false)) {
return false;
}
}
return true;
}
/**
* Returns true if all selected modules support grading.
*
* @return bool
*/
protected function support_grades() {
foreach ($this->get_module_names() as $modname => $modfullname) {
if (!plugin_supports('mod', $modname, FEATURE_GRADE_HAS_GRADE, false)) {
return false;
}
}
return true;
}
/**
* Returns an instance of component-specific module form for the first selected module
*
* @return moodleform_mod|null
*/
protected function get_module_form() {
global $CFG, $PAGE;
if ($this->_moduleform) {
return $this->_moduleform;
}
$cm = reset($this->cms);
$modname = $cm->modname;
$course = $cm->get_course();
$modmoodleform = "$CFG->dirroot/mod/$modname/mod_form.php";
if (file_exists($modmoodleform)) {
require_once($modmoodleform);
} else {
print_error('noformdesc');
}
list($module, $context, $cw, $cmrec, $data) = prepare_new_moduleinfo_data($course, $modname, 0);
//list($cm, $context, $module, $data, $cw) = get_moduleinfo_data($cm->get_course_module_record(), $course);
$data->return = 0;
$data->sr = 0;
$data->add = $modname;
// Initialise the form but discard all JS requirements it adds, our form has already added them.
$mformclassname = 'mod_'.$modname.'_mod_form';
if (!defined('PHPUNIT_TEST') || !PHPUNIT_TEST) {
$PAGE->start_collecting_javascript_requirements();
}
$this->_moduleform = new $mformclassname($data, 0, $cmrec, $course);
if (!defined('PHPUNIT_TEST') || !PHPUNIT_TEST) {
$PAGE->end_collecting_javascript_requirements();
}
return $this->_moduleform;
}
/**
* If all selected modules are of the same module type, adds custom completion rules from this module type
*
* @return array
*/
protected function add_custom_completion_rules() {
$modnames = array_keys($this->get_module_names());
if (count($modnames) != 1 || !plugin_supports('mod', $modnames[0], FEATURE_COMPLETION_HAS_RULES, false)) {
return [];
}
try {
// Add completion rules from the module form to this form.
$moduleform = $this->get_module_form();
$moduleform->_form = $this->_form;
if ($customcompletionelements = $moduleform->add_completion_rules()) {
$this->hascustomrules = true;
}
return $customcompletionelements;
} catch (Exception $e) {
debugging('Could not add custom completion rule of module ' . $modnames[0] .
' to this form, this has to be fixed by the developer', DEBUG_DEVELOPER);
return [];
}
}
/**
* Checks if at least one of the custom completion rules is enabled
*
* @param array $data Input data (not yet validated)
* @return bool True if one or more rules is enabled, false if none are;
* default returns false
*/
protected function completion_rule_enabled($data) {
if ($this->hascustomrules) {
return $this->get_module_form()->completion_rule_enabled($data);
}
return false;
}
/**
* Returns list of modules that have automatic completion rules that are not shown on this form
* (because they are not present in at least one other selected module).
*
* @return array
*/
protected function get_modules_with_hidden_rules() {
$modnames = $this->get_module_names();
if (count($modnames) <= 1) {
// No rules definitions conflicts if there is only one module type.
return [];
}
$conflicts = [];
if (!$this->support_views()) {
// If we don't display views rule but at least one module supports it - we have conflicts.
foreach ($modnames as $modname => $modfullname) {
if (empty($conflicts[$modname]) && plugin_supports('mod', $modname, FEATURE_COMPLETION_TRACKS_VIEWS, false)) {
$conflicts[$modname] = $modfullname;
}
}
}
if (!$this->support_grades()) {
// If we don't display grade rule but at least one module supports it - we have conflicts.
foreach ($modnames as $modname => $modfullname) {
if (empty($conflicts[$modname]) && plugin_supports('mod', $modname, FEATURE_GRADE_HAS_GRADE, false)) {
$conflicts[$modname] = $modfullname;
}
}
}
foreach ($modnames as $modname => $modfullname) {
// We do not display any custom completion rules, find modules that define them and add to conflicts list.
if (empty($conflicts[$modname]) && plugin_supports('mod', $modname, FEATURE_COMPLETION_HAS_RULES, false)) {
$conflicts[$modname] = $modfullname;
}
}
return $conflicts;
}
/**
* Form definition
*/
public function definition() {
$this->cms = $this->_customdata['cms'];
$cm = reset($this->cms); // First selected course module.
$mform = $this->_form;
$mform->addElement('hidden', 'id', $cm->course);
$mform->setType('id', PARAM_INT);
foreach ($this->cms as $cm) {
$mform->addElement('hidden', 'cmid['.$cm->id.']', $cm->id);
$mform->setType('cmid['.$cm->id.']', PARAM_INT);
}
// Unlock completion automatically (this element can be used in validation).
$mform->addElement('hidden', 'completionunlocked', 1);
$mform->setType('completionunlocked', PARAM_INT);
$mform->addElement('select', 'completion', get_string('completion', 'completion'),
array(COMPLETION_TRACKING_NONE=>get_string('completion_none', 'completion'),
COMPLETION_TRACKING_MANUAL=>get_string('completion_manual', 'completion')));
$mform->addHelpButton('completion', 'completion', 'completion');
$mform->setDefault('completion', COMPLETION_TRACKING_NONE);
// Automatic completion once you view it
$autocompletionpossible = false;
if ($this->support_views()) {
$mform->addElement('advcheckbox', 'completionview', get_string('completionview', 'completion'),
get_string('completionview_desc', 'completion'));
$mform->disabledIf('completionview', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
$autocompletionpossible = true;
}
// Automatic completion once it's graded
if ($this->support_grades()) {
$mform->addElement('advcheckbox', 'completionusegrade', get_string('completionusegrade', 'completion'),
get_string('completionusegrade_desc', 'completion'));
$mform->disabledIf('completionusegrade', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
$mform->addHelpButton('completionusegrade', 'completionusegrade', 'completion');
$autocompletionpossible = true;
}
// Automatic completion according to module-specific rules
foreach ($this->add_custom_completion_rules() as $element) {
$mform->disabledIf($element, 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
$autocompletionpossible = true;
}
// Automatic option only appears if possible
if ($autocompletionpossible) {
$mform->getElement('completion')->addOption(
get_string('completion_automatic', 'completion'),
COMPLETION_TRACKING_AUTOMATIC);
}
// Completion expected at particular date? (For progress tracking)
$mform->addElement('date_selector', 'completionexpected', get_string('completionexpected', 'completion'), ['optional' => true]);
$mform->addHelpButton('completionexpected', 'completionexpected', 'completion');
$mform->disabledIf('completionexpected', 'completion', 'eq', COMPLETION_TRACKING_NONE);
if ($conflicts = $this->get_modules_with_hidden_rules()) {
$mform->addElement('static', 'qwerty', '', get_string('hiddenrules', 'completion', join(', ', $conflicts)));
}
$this->add_action_buttons();
$modform = $this->get_module_form();
if ($modform) {
// Pre-fill the form with the current completion rules of the first selected module.
list($cmrec, $context, $module, $data, $cw) = get_moduleinfo_data($cm->get_course_module_record(), $cm->get_course());
$data = (array)$data;
$modform->data_preprocessing($data);
// Unset fields that will conflict with this form and set data to this form.
unset($data['cmid']);
unset($data['id']);
$this->set_data($data);
}
}
/**
* Form validation
*
* @param array $data array of ("fieldname"=>value) of submitted data
* @param array $files array of uploaded files "element_name"=>tmp_file_path
* @return array of "element_name"=>"error_description" if there are errors,
* or an empty array if everything is OK (true allowed for backwards compatibility too).
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
// Completion: Don't let them choose automatic completion without turning
// on some conditions.
if (array_key_exists('completion', $data) &&
$data['completion'] == COMPLETION_TRACKING_AUTOMATIC) {
if (empty($data['completionview']) && empty($data['completionusegrade']) &&
!$this->completion_rule_enabled($data)) {
$errors['completion'] = get_string('badautocompletion', 'completion');
}
}
return $errors;
}
/**
* Returns if this form has custom completion rules. This is only possible if all selected modules have the same
* module type and this module type supports custom completion rules
*
* @return bool
*/
public function has_custom_completion_rules() {
return $this->hascustomrules;
}
/**
* Return submitted data if properly submitted or returns NULL if validation fails or
* if there is no submitted data.
*
* @return object submitted data; NULL if not valid or not submitted or cancelled
*/
public function get_data() {
$data = parent::get_data();
if ($data && $this->hascustomrules) {
$this->get_module_form()->data_postprocessing($data);
}
return $data;
}
}

View File

@ -181,4 +181,90 @@ class manager {
}
return false;
}
/**
* Applies completion from the bulk edit form to all selected modules
*
* @param stdClass $data data received from the core_completion_bulkedit_form
* @param bool $updateinstance if we need to update the instance tables of the module (i.e. 'assign', 'forum', etc.) -
* if no module-specific completion rules were added to the form, update of the module table is not needed.
*/
public function apply_completion($data, $updateinstances) {
$updated = [];
$modinfo = get_fast_modinfo($this->courseid);
$cmids = $data->cmid;
$data = (array)$data;
unset($data['id']); // This is a course id, we don't want to confuse it with cmid or instance id.
unset($data['cmid']);
unset($data['submitbutton']);
foreach ($cmids as $cmid) {
$cm = $modinfo->get_cm($cmid);
if (self::can_edit_bulk_completion($this->courseid, $cm) && $this->apply_completion_cm($cm, $data, $updateinstances)) {
$updated[] = $cm->id;
}
}
if ($updated) {
// Now that modules are fully updated, also update completion data if required.
// This will wipe all user completion data and recalculate it.
rebuild_course_cache($this->courseid, true);
$modinfo = get_fast_modinfo($this->courseid);
$completion = new \completion_info($modinfo->get_course());
foreach ($updated as $cmid) {
$completion->reset_all_state($modinfo->get_cm($cmid));
}
}
}
/**
* Applies new completion rules to one course module
*
* @param \cm_info $cm
* @param array $data
* @param bool $updateinstance if we need to update the instance table of the module (i.e. 'assign', 'forum', etc.) -
* if no module-specific completion rules were added to the form, update of the module table is not needed.
* @return bool if module was updated
*/
protected function apply_completion_cm(\cm_info $cm, $data, $updateinstance) {
global $DB;
$defaults = ['completion' => COMPLETION_DISABLED, 'completionview' => COMPLETION_VIEW_NOT_REQUIRED,
'completionexpected' => 0, 'completiongradeitemnumber' => null];
if ($cm->completion == $data['completion'] && $cm->completion != COMPLETION_TRACKING_AUTOMATIC) {
// If old and new completion are either both "manual" or both "none" - no changes are needed.
return false;
}
$data += ['completion' => $cm->completion,
'completionexpected' => $cm->completionexpected,
'completionview' => $cm->completionview];
if (array_key_exists('completionusegrade', $data)) {
// Convert the 'use grade' checkbox into a grade-item number: 0 if checked, null if not.
$data['completiongradeitemnumber'] = !empty($data['completionusegrade']) ? 0 : null;
unset($data['completionusegrade']);
} else {
$data['completiongradeitemnumber'] = $cm->completiongradeitemnumber;
}
// Update module instance table.
if ($updateinstance) {
$moddata = ['id' => $cm->instance, 'timemodified' => time()] + array_diff_key($data, $defaults);
$DB->update_record($cm->modname, $moddata);
}
// Update course modules table.
$cmdata = ['id' => $cm->id, 'timemodified' => time()] + array_intersect_key($data, $defaults);
$DB->update_record('course_modules', $cmdata);
\core\event\course_module_updated::create_from_cm($cm, $cm->context)->trigger();
\core\notification::add(get_string('completionupdated', 'completion', $cm->get_formatted_name()),
\core\notification::SUCCESS);
return true;
}
}

View File

@ -0,0 +1,257 @@
<?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/>.
/**
* External completion functions unit tests
*
* @package core_completion
* @copyright 2017 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/completionlib.php');
/**
* External completion functions unit tests
*
* @package core_completion
* @copyright 2017 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_completion_bulk_update_testcase extends advanced_testcase {
/**
* Provider for test_bulk_form_submit_single
* @return array
*/
public function bulk_form_submit_single_provider() {
return [
'assign-1' => ['assign', ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionsubmit' => 1]],
'assign-2' => ['assign', ['completion' => COMPLETION_TRACKING_MANUAL]],
'book-1' => ['book', ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1]],
'book-2' => ['book', ['completion' => COMPLETION_TRACKING_MANUAL]],
'chat-1' => ['chat', ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1]],
'chat-2' => ['chat', ['completion' => COMPLETION_TRACKING_MANUAL]],
'choice-1' => ['choice', ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionsubmit' => 1]],
'choice-2' => ['choice', ['completion' => COMPLETION_TRACKING_MANUAL]],
'data-1' => ['data', ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1]],
'data-2' => ['data', ['completion' => COMPLETION_TRACKING_MANUAL]],
'feedback-1' => ['feedback', ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 0, 'completionsubmit' => 1]],
'feedback-2' => ['feedback', ['completion' => COMPLETION_TRACKING_MANUAL]],
'folder-1' => ['folder', ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1]],
'folder-2' => ['folder', ['completion' => COMPLETION_TRACKING_MANUAL]],
'forum-1' => ['forum',
['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completiondiscussions' => 1,
'completiondiscussionsenabled' => 1],
['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completiondiscussions' => 1]],
'forum-2' => ['forum', ['completion' => COMPLETION_TRACKING_MANUAL]],
'glossary-1' => ['glossary',
['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1, 'completionentries' => 3,
'completionentriesenabled' => 1],
['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1, 'completionentries' => 3]],
'glossary-2' => ['glossary', ['completion' => COMPLETION_TRACKING_MANUAL]],
'imscp-1' => ['imscp', ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1]],
'imscp-2' => ['imscp', ['completion' => COMPLETION_TRACKING_MANUAL]],
'label-1' => ['label', ['completion' => COMPLETION_TRACKING_MANUAL]],
'lesson-1' => ['lesson', ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionendreached' => 1]],
'lesson-2' => ['lesson', ['completion' => COMPLETION_TRACKING_MANUAL]],
'lti-1' => ['lti', ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1]],
'lti-2' => ['lti', ['completion' => COMPLETION_TRACKING_MANUAL]],
'page-1' => ['page', ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1]],
'page-2' => ['page', ['completion' => COMPLETION_TRACKING_MANUAL]],
'quiz-1' => ['quiz', ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionpass' => 1]],
'quiz-2' => ['quiz', ['completion' => COMPLETION_TRACKING_MANUAL]],
'resource-1' => ['resource', ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1]],
'resource-2' => ['resource', ['completion' => COMPLETION_TRACKING_MANUAL]],
'scorm-1' => ['scorm',
['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionscorerequired' => 1,
'completionstatusrequired' => [2 => 'passed']],
['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionscorerequired' => 1,
'completionstatusrequired' => 2]],
'scorm-2' => ['scorm', ['completion' => COMPLETION_TRACKING_MANUAL]],
'survey-1' => ['survey', ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionsubmit' => 1]],
'survey-2' => ['survey', ['completion' => COMPLETION_TRACKING_MANUAL]],
'url-1' => ['url', ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1]],
'url-2' => ['url', ['completion' => COMPLETION_TRACKING_MANUAL]],
'wiki-1' => ['wiki', ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1]],
'wiki-2' => ['wiki', ['completion' => COMPLETION_TRACKING_MANUAL]],
'workshop-1' => ['workshop', ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1]],
'workshop-2' => ['workshop', ['completion' => COMPLETION_TRACKING_MANUAL]],
];
}
/**
* Creates an instance of bulk edit completion form for one activity, validates and saves it
*
* @dataProvider bulk_form_submit_single_provider
* @param string $modname
* @param array $submitdata data to use in mock form submit
* @param array|null $validatedata data to validate the
*/
public function test_bulk_form_submit_single($modname, $submitdata, $validatedata = null) {
global $DB;
if ($validatedata === null) {
$validatedata = $submitdata;
}
$this->resetAfterTest();
$this->setAdminUser();
list($course, $cms) = $this->create_course_and_modules([$modname]);
// Submit the bulk completion form with the provided data and make sure it returns the same data.
core_completion_bulkedit_form::mock_submit(['id' => $course->id, 'cmid' => array_keys($cms)] + $submitdata, []);
$form = new core_completion_bulkedit_form(null, ['cms' => $cms]);
$this->assertTrue($form->is_validated());
$data = $form->get_data();
foreach ($validatedata as $key => $value) {
$this->assertEquals($value, $data->$key);
}
// Apply completion rules to the modules.
$manager = new core_completion\manager($course->id);
$manager->apply_completion($data, $form->has_custom_completion_rules());
// Make sure either course_modules or instance table was respectfully updated.
$cm = reset($cms);
$cmrec = $DB->get_record('course_modules', ['id' => $cm->id]);
$instancerec = $DB->get_record($modname, ['id' => $cm->instance]);
foreach ($validatedata as $key => $value) {
if (property_exists($cmrec, $key)) {
$this->assertEquals($value, $cmrec->$key);
} else {
$this->assertEquals($value, $instancerec->$key);
}
}
}
/**
* Creates a course and the number of modules
* @param array $modulenames
* @return array array of two elements - course and list of cm_info objects
*/
protected function create_course_and_modules($modulenames) {
global $CFG, $PAGE;
$CFG->enablecompletion = true;
$course = $this->getDataGenerator()->create_course(['enablecompletion' => 1], ['createsections' => true]);
$PAGE->set_course($course);
$cmids = [];
foreach ($modulenames as $modname) {
$module = $this->getDataGenerator()->create_module($modname, ['course' => $course->id]);
$cmids[] = $module->cmid;
}
$modinfo = get_fast_modinfo($course);
$cms = [];
foreach ($cmids as $cmid) {
$cms[$cmid] = $modinfo->get_cm($cmid);
}
return [$course, $cms];
}
/**
* Provider for test_bulk_form_submit_multiple
* @return array
*/
public function bulk_form_submit_multiple_provider() {
return [
'Several modules with the same module type (choice)' =>
[['modulenames' => ['choice', 'choice', 'choice'],
'submitdata' => ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionsubmit' => 1],
'validatedata' => ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionsubmit' => 1],
'cmdata' => ['completion' => COMPLETION_TRACKING_AUTOMATIC],
'instancedata' => [
['completionsubmit' => 1],
['completionsubmit' => 1],
['completionsubmit' => 1],
]]],
'Several modules with different module type' =>
[['modulenames' => ['choice', 'forum'],
'submitdata' => ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1],
'validatedata' => ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1],
'cmdata' => ['completion' => COMPLETION_TRACKING_AUTOMATIC],
'instancedata' => null]],
'Setting manual completion (completionview shoud be ignored)' =>
[['modulenames' => ['scorm', 'forum', 'label', 'assign'],
'submitdata' => ['completion' => COMPLETION_TRACKING_MANUAL, 'completionview' => 1],
'validatedata' => [],
'cmdata' => ['completion' => COMPLETION_TRACKING_MANUAL, 'completionview' => 0],
'instancedata' => null]],
'If at least one module does not support completionsubmit it can\'t be set' =>
[['modulenames' => ['survey', 'wiki'],
'submitdata' => ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1, 'completionsubmit' => 1],
'validatedata' => [],
'cmdata' => ['completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1],
'instancedata' => [
['completionsubmit' => 0],
[]
]]],
];
}
/**
* Use bulk completion edit for updating multiple modules
*
* @dataProvider bulk_form_submit_multiple_provider
* @param array $data
*/
public function test_bulk_form_submit_multiple($providerdata) {
global $DB;
$modulenames = $providerdata['modulenames'];
$submitdata = $providerdata['submitdata'];
$validatedata = $providerdata['validatedata'];
$cmdata = $providerdata['cmdata'];
$instancedata = $providerdata['instancedata'];
$this->resetAfterTest();
$this->setAdminUser();
list($course, $cms) = $this->create_course_and_modules($modulenames);
// Submit the bulk completion form with the provided data and make sure it returns the same data.
core_completion_bulkedit_form::mock_submit(['id' => $course->id, 'cmid' => array_keys($cms)] + $submitdata, []);
$form = new core_completion_bulkedit_form(null, ['cms' => $cms]);
$this->assertTrue($form->is_validated());
$data = $form->get_data();
foreach ($validatedata as $key => $value) {
$this->assertEquals($value, $data->$key);
}
// Apply completion rules to the modules.
$manager = new core_completion\manager($course->id);
$manager->apply_completion($data, $form->has_custom_completion_rules());
// Make sure either course_modules or instance table was respectfully updated.
$cnt = 0;
foreach ($cms as $cm) {
$cmrec = $DB->get_record('course_modules', ['id' => $cm->id]);
$instancerec = $DB->get_record($cm->modname, ['id' => $cm->instance]);
foreach ($cmdata as $key => $value) {
$this->assertEquals($value, $cmrec->$key);
}
if ($instancedata) {
foreach ($instancedata[$cnt] as $key => $value) {
$this->assertEquals($value, $instancerec->$key);
}
}
$cnt++;
}
}
}

View File

@ -77,7 +77,8 @@ class core_course_bulk_activity_completion_renderer extends plugin_renderer_base
}
public function activities_list($data) {
return parent::render_from_template('core_course/activityinstance', $data);
return html_writer::div(get_string('affectedactivities', 'completion', count($data->activities))).
parent::render_from_template('core_course/activityinstance', $data);
}
}

View File

@ -0,0 +1,80 @@
<?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/>.
/**
* Bulk activity completion selection
*
* @package core_completion
* @copyright 2017 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
include __DIR__ . "/../config.php";
require_once($CFG->libdir . '/completionlib.php');
$courseid = required_param('id', PARAM_INT);
$cmids = optional_param_array('cmid', [], PARAM_INT);
$course = get_course($courseid);
require_login($course);
navigation_node::override_active_url(new moodle_url('/course/completion.php', array('id' => $course->id)));
$PAGE->set_url(new moodle_url('/course/editbulkcompletion.php', ['id' => $courseid]));
$PAGE->set_title($course->shortname);
$PAGE->set_heading($course->fullname);
$PAGE->set_pagelayout('admin');
if (!core_completion\manager::can_edit_bulk_completion($course)) {
throw new required_capability_exception(context_course::instance($course->id),
'moodle/course:manageactivities', 'nopermission');
}
// Prepare list of modules to be updated.
$modinfo = get_fast_modinfo($courseid);
$cms = [];
foreach ($cmids as $cmid) {
$cm = $modinfo->get_cm($cmid);
if (core_completion\manager::can_edit_bulk_completion($course, $cm)) {
$cms[$cm->id] = $cm;
}
}
$returnurl = new moodle_url('/course/bulkcompletion.php', ['id' => $course->id]);
$manager = new \core_completion\manager($course->id);
if (empty($cms)) {
redirect($returnurl);
}
$form = new core_completion_bulkedit_form(null, ['cms' => $cms]);
if ($form->is_cancelled()) {
redirect($returnurl);
} else if ($data = $form->get_data()) {
$manager->apply_completion($data, $form->has_custom_completion_rules());
redirect($returnurl);
}
$renderer = $PAGE->get_renderer('core_course', 'bulk_activity_completion');
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('editcoursecompletionsettings', 'core_completion'));
echo $renderer->navigation($course->id, 'bulkcompletion');
$form->display();
echo $renderer->activities_list($manager->get_activities(array_keys($cms)));
echo $OUTPUT->footer();

View File

@ -148,13 +148,6 @@ if ($mform->is_cancelled()) {
redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn)));
}
} else if ($fromform = $mform->get_data()) {
// Convert the grade pass value - we may be using a language which uses commas,
// rather than decimal points, in numbers. These need to be converted so that
// they can be added to the DB.
if (isset($fromform->gradepass)) {
$fromform->gradepass = unformat_float($fromform->gradepass);
}
if (!empty($fromform->update)) {
list($cm, $fromform) = update_moduleinfo($cm, $fromform, $course, $mform);
} else if (!empty($fromform->add)) {

View File

@ -1,6 +1,8 @@
<?php
require_once ($CFG->libdir.'/formslib.php');
require_once($CFG->libdir.'/completionlib.php');
require_once($CFG->libdir.'/gradelib.php');
require_once ($CFG->libdir.'/plagiarismlib.php');
/**
* This class adds extra methods to form wrapper specific to be used for module
@ -181,6 +183,9 @@ abstract class moodleform_mod extends moodleform {
}
/**
* Allows module to modify data returned by get_moduleinfo_data() or prepare_new_moduleinfo_data() before calling set_data()
* This method is also called in the bulk activity completion form.
*
* Only available on moodleform_mod.
*
* @param array $default_values passed by reference
@ -1064,6 +1069,40 @@ abstract class moodleform_mod extends moodleform {
}
}
}
/**
* Allows modules to modify the data returned by form get_data().
* This method is also called in the bulk activity completion form.
*
* Only available on moodleform_mod.
*
* @param stdClass $data passed by reference
*/
public function data_postprocessing(&$data) {
}
/**
* Return submitted data if properly submitted or returns NULL if validation fails or
* if there is no submitted data.
*
* Do not override this method, override data_postprocessing() instead.
*
* @return object submitted data; NULL if not valid or not submitted or cancelled
*/
public function get_data() {
$data = parent::get_data();
if ($data) {
// Convert the grade pass value - we may be using a language which uses commas,
// rather than decimal points, in numbers. These need to be converted so that
// they can be added to the DB.
if (isset($data->gradepass)) {
$data->gradepass = unformat_float($data->gradepass);
}
$this->data_postprocessing($data);
}
return $data;
}
}

View File

@ -52,7 +52,7 @@
<div class="row m-b-2">
<div class="col">{{#str}}bulkactivitydetail, moodle{{/str}}</div>
</div>
<form method="post" action="bulkcompletion.php" class="mform" id="theform">
<form method="post" action="editbulkcompletion.php" class="mform" id="theform">
<div class="row m-b-2">
<div class="col">
<input type="submit" value="{{#str}}edit{{/str}}" class="btn btn-primary" name="submitbutton" aria-label="{{#str}}updateactivities, completion{{/str}}" />

View File

@ -32,6 +32,7 @@ $string['activityaggregation_any'] = 'ANY selected activities to be completed';
$string['activitiescompleted'] = 'Activity completion';
$string['activitiescompletednote'] = 'Note: Activity completion must be set for an activity to appear in the above list.';
$string['activitycompletion'] = 'Activity completion';
$string['affectedactivities'] = 'The changes will affect the following <b>{$a}</b> Activities/Resources';
$string['aggregationmethod'] = 'Aggregation method';
$string['all'] = 'All';
$string['any'] = 'Any';
@ -91,6 +92,7 @@ $string['completionsettingslocked'] = 'Completion settings locked';
$string['completionusegrade'] = 'Require grade';
$string['completionusegrade_desc'] = 'Student must receive a grade to complete this activity';
$string['completionusegrade_help'] = 'If enabled, the activity is considered complete when a student receives a grade. Pass and fail icons may be displayed if a pass grade for the activity has been set.';
$string['completionupdated'] = 'Updated completion for activity <b>{$a}</b>';
$string['completionview'] = 'Require view';
$string['completionview_desc'] = 'Student must view this activity to complete it';
$string['configcompletiondefault'] = 'The default setting for completion tracking when creating new activities.';
@ -119,6 +121,7 @@ $string['defaultcompletion'] = 'Default activity completion';
$string['deletecompletiondata'] = 'Delete completion data';
$string['dependencies'] = 'Dependencies';
$string['dependenciescompleted'] = 'Completion of other courses';
$string['hiddenrules'] = 'Some settings specific to <b>{$a}</b> have been hidden. To view unselect other activities';
$string['editcoursecompletionsettings'] = 'Edit course completion settings';
$string['enablecompletion'] = 'Enable completion tracking';
$string['enablecompletion_help'] = 'If enabled, activity completion conditions may be set in the activity settings and/or course completion conditions may be set. It is recommended to have this enabled in order for the course progress dashboard to display meaningful data.';

View File

@ -232,6 +232,9 @@ class moodle_page {
*/
protected $_requires = null;
/** @var page_requirements_manager Saves the requirement manager object used before switching to to fragments one. */
protected $savedrequires = null;
/**
* @var string The capability required by the user in order to edit blocks
* and block settings on this page.
@ -890,12 +893,23 @@ class moodle_page {
// JavaScript for the fragment to be collected. _wherethemewasinitialised is set when header() is called.
if (!empty($this->_wherethemewasinitialised)) {
// Change the current requirements manager over to the fragment manager to capture JS.
$this->savedrequires = $this->_requires;
$this->_requires = new fragment_requirements_manager();
} else {
throw new coding_exception('$OUTPUT->header() needs to be called before collecting JavaScript requirements.');
}
}
/**
* Switches back from collecting fragment JS requirement to the original requirement manager
*/
public function end_collecting_javascript_requirements() {
if ($this->savedrequires === null) {
throw new coding_exception('JavaScript collection has not been started.');
}
$this->_requires = $this->savedrequires;
}
/**
* Should the current user see this page in editing mode.
* That is, are they allowed to edit this page, and are they currently in

View File

@ -124,18 +124,22 @@ class mod_choice_mod_form extends moodleform_mod {
}
function get_data() {
$data = parent::get_data();
if (!$data) {
return false;
}
/**
* Allows module to modify the data returned by form get_data().
* This method is also called in the bulk activity completion form.
*
* Only available on moodleform_mod.
*
* @param stdClass $data passed by reference
*/
function data_postprocessing(&$data) {
parent::data_postprocessing($data);
// Set up completion section even if checkbox is not ticked
if (!empty($data->completionunlocked)) {
if (empty($data->completionsubmit)) {
$data->completionsubmit = 0;
}
}
return $data;
}
/**

View File

@ -160,9 +160,17 @@ class mod_feedback_mod_form extends moodleform_mod {
}
public function get_data() {
$data = parent::get_data();
if ($data) {
/**
* Allows module to modify the data returned by form get_data().
* This method is also called in the bulk activity completion form.
*
* Only available on moodleform_mod.
*
* @param stdClass $data passed by reference
*/
public function data_postprocessing(&$data) {
parent::data_postprocessing($data);
if (isset($data->page_after_submit_editor)) {
$data->page_after_submitformat = $data->page_after_submit_editor['format'];
$data->page_after_submit = $data->page_after_submit_editor['text'];
@ -175,8 +183,6 @@ class mod_feedback_mod_form extends moodleform_mod {
}
}
}
return $data;
}
/**

View File

@ -292,11 +292,16 @@ class mod_forum_mod_form extends moodleform_mod {
(!empty($data['completionpostsenabled']) && $data['completionposts']!=0);
}
function get_data() {
$data = parent::get_data();
if (!$data) {
return false;
}
/**
* Allows module to modify the data returned by form get_data().
* This method is also called in the bulk activity completion form.
*
* Only available on moodleform_mod.
*
* @param stdClass $data passed by reference
*/
function data_postprocessing(&$data) {
parent::data_postprocessing($data);
// Turn off completion settings if the checkboxes aren't ticked
if (!empty($data->completionunlocked)) {
$autocompletion = !empty($data->completion) && $data->completion==COMPLETION_TRACKING_AUTOMATIC;
@ -310,7 +315,6 @@ class mod_forum_mod_form extends moodleform_mod {
$data->completionposts = 0;
}
}
return $data;
}
}

View File

@ -202,11 +202,16 @@ class mod_glossary_mod_form extends moodleform_mod {
return (!empty($data['completionentriesenabled']) && $data['completionentries']!=0);
}
function get_data() {
$data = parent::get_data();
if (!$data) {
return false;
}
/**
* Allows module to modify the data returned by form get_data().
* This method is also called in the bulk activity completion form.
*
* Only available on moodleform_mod.
*
* @param stdClass $data passed by reference
*/
function data_postprocessing(&$data) {
parent::data_postprocessing($data);
if (!empty($data->completionunlocked)) {
// Turn off completion settings if the checkboxes aren't ticked
$autocompletion = !empty($data->completion) && $data->completion==COMPLETION_TRACKING_AUTOMATIC;
@ -214,7 +219,6 @@ class mod_glossary_mod_form extends moodleform_mod {
$data->completionentries = 0;
}
}
return $data;
}
/**

View File

@ -432,25 +432,26 @@ class mod_lesson_mod_form extends moodleform_mod {
return !empty($data['completionendreached']) || $data['completiontimespent'] > 0;
}
public function get_data() {
$data = parent::get_data();
if (!$data) {
return false;
}
/**
* Allows module to modify the data returned by form get_data().
* This method is also called in the bulk activity completion form.
*
* Only available on moodleform_mod.
*
* @param stdClass $data passed by reference
*/
public function data_postprocessing(&$data) {
parent::data_postprocessing($data);
// Turn off completion setting if the checkbox is not ticked.
if (!empty($data->completionunlocked)) {
$autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
if (empty($data->completiontimespentenabled) || !$autocompletion) {
$data->completiontimespent = 0;
}
}
if (!empty($data->completionunlocked)) {
$autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
if (empty($data->completionendreached) || !$autocompletion) {
$data->completionendreached = 0;
}
}
return $data;
}
}

View File

@ -544,13 +544,16 @@ class mod_scorm_mod_form extends moodleform_mod {
return $status || $score;
}
public function get_data($slashed = true) {
$data = parent::get_data($slashed);
if (!$data) {
return false;
}
/**
* Allows module to modify the data returned by form get_data().
* This method is also called in the bulk activity completion form.
*
* Only available on moodleform_mod.
*
* @param stdClass $data passed by reference
*/
public function data_postprocessing(&$data) {
parent::data_postprocessing($data);
// Convert completionstatusrequired to a proper integer, if any.
$total = 0;
if (isset($data->completionstatusrequired) && is_array($data->completionstatusrequired)) {
@ -574,7 +577,5 @@ class mod_scorm_mod_form extends moodleform_mod {
$data->completionscorerequired = null;
}
}
return $data;
}
}

View File

@ -47,17 +47,15 @@ class mod_survey_mod_form extends moodleform_mod {
}
/**
* Return submitted data if properly submitted or returns NULL if validation fails or
* if there is no submitted data.
* Allows module to modify the data returned by form get_data().
* This method is also called in the bulk activity completion form.
*
* @return stdClass submitted data; NULL if not valid or not submitted or cancelled
* Only available on moodleform_mod.
*
* @param stdClass $data passed by reference
*/
public function get_data() {
$data = parent::get_data();
if (!$data) {
return false;
}
public function data_postprocessing(&$data) {
parent::data_postprocessing($data);
if (!empty($data->completionunlocked)) {
// Turn off completion settings if the checkboxes aren't ticked.
$autocompletion = !empty($data->completion) &&
@ -66,7 +64,6 @@ class mod_survey_mod_form extends moodleform_mod {
$data->completionsubmit = 0;
}
}
return $data;
}
/**

View File

@ -35,7 +35,7 @@ if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once('moodleform_mod.php');
require_once($CFG->dirroot . '/course/moodleform_mod.php');
require_once($CFG->dirroot . '/mod/wiki/locallib.php');
require_once($CFG->dirroot . '/lib/datalib.php');