referring to the current course and database connection

referring to the current course and database connection

by Mark Tyers -
Number of replies: 6
bit of a newbie so please forgive the basic questions.

I have started writing code in the mod.html file for my new module and have two questions:

There must be a database connection already open - how do I use it to query the database ( I have created a new connection with hard-coded values to get started)...

I need to retrieve the current course id to use in my query - how should I be doing this?

Many thanks in advance.

Here is the code I added:

<!-- More rows go in here... -->
<tr>
<td colspan="2"><strong>Description</strong></td>
</tr>
<tr>
<td colspan="2"><textarea name="description" rows="8" cols="60"></textarea></td>
</tr>

<?php
$conn = mysql_connect("localhost", "moodleuser", "moodlepwd") or die ("could not connect");
mysql_select_db("moodle");
$sql = "SELECT id, short_name, long_name FROM coursework_criteria where course = 11";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
  $text = $row['short_name'] . ": " . $row['long_name'];
if ( strlen($text) > 72) $text = substr($text, 0, 72) . "...";
  $dropdown .= "<option value="" . $row['id'] . "">" . $text . "</option>";
}
?>
<tr><td colspan="2"><strong>Select Criteria</strong></td></tr>
<?php
for ($i=0; $i<6; $i++) {
  echo "<tr><td colspan="2"><select name="criteria$i"><option value="">PLEASE CHOOSE</option>$dropdown</select>";
}

?>
 

(Edited by <a href="http://moodle.org/user/view.php?id=1&course=5">Martin Dougiamas</a> -  Friday,  31 March 2006, 09:45 AM)
Average of ratings: -
In reply to Mark Tyers

Re: referring to the current course and database connection

by Mark Tyers -
have figured how to refer to the current course, but still struggling over how to interact with the database without using another DB connection.

I have since tried to get the save changes button to add the instance of my 'coursework' module but get an error:

Could not add a new instance of coursework

I notice a function called insert_record();

is this function pre-defined or will I need to write it myself?




My plan is (was) to :
  1. display a custom form when the user adds an instance (virtually done - just the DB code to tidy up)

  2. Be able to add an instance to my course - and delete it again (without adding any custom data to my tables)

  3. Add custom code to write the data to the appropriate tables and delete it again when the course is deleted.

  4. Create the form that the students will see

  5. get the form to add the appropriate data to the DB (will also need to get them to submit a file

  6. create the assessment screen

  7. create the summary screen


My PHP skills are pretty good (although have not worked with OOP much

My problems stem from not understanding the Moodle API

Are there any posts with an explanation / walkthrough of how to use this?

many thanks

Mark
In reply to Mark Tyers

Re: referring to the current course and database connection

by Dan Stowell -
Mark - Use Moodle's built-in functions for all database queries. They automatically handle special things like different database types, different table prefixes, etc.

If you're OK with reading PHP, then just look at the file lib/datalib.php and you'll see what all the functions do.

For example, to insert a field into the (imaginary) widget table:

$widget->name='Widget number one';
$widget->content='Blah blah';
insert_record('widgets', $widget);

Moodle uses this object-oriented way of inserting and retrieving database records. It's very nice not to have to mess around with the DB connection etc!
In reply to Dan Stowell

Re: referring to the current course and database connection

by Dan Stowell -
In reply to Dan Stowell

Re: referring to the current course and database connection

by Mark Tyers -
thanks Dan

just the info I needed

smile
In reply to Mark Tyers

Re: referring to the current course and database connection

by Mark Tyers -
Here is my modified code:

<?php
$course = $form->course;
$sql = "SELECT id, short_name, long_name FROM mdl_coursework_criteria where course = $course;";
$data = get_records_sql($sql);

foreach ($data as $row) {
    $text = $row->short_name . ": " . $row->long_name;
    if ( strlen($text) > 72) $text = substr($text, 0, 72) . "...";
    $dropdown .= "<option value=\"" . $row->id . "\">" . $text . "</option>";
}
?>