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 * A column type for the name of the question creator.
19 *
20 * @package   core_question
21 * @copyright 2009 Tim Hunt
22 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_question\bank;
26defined('MOODLE_INTERNAL') || die();
27
28
29/**
30 * A column type for the name of the question creator.
31 *
32 * @copyright 2009 Tim Hunt
33 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class creator_name_column extends column_base {
36    public function get_name() {
37        return 'creatorname';
38    }
39
40    protected function get_title() {
41        return get_string('createdby', 'question');
42    }
43
44    protected function display_content($question, $rowclasses) {
45        if (!empty($question->creatorfirstname) && !empty($question->creatorlastname)) {
46            $u = new \stdClass();
47            $u = username_load_fields_from_object($u, $question, 'creator');
48            $date = userdate($question->timecreated, get_string('strftimedatetime', 'langconfig'));
49            echo fullname($u) . '<br>' . \html_writer::tag('span', $date, array('class' => 'date'));
50        }
51    }
52
53    public function get_extra_joins() {
54        return array('uc' => 'LEFT JOIN {user} uc ON uc.id = q.createdby');
55    }
56
57    public function get_required_fields() {
58        $allnames = \core_user\fields::get_name_fields();
59        $requiredfields = array();
60        foreach ($allnames as $allname) {
61            $requiredfields[] = 'uc.' . $allname . ' AS creator' . $allname;
62        }
63        $requiredfields[] = 'q.timecreated';
64        return $requiredfields;
65    }
66
67    public function is_sortable() {
68        return array(
69            'firstname' => array('field' => 'uc.firstname', 'title' => get_string('firstname')),
70            'lastname' => array('field' => 'uc.lastname', 'title' => get_string('lastname')),
71            'timecreated' => array('field' => 'q.timecreated', 'title' => get_string('date'))
72        );
73    }
74}
75