Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/grouplib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00025 defined('MOODLE_INTERNAL') || die();
00026 
00030 define('NOGROUPS', 0);
00031 
00035 define('SEPARATEGROUPS', 1);
00036 
00040 define('VISIBLEGROUPS', 2);
00041 
00042 
00051 function groups_group_exists($groupid) {
00052     global $DB;
00053     return $DB->record_exists('groups', array('id'=>$groupid));
00054 }
00055 
00063 function groups_get_group_name($groupid) {
00064     global $DB;
00065     return $DB->get_field('groups', 'name', array('id'=>$groupid));
00066 }
00067 
00075 function groups_get_grouping_name($groupingid) {
00076     global $DB;
00077     return $DB->get_field('groupings', 'name', array('id'=>$groupingid));
00078 }
00079 
00089 function groups_get_group_by_name($courseid, $name) {
00090     global $DB;
00091     if ($groups = $DB->get_records('groups', array('courseid'=>$courseid, 'name'=>$name))) {
00092         return key($groups);
00093     }
00094     return false;
00095 }
00096 
00106 function groups_get_grouping_by_name($courseid, $name) {
00107     global $DB;
00108     if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid, 'name'=>$name))) {
00109         return key($groupings);
00110     }
00111     return false;
00112 }
00113 
00120 function groups_get_group($groupid, $fields='*', $strictness=IGNORE_MISSING) {
00121     global $DB;
00122     return $DB->get_record('groups', array('id'=>$groupid), $fields, $strictness);
00123 }
00124 
00132 function groups_get_grouping($groupingid, $fields='*', $strictness=IGNORE_MISSING) {
00133     global $DB;
00134     return $DB->get_record('groupings', array('id'=>$groupingid), $fields, $strictness);
00135 }
00136 
00146 function groups_get_all_groups($courseid, $userid=0, $groupingid=0, $fields='g.*') {
00147     global $DB;
00148 
00149     if (empty($userid)) {
00150         $userfrom  = "";
00151         $userwhere = "";
00152         $params = array();
00153 
00154     } else {
00155         list($usql, $params) = $DB->get_in_or_equal($userid);
00156         $userfrom  = ", {groups_members} gm";
00157         $userwhere = "AND g.id = gm.groupid AND gm.userid $usql";
00158     }
00159 
00160     if (!empty($groupingid)) {
00161         $groupingfrom  = ", {groupings_groups} gg";
00162         $groupingwhere = "AND g.id = gg.groupid AND gg.groupingid = ?";
00163         $params[] = $groupingid;
00164     } else {
00165         $groupingfrom  = "";
00166         $groupingwhere = "";
00167     }
00168 
00169     array_unshift($params, $courseid);
00170 
00171     return $DB->get_records_sql("SELECT $fields
00172                                    FROM {groups} g $userfrom $groupingfrom
00173                                   WHERE g.courseid = ? $userwhere $groupingwhere
00174                                ORDER BY name ASC", $params);
00175 }
00176 
00184 function groups_get_user_groups($courseid, $userid=0) {
00185     global $USER, $DB;
00186 
00187     if (empty($userid)) {
00188         $userid = $USER->id;
00189     }
00190 
00191     $sql = "SELECT g.id, gg.groupingid
00192               FROM {groups} g
00193                    JOIN {groups_members} gm   ON gm.groupid = g.id
00194               LEFT JOIN {groupings_groups} gg ON gg.groupid = g.id
00195              WHERE gm.userid = ? AND g.courseid = ?";
00196     $params = array($userid, $courseid);
00197 
00198     $rs = $DB->get_recordset_sql($sql, $params);
00199 
00200     if (!$rs->valid()) {
00201         $rs->close(); // Not going to iterate (but exit), close rs
00202         return array('0' => array());
00203     }
00204 
00205     $result    = array();
00206     $allgroups = array();
00207 
00208     foreach ($rs as $group) {
00209         $allgroups[$group->id] = $group->id;
00210         if (is_null($group->groupingid)) {
00211             continue;
00212         }
00213         if (!array_key_exists($group->groupingid, $result)) {
00214             $result[$group->groupingid] = array();
00215         }
00216         $result[$group->groupingid][$group->id] = $group->id;
00217     }
00218     $rs->close();
00219 
00220     $result['0'] = array_keys($allgroups); // all groups
00221 
00222     return $result;
00223 }
00224 
00234 function groups_get_all_groupings($courseid) {
00235     global $CFG, $DB;
00236 
00237     return $DB->get_records_sql("SELECT *
00238                                    FROM {groupings}
00239                                   WHERE courseid = ?
00240                                ORDER BY name ASC", array($courseid));
00241 }
00242 
00243 
00244 
00256 function groups_is_member($groupid, $userid=null) {
00257     global $USER, $DB;
00258 
00259     if (!$userid) {
00260         $userid = $USER->id;
00261     }
00262 
00263     return $DB->record_exists('groups_members', array('groupid'=>$groupid, 'userid'=>$userid));
00264 }
00265 
00277 function groups_has_membership($cm, $userid=null) {
00278     global $CFG, $USER, $DB;
00279 
00280     static $cache = array();
00281 
00282     if (empty($userid)) {
00283         $userid = $USER->id;
00284     }
00285 
00286     $cachekey = $userid.'|'.$cm->course.'|'.$cm->groupingid;
00287     if (isset($cache[$cachekey])) {
00288         return($cache[$cachekey]);
00289     }
00290 
00291     if ($cm->groupingid) {
00292         // find out if member of any group in selected activity grouping
00293         $sql = "SELECT 'x'
00294                   FROM {groups_members} gm, {groupings_groups} gg
00295                  WHERE gm.userid = ? AND gm.groupid = gg.groupid AND gg.groupingid = ?";
00296         $params = array($userid, $cm->groupingid);
00297 
00298     } else {
00299         // no grouping used - check all groups in course
00300         $sql = "SELECT 'x'
00301                   FROM {groups_members} gm, {groups} g
00302                  WHERE gm.userid = ? AND gm.groupid = g.id AND g.courseid = ?";
00303         $params = array($userid, $cm->course);
00304     }
00305 
00306     $cache[$cachekey] = $DB->record_exists_sql($sql, $params);
00307 
00308     return $cache[$cachekey];
00309 }
00310 
00321 function groups_get_members($groupid, $fields='u.*', $sort='lastname ASC') {
00322     global $DB;
00323 
00324     return $DB->get_records_sql("SELECT $fields
00325                                    FROM {user} u, {groups_members} gm
00326                                   WHERE u.id = gm.userid AND gm.groupid = ?
00327                                ORDER BY $sort", array($groupid));
00328 }
00329 
00330 
00341 function groups_get_grouping_members($groupingid, $fields='u.*', $sort='lastname ASC') {
00342     global $DB;
00343 
00344     return $DB->get_records_sql("SELECT $fields
00345                                    FROM {user} u
00346                                      INNER JOIN {groups_members} gm ON u.id = gm.userid
00347                                      INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid
00348                                   WHERE  gg.groupingid = ?
00349                                ORDER BY $sort", array($groupingid));
00350 }
00351 
00357 function groups_get_course_groupmode($course) {
00358     return $course->groupmode;
00359 }
00360 
00371 function groups_get_activity_groupmode($cm, $course=null) {
00372     global $COURSE, $DB;
00373 
00374     // get course object (reuse COURSE if possible)
00375     if (isset($course->id) and $course->id == $cm->course) {
00376         //ok
00377     } else if ($cm->course == $COURSE->id) {
00378         $course = $COURSE;
00379     } else {
00380         if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
00381             print_error('invalidcourseid');
00382         }
00383     }
00384 
00385     return empty($course->groupmodeforce) ? $cm->groupmode : $course->groupmode;
00386 }
00387 
00396 function groups_print_course_menu($course, $urlroot, $return=false) {
00397     global $USER, $OUTPUT;
00398 
00399     if (!$groupmode = $course->groupmode) {
00400         if ($return) {
00401             return '';
00402         } else {
00403             return;
00404         }
00405     }
00406 
00407     $context = get_context_instance(CONTEXT_COURSE, $course->id);
00408     $aag = has_capability('moodle/site:accessallgroups', $context);
00409 
00410     if ($groupmode == VISIBLEGROUPS or $aag) {
00411         $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
00412     } else {
00413         $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
00414     }
00415 
00416     $activegroup = groups_get_course_group($course, true, $allowedgroups);
00417 
00418     $groupsmenu = array();
00419     if (!$allowedgroups or $groupmode == VISIBLEGROUPS or $aag) {
00420         $groupsmenu[0] = get_string('allparticipants');
00421     }
00422 
00423     if ($allowedgroups) {
00424         foreach ($allowedgroups as $group) {
00425             $groupsmenu[$group->id] = format_string($group->name);
00426         }
00427     }
00428 
00429     if ($groupmode == VISIBLEGROUPS) {
00430         $grouplabel = get_string('groupsvisible');
00431     } else {
00432         $grouplabel = get_string('groupsseparate');
00433     }
00434 
00435     if ($aag and $course->defaultgroupingid) {
00436         if ($grouping = groups_get_grouping($course->defaultgroupingid)) {
00437             $grouplabel = $grouplabel . ' (' . format_string($grouping->name) . ')';
00438         }
00439     }
00440 
00441     if (count($groupsmenu) == 1) {
00442         $groupname = reset($groupsmenu);
00443         $output = $grouplabel.': '.$groupname;
00444     } else {
00445         $select = new single_select(new moodle_url($urlroot), 'group', $groupsmenu, $activegroup, null, 'selectgroup');
00446         $select->label = $grouplabel;
00447         $output = $OUTPUT->render($select);
00448     }
00449 
00450     $output = '<div class="groupselector">'.$output.'</div>';
00451 
00452     if ($return) {
00453         return $output;
00454     } else {
00455         echo $output;
00456     }
00457 }
00458 
00474 function groups_print_activity_menu($cm, $urlroot, $return=false, $hideallparticipants=false) {
00475     global $USER, $OUTPUT;
00476 
00477     if ($urlroot instanceof moodle_url) {
00478         // no changes necessary
00479 
00480     } else {
00481         if (strpos($urlroot, 'http') !== 0) { // Will also work for https
00482             // Display error if urlroot is not absolute (this causes the non-JS version to break)
00483             debugging('groups_print_activity_menu requires absolute URL for ' .
00484                       '$urlroot, not <tt>' . s($urlroot) . '</tt>. Example: ' .
00485                       'groups_print_activity_menu($cm, $CFG->wwwroot . \'/mod/mymodule/view.php?id=13\');',
00486                       DEBUG_DEVELOPER);
00487         }
00488         $urlroot = new moodle_url($urlroot);
00489     }
00490 
00491     if (!$groupmode = groups_get_activity_groupmode($cm)) {
00492         if ($return) {
00493             return '';
00494         } else {
00495             return;
00496         }
00497     }
00498 
00499     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00500     $aag = has_capability('moodle/site:accessallgroups', $context);
00501 
00502     if ($groupmode == VISIBLEGROUPS or $aag) {
00503         $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping
00504     } else {
00505         $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups
00506     }
00507 
00508     $activegroup = groups_get_activity_group($cm, true, $allowedgroups);
00509 
00510     $groupsmenu = array();
00511     if ((!$allowedgroups or $groupmode == VISIBLEGROUPS or $aag) and !$hideallparticipants) {
00512         $groupsmenu[0] = get_string('allparticipants');
00513     }
00514 
00515     if ($allowedgroups) {
00516         foreach ($allowedgroups as $group) {
00517             $groupsmenu[$group->id] = format_string($group->name);
00518         }
00519     }
00520 
00521     if ($groupmode == VISIBLEGROUPS) {
00522         $grouplabel = get_string('groupsvisible');
00523     } else {
00524         $grouplabel = get_string('groupsseparate');
00525     }
00526 
00527     if ($aag and $cm->groupingid) {
00528         if ($grouping = groups_get_grouping($cm->groupingid)) {
00529             $grouplabel = $grouplabel . ' (' . format_string($grouping->name) . ')';
00530         }
00531     }
00532 
00533     if (count($groupsmenu) == 1) {
00534         $groupname = reset($groupsmenu);
00535         $output = $grouplabel.': '.$groupname;
00536     } else {
00537         $select = new single_select($urlroot, 'group', $groupsmenu, $activegroup, null, 'selectgroup');
00538         $select->label = $grouplabel;
00539         $output = $OUTPUT->render($select);
00540     }
00541 
00542     $output = '<div class="groupselector">'.$output.'</div>';
00543 
00544     if ($return) {
00545         return $output;
00546     } else {
00547         echo $output;
00548     }
00549 }
00550 
00559 function groups_get_course_group($course, $update=false, $allowedgroups=null) {
00560     global $USER, $SESSION;
00561 
00562     if (!$groupmode = $course->groupmode) {
00563         // NOGROUPS used
00564         return false;
00565     }
00566 
00567     $context = get_context_instance(CONTEXT_COURSE, $course->id);
00568     if (has_capability('moodle/site:accessallgroups', $context)) {
00569         $groupmode = 'aag';
00570     }
00571 
00572     if (!is_array($allowedgroups)) {
00573         if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
00574             $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
00575         } else {
00576             $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
00577         }
00578     }
00579 
00580     _group_verify_activegroup($course->id, $groupmode, $course->defaultgroupingid, $allowedgroups);
00581 
00582     // set new active group if requested
00583     $changegroup = optional_param('group', -1, PARAM_INT);
00584     if ($update and $changegroup != -1) {
00585 
00586         if ($changegroup == 0) {
00587             // do not allow changing to all groups without accessallgroups capability
00588             if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
00589                 $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid] = 0;
00590             }
00591 
00592         } else {
00593             if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
00594                 $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid] = $changegroup;
00595             }
00596         }
00597     }
00598 
00599     return $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid];
00600 }
00601 
00610 function groups_get_activity_group($cm, $update=false, $allowedgroups=null) {
00611     global $USER, $SESSION;
00612 
00613     if (!$groupmode = groups_get_activity_groupmode($cm)) {
00614         // NOGROUPS used
00615         return false;
00616     }
00617 
00618     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00619     if (has_capability('moodle/site:accessallgroups', $context)) {
00620         $groupmode = 'aag';
00621     }
00622 
00623     if (!is_array($allowedgroups)) {
00624         if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
00625             $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid);
00626         } else {
00627             $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid);
00628         }
00629     }
00630 
00631     _group_verify_activegroup($cm->course, $groupmode, $cm->groupingid, $allowedgroups);
00632 
00633     // set new active group if requested
00634     $changegroup = optional_param('group', -1, PARAM_INT);
00635     if ($update and $changegroup != -1) {
00636 
00637         if ($changegroup == 0) {
00638             // allgroups visible only in VISIBLEGROUPS or when accessallgroups
00639             if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
00640                 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
00641             }
00642 
00643         } else {
00644             if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
00645                 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $changegroup;
00646             }
00647         }
00648     }
00649 
00650     return $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid];
00651 }
00652 
00661 function groups_get_activity_allowed_groups($cm,$userid=0) {
00662     // Use current user by default
00663     global $USER;
00664     if(!$userid) {
00665         $userid=$USER->id;
00666     }
00667 
00668     // Get groupmode for activity, taking into account course settings
00669     $groupmode=groups_get_activity_groupmode($cm);
00670 
00671     // If visible groups mode, or user has the accessallgroups capability,
00672     // then they can access all groups for the activity...
00673     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00674     if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
00675         return groups_get_all_groups($cm->course, 0, $cm->groupingid);
00676     } else {
00677         // ...otherwise they can only access groups they belong to
00678         return groups_get_all_groups($cm->course, $userid, $cm->groupingid);
00679     }
00680 }
00681 
00691 function groups_course_module_visible($cm, $userid=null) {
00692     global $CFG, $USER;
00693 
00694     if (empty($userid)) {
00695         $userid = $USER->id;
00696     }
00697     if (empty($CFG->enablegroupmembersonly)) {
00698         return true;
00699     }
00700     if (empty($cm->groupmembersonly)) {
00701         return true;
00702     }
00703     if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id), $userid) or groups_has_membership($cm, $userid)) {
00704         return true;
00705     }
00706     return false;
00707 }
00708 
00719 function _group_verify_activegroup($courseid, $groupmode, $groupingid, array $allowedgroups) {
00720     global $SESSION, $USER;
00721 
00722     // init activegroup array if necessary
00723     if (!isset($SESSION->activegroup)) {
00724         $SESSION->activegroup = array();
00725     }
00726     if (!array_key_exists($courseid, $SESSION->activegroup)) {
00727         $SESSION->activegroup[$courseid] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array(), 'aag'=>array());
00728     }
00729 
00730     // make sure that the current group info is ok
00731     if (array_key_exists($groupingid, $SESSION->activegroup[$courseid][$groupmode]) and !array_key_exists($SESSION->activegroup[$courseid][$groupmode][$groupingid], $allowedgroups)) {
00732         // active group does not exist anymore or is 0
00733         if ($SESSION->activegroup[$courseid][$groupmode][$groupingid] > 0 or $groupmode == SEPARATEGROUPS) {
00734             // do not do this if all groups selected and groupmode is not separate
00735             unset($SESSION->activegroup[$courseid][$groupmode][$groupingid]);
00736         }
00737     }
00738 
00739     // set up defaults if necessary
00740     if (!array_key_exists($groupingid, $SESSION->activegroup[$courseid][$groupmode])) {
00741         if ($groupmode == 'aag') {
00742             $SESSION->activegroup[$courseid][$groupmode][$groupingid] = 0; // all groups by default if user has accessallgroups
00743 
00744         } else if ($allowedgroups) {
00745             if ($groupmode != SEPARATEGROUPS and $mygroups = groups_get_all_groups($courseid, $USER->id, $groupingid)) {
00746                 $firstgroup = reset($mygroups);
00747             } else {
00748                 $firstgroup = reset($allowedgroups);
00749             }
00750             $SESSION->activegroup[$courseid][$groupmode][$groupingid] = $firstgroup->id;
00751 
00752         } else {
00753             // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
00754             // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
00755             $SESSION->activegroup[$courseid][$groupmode][$groupingid] = 0;
00756         }
00757     }
00758 }
 All Data Structures Namespaces Files Functions Variables Enumerations