Skip to content

Commit

Permalink
MDL-58267 completion: style and capabilities cleanup
Browse files Browse the repository at this point in the history
Part of MDL-58138 epic
  • Loading branch information
marinaglancy authored and snake committed Apr 19, 2017
1 parent 7f53e8a commit a64a9f9
Show file tree
Hide file tree
Showing 16 changed files with 155 additions and 90 deletions.
41 changes: 41 additions & 0 deletions completion/classes/bulkedit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,47 @@ protected function get_module_names() {
return $this->_modnames;
}

/**
* 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);
$course = $this->course;
$modname = $cm->modname;

$modmoodleform = "$CFG->dirroot/mod/$modname/mod_form.php";
if (file_exists($modmoodleform)) {
require_once($modmoodleform);
} else {
print_error('noformdesc');
}

list($cmrec, $context, $module, $data, $cw) = get_moduleinfo_data($cm, $course);
$data->return = 0;
$data->sr = 0;
$data->update = $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;
}

/**
* Form definition
*/
Expand Down
41 changes: 41 additions & 0 deletions completion/classes/defaultedit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,47 @@ protected function get_module_names() {
return $this->_modnames;
}

/**
* 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;
}

/**
* Form definition,
*/
Expand Down
36 changes: 1 addition & 35 deletions completion/classes/edit_base_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,41 +83,7 @@ protected function support_grades() {
*
* @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;
}
abstract protected function get_module_form();

/**
* If all selected modules are of the same module type, adds custom completion rules from this module type
Expand Down
40 changes: 40 additions & 0 deletions completion/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
use stdClass;
use context_course;
use cm_info;
use tabobject;
use lang_string;
use moodle_url;

/**
* Bulk activity completion manager class
Expand Down Expand Up @@ -234,6 +237,43 @@ public static function can_edit_bulk_completion($courseorid, $cm = null) {
return false;
}

/**
* @param stdClass|int $courseorid
* @return tabobject[]
*/
public static function get_available_completion_tabs($courseorid) {
$tabs = [];

$courseid = is_object($courseorid) ? $courseorid->id : $courseorid;
$coursecontext = context_course::instance($courseid);

if (has_capability('moodle/course:update', $coursecontext)) {
$tabs[] = new tabobject(
'completion',
new moodle_url('/course/completion.php', ['id' => $courseid]),
new lang_string('coursecompletion', 'completion')
);
}

if (has_capability('moodle/course:manageactivities', $coursecontext)) {
$tabs[] = new tabobject(
'defaultcompletion',
new moodle_url('/course/defaultcompletion.php', ['id' => $courseid]),
new lang_string('defaultcompletion', 'completion')
);
}

if (self::can_edit_bulk_completion($courseorid)) {
$tabs[] = new tabobject(
'bulkcompletion',
new moodle_url('/course/bulkcompletion.php', ['id' => $courseid]),
new lang_string('bulkactivitycompletion', 'completion')
);
}

return $tabs;
}

