Add data to tables - javascript

this question is quite large, since what I would like to ask is how to add data to pd_procesos_acumulados.
See Picture of tables
I'm actually working on Symfony and Workbench (In case it helps).
Basically, what I have is id_proceso (from pd_procesos_disciplinarios and will be saved to id_proceso_acumulado), and the user must input the id_acto (which will be saved into nro_interno_acum). An example of the URL where I get the id is this: "proceso/acumulacion/133"
Here is the Controller AcumulacionController:
/**
* #param ProcesoDisciplinario $proceso
*
* #Route("/proceso/acumular/{id}", name="acumular_proceso")
* #Method("POST")
* #ParamConverter("proceso", class="ProcesoBundle:ProcesoDisciplinario")
*
* #return JsonResponse
*/
public function AcumularAction(int $acumula,ProcesoDisciplinario $proceso){
$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository('ProcesoBundle:ProcesoDisciplinario');
$procesado = $repository->findOneById($proceso); //el proceso al que se le atribuye
$procesoAcumula = $acumula->get('procesoAcumuladoInput'); //el proceso a atribuir
return array('procesados'=>$procesado, 'procesoAcumula'=>$procesoAcumula);
}
ProcesoDisciplinario $proceso is the id of the data I get from the website, and int $acumula is the id that the user must input.
Basically, i need the user to add as many id as it can, and all of them will have one same id_proceso_acumulado.
Here is the html:
<div class="col-lg-6">
<div class="input-group">
<form id="procesoAcumuladoForm">
<input type="text" class="form-control" placeholder="Número de proceso" name="procesoAcumuladoInput" id="procesoAcumuladoInput">
<span class="input-group-btn">
<button class="btn btn-default btn-success" type="button" name="procesoAcumuladoGuardar" id="procesoAcumuladoGuardar" href="/proceso/acumulacion/{id}">Agregar Proceso</button>
</span>
</form>
</div>
</div>
Preferably I would like that when the data is added to the table, the page wouldn't need to refresh to show changes.
The id of aa_actos_administrativos (named ActoAdministrativo):
/**
* #var integer
*
* #ORM\Column(name="id_acto", type="bigint", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
the id of pd_procesos_disciplinarios (named ProcesoDisciplinario)
/**
* #var integer
*
* #ORM\Column(name="id_proceso", type="bigint", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
In case i skipped something, please let me know. I know this is a big question, so please I beg your pardon in case it's too much. Thanks in advance!

Related

MethodNotAllowedHttpException using Href

I'm trying to verify if an api we use in work is working
with Href, using the route /test/intent/ it should execute the controller and check if its working or not
blade.php
#section('content')
<div class="container">
<div class="col-12 mt-5 text-center">
Test
</div>
</div>
#endsection
route:
Route::post('/test/intent', 'Testing\DebitTestController#intent');
the error we are getting is
MethodNotAllowedHttpException
/**
* Throw a method not allowed HTTP exception.
*
* #param array $others
* #return void
*
* #throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
*/
protected function methodNotAllowed(array $others)
{
throw new MethodNotAllowedHttpException($others);
}
/**
* Get routes from the collection by method.
*
* #param string|null $method
* #return array
*/
public function get($method = null)
{
return is_null($method) ? $this->getRoutes() : Arr::get($this->routes, $method, []);
}
/**
enter code here
You can use url() helper if you don't want to name the route for some reason:
<a href="{{ url('admin/views/query/new_query') }}"

Laralvel autocomplete input from database

Hello im learning Laravel and trying to make an autocompletion of search with sugestion from a database. I see a lot of people doing it with JQuery so that is what i've tried. But when i run the site, i only get one letter of suggestion, and sometimes no suggestion at all. I suspekt my routing is off so i tried dd some information in the query function, but that doesnt show up either,
MY BLADE
<!DOCTYPE html>
<html>
<head>
<title>Laravel 8 Autocomplete Search using Bootstrap Typeahead JS -ItSolutionStuff.com</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.1/bootstrap3-typeahead.min.js"></script>
</head>
<body>
<div class="container">
<h1>Laravel 8 Autocomplete Search using Bootstrap Typeahead JS - ItSolutionStuff.com</h1>
<input class="typeahead form-control" type="text">
</div>
<script type="text/javascript">
var path = "{{ route('autocomplete') }}";
$('input.typeahead').typeahead({
source: function (query, process) {
return $.get(path, { query: query }, function (data) {
return process(data);
});
}
});
</script>
</body>
</html>
MY CONTROLLER
class TypeaheadController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('welcome');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function autocomplete(Request $request)
{
dd('h');
$data = Edit::select("Adresse")
->where("Adresse","LIKE","%{$request->query}%")
->get();
return response()->json($data);
}
}
MY Routing
Route::get('search', [TypeaheadController::class, 'index'])->name('search');
Route::get('autocomplete', [TypeaheadController::class, 'autocomplete'])->name('autocomplete');
If you guys have any other suggestion on how i can do it. feel free to suggest
$request->query is a reserved function on the $request object. in order to pull the '?query=xyz' from the url in the request you would have to use the $request->query('query') function to return xyz.

JavaScript: getElementById() not working (with either table or paragraph that previously access database)

I'm stuck on a problem where i want to retrieve a html-element for use in javascript. I'm trying to use the function getElementById(), whick did/does work another element (a shopping cart in which items are placed by clicking on a picture), but not for the ones i'm currently working on (a table (or after i gave up on that a paragraph) that holds some information via "data-"-attributes (e.g. data-price) (so that those don't have to be passed/accessed via json etc)).
I always get the error that the variable i'm assigning the gotten Element to is null ("TypeError: datentabelle is null").
This is the full .js-file called "MyJS.js":
function addpizza( pizzanummer, pizzaname, preis) {
"use strict";
var warenkorb = document.getElementById("warenkorb");
var opt = document.createElement('option');
opt.value = pizzanummer;
opt.label = pizzaname;
//opt.data-datapreis = preis;
warenkorb.appendChild(opt);
var para = document.getElementById("paragraphid");
let bisherpreis = para.textContent;
let neupreis = parseFloat(bisherpreis) + parseFloat(preis);
var opt = document.createElement('option');
para.textContent = neupreis.toFixed(2);
var datentabelle = document.getElementById(0); !!This is were the assignment is supposed to happen!!
var aktpreis = datentabelle.data-preis; !!This is were the error is thrown (when "datentabelle" is first accessed)!!
//calc();
}
/*
function calc(){
//no useable body currently
}
*/
This is an excerpt of the HTML-elements code/construction
foreach($dbergebnisse as $pizza){
$aktpizzanummer = $pizza["PizzaNummer"];
$aktname = $pizza["PizzaName"];
$aktpreis = $pizza["Preis"];
$aktbilddatei = $pizza["Bilddatei"];
echo <<< test10
<p id=$aktpizzanummer data-preis=$aktpreis> idptemp </p>
<p>$aktname</p>
<p>$aktpreis €</p>
<img src=$aktbilddatei alt="margherita" width="80" height="80" onclick="addpizza($aktpizzanummer,'$aktname',$aktpreis)">
test10;
and this is the corresponding full .php-file if relevant:
<?php // UTF-8 marker äöüÄÖÜ߀
/**
* Class PageTemplate for the exercises of the EWA lecture
* Demonstrates use of PHP including class and OO.
* Implements Zend coding standards.
* Generate documentation with Doxygen or phpdoc
*
* PHP Version 5
*
* #category File
* #package Pizzaservice
* #author Bernhard Kreling, <b.kreling#fbi.h-da.de>
* #author Ralf Hahn, <ralf.hahn#h-da.de>
* #license http://www.h-da.de none
* #Release 1.2
* #link http://www.fbi.h-da.de
*/
// to do: change name 'PageTemplate' throughout this file
require_once './Page.php';
/**
* This is a template for top level classes, which represent
* a complete web page and which are called directly by the user.
* Usually there will only be a single instance of such a class.
* The name of the template is supposed
* to be replaced by the name of the specific HTML page e.g. baker.
* The order of methods might correspond to the order of thinking
* during implementation.
* #author Bernhard Kreling, <b.kreling#fbi.h-da.de>
* #author Ralf Hahn, <ralf.hahn#h-da.de>
*/
class Bestellung extends Page
{
// to do: declare reference variables for members
// representing substructures/blocks
/**
* Instantiates members (to be defined above).
* Calls the constructor of the parent i.e. page class.
* So the database connection is established.
*
* #return none
*/
protected function __construct()
{
parent::__construct();
// to do: instantiate members representing substructures/blocks
}
/**
* Cleans up what ever is needed.
* Calls the destructor of the parent i.e. page class.
* So the database connection is closed.
*
* #return none
*/
protected function __destruct()
{
parent::__destruct();
}
/**
* Fetch all data that is necessary for later output.
* Data is stored in an easily accessible way e.g. as associative array.
*
* #return none
*/
protected function getViewData()
{
// to do: fetch data for this view from the database
$angebot = array();
$SQLabfrage = "SELECT * FROM angebot";
$Recordset = $this->_database ->query ($SQLabfrage);//$Recordset = $this->_database ->query ($SQLabfrage); //wird hier jetzt iwie einfach ein recordset deklariert (ohne klasse usw?)
if (!$Recordset)
throw new Exception("Query failed: ".$Connection->error);
//return $Recordset;
$counter = 0;
$pizza = array(); //konnte man iwie net drinnen definieren??
while ($record = $Recordset->fetch_assoc()){
$pizza["PizzaNummer"] = $record["PizzaNummer"];
$pizza["PizzaName"] = $record["PizzaName"];
$pizza["Preis"] = $record["Preis"];
$pizza["Bilddatei"] = $record["Bilddatei"];
//$angebot[0] = $pizza;
array_push($angebot, $pizza);
}
return $angebot;
}
/**
* First the necessary data is fetched and then the HTML is
* assembled for output. i.e. the header is generated, the content
* of the page ("view") is inserted and -if avaialable- the content of
* all views contained is generated.
* Finally the footer is added.
*
* #return none
*/
protected function generateView() //warum geht $record (in schleife) benutzen hier nicht, aber in getviewdata schon??
{
$dbergebnisse = $this->getViewData();
//$record = $this->$Recordset;
//$aktnummer = $record["PizzaNummer"];
$this->generatePageHeader('Bestellung');
// to do: call generateView() for all members
//<form action="https://echo.fbi.h-da.de/" method="POST">
//<form action="http://127.0.0.1/pizzaservice/Kundeerbend.php" method="POST"> //original
//$_SESSION["testwert"] = "bla";
//<form action="http://127.0.0.1/pizzaservice/Kundeerbend.php" method="POST">
echo <<< test1
<body>
<form action="http://127.0.0.1/pizzaservice/ewa/Kundeerbend.php" method="POST">
<h2> Bestellung</h2>
<h2> Speisekarte</h2>
test1;
foreach($dbergebnisse as $pizza){
$aktpizzanummer = $pizza["PizzaNummer"];
$aktname = $pizza["PizzaName"];
$aktpreis = $pizza["Preis"];
$aktbilddatei = $pizza["Bilddatei"];
echo <<< test10
<p id=$aktpizzanummer data-preis=$aktpreis> idptemp </p>
<p>$aktname</p>
<p>$aktpreis €</p>
<img src=$aktbilddatei alt="margherita" width="80" height="80" onclick="addpizza($aktpizzanummer,'$aktname',$aktpreis)">
test10;
}
/*
echo <<< test1
<body>
<form action="http://127.0.0.1/pizzaservice/Kundeerbend.php" method="POST">
<h2> Bestellung</h2>
<h2> Speisekarte</h2>
*/
/*
<!-- Pizzen aus Datenbank -->
<img src="ewabild.jpg" alt="margherita" width="80" height="80">
<p>Margherita</p>
<p>4.00€</p>
<img src="ewabild.jpg" alt="salami" width="80" height="80">
<p>Salami</p>
<p>4.50€</p>
<img src="ewabild.jpg" alt="hawaii" width="80" height="80">
<p>Hawaii</p>
<p>5.50€</p>
*/
$angebotsgroesse = count($dbergebnisse); //kein code in ausgabe!! (also nicht nach echo)
echo <<< test1
<h2>Warenkorb</h2>
<select name="top[]" id= "warenkorb" size=" $angebotsgroesse " multiple>
test1;
foreach($dbergebnisse as $pizza){
$aktpizzanummer = $pizza["PizzaNummer"];
$aktname = $pizza["PizzaName"];
$aktpreis = $pizza["Preis"];
$aktbilddatei = $pizza["Bilddatei"];
echo <<< test1
<option value = "$aktpizzanummer"> $aktname </option>
test1;
}
echo "</select>";
/*
echo <<< test2
<table name = "blatabelle" id="infoetc" size=" $angebotsgroesse " multiple>
<tr>
<td>"bla</td>
</tr>
test2;
foreach($dbergebnisse as $pizza){
$aktpizzanummer = $pizza["PizzaNummer"];
$aktname = $pizza["PizzaName"];
$aktpreis = $pizza["Preis"];
$aktbilddatei = $pizza["Bilddatei"];
echo <<< test1
<tr id = "$aktpizzanummer">
<td data-name>$aktname</td>
<td data-preis>$aktpreis</td>
</tr>
test1;
}
echo "</table>";
*/
echo <<< test1
<p id="paragraphid">0</p>
<input type="hidden" name="gesamtpreis" value="14.50" />
<p><label>
<input type="text" name="Zuname" placeholder="Ihr Nachname"/>
</label></p>
<p><label>
<input type="text" name="kundenadresse" placeholder="Ihre Adresse"/>
</label></p>
<input type="reset" name="Alle löschen" value="Alle Löschen" />
<input type="button" name="Auswahl löschen" value="Auswahl Löschen" />
<input type="submit" name="bestellen" value="Bestellen" />
</form>
<script type ="text/javascript" src="MyJS.js"></script>
</body>
test1;
// to do: output view of this page
$this->generatePageFooter();
}
/**
* Processes the data that comes via GET or POST i.e. CGI.
* If this page is supposed to do something with submitted
* data do it here.
* If the page contains blocks, delegate processing of the
* respective subsets of data to them.
*
* #return none
*/
protected function processReceivedData()
{
parent::processReceivedData();
// to do: call processReceivedData() for all members
}
/**
* This main-function has the only purpose to create an instance
* of the class and to get all the things going.
* I.e. the operations of the class are called to produce
* the output of the HTML-file.
* The name "main" is no keyword for php. It is just used to
* indicate that function as the central starting point.
* To make it simpler this is a static function. That is you can simply
* call it without first creating an instance of the class.
*
* #return none
*/
public static function main()
{
//session_start();
try {
$page = new Bestellung();
$page->processReceivedData();
$page->generateView();
}
catch (Exception $e) {
header("Content-type: text/plain; charset=UTF-8");
echo $e->getMessage();
}
}
}
// This call is starting the creation of the page.
// That is input is processed and output is created.
Bestellung::main();
// Zend standard does not like closing php-tag!
// PHP doesn't require the closing tag (it is assumed when the file ends).
// Not specifying the closing ? > helps to prevent accidents
// like additional whitespace which will cause session
// initialization to fail ("headers already sent").
//? >
Thanks for any help
You should refer to data-preis via dataset,
Cou your code will be smth like:
function addPizza() {
/* Your logic */
var datentabelle = document.getElementById(0);
var aktpreis = datentabelle.dataset.preis;
}

Setting a Quiz System

I'm developing a MCQs based Quiz system where my goal is to assist teacher in adding a new Question and Choices for that question on the same page. According to Symfony documentation, I can embed a Collection of Forms, so I tried embedding ChoiceType to Question form:
->add('answers', CollectionType::class, array(
'entry_type' => ChoiceType::class,
'allow_add' => true,
));
;
Code of new.html.twig page (new question):
<label> Choose answers : </label>
<ul class="tags" data-prototype="{{form_widget(form.answers.vars.prototype)|e('html_attr') }}">
</ul>
But I'm getting empty select input in the browser. Please suggest what could be the perfect solution in this regard?
Note:
I noticed that if I add
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
to my QuestionType I get the form with an empty select in new.html.twig
when I delete this import I get this error if I open new.html.twig :
Variable "expanded" does not exist in form_div_layout.html.twig at line 38
but I don't have any variable in my entities called 'expanded'
Choice Entity
class Choice
{
...
/**
* #var string
*
* #ORM\Column(name="answer", type="text")
*/
private $answer;
/**
* #var string
*
* #ORM\Column(name="correct", type="string", length=255)
*/
private $correct;
/**
* #var
* #ORM\ManyToOne(targetEntity="ChallengeBundle\Entity\Question",inversedBy="answers")
*/
private $question;
...
}
Question Entity:
class Question
{
...
/**
* #var
* #ORM\OneToMany(targetEntity="ChallengeBundle\Entity\Choice",mappedBy="question",cascade={"remove"})
*/
private $answers;
...
}
Choice Type:
class ChoiceType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('answer')
->add('correct')
;
}
/**
* #param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'ChallengeBundle\Entity\Choice'
));
}
}
1- If your goal is only to select existing Answers in Choice form you have to use EntityTypeField instead of CollectionTypeField in your ChoiceFormType :
->add('answers', EntityType::class, array(
// query choices from this entity
'class' => 'YourBundle:Question',
// use the Question.name property as the visible option string
'choice_label' => 'name',
// used to render a select box, check boxes or radios
// 'multiple' => true,
// 'expanded' => true,
));
2- But if you want to add new answers in your Choice form you have to keep CollectionTypeField as you do.
Then in you twig template when you render your Choice form you can call your Answer collection like this :
<ul class="answers" data-prototype="{{ form_widget(form.answers.vars.prototype)|e('html_attr') }}">
{% for answer in form.answers %}
<li>{{ form_row(answer.answer) }}</li>
<li>{{ form_row(answer.correct) }}</li>
{% endfor %}
</ul>
This will display first inputs empty
Finally, as the documentation says, you have to add some javascript to read html in data-prototype attribute and dynamically add new answer forms when you click a "Add a new answer" link.
Doc example (you just have to adapt this to your case) :
var $collectionHolder;
// setup an "add a tag" link
var $addTagLink = $('Add a tag');
var $newLinkLi = $('<li></li>').append($addTagLink);
jQuery(document).ready(function() {
// Get the ul that holds the collection of tags
$collectionHolder = $('ul.tags');
// add the "add a tag" anchor and li to the tags ul
$collectionHolder.append($newLinkLi);
// count the current form inputs we have (e.g. 2), use that as the new
// index when inserting a new item (e.g. 2)
$collectionHolder.data('index', $collectionHolder.find(':input').length);
$addTagLink.on('click', function(e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();
// add a new tag form (see next code block)
addTagForm($collectionHolder, $newLinkLi);
});
});
There is an excellent bundle for manage the symfony embedded forms and prototype. You don't need to code the js on hand and has a lot of options. Check in here.
Hope this help you.
You need to add the JavaScript as outlined in the documentation section Allowing "new" Tags with the "Prototype".
Excerpt from the docs:
The actual code needed to make this all work can vary quite a bit, but here's one example:
function addTagForm($collectionHolder, $newLinkLi) {
// Get the data-prototype explained earlier
var prototype = $collectionHolder.data('prototype');
// get the new index
var index = $collectionHolder.data('index');
// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.replace(/__name__/g, index);
// increase the index with one for the next item
$collectionHolder.data('index', index + 1);
// Display the form in the page in an li, before the "Add a tag" link li
var $newFormLi = $('<li></li>').append(newForm);
$newLinkLi.before($newFormLi);
}
var $collectionHolder;
// setup an "add a tag" link
var $addTagLink = $('Add a tag');
var $newLinkLi = $('<li></li>').append($addTagLink);
jQuery(document).ready(function() {
// Get the ul that holds the collection of tags
$collectionHolder = $('ul.tags');
// add the "add a tag" anchor and li to the tags ul
$collectionHolder.append($newLinkLi);
// count the current form inputs we have (e.g. 2), use that as the new
// index when inserting a new item (e.g. 2)
$collectionHolder.data('index', $collectionHolder.find(':input').length);
$addTagLink.on('click', function(e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();
// add a new tag form (see next code block)
addTagForm($collectionHolder, $newLinkLi);
});
});
Thank you all for your Help
I solve my problem by creating another entity It seem that symfony framework find a confusion in the name of my form ChoiceType and Symfony\Component\Form\Extension\Core\Type\ChoiceType

Add link on course page depending on course custom field

I am using Moodle 2.7 and have the following custom field for the courses in the database table mdl_course_info_field:
Full name: School course
Shrot name: school
Type: Menu of choices
Choices:
Highschool course
Prepschool course
The target was to show the link on every course page, where under the settings the chechbox for Highschool course is used. In the file mymoodle/local/link/functions.js there is the link:
if($('#page-course-view-topcollmytheme .orangebar p')) {
$('#page-course-view-topcollmytheme #section-0 .content > .summary').append('<button class="highschoollink">Hig school course</button>');
}
How to check, if the checkbox is choosen an then to show the link on the course page?
You can use a renderer to display a course header:
https://tracker.moodle.org/browse/MDL-36048
So you could include the school link in the course header - here is an example:
In /course/format/formatname/lib.php
Add this function to class format_formatname
/**
* Display's a header at the top of the sections.
*
* #return renderable class
*/
public function course_content_header() {
global $DB, $PAGE, $USER;
if (!isset($PAGE)) {
return null;
}
// Only display if we are on the course-view page.
if (strpos($PAGE->pagetype, 'course-view-') !== 0) {
return null;
}
$sql = "SELECT d.data
FROM {course_info_field} f
JOIN {course_info_data} d ON d.fieldid = f.id AND d.courseid = :courseid
WHERE f.shortname = :shortname";
$params = array('courseid' => $this->courseid, 'shortname' => 'school');
$schoolname = $DB->get_field_sql($sql, $params);
$schoolurl = '';
// You should store the school url in the database somewhere.
// Using switch code for this example.
switch ($schoolname) {
case 'high school' :
$schoolurl = new moodle_url('http://www.schoolsite.com');
break;
...
}
return new format_formatname_coursecontentheader($schoolname, $schoolurl);
}
Also add this class to /course/format/formatname/lib.php
class format_formatname_coursecontentheader implements renderable {
/**
* School name
*
* #var string $schoolname
*/
public $schoolname;
/**
* School url
*
* #var string $schoolurl
*/
public $schoolurl;
/**
* Class storing information to be passed and displayed in the course content header
*
* #param string $schoolname
* #param moodle_url $schoolurl
*/
public function __construct($schoolname, $schoolurl) {
$this->schoolname = $schoolname;
$this->schoolurl = $fields->schoolurl;
}
}
Then in /course/format/formatname/renderer.php
Add this function to class format_formatname_renderer
/**
* Renders course header
*
* #param renderable $courseheader
* #return string
*/
public function render_format_formatname_coursecontentheader($courseheader) {
$output = '';
$schoolname = $courseheader->schoolname;
$schoolurl = $courseheader->schoolurl;
$link = html_writer::link($schoolurl, $schoolname);
$output .= html_writer::div($link, 'format-formatname-schoollink');
return $output;
}
I suggest you add the link to the course as a URL resource.
Then make sure 'conditional activities' are enabled for the site.
Finally edit the URL activity to restrict access to users who have the correct user profile field.
See https://docs.moodle.org/27/en/Conditional_activities_settings for more details.

Categories