/**
 * Updates the position and/or geometry of a stage in a road by updating 
 * treasurehunt_stages table. Then, check if road is valid.
 *
 * @see treasurehunt_set_valid_road()
 * @param Feature $feature The feature with the id, position, road and geometry of the stage.
 * @param stdClass $context The context object.
 */
function treasurehunt_update_geometry_and_position_of_stage(Feature $feature, $context)
{
    global $DB;
    $stage = new stdClass();
    $stage->position = $feature->getProperty('stageposition');
    $stage->roadid = $feature->getProperty('roadid');
    $geometry = $feature->getGeometry();
    $stage->geom = treasurehunt_geometry_to_wkt($geometry);
    $stage->timemodified = time();
    $stage->id = $feature->getId();
    $parms = array('id' => $stage->id);
    $entry = $DB->get_record('treasurehunt_stages', $parms, 'id,position', MUST_EXIST);
    // It can not be change the position of stage once the road is blocked.
    if (treasurehunt_check_road_is_blocked($stage->roadid) && $stage->position != $entry->position) {
        print_error('notchangeorderstage', 'treasurehunt');
    }
    // It can not be save an existing stage without geometry.
    if (count($geometry->getComponents()) === 0) {
        print_error('saveemptyridle', 'treasurehunt');
    }
    $DB->update_record('treasurehunt_stages', $stage);
    // Check if road is valid.
    treasurehunt_set_valid_road($stage->roadid);
    // Trigger update stage event.
    $eventparams = array('context' => $context, 'objectid' => $stage->id);
    \mod_treasurehunt\event\stage_updated::create($eventparams)->trigger();
}
                } else {
                    if (isset($answers)) {
                        // Elimino las anteriores respuestas.
                        foreach ($answers as $answer) {
                            $DB->delete_records('treasurehunt_answers', array('id' => $answer->id));
                        }
                    }
                }
                // Actualizo la etapa con los ficheros procesados.
                $DB->update_record('treasurehunt_stages', $stage);
                // Trigger event and update completion (if entry was created).
                $eventparams = array('context' => $context, 'objectid' => $stage->id);
                if ($isnewentry) {
                    $event = \mod_treasurehunt\event\stage_created::create($eventparams);
                } else {
                    $event = \mod_treasurehunt\event\stage_updated::create($eventparams);
                }
                $event->trigger();
                // Actualizo el tiempo de modificacion del camino.
                $road = new stdClass();
                $road->id = $stage->roadid;
                $road->timemodified = time();
                $DB->update_record('treasurehunt_roads', $road);
                redirect($returnurl);
            }
        }
    }
} else {
    $returnurl = new moodle_url('/mod/treasurehunt/view.php', array('id' => $cmid));
    print_error('treasurehuntislocked', 'treasurehunt', $returnurl, treasurehunt_get_username_blocking_edition($treasurehunt->id));
}