Drag & drop reordering

Drag & drop reordering

by Michael Hughes -
Number of replies: 3
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Right here's a question smile

I have a 2 semester course format, it is inherits from the topics course format, and the *only* difference is that it presents the sections under 2 tabs: Semester 1 and Semester 2.

Without any additional modifications (really) drag and drop works nicely for uploading and reordering *resources*, but it doesn't work for re-ordering sections.

I've implemented this by setting section 0 to be always displayed but each subsequent section is semester 1 or semester 2 based on if it is an odd or even section (by doing $section % 2)

Section 1: Semester 2

Section 2: Semester 1

My renderer then simply does a "continue" on each section that doesn't satisfy this condition.

Now it would appear that the section re-ordering works on the basis of knowing the section being dragged (by it's "id", which apparently is actually it's ordinal position) and the "position" of the destination section that its being dropped on to. 

This is obviously not going to work in this case, because I'm not actually simply moving items up or down, I'm needing to reorder them within the odd or event items of the whole list.

I have a nice piece of PHP that does this perfectly *if* you're not using Javascript (ie the up/down buttons) which is easy to hooking as the renderer for the up/down icons simply points at this script instead of the default one.

How do I get the Drag & drop to have different logic?

Or equally how can I disable *only* the drag & drop section re-ordering (so I get the "static" reorder controls) but keep the d&d upload & resource reordering..

Oh and how can I do this from within the course format only, and *without* hacking the core.

big grin

Average of ratings: -
In reply to Michael Hughes

Re: Drag & drop reordering

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hi Michael,

The starting point for your investigations is the 'format.js' file contained in the course format's folder.  It has functions that are concerned with initialisation and drag and drop.  But unlike PHP, you will need to perform a 'Purge all caches' every time you make a change to see the effect.

I currently do not know how to disable JS D&D in an individual course format.

Cheers,

Gareth

In reply to Gareth J Barnard

Re: Drag & drop reordering

by Michael Hughes -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

This doesn't appear to help me.

It actually appears to work on the Javascript side, with the nodes simply swapping places in the DOM, however the re-ordering actually breaks and I think there may be a problem in the "core".

So...

I'm simply extending the topics format, but skipping every 2nd (ignoring the "general" topic 0 section) section.

If I have

Pos 0: [Section-0]*

Pos 1: [Section-1]*

Pos 2: [Section-2]

Pos 3: [Section-3]*

Pos 4: [Section-4]

Pos 5: [Section-5]*

Items marked * are being displayed and I want to move section 5 to section 3 (Pos 5 -> pos 3). 

Pos 0: [Section-0]*

Pos 1: [Section-1]*

Pos 2: [Section-2]

Pos 3: [Section-5]*

Pos 4: [Section-3]*

Pos 5: [Section-4]

However note this shifts the section-3 down one place into pos 4, so I also need to move section-3 from pos-3 in to pos-5, to prevent things get screwed up:

Pos 0: [Section-0]*

Pos 1: [Section-1]*

Pos 2: [Section-2]

Pos 3: [Section-5]*

Pos 4: [Section-4]

Pos 5: [Section-3]*

I can't find a way to hook in to anywhere to override the re-ordering. The move_section_to() function in course/lib.php defines the move action, which makes it impossible for a course format to define it's own custom section moving logic.

The call to course_get_format($course)->ajax_section_move() is insufficient as it executes *after* the (incorrect) move operations take place.

I would have thought it made more sense for move_section_to() to be implemented against format_base and overridden as appropriate by the course format.

In reply to Michael Hughes

Re: Drag & drop reordering

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Dear Michael,

Ok, the PHP orders the page as you have done in the 'renderer.php' of the course format.  With JavaScript on, 'format.js' in the format gets called with the 'from' and 'to' identifiers for what has been moved on the page.  IT does the reordering at the client side and then uses AJAX to tell the server what it has done.  Therefore you need to debug and understand the nature of that JavaScript in order to see what data is available to it so it can therefore make a suitable decision and order the DOM nodes accordingly.  You may need to add extra attributes in order to accomplish this task.  And check that the AJAX update data is correct.

Or it might be a case that you should not output the intermittent sections at all in the PHP.

Gareth