Tuesday, December 29, 2015

Things I built, once upon a time, using PHP and SQL

I have a "space" on one of our moodle sites.  It is a portal space, where we advertise our courses/services.  I built this space a few years back and of course, maintain it.  About once or twice a year, I go into this space to update some aspect of it.  Mostly, my customer contacts me with requests to make changes to it.   Included in this space is a staff area, where we introduce teachers and support staff who work on the project.  Standard stuff.  It looks like this when browsing it.

http://studentmoodle.accelerateu.org/student/portal/studentindex.php?action=staff

I did build into the page logic that presents edit buttons to privileged accounts.  This allows my customer to add/update a photo and a brief bio.  What the page does not do is allow for the addition or removal of new records.  Opps, a little lazy I suppose.  Consequently, my customer contacts me with removals, "they do not work for us anymore" and "we just hired so and so".

When it is time to add or remove a teacher, I go to the DB table that is feeding the page and add/remove the records.






This is a look at the table in its DB and the fields that I created in it.

I use a little SQL to create a statement to read this table and then a little PHP to display it on the page.





A little PHP on the view page that outputs the teachers info.

    include_once('../config.php');
    include_once('DAO.php');
      
    $teachers = getTeachers(); //this method is defined in the DAO.php file.
     
    $isadmin = false; //set the condition to false - so no one can see the edit buttons
   
    if(....'some condition that I just removed is true'... ){
        $isadmin = true; // update variable
    }
   
    $path = 'http://'.$_SERVER['SERVER_NAME'].'/student/portal/pathtophotos.../';
    //var_dump($path);

    foreach($teachers as $rec){   //loop over records          
        $rpath = $path.$rec->pic;
        

        echo '<div id="col1">
                <img src='.$rpath.' width="150" height="150">'; // set the photo dimensions
                if($isadmin){
                    echo " <br/>
                            <form action='somepage.php?action=someaction' method='POST'>
                                <input type='submit' value='Edit'>
                                <input type='hidden' name='id' value=".$rec->id.">
                            </form>
                        ";  
                }
        echo '</div>';  
      
        echo '<div id="col2">';
            echo '<b>'.$rec->fname.' '.$rec->lname.'</b>';  
            echo '<br/><a href="mailto:'.$rec->email.'">'.$rec->email.'</a>';                  
            echo '<br/>'.$rec->subjects;
            echo '<p></p>'.$rec->bio;
            echo '<hr>';
        echo '</div>';  
        echo '<div style="clear: both;"></div>';  
    }//foreach
A little SQL to get the teacher recs from DB.  $DB is defined and made avail by the moodle framework.
    function getTeachers(){
        global $DB;
        //echo "test";
       
        return
       
        $DB->get_records_sql('
            SELECT id, fname, lname, subjects, email, pic, bio
            FROM geniusintegration.teachers
           
            ORDER BY subjects, lname
           
        ');
    }

No comments:

Post a Comment