From 73b299c0586c370d496d3631be26fc0847d8b284 Mon Sep 17 00:00:00 2001
From: Francois Marier <francois@catalyst.net.nz>
Date: Thu, 6 Mar 2008 19:53:43 +1300
Subject: [PATCH] mod/certificate: [Oracle] Make sure the design parameters are not resetted everytime we edit the activity settings
Oracle doesn't like '0' or empty strings in nullable fields so let's make the none value actually be 'none'.
---
 mod/certificate/db/install.xml |   10 +++++-----
 mod/certificate/db/upgrade.php |   30 ++++++++++++++++++++++++++++++
 mod/certificate/lib.php        |   23 +++++++++++++----------
 mod/certificate/mod_form.php   |   12 ++++++------
 mod/certificate/version.php    |    2 +-
 5 files changed, 55 insertions(+), 22 deletions(-)

diff --git a/mod/certificate/db/install.xml b/mod/certificate/db/install.xml
index cea52e7..5fe242f 100644
--- a/mod/certificate/db/install.xml
+++ b/mod/certificate/db/install.xml
@@ -14,9 +14,9 @@
         <FIELD NAME="savecert" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="emailothers" NEXT="delivery"/>
         <FIELD NAME="delivery" TYPE="int" LENGTH="3" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="savecert" NEXT="certificatetype"/>
         <FIELD NAME="certificatetype" TYPE="char" LENGTH="50" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="delivery" NEXT="borderstyle"/>
-        <FIELD NAME="borderstyle" TYPE="char" LENGTH="30" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="certificatetype" NEXT="bordercolor"/>
-        <FIELD NAME="bordercolor" TYPE="char" LENGTH="30" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="borderstyle" NEXT="printwmark"/>
-        <FIELD NAME="printwmark" TYPE="char" LENGTH="30" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="bordercolor" NEXT="printdate"/>
+        <FIELD NAME="borderstyle" TYPE="char" LENGTH="30" NOTNULL="true" DEFAULT="none" SEQUENCE="false" ENUM="false" PREVIOUS="certificatetype" NEXT="bordercolor"/>
+        <FIELD NAME="bordercolor" TYPE="char" LENGTH="30" NOTNULL="true" DEFAULT="none" SEQUENCE="false" ENUM="false" PREVIOUS="borderstyle" NEXT="printwmark"/>
+        <FIELD NAME="printwmark" TYPE="char" LENGTH="30" NOTNULL="true" DEFAULT="none" SEQUENCE="false" ENUM="false" PREVIOUS="bordercolor" NEXT="printdate"/>
         <FIELD NAME="printdate" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="printwmark" NEXT="datefmt"/>
         <FIELD NAME="datefmt" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="printdate" NEXT="printnumber"/>
         <FIELD NAME="printnumber" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="datefmt" NEXT="printgrade"/>
@@ -27,8 +27,8 @@
         <FIELD NAME="requiredgrade" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="lockgrade" NEXT="printteacher"/>
         <FIELD NAME="printteacher" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="requiredgrade" NEXT="customtext"/>
         <FIELD NAME="customtext" TYPE="text" LENGTH="small" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="printteacher" NEXT="printsignature"/>
-        <FIELD NAME="printsignature" TYPE="char" LENGTH="30" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="customtext" NEXT="printseal"/>
-        <FIELD NAME="printseal" TYPE="char" LENGTH="30" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="printsignature" NEXT="timemodified"/>
+        <FIELD NAME="printsignature" TYPE="char" LENGTH="30" NOTNULL="true" DEFAULT="none" SEQUENCE="false" ENUM="false" PREVIOUS="customtext" NEXT="printseal"/>
+        <FIELD NAME="printseal" TYPE="char" LENGTH="30" NOTNULL="true" DEFAULT="none" SEQUENCE="false" ENUM="false" PREVIOUS="printsignature" NEXT="timemodified"/>
         <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="printseal" NEXT="title"/>
         <FIELD NAME="title" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false" COMMENT="Title to be displayed at the top of the certificate" PREVIOUS="timemodified" NEXT="coursename"/>
         <FIELD NAME="coursename" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false" COMMENT="Text to show instead of the course name on the certificate" PREVIOUS="title"/>
diff --git a/mod/certificate/db/upgrade.php b/mod/certificate/db/upgrade.php
index 5e4e8f8..7fa5ab8 100644
--- a/mod/certificate/db/upgrade.php
+++ b/mod/certificate/db/upgrade.php
@@ -99,6 +99,36 @@ function xmldb_certificate_upgrade($oldversion=0) {
         $result = $result && add_field($table, $field2);
     }
 
