update assign table

update assign table

by Dana T. -
Number of replies: 16

Hello everyone,

I might be wrong using this thread, since I haven't got the solution I am coming back to you.

I am still working on this code, any one who can tell me what is wrong, I really appreciate it. My block plugin is to take the time selected from datetimeselector and insert into the database assignment table, So that the duedate for assignments will be changed using the block.

The code below is in my edit.php

 

if ($fromform =$mform->get_data()) {
      $mytime = $fromform->selecttime;

        $record = new stdClass();
        $record->duedate = $mytime;
        $record=$attempt->id;
        $record->id = $attemptid;
        $record->course = $courseid ;
        $record->duedate         = $mform;
        $record->intro         = get_string('databasename', 'block_daterollover');
        $record->allowsubmissionsfromdate = $mform;
        $record->timemodified = time();
        $DB->update_record('assign', $record);
        
}
 else
 {
   $mform->display();
}

Average of ratings: -
In reply to Dana T.

Re: update assign table

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Dana,

I suggest you carefully read back through the code you've posted, there are several parts of it which don't quite make sense.

e.g.

$record = new stdClass();
...
$record = $attempt->id; // This line will delete the whole of the $record object and replace it with the $attempt->id value

$record->duedate = $mform;  // This replaces the due date with a form object (the due date should be an integer timestamp, not an object), maybe you meant $fromform->duedate ?

You might also want to consider making sure you are only including the values you want to change in the $record object, any you leave out will just stay at their current value in the database.

 

Average of ratings: Useful (1)
In reply to Davo Smith

Re: update assign table

by Dana T. -

Hi Davo,

Thank your for looking at my problem , I was trying just to take the time. U got what I needed to do, I only wanted to change the duedate current course assignment. I want to pass the value  $fromform->duedate, like you said

I even tried the following

$unixconverted->strtotime($data);
$record->timedue         = $unixconverted;

I think the value $fromform is already in timstamp form. I have provided my whole code in a recent thread.

 https://moodle.org/mod/forum/discuss.php?d=259556

In reply to Dana T.

Re: update assign table

by Dana T. -

Hi again,

  
       $record = new stdClass();
        $record->course = $courseid ;//for table identification
        $record->duedate         = $fromform->duedate;
        $currentime =time();
        $record->allowsubmissionsfromdate = $currentime;
        $record->timemodified = $currentime;
        $DB->update_record('assign', $record);

 

do you think this might be correct, I used $record->course = $courseid ;//for table identification, pls give some comments

In reply to Dana T.

Re: update assign table

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If you make sure Debugging is set to 'developer', then you will quickly see that this will not work - you will get an error about "missing 'id' field" (or something like that).

When calling update_record, you must include the 'id' field, or Moodle doesn't know which record to update.

If you don't know the id of the record you want to edit then you need to do a search to find it.

e.g.

$assignments = $DB->get_records('assign', array('course' => $courseid));
foreach ($assignments as $assignment) {
    $record = new stdClass();
    $record->id = $assignment->id;
    $record->duedate = $fromform->duedate;
...
    $DB->update_record('assign', $record);
}

 

Average of ratings: Useful (1)
In reply to Davo Smith

a required parameter (courseid) was missing

by Dana T. -

I was trying with the suggestion but I keep getting this debug info.

A required parameter(courseid) was missing

Debug info:

Error code:missingparam

stack trace:

-line 463 of /lib/setuplib.php:moodle_exception thrown

-line 545 of /lib/moodlelib.php:call to print_errot()

-line 11 of /blocks/daterollover/edit.php:call to required_param()

 

 

Here is the edit.php

<?php
 
require_once('../../config.php');
require_once('daterollover_form.php');
require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->dirroot.'/blocks/daterollover/daterollover_form.php');
 
     global $DB, $OUTPUT, $PAGE, $COURSE;
     require_login($SITE);
    $courseid = required_param('courseid',PARAM_INT);//line 11
    $coursecontext = context_course::instance($course->id);
    require_capability('block/daterollover:update', $coursecontext);
    block_load_calss('daterollover');
   $block = new block_daterollover();    
    $mform = new daterollover_form();

 if ($fromform =$mform->get_data()) {
        $assignments = $DB->get_records('assign',array('course'=> $courseid));
        foreach($assignments as $assignment){
        $record = new stdClass();
        $record->id = $assignment->id;
        $record->duedate         = $fromform->duedate;
        $DB->update_record('assign', $record);
    }
}
 redirect($CFG->wwwroot.'/course/view.php?id='.$courseid, '', 0);

