Friday, September 13, 2013

Custom moodle / php programming

When my customer asked me for the second time if I could change an enrollment date, I set off to build a little interface/function that would allow him to do it.  A quick review reminds me us that my enrollment report provides information regarding enrollment date and a couple calculated dates, along with some other info.  I very important business rule for my customer is the enrollment date for a student.  That is not supposed to ever change, but we know how that goes.  My quest then was to make the enrollment date a link that pulls up the current date and allows the user to change that.  Upon clicking the Update button, to have the date be converted from a string to a unixtime format, update the correct record and then reload the enrollment report.  In concept pretty simple and straight forward.  In implementation, a little more complexity.

I used these assets to help solve this requirement.
Moodle doc on update api
PHP strtotime documentation
redirect on cancel button using javascript

Here is my code that accomplished.  I created a page called updateEnrollDate.php and stored in the root of my plugin.  The index.php page is also located at the root of the plugin.

HTML


form method='post' action='updateEnrollDate.php'>
    input type='text' name='newdate' value=".$_REQUEST['edate']." size='8'>
    input type='hidden' name='checkit' value=".$_REQUEST['ueid'].">
    input type='submit' value='Update'>
    input type='button' value='Cancel' onclick='history.back()'>
/form>
Checking if form has been submitted.

if(isset($_REQUEST['checkit'])){
    //update the record
    updateDB($_REQUEST['checkit'],$_REQUEST['newdate']);
    //return to enroll report   
    header("Location: index.php");
}
 Calling function to update the DB, using moodle DB API.


function updateDB($ueid,$edate){
    global $DB;
    //echo "
in updateDB....";
    //var_dump($ueid);
    //var_dump($edate);
    $dataObj = new stdClass();
    $dataObj->id = $ueid;
    $unixconverted = strtotime($edate);
    $unixconvertedadjusted = strtotime('+1 day', $unixconverted);//why does my converted time come a day short? adding day
    $dataObj->timecreated = $unixconvertedadjusted;
    $table = 'user_enrolments';
   
    $DB->update_record($table, $dataObj);
   
}


Things that tripped me up while working through this?
  1. Using the $_REQUEST variable, I tried using $_FORM initially, did not work.
  2. Forgot to put the word global in front of $DB. errr
  3. Converting the $edate variable from a string entered in from to a unixtime format.
  4. putting mdl_ prefix in front of the table name for my $table variable.  The moodle API functions do not like that.
In the end, my customer should be happy and I wont have to do this for them.

Note: If I had been looking in my error log or if I had been echoing php errors to the console, I would have spent less time trying to figure out the steps listed above.  I get lazy and think the task is trivial, and do not thing to look in the logs.  Dummy!  Use the logs!



2 comments:

  1. can u post the whole code or send it via email

    ReplyDelete
  2. hi
    I want use function getAnswerText($aid,$qid,$sid)
    {
    $sql = "select
    a.answer
    FROM mdl_question_states s, mdl_question_answers a
    where a.question = s.question and a.id=$aid and a.question=$qid and s.id=$sid
    ";
    // $ars = dbCall($sql,false,'training_moodle');
    $ars = dbCall($sql,false,'moodle');
    return $ars->fields['answer'];
    }
    external php page how can achive this using moodle 1.9 v

    ReplyDelete