Skip to content

Commit

Permalink
MDL-52869 course: use inplace_editable for activity names
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Mar 15, 2016
1 parent e8952c5 commit f59f89b
Show file tree
Hide file tree
Showing 20 changed files with 404 additions and 732 deletions.
43 changes: 6 additions & 37 deletions blocks/site_main_menu/block_site_main_menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function get_content() {
require_once($CFG->dirroot.'/course/lib.php');
$context = context_course::instance($course->id);
$isediting = $this->page->user_is_editing() && has_capability('moodle/course:manageactivities', $context);
$courserenderer = $this->page->get_renderer('core', 'course');

/// extra fast view mode
if (!$isediting) {
Expand All @@ -69,32 +70,18 @@ function get_content() {
}

if (!empty($cm->url)) {
$attrs = array();
$attrs['title'] = $cm->modfullname;
$attrs['class'] = $cm->extraclasses . ' activity-action';
if ($cm->onclick) {
// Get on-click attribute value if specified and decode the onclick - it
// has already been encoded for display.
$attrs['onclick'] = htmlspecialchars_decode($cm->onclick);
}
if (!$cm->visible) {
$attrs['class'] .= ' dimmed';
}
$icon = '<img src="' . $cm->get_icon_url() . '" class="icon" alt="" />';
$content = html_writer::link($cm->url, $icon . $cm->get_formatted_name(), $attrs);
$content = html_writer::div($courserenderer->course_section_cm_name($cm), 'activity');
} else {
$content = $cm->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
}

$this->content->items[] = $indent.html_writer::div($content, 'main-menu-content');
$this->content->items[] = $indent . $content;
}
}
return $this->content;
}

// Slow & hacky editing mode.
/** @var core_course_renderer $courserenderer */
$courserenderer = $this->page->get_renderer('core', 'course');
$ismoving = ismoving($course->id);
course_create_sections_if_missing($course, 0);
$modinfo = get_fast_modinfo($course);
Expand All @@ -104,19 +91,16 @@ function get_content() {
$strmovehere = get_string('movehere');
$strmovefull = strip_tags(get_string('movefull', '', "'$USER->activitycopyname'"));
$strcancel= get_string('cancel');
$stractivityclipboard = $USER->activitycopyname;
} else {
$strmove = get_string('move');
}
$editbuttons = '';

if ($ismoving) {
$this->content->icons[] = '<img src="'.$OUTPUT->pix_url('t/move') . '" class="iconsmall" alt="" />';
$this->content->items[] = $USER->activitycopyname.'&nbsp;(<a href="'.$CFG->wwwroot.'/course/mod.php?cancelcopy=true&amp;sesskey='.sesskey().'">'.$strcancel.'</a>)';
}

