/** * Function to handle the group_user_added-event * * @param object $eventdata - the event's data */ function group_user_added_handler($eventdata) { global $CFG; //check if this group is relevant $group = groups_get_group($eventdata->groupid); $courseid = $group->courseid; //if(record_exists('mumiemodule_students', 'groupid', $eventdata->groupid)){ //BUT WE SHOULD BETTER CHECK IF THE WHOLE COURSE IS RELEVANT if (record_exists('mumiemodule', 'course', $courseid)) { event_logoutput("group_user_added_handler called \n", $eventdata); //check if current user might be tutor or not //$context = get_context_instance(CONTEXT_GROUP, $eventdata->groupid); $context = get_context_instance(CONTEXT_COURSE, $group->courseid); if (has_capability('mod/mumiemodule:tutorize', $context, $eventdata->userid)) { //this is a tutor //groups must not have more than one tutor in this activity! //REMOVE AFTER MUMIE-DB CHANGE $fields = 'u.id'; //$groupcontext = get_context_instance(CONTEXT_GROUP, $eventdata->groupid); $grouptutors = get_users_by_capability($context, 'mod/mumiemodule:tutorize', $fields, '', '', '', $eventdata->groupid); if (count($grouptutors) > 1) { $removed = groups_remove_member($eventdata->groupid, $eventdata->userid); error(get_string('toomuchtutorssingle', 'mumiemodule'), "../group/members.php?group=" . $eventdata->groupid); break; } //REMOVE TILL HERE if (!has_capability('mod/mumiemodule:teachcourse', $context, $eventdata->userid)) { //so this is really just a tutor, lecturers are already known in mumie //perhaps the tutor is already known in MUMIE $tutortocheck = new object(); $tutortocheck->syncid = 'moodle-' . $CFG . 'user-' . $eventdata->userid; $tutorexists = check_tutor($tutortocheck); $islecturer = check_lecturer($tutortocheck); if (!$tutorexists && !$islecturer) { $newuser = get_record('user', 'id', $eventdata->userid); send_single_user_to_mumie($newuser); } } //start creating the tutorial-object $tutorial = new object(); $tutorial->syncid = 'moodle-' . $CFG->prefix . 'groups-' . $eventdata->groupid; $tutorial->name = $name->name; $tutorial->description = $group->description; $tutorial->tutor = 'moodle-' . $CFG->prefix . 'user-' . $eventdata->userid; $tutorial->classid = 'moodle-' . $CFG->prefix . 'course-' . $courseid; change_tutorial_for_mumie($tutorial); } else { if (has_capability('mod/mumiemodule:participate', $context, $eventdata->userid)) { //this is a student //a student must not be in more than one group in this activity $studentsgroups = groups_get_all_groups($courseid, $eventdata->userid); if (count($studentsgroups) > 1) { $removed = groups_remove_member($eventdata->groupid, $eventdata->userid); error(get_string('justonegroupsingle', 'mumiemodule'), "../group/members.php?group=" . $eventdata->groupid); break; } $mumiemodules = get_records('mumiemodule', 'course', $courseid); //variable to keep in mind whether there are more than one mumiemodules in this course $morethanone = false; foreach ($mumiemodules as $mumiemodule) { mumiemodule_students_subscribe($eventdata->userid, $mumiemodule->id, $group, $morethanone); $morethanone = true; } } } } else { //ACHTUNG: dies könnte eine nachträglich eingefügte gruppe sein!!!!!!!!!!!!!!!!!!!!!!!!!! } return true; }
/** * Recursive function to add subscriptions for new teachers * students are just assigned via groups * * @param $userid - the ID of the user added * @param $context - the context the user is added in * @param roleid - the ID of the user's role */ function mumiemodule_add_teacher_subscriptions($userid, $context) { global $CFG; if (empty($context->contextlevel)) { return false; } switch ($context->contextlevel) { case CONTEXT_SYSTEM: // For the whole site if ($courses = get_records('course')) { foreach ($courses as $course) { $subcontext = get_context_instance(CONTEXT_COURSE, $course->id); mumiemodule_add_teacher_subscriptions($userid, $subcontext); } } break; case CONTEXT_COURSECAT: // For a whole category if ($courses = get_records('course', 'category', $context->instanceid)) { foreach ($courses as $course) { $subcontext = get_context_instance(CONTEXT_COURSE, $course->id); mumiemodule_add_teacher_subscriptions($userid, $subcontext); } } if ($categories = get_records('course_categories', 'parent', $context->instanceid)) { foreach ($categories as $category) { $subcontext = get_context_instance(CONTEXT_COURSECAT, $category->id); mumiemodule_add_teacher_subscriptions($userid, $subcontext); } } break; case CONTEXT_COURSE: // For a whole course if ($course = get_record('course', 'id', $context->instanceid)) { if ($mumiemodules = get_all_instances_in_course('mumiemodule', $course, $userid, false)) { $fields = 'id, username, password, firstname, lastname'; //check if this lecturer is already in mumie as user $lecturertocheck = new object(); $lecturertocheck->syncid = 'moodle-' . $CFG->prefix . 'user-' . $userid; $lecturerexists = check_lecturer($lecturertocheck, $course->id); if (!$lecturerexists) { $newuser = get_record('user', 'id', $userid, '', '', '', '', $fields); send_single_user_to_mumie($newuser); } foreach ($mumiemodules as $mumiemodule) { if ($modcontext = get_context_instance(CONTEXT_MODULE, $mumiemodule->coursemodule)) { $course_to_change = new object(); $course_to_change->syncid = 'moodle-' . $CFG->prefix . 'course-' . $course->id; $course_to_change->name = $course->fullname; $course_to_change->description = $course->summary; $course_to_change->semester = 'moodle-' . $CFG->prefix . 'course_categories-' . $course->category; $course_to_change->lecturers = ''; //$context = get_context_instance(CONTEXT_COURSE, $course->id); $roleid = get_field('role', 'id', 'shortname', 'editingteacher'); $fields = 'u.id, u.username, u.password, u.firstname, u.lastname'; if ($lecturers = get_role_users($roleid, $context, false, $fields)) { foreach ($lecturers as $lecturer) { //get mumie to know the lecturer: //send_single_user_to_mumie($lecturer); //MUMIE should already know them all $course_to_change->lecturers .= 'moodle-' . $CFG->prefix . 'user-' . $lecturer->id . ' '; } } //$course_to_change->lecturers .= 'moodle-'.$CFG->prefix.'user-'.$userid; //or he is in the list twice change_class_for_mumie($course_to_change); } } } } break; //is this relevant?? can we really assign a role to just one mumiemodule????? //is this relevant?? can we really assign a role to just one mumiemodule????? case CONTEXT_MODULE: // Just one module //necessary for mumiemodule??? if ($cm = get_coursemodule_from_id('mumiemodule', $context->instanceid)) { if ($mumiemodule = get_record('mumiemodule', 'id', $cm->instance)) { if (has_capability('mod/mumiemodule:tutorize', $modcontext, $userid)) { $newuser = get_record('user', 'id', $userid); send_single_user_to_mumie($newuser); } } } break; } return true; }
/** * Function to insert all neccesarry information out of a new MUMIE-module into MUMIE * * @param object $insert - big object with all information */ function insert_all_for_mumie($insert) { global $CFG; global $COURSE; $js = new JapsSynchronise(); //first we insert the new semester if MUMIE does not know it yet $semesterexists = check_semester($insert->semester); if (!$semesterexists) { insert_semester_for_mumie($insert->semester, $js); } //now we insert the new class $newclass = new object(); $newclass->syncid = $insert->syncid; $newclass->name = $insert->name; $newclass->description = $insert->description; $newclass->semester = $insert->semester->syncid; $newclass->lecturers = ''; //let's be sure to have all lecturers as users in the MUMIE DB if ($insert->lecturers) { foreach ($insert->lecturers as $lecturer) { //perhaps the lecturer is already in the MUMIE $lecturerexists = check_lecturer($lecturer); if (!$lecturerexists) { insert_user_for_mumie($lecturer, $js); } $newclass->lecturers .= $lecturer->syncid . ' '; } } $newclass->mumie_course_id = $insert->mumie_course_id; insert_class_for_mumie($newclass, $js); //next is the tutorials if ($insert->tutorials) { foreach ($insert->tutorials as $tutorial) { //we should make sure that the tutor is a user in the Mumie DB if ($tutorial->tutor) { //perhaps the tutor is already in the MUMIE $tutorexists = check_tutor($tutorial->tutor); $tutor_is_lecturer = check_lecturer($tutorial->tutor); if (!$tutorexists && !$tutor_is_lecturer) { insert_user_for_mumie($tutorial->tutor, $js); } $tutorial->tutor = $tutorial->tutor->syncid; } else { //no tutor => insert dummy $tutorial->tutor = 'moodle-dummy_tutor'; } $tutorial->classid = $insert->syncid; insert_tutorial_for_mumie($tutorial, $js); if ($tutorial->students) { foreach ($tutorial->students as $student) { //perhaps MUMIE already knows this student so we first check $studentexists = check_student($student); if (!$studentexists) { insert_user_for_mumie($student, $js); } add_user_to_mumie_tutorial($student->syncid, $tutorial->syncid, $js); } } } } }