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