Skip to content

Commit

Permalink
MDL-30617 Add a javascript interface for adding modules to a course
Browse files Browse the repository at this point in the history
This interface allows for additional metadata, such as the module's help
and icon, to be displayed.
  • Loading branch information
Andrew Robert Nicols authored and danpoltawski committed May 17, 2012
1 parent 2e6b68f commit 01e0e70
Show file tree
Hide file tree
Showing 10 changed files with 713 additions and 15 deletions.
48 changes: 43 additions & 5 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1827,10 +1827,10 @@ function print_section_add_menus($course, $section, $modnames, $vertical=false,
$straddactivity = get_string('addactivity');
$straddresource = get_string('addresource');

$output = '<div class="section_add_menus">';
$output = html_writer::start_tag('div', array('class' => 'section_add_menus', 'id' => 'add_menus-section-' . $section));

if (!$vertical) {
$output .= '<div class="horizontal">';
$output .= html_writer::start_tag('div', array('class' => 'horizontal'));
}

if (!empty($resources)) {
Expand All @@ -1846,10 +1846,31 @@ function print_section_add_menus($course, $section, $modnames, $vertical=false,
}

if (!$vertical) {
$output .= '</div>';
$output .= html_writer::end_tag('div');
}

$output .= '</div>';
$output .= html_writer::end_tag('div');

if (course_ajax_enabled($course)) {
$straddeither = get_string('addresourceoractivity');
// The module chooser link
$modchooser = '<div class="sectionaddmodule">';
$modchooser .= '<div class="section_add_menus"><a class="sectionmodchooserlink" href="#">';
$modchooser .= '<img alt="'.$straddeither.'" src="'.$OUTPUT->pix_url('t/add').'" class="activityicon">&nbsp;';
$modchooser .= '<span class="instancename">'.$straddeither.'</span>';
$modchooser .= '</a></div></div>';

// Wrap the normal output in a noscript div
$usemodchooser = get_user_preferences('usemodchooser', 1);
if ($usemodchooser) {
$output = html_writer::tag('div', $output, array('class' => 'hiddenifjs addresourcedropdown'));
$modchooser = html_writer::tag('div', $modchooser, array('class' => 'visibleifjs addresourcemodchooser'));
} else {
$output = html_writer::tag('div', $output, array('class' => 'visibleifjs addresourcedropdown'));
$modchooser = html_writer::tag('div', $modchooser, array('class' => 'hiddenifjs addresourcemodchooser'));
}
$output = $modchooser . $output;
}

if ($return) {
return $output;
Expand Down Expand Up @@ -1936,8 +1957,14 @@ function get_module_metadata($course, $modnames) {
$module->name = $modname;
$module->link = $urlbase . $modname;
$module->icon = $OUTPUT->pix_icon('icon', '', $module->name, array('class' => 'icon'));
if (get_string_manager()->string_exists('modulename_help', $modname)) {
$sm = get_string_manager();
if ($sm->string_exists('modulename_help', $modname)) {
$module->help = get_string('modulename_help', $modname);
if ($sm->string_exists('modulename_link', $modname)) { // Link to further info in Moodle docs
$link = get_string('modulename_link', $modname);
$linktext = get_string('morehelp');
$module->help .= html_writer::tag('div', $OUTPUT->doc_link($link, $linktext), array('class' => 'helpdoclink'));
}
}
$module->archetype = plugin_supports('mod', $modname, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER);
$modlist[$course->id][$modname] = $module;
Expand Down Expand Up @@ -4568,6 +4595,17 @@ function include_course_ajax($course, $usedmodules = array(), $enabledmodules =
// Load drag and drop upload AJAX.
dndupload_add_to_course($course, $enabledmodules);

// Add the module chooser
$PAGE->requires->yui_module('moodle-course-modchooser',
'M.course.init_chooser',
array(array('courseid' => $course->id))
);
$PAGE->requires->strings_for_js(array(
'addresourceoractivity',
'modchooserenable',
'modchooserdisable',
), 'moodle');

return true;
}

Expand Down
151 changes: 151 additions & 0 deletions course/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,155 @@ protected function course_category_tree_category(stdClass $category, $depth=1) {
$content .= html_writer::end_tag('div');
return $content;
}

/**
* Build the HTML for the module chooser javascript popup
*
* @param array $modules A set of modules as returned form @see
* get_module_metadata
* @param object $course The course that will be displayed
* @return string The composed HTML for the module
*/
public function course_modchooser($modules, $course) {
global $OUTPUT;

// Add the header
$header = html_writer::tag('div', get_string('choosemodtype', 'moodle'),
array('id' => 'choosertitle', 'class' => 'hd'));

$formcontent = html_writer::start_tag('form', array('action' => new moodle_url('/course/jumpto.php'),
'id' => 'chooserform', 'method' => 'post'));
$formcontent .= html_writer::start_tag('div', array('id' => 'typeformdiv'));
$formcontent .= html_writer::tag('input', '', array('type' => 'hidden', 'id' => 'course',
'name' => 'course', 'value' => $course->id));
$formcontent .= html_writer::tag('input', '',
array('type' => 'hidden', 'id' => 'jump', 'name' => 'jump', 'value' => ''));
$formcontent .= html_writer::tag('input', '', array('type' => 'hidden', 'name' => 'sesskey',
'value' => sesskey()));
$formcontent .= html_writer::end_tag('div');

// Put everything into one tag 'options'
$formcontent .= html_writer::start_tag('div', array('class' => 'options'));
$formcontent .= html_writer::tag('div', get_string('selectmoduletoviewhelp', 'moodle'),
array('class' => 'instruction'));
// Put all options into one tag 'alloptions' to allow us to handle scrolling
$formcontent .= html_writer::start_tag('div', array('class' => 'alloptions'));

// First display Resources
$resources = array_filter($modules,
create_function('$mod', 'return ($mod->archetype === MOD_CLASS_RESOURCE);'));
if (count($resources)) {
$formcontent .= $this->course_modchooser_title('resources');
$formcontent .= $this->course_modchooser_module_types($resources);
}

// Then activities
$activities = array_filter($modules,
create_function('$mod', 'return ($mod->archetype !== MOD_CLASS_RESOURCE);'));
if (count($activities)) {
$formcontent .= $this->course_modchooser_title('activities');
$formcontent .= $this->course_modchooser_module_types($activities);
}

$formcontent .= html_writer::end_tag('div'); // modoptions
$formcontent .= html_writer::end_tag('div'); // types

$formcontent .= html_writer::start_tag('div', array('class' => 'submitbuttons'));
$formcontent .= html_writer::tag('input', '',
array('type' => 'submit', 'name' => 'submitbutton', 'id' => 'submitbutton', 'value' => get_string('next')));
$formcontent .= html_writer::tag('input', '',
array('type' => 'submit', 'name' => 'addcancel', 'id' => 'addcancel', 'value' => get_string('cancel')));
$formcontent .= html_writer::end_tag('div');
$formcontent .= html_writer::end_tag('form');

// Wrap the whole form in a div
$formcontent = html_writer::tag('div', $formcontent, array('id' => 'chooseform'));

// Put all of the content together
$content = $formcontent;

$content = html_writer::tag('div', $content, array('id' => 'choosercontainer'));
return $header . html_writer::tag('div', $content, array('id' => 'chooserdialogue'));
}

/**
* Build the HTML for a specified set of modules
*
* @param array $modules A set of modules as used by the
* course_modchooser_module function
* @return string The composed HTML for the module
*/
protected function course_modchooser_module_types($modules) {
$return = '';
foreach ($modules as $module) {
if (!isset($module->types)) {
$return .= $this->course_modchooser_module($module);
} else {
$return .= $this->course_modchooser_module($module, array('nonoption'));
foreach ($module->types as $type) {
$return .= $this->course_modchooser_module($type, array('option', 'subtype'));
}
}
}
return $return;
}

/**
* Return the HTML for the specified module adding any required classes
*
* @param object $module An object containing the title, and link. An
* icon, and help text may optionally be specified. If the module
* contains subtypes in the types option, then these will also be
* displayed.
* @param array $classes Additional classes to add to the encompassing
* div element
* @return string The composed HTML for the module
*/
protected function course_modchooser_module($module, $classes = array('option')) {
$output = '';
$output .= html_writer::start_tag('div', array('class' => implode(' ', $classes)));
$output .= html_writer::start_tag('label', array('for' => 'module_' . $module->name));
if (!isset($module->types)) {
$output .= html_writer::tag('input', '', array('type' => 'radio',
'name' => 'jumplink', 'id' => 'module_' . $module->name, 'value' => $module->link));
}

$output .= html_writer::start_tag('span', array('class' => 'modicon'));
if (isset($module->icon)) {
// Add an icon if we have one
$output .= $module->icon;
}
$output .= html_writer::end_tag('span');

$output .= html_writer::tag('span', $module->title, array('class' => 'typename'));
if (!isset($module->help)) {
// Add help if found
$module->help = get_string('nohelpforactivityorresource', 'moodle');
}

// Format the help text using markdown with the following options
$options = new stdClass();
$options->trusted = false;
$options->noclean = false;
$options->smiley = false;
$options->filter = false;
$options->para = true;
$options->newlines = false;
$options->overflowdiv = false;
$module->help = format_text($module->help, FORMAT_MARKDOWN, $options);
$output .= html_writer::tag('span', $module->help, array('class' => 'typesummary'));
$output .= html_writer::end_tag('label');
$output .= html_writer::end_tag('div');

return $output;
}

protected function course_modchooser_title($title, $identifier = null) {
$module = new stdClass();
$module->name = $title;
$module->types = array();
$module->title = get_string($title, $identifier);
$module->help = '';
return $this->course_modchooser_module($module, array('moduletypetitle'));
}
}
Empty file added course/style.css
Empty file.
14 changes: 12 additions & 2 deletions course/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
$move = optional_param('move', 0, PARAM_INT);
$marker = optional_param('marker',-1 , PARAM_INT);
$switchrole = optional_param('switchrole',-1, PARAM_INT);
$modchooser = optional_param('modchooser', -1, PARAM_BOOL);

$params = array();
if (!empty($name)) {
Expand Down Expand Up @@ -129,6 +130,11 @@
redirect($PAGE->url);
}
}
if (($modchooser == 1) && confirm_sesskey()) {
set_user_preference('usemodchooser', $modchooser);
} else if (($modchooser == 0) && confirm_sesskey()) {
set_user_preference('usemodchooser', $modchooser);
}

if (has_capability('moodle/course:update', $context)) {
if ($hide && confirm_sesskey()) {
Expand Down Expand Up @@ -241,7 +247,11 @@

echo html_writer::end_tag('div');

// Include the command toolbox YUI module
include_course_ajax($course, $modnamesused, $modnames);
// Include course AJAX
if (include_course_ajax($course, $modnamesused)) {
// Add the module chooser
$renderer = $PAGE->get_renderer('core', 'course');
echo $renderer->course_modchooser(get_module_metadata($course, $modnames), $course);
}

echo $OUTPUT->footer();
Loading

0 comments on commit 01e0e70

Please sign in to comment.