+    if ($result && $oldversion < 2007061305) {
+        /// Changing the default of fields on table certificate from '0' to 'none'
+        $table = new XMLDBTable('certificate');
+
+        $field1 = new XMLDBField('printsignature');
+        $field1->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, 'none', 'customtext');
+        $result = $result && change_field_default($table, $field1) &&
+            execute_sql("UPDATE {$CFG->prefix}certificate SET printsignature = 'none' WHERE printsignature = '0'");
+
+        $field2 = new XMLDBField('printwmark');
+        $field2->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, 'none', 'bordercolor');
+        $result = $result && change_field_default($table, $field2) &&
+            execute_sql("UPDATE {$CFG->prefix}certificate SET printwmark = 'none' WHERE printwmark = '0'");
+
+        $field3 = new XMLDBField('printseal');
+        $field3->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, 'none', 'printsignature');
+        $result = $result && change_field_default($table, $field3) &&
+            execute_sql("UPDATE {$CFG->prefix}certificate SET printseal = 'none' WHERE printseal = '0'");
+
+        $field4 = new XMLDBField('bordercolor');
+        $field4->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, 'none', 'borderstyle');
+        $result = $result && change_field_default($table, $field4) &&
+            execute_sql("UPDATE {$CFG->prefix}certificate SET bordercolor = 'none' WHERE bordercolor = '0'");
+
+        $field5 = new XMLDBField('borderstyle');
+        $field5->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, 'none', 'certificatetype');
+        $result = $result && change_field_default($table, $field5) &&
+            execute_sql("UPDATE {$CFG->prefix}certificate SET borderstyle = 'none' WHERE borderstyle = '0'");
+    }
+
     return $result;
 }
 
