\n";
}
/**
* Render the table.
*/
public function display() {
$this->out(100, true);
}
/**
* Call appropriate methods on this table class to perform any processing on values before displaying in table.
* Takes raw data from the database and process it into human readable format, perhaps also adding html linking when
* displaying table as html, adding a div wrap, etc.
*
* See for example col_fullname below which will be called for a column whose name is 'fullname'.
*
* @param array|object $row row of data from db used to make one row of the table.
* @return array one row for the table, added using add_data_keyed method.
*/
public function format_row($row) {
\context_helper::preload_from_record($row);
$row->canaccept = false;
$row->user = \user_picture::unalias($row, [], $this->useridfield);
$row->select = null;
if (!$this->is_downloading()) {
if (has_capability('tool/policy:acceptbehalf', \context_system::instance()) ||
has_capability('tool/policy:acceptbehalf', \context_user::instance($row->id))) {
$row->canaccept = true;
$row->select = \html_writer::empty_tag('input',
['type' => 'checkbox', 'name' => 'userids[]', 'value' => $row->id, 'class' => 'usercheckbox',
'id' => 'selectuser' . $row->id]) .
\html_writer::tag('label', get_string('selectuser', 'tool_policy', $this->username($row->user, false)),
['for' => 'selectuser' . $row->id, 'class' => 'accesshide']);
$this->canagreeany = true;
}
}
return parent::format_row($row);
}
/**
* Get the column fullname value.
*
* @param stdClass $row
* @return string
*/
public function col_fullname($row) {
global $OUTPUT;
$userpic = $this->is_downloading() ? '' : $OUTPUT->user_picture($row->user);
return $userpic . $this->username($row->user, true);
}
/**
* User name with a link to profile
*
* @param stdClass $user
* @param bool $profilelink show link to profile (when we are downloading never show links)
* @return string
*/
protected function username($user, $profilelink = true) {
$canviewfullnames = has_capability('moodle/site:viewfullnames', \context_system::instance()) ||
has_capability('moodle/site:viewfullnames', \context_user::instance($user->id));
$name = fullname($user, $canviewfullnames);
if (!$this->is_downloading() && $profilelink) {
$profileurl = new \moodle_url('/user/profile.php', array('id' => $user->id));
return \html_writer::link($profileurl, $name);
}
return $name;
}
/**
* Helper.
*/
protected function get_return_url() {
$pageurl = $this->baseurl;
if ($this->currpage) {
$pageurl = new \moodle_url($pageurl, [$this->request[TABLE_VAR_PAGE] => $this->currpage]);
}
return $pageurl;
}
/**
* Return agreement status
*
* @param int $versionid either id of an individual version or empty for overall status
* @param stdClass $row
* @return string
*/
protected function status($versionid, $row) {
$onbehalf = false;
$versions = $versionid ? [$versionid => $this->versionids[$versionid]] : $this->versionids; // List of versions.
$accepted = []; // List of versionids that user has accepted.
$declined = [];
foreach ($versions as $v => $name) {
if ($row->{'status' . $v} !== null) {
if (empty($row->{'status' . $v})) {
$declined[] = $v;
} else {
$accepted[] = $v;
}
$agreedby = $row->{'usermodified' . $v};
if ($agreedby && $agreedby != $row->id) {
$onbehalf = true;
}
}
}
$ua = new user_agreement($row->id, $accepted, $declined, $this->get_return_url(), $versions, $onbehalf, $row->canaccept);
if ($this->is_downloading()) {
return $ua->export_for_download();
} else {
return $this->output->render($ua);
}
}
/**
* Get the column timemodified value.
*
* @param stdClass $row
* @return string
*/
public function col_timemodified($row) {
if ($row->timemodified) {
if ($this->is_downloading()) {
// Use timestamp format readable for both machines and humans.
return date_format_string($row->timemodified, '%Y-%m-%d %H:%M:%S %Z');
} else {
// Use localised calendar format.
return userdate($row->timemodified, get_string('strftimedatetime'));
}
} else {
return null;
}
}
/**
* Get the column note value.
*
* @param stdClass $row
* @return string
*/
public function col_note($row) {
if ($this->is_downloading()) {
return $row->note;
} else {
return format_text($row->note, FORMAT_MOODLE);
}
}
/**
* Get the column statusall value.
*
* @param stdClass $row
* @return string
*/
public function col_statusall($row) {
return $this->status(0, $row);
}
/**
* Generate the country column.
*
* @param \stdClass $data
* @return string
*/
public function col_country($data) {
if ($data->country && $this->countries === null) {
$this->countries = get_string_manager()->get_list_of_countries();
}
if (!empty($this->countries[$data->country])) {
return $this->countries[$data->country];
}
return '';
}
/**
* You can override this method in a child class. See the description of
* build_table which calls this method.
*
* @param string $column
* @param stdClass $row
* @return string
*/
public function other_cols($column, $row) {
if (preg_match('/^status([\d]+)$/', $column, $matches)) {
$versionid = $matches[1];
return $this->status($versionid, $row);
}
if (preg_match('/^usermodified([\d]+)$/', $column, $matches)) {
if ($row->$column && $row->$column != $row->id) {
$user = (object)['id' => $row->$column];
username_load_fields_from_object($user, $row, 'mod');
return $this->username($user, true);
}
return ''; // User agreed by themselves.
}
return null;
}
}