These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | // This file is part of Moodle - http://moodle.org/ |
||
| 3 | // |
||
| 4 | // Moodle is free software: you can redistribute it and/or modify |
||
| 5 | // it under the terms of the GNU General Public License as published by |
||
| 6 | // the Free Software Foundation, either version 3 of the License, or |
||
| 7 | // (at your option) any later version. |
||
| 8 | // |
||
| 9 | // Moodle is distributed in the hope that it will be useful, |
||
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 12 | // GNU General Public License for more details. |
||
| 13 | // |
||
| 14 | // You should have received a copy of the GNU General Public License |
||
| 15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Relationship's Autogroup page |
||
| 19 | * |
||
| 20 | * @package local_relationship |
||
| 21 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||
| 22 | */ |
||
| 23 | |||
| 24 | require_once(__DIR__.'/../../config.php'); |
||
| 25 | require($CFG->dirroot.'/local/relationship/lib.php'); |
||
| 26 | require_once($CFG->dirroot.'/local/relationship/locallib.php'); |
||
| 27 | require_once($CFG->dirroot.'/group/lib.php'); |
||
| 28 | |||
| 29 | require_login(); |
||
| 30 | |||
| 31 | $relationshipid = required_param('relationshipid', PARAM_INT); |
||
| 32 | $relationship = $DB->get_record('relationship', array('id' => $relationshipid), '*', MUST_EXIST); |
||
| 33 | $context = context::instance_by_id($relationship->contextid, MUST_EXIST); |
||
| 34 | |||
| 35 | require_capability('local/relationship:manage', $context); |
||
| 36 | if (!empty($relationship->component)) { |
||
| 37 | print_error('cantedit', 'local_relationship'); |
||
| 38 | } |
||
| 39 | |||
| 40 | $baseurl = new moodle_url('/local/relationship/autogroup.php', array('relationshipid' => $relationship->id)); |
||
| 41 | $returnurl = new moodle_url('/local/relationship/groups.php', array('relationshipid' => $relationship->id)); |
||
| 42 | |||
| 43 | relationship_set_header($context, $baseurl, $relationship, 'groups'); |
||
| 44 | |||
| 45 | // Print the page and form |
||
| 46 | $preview = ''; |
||
| 47 | $error = ''; |
||
| 48 | |||
| 49 | /// Create the form |
||
| 50 | $editform = new \local_relationship\form\autogroup(null, array('relationshipid' => $relationshipid)); |
||
| 51 | $editform->set_data(array('relationshipid' => $relationshipid)); |
||
| 52 | |||
| 53 | /// Handle form submission |
||
| 54 | if ($editform->is_cancelled()) { |
||
| 55 | redirect($returnurl); |
||
| 56 | } elseif ($data = $editform->get_data()) { |
||
| 57 | $numgrps = $data->number; |
||
| 58 | $userlimit = $data->userlimit; |
||
| 59 | $relationshipcohortid = $data->relationshipcohortid; |
||
| 60 | |||
| 61 | $existing_groups = $DB->get_records_menu('relationship_groups', array('relationshipid' => $relationshipid), null, 'name, id'); |
||
|
0 ignored issues
–
show
|
|||
| 62 | $course_groups = relationship_get_other_courses_group_names($relationshipid); |
||
| 63 | |||
| 64 | // Plan the allocation |
||
| 65 | $new_groups = array(); |
||
| 66 | if ($relationshipcohortid) { |
||
| 67 | $sql = "SELECT cm.userid, u.firstname, u.lastname |
||
| 68 | FROM {relationship_cohorts} rc |
||
| 69 | JOIN {cohort_members} cm ON (cm.cohortid = rc.cohortid) |
||
| 70 | JOIN {user} u ON (u.id = cm.userid) |
||
| 71 | WHERE rc.id = :relationshipcohortid |
||
| 72 | ORDER BY u.firstname"; |
||
| 73 | $members = $DB->get_records_sql($sql, array('relationshipcohortid' => $relationshipcohortid)); |
||
| 74 | $i = 0; |
||
| 75 | foreach ($members as $m) { |
||
| 76 | $new_groups[$i]['name'] = relationship_groups_parse_name(trim($data->namingscheme), "{$m->firstname} {$m->lastname}", true); |
||
|
0 ignored issues
–
show
|
|||
| 77 | $new_groups[$i]['userid'] = $m->userid; |
||
| 78 | $new_groups[$i]['exists'] = isset($existing_groups[$new_groups[$i]['name']]); |
||
| 79 | $new_groups[$i]['exists_external'] = isset($course_groups[$new_groups[$i]['name']]); |
||
| 80 | $i++; |
||
| 81 | } |
||
| 82 | } else { |
||
| 83 | // allocate the users - all groups equal count first |
||
| 84 | $count = $numgrps; |
||
| 85 | $i = 0; |
||
| 86 | while ($count > 0) { |
||
| 87 | $new_groups[$i]['name'] = relationship_groups_parse_name(trim($data->namingscheme), $i); |
||
| 88 | $new_groups[$i]['userid'] = 0; |
||
| 89 | $new_groups[$i]['exists'] = isset($existing_groups[$new_groups[$i]['name']]); |
||
| 90 | $new_groups[$i]['exists_external'] = isset($course_groups[$new_groups[$i]['name']]); |
||
| 91 | if (!$new_groups[$i]['exists'] && !$new_groups[$i]['exists_external']) { |
||
| 92 | $count--; |
||
| 93 | } |
||
| 94 | $i++; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | if (isset($data->preview)) { |
||
| 99 | $table = new html_table(); |
||
| 100 | $table->size = array('70%'); |
||
| 101 | $table->align = array('left'); |
||
| 102 | $table->width = '40%'; |
||
| 103 | |||
| 104 | $table->data = array(); |
||
| 105 | foreach ($new_groups as $group) { |
||
| 106 | if ($group['exists']) { |
||
| 107 | $text = html_writer::tag('span', $group['name'], array('style' => 'color:red; font-weight: bold;')).get_string('alreadyexists', 'local_relationship'); |
||
|
0 ignored issues
–
show
|
|||
| 108 | } else if ($group['exists_external']) { |
||
| 109 | $text = html_writer::tag('span', $group['name'], array('style' => 'color:red; font-weight: bold;')).get_string('alreadyexistsexternal', 'local_relationship'); |
||
|
0 ignored issues
–
show
|
|||
| 110 | } else { |
||
| 111 | $text = $group['name']; |
||
| 112 | } |
||
| 113 | $table->data[] = array($text); |
||
| 114 | } |
||
| 115 | $preview .= html_writer::table($table); |
||
| 116 | } else { |
||
| 117 | foreach ($new_groups as $key => $group) { |
||
| 118 | if (!$group['exists']) { |
||
| 119 | $newgroup = new stdClass(); |
||
| 120 | $newgroup->relationshipid = $relationshipid; |
||
| 121 | $newgroup->name = $group['name']; |
||
| 122 | $newgroup->uniformdistribution = 0; |
||
| 123 | $newgroup->userlimit = $userlimit; |
||
| 124 | $id = relationship_add_group($newgroup); |
||
| 125 | |||
| 126 | if ($group['userid']) { |
||
| 127 | relationship_add_member($id, $relationshipcohortid, $group['userid']); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | redirect($returnurl); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | relationship_set_title($relationship, 'autogroup'); |
||
| 137 | |||
| 138 | if ($error != '') { |
||
| 139 | echo $OUTPUT->notification($error); |
||
| 140 | } |
||
| 141 | |||
| 142 | /// Display the form |
||
| 143 | $editform->display(); |
||
| 144 | |||
| 145 | if ($preview !== '') { |
||
| 146 | echo $OUTPUT->heading(get_string('groupspreview', 'group')); |
||
| 147 | echo $preview; |
||
| 148 | } |
||
| 149 | |||
| 150 | echo $OUTPUT->footer(); |
||
| 151 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.