MDL-60515 groups: Update to weblib group function.
authorAdrian Greeve <adrian@moodle.com>
Mon, 30 Oct 2017 03:00:36 +0000 (11:00 +0800)
committerAdrian Greeve <adrian@moodle.com>
Mon, 30 Oct 2017 08:43:19 +0000 (16:43 +0800)
Added a new function to get the url for the group picture.

group/classes/output/group_details.php
group/templates/group_details.mustache
lib/weblib.php

index 30c67b7..11d4a62 100644 (file)
@@ -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)) {
index 05ac405..d05e1d2 100644 (file)
 
     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}}
 <div class="groupinfobox container-fluid p-y-1">
     <div class="row-fluid">
-        {{#picture}}
-        <div class="group-image col-sm-1 span1">{{{picture}}}</div>
-        {{/picture}}
-        <div {{#picture}}class="col-sm-11 span11"{{/picture}}{{^picture}}class="col"{{/picture}}>
+        {{#pictureurl}}
+        <div class="group-image col-sm-1 span1">{{{pictureurl}}}</div>
+        {{/pictureurl}}
+        <div {{#pictureurl}}class="col-sm-11 span11"{{/pictureurl}}{{^pictureurl}}class="col"{{/pictureurl}}>
             <div class="container-fluid">
                 <div class="row-fluid">
                     <div class="col">
index e8e2e95..4716080 100644 (file)
@@ -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 = '<a href="'. $CFG->wwwroot .'/user/index.php?id='. $courseid .'&amp;group='. $group->id .'">';
-    } 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 .= '<img class="grouppicture" src="'.$grouppictureurl.'"'.
-        ' alt="'.s(get_string('group').' '.$group->name).'" title="'.s($group->name).'"/>';
-
-    if ($link or has_capability('moodle/site:accessallgroups', $context)) {
-        $output .= '</a>';
-    }
-
-    if ($return) {
-        return $output;
-    } else {
-        echo $output;
-    }
+    return $grouppictureurl;
 }