/**
* Applies completion from the bulk edit form to all selected modules
*
Expand Down
8 changes: 4 additions & 4 deletions course/bulkcompletion.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@
$PAGE->set_heading($course->fullname);
$PAGE->set_pagelayout('admin');

// Get all that stuff I need for the renderer.
// Check access.
if (!core_completion\manager::can_edit_bulk_completion($id)) {
throw new required_capability_exception(context_course::instance($course->id),
'moodle/course:manageactivities', 'nopermission');
require_capability('moodle/course:manageactivities', context_course::instance($course->id));
}

// Get all that stuff I need for the renderer.
$manager = new \core_completion\manager($id);
$bulkcompletiondata = $manager->get_activities_and_headings();

Expand All @@ -72,7 +72,7 @@
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('bulkactivitycompletion', 'completion'));

echo $renderer->navigation($id, 'bulkcompletion');
echo $renderer->navigation($course, 'bulkcompletion');

$PAGE->requires->yui_module('moodle-core-formchangechecker',
'M.core_formchangechecker.init',
Expand Down
27 changes: 2 additions & 25 deletions course/classes/output/bulk_activity_completion_renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,8 @@
*/
class core_course_bulk_activity_completion_renderer extends plugin_renderer_base {

public function navigation($courseid, $page) {
$tabs = [];

if (has_capability('moodle/course:update', context_course::instance($courseid))) {
$tabs[] = new tabobject(
'completion',
new moodle_url('/course/completion.php', ['id' => $courseid]),
get_string('coursecompletion', 'completion')
);

$tabs[] = new tabobject(
'defaultcompletion',
new moodle_url('/course/defaultcompletion.php', ['id' => $courseid]),
get_string('defaultcompletion', 'completion')
);
}

if (core_completion\manager::can_edit_bulk_completion($courseid)) {
$tabs[] = new tabobject(
'bulkcompletion',
new moodle_url('/course/bulkcompletion.php', ['id' => $courseid]),
get_string('bulkactivitycompletion', 'completion')
);
}

public function navigation($courseorid, $page) {
$tabs = core_completion\manager::get_available_completion_tabs($courseorid);
if (count($tabs) > 1) {
return $this->tabtree($tabs, $page);
} else {
Expand Down
8 changes: 5 additions & 3 deletions course/completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@
require_login($course);
$context = context_course::instance($course->id);
if (!has_capability('moodle/course:update', $context)) {
if (core_completion\manager::can_edit_bulk_completion($course)) {
redirect(new moodle_url('/course/bulkcompletion.php', ['id' => $course->id]));
// User is not allowed to modify course completion.
// Check if they can see default completion or edit bulk completion and redirect there.
if ($tabs = core_completion\manager::get_available_completion_tabs($course)) {
redirect($tabs[0]->link);
} else {
require_capability('moodle/course:update', $context);
}
Expand Down Expand Up @@ -161,7 +163,7 @@
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('editcoursecompletionsettings', 'core_completion'));

echo $renderer->navigation($id, 'completion');
echo $renderer->navigation($course, 'completion');

$form->display();

Expand Down
4 changes: 2 additions & 2 deletions course/defaultcompletion.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
print_error('invalidcourseid');
}
require_login($course);
require_capability('moodle/course:update', context_course::instance($course->id));
require_capability('moodle/course:manageactivities', context_course::instance($course->id));

} else {
require_login();
Expand All @@ -67,7 +67,7 @@
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('defaultcompletion', 'completion'));

echo $renderer->navigation($id, 'defaultcompletion');
echo $renderer->navigation($course, 'defaultcompletion');

$PAGE->requires->yui_module('moodle-core-formchangechecker',
'M.core_formchangechecker.init',
Expand Down
5 changes: 2 additions & 3 deletions course/editbulkcompletion.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
$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');
require_capability('moodle/course:manageactivities', context_course::instance($course->id));
}

// Prepare list of modules to be updated.
Expand Down Expand Up @@ -70,7 +69,7 @@
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('bulkactivitycompletion', 'completion'));

echo $renderer->navigation($course->id, 'bulkcompletion');
echo $renderer->navigation($course, 'bulkcompletion');

echo $renderer->edit_bulk_completion($form, $manager->get_activities(array_keys($cms)));

Expand Down
2 changes: 1 addition & 1 deletion course/editdefaultcompletion.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('defaultcompletion', 'completion'));

echo $renderer->navigation($course->id, 'defaultcompletion');
echo $renderer->navigation($course, 'defaultcompletion');

echo $renderer->edit_default_completion($form, $modules);

Expand Down
3 changes: 2 additions & 1 deletion course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3870,7 +3870,8 @@ function course_get_user_administration_options($course, $context) {

$options = new stdClass;
$options->update = has_capability('moodle/course:update', $context);
$options->editcompletion = core_completion\manager::can_edit_bulk_completion($course);
$options->editcompletion = $CFG->enablecompletion && $course->enablecompletion &&
($options->update || count(core_completion\manager::get_available_completion_tabs($course, $context)) > 0);
$options->filters = has_capability('moodle/filter:manage', $context) &&
count(filter_get_available_in_context($context)) > 0;
$options->reports = has_capability('moodle/site:viewreports', $context);
Expand Down
2 changes: 1 addition & 1 deletion course/templates/activityinstance.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<div class="activity-completionstatus col-sm-6">
<div class="col-sm-1">
{{#completionstatus.icon}}
<img src="{{completionstatus.icon}}" class="m-r-2">
<img src="{{completionstatus.icon}}" class="m-r-2" alt=" " role="presentation">
{{/completionstatus.icon}}
{{^completionstatus.icon}}
<span class="m-r-3"></span>
Expand Down
8 changes: 4 additions & 4 deletions course/templates/bulkactivitycompletion.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@
</div>
</div>
<div class="topics">

{{#sections}}

<div class="topic-section m-b-1">
<div class="row m-b-1">
<div class="col-sm-12">
Expand All @@ -72,9 +72,9 @@
</div>
{{> core_course/activityinstance}}
</div>

{{/sections}}

</div>
<input type="hidden" name="id" value="{{courseid}}" />
<input type="hidden" name="sesskey" value="{{sesskey}}" />
Expand Down
9 changes: 5 additions & 4 deletions course/templates/defaultactivitycompletion.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,16 @@
{{#canmanage}}
<div class="module-section m-b-1">
<div class="row m-b-1">
<div class="col-sm-12">
<input type="checkbox" class="m-r-1" name="modids[]" value="{{id}}" aria-label="{{#str}}checkactivity, completion, {{formatedname}}{{/str}}">
<img src={{icon}} />
<div class="col-sm-6">
<label class="accesshide" for="modtype_{{id}}">Select {{formattedname}}</label>
<input id="modtype_{{id}}" type="checkbox" class="m-r-1" name="modids[]" value="{{id}}" aria-label="{{#str}}checkactivity, completion, {{formattedname}}{{/str}}">
<img src="{{icon}}" alt=" " role="presentation" />
<span>{{formattedname}}</span>
</div>
<div class="activity-completionstatus col-sm-6">
<div class="col-sm-1">
{{#completionstatus.icon}}
<img src="{{completionstatus.icon}}" class="m-r-2">
<img src="{{completionstatus.icon}}" class="m-r-2" alt=" " role="presentation">
{{/completionstatus.icon}}
{{^completionstatus.icon}}
<span class="m-r-3"></span>
Expand Down
2 changes: 1 addition & 1 deletion course/templates/editdefaultcompletion.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<div class="row m-b-1">
{{#modules}}
<div class="col-sm-2">
<img src="{{icon}}" class="m-r-1 m-b-1" alt=" " />
<img src="{{icon}}" class="m-r-1 m-b-1" alt=" " role="presentation" />
<span>{{{formattedname}}}</span>
</div>
{{/modules}}
Expand Down
Loading

0 comments on commit a64a9f9

Please sign in to comment.