| 1 | efrain | 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 | namespace qbank_comment;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | use core_question\local\bank\column_base;
 | 
        
           |  |  | 20 | use question_bank;
 | 
        
           |  |  | 21 |   | 
        
           |  |  | 22 | /**
 | 
        
           |  |  | 23 |  * A column to show the number of comments.
 | 
        
           |  |  | 24 |  *
 | 
        
           |  |  | 25 |  * @package    qbank_comment
 | 
        
           |  |  | 26 |  * @copyright  2021 Catalyst IT Australia Pty Ltd
 | 
        
           |  |  | 27 |  * @author     Safat Shahin <safatshahin@catalyst-au.net>
 | 
        
           |  |  | 28 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 29 |  */
 | 
        
           |  |  | 30 | class comment_count_column extends column_base {
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 |     /**
 | 
        
           |  |  | 33 |      * @var bool Comments enabled or not from config.
 | 
        
           |  |  | 34 |      */
 | 
        
           |  |  | 35 |     protected $commentsenabled = true;
 | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 |     /**
 | 
        
           |  |  | 38 |      * Load javascript module if enabled.
 | 
        
           |  |  | 39 |      *
 | 
        
           |  |  | 40 |      * @return void
 | 
        
           |  |  | 41 |      */
 | 
        
           |  |  | 42 |     public function init(): void {
 | 
        
           |  |  | 43 |         parent::init();
 | 
        
           |  |  | 44 |         $this->check_comments_status();
 | 
        
           |  |  | 45 |         if ($this->commentsenabled) {
 | 
        
           |  |  | 46 |             global $PAGE;
 | 
        
           |  |  | 47 |             $PAGE->requires->js_call_amd('qbank_comment/comment', 'init');
 | 
        
           |  |  | 48 |         }
 | 
        
           |  |  | 49 |     }
 | 
        
           |  |  | 50 |   | 
        
           |  |  | 51 |     /**
 | 
        
           |  |  | 52 |      * Check if comments is turned on in the system or not.
 | 
        
           |  |  | 53 |      */
 | 
        
           |  |  | 54 |     protected function check_comments_status(): void {
 | 
        
           |  |  | 55 |         global $CFG;
 | 
        
           |  |  | 56 |         if (!$CFG->usecomments) {
 | 
        
           |  |  | 57 |             $this->commentsenabled = false;
 | 
        
           |  |  | 58 |         }
 | 
        
           |  |  | 59 |     }
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 |     /**
 | 
        
           |  |  | 62 |      * Get the name of the column, used internally.
 | 
        
           |  |  | 63 |      *
 | 
        
           |  |  | 64 |      * @return string
 | 
        
           |  |  | 65 |      */
 | 
        
           |  |  | 66 |     public function get_name(): string {
 | 
        
           |  |  | 67 |         return 'commentcount';
 | 
        
           |  |  | 68 |     }
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 |     /**
 | 
        
           |  |  | 71 |      * Get the title of the column that will be displayed.
 | 
        
           |  |  | 72 |      *
 | 
        
           |  |  | 73 |      * @return string
 | 
        
           |  |  | 74 |      */
 | 
        
           |  |  | 75 |     public function get_title(): string {
 | 
        
           |  |  | 76 |         return get_string('commentplural', 'qbank_comment');
 | 
        
           |  |  | 77 |     }
 | 
        
           |  |  | 78 |   | 
        
           |  |  | 79 |     /**
 | 
        
           |  |  | 80 |      * Generate the content to be displayed.
 | 
        
           |  |  | 81 |      *
 | 
        
           |  |  | 82 |      * @param object $question The question object.
 | 
        
           |  |  | 83 |      * @param string $rowclasses Classes that can be added.
 | 
        
           |  |  | 84 |      */
 | 
        
           |  |  | 85 |     protected function display_content($question, $rowclasses): void {
 | 
        
           |  |  | 86 |         global $DB;
 | 
        
           | 1441 | ariadna | 87 |   | 
        
           | 1 | efrain | 88 |         $syscontext = \context_system::instance();
 | 
        
           | 1441 | ariadna | 89 |   | 
        
           |  |  | 90 |         $args = new \stdClass;
 | 
        
           |  |  | 91 |         $args->contextid = $syscontext->id;
 | 
        
           |  |  | 92 |         $args->courseid  = $this->qbank->course->id;
 | 
        
           |  |  | 93 |         $args->area      = 'question';
 | 
        
           |  |  | 94 |         $args->itemid    = $question->id;
 | 
        
           |  |  | 95 |         $args->component = 'qbank_comment';
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 |         $params = [
 | 
        
           |  |  | 98 |             'component' => $args->component,
 | 
        
           |  |  | 99 |             'commentarea' => $args->area,
 | 
        
           |  |  | 100 |             'itemid' => $args->itemid,
 | 
        
           |  |  | 101 |             'contextid' => $args->contextid,
 | 
        
           | 1 | efrain | 102 |         ];
 | 
        
           | 1441 | ariadna | 103 |         $commentcount = $DB->count_records('comments', $params);
 | 
        
           | 1 | efrain | 104 |         $attributes = [];
 | 
        
           | 1441 | ariadna | 105 |   | 
        
           |  |  | 106 |         // Build up the comment object to see if we have correct permissions to post.
 | 
        
           |  |  | 107 |         $comment = new \comment($args);
 | 
        
           |  |  | 108 |         if (question_has_capability_on($question, 'comment') && $comment->can_post()) {
 | 
        
           |  |  | 109 |             $tag = 'a';
 | 
        
           | 1 | efrain | 110 |             $target = 'questioncommentpreview_' . $question->id;
 | 
        
           |  |  | 111 |             $attributes = [
 | 
        
           |  |  | 112 |                 'href' => '#',
 | 
        
           |  |  | 113 |                 'data-target' => $target,
 | 
        
           |  |  | 114 |                 'data-questionid' => $question->id,
 | 
        
           |  |  | 115 |                 'data-courseid' => $this->qbank->course->id,
 | 
        
           |  |  | 116 |                 'data-contextid' => $syscontext->id,
 | 
        
           |  |  | 117 |             ];
 | 
        
           | 1441 | ariadna | 118 |         } else {
 | 
        
           |  |  | 119 |             $tag = 'span';
 | 
        
           | 1 | efrain | 120 |         }
 | 
        
           | 1441 | ariadna | 121 |         echo \html_writer::tag($tag, $commentcount, $attributes);
 | 
        
           | 1 | efrain | 122 |     }
 | 
        
           |  |  | 123 |   | 
        
           |  |  | 124 |     public function get_extra_classes(): array {
 | 
        
           | 1441 | ariadna | 125 |         return ['pe-3'];
 | 
        
           | 1 | efrain | 126 |     }
 | 
        
           |  |  | 127 |   | 
        
           |  |  | 128 |     public function get_default_width(): int {
 | 
        
           |  |  | 129 |         return 150;
 | 
        
           |  |  | 130 |     }
 | 
        
           |  |  | 131 | }
 |