MDL-58267 completion: allow to set default activity completion
Part of MDL-58138 epic
This commit is contained in:
committed by
Jake Dallimore
parent
06cdda468a
commit
7f53e8aa22
@@ -15,7 +15,7 @@
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Bulk activity completion form
|
||||
* Bulk edit activity completion form
|
||||
*
|
||||
* @package core_completion
|
||||
* @copyright 2017 Marina Glancy
|
||||
@@ -24,26 +24,18 @@
|
||||
|
||||
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
|
||||
* Bulk edit 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 {
|
||||
class core_completion_bulkedit_form extends core_completion_edit_base_form {
|
||||
/** @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
|
||||
@@ -61,231 +53,27 @@ class core_completion_bulkedit_form extends moodleform {
|
||||
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.
|
||||
$this->course = $cm->get_course();
|
||||
|
||||
$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();
|
||||
parent::definition();
|
||||
|
||||
$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());
|
||||
list($cmrec, $context, $module, $data, $cw) = get_moduleinfo_data($cm->get_course_module_record(), $this->course);
|
||||
$data = (array)$data;
|
||||
$modform->data_preprocessing($data);
|
||||
// Unset fields that will conflict with this form and set data to this form.
|
||||
@@ -294,52 +82,4 @@ class core_completion_bulkedit_form extends moodleform {
|
||||
$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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Default 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;
|
||||
|
||||
/**
|
||||
* Default 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_defaultedit_form extends core_completion_edit_base_form {
|
||||
/** @var array */
|
||||
protected $modules;
|
||||
/** @var array */
|
||||
protected $_modnames;
|
||||
|
||||
/**
|
||||
* 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->modules as $module) {
|
||||
$this->_modnames[$module->name] = $module->formattedname;
|
||||
}
|
||||
return $this->_modnames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Form definition,
|
||||
*/
|
||||
public function definition() {
|
||||
$this->course = $this->_customdata['course'];
|
||||
$this->modules = $this->_customdata['modules'];
|
||||
|
||||
$mform = $this->_form;
|
||||
|
||||
foreach ($this->modules as $modid => $module) {
|
||||
$mform->addElement('hidden', 'modids['.$modid.']', $modid);
|
||||
$mform->setType('modids['.$modid.']', PARAM_INT);
|
||||
}
|
||||
|
||||
parent::definition();
|
||||
|
||||
$modform = $this->get_module_form();
|
||||
if ($modform) {
|
||||
$modnames = array_keys($this->get_module_names());
|
||||
$modname = $modnames[0];
|
||||
// Pre-fill the form with the current completion rules of the first selected module type.
|
||||
list($module, $context, $cw, $cmrec, $data) = prepare_new_moduleinfo_data($this->course, $modname, 0);
|
||||
$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['modids']);
|
||||
unset($data['id']);
|
||||
$this->set_data($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,316 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Base form for changing completion rules
|
||||
*
|
||||
* @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');
|
||||
|
||||
/**
|
||||
* Base form for changing completion rules. Used in bulk editing activity completion and editing default activity completion
|
||||
*
|
||||
* @package core_completion
|
||||
* @copyright 2017 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
abstract class core_completion_edit_base_form extends moodleform {
|
||||
/** @var moodleform_mod Do not use directly, call $this->get_module_form() */
|
||||
protected $_moduleform = null;
|
||||
/** @var bool */
|
||||
protected $hascustomrules = false;
|
||||
/** @var stdClass */
|
||||
protected $course;
|
||||
|
||||
/**
|
||||
* Returns list of types of selected module types
|
||||
*
|
||||
* @return array modname=>modfullname
|
||||
*/
|
||||
abstract protected function get_module_names();
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
$modnames = array_keys($this->get_module_names());
|
||||
$modname = $modnames[0];
|
||||
$course = $this->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);
|
||||
$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() {
|
||||
$mform = $this->_form;
|
||||
|
||||
// Course id.
|
||||
$mform->addElement('hidden', 'id', $this->course->id);
|
||||
$mform->setType('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();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
+154
-13
@@ -28,6 +28,7 @@ namespace core_completion;
|
||||
|
||||
use stdClass;
|
||||
use context_course;
|
||||
use cm_info;
|
||||
|
||||
/**
|
||||
* Bulk activity completion manager class
|
||||
@@ -65,8 +66,7 @@ class manager {
|
||||
$sectionobject = new stdClass();
|
||||
$sectionobject->sectionnumber = $sectionnumber;
|
||||
$sectionobject->name = get_section_name($this->courseid, $sectioninfo);
|
||||
$activitiesdata = $this->get_activities($section, true);
|
||||
$sectionobject->activities = $activitiesdata->activities;
|
||||
$sectionobject->activities = $this->get_activities($section, true);
|
||||
$data->sections[] = $sectionobject;
|
||||
}
|
||||
return $data;
|
||||
@@ -77,7 +77,7 @@ class manager {
|
||||
*
|
||||
* @param array $cmids list of course module ids
|
||||
* @param bool $withcompletiondetails include completion details
|
||||
* @return \stdClass
|
||||
* @return array
|
||||
*/
|
||||
public function get_activities($cmids, $withcompletiondetails = false) {
|
||||
$moduleinfo = get_fast_modinfo($this->courseid);
|
||||
@@ -96,34 +96,43 @@ class manager {
|
||||
|
||||
// Get activity completion information.
|
||||
if ($moduleobject->canmanage) {
|
||||
$moduleobject->completionstatus = $this->get_completion_detail($mod); // This is a placeholder only. Must be replaced later.
|
||||
$moduleobject->completionstatus = $this->get_completion_detail($mod);
|
||||
} else {
|
||||
$moduleobject->completionstatus = ['icon' => null, 'string' => null];
|
||||
}
|
||||
|
||||
$activities[] = $moduleobject;
|
||||
}
|
||||
return (object)['activities' => $activities];
|
||||
return $activities;
|
||||
}
|
||||
|
||||
private function get_completion_detail(\cm_info $mod) {
|
||||
|
||||
/**
|
||||
* Get completion information on the selected module or module type
|
||||
*
|
||||
* @param cm_info|stdClass $mod either instance of cm_info (with 'customcompletionrules' in customdata) or
|
||||
* object with fields ->completion, ->completionview, ->completionexpected, ->completionusegrade
|
||||
* and ->customdata['customcompletionrules']
|
||||
* @return array
|
||||
*/
|
||||
private function get_completion_detail($mod) {
|
||||
global $OUTPUT;
|
||||
$strings = [];
|
||||
switch ($mod->completion) {
|
||||
case 0:
|
||||
case COMPLETION_TRACKING_NONE:
|
||||
$strings['string'] = get_string('none');
|
||||
break;
|
||||
|
||||
case 1:
|
||||
case COMPLETION_TRACKING_MANUAL:
|
||||
$strings['string'] = get_string('manual');
|
||||
$strings['icon'] = $OUTPUT->pix_url('i/completion-manual-enabled')->out();
|
||||
break;
|
||||
|
||||
case 2:
|
||||
case COMPLETION_TRACKING_AUTOMATIC:
|
||||
$strings['string'] = get_string('withconditions');
|
||||
|
||||
// Get the descriptions for all the active completion rules for the module.
|
||||
if ($ruledescriptions = $mod->get_completion_active_rule_descriptions()) {
|
||||
if ($ruledescriptions = $this->get_completion_active_rule_descriptions($mod)) {
|
||||
foreach ($ruledescriptions as $ruledescription) {
|
||||
$strings['string'] .= \html_writer::empty_tag('br') . $ruledescription;
|
||||
}
|
||||
@@ -139,8 +148,44 @@ class manager {
|
||||
return $strings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the descriptions for all active conditional completion rules for the current module.
|
||||
*
|
||||
* @param cm_info|stdClass $moduledata either instance of cm_info (with 'customcompletionrules' in customdata) or
|
||||
* object with fields ->completion, ->completionview, ->completionexpected, ->completionusegrade
|
||||
* and ->customdata['customcompletionrules']
|
||||
* @return array $activeruledescriptions an array of strings describing the active completion rules.
|
||||
*/
|
||||
protected function get_completion_active_rule_descriptions($moduledata) {
|
||||
$activeruledescriptions = [];
|
||||
|
||||
// Generate the description strings for the core conditional completion rules (if set).
|
||||
if (!empty($moduledata->completionview)) {
|
||||
$activeruledescriptions[] = get_string('completionview_desc', 'core_completion');
|
||||
}
|
||||
if ($moduledata instanceof cm_info && !is_null($moduledata->completiongradeitemnumber) ||
|
||||
($moduledata instanceof stdClass && !empty($moduledata->completionusegrade))) {
|
||||
$activeruledescriptions[] = get_string('completionusegrade_desc', 'core_completion');
|
||||
}
|
||||
|
||||
// Now, ask the module to provide descriptions for its custom conditional completion rules.
|
||||
if ($customruledescriptions = component_callback($moduledata->modname,
|
||||
'get_completion_active_rule_descriptions', [$moduledata])) {
|
||||
$activeruledescriptions = array_merge($activeruledescriptions, $customruledescriptions);
|
||||
}
|
||||
|
||||
if (!empty($moduledata->completionexpected)) {
|
||||
$activeruledescriptions[] = get_string('completionexpecteddesc', 'core_completion',
|
||||
userdate($moduledata->completionexpected));
|
||||
}
|
||||
|
||||
return $activeruledescriptions;
|
||||
}
|
||||
|
||||
public function get_activities_and_resources() {
|
||||
global $DB, $OUTPUT;
|
||||
global $DB, $OUTPUT, $CFG;
|
||||
require_once($CFG->dirroot.'/course/lib.php');
|
||||
|
||||
// Get enabled activities and resources.
|
||||
$modules = $DB->get_records('modules', ['visible' => 1], 'name ASC');
|
||||
$data = new stdClass();
|
||||
@@ -150,9 +195,16 @@ class manager {
|
||||
// Add icon information.
|
||||
$data->modules = array_values($modules);
|
||||
$coursecontext = context_course::instance($this->courseid);
|
||||
$canmanage = has_capability('moodle/course:manageactivities', $coursecontext);
|
||||
$course = get_course($this->courseid);
|
||||
foreach ($data->modules as $module) {
|
||||
$module->icon = $OUTPUT->pix_url('icon', $module->name)->out();
|
||||
$module->formatedname = format_string(get_string('pluginname', 'mod_' . $module->name), true, ['context' => $coursecontext]);
|
||||
$module->formattedname = format_string(get_string('modulenameplural', 'mod_' . $module->name),
|
||||
true, ['context' => $coursecontext]);
|
||||
$module->canmanage = $canmanage && \course_allowed_module($course, $module->name);
|
||||
$defaults = self::get_default_completion($course, $module, false);
|
||||
$defaults->modname = $module->name;
|
||||
$module->completionstatus = $this->get_completion_detail($defaults);
|
||||
}
|
||||
|
||||
return $data;
|
||||
@@ -186,7 +238,7 @@ class manager {
|
||||
* 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.) -
|
||||
* @param bool $updateinstances 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) {
|
||||
@@ -267,4 +319,93 @@ class manager {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Saves default completion from edit form to all selected module types
|
||||
*
|
||||
* @param stdClass $data data received from the core_completion_bulkedit_form
|
||||
* @param bool $updatecustomrules if we need to update the custom rules of the module -
|
||||
* if no module-specific completion rules were added to the form, update of the module table is not needed.
|
||||
*/
|
||||
public function apply_default_completion($data, $updatecustomrules) {
|
||||
global $DB;
|
||||
|
||||
$courseid = $data->id;
|
||||
$coursecontext = context_course::instance($courseid);
|
||||
if (!$modids = $data->modids) {
|
||||
return;
|
||||
}
|
||||
$defaults = ['completion' => COMPLETION_DISABLED, 'completionview' => COMPLETION_VIEW_NOT_REQUIRED,
|
||||
'completionexpected' => 0, 'completionusegrade' => 0];
|
||||
|
||||
$data = (array)$data;
|
||||
|
||||
if ($updatecustomrules) {
|
||||
$customdata = array_diff_key($data, $defaults);
|
||||
$data['customrules'] = $customdata ? json_encode($customdata) : null;
|
||||
$defaults['customrules'] = null;
|
||||
}
|
||||
$data = array_intersect_key($data, $defaults);
|
||||
|
||||
// Get names of the affected modules.
|
||||
list($modidssql, $params) = $DB->get_in_or_equal($modids);
|
||||
$params[] = 1;
|
||||
$modules = $DB->get_records_select_menu('modules', 'id ' . $modidssql . ' and visible = ?', $params, '', 'id, name');
|
||||
|
||||
foreach ($modids as $modid) {
|
||||
if (!array_key_exists($modid, $modules)) {
|
||||
continue;
|
||||
}
|
||||
if ($defaultsid = $DB->get_field('course_completion_defaults', 'id', ['course' => $courseid, 'module' => $modid])) {
|
||||
$DB->update_record('course_completion_defaults', $data + ['id' => $defaultsid]);
|
||||
} else {
|
||||
$defaultsid = $DB->insert_record('course_completion_defaults', $data + ['course' => $courseid, 'module' => $modid]);
|
||||
}
|
||||
// Trigger event.
|
||||
\core\event\completion_defaults_updated::create([
|
||||
'objectid' => $defaultsid,
|
||||
'context' => $coursecontext,
|
||||
'other' => ['modulename' => $modules[$modid]],
|
||||
])->trigger();
|
||||
// Add notification.
|
||||
\core\notification::add(get_string('defaultcompletionupdated', 'completion',
|
||||
get_string("modulenameplural", $modules[$modid])), \core\notification::SUCCESS);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns default completion rules for given module type in the given course
|
||||
*
|
||||
* @param stdClass $course
|
||||
* @param stdClass $module
|
||||
* @param bool $flatten if true all module custom completion rules become properties of the same object,
|
||||
* otherwise they can be found as array in ->customdata['customcompletionrules']
|
||||
* @return stdClass
|
||||
*/
|
||||
public static function get_default_completion($course, $module, $flatten = true) {
|
||||
global $DB, $CFG;
|
||||
if ($data = $DB->get_record('course_completion_defaults', ['course' => $course->id, 'module' => $module->id],
|
||||
'completion, completionview, completionexpected, completionusegrade, customrules')) {
|
||||
if ($data->customrules && ($customrules = @json_decode($data->customrules, true))) {
|
||||
if ($flatten) {
|
||||
foreach ($customrules as $key => $value) {
|
||||
$data->$key = $value;
|
||||
}
|
||||
} else {
|
||||
$data->customdata['customcompletionrules'] = $customrules;
|
||||
}
|
||||
}
|
||||
unset($data->customrules);
|
||||
} else {
|
||||
$data = new stdClass();
|
||||
$data->completion = COMPLETION_TRACKING_NONE;
|
||||
if ($CFG->completiondefault) {
|
||||
$completion = new \completion_info(get_fast_modinfo($course->id)->get_course());
|
||||
if ($completion->is_enabled() && plugin_supports('mod', $module->name, FEATURE_MODEDIT_DEFAULT_COMPLETION, true)) {
|
||||
$data->completion = COMPLETION_TRACKING_MANUAL;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user