OSDN Git Service

d20cd6345a8452eb5b6a2ab0152b02b20fa5b937
[magic3/magic3.git] / widgets / m / quizk / include / container / m_quizkProgressWidgetContainer.php
1 <?php
2 /**
3  * index.php用コンテナクラス
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
9  * @package    Magic3 Framework
10  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11  * @copyright  Copyright 2006-2009 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: m_quizkProgressWidgetContainer.php 1933 2009-05-28 10:54:45Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getCurrentWidgetContainerPath() .    '/m_quizkBaseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() .   '/quizkDb.php');
18
19 class m_quizkProgressWidgetContainer extends m_quizkBaseWidgetContainer
20 {
21         private $db;    // DB接続オブジェクト
22         private $setId;                         // 定義セットID
23         const CFG_DEFAULT_SET_ID_KEY = 'current_set_id';                // 現在の選択中のセットID取得用キー
24         const CURRENT_TASK = 'progress';                // 現在の画面
25         const NEXT_TASK = 'complete';           // 次の画面
26
27         /**
28          * コンストラクタ
29          */
30         function __construct()
31         {
32                 // 親クラスを呼び出す
33                 parent::__construct();
34                 
35                 // DBオブジェクト作成
36                 $this->db = new quizkDb();
37         }
38         /**
39          * テンプレートファイルを設定
40          *
41          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
42          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
43          *
44          * @param RequestManager $request               HTTPリクエスト処理クラス
45          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
46          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
47          */
48         function _setTemplate($request, &$param)
49         {       
50                 return 'progress.tmpl.html';
51         }
52         /**
53          * テンプレートにデータ埋め込む
54          *
55          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
56          *
57          * @param RequestManager $request               HTTPリクエスト処理クラス
58          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
59          * @param                                                               なし
60          */
61         function _assign($request, &$param)
62         {
63                 $this->setId = $this->db->getConfig(self::CFG_DEFAULT_SET_ID_KEY);              // パターンセットID
64                 $act = $request->trimValueOf('act');
65                 
66                 if ($act == 'answer'){          // 回答したとき
67                         $postSetId = $request->trimValueOf('sid');
68                         $questionId = $request->trimValueOf('qid');
69                         $answer = $request->trimValueOf('answer');
70
71                         // 回答状況をチェック
72                         $isErr = false;         // エラー発生状況
73                         $isRight = false;       // 正解かどうか
74                         $ret = $this->db->getItem($postSetId, $questionId, $row);
75                         if ($ret){
76                                 if ($row['qd_type'] != 0) $isErr = true;                // エラー発生状況
77                                 $answerId = $row['qd_answer_id'];
78                         } else {
79                                 $isErr = true;          // エラー発生状況
80                         }
81                         if (!$isErr){
82                                 if (strcmp($answerId, $answer) == 0) $isRight = true;   // 正解かどうか
83                                 
84                                 $logSerial = $this->gEnv->getCurrentAccessLogSerial();
85                                 $ret = $this->db->addPostData($this->mobileId, $postSetId, $questionId, $answer, $isRight, $logSerial);
86                                 if (!$ret) $isErr = true;
87                         }
88                         // 回答を表示
89                         if (!$isErr){
90                                 $ret = $this->db->getItem($postSetId, $answerId, $row);
91                                 if ($ret){
92                                         // 選択結果を表示
93                                         if ($isRight){
94                                                 $result = '正解';
95                                         } else {
96                                                 $result = '不正解';
97                                                 $this->tmpl->setAttribute('result_msg', 'visibility', 'visible');
98                                         }
99                                         $this->tmpl->setAttribute('result_area', 'visibility', 'visible');
100                                         $this->tmpl->addVar("result_area", "result", $result);
101                                         $this->tmpl->addVar("result_area", "title", $row['qd_title']);
102                                         $this->tmpl->addVar("result_area", "content", $row['qd_content']);
103                                 } else {
104                                         $isErr = true;
105                                 }
106                         }
107                         // 次の問題へのリンクを作成
108                         if (!$isErr){
109                                 $ret = $this->db->getNextQuestion($this->setId, $this->mobileId, $row);
110                                 if ($ret){              // 問題が残っているとき
111                                         $this->tmpl->addVar("result_area", "next_name", '次へ');
112                                         $this->tmpl->addVar('result_area', 'next_url', $this->gEnv->createCurrentPageUrlForMobile('task=' . self::CURRENT_TASK));
113                                 } else {
114                                         $this->tmpl->addVar("result_area", "next_name", '終了');
115                                         $this->tmpl->addVar('result_area', 'next_url', $this->gEnv->createCurrentPageUrlForMobile('task=' . self::NEXT_TASK));
116                                 }                                       
117                         }
118                         
119                         if ($isErr){
120                                 $message = sprintf(self::ERR_MESSAGE_FORMAT, 'エラーが発生しました');
121                                 $this->tmpl->addVar("_widget", "message", $message);
122                         }
123                 } else {
124                         // 問題を取得
125                         $ret = $this->db->getNextQuestion($this->setId, $this->mobileId, $row);
126                         if ($ret){              // 問題が残っているとき
127                                 $this->tmpl->setAttribute('question_area', 'visibility', 'visible');
128                         
129                                 // クイズ問題を作成
130                                 $this->createQuestion($row);
131                         } else {                // 次の問題がないとき
132                                 $count = $this->db->getQuestionCount($this->setId);
133                                 if ($count == 0){
134                                         $message = '問題が登録されていません';
135                                 } else {
136                                         $message = '全問回答しました';
137                                 }
138                                 $this->tmpl->addVar("_widget", "message", $message);
139                                 $this->tmpl->setAttribute('view_status_area', 'visibility', 'visible');
140                         }
141                 }
142                 $this->tmpl->addVar('question_area', 'act', 'answer');
143                 $this->tmpl->addVar('question_area', 'url', $this->gEnv->createCurrentPageUrlForMobile('task=' . self::CURRENT_TASK));
144                 $this->tmpl->addVar('_widget', 'top_url', $this->gEnv->createCurrentPageUrlForMobile(''));
145                 $this->tmpl->addVar('_widget', 'start_url', $this->gEnv->createCurrentPageUrlForMobile('task=' . self::NEXT_TASK));
146         }
147         /**
148          * クイズ問題、回答を作成
149          *
150          * @param array $row                    クイズ問題項目レコード
151          * @return                                              なし
152          */
153         function createQuestion($row)
154         {
155                 $answer = $row['qd_select_answer_id'];          // 回答ID
156                 $answerArray = array();
157                 if (!empty($answer)) $answerArray = explode(';', $answer);
158                 
159                 // 問題を作成
160                 $title = $this->convertToDispString($row['qd_title']);
161                 $content = $this->convertToDispString($row['qd_content']);
162                 $this->tmpl->addVar('question_area', 'title', $title);
163                 $this->tmpl->addVar('question_area', 'content', $content);
164                 $this->tmpl->addVar('question_area', 'sid', $this->setId);
165                 $this->tmpl->addVar('question_area', 'qid', $this->convertToDispString($row['qd_id']));
166                 
167                 if (count($answerArray) == 0) return;
168                 
169                 // 回答を作成
170                 $ret = $this->db->getAnswers($this->setId, $answerArray, $rows);
171                 if ($ret){
172                         $inputTag = '';
173                         for ($i = 0; $i < count($rows); $i++){
174                                 $title = $this->convertToDispString($rows[$i]['qd_title']);
175                                 $value = $this->convertToDispString($rows[$i]['qd_id']);
176                                 $inputTag .= '<input type="radio" name="answer" value="' . $value . '" />' . $title . '<br />' . M3_NL;
177                         }
178                         $this->tmpl->addVar('question_area', 'answer', $inputTag);
179                 }
180         }
181 }
182 ?>