if (!empty($modinfo->sections[0])) {
$options = array('overflowdiv'=>true);
foreach ($modinfo->sections[0] as $modnumber) {
$mod = $modinfo->cms[$modnumber];
if (!$mod->uservisible) {
Expand Down Expand Up @@ -153,27 +137,12 @@ function get_content() {
} else {
$indent = '';
}
$url = $mod->url;
if (!$url) {
if (!$mod->url) {
$content = $mod->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
} else {
//Accessibility: incidental image - should be empty Alt text
$attrs = array();
$attrs['title'] = $mod->modfullname;
$attrs['class'] = $mod->extraclasses . ' activity-action';
if ($mod->onclick) {
// Get on-click attribute value if specified and decode the onclick - it
// has already been encoded for display.
$attrs['onclick'] = htmlspecialchars_decode($mod->onclick);
}
if (!$mod->visible) {
$attrs['class'] .= ' dimmed';
}

$icon = '<img src="' . $mod->get_icon_url() . '" class="icon" alt="" />';
$content = html_writer::link($url, $icon . $mod->get_formatted_name(), $attrs);
$content = html_writer::div($courserenderer->course_section_cm_name($mod), ' activity');
}
$this->content->items[] = $indent.html_writer::div($content . $editbuttons, 'main-menu-content');
$this->content->items[] = $indent. $content . $editbuttons;
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions blocks/site_main_menu/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@
.block_site_main_menu .footer { margin-top: 1em; }
.block_site_main_menu .section_add_menus noscript div { display: inline;}
.block_site_main_menu .mod-indent,
.block_site_main_menu .main-menu-content { display: table-cell; }
.block_site_main_menu .main-menu-content > .activity-action { display: block; }
.block_site_main_menu .activity { display: table-cell; }
21 changes: 21 additions & 0 deletions blocks/site_main_menu/tests/behat/edit_activities.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@block @block_main_menu
Feature: Edit activities in main menu block
In order to use main menu block
As an admin
I need to add and edit activities there

@javascript
Scenario: Edit name of acitivity in-place in site main menu block
Given I log in as "admin"
And I am on site homepage
And I navigate to "Turn editing on" node in "Front page settings"
When I add a "Forum" to section "0" and I fill the form with:
| Forum name | My forum name |
And I click on "Edit title" "link" in the "//.[contains(@class,'block_site_main_menu')]//li[contains(.,'My forum name')]" "xpath_element"
And I set the field "New name for activity My forum name" to "New forum name"
And I press key "13" in the field "New name for activity My forum name"
Then I should not see "My forum name"
And I should see "New forum name"
And I follow "New forum name"
And I should not see "My forum name"
And I should see "New forum name"
34 changes: 8 additions & 26 deletions blocks/social_activities/block_social_activities.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function get_content() {
}

$course = $this->page->course;
$courserenderer = $this->page->get_renderer('core', 'course');

require_once($CFG->dirroot.'/course/lib.php');

Expand All @@ -58,25 +59,18 @@ function get_content() {
/// extra fast view mode
if (!$isediting) {
if (!empty($modinfo->sections[0])) {
$options = array('overflowdiv'=>true);
foreach($modinfo->sections[0] as $cmid) {
$cm = $modinfo->cms[$cmid];
if (!$cm->uservisible) {
continue;
}

$content = $cm->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
$instancename = $cm->get_formatted_name();

if (!($url = $cm->url)) {
if (!$cm->url) {
$content = $cm->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
$this->content->items[] = $content;
$this->content->icons[] = '';
} else {
$linkcss = $cm->visible ? '' : ' class="dimmed" ';
//Accessibility: incidental image - should be empty Alt text
$icon = '<img src="' . $cm->get_icon_url() . '" class="icon" alt="" />&nbsp;';
$this->content->items[] = '<a title="'.$cm->modplural.'" '.$linkcss.' '.$cm->extra.
' href="' . $url . '">' . $icon . $instancename . '</a>';
$this->content->items[] = html_writer::div($courserenderer->course_section_cm_name($cm), 'activity');
}
}
}
Expand All @@ -85,29 +79,23 @@ function get_content() {


// Slow & hacky editing mode.
/** @var core_course_renderer $courserenderer */
$courserenderer = $this->page->get_renderer('core', 'course');
$ismoving = ismoving($course->id);
$modinfo = get_fast_modinfo($course);
$section = $modinfo->get_section_info(0);

if ($ismoving) {
$strmovehere = get_string('movehere');
$strmovefull = strip_tags(get_string('movefull', '', "'$USER->activitycopyname'"));
$strcancel= get_string('cancel');
$stractivityclipboard = $USER->activitycopyname;
} else {
$strmove = get_string('move');
}
$editbuttons = '';

if ($ismoving) {
$this->content->icons[] = '&nbsp;<img align="bottom" src="'.$OUTPUT->pix_url('t/move') . '" class="iconsmall" alt="" />';
$this->content->items[] = $USER->activitycopyname.'&nbsp;(<a href="'.$CFG->wwwroot.'/course/mod.php?cancelcopy=true&amp;sesskey='.sesskey().'">'.$strcancel.'</a>)';
}

if (!empty($modinfo->sections[0])) {
$options = array('overflowdiv'=>true);
foreach ($modinfo->sections[0] as $modnumber) {
$mod = $modinfo->cms[$modnumber];
if (!$mod->uservisible) {
Expand Down Expand Up @@ -139,19 +127,13 @@ function get_content() {
'<img style="height:16px; width:80px; border:0px" src="'.$OUTPUT->pix_url('movehere') . '" alt="'.$strmovehere.'" /></a>';
$this->content->icons[] = '';
}
$content = $mod->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
$instancename = $mod->get_formatted_name();

$linkcss = $mod->visible ? '' : ' class="dimmed" ';

if (!($url = $mod->url)) {
if (!$mod->url) {
$content = $mod->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
$this->content->items[] = $content . $editbuttons;
$this->content->icons[] = '';
} else {
//Accessibility: incidental image - should be empty Alt text
$icon = '<img src="' . $mod->get_icon_url() . '" class="icon" alt="" />&nbsp;';
$this->content->items[] = '<a title="' . $mod->modfullname . '" ' . $linkcss . ' ' . $mod->extra .
' href="' . $url . '">' . $icon . $instancename . '</a>' . $editbuttons;
$this->content->items[] = html_writer::div($courserenderer->course_section_cm_name($mod), 'activity') .
$editbuttons;
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions blocks/social_activities/tests/behat/edit_activities.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@block @block_social_activities
Feature: Edit activities in social activities block
In order to use social activities block
As a teacher
I need to add and edit activities there

@javascript
Scenario: Edit name of acitivity in-place in social activities block
Given the following "courses" exist:
| fullname | shortname | format |
| Course 1 | C1 | social |
And the following "users" exist:
| username | firstname | lastname |
| user1 | User | One |
And the following "course enrolments" exist:
| user | course | role |
| user1 | C1 | editingteacher |
Given I log in as "user1"
And I follow "Course 1"
And I turn editing mode on
And I set the field "Add an activity to section 'section 0'" to "Forum"
And I set the field "Forum name" to "My forum name"
And I press "Save and return to course"
And I click on "Edit title" "link" in the "//.[contains(@class,'block_social_activities')]//li[contains(.,'My forum name')]" "xpath_element"
And I set the field "New name for activity My forum name" to "New forum name"
And I press key "13" in the field "New name for activity My forum name"
Then I should not see "My forum name" in the "Social activities" "block"
And I should see "New forum name"
And I follow "New forum name"
And I should not see "My forum name"
And I should see "New forum name"
100 changes: 100 additions & 0 deletions course/classes/output/course_module_name.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?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/>.

/**
* Contains class core_tag\output\course_module_name
*
* @package core_course
* @copyright 2016 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_course\output;

use context_module;
use lang_string;
use cm_info;

/**
* Class to prepare a course module name for display and in-place editing
*
* @package core_course
* @copyright 2016 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_module_name extends \core\output\inplace_editable {

/** @var cm_info */
protected $cm;

/** @var array */
protected $displayoptions;

/**
* Constructor.
*
* @param cm_info $cm
* @param bool $editable
* @param array $displayoptions
*/
public function __construct(cm_info $cm, $editable, $displayoptions = array()) {
$this->cm = $cm;
$this->displayoptions = $displayoptions;
$value = $cm->name;
$edithint = new lang_string('edittitle');
$editlabel = new lang_string('newactivityname', '', $cm->get_formatted_name());
$editable = $editable && has_capability('moodle/course:manageactivities',
context_module::instance($cm->id));
parent::__construct(
'core_course', 'activityname', $cm->id, $editable, $value, $value, $edithint, $editlabel);
}

/**
* Export this data so it can be used as the context for a mustache template (core/inplace_editable).
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return array data context for a mustache template
*/
public function export_for_template(\renderer_base $output) {
global $PAGE;
$courserenderer = $PAGE->get_renderer('core', 'course');
$this->displayvalue = $courserenderer->course_section_cm_name_title($this->cm, $this->displayoptions);
if (strval($this->displayvalue) === '') {
$this->editable = false;
}
return parent::export_for_template($output);
}

/**
* Updates course module name
*
* @param int $itemid course module id
* @param string $newvalue new name
* @return static
*/
public static function update($itemid, $newvalue) {
list($course, $cm) = get_course_and_cm_from_cmid($itemid);
$context = context_module::instance($cm->id);
// Check access.
require_login($course, false, $cm, true, true);
require_capability('moodle/course:manageactivities', $context);
// Update value.
set_coursemodule_name($cm->id, $newvalue);
// Return instance.
$cm = get_fast_modinfo($course)->get_cm($cm->id);
return new static($cm, true);
}
}
Loading

0 comments on commit f59f89b

Please sign in to comment.