See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
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 * View user acceptances to the policies 19 * 20 * @package tool_policy 21 * @copyright 2018 Marina Glancy 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace tool_policy; 26 27 use tool_policy\output\acceptances_filter; 28 use tool_policy\output\renderer; 29 use tool_policy\output\user_agreement; 30 use core_user; 31 use stdClass; 32 33 defined('MOODLE_INTERNAL') || die(); 34 35 global $CFG; 36 require_once($CFG->dirroot.'/lib/tablelib.php'); 37 38 /** 39 * Class acceptances_table 40 * 41 * @package tool_policy 42 * @copyright 2018 Marina Glancy 43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 44 */ 45 class acceptances_table extends \table_sql { 46 47 /** @var array */ 48 protected $versionids; 49 50 /** @var acceptances_filter */ 51 protected $acceptancesfilter; 52 53 /** @var renderer */ 54 protected $output; 55 56 /** 57 * @var string[] The list of countries. 58 */ 59 protected $countries; 60 61 /** @var bool are there any users that this user can agree on behalf of */ 62 protected $canagreeany = false; 63 64 /** 65 * Constructor. 66 * 67 * @param string $uniqueid Table identifier. 68 * @param acceptances_filter $acceptancesfilter 69 * @param renderer $output 70 */ 71 public function __construct($uniqueid, acceptances_filter $acceptancesfilter, renderer $output) { 72 global $CFG; 73 parent::__construct($uniqueid); 74 $this->set_attribute('id', 'acceptancetable'); 75 $this->acceptancesfilter = $acceptancesfilter; 76 $this->is_downloading(optional_param('download', 0, PARAM_ALPHA), 'user_acceptances'); 77 $this->baseurl = $acceptancesfilter->get_url(); 78 $this->output = $output; 79 80 $this->versionids = []; 81 $versions = $acceptancesfilter->get_versions(); 82 if (count($versions) > 1) { 83 foreach ($versions as $version) { 84 $this->versionids[$version->id] = $version->name; 85 } 86 } else { 87 $version = reset($versions); 88 $this->versionids[$version->id] = $version->name; 89 if ($version->status != policy_version::STATUS_ACTIVE) { 90 $this->versionids[$version->id] .= '<br>' . $version->revision; 91 } 92 } 93 94 $extrafields = get_extra_user_fields(\context_system::instance()); 95 $userfields = \user_picture::fields('u', $extrafields); 96 97 $this->set_sql("$userfields", 98 "{user} u", 99 'u.id <> :siteguestid AND u.deleted = 0', 100 ['siteguestid' => $CFG->siteguest]); 101 if (!$this->is_downloading()) { 102 $this->add_column_header('select', get_string('select'), false, 'colselect'); 103 } 104 $this->add_column_header('fullname', get_string('fullnameuser', 'core')); 105 foreach ($extrafields as $field) { 106 $this->add_column_header($field, get_user_field_name($field)); 107 } 108 109 if (!$this->is_downloading() && !has_capability('tool/policy:acceptbehalf', \context_system::instance())) { 110 // We will need to check capability to accept on behalf in each user's context, preload users contexts. 111 $this->sql->fields .= ',' . \context_helper::get_preload_record_columns_sql('ctx'); 112 $this->sql->from .= ' JOIN {context} ctx ON ctx.contextlevel = :usercontextlevel AND ctx.instanceid = u.id'; 113 $this->sql->params['usercontextlevel'] = CONTEXT_USER; 114 } 115 116 if ($this->acceptancesfilter->get_single_version()) { 117 $this->configure_for_single_version(); 118 } else { 119 $this->configure_for_multiple_versions(); 120 } 121 122 $this->build_sql_for_search_string($extrafields); 123 $this->build_sql_for_capability_filter(); 124 $this->build_sql_for_roles_filter(); 125 126 $this->sortable(true, 'firstname'); 127 } 128 129 /** 130 * Remove randomness from the list by always sorting by user id in the end 131 * 132 * @return array 133 */ 134 public function get_sort_columns() { 135 $c = parent::get_sort_columns(); 136 $c['u.id'] = SORT_ASC; 137 return $c; 138 } 139 140 /** 141 * Allows to add only one column name and header to the table (parent class methods only allow to set all). 142 * 143 * @param string $key 144 * @param string $label 145 * @param bool $sortable 146 * @param string $columnclass 147 */ 148 protected function add_column_header($key, $label, $sortable = true, $columnclass = '') { 149 if (empty($this->columns)) { 150 $this->define_columns([$key]); 151 $this->define_headers([$label]); 152 } else { 153 $this->columns[$key] = count($this->columns); 154 $this->column_style[$key] = array(); 155 $this->column_class[$key] = $columnclass; 156 $this->column_suppress[$key] = false; 157 $this->headers[] = $label; 158 } 159 if ($columnclass !== null) { 160 $this->column_class($key, $columnclass); 161 } 162 if (!$sortable) { 163 $this->no_sorting($key); 164 } 165 } 166 167 /** 168 * Helper configuration method. 169 */ 170 protected function configure_for_single_version() { 171 $userfieldsmod = get_all_user_name_fields(true, 'm', null, 'mod'); 172 $v = key($this->versionids); 173 $this->sql->fields .= ", $userfieldsmod, a{$v}.status AS status{$v}, a{$v}.note, ". 174 "a{$v}.timemodified, a{$v}.usermodified AS usermodified{$v}"; 175 176 $join = "JOIN {tool_policy_acceptances} a{$v} ON a{$v}.userid = u.id AND a{$v}.policyversionid=:versionid{$v}"; 177 $filterstatus = $this->acceptancesfilter->get_status_filter(); 178 if ($filterstatus == 1) { 179 $this->sql->from .= " $join AND a{$v}.status=1"; 180 } else if ($filterstatus == 2) { 181 $this->sql->from .= " $join AND a{$v}.status=0"; 182 } else { 183 $this->sql->from .= " LEFT $join"; 184 } 185 186 $this->sql->from .= " LEFT JOIN {user} m ON m.id = a{$v}.usermodified AND m.id <> u.id AND a{$v}.status IS NOT NULL"; 187 188 $this->sql->params['versionid' . $v] = $v; 189 190 if ($filterstatus === 0) { 191 $this->sql->where .= " AND a{$v}.status IS NULL"; 192 } 193 194 $this->add_column_header('status' . $v, get_string('response', 'tool_policy')); 195 $this->add_column_header('timemodified', get_string('responseon', 'tool_policy')); 196 $this->add_column_header('usermodified' . $v, get_string('responseby', 'tool_policy')); 197 $this->add_column_header('note', get_string('acceptancenote', 'tool_policy'), false); 198 } 199 200 /** 201 * Helper configuration method. 202 */ 203 protected function configure_for_multiple_versions() { 204 $this->add_column_header('statusall', get_string('acceptancestatusoverall', 'tool_policy')); 205 $filterstatus = $this->acceptancesfilter->get_status_filter(); 206 $statusall = []; 207 foreach ($this->versionids as $v => $versionname) { 208 $this->sql->fields .= ", a{$v}.status AS status{$v}, a{$v}.usermodified AS usermodified{$v}"; 209 $join = "JOIN {tool_policy_acceptances} a{$v} ON a{$v}.userid = u.id AND a{$v}.policyversionid=:versionid{$v}"; 210 if ($filterstatus == 1) { 211 $this->sql->from .= " {$join} AND a{$v}.status=1"; 212 } else if ($filterstatus == 2) { 213 $this->sql->from .= " {$join} AND a{$v}.status=0"; 214 } else { 215 $this->sql->from .= " LEFT {$join}"; 216 } 217 $this->sql->params['versionid' . $v] = $v; 218 $this->add_column_header('status' . $v, $versionname); 219 $statusall[] = "COALESCE(a{$v}.status, 0)"; 220 } 221 $this->sql->fields .= ",".join('+', $statusall)." AS statusall"; 222 223 if ($filterstatus === 0) { 224 $statussql = []; 225 foreach ($this->versionids as $v => $versionname) { 226 $statussql[] = "a{$v}.status IS NULL"; 227 } 228 $this->sql->where .= " AND (u.policyagreed = 0 OR ".join(" OR ", $statussql).")"; 229 } 230 } 231 232 /** 233 * Download the data. 234 */ 235 public function download() { 236 \core\session\manager::write_close(); 237 $this->out(0, false); 238 exit; 239 } 240 241 /** 242 * Get sql to add to where statement. 243 * 244 * @return string 245 */ 246 public function get_sql_where() { 247 list($where, $params) = parent::get_sql_where(); 248 $where = preg_replace('/firstname/', 'u.firstname', $where); 249 $where = preg_replace('/lastname/', 'u.lastname', $where); 250 return [$where, $params]; 251 } 252 253 /** 254 * Helper SQL query builder. 255 * 256 * @param array $userfields 257 */ 258 protected function build_sql_for_search_string($userfields) { 259 global $DB, $USER; 260 261 $search = $this->acceptancesfilter->get_search_strings(); 262 if (empty($search)) { 263 return; 264 } 265 266 $wheres = []; 267 $params = []; 268 foreach ($search as $index => $keyword) { 269 $searchkey1 = 'search' . $index . '1'; 270 $searchkey2 = 'search' . $index . '2'; 271 $searchkey3 = 'search' . $index . '3'; 272 $searchkey4 = 'search' . $index . '4'; 273 $searchkey5 = 'search' . $index . '5'; 274 $searchkey6 = 'search' . $index . '6'; 275 $searchkey7 = 'search' . $index . '7'; 276 277 $conditions = array(); 278 // Search by fullname. 279 $fullname = $DB->sql_fullname('u.firstname', 'u.lastname'); 280 $conditions[] = $DB->sql_like($fullname, ':' . $searchkey1, false, false); 281 282 // Search by email. 283 $email = $DB->sql_like('u.email', ':' . $searchkey2, false, false); 284 if (!in_array('email', $userfields)) { 285 $maildisplay = 'maildisplay' . $index; 286 $userid1 = 'userid' . $index . '1'; 287 // Prevent users who hide their email address from being found by others 288 // who aren't allowed to see hidden email addresses. 289 $email = "(". $email ." AND (" . 290 "u.maildisplay <> :$maildisplay " . 291 "OR u.id = :$userid1". // User can always find himself. 292 "))"; 293 $params[$maildisplay] = core_user::MAILDISPLAY_HIDE; 294 $params[$userid1] = $USER->id; 295 } 296 $conditions[] = $email; 297 298 // Search by idnumber. 299 $idnumber = $DB->sql_like('u.idnumber', ':' . $searchkey3, false, false); 300 if (!in_array('idnumber', $userfields)) { 301 $userid2 = 'userid' . $index . '2'; 302 // Users who aren't allowed to see idnumbers should at most find themselves 303 // when searching for an idnumber. 304 $idnumber = "(". $idnumber . " AND u.id = :$userid2)"; 305 $params[$userid2] = $USER->id; 306 } 307 $conditions[] = $idnumber; 308 309 // Search by middlename. 310 $middlename = $DB->sql_like('u.middlename', ':' . $searchkey4, false, false); 311 $conditions[] = $middlename; 312 313 // Search by alternatename. 314 $alternatename = $DB->sql_like('u.alternatename', ':' . $searchkey5, false, false); 315 $conditions[] = $alternatename; 316 317 // Search by firstnamephonetic. 318 $firstnamephonetic = $DB->sql_like('u.firstnamephonetic', ':' . $searchkey6, false, false); 319 $conditions[] = $firstnamephonetic; 320 321 // Search by lastnamephonetic. 322 $lastnamephonetic = $DB->sql_like('u.lastnamephonetic', ':' . $searchkey7, false, false); 323 $conditions[] = $lastnamephonetic; 324 325 $wheres[] = "(". implode(" OR ", $conditions) .") "; 326 $params[$searchkey1] = "%$keyword%"; 327 $params[$searchkey2] = "%$keyword%"; 328 $params[$searchkey3] = "%$keyword%"; 329 $params[$searchkey4] = "%$keyword%"; 330 $params[$searchkey5] = "%$keyword%"; 331 $params[$searchkey6] = "%$keyword%"; 332 $params[$searchkey7] = "%$keyword%"; 333 } 334 335 $this->sql->where .= ' AND '.join(' AND ', $wheres); 336 $this->sql->params += $params; 337 } 338 339 /** 340 * If there is a filter to find users who can/cannot accept on their own behalf add it to the SQL query 341 */ 342 protected function build_sql_for_capability_filter() { 343 global $CFG; 344 $hascapability = $this->acceptancesfilter->get_capability_accept_filter(); 345 if ($hascapability === null) { 346 return; 347 } 348 349 list($neededroles, $forbiddenroles) = get_roles_with_cap_in_context(\context_system::instance(), 'tool/policy:accept'); 350 351 if (empty($neededroles)) { 352 // There are no roles that allow to accept agreement on one own's behalf. 353 $this->sql->where .= $hascapability ? ' AND 1=0' : ''; 354 return; 355 } 356 357 if (empty($forbiddenroles)) { 358 // There are no roles that prohibit to accept agreement on one own's behalf. 359 $this->sql->where .= ' AND ' . $this->sql_has_role($neededroles, $hascapability); 360 return; 361 } 362 363 $defaultuserroleid = isset($CFG->defaultuserroleid) ? $CFG->defaultuserroleid : 0; 364 if (!empty($neededroles[$defaultuserroleid])) { 365 // Default role allows to accept agreement. Make sure user has/does not have one of the roles prohibiting it. 366 $this->sql->where .= ' AND ' . $this->sql_has_role($forbiddenroles, !$hascapability); 367 return; 368 } 369 370 if ($hascapability) { 371 // User has at least one role allowing to accept and no roles prohibiting. 372 $this->sql->where .= ' AND ' . $this->sql_has_role($neededroles); 373 $this->sql->where .= ' AND ' . $this->sql_has_role($forbiddenroles, false); 374 } else { 375 // Option 1: User has one of the roles prohibiting to accept. 376 $this->sql->where .= ' AND (' . $this->sql_has_role($forbiddenroles); 377 // Option 2: User has none of the roles allowing to accept. 378 $this->sql->where .= ' OR ' . $this->sql_has_role($neededroles, false) . ")"; 379 } 380 } 381 382 /** 383 * Returns SQL snippet for users that have (do not have) one of the given roles in the system context 384 * 385 * @param array $roles list of roles indexed by role id 386 * @param bool $positive true: return users who HAVE roles; false: return users who DO NOT HAVE roles 387 * @return string 388 */ 389 protected function sql_has_role($roles, $positive = true) { 390 global $CFG; 391 if (empty($roles)) { 392 return $positive ? '1=0' : '1=1'; 393 } 394 $defaultuserroleid = isset($CFG->defaultuserroleid) ? $CFG->defaultuserroleid : 0; 395 if (!empty($roles[$defaultuserroleid])) { 396 // No need to query, everybody has the default role. 397 return $positive ? '1=1' : '1=0'; 398 } 399 return "u.id " . ($positive ? "" : "NOT") . " IN ( 400 SELECT userid 401 FROM {role_assignments} 402 WHERE contextid = " . SYSCONTEXTID . " AND roleid IN (" . implode(',', array_keys($roles)) . ") 403 )"; 404 } 405 406 /** 407 * If there is a filter by user roles add it to the SQL query. 408 */ 409 protected function build_sql_for_roles_filter() { 410 foreach ($this->acceptancesfilter->get_role_filters() as $roleid) { 411 $this->sql->where .= ' AND ' . $this->sql_has_role([$roleid => $roleid]); 412 } 413 } 414 415 /** 416 * Hook that can be overridden in child classes to wrap a table in a form 417 * for example. Called only when there is data to display and not 418 * downloading. 419 */ 420 public function wrap_html_start() { 421 echo \html_writer::start_tag('form', 422 ['action' => new \moodle_url('/admin/tool/policy/accept.php')]); 423 echo \html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()]); 424 echo \html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'returnurl', 425 'value' => $this->get_return_url()]); 426 foreach (array_keys($this->versionids) as $versionid) { 427 echo \html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'versionids[]', 428 'value' => $versionid]); 429 } 430 } 431 432 /** 433 * Hook that can be overridden in child classes to wrap a table in a form 434 * for example. Called only when there is data to display and not 435 * downloading. 436 */ 437 public function wrap_html_finish() { 438 global $PAGE; 439 if ($this->canagreeany) { 440 echo \html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'action', 'value' => 'accept']); 441 echo \html_writer::empty_tag('input', ['type' => 'submit', 'data-action' => 'acceptmodal', 442 'value' => get_string('consentbulk', 'tool_policy'), 'class' => 'btn btn-primary mt-1']); 443 $PAGE->requires->js_call_amd('tool_policy/acceptmodal', 'getInstance', [\context_system::instance()->id]); 444 } 445 echo "</form>\n"; 446 } 447 448 /** 449 * Render the table. 450 */ 451 public function display() { 452 $this->out(100, true); 453 } 454 455 /** 456 * Call appropriate methods on this table class to perform any processing on values before displaying in table. 457 * Takes raw data from the database and process it into human readable format, perhaps also adding html linking when 458 * displaying table as html, adding a div wrap, etc. 459 * 460 * See for example col_fullname below which will be called for a column whose name is 'fullname'. 461 * 462 * @param array|object $row row of data from db used to make one row of the table. 463 * @return array one row for the table, added using add_data_keyed method. 464 */ 465 public function format_row($row) { 466 \context_helper::preload_from_record($row); 467 $row->canaccept = false; 468 $row->user = \user_picture::unalias($row, [], $this->useridfield); 469 $row->select = null; 470 if (!$this->is_downloading()) { 471 if (has_capability('tool/policy:acceptbehalf', \context_system::instance()) || 472 has_capability('tool/policy:acceptbehalf', \context_user::instance($row->id))) { 473 $row->canaccept = true; 474 $row->select = \html_writer::empty_tag('input', 475 ['type' => 'checkbox', 'name' => 'userids[]', 'value' => $row->id, 'class' => 'usercheckbox', 476 'id' => 'selectuser' . $row->id]) . 477 \html_writer::tag('label', get_string('selectuser', 'tool_policy', $this->username($row->user, false)), 478 ['for' => 'selectuser' . $row->id, 'class' => 'accesshide']); 479 $this->canagreeany = true; 480 } 481 } 482 return parent::format_row($row); 483 } 484 485 /** 486 * Get the column fullname value. 487 * 488 * @param stdClass $row 489 * @return string 490 */ 491 public function col_fullname($row) { 492 global $OUTPUT; 493 $userpic = $this->is_downloading() ? '' : $OUTPUT->user_picture($row->user); 494 return $userpic . $this->username($row->user, true); 495 } 496 497 /** 498 * User name with a link to profile 499 * 500 * @param stdClass $user 501 * @param bool $profilelink show link to profile (when we are downloading never show links) 502 * @return string 503 */ 504 protected function username($user, $profilelink = true) { 505 $canviewfullnames = has_capability('moodle/site:viewfullnames', \context_system::instance()) || 506 has_capability('moodle/site:viewfullnames', \context_user::instance($user->id)); 507 $name = fullname($user, $canviewfullnames); 508 if (!$this->is_downloading() && $profilelink) { 509 $profileurl = new \moodle_url('/user/profile.php', array('id' => $user->id)); 510 return \html_writer::link($profileurl, $name); 511 } 512 return $name; 513 } 514 515 /** 516 * Helper. 517 */ 518 protected function get_return_url() { 519 $pageurl = $this->baseurl; 520 if ($this->currpage) { 521 $pageurl = new \moodle_url($pageurl, [$this->request[TABLE_VAR_PAGE] => $this->currpage]); 522 } 523 return $pageurl; 524 } 525 526 /** 527 * Return agreement status 528 * 529 * @param int $versionid either id of an individual version or empty for overall status 530 * @param stdClass $row 531 * @return string 532 */ 533 protected function status($versionid, $row) { 534 $onbehalf = false; 535 $versions = $versionid ? [$versionid => $this->versionids[$versionid]] : $this->versionids; // List of versions. 536 $accepted = []; // List of versionids that user has accepted. 537 $declined = []; 538 539 foreach ($versions as $v => $name) { 540 if ($row->{'status' . $v} !== null) { 541 if (empty($row->{'status' . $v})) { 542 $declined[] = $v; 543 } else { 544 $accepted[] = $v; 545 } 546 $agreedby = $row->{'usermodified' . $v}; 547 if ($agreedby && $agreedby != $row->id) { 548 $onbehalf = true; 549 } 550 } 551 } 552 553 $ua = new user_agreement($row->id, $accepted, $declined, $this->get_return_url(), $versions, $onbehalf, $row->canaccept); 554 555 if ($this->is_downloading()) { 556 return $ua->export_for_download(); 557 558 } else { 559 return $this->output->render($ua); 560 } 561 } 562 563 /** 564 * Get the column timemodified value. 565 * 566 * @param stdClass $row 567 * @return string 568 */ 569 public function col_timemodified($row) { 570 if ($row->timemodified) { 571 if ($this->is_downloading()) { 572 // Use timestamp format readable for both machines and humans. 573 return date_format_string($row->timemodified, '%Y-%m-%d %H:%M:%S %Z'); 574 } else { 575 // Use localised calendar format. 576 return userdate($row->timemodified, get_string('strftimedatetime')); 577 } 578 } else { 579 return null; 580 } 581 } 582 583 /** 584 * Get the column note value. 585 * 586 * @param stdClass $row 587 * @return string 588 */ 589 public function col_note($row) { 590 if ($this->is_downloading()) { 591 return $row->note; 592 } else { 593 return format_text($row->note, FORMAT_MOODLE); 594 } 595 } 596 597 /** 598 * Get the column statusall value. 599 * 600 * @param stdClass $row 601 * @return string 602 */ 603 public function col_statusall($row) { 604 return $this->status(0, $row); 605 } 606 607 /** 608 * Generate the country column. 609 * 610 * @param \stdClass $data 611 * @return string 612 */ 613 public function col_country($data) { 614 if ($data->country && $this->countries === null) { 615 $this->countries = get_string_manager()->get_list_of_countries(); 616 } 617 if (!empty($this->countries[$data->country])) { 618 return $this->countries[$data->country]; 619 } 620 return ''; 621 } 622 623 /** 624 * You can override this method in a child class. See the description of 625 * build_table which calls this method. 626 * 627 * @param string $column 628 * @param stdClass $row 629 * @return string 630 */ 631 public function other_cols($column, $row) { 632 if (preg_match('/^status([\d]+)$/', $column, $matches)) { 633 $versionid = $matches[1]; 634 return $this->status($versionid, $row); 635 } 636 if (preg_match('/^usermodified([\d]+)$/', $column, $matches)) { 637 if ($row->$column && $row->$column != $row->id) { 638 $user = (object)['id' => $row->$column]; 639 username_load_fields_from_object($user, $row, 'mod'); 640 return $this->username($user, true); 641 } 642 return ''; // User agreed by themselves. 643 } 644 return parent::other_cols($column, $row); 645 } 646 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body