In reply to Dana T.

Re: a required parameter (courseid) was missing

by triveni mergu -

Hi,

last line of your code should be:

redirect(new moodle_url(($CFG->wwwroot.'/course/view.php', array('id' = $course->id)));

and make sure query string of url

lets assume: my course id = 10;

then my url would be /course/view.php?id=10

In reply to Dana T.

Re: a required parameter (courseid) was missing

by scott braithwaite -

you could do a check to see if you can get this to work, by adding to your url e.g. www.moodle.com/index.php the following so that it looks like this

www.moodle.com/index.php?id= 'your course id number here' and then on the page use $courseid = $_GET['your course id number here'] ;

 

if this works you will have to pass the course id through to the page that you are working on or change courseid to an optional parameter.

In reply to scott braithwaite

Re: a required parameter (courseid) was missing

by Dana T. -

Hi,

I am developing it now on a local server right now, before testing it. do you think your suggestion will work?

In reply to Dana T.

Re: a required parameter (courseid) was missing

by scott braithwaite -

it should work as this is a method that I have used numerous times

In reply to Dana T.

Re: a required parameter (courseid) was missing

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Please could you copy and paste the full URL of the page that is giving the error into here (you can remove your website name from the start, if you want to, it the the last bit that is important).

If you want it to work, your URL should look something like this:

[site url]/blocks/daterollover/edit.php?courseid=[XX]

(where XX is a number).

If there is no courseid=[XX] parameter in the URL, then the line of code:

$courseid = required_param('courseid', PARAM_INT);

will not work. That is the point of that line of code, to make sure the parameter is present and throw an error if it isn't.

In reply to Davo Smith

Re: a required parameter (courseid) was missing

by Dana T. -

Have patience with me guys, Here  is the url where my block resides,

localhost/moodle/course/view.php?id=2

Then I have a button in my block that will do the process and it will be redirected to

localhost/moodle/blocks/daterollover/edit.php

to do the process.

In reply to Dana T.

Re: a required parameter (courseid) was missing

by Dana T. -

Hi all,

As I said I am working on a local server, and I am trying to the best of my knowledge ....if anyone has the time to see it,  I have attached the whole code,  please do your suggestion to make it work ....

Thank you everyone for your help

In reply to Dana T.

Re: a required parameter (courseid) was missing

by Dana T. -

still waiting for someone to look at it......

In reply to Dana T.

Re: a required parameter (courseid) was missing

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If you want the edit.php file to have an 'id' param sent to it (with the courseid in it), then you are going to have to include it somewhere in the form that is posting to that page.

e.g.

global $COURSE;
$mform->addElement('hidden', 'id', $COURSE->id);

Your edit.php file also switches between $courseid (as gathered by the 'optional_param' line) and $course->id; you don't seem to have defined $course anywhere in your code. Did you mean to write something like:

$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);

 

Average of ratings: Useful (1)
In reply to Davo Smith

Re: a required parameter (courseid) was missing

by Dana T. -

Hi Davo,


Got no luck until now, but one question I have  which I didn't get it from the following line is , we are assigning $course, the auto incremented id from course table.

$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);

If it is, in the suggestion you gave me,

foreach($assignments as $assignment)

{

$record=new stdClass();

$record->id =$assignment->id; // is this line getting the auto incremented or the course id?

......

}

In reply to Dana T.

Re: a required parameter (courseid) was missing

by Dana T. -
Hello,
I am about to give up on this code, but my last try would be this.....I have the get_content() function  in (block_assignment_daterollover.php) which will be redirected into 'edit.php' , you can see the  'URL ' line below where it will be redirected, once it is redirected the page edit.php is coming with an empty white page, what do you think is wrong with it. This will be my last post. Would like to thank everyone for your help.

public function get_content() {
        global $CFG, $COURSE, $SESSION, $DB, $PAGE;
        if ($this->content !== null) {
            return $this->content;
        }
          
        $this->content->text = '';
        $context=$PAGE->context;
       $coursecontext = context->get_course_context();
        $courseid=$coursecontext->instanceid;

        if (has_capability('block/assignment_daterollover:changedate', $coursecontext)) {
            $url = new moodle_url('/blocks/assignment_daterollover/edit.php');
            $customdata = array('coursecontext' => $coursecontext);
            $mform = new assignment_daterollover_form($url->out(), $customdata);
            $form = $mform->display();
            $this->content->text .= $form;
        }

        $this->content->footer = '';
        return $this->content;
    }