Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
/**
18
 * This block allows the user to give the course a rating, which
19
 * is displayed in a custom table (<prefix>_block_rate_course).
20
 *
21
 * @package    block
22
 * @subpackage rate_course
23
 * @copyright  2009 Jenny Gray
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 *
26
 * Code was Rewritten for Moodle 2.X By Atar + Plus LTD for Comverse LTD.
27
 * @copyright &copy; 2011 Comverse LTD.
28
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
29
 */
30
 
31
class block_rate_course extends block_list
32
{
33
    public function init() {
34
        $this->title = get_string('courserating', 'block_rate_course');
35
    }
36
 
37
    public function applicable_formats() {
38
        return array('all' => true, 'mod' => false, 'tag' => false, 'my' => false);
39
    }
40
 
41
    public function has_config() {
42
        return true; // Config only for review part.
43
    }
44
 
45
    public function get_content() {
46
        global $CFG, $COURSE, $USER, $DB, $OUTPUT, $PAGE;
47
 
48
        if ($this->content !== null) {
49
            return $this->content;
50
        }
51
 
52
        $this->content = new stdClass;
53
        $this->content->items = array();
54
        $this->content->icons = array();
55
 
56
        if (substr($PAGE->pagetype, 0, 11) == 'course-view') {
57
            $qmod = $DB->get_record('modules', array('name'=>'questionnaire'));
58
            if ($qmod && ($qmod->visible=='1') && !empty($CFG->block_rate_course_quest)) {
59
                // Get the Give a Review instance id.
60
                $questionnaire = $DB->get_record_sql(
61
                                "SELECT id,sid FROM {questionnaire} WHERE name = ? AND course = ?",
62
                                array($CFG->block_rate_course_quest, $COURSE->id));
63
                if ($questionnaire) {
64
                    $url = new moodle_url('/mod/questionnaire/report.php',
65
                                    array('instance'=>$questionnaire->id, 'sid'=>$questionnaire->sid));
66
                    $this->content->items[] = $OUTPUT->action_link($url, get_string('viewreview', 'block_rate_course'));
67
                    $this->content->icons[] = $OUTPUT->pix_icon('review', get_string('viewreview', 'block_rate_course'),
68
                                    'block_rate_course', array('class'=>'icon'));
69
                }
70
            }
71
 
72
            $this->content->icons[] = $OUTPUT->pix_icon('star', get_string('giverating', 'block_rate_course'),
73
                            'block_rate_course', array('class'=>'icon'));
74
            $url = new moodle_url('/blocks/rate_course/rate.php', array('courseid'=>$COURSE->id));
75
            $this->content->items[] = $OUTPUT->action_link($url, get_string('giverating', 'block_rate_course'));
76
            $this->content->items[] = '';
77
            $this->content->icons[] = $OUTPUT->pix_icon('spacer', '');
78
 
79
            // Output current rating.
80
            $this->content->footer = '<div class="centered">'.
81
                            $this->display_rating($COURSE->id, true).'</div>';
82
        } else {
83
            if ($this->page->user_is_editing()) {
84
                $this->content->items[] = get_string('editingsitehome', 'block_rate_course');
85
            }
86
        }
87
        return $this->content;
88
 
89
    }
90
 
91
    /**
92
     * Checks whether any version of the course already exists.
93
     * @param int $courseid The ID of the course.
94
     * @return int  rating.
95
     */
96
    public function get_rating($courseid) {
97
        global $CFG, $DB;
98
        $sql = "SELECT AVG(rating) AS avg
99
        FROM {block_rate_course}
100
        WHERE course = $courseid";
101
 
102
        $avg = -1;
103
        if ($avgrec = $DB->get_record_sql($sql)) {
104
            $avg = $avgrec->avg * 2;  // Double it for half star scores.
105
            // Now round it up or down.
106
            $avg = round($avg);
107
        }
108
        return $avg;
109
    }
110
 
111
    /**
112
     * Outputs the current rating. Can be called outside the block.
113
     * @param int $courseid the ID of the course
114
     * @param bool $return return the string (true) or echo it immediately (false)
115
     * @return string the html to output graphic, alt text and number of ratings
116
     */
117
    public function display_rating($courseid, $return=false) {
118
        global $CFG, $DB;
119
        $count = $DB->count_records('block_rate_course', array('course'=>$courseid));
120
        $ratedby = '';
121
        if ($count > 0) {
122
            $ratedby = get_string ('rating_users', 'block_rate_course', $count);
123
        }
124
 
125
        $numstars = $this->get_rating( $courseid );
126
        if ($numstars == -1) {
127
            $alt = '';
128
        } else if ($numstars == 0) {
129
            $alt = get_string( 'rating_alt0', 'block_rate_course' );
130
        } else {
131
            $alt = get_string( 'rating_altnum', 'block_rate_course', $numstars/2 );
132
        }
133
 
134
        $res = '<img src="'.
135
                        $CFG->wwwroot.'/blocks/rate_course/pix/rating_graphic.php?courseid='.
136
                        $courseid.'" alt="'.$alt.'"/><br/>'.$ratedby;
137
 
138
        if ($return) {
139
            return $res;
140
        }
141
        echo $res;
142
    }
143
 
144
    public function show_rating($courseid) {
145
        global $CFG, $DB;
146
        // Pinned block check once per session for performance.
147
        if (!isset($_SESSION['starsenabled'])) {
148
            $_SESSION['starsenabled'] = $DB->get_field('block', 'visible',
149
                            array('name'=>'rate_course'));
150
            if ($_SESSION['starsenabled'] && !isset($_SESSION['starspinned'])) {
151
                $_SESSION['starspinned'] = $DB->get_record_sql(
152
                                "SELECT * FROM {block_pinned} p
153
                                JOIN {block} b ON b.id = p.blockid
154
                                WHERE pagetype = ? AND p.visible = ? AND b.name = ?",
155
                                array('course-view', 1, 'rate_course'));
156
            }
157
        }
158
        if (!$_SESSION['starsenabled']) {
159
            return false;
160
        }
161
        if ($_SESSION['starspinned']) {
162
            return true;
163
        }
164
 
165
        return $DB->get_record_sql("SELECT * FROM {block_instance} i
166
                        JOIN {block} b ON b.id = i.blockid
167
                        WHERE pageid = ? and b.name = ?", array($courseid, 'rate_course'));
168
    }
169
 
170
}