Proyectos de Subversion Moodle

Rev

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