2016-10-07 7 views
1

Я пытаюсь создать разделы в пределах Курс Moodle с использованием сценария , но я не могу. Я использую функцию Moodle course_create_sections_if_missing, но не могу получить разделы, отображаемые в Интернете, но в базе данных, которые они создаются.Вставьте разделы в курс Moodle

код я использую следующим образом:

if($numSections >0){     

    foreach ($sections as $nameSection) { 

     $idSection = checkSections($moodle_course , $nameSection); 

     if($idSection == -1){         

      m("crear section: ". $nameSection); 

      $maxSection = getMaxSections($moodle_course); 

      if($maxSection != -1){        
       m("max section: " . $maxSection); 

       $idSec = $maxSection + 1; 
       course_create_sections_if_missing($moodle_course , $idSec); 

       $res = updateNameSection($moodle_course , $nameSection, $idSec); 

       m("result update: " . $res); 

      }else{ 
       m("There aren't sections for the course"); 
      } 

     }else{ 
      m("section already exists: ". $nameSection); 
     } 
    } 
}else{ 
    m("No sections for the course. "); 
} 

Примечание: М функция («») отвечает за отображение текста на консоли.

функция checkSections заключается в следующем:

/** 
* Check if there is a section. 
* 
* @param $course id of the course. 
* @param $section name of the section 
* 
* @return id of section 
*/ 
function checkSections($course , $section){ 

    global $DB; 
    $id = null; 

    $sql = "SELECT id FROM mdl_course_sections where course = ? and name = ? "; 
    $sections = $DB->get_records_sql($sql, array($course, $section)); 

    foreach ($sections as $r) {      
     $id = $r->id; 
    } 

    if(is_null($id)) 
     return -1; 
    else 
     return $id; 
} 

функция getMaxSections заключается в следующем:

/** 
* Returns the id of the highest section for a course. 
* 
* @param $course id of the course. 
* 
* @return max id of section 
*/ 
function getMaxSections($course){ 

    global $DB; 
    $id = null; 

    $sql = "SELECT max(section) as sec FROM mdl_course_sections where course = ? "; 
    $sections = $DB->get_records_sql($sql, array($course)); 

    foreach ($sections as $r) {      
     $id = $r->sec; 
    } 

    if(is_null($id)) 
     return -1; 
    else 
     return $id; 
} 

функция updateNameSection заключается в следующем:

/** 
* Update the name of a section . 
* 
* @param $course id of the course. 
* @param $nameSection name of the section. 
* @param $idSection id of the section. 
* 
* @return result 
*/ 
function updateNameSection($course, $nameSection , $idSection){ 

    global $DB; 
    $id = null; 

    $sql = "update mdl_course_sections set name = ? where course = ? and section = ?"; 
    $result = $DB->execute($sql, array($nameSection , $course, $idSection)); 

    return $result; 
} 

Так что если кто-нибудь знает, как это сделать, или у вас есть примеры или документация, которые могут быть полезны, было бы полезно.

Заранее спасибо.

ответ

1

Вам необходимо обновить номер секции на столе mdl_course_format_options

Попробуйте это:

$sql = "update mdl_course_format_options set value = ? where courseid = ? and name ='numsections'"; 
$result = $DB->execute($sql, array($numSections , $course)); 
+1

Я постараюсь, что вы предлагаете мне, спасибо очень – Joacer

+1

это работает отлично !!! Большое спасибо. – Joacer

1

Стараюсь код, который @aleix предложить и она отлично работает для меня, и это, как код:

if($numSections >0){     

    foreach ($sections as $nameSection) { 

     $idSection = checkSections($moodle_course , $nameSection); 

     if($idSection == -1){         

      m("crear section: ". $nameSection); 

      $maxSection = getMaxSections($moodle_course); 

      if($maxSection != -1){        
       m("max section: " . $maxSection); 

       $idSec = $maxSection + 1; 
       course_create_sections_if_missing($moodle_course , $idSec); 

       $res = updateNameSection($moodle_course , $nameSection, $idSec); 


       if($res){ 
        $idSec = $idSec + 1; 
        $res2 = updateNumSectionsCourse($comunidad_moodle, $idSec); 
       }   
      }else{ 
       m("There aren't sections for the course"); 
      } 

     }else{ 
      m("section already exists: ". $nameSection); 
     } 
    } 
}else{ 
    m("No sections for the course. "); 
} 

и функция updateNumSectionsCourse это:

/** 
* Update the number of sections for a course. 
* 
* @param $course id of the course. 
* @param $numSections number of sections. 
* 
* @return result 
*/ 
function updateNumSectionsCourse($course, $numSections){ 

    global $DB; 

    $sql = "update mdl_course_format_options set value = ? where courseid = ? and name ='numsections'"; 
    $result = $DB->execute($sql, array($numSections , $course)); 

    return $result; 
}