| 1452 | ariadna | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | require_once('../../config.php');
 | 
        
           |  |  | 4 |   | 
        
           |  |  | 5 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 6 |   | 
        
           |  |  | 7 | global $CFG, $DB, $COURSE;
 | 
        
           |  |  | 8 |   | 
        
           |  |  | 9 | $id = required_param('id', PARAM_INT);    // Course Module ID.
 | 
        
           |  |  | 10 | $rating = required_param('rating', PARAM_INT);  // User selection.
 | 
        
           |  |  | 11 |   | 
        
           |  |  | 12 | if (! $course = $DB->get_record('course', array('id' => $id))) {
 | 
        
           |  |  | 13 |     print_error('Course ID not found');
 | 
        
           |  |  | 14 | }
 | 
        
           |  |  | 15 |   | 
        
           |  |  | 16 | require_login($course, false);
 | 
        
           |  |  | 17 | if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
 | 
        
           |  |  | 18 |     print_error('nocontext');
 | 
        
           |  |  | 19 | }
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | require_capability('block/cesa_course_rating:rate', $context);
 | 
        
           |  |  | 22 | global $USER;
 | 
        
           |  |  | 23 |   | 
        
           |  |  | 24 | // Validación: Verificar si el usuario ya ha calificado este curso
 | 
        
           |  |  | 25 | if ($DB->record_exists('block_cesa_course_rating', array('course' => $COURSE->id, 'user' => $USER->id))) {
 | 
        
           |  |  | 26 |     redirect($CFG->wwwroot . '/course/view.php?id=' . $COURSE->id, get_string('already_rated', 'block_cesa_course_rating'), null, \core\output\notification::NOTIFY_WARNING);
 | 
        
           |  |  | 27 | }
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | if ($form = data_submitted()) {
 | 
        
           |  |  | 30 |     // Si el usuario no ha calificado, se inserta el registro
 | 
        
           |  |  | 31 |     $DB->insert_record('block_cesa_course_rating', array('course' => $COURSE->id, 'user' => $USER->id, 'rating' => $rating));
 | 
        
           |  |  | 32 |     redirect($CFG->wwwroot . '/course/view.php?id=' . $COURSE->id, get_string('rating_success', 'block_cesa_course_rating'), null, \core\output\notification::NOTIFY_SUCCESS);
 | 
        
           |  |  | 33 | }
 |