예제 #1
0
 /**
  * unregisters/unqueues a user from a certain active-group
  *
  * @param int $agrpid active-group-id to unregister/unqueue user from
  * @param int $userid user to unregister/unqueue
  * @param bool $previewonly optional don't act, just return a preview
  * @return array ($error, $message)
  */
 private function unregister_from_agrp($agrpid = 0, $userid = 0, $previewonly = false)
 {
     global $USER, $PAGE, $DB, $CFG;
     if (empty($agrpid)) {
         print_error('missing_param', null, $PAGE->url);
     }
     if (empty($userid)) {
         $userid = $USER->id;
         require_capability('mod/grouptool:register', $this->context);
     }
     $regopen = $this->grouptool->allow_reg && ($this->grouptool->timedue == 0 || time() < $this->grouptool->timedue);
     if (!$regopen && !has_capability('mod/grouptool:register_students', $this->context)) {
         return array(true, get_string('reg_not_open', 'grouptool'));
     }
     if (empty($this->grouptool->allow_unreg)) {
         return array(true, get_string('unreg_not_allowed', 'grouptool'));
     }
     $message = new stdClass();
     if ($userid != $USER->id) {
         $userdata = $DB->get_record('user', array('id' => $userid));
         $message->username = fullname($userdata);
     }
     $groupdata = $this->get_active_groups(true, true, $agrpid);
     $groupdata = reset($groupdata);
     $message->groupname = $groupdata->name;
     $agrpids = $DB->get_fieldset_select('grouptool_agrps', 'id', "grouptoolid = ?", array($this->grouptool->id));
     list($agrpsql, $params) = $DB->get_in_or_equal($agrpids);
     array_unshift($params, $userid);
     $userregs = $DB->count_records_select('grouptool_registered', "modified_by >= 0 AND userid = ? AND agrpid " . $agrpsql, $params);
     $userqueues = $DB->count_records_select('grouptool_queued', "userid = ? AND agrpid " . $agrpsql, $params);
     $max = $this->grouptool->allow_multiple ? $this->grouptool->choose_max : 1;
     $min = $this->grouptool->allow_multiple ? $this->grouptool->choose_min : 0;
     if ($userregs + $userqueues <= $min) {
         if ($userid == $USER->id) {
             return array(true, get_string('you_have_too_less_regs', 'grouptool', $message));
         } else {
             return array(true, get_string('user_has_too_less_regs', 'grouptool', $message));
         }
     }
     if ($groupdata) {
         if ($this->get_rank_in_queue($groupdata->registered, $userid) != false) {
             // He is registered!
             if ($previewonly) {
                 if ($userid == $USER->id) {
                     return array(false, get_string('unreg_you_from_group', 'grouptool', $message));
                 } else {
                     return array(false, get_string('unreg_from_group', 'grouptool', $message));
                 }
             } else {
                 $records = $DB->get_records('grouptool_registered', array('agrpid' => $agrpid, 'userid' => $userid));
                 $DB->delete_records('grouptool_registered', array('agrpid' => $agrpid, 'userid' => $userid));
                 if (!empty($this->grouptool->immediate_reg)) {
                     groups_remove_member($groupdata->id, $userid);
                 }
                 foreach ($records as $data) {
                     // Trigger the event!
                     $data->groupid = $groupdata->id;
                     \mod_grouptool\event\registration_deleted::create_direct($this->cm, $data)->trigger();
                 }
                 // Get next queued user and put him in the group (and delete queue entry)!
                 if (!empty($this->grouptool->use_queue) && !empty($groupdata->queued)) {
                     $sql = "SELECT queued.*, agrp.groupid\n                                  FROM {grouptool_queued} queued\n                                  JOIN {grouptool_agrps} agrp ON agrp.id = queued.agrpid\n                                 WHERE agrpid = ?\n                              ORDER BY timestamp ASC\n                                 LIMIT 1";
                     $record = $DB->get_record_sql($sql, array($agrpid));
                     $newrecord = clone $record;
                     unset($newrecord->id);
                     $newrecord->modified_by = $newrecord->userid;
                     $newrecord->id = $DB->insert_record('grouptool_registered', $newrecord);
                     if (!empty($this->grouptool->immediate_reg)) {
                         groups_add_member($groupdata->id, $newrecord->userid);
                     }
                     $allowm = $this->grouptool->allow_multiple;
                     $usrregcnt = $this->get_user_reg_count(0, $newrecord->userid);
                     $max = $this->grouptool->choose_max;
                     if ($allowm && $usrregcnt >= $max || !$allowm) {
                         $agrps = $this->get_active_groups(false, false, 0, 0, 0, false);
                         $agrpids = array_keys($agrps);
                         list($sql, $params) = $DB->get_in_or_equal($agrpids);
                         $records = $DB->get_records_sql("SELECT queued.*, agrp.groupid\n                                                               FROM {grouptool_queued} queued\n                                                               JOIN {grouptool_agrps} agrp ON queued.agrpid = agrp.id\n                                                              WHERE userid = ? AND agrpid " . $sql, array_merge(array($newrecord->userid), $params));
                         $DB->delete_records_select('grouptool_queued', ' userid = ? AND agrpid ' . $sql, array_merge(array($newrecord->userid), $params));
                         foreach ($records as $cur) {
                             // Trigger the event!
                             \mod_grouptool\event\queue_entry_deleted::create_limit_violation($this->cm, $cur)->trigger();
                         }
                     }
                     $strgrouptools = get_string("modulenameplural", "grouptool");
                     $strgrouptool = get_string("modulename", "grouptool");
                     $postsubject = $this->course->shortname . ': ' . $strgrouptools . ': ' . format_string($this->grouptool->name, true);
                     $posttext = $this->course->shortname . ' -> ' . $strgrouptools . ' -> ' . format_string($this->grouptool->name, true) . "\n";
                     $posttext .= "----------------------------------------------------------\n";
                     $posttext .= get_string("register_you_in_group_successmail", "grouptool", $message) . "\n";
                     $posttext .= "----------------------------------------------------------\n";
                     $usermailformat = $DB->get_field('user', 'mailformat', array('id' => $newrecord->userid));
                     if ($usermailformat == 1) {
                         // HTML!
                         $posthtml = "<p><font face=\"sans-serif\">";
                         $posthtml = "<a href=\"" . $CFG->wwwroot . "/course/view.php?id=" . $this->course->id . "\">" . $this->course->shortname . "</a> ->";
                         $posthtml = "<a href=\"" . $CFG->wwwroot . "/mod/grouptool/index.php?id=" . $this->course->id . "\">" . $strgrouptools . "</a> ->";
                         $posthtml = "<a href=\"" . $CFG->wwwroot . "/mod/grouptool/view.php?id=" . $this->cm->id . "\">" . format_string($this->grouptool->name, true) . "</a></font></p>";
                         $posthtml .= "<hr /><font face=\"sans-serif\">";
                         $posthtml .= "<p>" . get_string("register_you_in_group_successmailhtml", "grouptool", $message) . "</p>";
                         $posthtml .= "</font><hr />";
                     } else {
                         $posthtml = "";
                     }
                     $messageuser = $DB->get_record('user', array('id' => $newrecord->userid));
                     $eventdata = new stdClass();
                     $eventdata->modulename = 'grouptool';
                     $userfrom = core_user::get_noreply_user();
                     $eventdata->userfrom = $userfrom;
                     $eventdata->userto = $messageuser;
                     $eventdata->subject = $postsubject;
                     $eventdata->fullmessage = $posttext;
                     $eventdata->fullmessageformat = FORMAT_PLAIN;
                     $eventdata->fullmessagehtml = $posthtml;
                     $eventdata->smallmessage = get_string('register_you_in_group_success', 'grouptool', $message);
                     $eventdata->name = 'grouptool_moveupreg';
                     $eventdata->component = 'mod_grouptool';
                     $eventdata->notification = 1;
                     $eventdata->contexturl = $CFG->wwwroot . '/mod/grouptool/view.php?id=' . $this->cm->id;
                     $eventdata->contexturlname = $this->grouptool->name;
                     message_send($eventdata);
                     $DB->delete_records('grouptool_queued', array('id' => $record->id));
                     // Trigger the event!
                     // We fetched groupid above in SQL.
                     \mod_grouptool\event\user_moved::promotion_from_queue($this->cm, $record, $newrecord)->trigger();
                 }
                 if ($userid == $USER->id) {
                     return array(false, get_string('unreg_you_from_group_success', 'grouptool', $message));
                 } else {
                     return array(false, get_string('unreg_from_group_success', 'grouptool', $message));
                 }
             }
         } else {
             if ($this->get_rank_in_queue($groupdata->queued, $userid) != false) {
                 // He is queued!
                 if ($previewonly) {
                     if ($userid == $USER->id) {
                         return array(false, get_string('unqueue_you_from_group', 'grouptool', $message));
                     } else {
                         return array(false, get_string('unqueue_from_group', 'grouptool', $message));
                     }
                 } else {
                     $records = $DB->get_records('grouptool_queued', array('agrpid' => $agrpid, 'userid' => $userid));
                     $DB->delete_records('grouptool_queued', array('agrpid' => $agrpid, 'userid' => $userid));
                     foreach ($records as $cur) {
                         // Trigger the Event!
                         $cur->groupid = $groupdata->id;
                         \mod_grouptool\event\queue_entry_deleted::create_direct($this->cm, $cur)->trigger();
                     }
                     if ($userid == $USER->id) {
                         return array(false, get_string('unqueue_you_from_group_success', 'grouptool', $message));
                     } else {
                         return array(false, get_string('unqueue_from_group_success', 'grouptool', $message));
                     }
                 }
             } else {
                 if ($userid == $USER->id) {
                     return array(true, get_string('you_are_not_in_queue_or_registered', 'grouptool', $message));
                 } else {
                     return array(true, get_string('not_in_queue_or_registered', 'grouptool', $message));
                 }
             }
         }
     } else {
         return array(true, get_string('error_getting_data', 'grouptool'));
     }
 }
