Permalink
Browse files

MDL-31096 Convert course resource/section javascript to YUI3 Module

AMOS BEGIN
  MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
  MOV [showweekfromothers,moodle],[showfromothers,format_week]
  MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
  MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
  • Loading branch information...
andrewnicols committed Jan 5, 2012
1 parent cd6081f commit ebaa29d107d3b15520e6e841201f281934df044f
@@ -55,8 +55,8 @@
$editing = $PAGE->user_is_editing();
if ($editing) {
- $strtopichide = get_string('hidetopicfromothers');
- $strtopicshow = get_string('showtopicfromothers');
+ $strtopichide = get_string('hidefromothers', 'format_topics');
+ $strtopicshow = get_string('showfromothers', 'format_topics');
$strmarkthistopic = get_string('markthistopic');
$strmarkedthistopic = get_string('markedthistopic');
$strmoveup = get_string('moveup');
@@ -189,16 +189,16 @@
if ($PAGE->user_is_editing() && has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id))) {
if ($course->marker == $section) { // Show the "light globe" on/off
- echo '<a href="view.php?id='.$course->id.'&amp;marker=0&amp;sesskey='.sesskey().'#section-'.$section.'" title="'.$strmarkedthistopic.'">'.'<img src="'.$OUTPUT->pix_url('i/marked') . '" alt="'.$strmarkedthistopic.'" class="icon"/></a><br />';
+ echo '<a href="view.php?id='.$course->id.'&amp;marker=0&amp;sesskey='.sesskey().'#section-'.$section.'" title="'.$strmarkedthistopic.'" class="editing_highlight">'.'<img src="'.$OUTPUT->pix_url('i/marked') . '" alt="'.$strmarkedthistopic.'" class="icon"/></a><br />';
} else {
- echo '<a href="view.php?id='.$course->id.'&amp;marker='.$section.'&amp;sesskey='.sesskey().'#section-'.$section.'" title="'.$strmarkthistopic.'">'.'<img src="'.$OUTPUT->pix_url('i/marker') . '" alt="'.$strmarkthistopic.'" class="icon"/></a><br />';
+ echo '<a href="view.php?id='.$course->id.'&amp;marker='.$section.'&amp;sesskey='.sesskey().'#section-'.$section.'" title="'.$strmarkthistopic.'" class="editing_highlight">'.'<img src="'.$OUTPUT->pix_url('i/marker') . '" alt="'.$strmarkthistopic.'" class="icon"/></a><br />';
}
if ($thissection->visible) { // Show the hide/show eye
- echo '<a href="view.php?id='.$course->id.'&amp;hide='.$section.'&amp;sesskey='.sesskey().'#section-'.$section.'" title="'.$strtopichide.'">'.
+ echo '<a href="view.php?id='.$course->id.'&amp;hide='.$section.'&amp;sesskey='.sesskey().'#section-'.$section.'" title="'.$strtopichide.'" class="editing_showhide">'.
'<img src="'.$OUTPUT->pix_url('i/hide') . '" class="icon hide" alt="'.$strtopichide.'" /></a><br />';
} else {
- echo '<a href="view.php?id='.$course->id.'&amp;show='.$section.'&amp;sesskey='.sesskey().'#section-'.$section.'" title="'.$strtopicshow.'">'.
+ echo '<a href="view.php?id='.$course->id.'&amp;show='.$section.'&amp;sesskey='.sesskey().'#section-'.$section.'" title="'.$strtopicshow.'" class="editing_showhide">'.
'<img src="'.$OUTPUT->pix_url('i/show') . '" class="icon hide" alt="'.$strtopicshow.'" /></a><br />';
}
if ($section > 1) { // Add a arrow to move section up
@@ -28,3 +28,5 @@
$string['section0name'] = 'General';
$string['page-course-view-topics'] = 'Any course main page in topics format';
$string['page-course-view-topics-x'] = 'Any course page in topics format';
+$string['hidefromothers'] = 'Hide topic';
+$string['showfromothers'] = 'Show topic';
@@ -47,8 +47,8 @@
$editing = $PAGE->user_is_editing();
if ($editing) {
- $strweekhide = get_string('hideweekfromothers');
- $strweekshow = get_string('showweekfromothers');
+ $strweekhide = get_string('hidefromothers', 'format_topics');
+ $strweekshow = get_string('showfromothers', 'format_topics');
$strmoveup = get_string('moveup');
$strmovedown = get_string('movedown');
}
@@ -194,10 +194,10 @@
if ($PAGE->user_is_editing() && has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id))) {
if ($thissection->visible) { // Show the hide/show eye
- echo '<a href="view.php?id='.$course->id.'&amp;hide='.$section.'&amp;sesskey='.sesskey().'#section-'.$section.'" title="'.$strweekhide.'">'.
+ echo '<a href="view.php?id='.$course->id.'&amp;hide='.$section.'&amp;sesskey='.sesskey().'#section-'.$section.'" title="'.$strweekhide.'" class="editing_showhide">'.
'<img src="'.$OUTPUT->pix_url('i/hide') . '" class="icon hide" alt="'.$strweekhide.'" /></a><br />';
} else {
- echo '<a href="view.php?id='.$course->id.'&amp;show='.$section.'&amp;sesskey='.sesskey().'#section-'.$section.'" title="'.$strweekshow.'">'.
+ echo '<a href="view.php?id='.$course->id.'&amp;show='.$section.'&amp;sesskey='.sesskey().'#section-'.$section.'" title="'.$strweekshow.'" class="editing_showhide">'.
'<img src="'.$OUTPUT->pix_url('i/show') . '" class="icon hide" alt="'.$strweekshow.'" /></a><br />';
}
if ($section > 1) { // Add a arrow to move section up
@@ -28,3 +28,5 @@
$string['section0name'] = 'General';
$string['page-course-view-weeks'] = 'Any course main page in weeks format';
$string['page-course-view-weeks-x'] = 'Any course page in weeks format';
+$string['hidefromothers'] = 'Hide week';
+$string['showfromothers'] = 'Show week';
View
@@ -1355,10 +1355,16 @@ function course_set_marker($courseid, $marker) {
/**
* For a given course section, marks it visible or hidden,
* and does the same for every activity in that section
+ *
+ * @param int $courseid course id
+ * @param int $sectionnumber The section number to adjust
+ * @param int $visibility The new visibility
+ * @return array A list of resources which were hidden in the section
*/
function set_section_visible($courseid, $sectionnumber, $visibility) {
global $DB;
+ $resourcestotoggle = array();
if ($section = $DB->get_record("course_sections", array("course"=>$courseid, "section"=>$sectionnumber))) {
$DB->set_field("course_sections", "visible", "$visibility", array("id"=>$section->id));
if (!empty($section->sequence)) {
@@ -1368,7 +1374,19 @@ function set_section_visible($courseid, $sectionnumber, $visibility) {
}
}
rebuild_course_cache($courseid);
+
+ // Determine which modules are visible for AJAX update
+ if (!empty($modules)) {
+ list($insql, $params) = $DB->get_in_or_equal($modules);
+ $select = 'id ' . $insql . ' AND visible = ?';
+ array_push($params, $visibility);
+ if (!$visibility) {
+ $select .= ' AND visibleold = 1';
+ }
+ $resourcestotoggle = $DB->get_fieldset_select('course_modules', 'id', $select, $params);
+ }
}
+ return $resourcestotoggle;
}
/**
@@ -4386,3 +4404,97 @@ function course_page_type_list($pagetype, $parentcontext, $currentcontext) {
);
}
}
+
+/**
+ * Include the relevant javascript and language strings for the resource
+ * toolbox YUI module
+ *
+ * @param integer $id The ID of the course being applied to
+ * @param array $modules An array containing the names of the modules in
+ * use on the page
+ * @param object $config An object containing configuration parameters for ajax modules including:
+ * * resourceurl The URL to post changes to for resource changes
+ * * sectionurl The URL to post changes to for section changes
+ * * pageparams Additional parameters to pass through in the post
+ * @return void
+ */
+function include_course_ajax($course, $modules = array(), $config = null) {
+ global $PAGE, $CFG, $USER;
+
+ // Ensure that ajax should be included
+ $courseformatajaxsupport = course_format_ajax_support($course->format);
+ if (!$CFG->enablecourseajax
+ || !$PAGE->theme->enablecourseajax
+ || !$CFG->enableajax
+ || empty($USER->editing)
+ || !$PAGE->user_is_editing()
+ || ($course->id != SITEID && !$courseformatajaxsupport->capable)) {
+ return;
+ }
+
+ if (!$config) {
+ $config = new stdClass();
+ }
+
+ // The URL to use for resource changes
+ if (!isset($config->resourceurl)) {
+ $config->resourceurl = '/course/rest.php';
+ }
+
+ // The URL to use for section changes
+ if (!isset($config->sectionurl)) {
+ $config->sectionurl = '/course/rest.php';
+ }
+
+ // Any additional parameters which need to be included on page submission
+ if (!isset($config->pageparams)) {
+ $config->pageparams = array();
+ }
+
+ // Include toolboxes
+ $PAGE->requires->yui_module('moodle-course-toolboxes',
+ 'M.course.init_resource_toolbox',
+ array(array(
+ 'courseid' => $course->id,
+ 'ajaxurl' => $config->resourceurl,
+ 'config' => $config,
+ ))
+ );
+ $PAGE->requires->yui_module('moodle-course-toolboxes',
+ 'M.course.init_section_toolbox',
+ array(array(
+ 'courseid' => $course->id,
+ 'format' => $course->format,
+ 'ajaxurl' => $config->sectionurl,
+ 'config' => $config,
+ ))
+ );
+
+ // Require various strings for the command toolbox
+ $PAGE->requires->strings_for_js(array(
+ 'moveleft',
+ 'deletechecktype',
+ 'deletechecktypename',
+ 'show',
+ 'hide',
+ 'groupsnone',
+ 'groupsvisible',
+ 'groupsseparate',
+ 'clicktochangeinbrackets',
+ 'markthistopic',
+ 'markedthistopic',
+ ), 'moodle');
+
+ // Include format-specific strings
+ if ($course->id != SITEID) {
+ $PAGE->requires->strings_for_js(array(
+ 'showfromothers',
+ 'hidefromothers',
+ ), 'format_' . $course->format);
+ }
+
+ // For confirming resource deletion we need the name of the module in question
+ foreach ($modules as $module => $modname) {
+ $PAGE->requires->string_for_js('pluginname', $module);
+ }
+}
View
@@ -87,7 +87,8 @@
switch ($field) {
case 'visible':
- set_section_visible($course->id, $id, $value);
+ $resourcestotoggle = set_section_visible($course->id, $id, $value);
+ echo json_encode(array('resourcestotoggle' => $resourcestotoggle));
break;
case 'move':
@@ -115,20 +116,14 @@
set_coursemodule_groupmode($cm->id, $value);
break;
- case 'indentleft':
+ case 'indent':
require_capability('moodle/course:manageactivities', $modcontext);
- if ($cm->indent > 0) {
- $cm->indent--;
+ $cm->indent = $value;
+ if ($cm->indent >= 0) {
$DB->update_record('course_modules', $cm);
}
break;
- case 'indentright':
- require_capability('moodle/course:manageactivities', $modcontext);
- $cm->indent++;
- $DB->update_record('course_modules', $cm);
- break;
-
case 'move':
require_capability('moodle/course:manageactivities', $modcontext);
if (!$section = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>$sectionid))) {
View
@@ -162,32 +162,6 @@
redirect($CFG->wwwroot .'/');
}
- // AJAX-capable course format?
- $useajax = false;
- $formatajax = course_format_ajax_support($course->format);
-
- if (!empty($CFG->enablecourseajax)
- and $formatajax->capable
- and !empty($USER->editing)
- and ajaxenabled($formatajax->testedbrowsers)
- and $PAGE->theme->enablecourseajax
- and has_capability('moodle/course:manageactivities', $context)) {
- $PAGE->requires->yui2_lib('dragdrop');
- $PAGE->requires->yui2_lib('connection');
- $PAGE->requires->yui2_lib('selector');
- $PAGE->requires->js('/lib/ajax/block_classes.js', true);
- $PAGE->requires->js('/lib/ajax/section_classes.js', true);
-
- // Okay, global variable alert. VERY UGLY. We need to create
- // this object here before the <blockname>_print_block()
- // function is called, since that function needs to set some
- // stuff in the javascriptportal object.
- $COURSE->javascriptportal = new jsportal();
- $useajax = true;
- }
-
- $CFG->blocksdrag = $useajax; // this will add a new class to the header so we can style differently
-
$completion = new completion_info($course);
if ($completion->is_enabled() && ajaxenabled()) {
$PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
@@ -257,16 +231,7 @@
echo html_writer::end_tag('div');
- // Use AJAX?
- if ($useajax && has_capability('moodle/course:manageactivities', $context)) {
- // At the bottom because we want to process sections and activities
- // after the relevant html has been generated. We're forced to do this
- // because of the way in which lib/ajax/ajaxcourse.js is written.
- echo html_writer::script(false, new moodle_url('/lib/ajax/ajaxcourse.js'));
- $COURSE->javascriptportal->print_javascript($course->id);
- }
-
+ // Include the command toolbox YUI module
+ include_course_ajax($course, $modnamesused);
echo $OUTPUT->footer();
-
-
Oops, something went wrong.

0 comments on commit ebaa29d

Please sign in to comment.