|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00011 /* 00012 * INTERNAL FUNCTIONS - to be used by moodle core only 00013 * require_once $CFG->dirroot.'/group/lib.php' must be used 00014 */ 00015 00023 function groups_add_member($grouporid, $userorid) { 00024 global $DB; 00025 00026 if (is_object($userorid)) { 00027 $userid = $userorid->id; 00028 $user = $userorid; 00029 } else { 00030 $userid = $userorid; 00031 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST); 00032 } 00033 00034 if (is_object($grouporid)) { 00035 $groupid = $grouporid->id; 00036 $group = $grouporid; 00037 } else { 00038 $groupid = $grouporid; 00039 $group = $DB->get_record('groups', array('id'=>$groupid), '*', MUST_EXIST); 00040 } 00041 00042 //check if the user a participant of the group course 00043 if (!is_enrolled(get_context_instance(CONTEXT_COURSE, $group->courseid), $userid)) { 00044 return false; 00045 } 00046 00047 if (groups_is_member($groupid, $userid)) { 00048 return true; 00049 } 00050 00051 $member = new stdClass(); 00052 $member->groupid = $groupid; 00053 $member->userid = $userid; 00054 $member->timeadded = time(); 00055 00056 $DB->insert_record('groups_members', $member); 00057 00058 //update group info 00059 $DB->set_field('groups', 'timemodified', $member->timeadded, array('id'=>$groupid)); 00060 00061 //trigger groups events 00062 $eventdata = new stdClass(); 00063 $eventdata->groupid = $groupid; 00064 $eventdata->userid = $userid; 00065 events_trigger('groups_member_added', $eventdata); 00066 00067 return true; 00068 } 00069 00076 function groups_remove_member($grouporid, $userorid) { 00077 global $DB; 00078 00079 if (is_object($userorid)) { 00080 $userid = $userorid->id; 00081 $user = $userorid; 00082 } else { 00083 $userid = $userorid; 00084 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST); 00085 } 00086 00087 if (is_object($grouporid)) { 00088 $groupid = $grouporid->id; 00089 $group = $grouporid; 00090 } else { 00091 $groupid = $grouporid; 00092 $group = $DB->get_record('groups', array('id'=>$groupid), '*', MUST_EXIST); 00093 } 00094 00095 if (!groups_is_member($groupid, $userid)) { 00096 return true; 00097 } 00098 00099 $DB->delete_records('groups_members', array('groupid'=>$groupid, 'userid'=>$userid)); 00100 00101 //update group info 00102 $DB->set_field('groups', 'timemodified', time(), array('id'=>$groupid)); 00103 00104 //trigger groups events 00105 $eventdata = new stdClass(); 00106 $eventdata->groupid = $groupid; 00107 $eventdata->userid = $userid; 00108 events_trigger('groups_member_removed', $eventdata); 00109 00110 return true; 00111 } 00112 00119 function groups_create_group($data, $editform = false, $editoroptions = false) { 00120 global $CFG, $DB; 00121 00122 //check that courseid exists 00123 $course = $DB->get_record('course', array('id' => $data->courseid), '*', MUST_EXIST); 00124 $context = get_context_instance(CONTEXT_COURSE, $course->id); 00125 00126 $data->timecreated = time(); 00127 $data->timemodified = $data->timecreated; 00128 $data->name = trim($data->name); 00129 00130 if ($editform and $editoroptions) { 00131 $data->description = $data->description_editor['text']; 00132 $data->descriptionformat = $data->description_editor['format']; 00133 } 00134 00135 $data->id = $DB->insert_record('groups', $data); 00136 00137 if ($editform and $editoroptions) { 00138 // Update description from editor with fixed files 00139 $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context, 'group', 'description', $data->id); 00140 $upd = new stdClass(); 00141 $upd->id = $data->id; 00142 $upd->description = $data->description; 00143 $upd->descriptionformat = $data->descriptionformat; 00144 $DB->update_record('groups', $upd); 00145 } 00146 00147 $group = $DB->get_record('groups', array('id'=>$data->id)); 00148 00149 if ($editform) { 00150 groups_update_group_icon($group, $data, $editform); 00151 } 00152 00153 //trigger groups events 00154 events_trigger('groups_group_created', $group); 00155 00156 return $group->id; 00157 } 00158 00164 function groups_create_grouping($data, $editoroptions=null) { 00165 global $DB; 00166 00167 $data->timecreated = time(); 00168 $data->timemodified = $data->timecreated; 00169 $data->name = trim($data->name); 00170 00171 if ($editoroptions !== null) { 00172 $data->description = $data->description_editor['text']; 00173 $data->descriptionformat = $data->description_editor['format']; 00174 } 00175 00176 $id = $DB->insert_record('groupings', $data); 00177 00178 //trigger groups events 00179 $data->id = $id; 00180 00181 if ($editoroptions !== null) { 00182 $description = new stdClass; 00183 $description->id = $data->id; 00184 $description->description_editor = $data->description_editor; 00185 $description = file_postupdate_standard_editor($description, 'description', $editoroptions, $editoroptions['context'], 'grouping', 'description', $description->id); 00186 $DB->update_record('groupings', $description); 00187 } 00188 00189 events_trigger('groups_grouping_created', $data); 00190 00191 return $id; 00192 } 00193 00200 function groups_update_group_icon($group, $data, $editform) { 00201 global $CFG, $DB; 00202 require_once("$CFG->libdir/gdlib.php"); 00203 00204 $fs = get_file_storage(); 00205 $context = get_context_instance(CONTEXT_COURSE, $group->courseid, MUST_EXIST); 00206 00207 //TODO: it would make sense to allow picture deleting too (skodak) 00208 00209 if ($iconfile = $editform->save_temp_file('imagefile')) { 00210 if (process_new_icon($context, 'group', 'icon', $group->id, $iconfile)) { 00211 $DB->set_field('groups', 'picture', 1, array('id'=>$group->id)); 00212 $group->picture = 1; 00213 } else { 00214 $fs->delete_area_files($context->id, 'group', 'icon', $group->id); 00215 $DB->set_field('groups', 'picture', 0, array('id'=>$group->id)); 00216 $group->picture = 0; 00217 } 00218 @unlink($iconfile); 00219 } 00220 } 00221 00229 function groups_update_group($data, $editform = false, $editoroptions = false) { 00230 global $CFG, $DB; 00231 00232 $context = get_context_instance(CONTEXT_COURSE, $data->courseid); 00233 00234 $data->timemodified = time(); 00235 $data->name = trim($data->name); 00236 00237 if ($editform and $editoroptions) { 00238 $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context, 'group', 'description', $data->id); 00239 } 00240 00241 $DB->update_record('groups', $data); 00242 00243 $group = $DB->get_record('groups', array('id'=>$data->id)); 00244 00245 if ($editform) { 00246 groups_update_group_icon($group, $data, $editform); 00247 } 00248 00249 //trigger groups events 00250 events_trigger('groups_group_updated', $group); 00251 00252 00253 return true; 00254 } 00255 00261 function groups_update_grouping($data, $editoroptions=null) { 00262 global $DB; 00263 $data->timemodified = time(); 00264 $data->name = trim($data->name); 00265 if ($editoroptions !== null) { 00266 $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $editoroptions['context'], 'grouping', 'description', $data->id); 00267 } 00268 $DB->update_record('groupings', $data); 00269 //trigger groups events 00270 events_trigger('groups_grouping_updated', $data); 00271 00272 return true; 00273 } 00274 00281 function groups_delete_group($grouporid) { 00282 global $CFG, $DB; 00283 require_once("$CFG->libdir/gdlib.php"); 00284 00285 if (is_object($grouporid)) { 00286 $groupid = $grouporid->id; 00287 $group = $grouporid; 00288 } else { 00289 $groupid = $grouporid; 00290 if (!$group = $DB->get_record('groups', array('id'=>$groupid))) { 00291 //silently ignore attempts to delete missing already deleted groups ;-) 00292 return true; 00293 } 00294 } 00295 00296 // delete group calendar events 00297 $DB->delete_records('event', array('groupid'=>$groupid)); 00298 //first delete usage in groupings_groups 00299 $DB->delete_records('groupings_groups', array('groupid'=>$groupid)); 00300 //delete members 00301 $DB->delete_records('groups_members', array('groupid'=>$groupid)); 00302 //group itself last 00303 $DB->delete_records('groups', array('id'=>$groupid)); 00304 00305 // Delete all files associated with this group 00306 $context = get_context_instance(CONTEXT_COURSE, $group->courseid); 00307 $fs = get_file_storage(); 00308 $fs->delete_area_files($context->id, 'group', 'description', $groupid); 00309 $fs->delete_area_files($context->id, 'group', 'icon', $groupid); 00310 00311 //trigger groups events 00312 events_trigger('groups_group_deleted', $group); 00313 00314 return true; 00315 } 00316 00322 function groups_delete_grouping($groupingorid) { 00323 global $DB; 00324 00325 if (is_object($groupingorid)) { 00326 $groupingid = $groupingorid->id; 00327 $grouping = $groupingorid; 00328 } else { 00329 $groupingid = $groupingorid; 00330 if (!$grouping = $DB->get_record('groupings', array('id'=>$groupingorid))) { 00331 //silently ignore attempts to delete missing already deleted groupings ;-) 00332 return true; 00333 } 00334 } 00335 00336 //first delete usage in groupings_groups 00337 $DB->delete_records('groupings_groups', array('groupingid'=>$groupingid)); 00338 // remove the default groupingid from course 00339 $DB->set_field('course', 'defaultgroupingid', 0, array('defaultgroupingid'=>$groupingid)); 00340 // remove the groupingid from all course modules 00341 $DB->set_field('course_modules', 'groupingid', 0, array('groupingid'=>$groupingid)); 00342 //group itself last 00343 $DB->delete_records('groupings', array('id'=>$groupingid)); 00344 00345 $context = get_context_instance(CONTEXT_COURSE, $grouping->courseid); 00346 $fs = get_file_storage(); 00347 $files = $fs->get_area_files($context->id, 'grouping', 'description', $groupingid); 00348 foreach ($files as $file) { 00349 $file->delete(); 00350 } 00351 00352 //trigger groups events 00353 events_trigger('groups_grouping_deleted', $grouping); 00354 00355 return true; 00356 } 00357 00365 function groups_delete_group_members($courseid, $userid=0, $showfeedback=false) { 00366 global $DB, $OUTPUT; 00367 00368 if (is_bool($userid)) { 00369 debugging('Incorrect userid function parameter'); 00370 return false; 00371 } 00372 00373 $params = array('courseid'=>$courseid); 00374 00375 if ($userid) { 00376 $usersql = "AND userid = :userid"; 00377 $params['userid'] = $userid; 00378 } else { 00379 $usersql = ""; 00380 } 00381 00382 $groupssql = "SELECT id FROM {groups} g WHERE g.courseid = :courseid"; 00383 $DB->delete_records_select('groups_members', "groupid IN ($groupssql) $usersql", $params); 00384 00385 //trigger groups events 00386 $eventdata = new stdClass(); 00387 $eventdata->courseid = $courseid; 00388 $eventdata->userid = $userid; 00389 events_trigger('groups_members_removed', $eventdata); 00390 00391 if ($showfeedback) { 00392 echo $OUTPUT->notification(get_string('deleted').' - '.get_string('groupmembers', 'group'), 'notifysuccess'); 00393 } 00394 00395 return true; 00396 } 00397 00404 function groups_delete_groupings_groups($courseid, $showfeedback=false) { 00405 global $DB, $OUTPUT; 00406 00407 $groupssql = "SELECT id FROM {groups} g WHERE g.courseid = ?"; 00408 $DB->delete_records_select('groupings_groups', "groupid IN ($groupssql)", array($courseid)); 00409 00410 //trigger groups events 00411 events_trigger('groups_groupings_groups_removed', $courseid); 00412 00413 // no need to show any feedback here - we delete usually first groupings and then groups 00414 00415 return true; 00416 } 00417 00424 function groups_delete_groups($courseid, $showfeedback=false) { 00425 global $CFG, $DB, $OUTPUT; 00426 00427 // delete any uses of groups 00428 // Any associated files are deleted as part of groups_delete_groupings_groups 00429 groups_delete_groupings_groups($courseid, $showfeedback); 00430 groups_delete_group_members($courseid, 0, $showfeedback); 00431 00432 // delete group pictures and descriptions 00433 $context = get_context_instance(CONTEXT_COURSE, $courseid); 00434 $fs = get_file_storage(); 00435 $fs->delete_area_files($context->id, 'group'); 00436 00437 // delete group calendar events 00438 $groupssql = "SELECT id FROM {groups} g WHERE g.courseid = ?"; 00439 $DB->delete_records_select('event', "groupid IN ($groupssql)", array($courseid)); 00440 00441 $context = get_context_instance(CONTEXT_COURSE, $courseid); 00442 $fs = get_file_storage(); 00443 $fs->delete_area_files($context->id, 'group'); 00444 00445 $DB->delete_records('groups', array('courseid'=>$courseid)); 00446 00447 // trigger groups events 00448 events_trigger('groups_groups_deleted', $courseid); 00449 00450 if ($showfeedback) { 00451 echo $OUTPUT->notification(get_string('deleted').' - '.get_string('groups', 'group'), 'notifysuccess'); 00452 } 00453 00454 return true; 00455 } 00456 00463 function groups_delete_groupings($courseid, $showfeedback=false) { 00464 global $DB, $OUTPUT; 00465 00466 // delete any uses of groupings 00467 $sql = "DELETE FROM {groupings_groups} 00468 WHERE groupingid in (SELECT id FROM {groupings} g WHERE g.courseid = ?)"; 00469 $DB->execute($sql, array($courseid)); 00470 00471 // remove the default groupingid from course 00472 $DB->set_field('course', 'defaultgroupingid', 0, array('id'=>$courseid)); 00473 // remove the groupingid from all course modules 00474 $DB->set_field('course_modules', 'groupingid', 0, array('course'=>$courseid)); 00475 00476 // Delete all files associated with groupings for this course 00477 $context = get_context_instance(CONTEXT_COURSE, $courseid); 00478 $fs = get_file_storage(); 00479 $fs->delete_area_files($context->id, 'grouping'); 00480 00481 $DB->delete_records('groupings', array('courseid'=>$courseid)); 00482 00483 // trigger groups events 00484 events_trigger('groups_groupings_deleted', $courseid); 00485 00486 if ($showfeedback) { 00487 echo $OUTPUT->notification(get_string('deleted').' - '.get_string('groupings', 'group'), 'notifysuccess'); 00488 } 00489 00490 return true; 00491 } 00492 00493 /* =================================== */ 00494 /* various functions used by groups UI */ 00495 /* =================================== */ 00496 00503 function groups_get_possible_roles($context) { 00504 $roles = get_profile_roles($context); 00505 return array_keys($roles); 00506 } 00507 00508 00517 function groups_get_potential_members($courseid, $roleid = null, $cohortid = null, $orderby = 'lastname ASC, firstname ASC') { 00518 global $DB; 00519 00520 $context = get_context_instance(CONTEXT_COURSE, $courseid); 00521 00522 // we are looking for all users with this role assigned in this context or higher 00523 $listofcontexts = get_related_contexts_string($context); 00524 00525 list($esql, $params) = get_enrolled_sql($context); 00526 00527 if ($roleid) { 00528 $params['roleid'] = $roleid; 00529 $where = "WHERE u.id IN (SELECT userid 00530 FROM {role_assignments} 00531 WHERE roleid = :roleid AND contextid $listofcontexts)"; 00532 } else { 00533 $where = ""; 00534 } 00535 00536 if ($cohortid) { 00537 $cohortjoin = "JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)"; 00538 $params['cohortid'] = $cohortid; 00539 } else { 00540 $cohortjoin = ""; 00541 } 00542 00543 $sql = "SELECT u.id, u.username, u.firstname, u.lastname, u.idnumber 00544 FROM {user} u 00545 JOIN ($esql) e ON e.id = u.id 00546 $cohortjoin 00547 $where 00548 ORDER BY $orderby"; 00549 00550 return $DB->get_records_sql($sql, $params); 00551 00552 } 00553 00560 function groups_parse_name($format, $groupnumber) { 00561 if (strstr($format, '@') !== false) { // Convert $groupnumber to a character series 00562 $letter = 'A'; 00563 for($i=0; $i<$groupnumber; $i++) { 00564 $letter++; 00565 } 00566 $str = str_replace('@', $letter, $format); 00567 } else { 00568 $str = str_replace('#', $groupnumber+1, $format); 00569 } 00570 return($str); 00571 } 00572 00579 function groups_assign_grouping($groupingid, $groupid) { 00580 global $DB; 00581 00582 if ($DB->record_exists('groupings_groups', array('groupingid'=>$groupingid, 'groupid'=>$groupid))) { 00583 return true; 00584 } 00585 $assign = new stdClass(); 00586 $assign->groupingid = $groupingid; 00587 $assign->groupid = $groupid; 00588 $assign->timeadded = time(); 00589 $DB->insert_record('groupings_groups', $assign); 00590 00591 return true; 00592 } 00593 00600 function groups_unassign_grouping($groupingid, $groupid) { 00601 global $DB; 00602 $DB->delete_records('groupings_groups', array('groupingid'=>$groupingid, 'groupid'=>$groupid)); 00603 00604 return true; 00605 } 00606 00625 function groups_get_members_by_role($groupid, $courseid, $fields='u.*', 00626 $sort='u.lastname ASC', $extrawheretest='', $whereparams=array()) { 00627 global $CFG, $DB; 00628 00629 // Retrieve information about all users and their roles on the course or 00630 // parent ('related') contexts 00631 $context = get_context_instance(CONTEXT_COURSE, $courseid); 00632 00633 if ($extrawheretest) { 00634 $extrawheretest = ' AND ' . $extrawheretest; 00635 } 00636 00637 $sql = "SELECT r.id AS roleid, r.shortname AS roleshortname, r.name AS rolename, 00638 u.id AS userid, $fields 00639 FROM {groups_members} gm 00640 JOIN {user} u ON u.id = gm.userid 00641 LEFT JOIN {role_assignments} ra ON (ra.userid = u.id AND ra.contextid ".get_related_contexts_string($context).") 00642 LEFT JOIN {role} r ON r.id = ra.roleid 00643 WHERE gm.groupid=:mgroupid 00644 ".$extrawheretest." 00645 ORDER BY r.sortorder, $sort"; 00646 $whereparams['mgroupid'] = $groupid; 00647 $rs = $DB->get_recordset_sql($sql, $whereparams); 00648 00649 return groups_calculate_role_people($rs, $context); 00650 } 00651 00661 function groups_calculate_role_people($rs, $context) { 00662 global $CFG, $DB; 00663 00664 if (!$rs) { 00665 return array(); 00666 } 00667 00668 $roles = $DB->get_records_menu('role', null, 'name', 'id, name'); 00669 $aliasnames = role_fix_names($roles, $context); 00670 00671 // Array of all involved roles 00672 $roles = array(); 00673 // Array of all retrieved users 00674 $users = array(); 00675 // Fill arrays 00676 foreach ($rs as $rec) { 00677 // Create information about user if this is a new one 00678 if (!array_key_exists($rec->userid, $users)) { 00679 // User data includes all the optional fields, but not any of the 00680 // stuff we added to get the role details 00681 $userdata = clone($rec); 00682 unset($userdata->roleid); 00683 unset($userdata->roleshortname); 00684 unset($userdata->rolename); 00685 unset($userdata->userid); 00686 $userdata->id = $rec->userid; 00687 00688 // Make an array to hold the list of roles for this user 00689 $userdata->roles = array(); 00690 $users[$rec->userid] = $userdata; 00691 } 00692 // If user has a role... 00693 if (!is_null($rec->roleid)) { 00694 // Create information about role if this is a new one 00695 if (!array_key_exists($rec->roleid,$roles)) { 00696 $roledata = new stdClass(); 00697 $roledata->id = $rec->roleid; 00698 $roledata->shortname = $rec->roleshortname; 00699 if (array_key_exists($rec->roleid, $aliasnames)) { 00700 $roledata->name = $aliasnames[$rec->roleid]; 00701 } else { 00702 $roledata->name = $rec->rolename; 00703 } 00704 $roledata->users = array(); 00705 $roles[$roledata->id] = $roledata; 00706 } 00707 // Record that user has role 00708 $users[$rec->userid]->roles[] = $roles[$rec->roleid]; 00709 } 00710 } 00711 $rs->close(); 00712 00713 // Return false if there weren't any users 00714 if (count($users) == 0) { 00715 return false; 00716 } 00717 00718 // Add pseudo-role for multiple roles 00719 $roledata = new stdClass(); 00720 $roledata->name = get_string('multipleroles','role'); 00721 $roledata->users = array(); 00722 $roles['*'] = $roledata; 00723 00724 $roledata = new stdClass(); 00725 $roledata->name = get_string('noroles','role'); 00726 $roledata->users = array(); 00727 $roles[0] = $roledata; 00728 00729 // Now we rearrange the data to store users by role 00730 foreach ($users as $userid=>$userdata) { 00731 $rolecount = count($userdata->roles); 00732 if ($rolecount == 0) { 00733 // does not have any roles 00734 $roleid = 0; 00735 } else if($rolecount > 1) { 00736 $roleid = '*'; 00737 } else { 00738 $roleid = $userdata->roles[0]->id; 00739 } 00740 $roles[$roleid]->users[$userid] = $userdata; 00741 } 00742 00743 // Delete roles not used 00744 foreach ($roles as $key=>$roledata) { 00745 if (count($roledata->users)===0) { 00746 unset($roles[$key]); 00747 } 00748 } 00749 00750 // Return list of roles containing their users 00751 return $roles; 00752 }