moosh icon indicating copy to clipboard operation
moosh copied to clipboard

-v flag not working in course-create

Open bbusschots-mu opened this issue 11 years ago • 2 comments

It seems the library being used for commanline processing has problems dealing with booleans.

When trying to create a course that is not visible, -v 0 and -v '0' both generate the same exception:

PHP Fatal error: Uncaught exception 'GetOptionKit\Exception\RequireValueException' with message 'Option 'v' requires a value.' in /usr/local/moosh/vendor/c9s/GetOptionKit/src/GetOptionKit/ContinuousOptionParser.php:174 Stack trace: #0 /usr/local/moosh/vendor/c9s/GetOptionKit/src/GetOptionKit/ContinuousOptionParser.php(124): GetOptionKit\ContinuousOptionParser->parse(Array) #1 /usr/local/moosh/moosh.php(122): GetOptionKit\ContinuousOptionParser->continueParse() #2 {main}

thrown in /usr/local/moosh/vendor/c9s/GetOptionKit/src/GetOptionKit/ContinuousOptionParser.php on line 174

I also tried -v 'false' and -v N but they also don't work, giving Dabatbase errors instead:

Default exception handler: Error writing to database Debug: Incorrect integer value: 'N' for column 'visible' at row 1 INSERT INTO mdl_course (fullname,shortname,format,idnumber,visible,category,timecreated,timemodified,sortorder,visibleold) VALUES(?,?,?,?,?,?,?,?,?,?) [array ( 0 => 'TEST - All Students [2014-2015]', 1 => 'TEST', 2 => 'weeks', 3 => 'test_2015_allstudents', 4 => 'N', 5 => '361', 6 => 1403023635, 7 => 1403023635, 8 => 0, 9 => 'N', )] Error code: dmlwriteexception

  • line 446 of /lib/dml/moodle_database.php: dml_write_exception thrown
  • line 1080 of /lib/dml/mysqli_native_moodle_database.php: call to moodle_database->query_end()
  • line 1122 of /lib/dml/mysqli_native_moodle_database.php: call to mysqli_native_moodle_database->insert_record_raw()
  • line 2524 of /course/lib.php: call to mysqli_native_moodle_database->insert_record()
  • line 48 of /usr/local/moosh/Moosh/Command/Moodle23/Course/CourseCreate.php: call to create_course()
  • line 213 of /usr/local/moosh/moosh.php: call to Moosh\Command\Moodle23\Course\CourseCreate->execute()

!!! Error writing to database !!!

I had a look at the docs for php-GetOptionKit, and tried with v|visible:=i in the hope that that would make it see sense, but alas not, it still doesn't think 0 is an integer!

I have two suggestions for possible fixes, and would be happy to submit either as a patch:

  1. replace the single -v flag that needs a value with two flags that do not, -v for a visible course, and -V for an invisible course
  2. alter the code so the -v flag expects 'yes' or 'no' rather than 1 or 0, and translates to 1 or before executing the moodle function to actually add the course

Obviously the real problem is that GetOptionKit is being dumb, but since that's not part of this codebase, I suggest we need to hack around it, so that probably means one of the above two approaches.

bbusschots-mu avatar Jun 17 '14 17:06 bbusschots-mu

Hi, thanks again. I think you're right and I'd go for option 1. We could even keep only -v option that would set it to invisible. With no flag the default could stay as visible. I don't have very strong opinion here, I don't use it too often - so suit yourself, your patch is welcome.

I'll look at reporting it to GetOptionKit as well - unless you want to?

tmuras avatar Jun 17 '14 20:06 tmuras

I thought about this more overnight and decided on a modified version of option 2, where 1 continues to work as-is, but n and no get translated to 0 before being passed on. The example in the docs shows -v 1, so I figure that's probably in a lot of people's code, and hence should't be broken. I have a patch ready and tested and will submit it shortly.

If you don't mind I'll leave it to you to report the problem to GetOptionKit, I've only used the library very tangentially in my contributions here, so I don't feel I know enough about it to have an intelligent conversation with them.

bbusschots-mu avatar Jun 18 '14 14:06 bbusschots-mu