/** * import users into a certain moodle-group and enrole them if not allready enroled * * @param int[] $groups array of ids of groups to import into * @param stdClass $data from form in import tab (textfield with idnumbers and group-selection) * @param bool $forceregistration Force registration in grouptool * @param bool $includedeleted include deleted users in import (can cause problems if they're not enrolled) * @param bool $previewonly optional preview only, don't take any action * @return array ($error, $message) */ public function import($groups, $data, $forceregistration = false, $includedeleted = 0, $previewonly = false) { global $DB, $OUTPUT, $CFG, $PAGE, $USER; $message = ""; $error = false; $users = preg_split("/[ ,;\t\n\r]+/", $data); // Prevent selection of all users if one of the above defined characters are in the beginning! foreach ($users as $key => $user) { if (empty($user)) { unset($users[$key]); } } $groupinfo = array(); foreach ($groups as $group) { $groupinfo[$group] = groups_get_group($group); } $imported = array(); $columns = $DB->get_columns('user'); if (empty($field) || !key_exists($field, $columns)) { $field = 'idnumber'; } $agrp = array(); foreach ($groups as $group) { $agrp[$group] = $DB->get_field('grouptool_agrps', 'id', array('grouptoolid' => $this->grouptool->id, 'groupid' => $group), IGNORE_MISSING); if (!$DB->record_exists('grouptool_agrps', array('grouptoolid' => $this->grouptool->id, 'groupid' => $group, 'active' => 1))) { $message .= $OUTPUT->notification(get_string('import_in_inactive_group_warning', 'grouptool', $groupinfo[$group]->name), array('notifyproblem')); } // We use MAX to trick Postgres into thinking this is a full GROUPU BY statement! $sql = ' SELECT agrps.id AS id, MAX(agrps.groupid) AS grpid, COUNT(regs.id) AS regs, MAX(grptl.use_individual) AS indi, MAX(grptl.grpsize) AS globalsize, MAX(agrps.grpsize) AS size, MAX(grptl.name) AS instancename FROM {grouptool_agrps} agrps JOIN {grouptool} grptl ON agrps.grouptoolid = grptl.id LEFT JOIN {grouptool_registered} regs ON agrps.id = regs.agrpid AND regs.modified_by >= 0 WHERE agrps.groupid = :grpid AND grptl.use_size = 1 AND agrps.active = 1 GROUP BY agrps.id '; $agrps = $DB->get_records_sql($sql, array('grpid' => $group)); $usercnt = count($users); foreach ($agrps as $cur) { if ($cur->indi && !empty($cur->size)) { if ($cur->regs + $usercnt > $cur->size) { $message .= html_writer::tag('div', $OUTPUT->notification(get_string('overflowwarning', 'grouptool', $cur), 'notifytiny')); } } else { if ($cur->regs + $usercnt > $cur->globalsize) { $message .= html_writer::tag('div', $OUTPUT->notification(get_string('overflowwarning', 'grouptool', $cur), 'notifytiny')); } } } } if (false !== ($gtimportfields = get_config('mod_grouptool', 'importfields'))) { $importfields = explode(',', $gtimportfields); } else { $importfields = array('username', 'idnumber'); } $prevtable = new html_table(); $prevtable->attributes['class'] = 'importpreview table table-striped table-hover'; $prevtable->id = 'importpreview'; $prevtable->head = array(get_string('fullname')); foreach ($importfields as $field) { $prevtable->head[] = get_string($field); } $prevtable->head[] = get_string('status'); $prevtable->data = array(); $pbar = new progress_bar('checkmarkimportprogress', 500, true); $count = count($users); $processed = 0; $pbar->update($processed, $count, get_string('import_progress_start', 'grouptool')); core_php_time_limit::raise(count($users) * 5); raise_memory_limit(MEMORY_HUGE); foreach ($users as $user) { $pbar->update($processed, $count, get_string('import_progress_search', 'grouptool') . ' ' . $user); foreach ($importfields as $field) { $sql = 'SELECT * FROM {user} WHERE ' . $DB->sql_like($field, ':userpattern'); if (empty($includedeleted)) { $sql .= ' AND deleted = :deleted'; $param = array('userpattern' => $user, 'deleted' => 0); } else { $param = array('userpattern' => $user); } $userinfo = $DB->get_records_sql($sql, $param); if (empty($userinfo)) { $param['userpattern'] = '%' . $user; $userinfo = $DB->get_records_sql($sql, $param); } else { if (count($userinfo) == 1) { break; } } if (empty($userinfo)) { $param['userpattern'] = $user . '%'; $userinfo = $DB->get_records_sql($sql, $param); } else { if (count($userinfo) == 1) { break; } } if (empty($userinfo)) { $param['userpattern'] = '%' . $user . '%'; $userinfo = $DB->get_records_sql($sql, $param); } else { if (count($userinfo) == 1) { break; } } if (!empty($userinfo) && count($userinfo) == 1) { break; } } $row = new html_table_row(); if (empty($userinfo)) { $row->cells[] = new html_table_cell($OUTPUT->notification(get_string('user_not_found', 'grouptool', $user), 'notifyproblem')); $row->cells[0]->colspan = count($prevtable->head); $error = true; } else { if (count($userinfo) > 1) { $tmprows = array(); foreach ($userinfo as $currentuser) { $tmprow = new html_table_row(); $tmprow->cells = array(); $tmprow->cells[] = new html_table_cell(fullname($currentuser)); foreach ($importfields as $curfield) { $tmprow->cells[] = new html_table_cell($currentuser->{$curfield}); } $tmprows[] = $tmprow; } $curkey = count($tmprows[0]->cells); $tmprows[0]->cells[$curkey] = new html_table_cell($OUTPUT->notification(get_string('found_multiple', 'grouptool'), 'notifyproblem')); $tmprows[0]->cells[$curkey]->rowspan = count($tmprows); foreach ($tmprows as $tmprow) { $prevtable->data[] = $tmprow; } $error = true; // We've added multiple rows manualle and can continue with the next user! continue; } else { $userinfo = reset($userinfo); $row->cells = array(new html_table_cell(fullname($userinfo))); foreach ($importfields as $curfield) { $row->cells[] = new html_table_cell(empty($userinfo->{$curfield}) ? '' : $userinfo->{$curfield}); } if (!is_enrolled($this->context, $userinfo->id)) { // We have to catch deleted users now, give a message and continue! if (!empty($userinfo->deleted)) { $userinfo->fullname = fullname($userinfo); $text = get_string('user_is_deleted', 'grouptool', $userinfo); $row->cells[] = new html_table_cell($OUTPUT->notification($text, 'notifyproblem')); $error = true; continue; } /* * if user's not enrolled already we force manual enrollment in course, * so we can add the user to the group */ require_once $CFG->dirroot . '/enrol/manual/locallib.php'; require_once $CFG->libdir . '/accesslib.php'; if (!($enrolmanual = enrol_get_plugin('manual'))) { throw new coding_exception('Can not instantiate enrol_manual'); } if (!($instance = $DB->get_record('enrol', array('courseid' => $this->course->id, 'enrol' => 'manual'), '*', IGNORE_MISSING))) { if ($instanceid = $enrolmanual->add_default_instance($this->course)) { $instance = $DB->get_record('enrol', array('courseid' => $this->course->id, 'enrol' => 'manual'), '*', MUST_EXIST); } } if ($instance != false) { $archroles = get_archetype_roles('student'); $archrole = array_shift($archroles); $enrolmanual->enrol_user($instance, $userinfo->id, $archrole->id, time()); } else { $row->cells[] = new html_table_cell($OUTPUT->notification(get_string('cant_enrol', 'grouptool'), 'notifyproblem')); } } foreach ($groups as $group) { $data = array('id' => $userinfo->id, 'idnumber' => $userinfo->idnumber, 'fullname' => fullname($userinfo), 'groupname' => $groupinfo[$group]->name); if (!$previewonly && $userinfo) { $attr = array('class' => 'notifysuccess'); $pbar->update($processed, $count, get_string('import_progress_import', 'grouptool') . ' ' . fullname($userinfo) . '...'); if (!groups_add_member($group, $userinfo->id)) { $error = true; $notification = $OUTPUT->notification(get_string('import_user_problem', 'grouptool', $data), 'notifyproblem'); $row->cells[] = new html_table_cell($notification); $row->attributes['class'] = 'error'; } else { $imported[] = $userinfo->id; $row->cells[] = get_string('import_user', 'grouptool', $data); $row->attributes['class'] = 'notifysuccess'; } if ($forceregistration && empty($agrp[$group])) { /* Registering in an non active Grouptool-group would cause problems * with incorrectly labeled buttons under certain circumstances. * We removed the automatic creation and registration in this newly inserted inactive group. * In no case, there should be a missing agrp entry anyway. */ $newgrpdata = $DB->get_record_sql('SELECT MAX(sort_order), MAX(grpsize) FROM {grouptool_agrps} WHERE grouptoolid = ?', array($this->grouptool->id)); // Insert agrp-entry for this group (even if it's not active)! var_dump($group); $agrp[$group] = new stdClass(); $agrp[$group]->grouptoolid = $this->grouptool->id; $agrp[$group]->groupid = $group; $agrp[$group]->active = 0; $agrp[$group]->sort_order = $newgrpdata->sortorder + 1; $agrp[$group]->grpsize = $newgrpdata->grpsize; $agrp[$group]->id = $DB->insert_record('grouptool_agrps', $agrp[$group]); \mod_grouptool\event\agrp_created::create_from_object($this->cm, $agrp[$group])->trigger(); $notification = $OUTPUT->notification(get_string('import_in_inactive_group_rejected', 'grouptool', $agrp[$group]), 'notifyproblem'); $row->cells[] = $notification; $row->attributes['class'] = 'error'; $agrp[$group] = $agrp[$group]->id; } else { if ($forceregistration && !empty($agrp[$group]) && !$DB->record_exists_select('grouptool_registered', "modified_by >= 0 AND agrpid = :agrpid AND userid = :userid", array('agrpid' => $agrp[$group], 'userid' => $userinfo->id))) { if ($reg = $DB->get_record('grouptool_registered', array('agrpid' => $agrp[$group], 'userid' => $userinfo->id, 'modified_by' => -1), IGNORE_MISSING)) { // If user is marked, we register him right now! $reg->modified_by = $USER->id; $DB->update_record('grouptool_registered', $reg); // TODO do we have to delete his marks and queues if theres enough registrations? } else { $reg = new stdClass(); $reg->agrpid = $agrp[$group]; $reg->userid = $userinfo->id; $reg->timestamp = time(); $reg->modified_by = $USER->id; // We don't need to log creation of registration, because we log import as whole! $reg->id = $DB->insert_record('grouptool_registered', $reg); } \mod_grouptool\event\user_imported::import_forced($this->cm, $reg->id, $agrp[$group], $group, $userinfo->id)->trigger(); } else { if (!$forceregistration) { // Trigger the event! \mod_grouptool\event\user_imported::import($this->cm, $group, $userinfo->id)->trigger(); } } } } else { if ($userinfo) { $attr = array('class' => 'prevsuccess'); $row->cells[] = get_string('import_user_prev', 'grouptool', $data); $row->attributes['class'] = 'prevsuccess'; } } } } } $prevtable->data[] = $row; unset($row); $processed++; } $processed++; if (!$previewonly) { $pbar->update($processed, $count, get_string('import_progress_completed', 'grouptool')); } else { $pbar->update($processed, $count, get_string('import_progress_preview_completed', 'grouptool')); } $message .= html_writer::table($prevtable); return array($error, $message); }
/** * group_created * * @param \core\event\group_created $event Event object containing useful data * @return bool true if success */ public static function group_created(\core\event\group_created $event) { global $DB; $data = $event->get_record_snapshot('groups', $event->objectid); $course = $DB->get_record('course', array('id' => $data->courseid)); if (!($grouptools = get_all_instances_in_course('grouptool', $course))) { return true; } $sortorder = $DB->get_records_sql("SELECT agrp.grouptoolid, MAX(agrp.sort_order) AS max\n FROM {grouptool_agrps} agrp\n GROUP BY agrp.grouptoolid"); foreach ($grouptools as $grouptool) { $newagrp = new StdClass(); $newagrp->grouptoolid = $grouptool->id; $newagrp->groupid = $data->id; if (!array_key_exists($grouptool->id, $sortorder)) { $newagrp->sort_order = 1; } else { $newagrp->sort_order = $sortorder[$grouptool->id]->max + 1; } $newagrp->active = 0; if (!$DB->record_exists('grouptool_agrps', array('grouptoolid' => $grouptool->id, 'groupid' => $data->id))) { $newagrp->id = $DB->insert_record('grouptool_agrps', $newagrp); // Trigger event! $cm = get_coursemodule_from_instance('grouptool', $grouptool->id); \mod_grouptool\event\agrp_created::create_from_object($cm, $newagrp)->trigger(); } } return true; }