Skip to content

Commit

Permalink
issue #5
Browse files Browse the repository at this point in the history
  • Loading branch information
rdebleu committed Jul 21, 2022
1 parent 9bfd4a0 commit e65c6d4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 33 deletions.
13 changes: 8 additions & 5 deletions format.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@

// Make sure all sections are created.
course_create_sections_if_missing($course, range(0, $course->numsections));

$renderer = $PAGE->get_renderer('format_masonry');
$outputclass = $format->get_output_classname('content');
$widget = new $outputclass($format);
echo $renderer->render($widget);
if ($PAGE->user_is_editing()) {
$PAGE->requires->js('/course/format/masonry/formatedit.js');
$outputclass = str_ireplace('masonry', 'topics', $outputclass);
$widget = new $outputclass($format);
$renderer = $PAGE->get_renderer('format_topics');
echo $renderer->render($widget);
$PAGE->requires->js('/course/format/topics/format.js');
} else {
$widget = new $outputclass($format);
$renderer = $PAGE->get_renderer('format_masonry');
echo $renderer->render($widget);
$PAGE->requires->js_init_call('M.masonry.init',
[[
'node' => '.masonry',
Expand Down
10 changes: 5 additions & 5 deletions formatedit.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ M.course.format.get_config = function() {
*/
M.course.format.swap_sections = function(Y, node1, node2) {
var CSS = {
COURSECONTENT: 'course-content',
SECTIONADDMENUS: 'section_add_menus'
COURSECONTENT: '.course-content',
SECTIONADDMENUS: '.section_add_menus'
};

var sectionlist = Y.Node.all('.' + CSS.COURSECONTENT + ' ' + M.course.format.get_section_selector(Y));
var sectionlist = Y.Node.all(CSS.COURSECONTENT + M.course.format.get_section_selector(Y));
// Swap the non-ajax menus, noting these are not always present (depends on theme and user prefs).
if (sectionlist.item(node1).one('.' + CSS.SECTIONADDMENUS)) {
sectionlist.item(node1).one('.' + CSS.SECTIONADDMENUS).swap(sectionlist.item(node2).one('.' + CSS.SECTIONADDMENUS));
if (sectionlist.item(node1).one(CSS.SECTIONADDMENUS)) {
sectionlist.item(node1).one(CSS.SECTIONADDMENUS).swap(sectionlist.item(node2).one(CSS.SECTIONADDMENUS));
}
};

Expand Down
65 changes: 44 additions & 21 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class format_masonry extends format_topics {
* @return renderer_base
*/
public function get_renderer(moodle_page $page) {
if ($page->user_is_editing()) {
return $page->get_renderer('format_topics');
}
return $page->get_renderer('format_masonry');
}

Expand Down Expand Up @@ -179,13 +182,36 @@ public function uses_indentation(): bool {
return false;
}

/**
* Prepares the templateable object to display section name.
*
* @param \section_info|\stdClass $section
* @param bool $linkifneeded
* @param bool $editable
* @param null|lang_string|string $edithint
* @param null|lang_string|string $editlabel
* @return inplace_editable
*/
public function inplace_editable_render_section_name($section, $linkifneeded = true,
$editable = null, $edithint = null, $editlabel = null) {
if (empty($edithint)) {
$edithint = new lang_string('editsectionname', 'format_topics');
}
if (empty($editlabel)) {
$title = get_section_name($section->course, $section);
$editlabel = new lang_string('newsectionname', 'format_topics', $title);
}
return parent::inplace_editable_render_section_name($section, $linkifneeded, $editable, $edithint, $editlabel);
}

/**
* This course format does not support drag and drop.
*
* @return bool if the course format uses components.
*/
public function supports_components() {
return false;
// Needed for Moving modules.
return true;
}

/**
Expand Down Expand Up @@ -216,26 +242,23 @@ public function get_config_for_external() {
public function can_delete_section($section) {
return true;
}
}

/**
* Callback used in WS core_course_edit_section when teacher performs an AJAX action on a section (show/hide).
*
* @param section_info|stdClass $section
* @param string $action
* @param int $sr
* @return null|array any data for the Javascript post-processor (must be json-encodeable)
*/
public function section_action($section, $action, $sr) {
global $PAGE;

if ($action === 'deleteSection') {
// Format 'topics' allows to set and remove markers in addition to common section actions.
require_capability('moodle/course:setcurrentsection', context_course::instance($this->courseid));
$rv = parent::delete_section($section);
} else {
$rv = parent::section_action($section, $action, $sr);
}
return $rv;
/**
* Implements callback inplace_editable() allowing to edit values in-place.
*
* @param string $itemtype
* @param int $itemid
* @param mixed $newvalue
* @return inplace_editable
*/
function format_masonry_inplace_editable($itemtype, $itemid, $newvalue) {
global $DB, $CFG;
require_once($CFG->dirroot . '/course/lib.php');
if ($itemtype === 'sectionname' || $itemtype === 'sectionnamenl') {
$section = $DB->get_record_sql(
'SELECT s.* FROM {course_sections} s JOIN {course} c ON s.course = c.id WHERE s.id = ? AND c.format = ?',
[$itemid, 'masonry'], MUST_EXIST);
return course_get_format($section->course)->inplace_editable_update_section_name($section, $itemtype, $newvalue);
}

}
4 changes: 2 additions & 2 deletions tests/masonry_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function test_format_editing() {
$USER->editing = true;
$PAGE->set_context(\context_course::instance($this->course->id));
$PAGE->get_renderer('core', 'course');
$this->assertInstanceOf('format_masonry\output\renderer', $format->get_renderer($PAGE));
$this->assertInstanceOf('format_topics\output\renderer', $format->get_renderer($PAGE));
$course = $this->course;
sesskey();
$_POST['marker'] = 2;
Expand All @@ -257,7 +257,7 @@ public function test_other() {
$this->assertCount(6, $format->course_format_options());
$this->assertTrue($format->allow_stealth_module_visibility(null, null));
$this->assertFalse($format->uses_indentation());
$this->assertFalse($format->supports_components());
$this->assertTrue($format->supports_components());
$this->assertFalse($format->uses_course_index());
$this->assertCount(6, $format->get_config_for_external());
}
Expand Down

0 comments on commit e65c6d4

Please sign in to comment.