예제 #2
0
 /**
  * group_remove_member_handler
  * event:       groups_member_removed
  * schedule:    instant
  *
  * @param \core\event\group_member_removed $event Event object containing useful data
  * @return bool true if success
  */
 public static function group_member_removed(\core\event\group_member_removed $event)
 {
     global $DB, $CFG;
     $sql = "SELECT DISTINCT {grouptool}.id, {grouptool}.ifmemberremoved, {grouptool}.course,\n                                {grouptool}.use_queue, {grouptool}.immediate_reg, {grouptool}.allow_multiple,\n                                {grouptool}.choose_max, {grouptool}.name\n                           FROM {grouptool}\n                     RIGHT JOIN {grouptool_agrps} agrp ON agrp.grouptoolid = {grouptool}.id\n                          WHERE agrp.groupid = ?";
     $params = array($event->objectid);
     if (!($grouptools = $DB->get_records_sql($sql, $params))) {
         return true;
     }
     $sql = "SELECT agrps.grouptoolid grouptoolid, agrps.id id\n                  FROM {grouptool_agrps} agrps\n                 WHERE agrps.groupid = :groupid";
     $agrp = $DB->get_records_sql($sql, array('groupid' => $event->objectid));
     foreach ($grouptools as $grouptool) {
         switch ($grouptool->ifmemberremoved) {
             case GROUPTOOL_FOLLOW:
                 $sql = "SELECT reg.id AS id, reg.agrpid AS agrpid, reg.userid AS userid, agrps.groupid\n                              FROM {grouptool_agrps} agrps\n                        INNER JOIN {grouptool_registered} reg ON agrps.id = reg.agrpid\n                             WHERE reg.userid = :userid\n                                   AND agrps.grouptoolid = :grouptoolid\n                                   AND agrps.groupid = :groupid";
                 if ($regs = $DB->get_records_sql($sql, array('grouptoolid' => $grouptool->id, 'userid' => $event->relateduserid, 'groupid' => $event->objectid))) {
                     $DB->delete_records_list('grouptool_registered', 'id', array_keys($regs));
                     foreach ($regs as $reg) {
                         // Trigger event!
                         $cm = get_coursemodule_from_instance('grouptool', $grouptool->id, $grouptool->course, false, MUST_EXIST);
                         \mod_grouptool\event\registration_deleted::create_via_eventhandler($cm, $reg)->trigger();
                     }
                     // Get next queued user and put him in the group (and delete queue entry)!
                     if (!empty($grouptool->use_queue)) {
                         $agrpids = $DB->get_fieldset_sql('SELECT id
                                                             FROM {grouptool_agrps}
                                                            WHERE grouptoolid = ?', array($grouptool->id));
                         list($agrpssql, $agrpsparam) = $DB->get_in_or_equal($agrpids);
                         $sql = "SELECT queued.id, MAX(queued.agrpid) AS agrpid, MAX(queued.userid) AS userid,\n                                                      MAX(queued.timestamp), (COUNT(DISTINCT reg.id) < ?) AS priority\n                                      FROM {grouptool_queued} queued\n                                 LEFT JOIN {grouptool_registered} reg ON queued.userid = reg.userid\n                                                                         AND reg.agrpid " . $agrpssql . "\n                                     WHERE queued.agrpid = ?\n                                  GROUP BY queued.id\n                                  ORDER BY priority DESC, queued.timestamp ASC\n                                     LIMIT 1";
                         $params = array_merge(array($grouptool->choose_max), $agrpsparam, array($agrp[$grouptool->id]->id));
                         $record = $DB->get_record_sql($sql, $params);
                         if (is_object($record)) {
                             $newrecord = clone $record;
                             unset($newrecord->id);
                             $newrecord->modified_by = $newrecord->userid;
                             $newrecord->id = $DB->insert_record('grouptool_registered', $newrecord);
                             if (!empty($grouptool->immediate_reg)) {
                                 groups_add_member($event->objectid, $newrecord->userid);
                             }
                             // Trigger event!
                             // We got the cm above already!
                             $newrecord->groupid = $event->objectid;
                             $record->groupid = $event->objectid;
                             \mod_grouptool\event\user_moved::promotion_from_queue($cm, $record, $newrecord)->trigger();
                             $allowm = $grouptool->allow_multiple;
                             $agrps = $DB->get_fieldset_sql("SELECT id\n                                                                FROM {grouptool_agrps} agrps\n                                                                WHERE agrps.grouptoolid = :grptlid", array('grptlid' => $grouptool->id));
                             list($sql, $params) = $DB->get_in_or_equal($agrps);
                             $usrregcnt = $DB->count_records_select('grouptool_registered', ' userid = ?
                                                                     AND agrpid ' . $sql, array_merge(array($newrecord->userid), $params));
                             $max = $grouptool->choose_max;
                             // Get belonging course!
                             $course = $DB->get_record('course', array('id' => $grouptool->course));
                             // Get CM!
                             $cm = get_coursemodule_from_instance('grouptool', $grouptool->id, $course->id);
                             $message = new stdClass();
                             $userdata = $DB->get_record('user', array('id' => $newrecord->userid));
                             $message->username = fullname($userdata);
                             $groupdata = $DB->get_record('grouptool_agrps', array('id' => $agrp[$grouptool->id]->id));
                             $groupdata->name = $DB->get_field('groups', 'name', array('id' => $groupdata->groupid));
                             $message->groupname = $groupdata->name;
                             $strgrouptools = get_string("modulenameplural", "grouptool");
                             $strgrouptool = get_string("modulename", "grouptool");
                             $postsubject = $course->shortname . ': ' . $strgrouptools . ': ' . format_string($grouptool->name, true);
                             $posttext = $course->shortname . ' -> ' . $strgrouptools . ' -> ' . format_string($grouptool->name, true) . "\n";
                             $posttext .= "----------------------------------------------------------\n";
                             $posttext .= get_string("register_you_in_group_successmail", "grouptool", $message) . "\n";
                             $posttext .= "----------------------------------------------------------\n";
                             $usermailformat = $DB->get_field('user', 'mailformat', array('id' => $newrecord->userid));
                             if ($usermailformat == 1) {
                                 // HTML!
                                 $posthtml = "<p><font face=\"sans-serif\">";
                                 $posthtml = "<a href=\"" . $CFG->wwwroot . "/course/view.php?id=" . $course->id . "\">" . $course->shortname . "</a> ->";
                                 $posthtml = "<a href=\"" . $CFG->wwwroot . "/mod/grouptool/index.php?id=" . $course->id . "\">" . $strgrouptools . "</a> ->";
                                 $posthtml = "<a href=\"" . $CFG->wwwroot . "/mod/grouptool/view.php?id=" . $cm->id . "\">" . format_string($grouptool->name, true) . "</a></font></p>";
                                 $posthtml .= "<hr /><font face=\"sans-serif\">";
                                 $posthtml .= "<p>" . get_string("register_you_in_group_successmailhtml", "grouptool", $message) . "</p>";
                                 $posthtml .= "</font><hr />";
                             } else {
                                 $posthtml = "";
                             }
                             $messageuser = $DB->get_record('user', array('id' => $newrecord->userid));
                             $eventdata = new stdClass();
                             $eventdata->modulename = 'grouptool';
                             $userfrom = core_user::get_noreply_user();
                             $eventdata->userfrom = $userfrom;
                             $eventdata->userto = $messageuser;
                             $eventdata->subject = $postsubject;
                             $eventdata->fullmessage = $posttext;
                             $eventdata->fullmessageformat = FORMAT_PLAIN;
                             $eventdata->fullmessagehtml = $posthtml;
                             $eventdata->smallmessage = get_string('register_you_in_group_success', 'grouptool', $message);
                             $eventdata->name = 'grouptool_moveupreg';
                             $eventdata->component = 'mod_grouptool';
                             $eventdata->notification = 1;
                             $eventdata->contexturl = $CFG->wwwroot . '/mod/grouptool/view.php?id=' . $cm->id;
                             $eventdata->contexturlname = $grouptool->name;
                             message_send($eventdata);
                             if ($allowm && $usrregcnt >= $max || !$allowm) {
                                 // Get all queue entries and trigger queue_entry_deleted events for each!
                                 $queueentries = $DB->get_records_sql("SELECT queued.*, agrp.groupid\n                                                                             FROM {grouptool_queued} queued\n                                                                             JOIN {grouptool_agrps} agrp ON queued.agrpid = agrp.id\n                                                                            WHERE userid = ? AND agrpid " . $sql, array_merge(array($newrecord->userid), $params));
                                 $DB->delete_records_select('grouptool_queued', ' userid = ? AND agrpid ' . $sql, array_merge(array($newrecord->userid), $params));
                                 foreach ($queueentries as $cur) {
                                     // Trigger event!
                                     // We got the cm above already!
                                     \mod_grouptool\event\queue_entry_deleted::create_via_eventhandler($cm, $cur)->trigger();
                                 }
                             } else {
                                 $queueentries = $DB->get_records_sql("SELECT queued.*, agrp.groupid\n                                                                             FROM {grouptool_queued} queued\n                                                                             JOIN {grouptool_agrps} agrp ON queued.agrpid = agrp.id\n                                                                            WHERE userid = :userid AND agrpid = :agrpid", array('userid' => $newrecord->userid, 'agrpid' => $agrp[$grouptool->id]->id));
                                 $DB->delete_records('grouptool_queued', array('userid' => $newrecord->userid, 'agrpid' => $agrp[$grouptool->id]->id));
                                 foreach ($queueentries as $cur) {
                                     // Trigger event!
                                     // We got the cm above already!
                                     \mod_grouptool\event\queue_entry_deleted::create_via_eventhandler($cm, $cur)->trigger();
                                 }
                             }
                         }
                     }
                 }
                 break;
             default:
             case GROUPTOOL_IGNORE:
                 break;
         }
     }
     return true;
 }