From c550fd0e1180130f57b5bbecb634cb129aa5ee5a Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Mon, 30 Oct 2017 11:00:36 +0800 Subject: [PATCH] MDL-60515 groups: Update to weblib group function. Added a new function to get the url for the group picture. --- group/classes/output/group_details.php | 2 +- group/templates/group_details.mustache | 14 +++--- lib/weblib.php | 60 ++++++++++++++++++-------- 3 files changed, 49 insertions(+), 27 deletions(-) diff --git a/group/classes/output/group_details.php b/group/classes/output/group_details.php index 30c67b7cb77..11d4a62e078 100644 --- a/group/classes/output/group_details.php +++ b/group/classes/output/group_details.php @@ -76,8 +76,8 @@ class group_details implements renderable, templatable { ]; $data = new stdClass(); - $data->picture = print_group_picture($this->group, $this->group->courseid, true, true, false); $data->name = format_string($this->group->name, true, ['context' => $context]); + $data->pictureurl = get_group_picture_url($this->group, $this->group->courseid, true); $data->description = format_text($description, $descriptionformat, $options); if (has_capability('moodle/course:managegroups', $context)) { diff --git a/group/templates/group_details.mustache b/group/templates/group_details.mustache index 05ac40595fc..d05e1d23775 100644 --- a/group/templates/group_details.mustache +++ b/group/templates/group_details.mustache @@ -27,25 +27,25 @@ Context variables required for this template: * name string Group Name - * picture string Group image HTML + * pictureurl string Group image url * description string Group description * edit string edit link to edit the group Example context (json): { "name": "Group Name", - "picture": "", + "pictureurl": "https://raw.githubusercontent.com/moodle/moodle/master/pix/g/f1.png", "description": "This is the description for Group Name", - "editurl": "" + "editurl": "http://www.moodle.org" } }} {{#name}}
- {{#picture}} -
{{{picture}}}
- {{/picture}} -
+ {{#pictureurl}} +
{{{pictureurl}}}
+ {{/pictureurl}} +
diff --git a/lib/weblib.php b/lib/weblib.php index e8e2e95de39..471608023db 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -2423,23 +2423,56 @@ function print_group_picture($group, $courseid, $large=false, $return=false, $li } } + $pictureurl = get_group_picture_url($group, $courseid, $large); + + // If there is no picture, do nothing. + if (!isset($pictureurl)) { + return; + } + + $context = context_course::instance($courseid); + + $groupname = s($group->name); + $pictureimage = html_writer::img($pictureurl, $groupname, ['title' => $groupname]); + + $output = ''; + if ($link or has_capability('moodle/site:accessallgroups', $context)) { + $linkurl = new moodle_url('/user/index.php', ['id' => $courseid, 'group' => $group->id]); + $output .= html_writer::link($linkurl, $pictureimage); + } else { + $output .= $pictureimage; + } + + if ($return) { + return $output; + } else { + echo $output; + } +} + +/** + * Return the url to the group picture. + * + * @param stdClass $group A group object. + * @param int $courseid The course ID for the group. + * @param bool $large A large or small group picture? Default is small. + * @return moodle_url Returns the url for the group picture. + */ +function get_group_picture_url($group, $courseid, $large = false) { + global $CFG; + $context = context_course::instance($courseid); // If there is no picture, do nothing. if (!$group->picture) { - return ''; + return; } // If picture is hidden, only show to those with course:managegroups. if ($group->hidepicture and !has_capability('moodle/course:managegroups', $context)) { - return ''; + return; } - if ($link or has_capability('moodle/site:accessallgroups', $context)) { - $output = ''; - } else { - $output = ''; - } if ($large) { $file = 'f1'; } else { @@ -2448,18 +2481,7 @@ function print_group_picture($group, $courseid, $large=false, $return=false, $li $grouppictureurl = moodle_url::make_pluginfile_url($context->id, 'group', 'icon', $group->id, '/', $file); $grouppictureurl->param('rev', $group->picture); - $output .= ''.s(get_string('group').' '.$group->name).''; - - if ($link or has_capability('moodle/site:accessallgroups', $context)) { - $output .= ''; - } - - if ($return) { - return $output; - } else { - echo $output; - } + return $grouppictureurl; } -- 2.43.0