Permalink
Browse files

MDL-36619 My Home: Fixed course movement when JS is turned off

  • Loading branch information...
Rajesh Taneja
Rajesh Taneja committed Nov 15, 2012
1 parent a5ec499 commit 9e1d4eda65cfe2c2c6bb9f9eebf5eae43c933ef5
@@ -85,9 +85,6 @@ public function get_content() {
// For each course, build category cache.
$this->content->text .= $renderer->course_overview($sortedcourses, $overviews);
$this->content->text .= $renderer->hidden_courses($totalcourses - count($sortedcourses));
- if ($this->page->user_is_editing() && ajaxenabled()) {
- $this->page->requires->js_init_call('M.block_course_overview.add_handles');
- }
}
return $this->content;
@@ -37,7 +37,11 @@
$string['hiddencoursecountplural'] = 'You have {$a} hidden courses';
$string['message'] = 'message';
$string['messages'] = 'messages';
+$string['movecourse'] = 'Move course: {$a}';
$string['movecoursehere'] = 'Move course here';
+$string['movetofirst'] = 'Move {$a} course to top';
+$string['moveafterhere'] = 'Move {$a->movingcoursename} course after {$a->currentcoursename}';
+$string['movingcourse'] = 'You are moving: {$a->fullname} ({$a->cancellink})';
$string['numtodisplay'] = 'Number of courses to display: ';
$string['otherexpanded'] = 'Other Courses Expanded';
$string['pluginname'] = 'Course overview';
@@ -2,12 +2,23 @@ M.block_course_overview = {}
M.block_course_overview.add_handles = function(Y) {
M.block_course_overview.Y = Y;
+ var MOVEICON = {
+ pix: "i/move_2d",
+ component: 'moodle'
+ };
+
YUI().use('dd-constrain', 'dd-proxy', 'dd-drop', 'dd-plugin', function(Y) {
//Static Vars
var goingUp = false, lastY = 0;
- var list = Y.Node.all('#course_list .coursebox');
+ var list = Y.Node.all('.course_list .coursebox');
list.each(function(v, k) {
+ // Replace move link and image with move_2d image.
+ var imagenode = v.one('.course_title .move a img');
+ imagenode.setAttribute('src', M.util.image_url(MOVEICON.pix, MOVEICON.component));
+ imagenode.addClass('cursor');
+ v.one('.course_title .move a').replace(imagenode);
+
var dd = new Y.DD.Drag({
node: v,
target: {
@@ -16,18 +27,11 @@ M.block_course_overview.add_handles = function(Y) {
}).plug(Y.Plugin.DDProxy, {
moveOnEnd: false
}).plug(Y.Plugin.DDConstrained, {
- constrain2node: '#course_list'
+ constrain2node: '.course_list'
});
dd.addHandle('.course_title .move');
});
- var drops = Y.Node.all('#coursebox');
- drops.each(function(v, k) {
- var tar = new Y.DD.Drop({
- node: v
- });
- });
-
Y.DD.DDM.on('drag:start', function(e) {
//Get our drag object
var drag = e.target;
@@ -101,7 +105,7 @@ M.block_course_overview.add_handles = function(Y) {
M.block_course_overview.save = function() {
var Y = M.block_course_overview.Y;
- var sortorder = Y.one('#course_list').get('children').getAttribute('id');
+ var sortorder = Y.one('.course_list').get('children').getAttribute('id');
for (var i = 0; i < sortorder.length; i++) {
sortorder[i] = sortorder[i].substring(7);
}
@@ -27,47 +27,34 @@
require_sesskey();
require_login();
-$source = required_param('source', PARAM_INT);
-$move = required_param('move', PARAM_INT);
-
-list($courses_sorted, $sitecourses, $coursecount) = block_course_overview_get_sorted_courses();
-$sortorder = array_keys($courses_sorted);
-// Now resort based on new weight for chosen course.
-$neworder = array();
-
-$sourcekey = array_search($source, $sortorder);
-if ($sourcekey === false) {
- print_error("invalidcourseid", null, null, $source);
+$coursetomove = required_param('courseid', PARAM_INT);
+$moveto = required_param('moveto', PARAM_INT);
+
+list($courses, $sitecourses, $coursecount) = block_course_overview_get_sorted_courses();
+$sortedcourses = array_keys($courses);
+
+$currentcourseindex = array_search($coursetomove, $sortedcourses);
+// If coursetomove is not found or moveto < 0 or > count($sortedcourses) then throw error.
+if ($currentcourseindex === false) {
+ print_error("invalidcourseid", null, null, $coursetomove);
+} else if (($moveto < 0) || ($moveto >= count($sortedcourses))) {
+ print_error("invalidaction");
}
-$destination = $sourcekey + $move;
-if ($destination < 0) {
- print_error("listcantmoveup");
-} else if ($destination >= count($courses_sorted)) {
- print_error("listcantmovedown");
+// If current course index is same as destination index then don't do anything.
+if ($currentcourseindex === $moveto) {
+ redirect(new moodle_url('/my/index.php'));
}
// Create neworder list for courses.
-unset($sortorder[$sourcekey]);
-if ($move == -1) {
- if ($destination > 0) {
- $neworder = array_slice($sortorder, 0, $destination, true);
- }
- $neworder[] = $source;
- $remaningcourses = array_slice($sortorder, $destination);
- foreach ($remaningcourses as $courseid) {
- $neworder[] = $courseid;
- }
-} else if (($move == 1)) {
- $neworder = array_slice($sortorder, 0, $destination);
- $neworder[] = $source;
- if (($destination) < count($courses_sorted)) {
- $remaningcourses = array_slice($sortorder, $destination);
- foreach ($remaningcourses as $courseid) {
- $neworder[] = $courseid;
- }
- }
-}
+$neworder = array();
-block_course_overview_update_myorder($neworder);
+unset($sortedcourses[$currentcourseindex]);
+$neworder = array_slice($sortedcourses, 0, $moveto, true);
+$neworder[] = $coursetomove;
+$remaningcourses = array_slice($sortedcourses, $moveto);
+foreach ($remaningcourses as $courseid) {
+ $neworder[] = $courseid;
+}
+block_course_overview_update_myorder(array_values($neworder));
redirect(new moodle_url('/my/index.php'));
@@ -41,67 +41,64 @@ class block_course_overview_renderer extends plugin_renderer_base {
public function course_overview($courses, $overviews) {
$html = '';
$config = get_config('block_course_overview');
-
- $html .= html_writer::start_tag('div', array('id' => 'course_list'));
+ $ismovingcourse = false;
$courseordernumber = 0;
$maxcourses = count($courses);
- // Intialize string/icon etc if user is editing.
- $url = null;
- $moveicon = null;
- $moveup[] = null;
- $movedown[] = null;
- if ($this->page->user_is_editing()) {
+ $userediting = false;
+ // Intialise string/icon etc if user is editing and courses > 1
+ if ($this->page->user_is_editing() && (count($courses) > 1)) {
+ $userediting = true;
+ // If ajaxenabled then include DND JS and replace link with move image.
if (ajaxenabled()) {
- $moveicon = html_writer::tag('div',
- html_writer::empty_tag('img',
- array('src' => $this->pix_url('i/move_2d')->out(false),
- 'alt' => get_string('move'), 'class' => 'cursor',
- 'title' => get_string('move'))
- ), array('class' => 'move')
- );
- } else {
- $url = new moodle_url('/blocks/course_overview/move.php', array('sesskey' => sesskey()));
- $moveup['str'] = get_string('moveup');
- $moveup['icon'] = $this->pix_url('t/up');
- $movedown['str'] = get_string('movedown');
- $movedown['icon'] = $this->pix_url('t/down');
+ $this->page->requires->js_init_call('M.block_course_overview.add_handles');
}
+
+ // Check if course is moving
+ $ismovingcourse = optional_param('movecourse', FALSE, PARAM_BOOL);
+ $movingcourseid = optional_param('courseid', 0, PARAM_INT);
+ }
+
+ // Render first movehere icon.
+ if ($ismovingcourse) {
+ // Remove movecourse param from url.
+ $this->page->ensure_param_not_in_url('movecourse');
+
+ // Show moving course notice, so user knows what is being moved.
+ $html .= $this->output->box_start('notice');
+ $a = new stdClass();
+ $a->fullname = $courses[$movingcourseid]->fullname;
+ $a->cancellink = html_writer::link($this->page->url, get_string('cancel'));
+ $html .= get_string('movingcourse', 'block_course_overview', $a);
+ $html .= $this->output->box_end();
+
+ $moveurl = new moodle_url('/blocks/course_overview/move.php',
+ array('sesskey' => sesskey(), 'moveto' => 0, 'courseid' => $movingcourseid));
+ // Create move icon, so it can be used.
+ $movetofirsticon = html_writer::empty_tag('img',
+ array('src' => $this->output->pix_url('movehere'),
+ 'alt' => get_string('movetofirst', 'block_course_overview', $courses[$movingcourseid]->fullname),
+ 'title' => get_string('movehere')));
+ $moveurl = html_writer::link($moveurl, $movetofirsticon);
+ $html .= html_writer::tag('div', $moveurl, array('class' => 'movehere'));
}
foreach ($courses as $key => $course) {
+ // If moving course, then don't show course which needs to be moved.
+ if ($ismovingcourse && ($course->id == $movingcourseid)) {
+ continue;
+ }
$html .= $this->output->box_start('coursebox', "course-{$course->id}");
$html .= html_writer::start_tag('div', array('class' => 'course_title'));
- // Ajax enabled then add moveicon html
- if (!is_null($moveicon)) {
- $html .= $moveicon;
- } else if (!is_null($url)) {
- // Add course id to move link
- $url->param('source', $course->id);
- $html .= html_writer::start_tag('div', array('class' => 'moveicons'));
- // Add an arrow to move course up.
- if ($courseordernumber > 0) {
- $url->param('move', -1);
- $html .= html_writer::link($url,
- html_writer::empty_tag('img', array('src' => $moveup['icon'],
- 'class' => 'up', 'alt' => $moveup['str'])),
- array('title' => $moveup['str'], 'class' => 'moveup'));
- } else {
- // Add a spacer to keep keep down arrow icons at right position.
- $html .= html_writer::empty_tag('img', array('src' => $this->pix_url('spacer'),
- 'class' => 'movedownspacer'));
- }
- // Add an arrow to move course down.
- if ($courseordernumber <= $maxcourses-2) {
- $url->param('move', 1);
- $html .= html_writer::link($url, html_writer::empty_tag('img',
- array('src' => $movedown['icon'], 'class' => 'down', 'alt' => $movedown['str'])),
- array('title' => $movedown['str'], 'class' => 'movedown'));
- } else {
- // Add a spacer to keep keep up arrow icons at right position.
- $html .= html_writer::empty_tag('img', array('src' => $this->pix_url('spacer'),
- 'class' => 'moveupspacer'));
- }
- $html .= html_writer::end_tag('div');
+ // If user is editing, then add move icons.
+ if ($userediting && !$ismovingcourse) {
+ $moveicon = html_writer::empty_tag('img',
+ array('src' => $this->pix_url('t/move')->out(false),
+ 'alt' => get_string('movecourse', 'block_course_overview', $course->fullname),
+ 'title' => get_string('move')));
+ $moveurl = new moodle_url($this->page->url, array('sesskey' => sesskey(), 'movecourse' => 1, 'courseid' => $course->id));
+ $moveurl = html_writer::link($moveurl, $moveicon);
+ $html .= html_writer::tag('div', $moveurl, array('class' => 'move'));
+
}
$attributes = array('title' => s($course->fullname));
@@ -125,17 +122,30 @@ public function course_overview($courses, $overviews) {
}
}
- if (isset($overviews[$course->id])) {
+ // If user is moving courses, then down't show overview.
+ if (isset($overviews[$course->id]) && !$ismovingcourse) {
$html .= $this->activity_display($course->id, $overviews[$course->id]);
}
$html .= $this->output->box('', 'flush');
$html .= $this->output->box_end();
$courseordernumber++;
+ if ($ismovingcourse) {
+ $moveurl = new moodle_url('/blocks/course_overview/move.php',
+ array('sesskey' => sesskey(), 'moveto' => $courseordernumber, 'courseid' => $movingcourseid));
+ $a = new stdClass();
+ $a->movingcoursename = $courses[$movingcourseid]->fullname;
+ $a->currentcoursename = $course->fullname;
+ $movehereicon = html_writer::empty_tag('img',
+ array('src' => $this->output->pix_url('movehere'),
+ 'alt' => get_string('moveafterhere', 'block_course_overview', $a),
+ 'title' => get_string('movehere')));
+ $moveurl = html_writer::link($moveurl, $movehereicon);
+ $html .= html_writer::tag('div', $moveurl, array('class' => 'movehere'));
+ }
}
- $html .= html_writer::end_tag('div');
-
- return $html;
+ // Wrap course list in a div and return.
+ return html_writer::tag('div', $html, array('class' => 'course_list'));
}
/**
@@ -64,36 +64,7 @@
padding: 2px 10px;
}
-.editing .block_course_overview .moveicons {
- display: block;
- float: left;
- padding-right: 5px;
-}
-.dir-rtl.editing .block_course_overview .moveicons {
- float:right;
-}
-
-.editing .block_course_overview .moveup {
- padding-right: 5px;
-}
-
-.editing .block_course_overview .movedownspacer {
- float: left;
- width: 14px;
-}
-.dir-rtl.editing .block_course_overview .movedownspacer {
- float:right;
-}
-
-.editing .block_course_overview .moveupspacer {
- float: right;
- width: 14px;
-}
-.dir-rtl.editing .block_course_overview .moveupspacer {
- float:left;
-}
-
-.block_course_overview #course_list {
+.block_course_overview .course_list {
width: 100%;
}
@@ -122,10 +93,6 @@
text-align: right;
}
-.block_course_overview .coursemovetarget {
- display: block;
- height: 1em;
- margin-bottom: 1em;
- border-width: 2px;
- border-style: dashed;
+.block_course_overview .content .course_list .movehere{
+ margin-bottom: 15px;
}
@@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
-$plugin->version = 2012121000; // The current plugin version (Date: YYYYMMDDXX)
+$plugin->version = 2013012300; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2012112900; // Requires this Moodle version
$plugin->component = 'block_course_overview'; // Full name of the plugin (used for diagnostics)

0 comments on commit 9e1d4ed

Please sign in to comment.