]> git.mjollnir.org Git - moodle.git/commitdiff
fix for MDL-6336
authortoyomoyo <toyomoyo>
Fri, 6 Oct 2006 08:26:37 +0000 (08:26 +0000)
committertoyomoyo <toyomoyo>
Fri, 6 Oct 2006 08:26:37 +0000 (08:26 +0000)
mod/lesson/db/migrate2utf8.php
mod/lesson/db/migrate2utf8.xml

index 338c556e5ffa12fe6b53a618a76156116d5afcab..6a7dd3a8a6a1a026b61a77367969e6e0c62ed8fc 100755 (executable)
@@ -1,4 +1,63 @@
 <?php // $Id$
+
+// fix for MDL-6336
+// handling serialized object
+function migrate2utf8_lesson_attempts_useranswer($recordid) {
+    global $CFG, $globallang;
+  
+    /// Some trivial checks
+    if (empty($recordid)) {
+        log_the_problem_somewhere();
+        return false;
+    }
+
+    $user = get_record_sql("SELECT la.userid
+           FROM {$CFG->prefix}lesson_attempts la
+           WHERE la.id=$recordid");
+
+    $course = get_record_sql("SELECT l.course
+           FROM {$CFG->prefix}lesson l,
+                {$CFG->prefix}lesson_attempts la
+           WHERE l.id = la.lessonid
+                 AND la.id = $recordid");
+  
+    if (!$lessonattempts = get_record('lesson_attempts','id',$recordid)) {
+        log_the_problem_somewhere();
+        return false;
+    }
+  
+    if ($globallang) {
+        $fromenc = $globallang;
+    } else {
+        $sitelang   = $CFG->lang;
+        $courselang = get_course_lang($course->course);
+        $userlang   = get_user_lang($user->userid);        
+        $fromencstudent = get_original_encoding($sitelang, $courselang, $userlang); // this is used for answer field
+        $userlang = get_main_teacher_lang($course->course);       
+        $fromencteacher = get_original_encoding($sitelang, $courselang, $userlang); // this is used for response field       
+    }
+    
+    $result = $lessonattempts->useranswer; // init to avoid warnings
+    // if unserialize success, meaning it is an object
+    if ($attempt = unserialize($lessonattempts->useranswer)) {
+        $attempt->answer = utfconvert($attempt->answer, $fromencstudent);
+        $attempt->response = utfconvert(attempt->response, $fromencteacher);
+        $newla = new object;
+        $newla->id = $recordid;
+        $newla->useranswer = serialize($attempt); // serialize it back    
+        migrate2utf8_update_record('lesson_attempts', $newla);
+      
+    } else { // just a string
+        $result = utfconvert($lessonattempts->useranswer, $fromencstudent);
+        $newla = new object;
+        $newla->id = $recordid;
+        $newla->useranswer =  $result;// serialize it back
+        migrate2utf8_update_record('lesson_attempts', $newla);
+    }
+    
+    return $result;
+}
+
 function migrate2utf8_lesson_answers_answer($recordid){
     global $CFG, $globallang;
 
index 0e4da1551e5823c97b6975fb83b7048154b83ad5..e06d6f7aca1e88d82dcf2c5596c552b307811554 100755 (executable)
@@ -5,19 +5,10 @@
     <TABLE name="lesson_timer" />
     <TABLE name="lesson_attempts">
       <FIELDS>
-        <FIELD name="useranswer" method="PLAIN_SQL_UPDATE" type="text" length="0">
-          <SQL_DETECT_USER>
-           SELECT la.userid
-           FROM {$CFG->prefix}lesson_attempts la
-           WHERE la.id=RECORDID
-          </SQL_DETECT_USER>
-          <SQL_DETECT_COURSE>
-           SELECT l.course
-           FROM {$CFG->prefix}lesson l,
-                {$CFG->prefix}lesson_attempts la
-           WHERE l.id = la.lessonid
-                 AND la.id = RECORDID
-          </SQL_DETECT_COURSE>
+        <FIELD name="useranswer" method="PHP_FUNCTION" type="text" length="0">
+          <PHP_FUNCTION>
+            migrate2utf8_lesson_attempts_useranswer(RECORDID)
+          </PHP_FUNCTION>
         </FIELD>
       </FIELDS>
     </TABLE>