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 | * Internal functions used to help manage the Relationship CRUD interface |
||
| 19 | * |
||
| 20 | * @package local_relationship |
||
| 21 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||
| 22 | */ |
||
| 23 | |||
| 24 | defined('MOODLE_INTERNAL') || die(); |
||
| 25 | |||
| 26 | require_once($CFG->dirroot . '/user/selector/lib.php'); |
||
| 27 | require_once($CFG->dirroot . '/tag/lib.php'); |
||
| 28 | |||
| 29 | function relationship_set_header($context, $url, $relationship=null, $module=null) { |
||
| 30 | global $PAGE, $COURSE, $DB; |
||
| 31 | |||
| 32 | if ($context->contextlevel != CONTEXT_COURSECAT) { |
||
| 33 | print_error('invalidcontext'); |
||
| 34 | } |
||
| 35 | $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST); |
||
| 36 | $navtitle = get_string('relationships', 'local_relationship'); |
||
| 37 | |||
| 38 | $PAGE->set_pagelayout('standard'); |
||
| 39 | $PAGE->set_context($context); |
||
| 40 | $PAGE->set_url($url); |
||
| 41 | $PAGE->set_heading($COURSE->fullname); |
||
| 42 | $PAGE->set_title($navtitle); |
||
| 43 | |||
| 44 | $PAGE->navbar->add($category->name, new moodle_url('/course/index.php', array('categoryid'=>$category->id))); |
||
| 45 | $PAGE->navbar->add($navtitle, new moodle_url('/local/relationship/index.php', array('contextid'=>$context->id))); |
||
| 46 | if($module) { |
||
| 47 | $PAGE->navbar->add(get_string($module, 'local_relationship'), |
||
| 48 | new moodle_url("/local/relationship/{$module}.php", array('relationshipid'=>$relationship->id))); |
||
|
0 ignored issues
–
show
|
|||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | function relationship_set_title($relationship=null, $action=null, $param=null) { |
||
| 53 | global $OUTPUT; |
||
| 54 | |||
| 55 | echo $OUTPUT->header(); |
||
| 56 | if($relationship) { |
||
| 57 | echo $OUTPUT->heading(get_string('relationship', 'local_relationship') . ': ' . format_string($relationship->name)); |
||
|
0 ignored issues
–
show
|
|||
| 58 | echo html_writer::empty_tag('BR'); |
||
| 59 | } |
||
| 60 | if($action) { |
||
| 61 | echo $OUTPUT->heading(get_string($action, 'local_relationship', $param), '4'); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | function relationship_groups_parse_name($format, $value, $value_is_a_name=false) { |
||
| 66 | if($value_is_a_name) { |
||
| 67 | if (strstr($format, '@') !== false) { |
||
| 68 | $str = str_replace('@', $value, $format); |
||
| 69 | } else { |
||
| 70 | $str = str_replace('#', $value, $format); |
||
| 71 | } |
||
| 72 | } else { |
||
| 73 | if (strstr($format, '@') !== false) { // Convert $value to a character series |
||
| 74 | $letter = 'A'; |
||
| 75 | for($i=0; $i<$value; $i++) { |
||
| 76 | $letter++; |
||
| 77 | } |
||
| 78 | $str = str_replace('@', $letter, $format); |
||
| 79 | } else { // Convert $value to a number series |
||
| 80 | $str = str_replace('#', $value+1, $format); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | return($str); |
||
| 84 | } |
||
| 85 | |||
| 86 | function relationship_get_role_options() { |
||
| 87 | $all_roles = role_get_names(); |
||
| 88 | $ctx_roles = get_roles_for_contextlevels(CONTEXT_COURSE); |
||
| 89 | $roles = array(); |
||
| 90 | foreach($ctx_roles AS $id=>$roleid) { |
||
| 91 | if($roleid > 2) { |
||
| 92 | $roles[$roleid] = $all_roles[$roleid]->localname; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | asort($roles); |
||
| 96 | return $roles; |
||
| 97 | } |
||
| 98 | |||
| 99 | function relationship_get_cohort_options($relationshipid) { |
||
| 100 | global $DB; |
||
| 101 | |||
| 102 | $relationship = $DB->get_record('relationship', array('id' => $relationshipid), '*', MUST_EXIST); |
||
| 103 | $context = context::instance_by_id($relationship->contextid); |
||
| 104 | |||
| 105 | $contextids = array(); |
||
| 106 | foreach($context->get_parent_context_ids(true) as $ctxid) { |
||
| 107 | $context = context::instance_by_id($ctxid); |
||
| 108 | if (has_capability('moodle/cohort:view', $context)) { |
||
| 109 | $contextids[] = $ctxid; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | list($in_sql, $params) = $DB->get_in_or_equal($contextids, SQL_PARAMS_NAMED); |
||
| 113 | $sql = "SELECT id, name FROM {cohort} WHERE contextid {$in_sql} ORDER BY name"; |
||
| 114 | return $DB->get_records_sql_menu($sql, $params); |
||
| 115 | } |
||
| 116 | |||
| 117 | function relationship_get_courses($relationshipid) { |
||
| 118 | global $DB; |
||
| 119 | |||
| 120 | $sql = "SELECT DISTINCT c.id, c.shortname, c.fullname |
||
| 121 | FROM {enrol} e |
||
| 122 | JOIN {course} c ON (c.id = e.courseid) |
||
| 123 | WHERE e.enrol = 'relationship' |
||
| 124 | AND e.customint1 = :relationshipid |
||
| 125 | ORDER BY c.fullname"; |
||
| 126 | return $DB->get_records_sql($sql, array('relationshipid'=>$relationshipid)); |
||
| 127 | } |
||
| 128 | |||
| 129 | // Nomes de outros grupos que não os do próprio relacionamento |
||
| 130 | function relationship_get_other_courses_group_names($relationshipid) { |
||
| 131 | global $DB; |
||
| 132 | |||
| 133 | $sql = "SELECT DISTINCT g.name, e.courseid, c.fullname |
||
| 134 | FROM {enrol} e |
||
| 135 | JOIN {course} c ON (c.id = e.courseid) |
||
| 136 | JOIN {groups} g ON (g.courseid = e.courseid) |
||
| 137 | WHERE e.enrol = 'relationship' |
||
| 138 | AND e.customint1 = :relationshipid |
||
| 139 | AND g.idnumber NOT LIKE 'relationship_{$relationshipid}_%' |
||
| 140 | ORDER BY g.name"; |
||
| 141 | $groups = array(); |
||
| 142 | foreach($DB->get_records_sql($sql, array('relationshipid'=>$relationshipid)) as $r) { |
||
| 143 | $groups[$r->name][] = $r; |
||
| 144 | } |
||
| 145 | return $groups; |
||
| 146 | } |
||
| 147 | |||
| 148 | function relationship_is_in_use($relationshipid) { |
||
| 149 | global $DB; |
||
| 150 | |||
| 151 | return $DB->record_exists('enrol', array('enrol'=>'relationship', 'customint1'=>$relationshipid)); |
||
| 152 | } |
||
| 153 | |||
| 154 | function relationship_courses_where_is_in_use($relationshipid) { |
||
| 155 | global $DB; |
||
| 156 | |||
| 157 | $sql = "SELECT DISTINCT c.id, c.shortname, c.fullname |
||
| 158 | FROM {enrol} e |
||
| 159 | JOIN {course} c ON (c.id = e.courseid) |
||
| 160 | WHERE e.enrol = 'relationship' |
||
| 161 | AND e.customint1 = :relationshipid"; |
||
| 162 | return $DB->get_records_sql($sql, array('relationshipid'=>$relationshipid)); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Somehow deal with relationships when deleting course category, |
||
| 167 | * we can not just delete them because they might be used in enrol |
||
| 168 | * plugins or referenced in external systems. |
||
| 169 | * @param stdClass|coursecat $category |
||
| 170 | * @return void |
||
| 171 | */ |
||
| 172 | function relationship_delete_category($category) { |
||
| 173 | global $DB; |
||
| 174 | // TODO: make sure that relationships are really, really not used anywhere and delete, for now just move to parent or system context |
||
|
0 ignored issues
–
show
|
|||
| 175 | |||
| 176 | $oldcontext = context_coursecat::instance($category->id); |
||
| 177 | |||
| 178 | if ($category->parent and $parent = $DB->get_record('course_categories', array('id'=>$category->parent))) { |
||
| 179 | $parentcontext = context_coursecat::instance($parent->id); |
||
| 180 | $sql = "UPDATE {relationship} SET contextid = :newcontext WHERE contextid = :oldcontext"; |
||
| 181 | $params = array('oldcontext'=>$oldcontext->id, 'newcontext'=>$parentcontext->id); |
||
| 182 | } else { |
||
| 183 | $syscontext = context_system::instance(); |
||
| 184 | $sql = "UPDATE {relationship} SET contextid = :newcontext WHERE contextid = :oldcontext"; |
||
| 185 | $params = array('oldcontext'=>$oldcontext->id, 'newcontext'=>$syscontext->id); |
||
| 186 | } |
||
| 187 | |||
| 188 | $DB->execute($sql, $params); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get all the relationships defined in given context. |
||
| 193 | * |
||
| 194 | * @param int $contextid |
||
| 195 | * @param int $page number of the current page |
||
| 196 | * @param int $perpage items per page |
||
| 197 | * @param string $search search string |
||
| 198 | * @return array Array(totalrelationships => int, relationships => array, allrelationships => int) |
||
| 199 | */ |
||
| 200 | function relationship_search_relationships($contextid, $page = 0, $perpage = 25, $search = '') { |
||
| 201 | global $DB; |
||
| 202 | |||
| 203 | // Add some additional sensible conditions |
||
| 204 | $tests = array('contextid = ?'); |
||
| 205 | $params = array($contextid); |
||
| 206 | |||
| 207 | if (!empty($search)) { |
||
| 208 | $conditions = array('name', 'idnumber', 'description'); |
||
| 209 | $searchparam = '%' . $DB->sql_like_escape($search) . '%'; |
||
| 210 | foreach ($conditions as $key=>$condition) { |
||
| 211 | $conditions[$key] = $DB->sql_like($condition, "?", false); |
||
| 212 | $params[] = $searchparam; |
||
| 213 | } |
||
| 214 | $tests[] = '(' . implode(' OR ', $conditions) . ')'; |
||
| 215 | } |
||
| 216 | $wherecondition = implode(' AND ', $tests); |
||
| 217 | |||
| 218 | $fields = "SELECT *"; |
||
| 219 | $countfields = "SELECT COUNT(1)"; |
||
| 220 | $sql = " FROM {relationship} |
||
| 221 | WHERE $wherecondition"; |
||
| 222 | $order = " ORDER BY name ASC"; |
||
| 223 | $allrelationships = $DB->count_records('relationship', array('contextid'=>$contextid)); |
||
| 224 | $totalrelationships = $DB->count_records_sql($countfields . $sql, $params); |
||
| 225 | $relationships = $DB->get_records_sql($fields . $sql . $order, $params, $page*$perpage, $perpage); |
||
| 226 | foreach($relationships as $rl) { |
||
| 227 | $rl->tags = tag_get_tags_array('relationship', $rl->id); |
||
| 228 | } |
||
| 229 | |||
| 230 | return array('totalrelationships' => $totalrelationships, 'relationships' => $relationships, 'allrelationships'=>$allrelationships); |
||
|
0 ignored issues
–
show
|
|||
| 231 | } |
||
| 232 | |||
| 233 | |||
| 234 | |||
| 235 | |||
| 236 | |||
| 237 |
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.