Databases: Database Access for a resource-Object

Databases: Database Access for a resource-Object

Thomas Temme -
回帖数:3

Hi,

First I want to say, that I'm new to Php-Programming. I'm just a Java-Developer.

I want to write a php-File, which saves an Resource-Object to the Database. I have seen, that therefore is the dmllib.php with the method insert_record, which needs as parameter the object. Is this right? Now I've tried to include the resource_base and resource_text - Classes in my php-File, to create a resource_text-Object. But then there comes the following Error:

"Fatal error: Call to undefined function set_config() in C:\Dokumente und Einstellungen\User\Desktop\MoodleWindowsInstaller-latest-19\server\moodle\mod\resource\lib.php on line 10"

The resource_base class is defined in lib.php

The thing that confuses me is, how I can import the resource_base - Class, in my PHP-File, whithout executing the code around the class in the "lib.php". Sorry for this question, but I'm actually programming Java and I do not know, how it works under PHP. It confuses me a bit, that there is code in lib.php around the resource_base-Class. Could someone help me?

Thanks

Thomas

回复Thomas Temme

Re: Databases: Database Access for a resource-Object

Tim Hunt -
Core developers的头像 Documentation writers的头像 Particularly helpful Moodlers的头像 Peer reviewers的头像 Plugin developers的头像
If you are unfamiliar with PHP, then the best way to create a new resource type is likely to be to copy and paste an existing one.

There ought to be a good how-to guide at http://docs.moodle.org/en/Development:Resource_types, but sadly no-one has written one yet.

Normally, at the top of each Moodle PHP file, you should start with require_once('../../config.php'); (with an appropirate number of ..s, and that will include all the Moodle standard libraries, which is wehre set_config is defined.

And normally, inclding lib.php form your resource class is the right thing to do, and the code in that file is there for a reason.
回复Thomas Temme

Re: Databases: Database Access for a resource-Object

Thomas Temme -

Thanks, that has helped.

Now it works and I can write a resource-Object to the database. But now I have the problem, that the resource isn't shown in a course. Therefore I've added a reference in mdl_course_modules, but that doesn't help.

Could you explain me, what I have to do, that the resource is shown in moodle?

I've seen, that in the table mdl_course is a field called "modinfo", where are some infos about the modules in it. Has it to do with this? Have I to write something in this column for my course?

Thanks!

Here is my code which adds the resource and the reference to a course:

<?php
         echo "<p>Der Code</p>\n";
   error_reporting(E_ALL);
   echo("test1 \n ");
    
     require_once('../config.php');
     require_once('../mod/resource/lib.php');
     require_once('../mod/resource/type/html/resource.class.php');
     require_once('../course/lib.php');

    

    $testresource = new resource_html();
     $testresource -> timemodified = time();
     $testresource -> name =  "neuer Name";
     $testresource -> type = "html";
     $testresource -> summary = "Meine Zusammenfassung";
     $testresource -> alltext = "Mein langer langer Artikel";
     $testresource -> course = 3;
    # $testresource -> reference = 3;
    

   insert_record("resource", $testresource);
    
    if (isset($SESSION->modform)) {   // Variables are stored in the session
        $mod = $SESSION->modform;
        unset($SESSION->modform);
    } else {
        $mod = (object)$_POST;
    }
    $mod -> course = 3;
    $mod -> module = 14;
    $mod -> instance = get_field("resource", "id", "name", $testresource -> name);
    echo($mod -> instance);
    $mod -> section = 3;
    $mod -> added = time();
    $mod -> visible = 1;
    $mod -> visibleold = 1;
    $mod -> idnumber = "";
    add_course_module($mod);
    
    
    
  
        
   echo("test2");
            ?>

回复Thomas Temme

Re: Databases: Database Access for a resource-Object

Tim Hunt -
Core developers的头像 Documentation writers的头像 Particularly helpful Moodlers的头像 Peer reviewers的头像 Plugin developers的头像
There is a function called something like rebuild_course_cache that you can call to rebuild modinfo.

I suggest you follow through exactly what code is executed when a use clicks Save on the add resource form, to see if there is anything else you need to do.