New plugin problem

New plugin problem

by Ангел Бачев -
Number of replies: 0

Hello, guys.

I'm new to Moodle development.

I've got a university project - to develop a Moodle plugin for managing thesis topics.

 

I had read tutorials for blocks, modules, Forms, DB and a lot more in  this  site. I understood some things but a lot are still unclear for me.

 

I decided to create a list block which is displayed on Front page, and depending from user, different options are shown. 

If user is teacher then he has 3 options - Add new topic, View topics, and

View applications.

So with the Add new topic I have no problems. But with the second one I've got a big trouble.

On the second one user has search box, with some fields which he can search by and Initialize  and Seach buttons. If he clicks on Initialize button all the data is cleared.When he enters the search criteria, and

clicks on Search button a database request is done and a table with results is shown. Each result is displayed on a separate row and each row has Edit and Delete buttons.

I want when user clicks on Delete button the selected row to be deleted from database, and the row to be removed from search results.

When the Delete button is pressed the record is deleted form database, but all the results form previos search are lost and no results are shown.

How can I keep the previous results.

 

Here is the code of the page for viewing topics

//view_topics.php

 

//<?php

require_once('../../config.php');
require_once('lib.php');
require_once('view_topics_form.php');
require_once('search_topics_form.php');

require_login();

$topic_id = optional_param('topic_id', 0, PARAM_INT);
$action = optional_param('action', ' act', PARAM_ALPHA);

$blockname = get_string('pluginname', 'block_thesis_topics');
$header = get_string('viewtopics', 'block_thesis_topics');

$title = "{$blockname}: {$header}";

$context = get_context_instance(CONTEXT_SYSTEM);

$PAGE->set_context($context);
$PAGE->set_url('/blocks/thesis_topics/view_topics.php');
$PAGE->navbar->add($blockname);
$PAGE->navbar->add($header);
$PAGE->set_title($title);
$PAGE->set_heading($title);
$PAGE->set_pagetype($blockname);

$search_form = new search_topics_form(null, null);

if (empty($topics))
$topics = array();

if ($search_form->is_cancelled()) {
redirect(new moodle_url('view_topics.php'));
} else if ($search_form->is_submitted()) {
$data = $search_form->get_data();
$title = $data->title;
$only_mine = $data->only_mine;
$teacher = $data->teacher;
$status = $data->status;
$constraints = array('title' => $title, 'only_mine' => $only_mine, 'teacher' => $teacher, 'status' => $status);
$topics = get_topics($constraints);
}

echo $OUTPUT->header();
echo $OUTPUT->heading($header);

$search_form->display();


if ($action == "delete") {
delete_topic($topics, $topic_id);
}

if (!empty($topics))
echo print_topics($topics);


echo $OUTPUT->footer();

 

//search_topics_form.php

<?php

require_once($CFG->libdir . '/formslib.php');

require_once( 'lib.php');

class search_topics_form extends moodleform {

public function definition() {

global $USER;

$mform = & $this->_form;

$mform->addElement('header', 'search_criteria', get_string('searchcriteria', 'block_thesis_topics'));
$mform->addElement('text', 'title', get_string('caption', 'block_thesis_topics'));
$mform->addElement('advcheckbox', 'only_mine', get_string('onlymine', 'block_thesis_topics'), '', array('group' => 1), array(false, true));
$mform->addElement('text', 'teacher', get_string('teacher', 'block_thesis_topics'));
$status_selection = get_status_lst();
$mform->addElement('select', 'status', get_string('status', 'block_thesis_topics'), $status_selection);
$mform->setDefault('status', '');

$buttons = array(
$mform->createElement('submit', 'search', get_string('search', 'block_thesis_topics')),
$mform->createElement('cancel', 'init', get_string('init', 'block_thesis_topics'))
);

$mform->addGroup($buttons, 'buttons', get_string('actions', 'block_thesis_topics'), array(' '), false);
$mform->closeHeaderBefore('results');
}

}

//Function get_topics

function get_topics($constraints) {
global $DB, $USER;

$where_clause = "";
if (($title = $constraints['title']) != '') {
$where_clause = "t.title like '%" . $title . "%'";
}
if (($only_mine = $constraints['only_mine'])) {
$constraint = "t.teacherid=" . $USER->id;
if ($where_clause == "")
$where_clause = $constraint;
else
$where_clause = $where_clause . ", " . $constraint;
} else if (($teacher = $constraints['teacher']) != '') {
$constraint = "u.firstname like '%" . $teacher . "%' OR u.lastname like '%" . $teacher . "%'";
if ($where_clause == "")
$where_clause = $constraint;
else
$where_clause = $where_clause . ", " . $constraint;
}
if (($status = $constraints['status']) != '') {
$constraint = "t.status='" . $status . "'";
if ($where_clause == "")
$where_clause = $constraint;
else
$where_clause = $where_clause . ", " . $constraint;
}

if ($where_clause != "")
$where_clause = " WHERE " . $where_clause;


$sql = "SELECT t.id, t.title, u.firstname, u.lastname, t.status
FROM {block_thesis_topics} t
JOIN {user} u ON t.teacherid = u.id" . $where_clause;


$records = $DB->get_records_sql($sql, array());

return $records;
}

//print_topics

function print_topics($topics) {
$number_lbl = get_string('number', 'block_thesis_topics');
$topic_lbl = get_string('topic', 'block_thesis_topics');
$teacher_lbl = get_string('teacher', 'block_thesis_topics');
$status_lbl = get_string('status', 'block_thesis_topics');
$edit_lbl = get_string('edit', 'block_thesis_topics');
$delete_lbl = get_string('delete', 'block_thesis_topics');
$result_table = '
<h1>Results</h1>
<table>
<tr>
<th>' . $number_lbl . '</th>
<th>' . $topic_lbl . '</th>
<th>' . $teacher_lbl . '</th>
<th>' . $status_lbl . '<th>
<th colspan="2">&nbsp;</th>
</tr>
';
$number = 1;
foreach ($topics as $topic) {
$result_table.= '<tr><td>' . $number++ . '</td>';
$result_table.= '<td>' . $topic->title . '</td>';
$result_table.= '<td>' . $topic->firstname . ' ' . $topic->lastname . '&nbsp;</td>';
$result_table.= '<td>' . $topic->status . '&nbsp;</td>';
$topic_id = $topic->id;
//The loop is left open
$result_table.= '
<td>
<form name="edit_topic" action="view_topics.php" method="POST">
<input type="hidden" name="topic_id" value="' . $topic_id . '"/>
<input type="submit" name="edit_topic" value="' . $edit_lbl . '"/>
</form>
</td>
<td>
<form name="delete_topic" action="view_topics.php" method="POST">
<input type="hidden" name="topic_id" value="' . $topic_id . '"/>
<input type="hidden" name="action" value="delete"/>
<input type="submit" name="delete_topic" value="' . $delete_lbl . '"/>
</form>
</td></tr>
';
}
$result_table.="</table>";

return $result_table;
}

 

//delete_topic

function delete_topic($topics, $topic_id) {
global $DB;

$table = 'block_thesis_topics';
$conditions = array('id' => $topic_id);
$DB->delete_records($table, $conditions);
$topics_new = array();
foreach ($topics as $topic) {
if ($topic->id <> $topic_id) {
$topics_new[] = $topic;
}
}
$topics = $topics_new;
}

 

 

Please, give me advice what to do in order when I click on delete button not to lose results from previous search.

Average of ratings: -