Proyectos de Subversion Moodle

Rev

Rev 97 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
96 efrain 1
<?php
2
 
3
class block_cesa_course_rating extends block_base {
4
    public function init() {
5
        $this->title = get_string('pluginname', 'block_cesa_course_rating');
6
    }
7
 
8
    public function applicable_formats() {
9
 
10
        return [
11
            'all' => false,
12
            'my' => false,
13
            'admin' => false,
14
            'course' => false,
15
            'course-view' => true,
16
            'enrol' => true,
17
        ];
18
 
19
    }
20
 
21
    function specialization() {
22
        global $CFG, $DB;
23
        if (empty($this->config)) {
24
            $this->instance->defaultregion = 'below-content';
25
            $this->instance->region = 'below-content';
26
            $DB->update_record('block_instances', $this->instance);
27
 
28
            $this->config->title = 'Students feedback';
29
        }
30
    }
31
 
32
    function instance_allow_multiple() {
33
        return false;
34
    }
35
 
36
    public function html_attributes() {
37
      global $CFG;
38
      $attributes = parent::html_attributes();
39
 
40
      /*
41
      $ccn_mt = '';
42
      $ccn_mb = '';
43
      $ccn_pt = '';
44
      $ccn_pb = '';
45
      $ccn_css_class = '';
46
 
47
      if(!empty($this->config->ccn_margin_top)){
48
          if($this->config->ccn_margin_top == '0') {
49
              $ccn_mt = '';
50
          } elseif($this->config->ccn_margin_top == 'zero') {
51
              $ccn_mt = 'margin-top:0px;';
52
          } else {
53
              $ccn_mt = 'margin-top:'.$this->config->ccn_margin_top.'px;';
54
          }
55
      }
56
      if(!empty($this->config->ccn_margin_bottom)){
57
          $ccn_mb = 'margin-bottom:'.$this->config->ccn_margin_bottom.'px;';
58
      } else {
59
          $ccn_mb = '';
60
      }
61
      if(!empty($this->config->ccn_padding_top)){
62
          $ccn_pt = 'padding-top:'.$this->config->ccn_padding_top.'px;';
63
      } else {
64
          $ccn_pt = '';
65
      }
66
      if(!empty($this->config->ccn_padding_bottom)){
67
          $ccn_pb = 'padding-bottom:'.$this->config->ccn_padding_bottom.'px;';
68
      } else {
69
          $ccn_pb = '';
70
      }
71
      if(!empty($this->config->ccn_css_class)){
72
          $ccn_css_class = $this->config->ccn_css_class;
73
      } else {
74
          $ccn_css_class = '';
75
      }
76
 
77
 
78
      $ccn_stylize = $ccn_mt . $ccn_mb . $ccn_pt . $ccn_pb;
79
 
80
 
81
 
82
 
83
 
84
      $attributes['ccn_style'] = $ccn_stylize;
85
      // $attributes['class'] .= ' block_'. $this->name();
86
      $attributes['class'] .= ' '. $ccn_css_class;
87
      */
88
 
89
 
90
      return $attributes;
91
    }
92
 
93
    public function has_config() {
94
        return true;
95
    }
96
 
97
    public function get_content() {
98
        global $CFG, $COURSE;
99
 
100
        if ($this->content !== null) {
101
            // return $this->content;
102
        }
103
 
104
        $this->content = new stdClass;
105
 
106
        if(!empty($this->config->title)){$this->content->title =  format_text($this->config->title, FORMAT_HTML, array('filter' => true));} else {$this->content->title = get_string('pluginname', 'block_cesa_course_rating');}
107
 
108
        $courseid = $COURSE->id;
109
        $context = get_context_instance(CONTEXT_COURSE, $courseid);
110
 
111
        $canRate = has_capability('block/cesa_course_rating:rate', $context);
112
        if($canRate == 1) {
113
          $canRateClass = 'ccn-can-rate';
114
        } else {
115
          $canRateClass = 'ccn-cannot-rate';
116
        }
117
 
118
        $ccnSubmitRating = $this->submit_rating();
119
        $this->content->text = '';
120
 
121
        $ccnRating    = number_format($this->overall_rating($COURSE->id), 1);
122
 
123
        $ccnStar      = '<li class="list-inline-item"><i class="fa fa-star"></i></li>';
124
        $ccnStarHalf  = '<li class="list-inline-item"><i class="fa fa-star-half-o"></i></li>';
125
        $ccnStarVoid  = '<li class="list-inline-item"><i class="fa fa-star-o"></i></li>';
126
 
127
        if($ccnRating == 5) {
128
          $ccnStars = str_repeat($ccnStar, 5);
129
        } elseif($ccnRating == 4.5) {
130
          $ccnStars = str_repeat($ccnStar, 4) . str_repeat($ccnStarHalf, 1);
131
        } elseif($ccnRating == 4) {
132
          $ccnStars = str_repeat($ccnStar, 4) . str_repeat($ccnStarVoid, 1);
133
        } elseif($ccnRating == 3.5) {
134
          $ccnStars = str_repeat($ccnStar, 3) . str_repeat($ccnStarHalf, 1) . str_repeat($ccnStarVoid, 1);
135
        } elseif($ccnRating == 3) {
136
          $ccnStars = str_repeat($ccnStar, 3) . str_repeat($ccnStarVoid, 2);
137
        } elseif($ccnRating == 2.5) {
138
          $ccnStars = str_repeat($ccnStar, 2) . str_repeat($ccnStarHalf, 1)  . str_repeat($ccnStarVoid, 2);
139
        } elseif($ccnRating == 2) {
140
          $ccnStars = str_repeat($ccnStar, 2) . str_repeat($ccnStarVoid, 3);
141
        } elseif($ccnRating == 1.5) {
142
          $ccnStars = str_repeat($ccnStar, 1) . str_repeat($ccnStarHalf, 1)  . str_repeat($ccnStarVoid, 3);
143
        } elseif($ccnRating == 0.5) {
144
          $ccnStars = str_repeat($ccnStarHalf, 1) . str_repeat($ccnStarVoid, 4);
145
        } else {
146
          $ccnStars = str_repeat($ccnStarVoid, 5);
147
        }
148
 
149
        $ccnFive = $this->get_specific_average($COURSE->id, 5);
150
        $ccnFour = $this->get_specific_average($COURSE->id, 4);
151
        $ccnThree = $this->get_specific_average($COURSE->id, 3);
152
        $ccnTwo = $this->get_specific_average($COURSE->id, 2);
153
        $ccnOne = $this->get_specific_average($COURSE->id, 1);
154
 
155
        $this->content->text .= '
156
        	<div class="cs_row_five">
157
									<div class="student_feedback_container">
158
										<h4 data-ccn="title" class="aii_title">'.$this->content->title.'</h4>
159
										<div class="s_feeback_content">
160
									        <ul class="skills">
161
									        	<li class="list-inline-item">'.get_string('stars_5', 'block_cesa_course_rating').'</li>
162
									            <li class="list-inline-item progressbar1" data-width="'.$ccnFive.'" data-target="100">'.$ccnFive.'%</li>
163
									        </ul>
164
									        <ul class="skills">
165
									        	<li class="list-inline-item">'.get_string('stars_4', 'block_cesa_course_rating').'</li>
166
									            <li class="list-inline-item progressbar2" data-width="'.$ccnFour.'" data-target="100">'.$ccnFour.'%</li>
167
									        </ul>
168
									        <ul class="skills">
169
									        	<li class="list-inline-item">'.get_string('stars_3', 'block_cesa_course_rating').'</li>
170
									            <li class="list-inline-item progressbar3" data-width="'.$ccnThree.'" data-target="100">'.$ccnThree.'%</li>
171
									        </ul>
172
									        <ul class="skills">
173
									        	<li class="list-inline-item">'.get_string('stars_2', 'block_cesa_course_rating').'</li>
174
									            <li class="list-inline-item progressbar4" data-width="'.$ccnTwo.'" data-target="100">'.$ccnTwo.'%</li>
175
									        </ul>
176
									        <ul class="skills">
177
									        	<li class="list-inline-item">'.get_string('stars_1', 'block_cesa_course_rating').'</li>
178
									            <li class="list-inline-item progressbar5" data-width="'.$ccnOne.'" data-target="100">'.$ccnOne.'%</li>
179
									        </ul>
180
										</div>
181
										<div class="aii_average_review text-center '.$canRateClass.'">
182
											<div class="av_content">
183
												<h2>'.$ccnRating.'</h2>
184
												<ul class="aii_rive_list mb0">
185
													'.$ccnStars.'
186
												</ul>
187
												<p>'.$this->count_ratings($COURSE->id).'</p>';
188
                        if($canRate == 1){
189
                          $this->content->text .= $ccnSubmitRating;
190
                        }
191
                      $this->content->text .='
192
											</div>
193
										</div>
194
									</div>
195
								</div>';
196
        return $this->content;
197
 
198
    }
199
 
200
    public function overall_rating($courseID) {
201
        global $CFG, $DB;
202
        $sql = "  SELECT AVG(rating) AS average
203
                  FROM {block_cesa_course_rating}
204
                  WHERE course = $courseID
205
               ";
206
        $totalAverage = -1;
207
        if ($getAverage = $DB->get_record_sql($sql)) {
208
            $totalAverage = round($getAverage->average * 2) / 2;
209
        }
210
        return $totalAverage;
211
    }
212
 
213
    public function count_ratings($courseID) {
214
        global $CFG, $DB;
215
        $countRecords = $DB->count_records('block_cesa_course_rating', array('course'=>$courseID));
216
        $countRatings = '';
217
        if ($countRecords > 0) {
218
            $countRatings = get_string ('rated_by', 'block_cesa_course_rating', $countRecords);
219
        } else {
220
            $countRatings = get_string ('rated_by_none', 'block_cesa_course_rating');
221
        }
222
        return $countRatings;
223
    }
224
    public function count_ratings_external($courseID) {
225
        global $CFG, $DB;
226
        $countRecords = $DB->count_records('block_cesa_course_rating', array('course'=>$courseID));
227
        return $countRecords;
228
    }
229
 
230
    public function get_specific_average($courseID, $rating) {
231
        global $CFG, $DB;
232
        $countOnlyRating        = $DB->count_records('block_cesa_course_rating', array('course'=>$courseID, 'rating'=>$rating));
233
        $countExcludingRating   = $DB->count_records_sql(
234
        "       SELECT COUNT(*)
235
                FROM {block_cesa_course_rating}
236
                WHERE course = $courseID
237
                AND rating <> $rating
238
        ");
239
 
240
        $countTotal             = $DB->count_records('block_cesa_course_rating', array('course'=>$courseID));
241
 
242
        if($countTotal == 0) {
243
          $result = '0';
244
        } else {
245
          $result = $countOnlyRating / $countTotal * 100;
246
        }
247
        return $result;
248
 
249
    }
250
 
251
    public function submit_rating() {
252
 
253
      global $CFG, $COURSE;
254
 
255
      $courseid = $COURSE->id;
256
      $context = get_context_instance(CONTEXT_COURSE, $courseid);
257
 
258
      $ccnStar =  '<span class="fa fa-star"></span>';
259
      $return =   '<form id="ccn-star-rate" method="post" action="'.$CFG->wwwroot.'/blocks/cesa_course_rating/rate_course.php">
260
                    <input name="id" type="hidden" value="'.$courseid.'" />
261
                    <div class="ccn-star-rate-inner">';
262
                      for ($i = 5; $i >= 1; $i--) {
263
                        $printCcnStar = str_repeat($ccnStar, $i);
264
                        $return .='    <input required type="radio" id="stars-'.$i.'" name="rating" value="'.$i.'" /><label for="stars-'.$i.'"></label>';
265
                      }
266
      $return .= '  </div>
267
                    <button class="btn btn-primary" type="submit">'.get_string('rate_course', 'block_cesa_course_rating').'</button>
268
                  </form>';
269
      return $return;
270
    }
271
 
272
    public function external_star_rating($courseID) {
273
 
274
      $ccnStar      = '<li class="list-inline-item"><i class="fa fa-star"></i></li>';
275
      $ccnStarHalf  = '<li class="list-inline-item"><i class="fa fa-star-half-o"></i></li>';
276
      $ccnStarVoid  = '<li class="list-inline-item"><i class="fa fa-star-o"></i></li>';
277
      $ccnRating    = $this->overall_rating($courseID);
278
 
279
      if($ccnRating == 5) {
280
        $ccnStars = str_repeat($ccnStar, 5);
281
      } elseif($ccnRating == 4.5) {
282
        $ccnStars = str_repeat($ccnStar, 4) . str_repeat($ccnStarHalf, 1);
283
      } elseif($ccnRating == 4) {
284
        $ccnStars = str_repeat($ccnStar, 4) . str_repeat($ccnStarVoid, 1);
285
      } elseif($ccnRating == 3.5) {
286
        $ccnStars = str_repeat($ccnStar, 3) . str_repeat($ccnStarHalf, 1) . str_repeat($ccnStarVoid, 1);
287
      } elseif($ccnRating == 3) {
288
        $ccnStars = str_repeat($ccnStar, 3) . str_repeat($ccnStarVoid, 2);
289
      } elseif($ccnRating == 2.5) {
290
        $ccnStars = str_repeat($ccnStar, 2) . str_repeat($ccnStarHalf, 1)  . str_repeat($ccnStarVoid, 2);
291
      } elseif($ccnRating == 2) {
292
        $ccnStars = str_repeat($ccnStar, 2) . str_repeat($ccnStarVoid, 3);
293
      } elseif($ccnRating == 1.5) {
294
        $ccnStars = str_repeat($ccnStar, 1) . str_repeat($ccnStarHalf, 1)  . str_repeat($ccnStarVoid, 3);
295
      } elseif($ccnRating == 0.5) {
296
        $ccnStars = str_repeat($ccnStarHalf, 1) . str_repeat($ccnStarVoid, 4);
297
      } else {
298
        $ccnStars = str_repeat($ccnStarVoid, 5);
299
      }
300
 
301
      $return = '<div class="ccn-external-stars">'.$ccnStars.'<li class="list-inline-item"><span>('.$this->count_ratings_external($courseID).')</span></li></div>';
302
      return $return;
303
 
304
    }
305
 
306
}