Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranrecio committed Oct 28, 2024
1 parent 7a2c10d commit 2c3b1be
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 92 deletions.
56 changes: 43 additions & 13 deletions course/format/classes/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -950,11 +950,33 @@ public function get_view_url($section, $options = array()) {
* @param moodle_url|null $returnurl optional custom return url
* @return \moodle_url
*/
public function get_update_url(?moodle_url $returnurl = null): moodle_url {
public function get_update_url(
string $action,
array $ids = [],
?int $targetsectionid = null,
?int $targetcmid = null,
?moodle_url $returnurl = null
): moodle_url {
$params = [
'courseid' => $this->get_courseid(),
'sesskey' => sesskey(),
'action' => $action,
];

if (count($ids) === 1) {
$params['id'] = reset($ids);
} else {
foreach ($ids as $key => $id) {
$params["ids[]"] = $id;
}
}

if ($targetsectionid) {
$params['sectionid'] = $targetsectionid;
}
if ($targetcmid) {
$params['cmid'] = $targetcmid;
}
if ($returnurl) {
$params['pageurl'] = $returnurl->out_as_local_url();
}
Expand All @@ -968,30 +990,38 @@ public function get_update_url(?moodle_url $returnurl = null): moodle_url {
* so we must translate to an old non-ajax url while non-ajax
* course editing is still supported.
*
* @deprecated since Moodle 5.0
* @todo Remove this method in Moodle 6.0 (MDL-83530).
*
* @param string $action action name the reactive action
* @param cm_info $cm course module
* @return moodle_url
*/
#[\core\attribute\deprecated(
replacement: 'core_courseformat\output\local\content\cm\controlmenu',
since: '5.0',
mdl: 'MDL-83527',
reason: 'Replaced by get_update_url.',
final: true,
)]
public function get_non_ajax_cm_action_url(string $action, cm_info $cm): moodle_url {
\core\deprecation::emit_deprecation_if_present([$this, __FUNCTION__]);
$nonajaxactions = [
'cmDelete' => 'delete',
'cmDuplicate' => 'duplicate',
'cmHide' => 'hide',
'cmShow' => 'show',
'cmStealth' => 'stealth',
'cmDelete' => 'cm_delete',
'cmDuplicate' => 'cm_duplicate',
'cmHide' => 'cm_hide',
'cmShow' => 'cm_show',
'cmStealth' => 'cm_stealth',
];
if (!isset($nonajaxactions[$action])) {
throw new coding_exception('Unknown activity action: ' . $action);
}
$nonajaxaction = $nonajaxactions[$action];
$nonajaxurl = new moodle_url(
'/course/mod.php',
['sesskey' => sesskey(), $nonajaxaction => $cm->id]
return $this->get_update_url(
action: $nonajaxaction,
ids: [$cm->id],
returnurl: $this->get_view_url($this->get_sectionnum(), ['navigation' => true]),
);
if (!is_null($this->get_sectionid())) {
$nonajaxurl->param('sr', $this->get_sectionnum());
}
return $nonajaxurl;
}

/**
Expand Down
43 changes: 19 additions & 24 deletions course/format/classes/output/local/content/cm/controlmenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,10 @@ protected function get_cm_moveend_item(): ?action_menu_link {
return null;
}

$url = new url(
'/course/mod.php',
[
'sesskey' => sesskey(),
'sr' => $this->mod->sectionnum,
'id' => $this->mod->id,
'indent' => 1,
],
$url = $this->format->get_update_url(
action: 'cm_moveright',
ids: [$this->mod->id],
returnurl: $this->baseurl,
);

$icon = (right_to_left()) ? 't/left' : 't/right';
Expand Down Expand Up @@ -272,14 +268,10 @@ protected function get_cm_movestart_item(): ?action_menu_link {
return null;
}

$url = new url(
'/course/mod.php',
[
'sesskey' => sesskey(),
'sr' => $this->mod->sectionnum,
'id' => $this->mod->id,
'indent' => -1,
],
$url = $this->format->get_update_url(
action: 'cm_moveleft',
ids: [$this->mod->id],
returnurl: $this->baseurl,
);

$icon = (right_to_left()) ? 't/right' : 't/left';
Expand Down Expand Up @@ -330,8 +322,14 @@ protected function get_cm_duplicate_item(): ?action_menu_link {
return null;
}

$url = $this->format->get_update_url(
action: 'cm_duplicate',
ids: [$this->mod->id],
returnurl: $this->baseurl,
);

return new action_menu_link_secondary(
url: new url($this->baseurl, ['duplicate' => $this->mod->id]),
url: $url,
icon: new pix_icon('t/copy', ''),
text: get_string('duplicate'),
attributes: [
Expand Down Expand Up @@ -402,13 +400,10 @@ protected function get_cm_delete_item(): ?action_menu_link {
return null;
}

$url = new url(
'/course/mod.php',
[
'sesskey' => sesskey(),
'delete' => $this->mod->id,
'sr' => $this->mod->sectionnum,
],
$url = $this->format->get_update_url(
action: 'cm_delete',
ids: [$this->mod->id],
returnurl: $this->baseurl,
);

return new action_menu_link_secondary(
Expand Down
26 changes: 17 additions & 9 deletions course/format/classes/output/local/content/cm/groupmode.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,17 @@ public function get_choice_list(): choicelist {
$choice->add_option(
NOGROUPS,
get_string('groupsnone', 'group'),
$this->get_option_data(null, 'cmNoGroups', $this->mod->id)
$this->get_option_data(null, 'cmNoGroups', 'cm_nogroups')
);
$choice->add_option(
SEPARATEGROUPS,
get_string('groupsseparate', 'group'),
$this->get_option_data('groupsseparate', 'cmSeparateGroups', $this->mod->id)
$this->get_option_data('groupsseparate', 'cmSeparateGroups', 'cm_separategroups')
);
$choice->add_option(
VISIBLEGROUPS,
get_string('groupsvisible', 'group'),
$this->get_option_data('groupsvisible', 'cmVisibleGroups', $this->mod->id)
$this->get_option_data('groupsvisible', 'cmVisibleGroups', 'cm_visiblegroups')
);
$choice->set_selected_value($this->mod->effectivegroupmode);
return $choice;
Expand All @@ -177,18 +177,26 @@ public function get_choice_list(): choicelist {
/**
* Get the data for the option.
* @param string|null $name the name of the option
* @param string $action the state action of the option
* @param int $id the id of the module
* @param string $mutation the mutation name
* @param string $stateaction the state action name
* @return array
*/
private function get_option_data(?string $name, string $action, int $id): array {
private function get_option_data(?string $name, string $mutation, string $stateaction): array {
$format = $this->format;
$nonajaxurl = $format->get_update_url(
action: $stateaction,
ids: [$this->mod->id],
returnurl: $format->get_view_url($format->get_sectionnum(), ['navigation' => true]),
);

return [
'description' => ($name) ? get_string("groupmode_{$name}_help", 'group') : null,
// The dropdown icons are decorative, so we don't need to provide alt text.
'icon' => $this->get_action_icon($action),
'icon' => $this->get_action_icon($mutation),
'url' => $nonajaxurl,
'extras' => [
'data-id' => $id,
'data-action' => $action,
'data-id' => $this->mod->id,
'data-action' => $mutation,
]
];
}
Expand Down
24 changes: 15 additions & 9 deletions course/format/classes/output/local/content/cm/visibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,20 +236,20 @@ protected function create_choice_list(): choicelist {
$choice->add_option(
'show',
get_string("availability_{$label}", 'core_courseformat'),
$this->get_option_data($label, 'cmShow')
$this->get_option_data($label, 'cmShow', 'cm_show')
);
}
$choice->add_option(
'hide',
get_string('availability_hide', 'core_courseformat'),
$this->get_option_data('hide', 'cmHide')
$this->get_option_data('hide', 'cmHide', 'cm_hide')
);

if ($CFG->allowstealth && $this->format->allow_stealth_module_visibility($this->mod, $this->section)) {
$choice->add_option(
'stealth',
get_string('availability_stealth', 'core_courseformat'),
$this->get_option_data('stealth', 'cmStealth')
$this->get_option_data('stealth', 'cmStealth', 'cm_stealth')
);
}
return $choice;
Expand All @@ -258,19 +258,25 @@ protected function create_choice_list(): choicelist {
/**
* Get the data for the option.
* @param string $name the name of the option
* @param string $action the state action of the option
* @param string $mutation the mutation name
* @param string $stateaction the state action name
* @return array
*/
private function get_option_data(string $name, string $action): array {
private function get_option_data(string $name, string $mutation, string $stateaction): array {
$format = $this->format;
$nonajaxurl = $format->get_update_url(
action: $stateaction,
ids: [$this->mod->id],
returnurl: $format->get_view_url($format->get_sectionnum(), ['navigation' => true]),
);

return [
'description' => get_string("availability_{$name}_help", 'core_courseformat'),
'icon' => $this->get_icon($name),
// Non-ajax behat is not smart enough to discrimante hidden links
// so we need to keep providing the non-ajax links.
'url' => $this->format->get_non_ajax_cm_action_url($action, $this->mod),
'url' => $nonajaxurl,
'extras' => [
'data-id' => $this->mod->id,
'data-action' => $action,
'data-action' => $mutation,
]
];
}
Expand Down
27 changes: 13 additions & 14 deletions course/format/classes/output/local/content/section/controlmenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,11 @@ protected function get_section_visibility_item(): ?action_menu_link {
}
$sectionreturn = $this->format->get_sectionnum();

$url = clone($this->baseurl);

$strhide = get_string('hide');
$strshow = get_string('show');

if ($this->section->visible) {
$url->param('hide', $this->section->sectionnum);
$stateaction = 'section_hide';
$icon = 'i/show';
$name = $strhide;
$attributes = [
Expand All @@ -205,7 +203,7 @@ protected function get_section_visibility_item(): ?action_menu_link {
'data-swapicon' => 'i/hide',
];
} else {
$url->param('show', $this->section->sectionnum);
$stateaction = 'section_show';
$icon = 'i/hide';
$name = $strshow;
$attributes = [
Expand All @@ -219,6 +217,12 @@ protected function get_section_visibility_item(): ?action_menu_link {
];
}

$url = $this->format->get_update_url(
action: $stateaction,
ids: [$this->section->id],
returnurl: $this->baseurl,
);

return new action_menu_link_secondary(
url: $url,
icon: new pix_icon($icon, ''),
Expand Down Expand Up @@ -300,17 +304,12 @@ protected function get_section_delete_item(): ?action_menu_link {
return null;
}

$params = [
'id' => $this->section->id,
'delete' => 1,
'sesskey' => sesskey(),
];
$params['sr'] ??= $this->format->get_sectionnum();

$url = new url(
'/course/editsection.php',
$params,
$url = $this->format->get_update_url(
action: 'section_delete',
ids: [$this->section->id],
returnurl: $this->baseurl,
);

return new action_menu_link_secondary(
url: $url,
icon: new pix_icon('i/delete', ''),
Expand Down
24 changes: 13 additions & 11 deletions course/format/classes/output/local/content/section/visibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ protected function get_visibility_dropdown(\renderer_base $output): array {
$choice->add_option(
'show',
get_string('availability_show', 'core_courseformat'),
$this->get_option_data('show', 'sectionShow')
$this->get_option_data('show', 'sectionShow', 'section_show')
);
$choice->add_option(
'hide',
get_string('availability_hide', 'core_courseformat'),
$this->get_option_data('hide', 'sectionHide')
$this->get_option_data('hide', 'sectionHide', 'section_hide')
);
$choice->set_selected_value('hide');

Expand All @@ -140,23 +140,25 @@ protected function get_visibility_dropdown(\renderer_base $output): array {
* Get the data for the option.
*
* @param string $name the name of the option
* @param string $action the state action of the option
* @param string $mutation the mutation name
* @param string $stateaction the state action name
* @return array
*/
private function get_option_data(string $name, string $action): array {
$baseurl = course_get_url($this->section->course, $this->section);
$baseurl->param('sesskey', sesskey());
$baseurl->param($action, $this->section->section);
private function get_option_data(string $name, string $mutation, string $stateaction): array {
$format = $this->format;
$nonajaxurl = $format->get_update_url(
action: $stateaction,
ids: [$this->section->id],
returnurl: $format->get_view_url($format->get_sectionnum(), ['navigation' => true]),
);

return [
'description' => get_string("availability_{$name}_help", 'core_courseformat'),
'icon' => $this->get_icon($name),
// Non-ajax behat is not smart enough to discrimante hidden links
// so we need to keep providing the non-ajax links.
'url' => $baseurl,
'url' => $nonajaxurl,
'extras' => [
'data-id' => $this->section->id,
'data-action' => $action,
'data-action' => $mutation,
],
];
}
Expand Down
4 changes: 2 additions & 2 deletions course/format/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
'sesskey' => sesskey(),
]
);
foreach ($ids as $id) {
$currenturl->param('ids[]', $id);
foreach ($ids as $key => $id) {
$currenturl->param("ids[]", $id);
}

require_sesskey();
Expand Down
Loading

0 comments on commit 2c3b1be

Please sign in to comment.