Proyectos de Subversion Moodle

Rev

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