Proyectos de Subversion Moodle

Rev

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