diff --git a/mod/certificate/lib.php b/mod/certificate/lib.php
index 7f3de80..cf7159a 100644
--- a/mod/certificate/lib.php
+++ b/mod/certificate/lib.php
@@ -758,9 +758,7 @@ function cert_printtext( $x, $y, $align, $font, $style, $size, $text) {
 function draw_frame($certificate, $orientation) {
     global $pdf, $certificate;
 
-    if($certificate->bordercolor == 0)    {
-    } else if($certificate->bordercolor > 0)    { //do nothing
-
+    if($certificate->bordercolor != 'none')    {
         switch ($orientation) {
             case 'L':
             
@@ -883,9 +881,7 @@ function draw_frame($certificate, $orientation) {
 function draw_frame_letter($certificate, $orientation) {
     global $pdf, $certificate;
 
-    if($certificate->bordercolor == 0)    {
-    } elseif($certificate->bordercolor > 0) { //do nothing
-    
+    if($certificate->bordercolor != 'none')    {
         switch ($orientation) {
             case 'L':
     // create outer line border in selected color
@@ -1007,6 +1003,7 @@ function print_border($border, $orientation) {
     switch($border) {
         case '0':
         case '':
+        case 'none':
         break;
         default:
         switch ($orientation) {
@@ -1034,6 +1031,7 @@ function print_border_letter($border, $orientation) {
     switch($border) {
         case '0':
         case '':
+        case 'none':
         break;
         default:
         switch ($orientation) {
@@ -1061,6 +1059,7 @@ function print_watermark($wmark, $orientation) {
     switch($wmark) {
         case '0':
         case '':
+        case 'none':
         break;
         default:
         switch ($orientation) {
@@ -1088,6 +1087,7 @@ function print_watermark_letter($wmark, $orientation) {
     switch($wmark) {
         case '0':
         case '':
+        case 'none':
         break;
         default:
         switch ($orientation) {
@@ -1117,6 +1117,7 @@ function print_signature($sig, $orientation, $x, $y, $w, $h) {
         switch($sig) {
             case '0':
             case '':
+            case 'none':
             break;
             default:
             if(file_exists("$CFG->dirroot/mod/certificate/pix/signatures/$sig")) {
@@ -1129,6 +1130,7 @@ function print_signature($sig, $orientation, $x, $y, $w, $h) {
         switch($sig) {
             case '0':
             case '':
+            case 'none':
             break;
             default:
             if(file_exists("$CFG->dirroot/mod/certificate/pix/signatures/$sig")) {
@@ -1149,6 +1151,7 @@ function print_seal($seal, $orientation, $x, $y, $w, $h) {
     switch($seal) {
         case '0':
         case '':
+        case 'none':
         break;
         default:
         switch ($orientation) {
@@ -1270,7 +1273,7 @@ function certificate_get_borders () {
     ksort($borderstyleoptions);
 
 /// Add default borders
-    $borderstyleoptions[0] = get_string('no');    
+    $borderstyleoptions['none'] = get_string('no');
     return $borderstyleoptions;
     }
 
@@ -1295,7 +1298,7 @@ function certificate_get_seals () {
     }
         ksort($sealoptions);
 
-    $sealoptions[0] = get_string('no');
+    $sealoptions['none'] = get_string('no');
     return $sealoptions;
     }
 /************************************************************************
@@ -1322,7 +1325,7 @@ function certificate_get_watermarks () {
 /// Order watermarks
     ksort($wmarkoptions);
 
-    $wmarkoptions[0] = get_string('no');
+    $wmarkoptions['none'] = get_string('no');
     return $wmarkoptions;
     
     }
@@ -1349,7 +1352,7 @@ function certificate_get_signatures () {
     }
     ksort($signatureoptions);
 
-    $signatureoptions[0] = get_string('no');
+    $signatureoptions['none'] = get_string('no');
     return $signatureoptions;
 }
 
diff --git a/mod/certificate/mod_form.php b/mod/certificate/mod_form.php
index ee09c71..9101d7e 100644
--- a/mod/certificate/mod_form.php
+++ b/mod/certificate/mod_form.php
@@ -120,27 +120,27 @@ class mod_certificate_mod_form extends moodleform_mod {
 
         $borderstyleoptions = certificate_get_borders();
 		$mform->addElement('select', 'borderstyle', get_string('borderstyle', 'certificate'), $borderstyleoptions);
-        $mform->setDefault('borderstyle', 0);
+        $mform->setDefault('borderstyle', 'none');
 	    $mform->setHelpButton('borderstyle', array('border', get_string('border', 'certificate'), 'certificate'));		
 
-        $printframe = array( 0 => get_string('no'), 1 => get_string('borderblack', 'certificate'), 2 => get_string('borderbrown', 'certificate'), 3 => get_string('borderblue', 'certificate'), 4 => get_string('bordergreen', 'certificate'));
+        $printframe = array( 'none' => get_string('no'), '1' => get_string('borderblack', 'certificate'), '2' => get_string('borderbrown', 'certificate'), '3' => get_string('borderblue', 'certificate'), '4' => get_string('bordergreen', 'certificate'));
         $mform->addElement('select', 'bordercolor', get_string('bordercolor', 'certificate'), $printframe);
-        $mform->setDefault('bordercolor', 0);
+        $mform->setDefault('bordercolor', 'none');
 	    $mform->setHelpButton('bordercolor', array('borderline', get_string('bordercolor', 'certificate'), 'certificate'));
 
         $wmarkoptions = certificate_get_watermarks();
         $mform->addElement('select', 'printwmark', get_string('printwmark', 'certificate'),$wmarkoptions);
-        $mform->setDefault('printwmark', 0);
+        $mform->setDefault('printwmark', 'none');
 	    $mform->setHelpButton('printwmark', array('watermark', get_string('printwmark', 'certificate'), 'certificate'));	
 
 		$signatureoptions = certificate_get_signatures ();
 		$mform->addElement('select', 'printsignature', get_string('printsignature', 'certificate'), $signatureoptions);
-        $mform->setDefault('printsignature', 0);
+        $mform->setDefault('printsignature', 'none');
 	    $mform->setHelpButton('printsignature', array('signature', get_string('printsignature', 'certificate'), 'certificate'));
 		
 		$sealoptions = certificate_get_seals();
 		$mform->addElement('select', 'printseal', get_string('printseal', 'certificate'),$sealoptions);
-        $mform->setDefault('printseal', 0);
+        $mform->setDefault('printseal', 'none');
 	    $mform->setHelpButton('printseal', array('seal', get_string('printseal', 'certificate'), 'certificate'));					
 //-------------------------------------------------------------------------------
 		$this->standard_coursemodule_elements();
diff --git a/mod/certificate/version.php b/mod/certificate/version.php
index de847ca..6478fbd 100644
--- a/mod/certificate/version.php
+++ b/mod/certificate/version.php
@@ -5,7 +5,7 @@
 ///  This fragment is called by moodle_needs_upgrading() and /admin/index.php
 ///////////////////////////////////////////////////////////////////////////////
 
-$module->version  = 2007061304;  // The current module version (Date: YYYYMMDDXX)
+$module->version  = 2007061305;  // The current module version (Date: YYYYMMDDXX)
 $module->requires = 2007021503;  // Requires this Moodle version
 $module->cron     = 0;           // Period for cron to check this module (secs)
 
-- 
1.5.2.5


