. /** * Renderer for use with the course section and all the goodness that falls * within it. * * This renderer should contain methods useful to courses, and categories. * * @package moodlecore * @copyright 2010 Sam Hemelryk * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ /** * The core course renderer * * Can be retrieved with the following: * $renderer = $PAGE->get_renderer('core','course'); */ class core_course_renderer extends plugin_renderer_base { /** * A cache of strings * @var stdClass */ protected $strings; /** * Override the constructor so that we can initialise the string cache * * @param moodle_page $page * @param string $target */ public function __construct(moodle_page $page, $target) { $this->strings = new stdClass; parent::__construct($page, $target); $this->add_modchoosertoggle(); } /** * Adds the item in course settings navigation to toggle modchooser * * Theme can overwrite as an empty function to exclude it (for example if theme does not * use modchooser at all) */ protected function add_modchoosertoggle() { global $CFG; static $modchoosertoggleadded = false; // Add the module chooser toggle to the course page if ($modchoosertoggleadded || $this->page->state > moodle_page::STATE_PRINTING_HEADER || $this->page->course->id == SITEID || !$this->page->user_is_editing() || !($context = context_course::instance($this->page->course->id)) || !has_capability('moodle/course:update', $context) || !course_ajax_enabled($this->page->course) || !($coursenode = $this->page->settingsnav->find('courseadmin', navigation_node::TYPE_COURSE)) || !$coursenode->get('editsettings')) { // too late or we are on site page or we could not find the course settings node // or we are not allowed to edit return; } $modchoosertoggleadded = true; if ($this->page->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) { // We are on the course page, retain the current page params e.g. section. $modchoosertoggleurl = clone($this->page->url); } else { // Edit on the main course page. $modchoosertoggleurl = new moodle_url('/course/view.php', array('id' => $this->page->course->id, 'return' => $this->page->url->out_as_local_url(false))); } $modchoosertoggleurl->param('sesskey', sesskey()); if ($usemodchooser = get_user_preferences('usemodchooser', $CFG->modchooserdefault)) { $modchoosertogglestring = get_string('modchooserdisable', 'moodle'); $modchoosertoggleurl->param('modchooser', 'off'); } else { $modchoosertogglestring = get_string('modchooserenable', 'moodle'); $modchoosertoggleurl->param('modchooser', 'on'); } $modchoosertoggle = navigation_node::create($modchoosertogglestring, $modchoosertoggleurl, navigation_node::TYPE_SETTING); $coursenode->add_node($modchoosertoggle, 'editsettings'); $modchoosertoggle->add_class('modchoosertoggle'); $modchoosertoggle->add_class('visibleifjs'); user_preference_allow_ajax_update('usemodchooser', PARAM_BOOL); } /** * Renders course info box. * * @param stdClass $course * @return string */ public function course_info_box(stdClass $course) { global $CFG; $context = context_course::instance($course->id); $content = ''; $content .= $this->output->box_start('generalbox info'); $summary = file_rewrite_pluginfile_urls($course->summary, 'pluginfile.php', $context->id, 'course', 'summary', null); $content .= format_text($summary, $course->summaryformat, array('overflowdiv'=>true), $course->id); if (!empty($CFG->coursecontact)) { $coursecontactroles = explode(',', $CFG->coursecontact); foreach ($coursecontactroles as $roleid) { if ($users = get_role_users($roleid, $context, true, '', null, false)) { foreach ($users as $teacher) { $role = new stdClass(); $role->id = $teacher->roleid; $role->name = $teacher->rolename; $role->shortname = $teacher->roleshortname; $role->coursealias = $teacher->rolecoursealias; $fullname = fullname($teacher, has_capability('moodle/site:viewfullnames', $context)); $namesarray[] = role_get_name($role, $context).': '.$fullname.''; } } } if (!empty($namesarray)) { $content .= ""; } } $content .= $this->output->box_end(); return $content; } /** * Renderers a structured array of courses and categories into a nice * XHTML tree structure. * * This method was designed initially to display the front page course/category * combo view. The structure can be retrieved by get_course_category_tree() * * @param array $structure * @return string */ public function course_category_tree(array $structure) { $this->strings->summary = get_string('summary'); // Generate an id and the required JS call to make this a nice widget $id = html_writer::random_id('course_category_tree'); $this->page->requires->js_init_call('M.util.init_toggle_class_on_click', array($id, '.category.with_children .category_label', 'collapsed', '.category.with_children')); // Start content generation $content = html_writer::start_tag('div', array('class'=>'course_category_tree', 'id'=>$id)); foreach ($structure as $category) { $content .= $this->course_category_tree_category($category); } $content .= html_writer::start_tag('div', array('class'=>'controls')); $content .= html_writer::tag('div', get_string('collapseall'), array('class'=>'addtoall expandall')); $content .= html_writer::tag('div', get_string('expandall'), array('class'=>'removefromall collapseall')); $content .= html_writer::end_tag('div'); $content .= html_writer::end_tag('div'); // Return the course category tree HTML return $content; } /** * Renderers a category for use with course_category_tree * * @param array $category * @param int $depth * @return string */ protected function course_category_tree_category(stdClass $category, $depth=1) { $content = ''; $hassubcategories = (isset($category->categories) && count($category->categories)>0); $hascourses = (isset($category->courses) && count($category->courses)>0); $classes = array('category'); if ($category->parent != 0) { $classes[] = 'subcategory'; } if (empty($category->visible)) { $classes[] = 'dimmed_category'; } if ($hassubcategories || $hascourses) { $classes[] = 'with_children'; if ($depth > 1) { $classes[] = 'collapsed'; } } $categoryname = format_string($category->name, true, array('context' => context_coursecat::instance($category->id))); $content .= html_writer::start_tag('div', array('class'=>join(' ', $classes))); $content .= html_writer::start_tag('div', array('class'=>'category_label')); $content .= html_writer::link(new moodle_url('/course/category.php', array('id'=>$category->id)), $categoryname, array('class'=>'category_link')); $content .= html_writer::end_tag('div'); if ($hassubcategories) { $content .= html_writer::start_tag('div', array('class'=>'subcategories')); foreach ($category->categories as $subcategory) { $content .= $this->course_category_tree_category($subcategory, $depth+1); } $content .= html_writer::end_tag('div'); } if ($hascourses) { $content .= html_writer::start_tag('div', array('class'=>'courses')); $coursecount = 0; $strinfo = new lang_string('info'); foreach ($category->courses as $course) { $classes = array('course'); $linkclass = 'course_link'; if (!$course->visible) { $linkclass .= ' dimmed'; } $coursecount ++; $classes[] = ($coursecount%2)?'odd':'even'; $content .= html_writer::start_tag('div', array('class'=>join(' ', $classes))); $content .= html_writer::link(new moodle_url('/course/view.php', array('id'=>$course->id)), format_string($course->fullname), array('class'=>$linkclass)); $content .= html_writer::start_tag('div', array('class'=>'course_info clearfix')); // print enrol info if ($icons = enrol_get_course_info_icons($course)) { foreach ($icons as $pix_icon) { $content .= $this->render($pix_icon); } } if ($course->summary) { $url = new moodle_url('/course/info.php', array('id' => $course->id)); $image = html_writer::empty_tag('img', array('src'=>$this->output->pix_url('i/info'), 'alt'=>$this->strings->summary)); $content .= $this->action_link($url, $image, new popup_action('click', $url, 'courseinfo'), array('title' => $this->strings->summary)); } $content .= html_writer::end_tag('div'); $content .= html_writer::end_tag('div'); } $content .= html_writer::end_tag('div'); } $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) { static $isdisplayed = false; if ($isdisplayed) { return ''; } $isdisplayed = true; // Add the module chooser $this->page->requires->yui_module('moodle-course-modchooser', 'M.course.init_chooser', array(array('courseid' => $course->id, 'closeButtonTitle' => get_string('close', 'editor'))) ); $this->page->requires->strings_for_js(array( 'addresourceoractivity', 'modchooserenable', 'modchooserdisable', ), 'moodle'); // Add the header $header = html_writer::tag('div', get_string('addresourceoractivity', 'moodle'), array('class' => 'hd choosertitle')); $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', 'class' => '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')); // Activities $activities = array_filter($modules, function($mod) { return ($mod->archetype !== MOD_ARCHETYPE_RESOURCE && $mod->archetype !== MOD_ARCHETYPE_SYSTEM); }); if (count($activities)) { $formcontent .= $this->course_modchooser_title('activities'); $formcontent .= $this->course_modchooser_module_types($activities); } // Resources $resources = array_filter($modules, function($mod) { return ($mod->archetype === MOD_ARCHETYPE_RESOURCE); }); if (count($resources)) { $formcontent .= $this->course_modchooser_title('resources'); $formcontent .= $this->course_modchooser_module_types($resources); } $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', 'class' => 'submitbutton', 'value' => get_string('add'))); $formcontent .= html_writer::tag('input', '', array('type' => 'submit', 'name' => 'addcancel', 'class' => '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('class' => 'choosercontainer')); return $header . html_writer::tag('div', $content, array('class' => 'chooserdialoguebody')); } /** * 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')); } /** * Renders HTML for displaying the sequence of course module editing buttons * * @see course_get_cm_edit_actions() * * @param array $actions array of action_link or pix_icon objects * @return string */ public function course_section_cm_edit_actions($actions) { $output = html_writer::start_tag('span', array('class' => 'commands')); foreach ($actions as $action) { if ($action instanceof renderable) { $output .= $this->output->render($action); } else { $output .= $action; } } $output .= html_writer::end_tag('span'); return $output; } /** * Renders HTML for the menus to add activities and resources to the current course * * Note, if theme overwrites this function and it does not use modchooser, * see also {@link core_course_renderer::add_modchoosertoggle()} * * @param stdClass $course * @param int $section relative section number (field course_sections.section) * @param int $sectionreturn The section to link back to * @param array $displayoptions additional display options, for example blocks add * option 'inblock' => true, suggesting to display controls vertically * @return string */ function course_section_add_cm_control($course, $section, $sectionreturn = null, $displayoptions = array()) { global $CFG; $vertical = !empty($displayoptions['inblock']); // check to see if user can add menus and there are modules to add if (!has_capability('moodle/course:manageactivities', context_course::instance($course->id)) || !$this->page->user_is_editing() || !($modnames = get_module_types_names()) || empty($modnames)) { return ''; } // Retrieve all modules with associated metadata $modules = get_module_metadata($course, $modnames, $sectionreturn); $urlparams = array('section' => $section); // We'll sort resources and activities into two lists $activities = array(MOD_CLASS_ACTIVITY => array(), MOD_CLASS_RESOURCE => array()); foreach ($modules as $module) { if (!array_key_exists($module->archetype, $activities)) { // System modules cannot be added by user, do not add to dropdown } else if (isset($module->types)) { // This module has a subtype // NOTE: this is legacy stuff, module subtypes are very strongly discouraged!! $subtypes = array(); foreach ($module->types as $subtype) { $link = $subtype->link->out(true, $urlparams); $subtypes[$link] = $subtype->title; } // Sort module subtypes into the list if (!empty($module->title)) { // This grouping has a name $activities[$module->archetype][] = array($module->title => $subtypes); } else { // This grouping does not have a name $activities[$module->archetype] = array_merge($activities[$module->archetype], $subtypes); } } else { // This module has no subtypes $link = $module->link->out(true, $urlparams); $activities[$module->archetype][$link] = $module->title; } } $straddactivity = get_string('addactivity'); $straddresource = get_string('addresource'); $sectionname = get_section_name($course, $section); $strresourcelabel = get_string('addresourcetosection', null, $sectionname); $stractivitylabel = get_string('addactivitytosection', null, $sectionname); $output = html_writer::start_tag('div', array('class' => 'section_add_menus', 'id' => 'add_menus-section-' . $section)); if (!$vertical) { $output .= html_writer::start_tag('div', array('class' => 'horizontal')); } if (!empty($activities[MOD_CLASS_RESOURCE])) { $select = new url_select($activities[MOD_CLASS_RESOURCE], '', array(''=>$straddresource), "ressection$section"); $select->set_help_icon('resources'); $select->set_label($strresourcelabel, array('class' => 'accesshide')); $output .= $this->output->render($select); } if (!empty($activities[MOD_CLASS_ACTIVITY])) { $select = new url_select($activities[MOD_CLASS_ACTIVITY], '', array(''=>$straddactivity), "section$section"); $select->set_help_icon('activities'); $select->set_label($stractivitylabel, array('class' => 'accesshide')); $output .= $this->output->render($select); } if (!$vertical) { $output .= html_writer::end_tag('div'); } $output .= html_writer::end_tag('div'); if (course_ajax_enabled($course) && $course->id == $this->page->course->id) { // modchooser can be added only for the current course set on the page! $straddeither = get_string('addresourceoractivity'); // The module chooser link $modchooser = html_writer::start_tag('div', array('class' => 'mdl-right')); $modchooser.= html_writer::start_tag('div', array('class' => 'section-modchooser')); $icon = $this->output->pix_icon('t/add', ''); $span = html_writer::tag('span', $straddeither, array('class' => 'section-modchooser-text')); $modchooser .= html_writer::tag('span', $icon . $span, array('class' => 'section-modchooser-link')); $modchooser.= html_writer::end_tag('div'); $modchooser.= html_writer::end_tag('div'); // Wrap the normal output in a noscript div $usemodchooser = get_user_preferences('usemodchooser', $CFG->modchooserdefault); if ($usemodchooser) { $output = html_writer::tag('div', $output, array('class' => 'hiddenifjs addresourcedropdown')); $modchooser = html_writer::tag('div', $modchooser, array('class' => 'visibleifjs addresourcemodchooser')); } else { // If the module chooser is disabled, we need to ensure that the dropdowns are shown even if javascript is disabled $output = html_writer::tag('div', $output, array('class' => 'show addresourcedropdown')); $modchooser = html_writer::tag('div', $modchooser, array('class' => 'hide addresourcemodchooser')); } $output = $this->course_modchooser($modules, $course) . $modchooser . $output; } return $output; } /** * Renders html for completion box on course page * * If completion is disabled, returns empty string * If completion is automatic, returns an icon of the current completion state * If completion is manual, returns a form (with an icon inside) that allows user to * toggle completion * * @param stdClass $course course object * @param completion_info $completioninfo completion info for the course, it is recommended * to fetch once for all modules in course/section for performance * @param cm_info $mod module to show completion for * @param array $displayoptions display options, not used in core * @return string */ public function course_section_cm_completion($course, &$completioninfo, cm_info $mod, $displayoptions = array()) { global $CFG; $output = ''; if (!empty($displayoptions['hidecompletion']) || !isloggedin() || isguestuser() || !$mod->uservisible) { return $output; } if ($completioninfo === null) { $completioninfo = new completion_info($course); } $completion = $completioninfo->is_enabled($mod); if ($completion == COMPLETION_TRACKING_NONE) { return $output; } $completiondata = $completioninfo->get_data($mod, true); $completionicon = ''; if ($this->page->user_is_editing()) { switch ($completion) { case COMPLETION_TRACKING_MANUAL : $completionicon = 'manual-enabled'; break; case COMPLETION_TRACKING_AUTOMATIC : $completionicon = 'auto-enabled'; break; } } else if ($completion == COMPLETION_TRACKING_MANUAL) { switch($completiondata->completionstate) { case COMPLETION_INCOMPLETE: $completionicon = 'manual-n'; break; case COMPLETION_COMPLETE: $completionicon = 'manual-y'; break; } } else { // Automatic switch($completiondata->completionstate) { case COMPLETION_INCOMPLETE: $completionicon = 'auto-n'; break; case COMPLETION_COMPLETE: $completionicon = 'auto-y'; break; case COMPLETION_COMPLETE_PASS: $completionicon = 'auto-pass'; break; case COMPLETION_COMPLETE_FAIL: $completionicon = 'auto-fail'; break; } } if ($completionicon) { $formattedname = $mod->get_formatted_name(); $imgalt = get_string('completion-alt-' . $completionicon, 'completion', $formattedname); if ($completion == COMPLETION_TRACKING_MANUAL && !$this->page->user_is_editing()) { $imgtitle = get_string('completion-title-' . $completionicon, 'completion', $formattedname); $newstate = $completiondata->completionstate == COMPLETION_COMPLETE ? COMPLETION_INCOMPLETE : COMPLETION_COMPLETE; // In manual mode the icon is a toggle form... // If this completion state is used by the // conditional activities system, we need to turn // off the JS. $extraclass = ''; if (!empty($CFG->enableavailability) && condition_info::completion_value_used_as_condition($course, $mod)) { $extraclass = ' preventjs'; } $output .= html_writer::start_tag('form', array('method' => 'post', 'action' => new moodle_url('/course/togglecompletion.php'), 'class' => 'togglecompletion'. $extraclass)); $output .= html_writer::start_tag('div'); $output .= html_writer::empty_tag('input', array( 'type' => 'hidden', 'name' => 'id', 'value' => $mod->id)); $output .= html_writer::empty_tag('input', array( 'type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey())); $output .= html_writer::empty_tag('input', array( 'type' => 'hidden', 'name' => 'modulename', 'value' => $mod->name)); $output .= html_writer::empty_tag('input', array( 'type' => 'hidden', 'name' => 'completionstate', 'value' => $newstate)); $output .= html_writer::empty_tag('input', array( 'type' => 'image', 'src' => $this->output->pix_url('i/completion-'.$completionicon), 'alt' => $imgalt, 'title' => $imgtitle)); $output .= html_writer::end_tag('div'); $output .= html_writer::end_tag('form'); } else { // In auto mode, or when editing, the icon is just an image $completionpixicon = new pix_icon('i/completion-'.$completionicon, $imgalt, '', array('title' => $imgalt)); $output .= html_writer::tag('span', $this->output->render($completionpixicon), array('class' => 'autocompletion')); } } return $output; } /** * Checks if course module has any conditions that may make it unavailable for * all or some of the students * * This function is internal and is only used to create CSS classes for the module name/text * * @param cm_info $mod * @return bool */ protected function is_cm_conditionally_hidden(cm_info $mod) { global $CFG; $conditionalhidden = false; if (!empty($CFG->enableavailability)) { $conditionalhidden = $mod->availablefrom > time() || ($mod->availableuntil && $mod->availableuntil < time()) || count($mod->conditionsgrade) > 0 || count($mod->conditionscompletion) > 0; } return $conditionalhidden; } /** * Renders html to display a name with the link to the course module on a course page * * If module is unavailable for user but still needs to be displayed * in the list, just the name is returned without a link * * Note, that for course modules that never have separate pages (i.e. labels) * this function return an empty string * * @param cm_info $mod * @param array $displayoptions * @return string */ public function course_section_cm_name(cm_info $mod, $displayoptions = array()) { global $CFG; $output = ''; if (!$mod->uservisible && (empty($mod->showavailability) || empty($mod->availableinfo))) { // nothing to be displayed to the user return $output; } $url = $mod->get_url(); if (!$url) { return $output; } //Accessibility: for files get description via icon, this is very ugly hack! $instancename = $mod->get_formatted_name(); $altname = ''; $altname = $mod->modfullname; // Avoid unnecessary duplication: if e.g. a forum name already // includes the word forum (or Forum, etc) then it is unhelpful // to include that in the accessible description that is added. if (false !== strpos(textlib::strtolower($instancename), textlib::strtolower($altname))) { $altname = ''; } // File type after name, for alphabetic lists (screen reader). if ($altname) { $altname = get_accesshide(' '.$altname); } $conditionalhidden = $this->is_cm_conditionally_hidden($mod); $accessiblebutdim = !$mod->visible || $conditionalhidden; $linkclasses = ''; $accesstext = ''; $textclasses = ''; if ($accessiblebutdim) { $linkclasses .= ' dimmed'; $textclasses .= ' dimmed_text'; if ($conditionalhidden) { $linkclasses .= ' conditionalhidden'; $textclasses .= ' conditionalhidden'; } if ($mod->uservisible) { // show accessibility note only if user can access the module himself $accesstext = get_accesshide(get_string('hiddenfromstudents').': '); } } // Get on-click attribute value if specified and decode the onclick - it // has already been encoded for display (puke). $onclick = htmlspecialchars_decode($mod->get_on_click(), ENT_QUOTES); $groupinglabel = ''; if (!empty($mod->groupingid) && has_capability('moodle/course:managegroups', context_course::instance($mod->course))) { $groupings = groups_get_all_groupings($mod->course); $groupinglabel = html_writer::tag('span', '('.format_string($groupings[$mod->groupingid]->name).')', array('class' => 'groupinglabel '.$textclasses)); } // Display link itself. $activitylink = html_writer::empty_tag('img', array('src' => $mod->get_icon_url(), 'class' => 'iconlarge activityicon', 'alt' => $mod->modfullname)) . $accesstext . html_writer::tag('span', $instancename . $altname, array('class' => 'instancename')); if ($mod->uservisible) { $output .= html_writer::link($url, $activitylink, array('class' => $linkclasses, 'onclick' => $onclick)) . $groupinglabel; } else { // We may be displaying this just in order to show information // about visibility, without the actual link ($mod->uservisible) $output .= html_writer::tag('div', $activitylink, array('class' => $textclasses)) . $groupinglabel; } return $output; } /** * Renders html to display the module content on the course page (i.e. text of the labels) * * @param cm_info $mod * @param array $displayoptions * @return string */ public function course_section_cm_text(cm_info $mod, $displayoptions = array()) { $output = ''; if (!$mod->uservisible && (empty($mod->showavailability) || empty($mod->availableinfo))) { // nothing to be displayed to the user return $output; } $content = $mod->get_formatted_content(array('overflowdiv' => true, 'noclean' => true)); $conditionalhidden = $this->is_cm_conditionally_hidden($mod); $accessiblebutdim = !$mod->visible || $conditionalhidden; $textclasses = ''; $accesstext = ''; if ($accessiblebutdim) { $textclasses .= ' dimmed_text'; if ($conditionalhidden) { $textclasses .= ' conditionalhidden'; } if ($mod->uservisible) { // show accessibility note only if user can access the module himself $accesstext = get_accesshide(get_string('hiddenfromstudents').': '); } } if ($mod->get_url()) { if ($content) { // If specified, display extra content after link. $output = html_writer::tag('div', $content, array('class' => trim('contentafterlink ' . $textclasses))); } } else { // No link, so display only content. $output = html_writer::tag('div', $accesstext . $content, array('class' => $textclasses)); } return $output; } /** * Renders HTML to show course module availability information (for someone who isn't allowed * to see the activity itself, or for staff) * * @param cm_info $mod * @param array $displayoptions * @return string */ public function course_section_cm_availability(cm_info $mod, $displayoptions = array()) { global $CFG; if (!$mod->uservisible) { // this is a student who is not allowed to see the module but might be allowed // to see availability info (i.e. "Available from ...") if (!empty($mod->showavailability) && !empty($mod->availableinfo)) { $output = html_writer::tag('div', $mod->availableinfo, array('class' => 'availabilityinfo')); } return $output; } // this is a teacher who is allowed to see module but still should see the // information that module is not available to all/some students $modcontext = context_module::instance($mod->id); $canviewhidden = has_capability('moodle/course:viewhiddenactivities', $modcontext); if ($canviewhidden && !empty($CFG->enableavailability)) { // Don't add availability information if user is not editing and activity is hidden. if ($mod->visible || $this->page->user_is_editing()) { $hidinfoclass = ''; if (!$mod->visible) { $hidinfoclass = 'hide'; } $ci = new condition_info($mod); $fullinfo = $ci->get_full_information(); if($fullinfo) { return '
'.get_string($mod->showavailability ? 'userrestriction_visible' : 'userrestriction_hidden','condition', $fullinfo).'
'; } } } return ''; } /** * Renders HTML to display one course module in a course section * * This includes link, content, availability, completion info and additional information * that module type wants to display (i.e. number of unread forum posts) * * This function calls: * {@link core_course_renderer::course_section_cm_name()} * {@link cm_info::get_after_link()} * {@link core_course_renderer::course_section_cm_text()} * {@link core_course_renderer::course_section_cm_availability()} * {@link core_course_renderer::course_section_cm_completion()} * {@link course_get_cm_edit_actions()} * {@link core_course_renderer::course_section_cm_edit_actions()} * * @param stdClass $course * @param completion_info $completioninfo * @param cm_info $mod * @param int|null $sectionreturn * @param array $displayoptions * @return string */ public function course_section_cm($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = array()) { $output = ''; // We return empty string (because course module will not be displayed at all) // if: // 1) The activity is not visible to users // and // 2a) The 'showavailability' option is not set (if that is set, // we need to display the activity so we can show // availability info) // or // 2b) The 'availableinfo' is empty, i.e. the activity was // hidden in a way that leaves no info, such as using the // eye icon. if (!$mod->uservisible && (empty($mod->showavailability) || empty($mod->availableinfo))) { return $output; } $indentclasses = 'mod-indent'; if (!empty($mod->indent)) { $indentclasses .= ' mod-indent-'.$mod->indent; if ($mod->indent > 15) { $indentclasses .= ' mod-indent-huge'; } } $output .= html_writer::start_tag('div', array('class' => $indentclasses)); // Start the div for the activity title, excluding the edit icons. $output .= html_writer::start_tag('div', array('class' => 'activityinstance')); // Display the link to the module (or do nothing if module has no url) $output .= $this->course_section_cm_name($mod, $displayoptions); // Module can put text after the link (e.g. forum unread) $output .= $mod->get_after_link(); // Closing the tag which contains everything but edit icons. Content part of the module should not be part of this. $output .= html_writer::end_tag('div'); // .activityinstance // If there is content but NO link (eg label), then display the // content here (BEFORE any icons). In this case cons must be // displayed after the content so that it makes more sense visually // and for accessibility reasons, e.g. if you have a one-line label // it should work similarly (at least in terms of ordering) to an // activity. $contentpart = $this->course_section_cm_text($mod, $displayoptions); $url = $mod->get_url(); if (empty($url)) { $output .= $contentpart; } if ($this->page->user_is_editing()) { $editactions = course_get_cm_edit_actions($mod, $mod->indent, $sectionreturn); $output .= ' '. $this->course_section_cm_edit_actions($editactions); $output .= $mod->get_after_edit_icons(); } $output .= $this->course_section_cm_completion($course, $completioninfo, $mod, $displayoptions); // If there is content AND a link, then display the content here // (AFTER any icons). Otherwise it was displayed before if (!empty($url)) { $output .= $contentpart; } // show availability info (if module is not available) $output .= $this->course_section_cm_availability($mod, $displayoptions); $output .= html_writer::end_tag('div'); // $indentclasses return $output; } /** * Renders HTML to display a list of course modules in a course section * Also displays "move here" controls in Javascript-disabled mode * * This function calls {@link core_course_renderer::course_section_cm()} * * @param stdClass $course course object * @param int|stdClass|section_info $section relative section number or section object * @param int $sectionreturn section number to return to * @param int $displayoptions * @return void */ public function course_section_cm_list($course, $section, $sectionreturn = null, $displayoptions = array()) { global $USER; $output = ''; $modinfo = get_fast_modinfo($course); if (is_object($section)) { $section = $modinfo->get_section_info($section->section); } else { $section = $modinfo->get_section_info($section); } $completioninfo = new completion_info($course); // check if we are currently in the process of moving a module with JavaScript disabled $ismoving = $this->page->user_is_editing() && ismoving($course->id); if ($ismoving) { $movingpix = new pix_icon('movehere', get_string('movehere'), 'moodle', array('class' => 'movetarget')); $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'")); } // Get the list of modules visible to user (excluding the module being moved if there is one) $moduleshtml = array(); if (!empty($modinfo->sections[$section->section])) { foreach ($modinfo->sections[$section->section] as $modnumber) { $mod = $modinfo->cms[$modnumber]; if ($ismoving and $mod->id == $USER->activitycopy) { // do not display moving mod continue; } if ($modulehtml = $this->course_section_cm($course, $completioninfo, $mod, $sectionreturn, $displayoptions)) { $moduleshtml[$modnumber] = $modulehtml; } } } if (!empty($moduleshtml) || $ismoving) { $output .= html_writer::start_tag('ul', array('class' => 'section img-text')); foreach ($moduleshtml as $modnumber => $modulehtml) { if ($ismoving) { $movingurl = new moodle_url('/course/mod.php', array('moveto' => $modnumber, 'sesskey' => sesskey())); $output .= html_writer::tag('li', html_writer::link($movingurl, $this->output->render($movingpix)), array('class' => 'movehere', 'title' => $strmovefull)); } $mod = $modinfo->cms[$modnumber]; $modclasses = 'activity '. $mod->modname. ' modtype_'.$mod->modname. ' '. $mod->get_extra_classes(); $output .= html_writer::start_tag('li', array('class' => $modclasses, 'id' => 'module-'. $mod->id)); $output .= $modulehtml; $output .= html_writer::end_tag('li'); } if ($ismoving) { $movingurl = new moodle_url('/course/mod.php', array('movetosection' => $section->id, 'sesskey' => sesskey())); $output .= html_writer::tag('li', html_writer::link($movingurl, $this->output->render($movingpix)), array('class' => 'movehere', 'title' => $strmovefull)); } $output .= html_writer::end_tag('ul'); // .section } return $output; } }